blob: bb31cd519d25d163bea32c1f4e0df58f6acb6129 [file] [log] [blame]
Mark Kettenise1311b62021-10-23 16:58:01 +02001// 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 Kettenise1311b62021-10-23 16:58:01 +02008#include <dm.h>
Mark Kettenis1c653ac2023-01-21 20:27:52 +01009#include <iommu.h>
Mark Kettenis97b8bf02023-01-21 20:27:56 +010010#include <malloc.h>
Mark Kettenis1c653ac2023-01-21 20:27:52 +010011#include <phys2bus.h>
12#include <asm/io.h>
Mark Kettenise1311b62021-10-23 16:58:01 +020013
14#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Mark Kettenis97b8bf02023-01-21 20:27:56 +010015
16#if CONFIG_IS_ENABLED(PCI)
17static 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 Kettenise1311b62021-10-23 16:58:01 +020075int dev_iommu_enable(struct udevice *dev)
76{
77 struct ofnode_phandle_args args;
78 struct udevice *dev_iommu;
Caleb Connolly5cac4b82023-12-11 18:41:41 +000079 const struct iommu_ops *ops;
Mark Kettenise1311b62021-10-23 16:58:01 +020080 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 Connolly7724a572024-01-04 17:12:22 +000088 log_err("%s: Failed to parse 'iommus' property for '%s': %d\n",
89 __func__, dev->name, ret);
Mark Kettenise1311b62021-10-23 16:58:01 +020090 return ret;
91 }
92
93 ret = uclass_get_device_by_ofnode(UCLASS_IOMMU, args.node,
94 &dev_iommu);
95 if (ret) {
Caleb Connolly7724a572024-01-04 17:12:22 +000096 log_err("%s: Failed to find IOMMU device for '%s': %d\n",
97 __func__, dev->name, ret);
Mark Kettenise1311b62021-10-23 16:58:01 +020098 return ret;
99 }
Mark Kettenis1c653ac2023-01-21 20:27:52 +0100100 dev->iommu = dev_iommu;
Caleb Connolly5cac4b82023-12-11 18:41:41 +0000101
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 Connolly7724a572024-01-04 17:12:22 +0000108 if (ret) {
109 log_err("%s: Failed to connect '%s' to IOMMU '%s': %d\n",
110 __func__, dev->name, dev->iommu->name, ret);
Caleb Connolly5cac4b82023-12-11 18:41:41 +0000111 return ret;
Caleb Connolly7724a572024-01-04 17:12:22 +0000112 }
Caleb Connolly5cac4b82023-12-11 18:41:41 +0000113 }
Mark Kettenise1311b62021-10-23 16:58:01 +0200114 }
115
Caleb Connollyed247d72023-12-11 18:41:40 +0000116#if CONFIG_IS_ENABLED(PCI)
117 if (count < 0 && device_is_on_pci_bus(dev))
Mark Kettenis97b8bf02023-01-21 20:27:56 +0100118 return dev_pci_iommu_enable(dev);
Caleb Connollyed247d72023-12-11 18:41:40 +0000119#endif
Mark Kettenis97b8bf02023-01-21 20:27:56 +0100120
Mark Kettenise1311b62021-10-23 16:58:01 +0200121 return 0;
122}
123#endif
124
Mark Kettenis1c653ac2023-01-21 20:27:52 +0100125dma_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
138void 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 Kettenise1311b62021-10-23 16:58:01 +0200149UCLASS_DRIVER(iommu) = {
150 .id = UCLASS_IOMMU,
151 .name = "iommu",
152};