Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY UCLASS_IOMMU |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame^] | 10 | #include <iommu.h> |
| 11 | #include <phys2bus.h> |
| 12 | #include <asm/io.h> |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 13 | |
| 14 | #if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 15 | int dev_iommu_enable(struct udevice *dev) |
| 16 | { |
| 17 | struct ofnode_phandle_args args; |
| 18 | struct udevice *dev_iommu; |
| 19 | int i, count, ret = 0; |
| 20 | |
| 21 | count = dev_count_phandle_with_args(dev, "iommus", |
| 22 | "#iommu-cells", 0); |
| 23 | for (i = 0; i < count; i++) { |
| 24 | ret = dev_read_phandle_with_args(dev, "iommus", |
| 25 | "#iommu-cells", 0, i, &args); |
| 26 | if (ret) { |
| 27 | debug("%s: dev_read_phandle_with_args failed: %d\n", |
| 28 | __func__, ret); |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | ret = uclass_get_device_by_ofnode(UCLASS_IOMMU, args.node, |
| 33 | &dev_iommu); |
| 34 | if (ret) { |
| 35 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
| 36 | __func__, ret); |
| 37 | return ret; |
| 38 | } |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame^] | 39 | dev->iommu = dev_iommu; |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | #endif |
| 45 | |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame^] | 46 | dma_addr_t dev_iommu_dma_map(struct udevice *dev, void *addr, size_t size) |
| 47 | { |
| 48 | const struct iommu_ops *ops; |
| 49 | |
| 50 | if (dev->iommu) { |
| 51 | ops = device_get_ops(dev->iommu); |
| 52 | if (ops && ops->map) |
| 53 | return ops->map(dev->iommu, addr, size); |
| 54 | } |
| 55 | |
| 56 | return dev_phys_to_bus(dev, virt_to_phys(addr)); |
| 57 | } |
| 58 | |
| 59 | void dev_iommu_dma_unmap(struct udevice *dev, dma_addr_t addr, size_t size) |
| 60 | { |
| 61 | const struct iommu_ops *ops; |
| 62 | |
| 63 | if (dev->iommu) { |
| 64 | ops = device_get_ops(dev->iommu); |
| 65 | if (ops && ops->unmap) |
| 66 | ops->unmap(dev->iommu, addr, size); |
| 67 | } |
| 68 | } |
| 69 | |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 70 | UCLASS_DRIVER(iommu) = { |
| 71 | .id = UCLASS_IOMMU, |
| 72 | .name = "iommu", |
| 73 | }; |