blob: 6babc0e3a67203ca7db2b6510380e995067a117a [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
8#include <common.h>
9#include <dm.h>
Mark Kettenis1c653ac2023-01-21 20:27:52 +010010#include <iommu.h>
Mark Kettenis97b8bf02023-01-21 20:27:56 +010011#include <malloc.h>
Mark Kettenis1c653ac2023-01-21 20:27:52 +010012#include <phys2bus.h>
13#include <asm/io.h>
Mark Kettenise1311b62021-10-23 16:58:01 +020014
15#if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Mark Kettenis97b8bf02023-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 Kettenise1311b62021-10-23 16:58:01 +020076int dev_iommu_enable(struct udevice *dev)
77{
78 struct ofnode_phandle_args args;
79 struct udevice *dev_iommu;
Caleb Connolly5cac4b82023-12-11 18:41:41 +000080 const struct iommu_ops *ops;
Mark Kettenise1311b62021-10-23 16:58:01 +020081 int i, count, ret = 0;
82
83 count = dev_count_phandle_with_args(dev, "iommus",
84 "#iommu-cells", 0);
85 for (i = 0; i < count; i++) {
86 ret = dev_read_phandle_with_args(dev, "iommus",
87 "#iommu-cells", 0, i, &args);
88 if (ret) {
89 debug("%s: dev_read_phandle_with_args failed: %d\n",
90 __func__, ret);
91 return ret;
92 }
93
94 ret = uclass_get_device_by_ofnode(UCLASS_IOMMU, args.node,
95 &dev_iommu);
96 if (ret) {
97 debug("%s: uclass_get_device_by_ofnode failed: %d\n",
98 __func__, ret);
99 return ret;
100 }
Mark Kettenis1c653ac2023-01-21 20:27:52 +0100101 dev->iommu = dev_iommu;
Caleb Connolly5cac4b82023-12-11 18:41:41 +0000102
103 if (dev->parent && dev->parent->iommu == dev_iommu)
104 continue;
105
106 ops = device_get_ops(dev->iommu);
107 if (ops && ops->connect) {
108 ret = ops->connect(dev);
109 if (ret)
110 return ret;
111 }
Mark Kettenise1311b62021-10-23 16:58:01 +0200112 }
113
Caleb Connollyed247d72023-12-11 18:41:40 +0000114#if CONFIG_IS_ENABLED(PCI)
115 if (count < 0 && device_is_on_pci_bus(dev))
Mark Kettenis97b8bf02023-01-21 20:27:56 +0100116 return dev_pci_iommu_enable(dev);
Caleb Connollyed247d72023-12-11 18:41:40 +0000117#endif
Mark Kettenis97b8bf02023-01-21 20:27:56 +0100118
Mark Kettenise1311b62021-10-23 16:58:01 +0200119 return 0;
120}
121#endif
122
Mark Kettenis1c653ac2023-01-21 20:27:52 +0100123dma_addr_t dev_iommu_dma_map(struct udevice *dev, void *addr, size_t size)
124{
125 const struct iommu_ops *ops;
126
127 if (dev->iommu) {
128 ops = device_get_ops(dev->iommu);
129 if (ops && ops->map)
130 return ops->map(dev->iommu, addr, size);
131 }
132
133 return dev_phys_to_bus(dev, virt_to_phys(addr));
134}
135
136void dev_iommu_dma_unmap(struct udevice *dev, dma_addr_t addr, size_t size)
137{
138 const struct iommu_ops *ops;
139
140 if (dev->iommu) {
141 ops = device_get_ops(dev->iommu);
142 if (ops && ops->unmap)
143 ops->unmap(dev->iommu, addr, size);
144 }
145}
146
Mark Kettenise1311b62021-10-23 16:58:01 +0200147UCLASS_DRIVER(iommu) = {
148 .id = UCLASS_IOMMU,
149 .name = "iommu",
150};