xref: /linux/drivers/acpi/loongarch/iovt.c (revision 09d28f8d47ce8fd8ac8da34e46abe560e48804e2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/acpi.h>
4 #include <linux/acpi_iovt.h>
5 #include <linux/pci.h>
6 #include "init.h"
7 
8 struct iovt_device_entry {
9 	struct list_head list;
10 	int start_devid, end_devid;
11 };
12 
13 struct iovt_fwnode {
14 	struct list_head list;
15 	struct fwnode_handle *fwnode;
16 	int flag;
17 	int nid, segment, devid;
18 	struct list_head ep_list;
19 };
20 
21 static bool checksum_valid;
22 static LIST_HEAD(iovt_fwnode_list);
23 static DEFINE_SPINLOCK(iovt_fwnode_lock);
24 
25 static u8 acpi_iovt_checksum(u8 *buffer, u32 len)
26 {
27 	u8 sum = 0, *end = buffer + len;
28 
29 	while (buffer < end)
30 		sum += *buffer++;
31 
32 	return sum;
33 }
34 
35 #ifdef CONFIG_PCI
36 static void __init iovt_enable_acs(struct acpi_iovt_iommu *iommu)
37 {
38 	static bool acs_enabled __initdata;
39 
40 	if (acs_enabled)
41 		return;
42 
43 	/* IOMMU V1 only supports PCI device management */
44 	if ((iommu->header.type == ACPI_IOVT_IOMMU_V1) ||
45 	    (iommu->flags & (ACPI_IOVT_IOMMU_PCI_DEVICE | ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT))) {
46 		pci_request_acs();
47 		acs_enabled = true;
48 	}
49 }
50 #else
51 static inline void iovt_enable_acs(struct acpi_iovt_iommu *iommu) { }
52 #endif
53 
54 static int __init iovt_get_pci_iommu_fwnode(struct iovt_fwnode *np, u16 segment, u16 bdf)
55 {
56 	struct pci_dev *pdev;
57 	struct fwnode_handle *fwnode;
58 
59 	pdev = pci_get_domain_bus_and_slot(segment, PCI_BUS_NUM(bdf), bdf & 0xff);
60 	if (!pdev) {
61 		pr_err("No PCI IOMMU found for segment 0x%x bdf 0x%x\n", segment, bdf);
62 		return -ENODEV;
63 	}
64 
65 	fwnode = dev_fwnode(&pdev->dev);
66 	if (!fwnode) {
67 		/*
68 		 * PCI devices aren't necessarily described by ACPI. Create
69 		 * a fwnode so the IOMMU subsystem can identify this device.
70 		 */
71 		fwnode = acpi_alloc_fwnode_static();
72 		if (!fwnode) {
73 			pci_dev_put(pdev);
74 			return -ENOMEM;
75 		}
76 		set_primary_fwnode(&pdev->dev, fwnode);
77 	}
78 
79 	np->fwnode = dev_fwnode(&pdev->dev);
80 	if (np->flag & ACPI_IOVT_IOMMU_PXM_VALID)
81 		set_dev_node(&pdev->dev, np->nid);
82 	pci_dev_put(pdev);
83 
84 	return 0;
85 }
86 
87 static int __init iovt_validate_entries(struct acpi_iovt_iommu *iommu)
88 {
89 	int i, done = 1;
90 	struct acpi_iovt_device_entry *ep;
91 
92 	ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, iommu, iommu->device_entry_offset);
93 	for (i = 0; i < iommu->device_entry_num; i++) {
94 		switch (ep->type) {
95 		case ACPI_IOVT_DEVICE_ENTRY_START:
96 			if (!done)
97 				return -EINVAL;
98 
99 			done = 0;
100 			break;
101 		case ACPI_IOVT_DEVICE_ENTRY_END:
102 			if (done)
103 				return -EINVAL;
104 
105 			done = 1;
106 			break;
107 		case ACPI_IOVT_DEVICE_ENTRY_SINGLE:
108 		default:  /* Reserved for future */
109 			if (!done)
110 				return -EINVAL;
111 
112 			done = 1;
113 			break;
114 		}
115 		ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, ep, ep->length);
116 	}
117 
118 	return done ? 0 : -EINVAL;
119 }
120 
121 static int __init iovt_add_iommu(struct acpi_iovt_iommu *iommu)
122 {
123 	int i, ret;
124 	struct iovt_fwnode *np;
125 	struct iovt_device_entry *entry;
126 	struct acpi_iovt_device_entry *ep;
127 
128 	np = kzalloc_obj(struct iovt_fwnode, GFP_ATOMIC);
129 	if (WARN_ON(!np))
130 		return -ENOMEM;
131 
132 	INIT_LIST_HEAD(&np->list);
133 	np->flag = iommu->flags;
134 	np->segment = iommu->segment;
135 	if (np->flag & ACPI_IOVT_IOMMU_PXM_VALID)
136 		np->nid = pxm_to_node(iommu->proximity_domain);
137 
138 	if (np->flag & ACPI_IOVT_IOMMU_PCI_DEVICE) {
139 		np->devid = iommu->device_id;
140 		ret = iovt_get_pci_iommu_fwnode(np, np->segment, np->devid);
141 		if (ret < 0) {
142 			kfree(np);
143 			return ret;
144 		}
145 	} else {
146 		np->fwnode = acpi_alloc_fwnode_static();
147 		if (!np->fwnode) {
148 			kfree(np);
149 			return -ENOMEM;
150 		}
151 	}
152 
153 	/* All devices in the segment are managed by this IOMMU */
154 	if (np->flag & ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT)
155 		goto skip;
156 
157 	if (iovt_validate_entries(iommu) < 0) {
158 		kfree(np);
159 		return -EINVAL;
160 	}
161 
162 	INIT_LIST_HEAD(&np->ep_list);
163 	ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, iommu, iommu->device_entry_offset);
164 	for (i = 0; i < iommu->device_entry_num; i++) {
165 		switch (ep->type) {
166 		case ACPI_IOVT_DEVICE_ENTRY_START:
167 			entry = kzalloc_obj(struct iovt_device_entry, GFP_ATOMIC);
168 			if (!entry)
169 				return -ENOMEM;
170 
171 			entry->start_devid = ep->device_id;
172 			break;
173 		case ACPI_IOVT_DEVICE_ENTRY_END:
174 			/* START & END are paired */
175 			entry->end_devid = ep->device_id;
176 			list_add_tail(&entry->list, &np->ep_list);
177 			break;
178 		case ACPI_IOVT_DEVICE_ENTRY_SINGLE:
179 			entry = kzalloc_obj(struct iovt_device_entry, GFP_ATOMIC);
180 			if (!entry)
181 				return -ENOMEM;
182 
183 			entry->start_devid = ep->device_id;
184 			entry->end_devid = ep->device_id;
185 			list_add_tail(&entry->list, &np->ep_list);
186 			break;
187 		default:
188 			break;
189 		}
190 		ep = ACPI_ADD_PTR(struct acpi_iovt_device_entry, ep, ep->length);
191 	}
192 
193 skip:
194 	spin_lock(&iovt_fwnode_lock);
195 	list_add_tail(&np->list, &iovt_fwnode_list);
196 	spin_unlock(&iovt_fwnode_lock);
197 	return 0;
198 }
199 
200 static void __init iovt_init_devices(struct acpi_table_header *header)
201 {
202 	int i;
203 	struct acpi_table_iovt *iovt;
204 	struct acpi_iovt_iommu *iommu;
205 
206 	/* Get the first IOVT node */
207 	iovt = (struct acpi_table_iovt *)header;
208 	iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iovt, iovt->iommu_offset);
209 	for (i = 0; i < iovt->iommu_count; i++) {
210 		iovt_add_iommu(iommu);
211 		iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iommu, iommu->header.length);
212 	}
213 }
214 
215 void __init acpi_iovt_init(void)
216 {
217 	int i;
218 	acpi_status status;
219 	struct acpi_table_header *hdr;
220 	struct acpi_table_iovt *iovt;
221 	struct acpi_iovt_iommu *iommu;
222 
223 	status = acpi_get_table(ACPI_SIG_IOVT, 0, &hdr);
224 	if (ACPI_FAILURE(status)) {
225 		if (status != AE_NOT_FOUND)
226 			pr_err("Failed to get table, %s\n", acpi_format_exception(status));
227 
228 		return;
229 	}
230 
231 	if (acpi_iovt_checksum((unsigned char *)hdr, hdr->length)) {
232 		pr_err("IOVT invalid checksum\n");
233 		acpi_put_table(hdr);
234 		return;
235 	}
236 
237 	checksum_valid = true;
238 	iovt = (struct acpi_table_iovt *)hdr;
239 	iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iovt, iovt->iommu_offset);
240 	for (i = 0; i < iovt->iommu_count; i++) {
241 		iovt_enable_acs(iommu);
242 		iommu = ACPI_ADD_PTR(struct acpi_iovt_iommu, iommu, iommu->header.length);
243 	}
244 
245 	acpi_put_table(hdr);
246 }
247 
248 void __init acpi_iovt_late_init(void)
249 {
250 	acpi_status status;
251 	struct acpi_table_header *hdr;
252 
253 	if (!checksum_valid)
254 		return;
255 
256 	status = acpi_get_table(ACPI_SIG_IOVT, 0, &hdr);
257 	if (ACPI_FAILURE(status)) {
258 		if (status != AE_NOT_FOUND)
259 			pr_err("Failed to get table, %s\n", acpi_format_exception(status));
260 
261 		return;
262 	}
263 
264 	iovt_init_devices(hdr);
265 	acpi_put_table(hdr);
266 }
267 
268 static int iovt_pci_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
269 {
270 	struct iovt_fwnode *np;
271 	struct iovt_device_entry *entry;
272 	struct device *aliased_dev = data;
273 	u32 domain = pci_domain_nr(pdev->bus);
274 
275 	list_for_each_entry(np, &iovt_fwnode_list, list) {
276 		if (domain != np->segment)
277 			continue;
278 
279 		/* We're not translating ourself */
280 		if (dev_id == np->devid)
281 			return -EINVAL;
282 
283 		if (np->flag & ACPI_IOVT_IOMMU_MAGAGE_BY_SEGMENT)
284 			goto found;
285 
286 		list_for_each_entry(entry, &np->ep_list, list) {
287 			if (dev_id >= entry->start_devid && dev_id <= entry->end_devid)
288 				goto found;
289 		}
290 	}
291 
292 	return -ENODEV;
293 
294 found:
295 	return acpi_iommu_fwspec_init(aliased_dev, (domain << 16) + dev_id, np->fwnode);
296 }
297 
298 /*
299  * iovt_iommu_configure_id - Set-up IOMMU configuration for a device.
300  *
301  * @dev: device to configure
302  * @id_in: optional input id const value pointer
303  *
304  * Returns: 0 on success, <0 on failure
305  */
306 int iovt_iommu_configure_id(struct device *dev, const u32 *id_in)
307 {
308 	if (!dev_is_pci(dev))
309 		return -ENODEV;
310 
311 	return pci_for_each_dma_alias(to_pci_dev(dev), iovt_pci_iommu_init, dev);
312 }
313