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 | |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame] | 9 | #include <iommu.h> |
Mark Kettenis | 97b8bf0 | 2023-01-21 20:27:56 +0100 | [diff] [blame] | 10 | #include <malloc.h> |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame] | 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)) |
Mark Kettenis | 97b8bf0 | 2023-01-21 20:27:56 +0100 | [diff] [blame] | 15 | |
| 16 | #if CONFIG_IS_ENABLED(PCI) |
| 17 | static int dev_pci_iommu_enable(struct udevice *dev) |
| 18 | { |
| 19 | struct udevice *parent = dev->parent; |
| 20 | struct udevice *dev_iommu; |
| 21 | u32 *iommu_map; |
| 22 | u32 iommu_map_mask, length, phandle, rid, rid_base; |
| 23 | int i, count, len, ret; |
| 24 | |
| 25 | while (parent) { |
| 26 | len = dev_read_size(parent, "iommu-map"); |
| 27 | if (len > 0) |
| 28 | break; |
| 29 | parent = parent->parent; |
| 30 | } |
| 31 | |
| 32 | if (len <= 0) |
| 33 | return 0; |
| 34 | |
| 35 | iommu_map = malloc(len); |
| 36 | if (!iommu_map) |
| 37 | return -ENOMEM; |
| 38 | |
| 39 | count = len / sizeof(u32); |
| 40 | ret = dev_read_u32_array(parent, "iommu-map", iommu_map, count); |
| 41 | if (ret < 0) { |
| 42 | free(iommu_map); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | iommu_map_mask = dev_read_u32_default(parent, "iommu-map-mask", ~0); |
| 47 | rid = (dm_pci_get_bdf(dev) >> 8) & iommu_map_mask; |
| 48 | |
| 49 | /* Loop over entries until mapping is found. */ |
| 50 | for (i = 0; i < count; i += 4) { |
| 51 | rid_base = iommu_map[i]; |
| 52 | phandle = iommu_map[i + 1]; |
| 53 | length = iommu_map[i + 3]; |
| 54 | |
| 55 | if (rid < rid_base || rid >= rid_base + length) |
| 56 | continue; |
| 57 | |
| 58 | ret = uclass_get_device_by_phandle_id(UCLASS_IOMMU, phandle, |
| 59 | &dev_iommu); |
| 60 | if (ret) { |
| 61 | debug("%s: uclass_get_device_by_ofnode failed: %d\n", |
| 62 | __func__, ret); |
| 63 | free(iommu_map); |
| 64 | return ret; |
| 65 | } |
| 66 | dev->iommu = dev_iommu; |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | free(iommu_map); |
| 71 | return 0; |
| 72 | } |
| 73 | #endif |
| 74 | |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 75 | int dev_iommu_enable(struct udevice *dev) |
| 76 | { |
| 77 | struct ofnode_phandle_args args; |
| 78 | struct udevice *dev_iommu; |
Caleb Connolly | 5cac4b8 | 2023-12-11 18:41:41 +0000 | [diff] [blame] | 79 | const struct iommu_ops *ops; |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 80 | int i, count, ret = 0; |
| 81 | |
| 82 | count = dev_count_phandle_with_args(dev, "iommus", |
| 83 | "#iommu-cells", 0); |
| 84 | for (i = 0; i < count; i++) { |
| 85 | ret = dev_read_phandle_with_args(dev, "iommus", |
| 86 | "#iommu-cells", 0, i, &args); |
| 87 | if (ret) { |
Caleb Connolly | 7724a57 | 2024-01-04 17:12:22 +0000 | [diff] [blame] | 88 | log_err("%s: Failed to parse 'iommus' property for '%s': %d\n", |
| 89 | __func__, dev->name, ret); |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | ret = uclass_get_device_by_ofnode(UCLASS_IOMMU, args.node, |
| 94 | &dev_iommu); |
| 95 | if (ret) { |
Caleb Connolly | 7724a57 | 2024-01-04 17:12:22 +0000 | [diff] [blame] | 96 | log_err("%s: Failed to find IOMMU device for '%s': %d\n", |
| 97 | __func__, dev->name, ret); |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 98 | return ret; |
| 99 | } |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame] | 100 | dev->iommu = dev_iommu; |
Caleb Connolly | 5cac4b8 | 2023-12-11 18:41:41 +0000 | [diff] [blame] | 101 | |
| 102 | if (dev->parent && dev->parent->iommu == dev_iommu) |
| 103 | continue; |
| 104 | |
| 105 | ops = device_get_ops(dev->iommu); |
| 106 | if (ops && ops->connect) { |
| 107 | ret = ops->connect(dev); |
Caleb Connolly | 7724a57 | 2024-01-04 17:12:22 +0000 | [diff] [blame] | 108 | if (ret) { |
| 109 | log_err("%s: Failed to connect '%s' to IOMMU '%s': %d\n", |
| 110 | __func__, dev->name, dev->iommu->name, ret); |
Caleb Connolly | 5cac4b8 | 2023-12-11 18:41:41 +0000 | [diff] [blame] | 111 | return ret; |
Caleb Connolly | 7724a57 | 2024-01-04 17:12:22 +0000 | [diff] [blame] | 112 | } |
Caleb Connolly | 5cac4b8 | 2023-12-11 18:41:41 +0000 | [diff] [blame] | 113 | } |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Caleb Connolly | ed247d7 | 2023-12-11 18:41:40 +0000 | [diff] [blame] | 116 | #if CONFIG_IS_ENABLED(PCI) |
| 117 | if (count < 0 && device_is_on_pci_bus(dev)) |
Mark Kettenis | 97b8bf0 | 2023-01-21 20:27:56 +0100 | [diff] [blame] | 118 | return dev_pci_iommu_enable(dev); |
Caleb Connolly | ed247d7 | 2023-12-11 18:41:40 +0000 | [diff] [blame] | 119 | #endif |
Mark Kettenis | 97b8bf0 | 2023-01-21 20:27:56 +0100 | [diff] [blame] | 120 | |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | #endif |
| 124 | |
Mark Kettenis | 1c653ac | 2023-01-21 20:27:52 +0100 | [diff] [blame] | 125 | dma_addr_t dev_iommu_dma_map(struct udevice *dev, void *addr, size_t size) |
| 126 | { |
| 127 | const struct iommu_ops *ops; |
| 128 | |
| 129 | if (dev->iommu) { |
| 130 | ops = device_get_ops(dev->iommu); |
| 131 | if (ops && ops->map) |
| 132 | return ops->map(dev->iommu, addr, size); |
| 133 | } |
| 134 | |
| 135 | return dev_phys_to_bus(dev, virt_to_phys(addr)); |
| 136 | } |
| 137 | |
| 138 | void dev_iommu_dma_unmap(struct udevice *dev, dma_addr_t addr, size_t size) |
| 139 | { |
| 140 | const struct iommu_ops *ops; |
| 141 | |
| 142 | if (dev->iommu) { |
| 143 | ops = device_get_ops(dev->iommu); |
| 144 | if (ops && ops->unmap) |
| 145 | ops->unmap(dev->iommu, addr, size); |
| 146 | } |
| 147 | } |
| 148 | |
Mark Kettenis | e1311b6 | 2021-10-23 16:58:01 +0200 | [diff] [blame] | 149 | UCLASS_DRIVER(iommu) = { |
| 150 | .id = UCLASS_IOMMU, |
| 151 | .name = "iommu", |
| 152 | }; |