]>
Commit | Line | Data |
---|---|---|
fc2100eb JR |
1 | /* |
2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. | |
63ce3ae8 | 3 | * Author: Joerg Roedel <jroedel@suse.de> |
fc2100eb JR |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published | |
7 | * by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
92e7066f | 19 | #define pr_fmt(fmt) "iommu: " fmt |
7d3002cc | 20 | |
905d66c1 | 21 | #include <linux/device.h> |
40998188 | 22 | #include <linux/kernel.h> |
fc2100eb JR |
23 | #include <linux/bug.h> |
24 | #include <linux/types.h> | |
60db4027 AM |
25 | #include <linux/module.h> |
26 | #include <linux/slab.h> | |
fc2100eb JR |
27 | #include <linux/errno.h> |
28 | #include <linux/iommu.h> | |
d72e31c9 AW |
29 | #include <linux/idr.h> |
30 | #include <linux/notifier.h> | |
31 | #include <linux/err.h> | |
104a1c13 | 32 | #include <linux/pci.h> |
f096c061 | 33 | #include <linux/bitops.h> |
57f98d2f | 34 | #include <linux/property.h> |
7f6db171 | 35 | #include <trace/events/iommu.h> |
d72e31c9 AW |
36 | |
37 | static struct kset *iommu_group_kset; | |
e38d1f13 | 38 | static DEFINE_IDA(iommu_group_ida); |
d806b39f | 39 | static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA; |
d72e31c9 | 40 | |
b22f6434 TR |
41 | struct iommu_callback_data { |
42 | const struct iommu_ops *ops; | |
43 | }; | |
44 | ||
d72e31c9 AW |
45 | struct iommu_group { |
46 | struct kobject kobj; | |
47 | struct kobject *devices_kobj; | |
48 | struct list_head devices; | |
49 | struct mutex mutex; | |
50 | struct blocking_notifier_head notifier; | |
51 | void *iommu_data; | |
52 | void (*iommu_data_release)(void *iommu_data); | |
53 | char *name; | |
54 | int id; | |
53723dc5 | 55 | struct iommu_domain *default_domain; |
e39cb8a3 | 56 | struct iommu_domain *domain; |
d72e31c9 AW |
57 | }; |
58 | ||
198706b5 | 59 | struct group_device { |
d72e31c9 AW |
60 | struct list_head list; |
61 | struct device *dev; | |
62 | char *name; | |
63 | }; | |
64 | ||
65 | struct iommu_group_attribute { | |
66 | struct attribute attr; | |
67 | ssize_t (*show)(struct iommu_group *group, char *buf); | |
68 | ssize_t (*store)(struct iommu_group *group, | |
69 | const char *buf, size_t count); | |
70 | }; | |
71 | ||
cf610ca1 EA |
72 | static const char * const iommu_group_resv_type_string[] = { |
73 | [IOMMU_RESV_DIRECT] = "direct", | |
74 | [IOMMU_RESV_RESERVED] = "reserved", | |
75 | [IOMMU_RESV_MSI] = "msi", | |
76 | }; | |
77 | ||
d72e31c9 AW |
78 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
79 | struct iommu_group_attribute iommu_group_attr_##_name = \ | |
80 | __ATTR(_name, _mode, _show, _store) | |
fc2100eb | 81 | |
d72e31c9 AW |
82 | #define to_iommu_group_attr(_attr) \ |
83 | container_of(_attr, struct iommu_group_attribute, attr) | |
84 | #define to_iommu_group(_kobj) \ | |
85 | container_of(_kobj, struct iommu_group, kobj) | |
fc2100eb | 86 | |
f6cbf74b JR |
87 | static LIST_HEAD(iommu_device_list); |
88 | static DEFINE_SPINLOCK(iommu_device_lock); | |
89 | ||
90 | int iommu_device_register(struct iommu_device *iommu) | |
91 | { | |
92 | spin_lock(&iommu_device_lock); | |
93 | list_add_tail(&iommu->list, &iommu_device_list); | |
94 | spin_unlock(&iommu_device_lock); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | void iommu_device_unregister(struct iommu_device *iommu) | |
100 | { | |
101 | spin_lock(&iommu_device_lock); | |
102 | list_del(&iommu->list); | |
103 | spin_unlock(&iommu_device_lock); | |
104 | } | |
105 | ||
53723dc5 JR |
106 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
107 | unsigned type); | |
e39cb8a3 JR |
108 | static int __iommu_attach_device(struct iommu_domain *domain, |
109 | struct device *dev); | |
110 | static int __iommu_attach_group(struct iommu_domain *domain, | |
111 | struct iommu_group *group); | |
112 | static void __iommu_detach_group(struct iommu_domain *domain, | |
113 | struct iommu_group *group); | |
53723dc5 | 114 | |
d806b39f WD |
115 | static int __init iommu_set_def_domain_type(char *str) |
116 | { | |
117 | bool pt; | |
118 | ||
119 | if (!str || strtobool(str, &pt)) | |
120 | return -EINVAL; | |
121 | ||
122 | iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA; | |
123 | return 0; | |
124 | } | |
125 | early_param("iommu.passthrough", iommu_set_def_domain_type); | |
126 | ||
d72e31c9 AW |
127 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
128 | struct attribute *__attr, char *buf) | |
1460432c | 129 | { |
d72e31c9 AW |
130 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
131 | struct iommu_group *group = to_iommu_group(kobj); | |
132 | ssize_t ret = -EIO; | |
1460432c | 133 | |
d72e31c9 AW |
134 | if (attr->show) |
135 | ret = attr->show(group, buf); | |
136 | return ret; | |
137 | } | |
138 | ||
139 | static ssize_t iommu_group_attr_store(struct kobject *kobj, | |
140 | struct attribute *__attr, | |
141 | const char *buf, size_t count) | |
142 | { | |
143 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); | |
144 | struct iommu_group *group = to_iommu_group(kobj); | |
145 | ssize_t ret = -EIO; | |
1460432c | 146 | |
d72e31c9 AW |
147 | if (attr->store) |
148 | ret = attr->store(group, buf, count); | |
149 | return ret; | |
1460432c | 150 | } |
1460432c | 151 | |
d72e31c9 AW |
152 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
153 | .show = iommu_group_attr_show, | |
154 | .store = iommu_group_attr_store, | |
155 | }; | |
1460432c | 156 | |
d72e31c9 AW |
157 | static int iommu_group_create_file(struct iommu_group *group, |
158 | struct iommu_group_attribute *attr) | |
159 | { | |
160 | return sysfs_create_file(&group->kobj, &attr->attr); | |
1460432c | 161 | } |
1460432c | 162 | |
d72e31c9 AW |
163 | static void iommu_group_remove_file(struct iommu_group *group, |
164 | struct iommu_group_attribute *attr) | |
165 | { | |
166 | sysfs_remove_file(&group->kobj, &attr->attr); | |
167 | } | |
168 | ||
169 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) | |
170 | { | |
171 | return sprintf(buf, "%s\n", group->name); | |
172 | } | |
173 | ||
c7141f87 EA |
174 | /** |
175 | * iommu_insert_resv_region - Insert a new region in the | |
176 | * list of reserved regions. | |
177 | * @new: new region to insert | |
178 | * @regions: list of regions | |
179 | * | |
180 | * The new element is sorted by address with respect to the other | |
181 | * regions of the same type. In case it overlaps with another | |
182 | * region of the same type, regions are merged. In case it | |
183 | * overlaps with another region of different type, regions are | |
184 | * not merged. | |
185 | */ | |
186 | static int iommu_insert_resv_region(struct iommu_resv_region *new, | |
187 | struct list_head *regions) | |
188 | { | |
189 | struct iommu_resv_region *region; | |
190 | phys_addr_t start = new->start; | |
191 | phys_addr_t end = new->start + new->length - 1; | |
192 | struct list_head *pos = regions->next; | |
193 | ||
194 | while (pos != regions) { | |
195 | struct iommu_resv_region *entry = | |
196 | list_entry(pos, struct iommu_resv_region, list); | |
197 | phys_addr_t a = entry->start; | |
198 | phys_addr_t b = entry->start + entry->length - 1; | |
199 | int type = entry->type; | |
200 | ||
201 | if (end < a) { | |
202 | goto insert; | |
203 | } else if (start > b) { | |
204 | pos = pos->next; | |
205 | } else if ((start >= a) && (end <= b)) { | |
206 | if (new->type == type) | |
207 | goto done; | |
208 | else | |
209 | pos = pos->next; | |
210 | } else { | |
211 | if (new->type == type) { | |
212 | phys_addr_t new_start = min(a, start); | |
213 | phys_addr_t new_end = max(b, end); | |
214 | ||
215 | list_del(&entry->list); | |
216 | entry->start = new_start; | |
217 | entry->length = new_end - new_start + 1; | |
218 | iommu_insert_resv_region(entry, regions); | |
219 | } else { | |
220 | pos = pos->next; | |
221 | } | |
222 | } | |
223 | } | |
224 | insert: | |
225 | region = iommu_alloc_resv_region(new->start, new->length, | |
226 | new->prot, new->type); | |
227 | if (!region) | |
228 | return -ENOMEM; | |
229 | ||
230 | list_add_tail(®ion->list, pos); | |
231 | done: | |
232 | return 0; | |
233 | } | |
234 | ||
235 | static int | |
236 | iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, | |
237 | struct list_head *group_resv_regions) | |
238 | { | |
239 | struct iommu_resv_region *entry; | |
88178a77 | 240 | int ret = 0; |
c7141f87 EA |
241 | |
242 | list_for_each_entry(entry, dev_resv_regions, list) { | |
243 | ret = iommu_insert_resv_region(entry, group_resv_regions); | |
244 | if (ret) | |
245 | break; | |
246 | } | |
247 | return ret; | |
248 | } | |
249 | ||
250 | int iommu_get_group_resv_regions(struct iommu_group *group, | |
251 | struct list_head *head) | |
252 | { | |
198706b5 | 253 | struct group_device *device; |
c7141f87 EA |
254 | int ret = 0; |
255 | ||
256 | mutex_lock(&group->mutex); | |
257 | list_for_each_entry(device, &group->devices, list) { | |
258 | struct list_head dev_resv_regions; | |
259 | ||
260 | INIT_LIST_HEAD(&dev_resv_regions); | |
261 | iommu_get_resv_regions(device->dev, &dev_resv_regions); | |
262 | ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); | |
263 | iommu_put_resv_regions(device->dev, &dev_resv_regions); | |
264 | if (ret) | |
265 | break; | |
266 | } | |
267 | mutex_unlock(&group->mutex); | |
268 | return ret; | |
269 | } | |
270 | EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); | |
271 | ||
cf610ca1 EA |
272 | static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, |
273 | char *buf) | |
274 | { | |
275 | struct iommu_resv_region *region, *next; | |
276 | struct list_head group_resv_regions; | |
277 | char *str = buf; | |
278 | ||
279 | INIT_LIST_HEAD(&group_resv_regions); | |
280 | iommu_get_group_resv_regions(group, &group_resv_regions); | |
281 | ||
282 | list_for_each_entry_safe(region, next, &group_resv_regions, list) { | |
283 | str += sprintf(str, "0x%016llx 0x%016llx %s\n", | |
284 | (long long int)region->start, | |
285 | (long long int)(region->start + | |
286 | region->length - 1), | |
287 | iommu_group_resv_type_string[region->type]); | |
288 | kfree(region); | |
289 | } | |
290 | ||
291 | return (str - buf); | |
292 | } | |
293 | ||
d72e31c9 AW |
294 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
295 | ||
cf610ca1 EA |
296 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
297 | iommu_group_show_resv_regions, NULL); | |
298 | ||
d72e31c9 AW |
299 | static void iommu_group_release(struct kobject *kobj) |
300 | { | |
301 | struct iommu_group *group = to_iommu_group(kobj); | |
302 | ||
269aa808 JR |
303 | pr_debug("Releasing group %d\n", group->id); |
304 | ||
d72e31c9 AW |
305 | if (group->iommu_data_release) |
306 | group->iommu_data_release(group->iommu_data); | |
307 | ||
feccf398 | 308 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 | 309 | |
53723dc5 JR |
310 | if (group->default_domain) |
311 | iommu_domain_free(group->default_domain); | |
312 | ||
d72e31c9 AW |
313 | kfree(group->name); |
314 | kfree(group); | |
315 | } | |
316 | ||
317 | static struct kobj_type iommu_group_ktype = { | |
318 | .sysfs_ops = &iommu_group_sysfs_ops, | |
319 | .release = iommu_group_release, | |
320 | }; | |
321 | ||
322 | /** | |
323 | * iommu_group_alloc - Allocate a new group | |
324 | * @name: Optional name to associate with group, visible in sysfs | |
325 | * | |
326 | * This function is called by an iommu driver to allocate a new iommu | |
327 | * group. The iommu group represents the minimum granularity of the iommu. | |
328 | * Upon successful return, the caller holds a reference to the supplied | |
329 | * group in order to hold the group until devices are added. Use | |
330 | * iommu_group_put() to release this extra reference count, allowing the | |
331 | * group to be automatically reclaimed once it has no devices or external | |
332 | * references. | |
333 | */ | |
334 | struct iommu_group *iommu_group_alloc(void) | |
1460432c | 335 | { |
d72e31c9 AW |
336 | struct iommu_group *group; |
337 | int ret; | |
338 | ||
339 | group = kzalloc(sizeof(*group), GFP_KERNEL); | |
340 | if (!group) | |
341 | return ERR_PTR(-ENOMEM); | |
342 | ||
343 | group->kobj.kset = iommu_group_kset; | |
344 | mutex_init(&group->mutex); | |
345 | INIT_LIST_HEAD(&group->devices); | |
346 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); | |
347 | ||
feccf398 HK |
348 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
349 | if (ret < 0) { | |
d72e31c9 | 350 | kfree(group); |
feccf398 | 351 | return ERR_PTR(ret); |
d72e31c9 | 352 | } |
feccf398 | 353 | group->id = ret; |
1460432c | 354 | |
d72e31c9 AW |
355 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
356 | NULL, "%d", group->id); | |
357 | if (ret) { | |
feccf398 | 358 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 AW |
359 | kfree(group); |
360 | return ERR_PTR(ret); | |
361 | } | |
362 | ||
363 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); | |
364 | if (!group->devices_kobj) { | |
365 | kobject_put(&group->kobj); /* triggers .release & free */ | |
366 | return ERR_PTR(-ENOMEM); | |
367 | } | |
368 | ||
369 | /* | |
370 | * The devices_kobj holds a reference on the group kobject, so | |
371 | * as long as that exists so will the group. We can therefore | |
372 | * use the devices_kobj for reference counting. | |
373 | */ | |
374 | kobject_put(&group->kobj); | |
375 | ||
cf610ca1 EA |
376 | ret = iommu_group_create_file(group, |
377 | &iommu_group_attr_reserved_regions); | |
378 | if (ret) | |
379 | return ERR_PTR(ret); | |
380 | ||
269aa808 JR |
381 | pr_debug("Allocated group %d\n", group->id); |
382 | ||
d72e31c9 AW |
383 | return group; |
384 | } | |
385 | EXPORT_SYMBOL_GPL(iommu_group_alloc); | |
386 | ||
aa16bea9 AK |
387 | struct iommu_group *iommu_group_get_by_id(int id) |
388 | { | |
389 | struct kobject *group_kobj; | |
390 | struct iommu_group *group; | |
391 | const char *name; | |
392 | ||
393 | if (!iommu_group_kset) | |
394 | return NULL; | |
395 | ||
396 | name = kasprintf(GFP_KERNEL, "%d", id); | |
397 | if (!name) | |
398 | return NULL; | |
399 | ||
400 | group_kobj = kset_find_obj(iommu_group_kset, name); | |
401 | kfree(name); | |
402 | ||
403 | if (!group_kobj) | |
404 | return NULL; | |
405 | ||
406 | group = container_of(group_kobj, struct iommu_group, kobj); | |
407 | BUG_ON(group->id != id); | |
408 | ||
409 | kobject_get(group->devices_kobj); | |
410 | kobject_put(&group->kobj); | |
411 | ||
412 | return group; | |
413 | } | |
414 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); | |
415 | ||
d72e31c9 AW |
416 | /** |
417 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group | |
418 | * @group: the group | |
419 | * | |
420 | * iommu drivers can store data in the group for use when doing iommu | |
421 | * operations. This function provides a way to retrieve it. Caller | |
422 | * should hold a group reference. | |
423 | */ | |
424 | void *iommu_group_get_iommudata(struct iommu_group *group) | |
425 | { | |
426 | return group->iommu_data; | |
427 | } | |
428 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); | |
429 | ||
430 | /** | |
431 | * iommu_group_set_iommudata - set iommu_data for a group | |
432 | * @group: the group | |
433 | * @iommu_data: new data | |
434 | * @release: release function for iommu_data | |
435 | * | |
436 | * iommu drivers can store data in the group for use when doing iommu | |
437 | * operations. This function provides a way to set the data after | |
438 | * the group has been allocated. Caller should hold a group reference. | |
439 | */ | |
440 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, | |
441 | void (*release)(void *iommu_data)) | |
1460432c | 442 | { |
d72e31c9 AW |
443 | group->iommu_data = iommu_data; |
444 | group->iommu_data_release = release; | |
445 | } | |
446 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); | |
1460432c | 447 | |
d72e31c9 AW |
448 | /** |
449 | * iommu_group_set_name - set name for a group | |
450 | * @group: the group | |
451 | * @name: name | |
452 | * | |
453 | * Allow iommu driver to set a name for a group. When set it will | |
454 | * appear in a name attribute file under the group in sysfs. | |
455 | */ | |
456 | int iommu_group_set_name(struct iommu_group *group, const char *name) | |
457 | { | |
458 | int ret; | |
459 | ||
460 | if (group->name) { | |
461 | iommu_group_remove_file(group, &iommu_group_attr_name); | |
462 | kfree(group->name); | |
463 | group->name = NULL; | |
464 | if (!name) | |
465 | return 0; | |
466 | } | |
467 | ||
468 | group->name = kstrdup(name, GFP_KERNEL); | |
469 | if (!group->name) | |
470 | return -ENOMEM; | |
471 | ||
472 | ret = iommu_group_create_file(group, &iommu_group_attr_name); | |
473 | if (ret) { | |
474 | kfree(group->name); | |
475 | group->name = NULL; | |
476 | return ret; | |
477 | } | |
1460432c AW |
478 | |
479 | return 0; | |
480 | } | |
d72e31c9 | 481 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
1460432c | 482 | |
beed2821 JR |
483 | static int iommu_group_create_direct_mappings(struct iommu_group *group, |
484 | struct device *dev) | |
485 | { | |
486 | struct iommu_domain *domain = group->default_domain; | |
865e51e5 | 487 | struct iommu_resv_region *entry; |
beed2821 JR |
488 | struct list_head mappings; |
489 | unsigned long pg_size; | |
490 | int ret = 0; | |
491 | ||
492 | if (!domain || domain->type != IOMMU_DOMAIN_DMA) | |
493 | return 0; | |
494 | ||
d16e0faa | 495 | BUG_ON(!domain->pgsize_bitmap); |
beed2821 | 496 | |
d16e0faa | 497 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
beed2821 JR |
498 | INIT_LIST_HEAD(&mappings); |
499 | ||
865e51e5 | 500 | iommu_get_resv_regions(dev, &mappings); |
beed2821 JR |
501 | |
502 | /* We need to consider overlapping regions for different devices */ | |
503 | list_for_each_entry(entry, &mappings, list) { | |
504 | dma_addr_t start, end, addr; | |
505 | ||
865e51e5 EA |
506 | if (domain->ops->apply_resv_region) |
507 | domain->ops->apply_resv_region(dev, domain, entry); | |
33b21a6b | 508 | |
beed2821 JR |
509 | start = ALIGN(entry->start, pg_size); |
510 | end = ALIGN(entry->start + entry->length, pg_size); | |
511 | ||
18d2f738 EA |
512 | if (entry->type != IOMMU_RESV_DIRECT) |
513 | continue; | |
514 | ||
beed2821 JR |
515 | for (addr = start; addr < end; addr += pg_size) { |
516 | phys_addr_t phys_addr; | |
517 | ||
518 | phys_addr = iommu_iova_to_phys(domain, addr); | |
519 | if (phys_addr) | |
520 | continue; | |
521 | ||
522 | ret = iommu_map(domain, addr, addr, pg_size, entry->prot); | |
523 | if (ret) | |
524 | goto out; | |
525 | } | |
526 | ||
527 | } | |
528 | ||
529 | out: | |
865e51e5 | 530 | iommu_put_resv_regions(dev, &mappings); |
beed2821 JR |
531 | |
532 | return ret; | |
533 | } | |
534 | ||
d72e31c9 AW |
535 | /** |
536 | * iommu_group_add_device - add a device to an iommu group | |
537 | * @group: the group into which to add the device (reference should be held) | |
538 | * @dev: the device | |
539 | * | |
540 | * This function is called by an iommu driver to add a device into a | |
541 | * group. Adding a device increments the group reference count. | |
542 | */ | |
543 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) | |
1460432c | 544 | { |
d72e31c9 | 545 | int ret, i = 0; |
198706b5 | 546 | struct group_device *device; |
d72e31c9 AW |
547 | |
548 | device = kzalloc(sizeof(*device), GFP_KERNEL); | |
549 | if (!device) | |
550 | return -ENOMEM; | |
551 | ||
552 | device->dev = dev; | |
1460432c | 553 | |
d72e31c9 AW |
554 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
555 | if (ret) { | |
556 | kfree(device); | |
557 | return ret; | |
558 | } | |
559 | ||
560 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); | |
561 | rename: | |
562 | if (!device->name) { | |
563 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
564 | kfree(device); | |
565 | return -ENOMEM; | |
566 | } | |
1460432c | 567 | |
d72e31c9 AW |
568 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
569 | &dev->kobj, device->name); | |
570 | if (ret) { | |
571 | kfree(device->name); | |
572 | if (ret == -EEXIST && i >= 0) { | |
573 | /* | |
574 | * Account for the slim chance of collision | |
575 | * and append an instance to the name. | |
576 | */ | |
577 | device->name = kasprintf(GFP_KERNEL, "%s.%d", | |
578 | kobject_name(&dev->kobj), i++); | |
579 | goto rename; | |
580 | } | |
581 | ||
582 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
583 | kfree(device); | |
584 | return ret; | |
585 | } | |
586 | ||
587 | kobject_get(group->devices_kobj); | |
588 | ||
589 | dev->iommu_group = group; | |
590 | ||
beed2821 JR |
591 | iommu_group_create_direct_mappings(group, dev); |
592 | ||
d72e31c9 AW |
593 | mutex_lock(&group->mutex); |
594 | list_add_tail(&device->list, &group->devices); | |
e39cb8a3 JR |
595 | if (group->domain) |
596 | __iommu_attach_device(group->domain, dev); | |
d72e31c9 AW |
597 | mutex_unlock(&group->mutex); |
598 | ||
599 | /* Notify any listeners about change to group. */ | |
600 | blocking_notifier_call_chain(&group->notifier, | |
601 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); | |
d1cf7e82 SK |
602 | |
603 | trace_add_device_to_group(group->id, dev); | |
269aa808 JR |
604 | |
605 | pr_info("Adding device %s to group %d\n", dev_name(dev), group->id); | |
606 | ||
1460432c AW |
607 | return 0; |
608 | } | |
d72e31c9 | 609 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
1460432c | 610 | |
d72e31c9 AW |
611 | /** |
612 | * iommu_group_remove_device - remove a device from it's current group | |
613 | * @dev: device to be removed | |
614 | * | |
615 | * This function is called by an iommu driver to remove the device from | |
616 | * it's current group. This decrements the iommu group reference count. | |
617 | */ | |
618 | void iommu_group_remove_device(struct device *dev) | |
619 | { | |
620 | struct iommu_group *group = dev->iommu_group; | |
198706b5 | 621 | struct group_device *tmp_device, *device = NULL; |
d72e31c9 | 622 | |
269aa808 JR |
623 | pr_info("Removing device %s from group %d\n", dev_name(dev), group->id); |
624 | ||
d72e31c9 AW |
625 | /* Pre-notify listeners that a device is being removed. */ |
626 | blocking_notifier_call_chain(&group->notifier, | |
627 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); | |
628 | ||
629 | mutex_lock(&group->mutex); | |
630 | list_for_each_entry(tmp_device, &group->devices, list) { | |
631 | if (tmp_device->dev == dev) { | |
632 | device = tmp_device; | |
633 | list_del(&device->list); | |
634 | break; | |
635 | } | |
636 | } | |
637 | mutex_unlock(&group->mutex); | |
638 | ||
639 | if (!device) | |
640 | return; | |
641 | ||
642 | sysfs_remove_link(group->devices_kobj, device->name); | |
643 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
644 | ||
2e757086 SK |
645 | trace_remove_device_from_group(group->id, dev); |
646 | ||
d72e31c9 AW |
647 | kfree(device->name); |
648 | kfree(device); | |
649 | dev->iommu_group = NULL; | |
650 | kobject_put(group->devices_kobj); | |
651 | } | |
652 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); | |
653 | ||
426a2738 JR |
654 | static int iommu_group_device_count(struct iommu_group *group) |
655 | { | |
198706b5 | 656 | struct group_device *entry; |
426a2738 JR |
657 | int ret = 0; |
658 | ||
659 | list_for_each_entry(entry, &group->devices, list) | |
660 | ret++; | |
661 | ||
662 | return ret; | |
663 | } | |
664 | ||
d72e31c9 AW |
665 | /** |
666 | * iommu_group_for_each_dev - iterate over each device in the group | |
667 | * @group: the group | |
668 | * @data: caller opaque data to be passed to callback function | |
669 | * @fn: caller supplied callback function | |
670 | * | |
671 | * This function is called by group users to iterate over group devices. | |
672 | * Callers should hold a reference count to the group during callback. | |
673 | * The group->mutex is held across callbacks, which will block calls to | |
674 | * iommu_group_add/remove_device. | |
675 | */ | |
e39cb8a3 JR |
676 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
677 | int (*fn)(struct device *, void *)) | |
d72e31c9 | 678 | { |
198706b5 | 679 | struct group_device *device; |
d72e31c9 AW |
680 | int ret = 0; |
681 | ||
d72e31c9 AW |
682 | list_for_each_entry(device, &group->devices, list) { |
683 | ret = fn(device->dev, data); | |
684 | if (ret) | |
685 | break; | |
686 | } | |
e39cb8a3 JR |
687 | return ret; |
688 | } | |
689 | ||
690 | ||
691 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, | |
692 | int (*fn)(struct device *, void *)) | |
693 | { | |
694 | int ret; | |
695 | ||
696 | mutex_lock(&group->mutex); | |
697 | ret = __iommu_group_for_each_dev(group, data, fn); | |
d72e31c9 | 698 | mutex_unlock(&group->mutex); |
e39cb8a3 | 699 | |
d72e31c9 AW |
700 | return ret; |
701 | } | |
702 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); | |
703 | ||
704 | /** | |
705 | * iommu_group_get - Return the group for a device and increment reference | |
706 | * @dev: get the group that this device belongs to | |
707 | * | |
708 | * This function is called by iommu drivers and users to get the group | |
709 | * for the specified device. If found, the group is returned and the group | |
710 | * reference in incremented, else NULL. | |
711 | */ | |
712 | struct iommu_group *iommu_group_get(struct device *dev) | |
713 | { | |
714 | struct iommu_group *group = dev->iommu_group; | |
715 | ||
716 | if (group) | |
717 | kobject_get(group->devices_kobj); | |
718 | ||
719 | return group; | |
720 | } | |
721 | EXPORT_SYMBOL_GPL(iommu_group_get); | |
722 | ||
13f59a78 RM |
723 | /** |
724 | * iommu_group_ref_get - Increment reference on a group | |
725 | * @group: the group to use, must not be NULL | |
726 | * | |
727 | * This function is called by iommu drivers to take additional references on an | |
728 | * existing group. Returns the given group for convenience. | |
729 | */ | |
730 | struct iommu_group *iommu_group_ref_get(struct iommu_group *group) | |
731 | { | |
732 | kobject_get(group->devices_kobj); | |
733 | return group; | |
734 | } | |
735 | ||
d72e31c9 AW |
736 | /** |
737 | * iommu_group_put - Decrement group reference | |
738 | * @group: the group to use | |
739 | * | |
740 | * This function is called by iommu drivers and users to release the | |
741 | * iommu group. Once the reference count is zero, the group is released. | |
742 | */ | |
743 | void iommu_group_put(struct iommu_group *group) | |
744 | { | |
745 | if (group) | |
746 | kobject_put(group->devices_kobj); | |
747 | } | |
748 | EXPORT_SYMBOL_GPL(iommu_group_put); | |
749 | ||
750 | /** | |
751 | * iommu_group_register_notifier - Register a notifier for group changes | |
752 | * @group: the group to watch | |
753 | * @nb: notifier block to signal | |
754 | * | |
755 | * This function allows iommu group users to track changes in a group. | |
756 | * See include/linux/iommu.h for actions sent via this notifier. Caller | |
757 | * should hold a reference to the group throughout notifier registration. | |
758 | */ | |
759 | int iommu_group_register_notifier(struct iommu_group *group, | |
760 | struct notifier_block *nb) | |
761 | { | |
762 | return blocking_notifier_chain_register(&group->notifier, nb); | |
763 | } | |
764 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); | |
765 | ||
766 | /** | |
767 | * iommu_group_unregister_notifier - Unregister a notifier | |
768 | * @group: the group to watch | |
769 | * @nb: notifier block to signal | |
770 | * | |
771 | * Unregister a previously registered group notifier block. | |
772 | */ | |
773 | int iommu_group_unregister_notifier(struct iommu_group *group, | |
774 | struct notifier_block *nb) | |
775 | { | |
776 | return blocking_notifier_chain_unregister(&group->notifier, nb); | |
777 | } | |
778 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); | |
779 | ||
780 | /** | |
781 | * iommu_group_id - Return ID for a group | |
782 | * @group: the group to ID | |
783 | * | |
784 | * Return the unique ID for the group matching the sysfs group number. | |
785 | */ | |
786 | int iommu_group_id(struct iommu_group *group) | |
787 | { | |
788 | return group->id; | |
789 | } | |
790 | EXPORT_SYMBOL_GPL(iommu_group_id); | |
1460432c | 791 | |
f096c061 AW |
792 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
793 | unsigned long *devfns); | |
794 | ||
104a1c13 AW |
795 | /* |
796 | * To consider a PCI device isolated, we require ACS to support Source | |
797 | * Validation, Request Redirection, Completer Redirection, and Upstream | |
798 | * Forwarding. This effectively means that devices cannot spoof their | |
799 | * requester ID, requests and completions cannot be redirected, and all | |
800 | * transactions are forwarded upstream, even as it passes through a | |
801 | * bridge where the target device is downstream. | |
802 | */ | |
803 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) | |
804 | ||
f096c061 AW |
805 | /* |
806 | * For multifunction devices which are not isolated from each other, find | |
807 | * all the other non-isolated functions and look for existing groups. For | |
808 | * each function, we also need to look for aliases to or from other devices | |
809 | * that may already have a group. | |
810 | */ | |
811 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, | |
812 | unsigned long *devfns) | |
813 | { | |
814 | struct pci_dev *tmp = NULL; | |
815 | struct iommu_group *group; | |
816 | ||
817 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) | |
818 | return NULL; | |
819 | ||
820 | for_each_pci_dev(tmp) { | |
821 | if (tmp == pdev || tmp->bus != pdev->bus || | |
822 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || | |
823 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) | |
824 | continue; | |
825 | ||
826 | group = get_pci_alias_group(tmp, devfns); | |
827 | if (group) { | |
828 | pci_dev_put(tmp); | |
829 | return group; | |
830 | } | |
831 | } | |
832 | ||
833 | return NULL; | |
834 | } | |
835 | ||
836 | /* | |
338c3149 JL |
837 | * Look for aliases to or from the given device for existing groups. DMA |
838 | * aliases are only supported on the same bus, therefore the search | |
f096c061 AW |
839 | * space is quite small (especially since we're really only looking at pcie |
840 | * device, and therefore only expect multiple slots on the root complex or | |
841 | * downstream switch ports). It's conceivable though that a pair of | |
842 | * multifunction devices could have aliases between them that would cause a | |
843 | * loop. To prevent this, we use a bitmap to track where we've been. | |
844 | */ | |
845 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, | |
846 | unsigned long *devfns) | |
847 | { | |
848 | struct pci_dev *tmp = NULL; | |
849 | struct iommu_group *group; | |
850 | ||
851 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) | |
852 | return NULL; | |
853 | ||
854 | group = iommu_group_get(&pdev->dev); | |
855 | if (group) | |
856 | return group; | |
857 | ||
858 | for_each_pci_dev(tmp) { | |
859 | if (tmp == pdev || tmp->bus != pdev->bus) | |
860 | continue; | |
861 | ||
862 | /* We alias them or they alias us */ | |
338c3149 | 863 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
f096c061 AW |
864 | group = get_pci_alias_group(tmp, devfns); |
865 | if (group) { | |
866 | pci_dev_put(tmp); | |
867 | return group; | |
868 | } | |
869 | ||
870 | group = get_pci_function_alias_group(tmp, devfns); | |
871 | if (group) { | |
872 | pci_dev_put(tmp); | |
873 | return group; | |
874 | } | |
875 | } | |
876 | } | |
877 | ||
878 | return NULL; | |
879 | } | |
880 | ||
104a1c13 AW |
881 | struct group_for_pci_data { |
882 | struct pci_dev *pdev; | |
883 | struct iommu_group *group; | |
884 | }; | |
885 | ||
886 | /* | |
887 | * DMA alias iterator callback, return the last seen device. Stop and return | |
888 | * the IOMMU group if we find one along the way. | |
889 | */ | |
890 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) | |
891 | { | |
892 | struct group_for_pci_data *data = opaque; | |
893 | ||
894 | data->pdev = pdev; | |
895 | data->group = iommu_group_get(&pdev->dev); | |
896 | ||
897 | return data->group != NULL; | |
898 | } | |
899 | ||
6eab556a JR |
900 | /* |
901 | * Generic device_group call-back function. It just allocates one | |
902 | * iommu-group per device. | |
903 | */ | |
904 | struct iommu_group *generic_device_group(struct device *dev) | |
905 | { | |
906 | struct iommu_group *group; | |
907 | ||
908 | group = iommu_group_alloc(); | |
909 | if (IS_ERR(group)) | |
910 | return NULL; | |
911 | ||
912 | return group; | |
913 | } | |
914 | ||
104a1c13 AW |
915 | /* |
916 | * Use standard PCI bus topology, isolation features, and DMA alias quirks | |
917 | * to find or create an IOMMU group for a device. | |
918 | */ | |
5e62292b | 919 | struct iommu_group *pci_device_group(struct device *dev) |
104a1c13 | 920 | { |
5e62292b | 921 | struct pci_dev *pdev = to_pci_dev(dev); |
104a1c13 AW |
922 | struct group_for_pci_data data; |
923 | struct pci_bus *bus; | |
924 | struct iommu_group *group = NULL; | |
f096c061 | 925 | u64 devfns[4] = { 0 }; |
104a1c13 | 926 | |
5e62292b JR |
927 | if (WARN_ON(!dev_is_pci(dev))) |
928 | return ERR_PTR(-EINVAL); | |
929 | ||
104a1c13 AW |
930 | /* |
931 | * Find the upstream DMA alias for the device. A device must not | |
932 | * be aliased due to topology in order to have its own IOMMU group. | |
933 | * If we find an alias along the way that already belongs to a | |
934 | * group, use it. | |
935 | */ | |
936 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) | |
937 | return data.group; | |
938 | ||
939 | pdev = data.pdev; | |
940 | ||
941 | /* | |
942 | * Continue upstream from the point of minimum IOMMU granularity | |
943 | * due to aliases to the point where devices are protected from | |
944 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing | |
945 | * group, use it. | |
946 | */ | |
947 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { | |
948 | if (!bus->self) | |
949 | continue; | |
950 | ||
951 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) | |
952 | break; | |
953 | ||
954 | pdev = bus->self; | |
955 | ||
956 | group = iommu_group_get(&pdev->dev); | |
957 | if (group) | |
958 | return group; | |
959 | } | |
960 | ||
961 | /* | |
f096c061 AW |
962 | * Look for existing groups on device aliases. If we alias another |
963 | * device or another device aliases us, use the same group. | |
104a1c13 | 964 | */ |
f096c061 AW |
965 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
966 | if (group) | |
967 | return group; | |
104a1c13 AW |
968 | |
969 | /* | |
f096c061 AW |
970 | * Look for existing groups on non-isolated functions on the same |
971 | * slot and aliases of those funcions, if any. No need to clear | |
972 | * the search bitmap, the tested devfns are still valid. | |
104a1c13 | 973 | */ |
f096c061 AW |
974 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
975 | if (group) | |
976 | return group; | |
104a1c13 AW |
977 | |
978 | /* No shared group found, allocate new */ | |
53723dc5 | 979 | group = iommu_group_alloc(); |
409e553d DC |
980 | if (IS_ERR(group)) |
981 | return NULL; | |
982 | ||
53723dc5 | 983 | return group; |
104a1c13 AW |
984 | } |
985 | ||
986 | /** | |
987 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device | |
988 | * @dev: target device | |
989 | * | |
990 | * This function is intended to be called by IOMMU drivers and extended to | |
991 | * support common, bus-defined algorithms when determining or creating the | |
992 | * IOMMU group for a device. On success, the caller will hold a reference | |
993 | * to the returned IOMMU group, which will already include the provided | |
994 | * device. The reference should be released with iommu_group_put(). | |
995 | */ | |
996 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) | |
997 | { | |
46c6b2bc | 998 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
c4a783b8 | 999 | struct iommu_group *group; |
104a1c13 AW |
1000 | int ret; |
1001 | ||
1002 | group = iommu_group_get(dev); | |
1003 | if (group) | |
1004 | return group; | |
1005 | ||
46c6b2bc | 1006 | group = ERR_PTR(-EINVAL); |
c4a783b8 | 1007 | |
46c6b2bc JR |
1008 | if (ops && ops->device_group) |
1009 | group = ops->device_group(dev); | |
104a1c13 AW |
1010 | |
1011 | if (IS_ERR(group)) | |
1012 | return group; | |
1013 | ||
1228236d JR |
1014 | /* |
1015 | * Try to allocate a default domain - needs support from the | |
1016 | * IOMMU driver. | |
1017 | */ | |
1018 | if (!group->default_domain) { | |
d806b39f WD |
1019 | struct iommu_domain *dom; |
1020 | ||
1021 | dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); | |
1022 | if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { | |
1023 | dev_warn(dev, | |
1024 | "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", | |
1025 | iommu_def_domain_type); | |
1026 | dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); | |
1027 | } | |
1028 | ||
1029 | group->default_domain = dom; | |
eebb8034 | 1030 | if (!group->domain) |
d806b39f | 1031 | group->domain = dom; |
1228236d JR |
1032 | } |
1033 | ||
104a1c13 AW |
1034 | ret = iommu_group_add_device(group, dev); |
1035 | if (ret) { | |
1036 | iommu_group_put(group); | |
1037 | return ERR_PTR(ret); | |
1038 | } | |
1039 | ||
1040 | return group; | |
1041 | } | |
1042 | ||
6827ca83 JR |
1043 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
1044 | { | |
1045 | return group->default_domain; | |
1046 | } | |
1047 | ||
d72e31c9 | 1048 | static int add_iommu_group(struct device *dev, void *data) |
1460432c | 1049 | { |
b22f6434 TR |
1050 | struct iommu_callback_data *cb = data; |
1051 | const struct iommu_ops *ops = cb->ops; | |
38667f18 | 1052 | int ret; |
d72e31c9 AW |
1053 | |
1054 | if (!ops->add_device) | |
461bfb3f | 1055 | return 0; |
1460432c | 1056 | |
d72e31c9 AW |
1057 | WARN_ON(dev->iommu_group); |
1058 | ||
38667f18 JR |
1059 | ret = ops->add_device(dev); |
1060 | ||
1061 | /* | |
1062 | * We ignore -ENODEV errors for now, as they just mean that the | |
1063 | * device is not translated by an IOMMU. We still care about | |
1064 | * other errors and fail to initialize when they happen. | |
1065 | */ | |
1066 | if (ret == -ENODEV) | |
1067 | ret = 0; | |
1068 | ||
1069 | return ret; | |
1460432c AW |
1070 | } |
1071 | ||
8da30142 JR |
1072 | static int remove_iommu_group(struct device *dev, void *data) |
1073 | { | |
1074 | struct iommu_callback_data *cb = data; | |
1075 | const struct iommu_ops *ops = cb->ops; | |
1076 | ||
1077 | if (ops->remove_device && dev->iommu_group) | |
1078 | ops->remove_device(dev); | |
1460432c AW |
1079 | |
1080 | return 0; | |
1081 | } | |
1082 | ||
d72e31c9 AW |
1083 | static int iommu_bus_notifier(struct notifier_block *nb, |
1084 | unsigned long action, void *data) | |
1460432c AW |
1085 | { |
1086 | struct device *dev = data; | |
b22f6434 | 1087 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
d72e31c9 AW |
1088 | struct iommu_group *group; |
1089 | unsigned long group_action = 0; | |
1090 | ||
1091 | /* | |
1092 | * ADD/DEL call into iommu driver ops if provided, which may | |
1093 | * result in ADD/DEL notifiers to group->notifier | |
1094 | */ | |
1095 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
1096 | if (ops->add_device) | |
1097 | return ops->add_device(dev); | |
843cb6dc | 1098 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
d72e31c9 AW |
1099 | if (ops->remove_device && dev->iommu_group) { |
1100 | ops->remove_device(dev); | |
1101 | return 0; | |
1102 | } | |
1103 | } | |
1460432c | 1104 | |
d72e31c9 AW |
1105 | /* |
1106 | * Remaining BUS_NOTIFYs get filtered and republished to the | |
1107 | * group, if anyone is listening | |
1108 | */ | |
1109 | group = iommu_group_get(dev); | |
1110 | if (!group) | |
1111 | return 0; | |
1460432c | 1112 | |
d72e31c9 AW |
1113 | switch (action) { |
1114 | case BUS_NOTIFY_BIND_DRIVER: | |
1115 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; | |
1116 | break; | |
1117 | case BUS_NOTIFY_BOUND_DRIVER: | |
1118 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; | |
1119 | break; | |
1120 | case BUS_NOTIFY_UNBIND_DRIVER: | |
1121 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; | |
1122 | break; | |
1123 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
1124 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; | |
1125 | break; | |
1126 | } | |
1460432c | 1127 | |
d72e31c9 AW |
1128 | if (group_action) |
1129 | blocking_notifier_call_chain(&group->notifier, | |
1130 | group_action, dev); | |
1460432c | 1131 | |
d72e31c9 | 1132 | iommu_group_put(group); |
1460432c AW |
1133 | return 0; |
1134 | } | |
1135 | ||
fb3e3065 | 1136 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
ff21776d | 1137 | { |
fb3e3065 MS |
1138 | int err; |
1139 | struct notifier_block *nb; | |
b22f6434 TR |
1140 | struct iommu_callback_data cb = { |
1141 | .ops = ops, | |
1142 | }; | |
1143 | ||
fb3e3065 MS |
1144 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
1145 | if (!nb) | |
1146 | return -ENOMEM; | |
1147 | ||
1148 | nb->notifier_call = iommu_bus_notifier; | |
1149 | ||
1150 | err = bus_register_notifier(bus, nb); | |
8da30142 JR |
1151 | if (err) |
1152 | goto out_free; | |
d7da6bdc HS |
1153 | |
1154 | err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group); | |
8da30142 JR |
1155 | if (err) |
1156 | goto out_err; | |
1157 | ||
d7da6bdc HS |
1158 | |
1159 | return 0; | |
8da30142 JR |
1160 | |
1161 | out_err: | |
1162 | /* Clean up */ | |
1163 | bus_for_each_dev(bus, NULL, &cb, remove_iommu_group); | |
1164 | bus_unregister_notifier(bus, nb); | |
1165 | ||
1166 | out_free: | |
1167 | kfree(nb); | |
1168 | ||
1169 | return err; | |
ff21776d | 1170 | } |
fc2100eb | 1171 | |
ff21776d JR |
1172 | /** |
1173 | * bus_set_iommu - set iommu-callbacks for the bus | |
1174 | * @bus: bus. | |
1175 | * @ops: the callbacks provided by the iommu-driver | |
1176 | * | |
1177 | * This function is called by an iommu driver to set the iommu methods | |
1178 | * used for a particular bus. Drivers for devices on that bus can use | |
1179 | * the iommu-api after these ops are registered. | |
1180 | * This special function is needed because IOMMUs are usually devices on | |
1181 | * the bus itself, so the iommu drivers are not initialized when the bus | |
1182 | * is set up. With this function the iommu-driver can set the iommu-ops | |
1183 | * afterwards. | |
1184 | */ | |
b22f6434 | 1185 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
fc2100eb | 1186 | { |
d7da6bdc HS |
1187 | int err; |
1188 | ||
ff21776d JR |
1189 | if (bus->iommu_ops != NULL) |
1190 | return -EBUSY; | |
fc2100eb | 1191 | |
ff21776d JR |
1192 | bus->iommu_ops = ops; |
1193 | ||
1194 | /* Do IOMMU specific setup for this bus-type */ | |
d7da6bdc HS |
1195 | err = iommu_bus_init(bus, ops); |
1196 | if (err) | |
1197 | bus->iommu_ops = NULL; | |
1198 | ||
1199 | return err; | |
fc2100eb | 1200 | } |
ff21776d | 1201 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 1202 | |
a1b60c1c | 1203 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 1204 | { |
94441c3b | 1205 | return bus->iommu_ops != NULL; |
fc2100eb | 1206 | } |
a1b60c1c | 1207 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 1208 | |
3c0e0ca0 JR |
1209 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
1210 | { | |
1211 | if (!bus->iommu_ops || !bus->iommu_ops->capable) | |
1212 | return false; | |
1213 | ||
1214 | return bus->iommu_ops->capable(cap); | |
1215 | } | |
1216 | EXPORT_SYMBOL_GPL(iommu_capable); | |
1217 | ||
4f3f8d9d OBC |
1218 | /** |
1219 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
1220 | * @domain: iommu domain | |
1221 | * @handler: fault handler | |
77ca2332 | 1222 | * @token: user data, will be passed back to the fault handler |
0ed6d2d2 OBC |
1223 | * |
1224 | * This function should be used by IOMMU users which want to be notified | |
1225 | * whenever an IOMMU fault happens. | |
1226 | * | |
1227 | * The fault handler itself should return 0 on success, and an appropriate | |
1228 | * error code otherwise. | |
4f3f8d9d OBC |
1229 | */ |
1230 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77ca2332 OBC |
1231 | iommu_fault_handler_t handler, |
1232 | void *token) | |
4f3f8d9d OBC |
1233 | { |
1234 | BUG_ON(!domain); | |
1235 | ||
1236 | domain->handler = handler; | |
77ca2332 | 1237 | domain->handler_token = token; |
4f3f8d9d | 1238 | } |
30bd918c | 1239 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 1240 | |
53723dc5 JR |
1241 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
1242 | unsigned type) | |
fc2100eb JR |
1243 | { |
1244 | struct iommu_domain *domain; | |
fc2100eb | 1245 | |
94441c3b | 1246 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
1247 | return NULL; |
1248 | ||
53723dc5 | 1249 | domain = bus->iommu_ops->domain_alloc(type); |
fc2100eb JR |
1250 | if (!domain) |
1251 | return NULL; | |
1252 | ||
8539c7c1 | 1253 | domain->ops = bus->iommu_ops; |
53723dc5 | 1254 | domain->type = type; |
d16e0faa RM |
1255 | /* Assume all sizes by default; the driver may override this later */ |
1256 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; | |
905d66c1 | 1257 | |
fc2100eb | 1258 | return domain; |
fc2100eb | 1259 | } |
fc2100eb | 1260 | |
53723dc5 JR |
1261 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
1262 | { | |
1263 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); | |
fc2100eb JR |
1264 | } |
1265 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
1266 | ||
1267 | void iommu_domain_free(struct iommu_domain *domain) | |
1268 | { | |
89be34a1 | 1269 | domain->ops->domain_free(domain); |
fc2100eb JR |
1270 | } |
1271 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
1272 | ||
426a2738 JR |
1273 | static int __iommu_attach_device(struct iommu_domain *domain, |
1274 | struct device *dev) | |
fc2100eb | 1275 | { |
b54db778 | 1276 | int ret; |
e5aa7f00 JR |
1277 | if (unlikely(domain->ops->attach_dev == NULL)) |
1278 | return -ENODEV; | |
1279 | ||
b54db778 SK |
1280 | ret = domain->ops->attach_dev(domain, dev); |
1281 | if (!ret) | |
1282 | trace_attach_device_to_domain(dev); | |
1283 | return ret; | |
fc2100eb | 1284 | } |
426a2738 JR |
1285 | |
1286 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
1287 | { | |
1288 | struct iommu_group *group; | |
1289 | int ret; | |
1290 | ||
1291 | group = iommu_group_get(dev); | |
1292 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ | |
1293 | if (group == NULL) | |
1294 | return __iommu_attach_device(domain, dev); | |
1295 | ||
1296 | /* | |
1297 | * We have a group - lock it to make sure the device-count doesn't | |
1298 | * change while we are attaching | |
1299 | */ | |
1300 | mutex_lock(&group->mutex); | |
1301 | ret = -EINVAL; | |
1302 | if (iommu_group_device_count(group) != 1) | |
1303 | goto out_unlock; | |
1304 | ||
e39cb8a3 | 1305 | ret = __iommu_attach_group(domain, group); |
426a2738 JR |
1306 | |
1307 | out_unlock: | |
1308 | mutex_unlock(&group->mutex); | |
1309 | iommu_group_put(group); | |
1310 | ||
1311 | return ret; | |
1312 | } | |
fc2100eb JR |
1313 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
1314 | ||
426a2738 JR |
1315 | static void __iommu_detach_device(struct iommu_domain *domain, |
1316 | struct device *dev) | |
fc2100eb | 1317 | { |
e5aa7f00 JR |
1318 | if (unlikely(domain->ops->detach_dev == NULL)) |
1319 | return; | |
1320 | ||
1321 | domain->ops->detach_dev(domain, dev); | |
69980630 | 1322 | trace_detach_device_from_domain(dev); |
fc2100eb | 1323 | } |
426a2738 JR |
1324 | |
1325 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
1326 | { | |
1327 | struct iommu_group *group; | |
1328 | ||
1329 | group = iommu_group_get(dev); | |
1330 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ | |
1331 | if (group == NULL) | |
1332 | return __iommu_detach_device(domain, dev); | |
1333 | ||
1334 | mutex_lock(&group->mutex); | |
1335 | if (iommu_group_device_count(group) != 1) { | |
1336 | WARN_ON(1); | |
1337 | goto out_unlock; | |
1338 | } | |
1339 | ||
e39cb8a3 | 1340 | __iommu_detach_group(domain, group); |
426a2738 JR |
1341 | |
1342 | out_unlock: | |
1343 | mutex_unlock(&group->mutex); | |
1344 | iommu_group_put(group); | |
1345 | } | |
fc2100eb JR |
1346 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
1347 | ||
2c1296d9 JR |
1348 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
1349 | { | |
1350 | struct iommu_domain *domain; | |
1351 | struct iommu_group *group; | |
1352 | ||
1353 | group = iommu_group_get(dev); | |
1354 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ | |
1355 | if (group == NULL) | |
1356 | return NULL; | |
1357 | ||
1358 | domain = group->domain; | |
1359 | ||
1360 | iommu_group_put(group); | |
1361 | ||
1362 | return domain; | |
1363 | } | |
1364 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); | |
fc2100eb | 1365 | |
d72e31c9 AW |
1366 | /* |
1367 | * IOMMU groups are really the natrual working unit of the IOMMU, but | |
1368 | * the IOMMU API works on domains and devices. Bridge that gap by | |
1369 | * iterating over the devices in a group. Ideally we'd have a single | |
1370 | * device which represents the requestor ID of the group, but we also | |
1371 | * allow IOMMU drivers to create policy defined minimum sets, where | |
1372 | * the physical hardware may be able to distiguish members, but we | |
1373 | * wish to group them at a higher level (ex. untrusted multi-function | |
1374 | * PCI devices). Thus we attach each device. | |
1375 | */ | |
1376 | static int iommu_group_do_attach_device(struct device *dev, void *data) | |
1377 | { | |
1378 | struct iommu_domain *domain = data; | |
1379 | ||
426a2738 | 1380 | return __iommu_attach_device(domain, dev); |
d72e31c9 AW |
1381 | } |
1382 | ||
e39cb8a3 JR |
1383 | static int __iommu_attach_group(struct iommu_domain *domain, |
1384 | struct iommu_group *group) | |
1385 | { | |
1386 | int ret; | |
1387 | ||
1388 | if (group->default_domain && group->domain != group->default_domain) | |
1389 | return -EBUSY; | |
1390 | ||
1391 | ret = __iommu_group_for_each_dev(group, domain, | |
1392 | iommu_group_do_attach_device); | |
1393 | if (ret == 0) | |
1394 | group->domain = domain; | |
1395 | ||
1396 | return ret; | |
d72e31c9 AW |
1397 | } |
1398 | ||
1399 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) | |
1400 | { | |
e39cb8a3 JR |
1401 | int ret; |
1402 | ||
1403 | mutex_lock(&group->mutex); | |
1404 | ret = __iommu_attach_group(domain, group); | |
1405 | mutex_unlock(&group->mutex); | |
1406 | ||
1407 | return ret; | |
d72e31c9 AW |
1408 | } |
1409 | EXPORT_SYMBOL_GPL(iommu_attach_group); | |
1410 | ||
1411 | static int iommu_group_do_detach_device(struct device *dev, void *data) | |
1412 | { | |
1413 | struct iommu_domain *domain = data; | |
1414 | ||
426a2738 | 1415 | __iommu_detach_device(domain, dev); |
d72e31c9 AW |
1416 | |
1417 | return 0; | |
1418 | } | |
1419 | ||
e39cb8a3 JR |
1420 | static void __iommu_detach_group(struct iommu_domain *domain, |
1421 | struct iommu_group *group) | |
1422 | { | |
1423 | int ret; | |
1424 | ||
1425 | if (!group->default_domain) { | |
1426 | __iommu_group_for_each_dev(group, domain, | |
1427 | iommu_group_do_detach_device); | |
1428 | group->domain = NULL; | |
1429 | return; | |
1430 | } | |
1431 | ||
1432 | if (group->domain == group->default_domain) | |
1433 | return; | |
1434 | ||
1435 | /* Detach by re-attaching to the default domain */ | |
1436 | ret = __iommu_group_for_each_dev(group, group->default_domain, | |
1437 | iommu_group_do_attach_device); | |
1438 | if (ret != 0) | |
1439 | WARN_ON(1); | |
1440 | else | |
1441 | group->domain = group->default_domain; | |
1442 | } | |
1443 | ||
d72e31c9 AW |
1444 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
1445 | { | |
e39cb8a3 JR |
1446 | mutex_lock(&group->mutex); |
1447 | __iommu_detach_group(domain, group); | |
1448 | mutex_unlock(&group->mutex); | |
d72e31c9 AW |
1449 | } |
1450 | EXPORT_SYMBOL_GPL(iommu_detach_group); | |
1451 | ||
bb5547ac | 1452 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
fc2100eb | 1453 | { |
e5aa7f00 JR |
1454 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
1455 | return 0; | |
1456 | ||
1457 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
1458 | } |
1459 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 | 1460 | |
bd13969b AW |
1461 | static size_t iommu_pgsize(struct iommu_domain *domain, |
1462 | unsigned long addr_merge, size_t size) | |
1463 | { | |
1464 | unsigned int pgsize_idx; | |
1465 | size_t pgsize; | |
1466 | ||
1467 | /* Max page size that still fits into 'size' */ | |
1468 | pgsize_idx = __fls(size); | |
1469 | ||
1470 | /* need to consider alignment requirements ? */ | |
1471 | if (likely(addr_merge)) { | |
1472 | /* Max page size allowed by address */ | |
1473 | unsigned int align_pgsize_idx = __ffs(addr_merge); | |
1474 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); | |
1475 | } | |
1476 | ||
1477 | /* build a mask of acceptable page sizes */ | |
1478 | pgsize = (1UL << (pgsize_idx + 1)) - 1; | |
1479 | ||
1480 | /* throw away page sizes not supported by the hardware */ | |
d16e0faa | 1481 | pgsize &= domain->pgsize_bitmap; |
bd13969b AW |
1482 | |
1483 | /* make sure we're still sane */ | |
1484 | BUG_ON(!pgsize); | |
1485 | ||
1486 | /* pick the biggest page */ | |
1487 | pgsize_idx = __fls(pgsize); | |
1488 | pgsize = 1UL << pgsize_idx; | |
1489 | ||
1490 | return pgsize; | |
1491 | } | |
1492 | ||
cefc53c7 | 1493 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
7d3002cc | 1494 | phys_addr_t paddr, size_t size, int prot) |
cefc53c7 | 1495 | { |
7d3002cc OBC |
1496 | unsigned long orig_iova = iova; |
1497 | unsigned int min_pagesz; | |
1498 | size_t orig_size = size; | |
06bfcaa9 | 1499 | phys_addr_t orig_paddr = paddr; |
7d3002cc | 1500 | int ret = 0; |
cefc53c7 | 1501 | |
9db4ad91 | 1502 | if (unlikely(domain->ops->map == NULL || |
d16e0faa | 1503 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 | 1504 | return -ENODEV; |
cefc53c7 | 1505 | |
a10315e5 JR |
1506 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
1507 | return -EINVAL; | |
1508 | ||
7d3002cc | 1509 | /* find out the minimum page size supported */ |
d16e0faa | 1510 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1511 | |
1512 | /* | |
1513 | * both the virtual address and the physical one, as well as | |
1514 | * the size of the mapping, must be aligned (at least) to the | |
1515 | * size of the smallest page supported by the hardware | |
1516 | */ | |
1517 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { | |
abedb049 | 1518 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
6197ca82 | 1519 | iova, &paddr, size, min_pagesz); |
7d3002cc OBC |
1520 | return -EINVAL; |
1521 | } | |
1522 | ||
abedb049 | 1523 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
7d3002cc OBC |
1524 | |
1525 | while (size) { | |
bd13969b | 1526 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
7d3002cc | 1527 | |
abedb049 | 1528 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
6197ca82 | 1529 | iova, &paddr, pgsize); |
7d3002cc OBC |
1530 | |
1531 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); | |
1532 | if (ret) | |
1533 | break; | |
1534 | ||
1535 | iova += pgsize; | |
1536 | paddr += pgsize; | |
1537 | size -= pgsize; | |
1538 | } | |
1539 | ||
1540 | /* unroll mapping in case something went wrong */ | |
1541 | if (ret) | |
1542 | iommu_unmap(domain, orig_iova, orig_size - size); | |
e0be7c86 | 1543 | else |
06bfcaa9 | 1544 | trace_map(orig_iova, orig_paddr, orig_size); |
7d3002cc OBC |
1545 | |
1546 | return ret; | |
cefc53c7 JR |
1547 | } |
1548 | EXPORT_SYMBOL_GPL(iommu_map); | |
1549 | ||
7d3002cc | 1550 | size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size) |
cefc53c7 | 1551 | { |
7d3002cc OBC |
1552 | size_t unmapped_page, unmapped = 0; |
1553 | unsigned int min_pagesz; | |
6fd492fd | 1554 | unsigned long orig_iova = iova; |
cefc53c7 | 1555 | |
57886518 | 1556 | if (unlikely(domain->ops->unmap == NULL || |
d16e0faa | 1557 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 JR |
1558 | return -ENODEV; |
1559 | ||
a10315e5 JR |
1560 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
1561 | return -EINVAL; | |
1562 | ||
7d3002cc | 1563 | /* find out the minimum page size supported */ |
d16e0faa | 1564 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1565 | |
1566 | /* | |
1567 | * The virtual address, as well as the size of the mapping, must be | |
1568 | * aligned (at least) to the size of the smallest page supported | |
1569 | * by the hardware | |
1570 | */ | |
1571 | if (!IS_ALIGNED(iova | size, min_pagesz)) { | |
6197ca82 JP |
1572 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
1573 | iova, size, min_pagesz); | |
7d3002cc OBC |
1574 | return -EINVAL; |
1575 | } | |
1576 | ||
6197ca82 | 1577 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
7d3002cc OBC |
1578 | |
1579 | /* | |
1580 | * Keep iterating until we either unmap 'size' bytes (or more) | |
1581 | * or we hit an area that isn't mapped. | |
1582 | */ | |
1583 | while (unmapped < size) { | |
bd13969b | 1584 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
7d3002cc | 1585 | |
bd13969b | 1586 | unmapped_page = domain->ops->unmap(domain, iova, pgsize); |
7d3002cc OBC |
1587 | if (!unmapped_page) |
1588 | break; | |
1589 | ||
6197ca82 JP |
1590 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
1591 | iova, unmapped_page); | |
7d3002cc OBC |
1592 | |
1593 | iova += unmapped_page; | |
1594 | unmapped += unmapped_page; | |
1595 | } | |
1596 | ||
db8614d3 | 1597 | trace_unmap(orig_iova, size, unmapped); |
7d3002cc | 1598 | return unmapped; |
cefc53c7 JR |
1599 | } |
1600 | EXPORT_SYMBOL_GPL(iommu_unmap); | |
1460432c | 1601 | |
315786eb OH |
1602 | size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
1603 | struct scatterlist *sg, unsigned int nents, int prot) | |
1604 | { | |
38ec010d | 1605 | struct scatterlist *s; |
315786eb | 1606 | size_t mapped = 0; |
18f23409 | 1607 | unsigned int i, min_pagesz; |
38ec010d | 1608 | int ret; |
315786eb | 1609 | |
d16e0faa | 1610 | if (unlikely(domain->pgsize_bitmap == 0UL)) |
18f23409 | 1611 | return 0; |
315786eb | 1612 | |
d16e0faa | 1613 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
18f23409 RM |
1614 | |
1615 | for_each_sg(sg, s, nents, i) { | |
3e6110fd | 1616 | phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset; |
18f23409 RM |
1617 | |
1618 | /* | |
1619 | * We are mapping on IOMMU page boundaries, so offset within | |
1620 | * the page must be 0. However, the IOMMU may support pages | |
1621 | * smaller than PAGE_SIZE, so s->offset may still represent | |
1622 | * an offset of that boundary within the CPU page. | |
1623 | */ | |
1624 | if (!IS_ALIGNED(s->offset, min_pagesz)) | |
38ec010d JR |
1625 | goto out_err; |
1626 | ||
1627 | ret = iommu_map(domain, iova + mapped, phys, s->length, prot); | |
1628 | if (ret) | |
1629 | goto out_err; | |
1630 | ||
1631 | mapped += s->length; | |
315786eb OH |
1632 | } |
1633 | ||
1634 | return mapped; | |
38ec010d JR |
1635 | |
1636 | out_err: | |
1637 | /* undo mappings already done */ | |
1638 | iommu_unmap(domain, iova, mapped); | |
1639 | ||
1640 | return 0; | |
1641 | ||
315786eb OH |
1642 | } |
1643 | EXPORT_SYMBOL_GPL(default_iommu_map_sg); | |
d7787d57 JR |
1644 | |
1645 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, | |
80f97f0f | 1646 | phys_addr_t paddr, u64 size, int prot) |
d7787d57 JR |
1647 | { |
1648 | if (unlikely(domain->ops->domain_window_enable == NULL)) | |
1649 | return -ENODEV; | |
1650 | ||
80f97f0f VS |
1651 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
1652 | prot); | |
d7787d57 JR |
1653 | } |
1654 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); | |
1655 | ||
1656 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) | |
1657 | { | |
1658 | if (unlikely(domain->ops->domain_window_disable == NULL)) | |
1659 | return; | |
1660 | ||
1661 | return domain->ops->domain_window_disable(domain, wnd_nr); | |
1662 | } | |
1663 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); | |
1664 | ||
d72e31c9 | 1665 | static int __init iommu_init(void) |
1460432c | 1666 | { |
d72e31c9 AW |
1667 | iommu_group_kset = kset_create_and_add("iommu_groups", |
1668 | NULL, kernel_kobj); | |
d72e31c9 AW |
1669 | BUG_ON(!iommu_group_kset); |
1670 | ||
1671 | return 0; | |
1460432c | 1672 | } |
d7ef9995 | 1673 | core_initcall(iommu_init); |
0cd76dd1 JR |
1674 | |
1675 | int iommu_domain_get_attr(struct iommu_domain *domain, | |
1676 | enum iommu_attr attr, void *data) | |
1677 | { | |
0ff64f80 | 1678 | struct iommu_domain_geometry *geometry; |
d2e12160 | 1679 | bool *paging; |
0ff64f80 | 1680 | int ret = 0; |
69356712 | 1681 | u32 *count; |
0ff64f80 JR |
1682 | |
1683 | switch (attr) { | |
1684 | case DOMAIN_ATTR_GEOMETRY: | |
1685 | geometry = data; | |
1686 | *geometry = domain->geometry; | |
1687 | ||
d2e12160 JR |
1688 | break; |
1689 | case DOMAIN_ATTR_PAGING: | |
1690 | paging = data; | |
d16e0faa | 1691 | *paging = (domain->pgsize_bitmap != 0UL); |
69356712 JR |
1692 | break; |
1693 | case DOMAIN_ATTR_WINDOWS: | |
1694 | count = data; | |
1695 | ||
1696 | if (domain->ops->domain_get_windows != NULL) | |
1697 | *count = domain->ops->domain_get_windows(domain); | |
1698 | else | |
1699 | ret = -ENODEV; | |
1700 | ||
0ff64f80 JR |
1701 | break; |
1702 | default: | |
1703 | if (!domain->ops->domain_get_attr) | |
1704 | return -EINVAL; | |
0cd76dd1 | 1705 | |
0ff64f80 JR |
1706 | ret = domain->ops->domain_get_attr(domain, attr, data); |
1707 | } | |
1708 | ||
1709 | return ret; | |
0cd76dd1 JR |
1710 | } |
1711 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); | |
1712 | ||
1713 | int iommu_domain_set_attr(struct iommu_domain *domain, | |
1714 | enum iommu_attr attr, void *data) | |
1715 | { | |
69356712 JR |
1716 | int ret = 0; |
1717 | u32 *count; | |
1718 | ||
1719 | switch (attr) { | |
1720 | case DOMAIN_ATTR_WINDOWS: | |
1721 | count = data; | |
1722 | ||
1723 | if (domain->ops->domain_set_windows != NULL) | |
1724 | ret = domain->ops->domain_set_windows(domain, *count); | |
1725 | else | |
1726 | ret = -ENODEV; | |
1460432c | 1727 | |
69356712 JR |
1728 | break; |
1729 | default: | |
1730 | if (domain->ops->domain_set_attr == NULL) | |
1731 | return -EINVAL; | |
1732 | ||
1733 | ret = domain->ops->domain_set_attr(domain, attr, data); | |
1734 | } | |
1735 | ||
1736 | return ret; | |
1460432c | 1737 | } |
0cd76dd1 | 1738 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
a1015c2b | 1739 | |
865e51e5 | 1740 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
1741 | { |
1742 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
1743 | ||
865e51e5 EA |
1744 | if (ops && ops->get_resv_regions) |
1745 | ops->get_resv_regions(dev, list); | |
a1015c2b JR |
1746 | } |
1747 | ||
865e51e5 | 1748 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
1749 | { |
1750 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
1751 | ||
865e51e5 EA |
1752 | if (ops && ops->put_resv_regions) |
1753 | ops->put_resv_regions(dev, list); | |
a1015c2b | 1754 | } |
d290f1e7 | 1755 | |
d142b653 EA |
1756 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
1757 | size_t length, | |
1758 | int prot, int type) | |
1759 | { | |
1760 | struct iommu_resv_region *region; | |
1761 | ||
1762 | region = kzalloc(sizeof(*region), GFP_KERNEL); | |
1763 | if (!region) | |
1764 | return NULL; | |
1765 | ||
1766 | INIT_LIST_HEAD(®ion->list); | |
1767 | region->start = start; | |
1768 | region->length = length; | |
1769 | region->prot = prot; | |
1770 | region->type = type; | |
1771 | return region; | |
1772 | } | |
1773 | ||
d290f1e7 JR |
1774 | /* Request that a device is direct mapped by the IOMMU */ |
1775 | int iommu_request_dm_for_dev(struct device *dev) | |
1776 | { | |
1777 | struct iommu_domain *dm_domain; | |
1778 | struct iommu_group *group; | |
1779 | int ret; | |
1780 | ||
1781 | /* Device must already be in a group before calling this function */ | |
1782 | group = iommu_group_get_for_dev(dev); | |
409e553d DC |
1783 | if (IS_ERR(group)) |
1784 | return PTR_ERR(group); | |
d290f1e7 JR |
1785 | |
1786 | mutex_lock(&group->mutex); | |
1787 | ||
1788 | /* Check if the default domain is already direct mapped */ | |
1789 | ret = 0; | |
1790 | if (group->default_domain && | |
1791 | group->default_domain->type == IOMMU_DOMAIN_IDENTITY) | |
1792 | goto out; | |
1793 | ||
1794 | /* Don't change mappings of existing devices */ | |
1795 | ret = -EBUSY; | |
1796 | if (iommu_group_device_count(group) != 1) | |
1797 | goto out; | |
1798 | ||
1799 | /* Allocate a direct mapped domain */ | |
1800 | ret = -ENOMEM; | |
1801 | dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); | |
1802 | if (!dm_domain) | |
1803 | goto out; | |
1804 | ||
1805 | /* Attach the device to the domain */ | |
1806 | ret = __iommu_attach_group(dm_domain, group); | |
1807 | if (ret) { | |
1808 | iommu_domain_free(dm_domain); | |
1809 | goto out; | |
1810 | } | |
1811 | ||
1812 | /* Make the direct mapped domain the default for this group */ | |
1813 | if (group->default_domain) | |
1814 | iommu_domain_free(group->default_domain); | |
1815 | group->default_domain = dm_domain; | |
1816 | ||
1817 | pr_info("Using direct mapping for device %s\n", dev_name(dev)); | |
1818 | ||
1819 | ret = 0; | |
1820 | out: | |
1821 | mutex_unlock(&group->mutex); | |
1822 | iommu_group_put(group); | |
1823 | ||
1824 | return ret; | |
1825 | } | |
57f98d2f | 1826 | |
e4f10ffe LP |
1827 | struct iommu_instance { |
1828 | struct list_head list; | |
1829 | struct fwnode_handle *fwnode; | |
1830 | const struct iommu_ops *ops; | |
1831 | }; | |
1832 | static LIST_HEAD(iommu_instance_list); | |
1833 | static DEFINE_SPINLOCK(iommu_instance_lock); | |
1834 | ||
1835 | void iommu_register_instance(struct fwnode_handle *fwnode, | |
1836 | const struct iommu_ops *ops) | |
1837 | { | |
1838 | struct iommu_instance *iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); | |
1839 | ||
1840 | if (WARN_ON(!iommu)) | |
1841 | return; | |
1842 | ||
1843 | of_node_get(to_of_node(fwnode)); | |
1844 | INIT_LIST_HEAD(&iommu->list); | |
1845 | iommu->fwnode = fwnode; | |
1846 | iommu->ops = ops; | |
1847 | spin_lock(&iommu_instance_lock); | |
1848 | list_add_tail(&iommu->list, &iommu_instance_list); | |
1849 | spin_unlock(&iommu_instance_lock); | |
1850 | } | |
1851 | ||
c56bd31d | 1852 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
e4f10ffe LP |
1853 | { |
1854 | struct iommu_instance *instance; | |
1855 | const struct iommu_ops *ops = NULL; | |
1856 | ||
1857 | spin_lock(&iommu_instance_lock); | |
1858 | list_for_each_entry(instance, &iommu_instance_list, list) | |
1859 | if (instance->fwnode == fwnode) { | |
1860 | ops = instance->ops; | |
1861 | break; | |
1862 | } | |
1863 | spin_unlock(&iommu_instance_lock); | |
1864 | return ops; | |
1865 | } | |
1866 | ||
57f98d2f RM |
1867 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
1868 | const struct iommu_ops *ops) | |
1869 | { | |
1870 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1871 | ||
1872 | if (fwspec) | |
1873 | return ops == fwspec->ops ? 0 : -EINVAL; | |
1874 | ||
1875 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); | |
1876 | if (!fwspec) | |
1877 | return -ENOMEM; | |
1878 | ||
1879 | of_node_get(to_of_node(iommu_fwnode)); | |
1880 | fwspec->iommu_fwnode = iommu_fwnode; | |
1881 | fwspec->ops = ops; | |
1882 | dev->iommu_fwspec = fwspec; | |
1883 | return 0; | |
1884 | } | |
1885 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); | |
1886 | ||
1887 | void iommu_fwspec_free(struct device *dev) | |
1888 | { | |
1889 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1890 | ||
1891 | if (fwspec) { | |
1892 | fwnode_handle_put(fwspec->iommu_fwnode); | |
1893 | kfree(fwspec); | |
1894 | dev->iommu_fwspec = NULL; | |
1895 | } | |
1896 | } | |
1897 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); | |
1898 | ||
1899 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) | |
1900 | { | |
1901 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1902 | size_t size; | |
1903 | int i; | |
1904 | ||
1905 | if (!fwspec) | |
1906 | return -EINVAL; | |
1907 | ||
1908 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); | |
1909 | if (size > sizeof(*fwspec)) { | |
1910 | fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); | |
1911 | if (!fwspec) | |
1912 | return -ENOMEM; | |
1913 | } | |
1914 | ||
1915 | for (i = 0; i < num_ids; i++) | |
1916 | fwspec->ids[fwspec->num_ids + i] = ids[i]; | |
1917 | ||
1918 | fwspec->num_ids += num_ids; | |
1919 | dev->iommu_fwspec = fwspec; | |
1920 | return 0; | |
1921 | } | |
1922 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); |