blob: 98731d5e2c440722a0dca7989d066111d9f38f40 [file] [log] [blame]
Mark Kettenis40dbf032021-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
8#include <common.h>
9#include <dm.h>
Mark Kettenisdd6b68e2023-01-21 20:27:52 +010010#include <iommu.h>
Mark Kettenis1fc23292023-01-21 20:27:56 +010011#include <malloc.h>
Mark Kettenisdd6b68e2023-01-21 20:27:52 +010012#include <phys2bus.h>
13#include <asm/io.h>
Mark Kettenis40dbf032021-10-23 16:58:01 +020014
15#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Mark Kettenis1fc23292023-01-21 20:27:56 +010016
17#if CONFIG_IS_ENABLED(PCI)
18static int dev_pci_iommu_enable(struct udevice *dev)
19{
20 struct udevice *parent = dev->parent;
21 struct udevice *dev_iommu;
22 u32 *iommu_map;
23 u32 iommu_map_mask, length, phandle, rid, rid_base;
24 int i, count, len, ret;
25
26 while (parent) {
27 len = dev_read_size(parent, "iommu-map");
28 if (len > 0)
29 break;
30 parent = parent->parent;
31 }
32
33 if (len <= 0)
34 return 0;
35
36 iommu_map = malloc(len);
37 if (!iommu_map)
38 return -ENOMEM;
39
40 count = len / sizeof(u32);
41 ret = dev_read_u32_array(parent, "iommu-map", iommu_map, count);
42 if (ret < 0) {
43 free(iommu_map);
44 return 0;
45 }
46
47 iommu_map_mask = dev_read_u32_default(parent, "iommu-map-mask", ~0);
48 rid = (dm_pci_get_bdf(dev) >> 8) & iommu_map_mask;
49
50 /* Loop over entries until mapping is found. */
51 for (i = 0; i < count; i += 4) {
52 rid_base = iommu_map[i];
53 phandle = iommu_map[i + 1];
54 length = iommu_map[i + 3];
55
56 if (rid < rid_base || rid >= rid_base + length)
57 continue;
58
59 ret = uclass_get_device_by_phandle_id(UCLASS_IOMMU, phandle,
60 &dev_iommu);
61 if (ret) {
62 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
63 __func__, ret);
64 free(iommu_map);
65 return ret;
66 }
67 dev->iommu = dev_iommu;
68 break;
69 }
70
71 free(iommu_map);
72 return 0;
73}
74#endif
75
Mark Kettenis40dbf032021-10-23 16:58:01 +020076int dev_iommu_enable(struct udevice *dev)
77{
78 struct ofnode_phandle_args args;
79 struct udevice *dev_iommu;
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) {
88 debug("%s: dev_read_phandle_with_args failed: %d\n",
89 __func__, ret);
90 return ret;
91 }
92
93 ret = uclass_get_device_by_ofnode(UCLASS_IOMMU, args.node,
94 &dev_iommu);
95 if (ret) {
96 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
97 __func__, ret);
98 return ret;
99 }
Mark Kettenisdd6b68e2023-01-21 20:27:52 +0100100 dev->iommu = dev_iommu;
Mark Kettenis40dbf032021-10-23 16:58:01 +0200101 }
102
Caleb Connollyd24e9962023-09-12 21:03:05 +0100103#if CONFIG_IS_ENABLED(PCI)
104 if (count < 0 && device_is_on_pci_bus(dev))
Mark Kettenis1fc23292023-01-21 20:27:56 +0100105 return dev_pci_iommu_enable(dev);
Caleb Connollyd24e9962023-09-12 21:03:05 +0100106#endif
Mark Kettenis1fc23292023-01-21 20:27:56 +0100107
Mark Kettenis40dbf032021-10-23 16:58:01 +0200108 return 0;
109}
110#endif
111
Mark Kettenisdd6b68e2023-01-21 20:27:52 +0100112dma_addr_t dev_iommu_dma_map(struct udevice *dev, void *addr, size_t size)
113{
114 const struct iommu_ops *ops;
115
116 if (dev->iommu) {
117 ops = device_get_ops(dev->iommu);
118 if (ops && ops->map)
119 return ops->map(dev->iommu, addr, size);
120 }
121
122 return dev_phys_to_bus(dev, virt_to_phys(addr));
123}
124
125void dev_iommu_dma_unmap(struct udevice *dev, dma_addr_t 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->unmap)
132 ops->unmap(dev->iommu, addr, size);
133 }
134}
135
Mark Kettenis40dbf032021-10-23 16:58:01 +0200136UCLASS_DRIVER(iommu) = {
137 .id = UCLASS_IOMMU,
138 .name = "iommu",
139};