1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 */
6
7 #define pr_fmt(fmt) "iommu: " fmt
8
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/iommu.h>
18 #include <linux/idr.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/pci.h>
22 #include <linux/bitops.h>
23 #include <linux/property.h>
24 #include <linux/fsl/mc.h>
25 #include <linux/module.h>
26 #include <trace/events/iommu.h>
27
28 static struct kset *iommu_group_kset;
29 static DEFINE_IDA(iommu_group_ida);
30
31 static unsigned int iommu_def_domain_type __read_mostly;
32 static bool iommu_dma_strict __read_mostly = true;
33 static u32 iommu_cmd_line __read_mostly;
34
35 struct iommu_group {
36 struct kobject kobj;
37 struct kobject *devices_kobj;
38 struct list_head devices;
39 struct mutex mutex;
40 struct blocking_notifier_head notifier;
41 void *iommu_data;
42 void (*iommu_data_release)(void *iommu_data);
43 char *name;
44 int id;
45 struct iommu_domain *default_domain;
46 struct iommu_domain *domain;
47 };
48
49 struct group_device {
50 struct list_head list;
51 struct device *dev;
52 char *name;
53 };
54
55 struct iommu_group_attribute {
56 struct attribute attr;
57 ssize_t (*show)(struct iommu_group *group, char *buf);
58 ssize_t (*store)(struct iommu_group *group,
59 const char *buf, size_t count);
60 };
61
62 static const char * const iommu_group_resv_type_string[] = {
63 [IOMMU_RESV_DIRECT] = "direct",
64 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
65 [IOMMU_RESV_RESERVED] = "reserved",
66 [IOMMU_RESV_MSI] = "msi",
67 [IOMMU_RESV_SW_MSI] = "msi",
68 };
69
70 #define IOMMU_CMD_LINE_DMA_API BIT(0)
71
iommu_set_cmd_line_dma_api(void)72 static void iommu_set_cmd_line_dma_api(void)
73 {
74 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
75 }
76
iommu_cmd_line_dma_api(void)77 static bool iommu_cmd_line_dma_api(void)
78 {
79 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
80 }
81
82 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
83 struct iommu_group_attribute iommu_group_attr_##_name = \
84 __ATTR(_name, _mode, _show, _store)
85
86 #define to_iommu_group_attr(_attr) \
87 container_of(_attr, struct iommu_group_attribute, attr)
88 #define to_iommu_group(_kobj) \
89 container_of(_kobj, struct iommu_group, kobj)
90
91 static LIST_HEAD(iommu_device_list);
92 static DEFINE_SPINLOCK(iommu_device_lock);
93
94 /*
95 * Use a function instead of an array here because the domain-type is a
96 * bit-field, so an array would waste memory.
97 */
iommu_domain_type_str(unsigned int t)98 static const char *iommu_domain_type_str(unsigned int t)
99 {
100 switch (t) {
101 case IOMMU_DOMAIN_BLOCKED:
102 return "Blocked";
103 case IOMMU_DOMAIN_IDENTITY:
104 return "Passthrough";
105 case IOMMU_DOMAIN_UNMANAGED:
106 return "Unmanaged";
107 case IOMMU_DOMAIN_DMA:
108 return "Translated";
109 default:
110 return "Unknown";
111 }
112 }
113
iommu_subsys_init(void)114 static int __init iommu_subsys_init(void)
115 {
116 bool cmd_line = iommu_cmd_line_dma_api();
117
118 if (!cmd_line) {
119 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
120 iommu_set_default_passthrough(false);
121 else
122 iommu_set_default_translated(false);
123
124 if (iommu_default_passthrough() && mem_encrypt_active()) {
125 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
126 iommu_set_default_translated(false);
127 }
128 }
129
130 pr_info("Default domain type: %s %s\n",
131 iommu_domain_type_str(iommu_def_domain_type),
132 cmd_line ? "(set via kernel command line)" : "");
133
134 return 0;
135 }
136 subsys_initcall(iommu_subsys_init);
137
iommu_device_register(struct iommu_device * iommu)138 int iommu_device_register(struct iommu_device *iommu)
139 {
140 spin_lock(&iommu_device_lock);
141 list_add_tail(&iommu->list, &iommu_device_list);
142 spin_unlock(&iommu_device_lock);
143 return 0;
144 }
145 EXPORT_SYMBOL_GPL(iommu_device_register);
146
iommu_device_unregister(struct iommu_device * iommu)147 void iommu_device_unregister(struct iommu_device *iommu)
148 {
149 spin_lock(&iommu_device_lock);
150 list_del(&iommu->list);
151 spin_unlock(&iommu_device_lock);
152 }
153 EXPORT_SYMBOL_GPL(iommu_device_unregister);
154
iommu_get_dev_param(struct device * dev)155 static struct iommu_param *iommu_get_dev_param(struct device *dev)
156 {
157 struct iommu_param *param = dev->iommu_param;
158
159 if (param)
160 return param;
161
162 param = kzalloc(sizeof(*param), GFP_KERNEL);
163 if (!param)
164 return NULL;
165
166 mutex_init(¶m->lock);
167 dev->iommu_param = param;
168 return param;
169 }
170
iommu_free_dev_param(struct device * dev)171 static void iommu_free_dev_param(struct device *dev)
172 {
173 kfree(dev->iommu_param);
174 dev->iommu_param = NULL;
175 }
176
iommu_probe_device(struct device * dev)177 int iommu_probe_device(struct device *dev)
178 {
179 const struct iommu_ops *ops = dev->bus->iommu_ops;
180 int ret;
181
182 WARN_ON(dev->iommu_group);
183 if (!ops)
184 return -EINVAL;
185
186 if (!iommu_get_dev_param(dev))
187 return -ENOMEM;
188
189 if (!try_module_get(ops->owner)) {
190 ret = -EINVAL;
191 goto err_free_dev_param;
192 }
193
194 ret = ops->add_device(dev);
195 if (ret)
196 goto err_module_put;
197
198 return 0;
199
200 err_module_put:
201 module_put(ops->owner);
202 err_free_dev_param:
203 iommu_free_dev_param(dev);
204 return ret;
205 }
206
iommu_release_device(struct device * dev)207 void iommu_release_device(struct device *dev)
208 {
209 const struct iommu_ops *ops = dev->bus->iommu_ops;
210
211 if (dev->iommu_group)
212 ops->remove_device(dev);
213
214 if (dev->iommu_param) {
215 module_put(ops->owner);
216 iommu_free_dev_param(dev);
217 }
218 }
219
220 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
221 unsigned type);
222 static int __iommu_attach_device(struct iommu_domain *domain,
223 struct device *dev);
224 static int __iommu_attach_group(struct iommu_domain *domain,
225 struct iommu_group *group);
226 static void __iommu_detach_group(struct iommu_domain *domain,
227 struct iommu_group *group);
228
iommu_set_def_domain_type(char * str)229 static int __init iommu_set_def_domain_type(char *str)
230 {
231 bool pt;
232 int ret;
233
234 ret = kstrtobool(str, &pt);
235 if (ret)
236 return ret;
237
238 if (pt)
239 iommu_set_default_passthrough(true);
240 else
241 iommu_set_default_translated(true);
242
243 return 0;
244 }
245 early_param("iommu.passthrough", iommu_set_def_domain_type);
246
iommu_dma_setup(char * str)247 static int __init iommu_dma_setup(char *str)
248 {
249 return kstrtobool(str, &iommu_dma_strict);
250 }
251 early_param("iommu.strict", iommu_dma_setup);
252
iommu_group_attr_show(struct kobject * kobj,struct attribute * __attr,char * buf)253 static ssize_t iommu_group_attr_show(struct kobject *kobj,
254 struct attribute *__attr, char *buf)
255 {
256 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
257 struct iommu_group *group = to_iommu_group(kobj);
258 ssize_t ret = -EIO;
259
260 if (attr->show)
261 ret = attr->show(group, buf);
262 return ret;
263 }
264
iommu_group_attr_store(struct kobject * kobj,struct attribute * __attr,const char * buf,size_t count)265 static ssize_t iommu_group_attr_store(struct kobject *kobj,
266 struct attribute *__attr,
267 const char *buf, size_t count)
268 {
269 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
270 struct iommu_group *group = to_iommu_group(kobj);
271 ssize_t ret = -EIO;
272
273 if (attr->store)
274 ret = attr->store(group, buf, count);
275 return ret;
276 }
277
278 static const struct sysfs_ops iommu_group_sysfs_ops = {
279 .show = iommu_group_attr_show,
280 .store = iommu_group_attr_store,
281 };
282
iommu_group_create_file(struct iommu_group * group,struct iommu_group_attribute * attr)283 static int iommu_group_create_file(struct iommu_group *group,
284 struct iommu_group_attribute *attr)
285 {
286 return sysfs_create_file(&group->kobj, &attr->attr);
287 }
288
iommu_group_remove_file(struct iommu_group * group,struct iommu_group_attribute * attr)289 static void iommu_group_remove_file(struct iommu_group *group,
290 struct iommu_group_attribute *attr)
291 {
292 sysfs_remove_file(&group->kobj, &attr->attr);
293 }
294
iommu_group_show_name(struct iommu_group * group,char * buf)295 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
296 {
297 return sprintf(buf, "%s\n", group->name);
298 }
299
300 /**
301 * iommu_insert_resv_region - Insert a new region in the
302 * list of reserved regions.
303 * @new: new region to insert
304 * @regions: list of regions
305 *
306 * Elements are sorted by start address and overlapping segments
307 * of the same type are merged.
308 */
iommu_insert_resv_region(struct iommu_resv_region * new,struct list_head * regions)309 int iommu_insert_resv_region(struct iommu_resv_region *new,
310 struct list_head *regions)
311 {
312 struct iommu_resv_region *iter, *tmp, *nr, *top;
313 LIST_HEAD(stack);
314
315 nr = iommu_alloc_resv_region(new->start, new->length,
316 new->prot, new->type);
317 if (!nr)
318 return -ENOMEM;
319
320 /* First add the new element based on start address sorting */
321 list_for_each_entry(iter, regions, list) {
322 if (nr->start < iter->start ||
323 (nr->start == iter->start && nr->type <= iter->type))
324 break;
325 }
326 list_add_tail(&nr->list, &iter->list);
327
328 /* Merge overlapping segments of type nr->type in @regions, if any */
329 list_for_each_entry_safe(iter, tmp, regions, list) {
330 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
331
332 /* no merge needed on elements of different types than @new */
333 if (iter->type != new->type) {
334 list_move_tail(&iter->list, &stack);
335 continue;
336 }
337
338 /* look for the last stack element of same type as @iter */
339 list_for_each_entry_reverse(top, &stack, list)
340 if (top->type == iter->type)
341 goto check_overlap;
342
343 list_move_tail(&iter->list, &stack);
344 continue;
345
346 check_overlap:
347 top_end = top->start + top->length - 1;
348
349 if (iter->start > top_end + 1) {
350 list_move_tail(&iter->list, &stack);
351 } else {
352 top->length = max(top_end, iter_end) - top->start + 1;
353 list_del(&iter->list);
354 kfree(iter);
355 }
356 }
357 list_splice(&stack, regions);
358 return 0;
359 }
360
361 static int
iommu_insert_device_resv_regions(struct list_head * dev_resv_regions,struct list_head * group_resv_regions)362 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
363 struct list_head *group_resv_regions)
364 {
365 struct iommu_resv_region *entry;
366 int ret = 0;
367
368 list_for_each_entry(entry, dev_resv_regions, list) {
369 ret = iommu_insert_resv_region(entry, group_resv_regions);
370 if (ret)
371 break;
372 }
373 return ret;
374 }
375
iommu_get_group_resv_regions(struct iommu_group * group,struct list_head * head)376 int iommu_get_group_resv_regions(struct iommu_group *group,
377 struct list_head *head)
378 {
379 struct group_device *device;
380 int ret = 0;
381
382 mutex_lock(&group->mutex);
383 list_for_each_entry(device, &group->devices, list) {
384 struct list_head dev_resv_regions;
385
386 INIT_LIST_HEAD(&dev_resv_regions);
387 iommu_get_resv_regions(device->dev, &dev_resv_regions);
388 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
389 iommu_put_resv_regions(device->dev, &dev_resv_regions);
390 if (ret)
391 break;
392 }
393 mutex_unlock(&group->mutex);
394 return ret;
395 }
396 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
397
iommu_group_show_resv_regions(struct iommu_group * group,char * buf)398 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
399 char *buf)
400 {
401 struct iommu_resv_region *region, *next;
402 struct list_head group_resv_regions;
403 char *str = buf;
404
405 INIT_LIST_HEAD(&group_resv_regions);
406 iommu_get_group_resv_regions(group, &group_resv_regions);
407
408 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
409 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
410 (long long int)region->start,
411 (long long int)(region->start +
412 region->length - 1),
413 iommu_group_resv_type_string[region->type]);
414 kfree(region);
415 }
416
417 return (str - buf);
418 }
419
iommu_group_show_type(struct iommu_group * group,char * buf)420 static ssize_t iommu_group_show_type(struct iommu_group *group,
421 char *buf)
422 {
423 char *type = "unknown\n";
424
425 if (group->default_domain) {
426 switch (group->default_domain->type) {
427 case IOMMU_DOMAIN_BLOCKED:
428 type = "blocked\n";
429 break;
430 case IOMMU_DOMAIN_IDENTITY:
431 type = "identity\n";
432 break;
433 case IOMMU_DOMAIN_UNMANAGED:
434 type = "unmanaged\n";
435 break;
436 case IOMMU_DOMAIN_DMA:
437 type = "DMA\n";
438 break;
439 }
440 }
441 strcpy(buf, type);
442
443 return strlen(type);
444 }
445
446 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
447
448 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
449 iommu_group_show_resv_regions, NULL);
450
451 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
452
iommu_group_release(struct kobject * kobj)453 static void iommu_group_release(struct kobject *kobj)
454 {
455 struct iommu_group *group = to_iommu_group(kobj);
456
457 pr_debug("Releasing group %d\n", group->id);
458
459 if (group->iommu_data_release)
460 group->iommu_data_release(group->iommu_data);
461
462 ida_simple_remove(&iommu_group_ida, group->id);
463
464 if (group->default_domain)
465 iommu_domain_free(group->default_domain);
466
467 kfree(group->name);
468 kfree(group);
469 }
470
471 static struct kobj_type iommu_group_ktype = {
472 .sysfs_ops = &iommu_group_sysfs_ops,
473 .release = iommu_group_release,
474 };
475
476 /**
477 * iommu_group_alloc - Allocate a new group
478 *
479 * This function is called by an iommu driver to allocate a new iommu
480 * group. The iommu group represents the minimum granularity of the iommu.
481 * Upon successful return, the caller holds a reference to the supplied
482 * group in order to hold the group until devices are added. Use
483 * iommu_group_put() to release this extra reference count, allowing the
484 * group to be automatically reclaimed once it has no devices or external
485 * references.
486 */
iommu_group_alloc(void)487 struct iommu_group *iommu_group_alloc(void)
488 {
489 struct iommu_group *group;
490 int ret;
491
492 group = kzalloc(sizeof(*group), GFP_KERNEL);
493 if (!group)
494 return ERR_PTR(-ENOMEM);
495
496 group->kobj.kset = iommu_group_kset;
497 mutex_init(&group->mutex);
498 INIT_LIST_HEAD(&group->devices);
499 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
500
501 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
502 if (ret < 0) {
503 kfree(group);
504 return ERR_PTR(ret);
505 }
506 group->id = ret;
507
508 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
509 NULL, "%d", group->id);
510 if (ret) {
511 ida_simple_remove(&iommu_group_ida, group->id);
512 kfree(group);
513 return ERR_PTR(ret);
514 }
515
516 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
517 if (!group->devices_kobj) {
518 kobject_put(&group->kobj); /* triggers .release & free */
519 return ERR_PTR(-ENOMEM);
520 }
521
522 /*
523 * The devices_kobj holds a reference on the group kobject, so
524 * as long as that exists so will the group. We can therefore
525 * use the devices_kobj for reference counting.
526 */
527 kobject_put(&group->kobj);
528
529 ret = iommu_group_create_file(group,
530 &iommu_group_attr_reserved_regions);
531 if (ret)
532 return ERR_PTR(ret);
533
534 ret = iommu_group_create_file(group, &iommu_group_attr_type);
535 if (ret)
536 return ERR_PTR(ret);
537
538 pr_debug("Allocated group %d\n", group->id);
539
540 return group;
541 }
542 EXPORT_SYMBOL_GPL(iommu_group_alloc);
543
iommu_group_get_by_id(int id)544 struct iommu_group *iommu_group_get_by_id(int id)
545 {
546 struct kobject *group_kobj;
547 struct iommu_group *group;
548 const char *name;
549
550 if (!iommu_group_kset)
551 return NULL;
552
553 name = kasprintf(GFP_KERNEL, "%d", id);
554 if (!name)
555 return NULL;
556
557 group_kobj = kset_find_obj(iommu_group_kset, name);
558 kfree(name);
559
560 if (!group_kobj)
561 return NULL;
562
563 group = container_of(group_kobj, struct iommu_group, kobj);
564 BUG_ON(group->id != id);
565
566 kobject_get(group->devices_kobj);
567 kobject_put(&group->kobj);
568
569 return group;
570 }
571 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
572
573 /**
574 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
575 * @group: the group
576 *
577 * iommu drivers can store data in the group for use when doing iommu
578 * operations. This function provides a way to retrieve it. Caller
579 * should hold a group reference.
580 */
iommu_group_get_iommudata(struct iommu_group * group)581 void *iommu_group_get_iommudata(struct iommu_group *group)
582 {
583 return group->iommu_data;
584 }
585 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
586
587 /**
588 * iommu_group_set_iommudata - set iommu_data for a group
589 * @group: the group
590 * @iommu_data: new data
591 * @release: release function for iommu_data
592 *
593 * iommu drivers can store data in the group for use when doing iommu
594 * operations. This function provides a way to set the data after
595 * the group has been allocated. Caller should hold a group reference.
596 */
iommu_group_set_iommudata(struct iommu_group * group,void * iommu_data,void (* release)(void * iommu_data))597 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
598 void (*release)(void *iommu_data))
599 {
600 group->iommu_data = iommu_data;
601 group->iommu_data_release = release;
602 }
603 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
604
605 /**
606 * iommu_group_set_name - set name for a group
607 * @group: the group
608 * @name: name
609 *
610 * Allow iommu driver to set a name for a group. When set it will
611 * appear in a name attribute file under the group in sysfs.
612 */
iommu_group_set_name(struct iommu_group * group,const char * name)613 int iommu_group_set_name(struct iommu_group *group, const char *name)
614 {
615 int ret;
616
617 if (group->name) {
618 iommu_group_remove_file(group, &iommu_group_attr_name);
619 kfree(group->name);
620 group->name = NULL;
621 if (!name)
622 return 0;
623 }
624
625 group->name = kstrdup(name, GFP_KERNEL);
626 if (!group->name)
627 return -ENOMEM;
628
629 ret = iommu_group_create_file(group, &iommu_group_attr_name);
630 if (ret) {
631 kfree(group->name);
632 group->name = NULL;
633 return ret;
634 }
635
636 return 0;
637 }
638 EXPORT_SYMBOL_GPL(iommu_group_set_name);
639
iommu_group_create_direct_mappings(struct iommu_group * group,struct device * dev)640 static int iommu_group_create_direct_mappings(struct iommu_group *group,
641 struct device *dev)
642 {
643 struct iommu_domain *domain = group->default_domain;
644 struct iommu_resv_region *entry;
645 struct list_head mappings;
646 unsigned long pg_size;
647 int ret = 0;
648
649 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
650 return 0;
651
652 BUG_ON(!domain->pgsize_bitmap);
653
654 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
655 INIT_LIST_HEAD(&mappings);
656
657 iommu_get_resv_regions(dev, &mappings);
658
659 /* We need to consider overlapping regions for different devices */
660 list_for_each_entry(entry, &mappings, list) {
661 dma_addr_t start, end, addr;
662
663 if (domain->ops->apply_resv_region)
664 domain->ops->apply_resv_region(dev, domain, entry);
665
666 start = ALIGN(entry->start, pg_size);
667 end = ALIGN(entry->start + entry->length, pg_size);
668
669 if (entry->type != IOMMU_RESV_DIRECT &&
670 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
671 continue;
672
673 for (addr = start; addr < end; addr += pg_size) {
674 phys_addr_t phys_addr;
675
676 phys_addr = iommu_iova_to_phys(domain, addr);
677 if (phys_addr)
678 continue;
679
680 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
681 if (ret)
682 goto out;
683 }
684
685 }
686
687 iommu_flush_tlb_all(domain);
688
689 out:
690 iommu_put_resv_regions(dev, &mappings);
691
692 return ret;
693 }
694
695 /**
696 * iommu_group_add_device - add a device to an iommu group
697 * @group: the group into which to add the device (reference should be held)
698 * @dev: the device
699 *
700 * This function is called by an iommu driver to add a device into a
701 * group. Adding a device increments the group reference count.
702 */
iommu_group_add_device(struct iommu_group * group,struct device * dev)703 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
704 {
705 int ret, i = 0;
706 struct group_device *device;
707
708 device = kzalloc(sizeof(*device), GFP_KERNEL);
709 if (!device)
710 return -ENOMEM;
711
712 device->dev = dev;
713
714 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
715 if (ret)
716 goto err_free_device;
717
718 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
719 rename:
720 if (!device->name) {
721 ret = -ENOMEM;
722 goto err_remove_link;
723 }
724
725 ret = sysfs_create_link_nowarn(group->devices_kobj,
726 &dev->kobj, device->name);
727 if (ret) {
728 if (ret == -EEXIST && i >= 0) {
729 /*
730 * Account for the slim chance of collision
731 * and append an instance to the name.
732 */
733 kfree(device->name);
734 device->name = kasprintf(GFP_KERNEL, "%s.%d",
735 kobject_name(&dev->kobj), i++);
736 goto rename;
737 }
738 goto err_free_name;
739 }
740
741 kobject_get(group->devices_kobj);
742
743 dev->iommu_group = group;
744
745 iommu_group_create_direct_mappings(group, dev);
746
747 mutex_lock(&group->mutex);
748 list_add_tail(&device->list, &group->devices);
749 if (group->domain)
750 ret = __iommu_attach_device(group->domain, dev);
751 mutex_unlock(&group->mutex);
752 if (ret)
753 goto err_put_group;
754
755 /* Notify any listeners about change to group. */
756 blocking_notifier_call_chain(&group->notifier,
757 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
758
759 trace_add_device_to_group(group->id, dev);
760
761 dev_info(dev, "Adding to iommu group %d\n", group->id);
762
763 return 0;
764
765 err_put_group:
766 mutex_lock(&group->mutex);
767 list_del(&device->list);
768 mutex_unlock(&group->mutex);
769 dev->iommu_group = NULL;
770 kobject_put(group->devices_kobj);
771 sysfs_remove_link(group->devices_kobj, device->name);
772 err_free_name:
773 kfree(device->name);
774 err_remove_link:
775 sysfs_remove_link(&dev->kobj, "iommu_group");
776 err_free_device:
777 kfree(device);
778 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
779 return ret;
780 }
781 EXPORT_SYMBOL_GPL(iommu_group_add_device);
782
783 /**
784 * iommu_group_remove_device - remove a device from it's current group
785 * @dev: device to be removed
786 *
787 * This function is called by an iommu driver to remove the device from
788 * it's current group. This decrements the iommu group reference count.
789 */
iommu_group_remove_device(struct device * dev)790 void iommu_group_remove_device(struct device *dev)
791 {
792 struct iommu_group *group = dev->iommu_group;
793 struct group_device *tmp_device, *device = NULL;
794
795 dev_info(dev, "Removing from iommu group %d\n", group->id);
796
797 /* Pre-notify listeners that a device is being removed. */
798 blocking_notifier_call_chain(&group->notifier,
799 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
800
801 mutex_lock(&group->mutex);
802 list_for_each_entry(tmp_device, &group->devices, list) {
803 if (tmp_device->dev == dev) {
804 device = tmp_device;
805 list_del(&device->list);
806 break;
807 }
808 }
809 mutex_unlock(&group->mutex);
810
811 if (!device)
812 return;
813
814 sysfs_remove_link(group->devices_kobj, device->name);
815 sysfs_remove_link(&dev->kobj, "iommu_group");
816
817 trace_remove_device_from_group(group->id, dev);
818
819 kfree(device->name);
820 kfree(device);
821 dev->iommu_group = NULL;
822 kobject_put(group->devices_kobj);
823 }
824 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
825
iommu_group_device_count(struct iommu_group * group)826 static int iommu_group_device_count(struct iommu_group *group)
827 {
828 struct group_device *entry;
829 int ret = 0;
830
831 list_for_each_entry(entry, &group->devices, list)
832 ret++;
833
834 return ret;
835 }
836
837 /**
838 * iommu_group_for_each_dev - iterate over each device in the group
839 * @group: the group
840 * @data: caller opaque data to be passed to callback function
841 * @fn: caller supplied callback function
842 *
843 * This function is called by group users to iterate over group devices.
844 * Callers should hold a reference count to the group during callback.
845 * The group->mutex is held across callbacks, which will block calls to
846 * iommu_group_add/remove_device.
847 */
__iommu_group_for_each_dev(struct iommu_group * group,void * data,int (* fn)(struct device *,void *))848 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
849 int (*fn)(struct device *, void *))
850 {
851 struct group_device *device;
852 int ret = 0;
853
854 list_for_each_entry(device, &group->devices, list) {
855 ret = fn(device->dev, data);
856 if (ret)
857 break;
858 }
859 return ret;
860 }
861
862
iommu_group_for_each_dev(struct iommu_group * group,void * data,int (* fn)(struct device *,void *))863 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
864 int (*fn)(struct device *, void *))
865 {
866 int ret;
867
868 mutex_lock(&group->mutex);
869 ret = __iommu_group_for_each_dev(group, data, fn);
870 mutex_unlock(&group->mutex);
871
872 return ret;
873 }
874 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
875
876 /**
877 * iommu_group_get - Return the group for a device and increment reference
878 * @dev: get the group that this device belongs to
879 *
880 * This function is called by iommu drivers and users to get the group
881 * for the specified device. If found, the group is returned and the group
882 * reference in incremented, else NULL.
883 */
iommu_group_get(struct device * dev)884 struct iommu_group *iommu_group_get(struct device *dev)
885 {
886 struct iommu_group *group = dev->iommu_group;
887
888 if (group)
889 kobject_get(group->devices_kobj);
890
891 return group;
892 }
893 EXPORT_SYMBOL_GPL(iommu_group_get);
894
895 /**
896 * iommu_group_ref_get - Increment reference on a group
897 * @group: the group to use, must not be NULL
898 *
899 * This function is called by iommu drivers to take additional references on an
900 * existing group. Returns the given group for convenience.
901 */
iommu_group_ref_get(struct iommu_group * group)902 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
903 {
904 kobject_get(group->devices_kobj);
905 return group;
906 }
907 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
908
909 /**
910 * iommu_group_put - Decrement group reference
911 * @group: the group to use
912 *
913 * This function is called by iommu drivers and users to release the
914 * iommu group. Once the reference count is zero, the group is released.
915 */
iommu_group_put(struct iommu_group * group)916 void iommu_group_put(struct iommu_group *group)
917 {
918 if (group)
919 kobject_put(group->devices_kobj);
920 }
921 EXPORT_SYMBOL_GPL(iommu_group_put);
922
923 /**
924 * iommu_group_register_notifier - Register a notifier for group changes
925 * @group: the group to watch
926 * @nb: notifier block to signal
927 *
928 * This function allows iommu group users to track changes in a group.
929 * See include/linux/iommu.h for actions sent via this notifier. Caller
930 * should hold a reference to the group throughout notifier registration.
931 */
iommu_group_register_notifier(struct iommu_group * group,struct notifier_block * nb)932 int iommu_group_register_notifier(struct iommu_group *group,
933 struct notifier_block *nb)
934 {
935 return blocking_notifier_chain_register(&group->notifier, nb);
936 }
937 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
938
939 /**
940 * iommu_group_unregister_notifier - Unregister a notifier
941 * @group: the group to watch
942 * @nb: notifier block to signal
943 *
944 * Unregister a previously registered group notifier block.
945 */
iommu_group_unregister_notifier(struct iommu_group * group,struct notifier_block * nb)946 int iommu_group_unregister_notifier(struct iommu_group *group,
947 struct notifier_block *nb)
948 {
949 return blocking_notifier_chain_unregister(&group->notifier, nb);
950 }
951 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
952
953 /**
954 * iommu_register_device_fault_handler() - Register a device fault handler
955 * @dev: the device
956 * @handler: the fault handler
957 * @data: private data passed as argument to the handler
958 *
959 * When an IOMMU fault event is received, this handler gets called with the
960 * fault event and data as argument. The handler should return 0 on success. If
961 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
962 * complete the fault by calling iommu_page_response() with one of the following
963 * response code:
964 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
965 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
966 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
967 * page faults if possible.
968 *
969 * Return 0 if the fault handler was installed successfully, or an error.
970 */
iommu_register_device_fault_handler(struct device * dev,iommu_dev_fault_handler_t handler,void * data)971 int iommu_register_device_fault_handler(struct device *dev,
972 iommu_dev_fault_handler_t handler,
973 void *data)
974 {
975 struct iommu_param *param = dev->iommu_param;
976 int ret = 0;
977
978 if (!param)
979 return -EINVAL;
980
981 mutex_lock(¶m->lock);
982 /* Only allow one fault handler registered for each device */
983 if (param->fault_param) {
984 ret = -EBUSY;
985 goto done_unlock;
986 }
987
988 get_device(dev);
989 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
990 if (!param->fault_param) {
991 put_device(dev);
992 ret = -ENOMEM;
993 goto done_unlock;
994 }
995 param->fault_param->handler = handler;
996 param->fault_param->data = data;
997 mutex_init(¶m->fault_param->lock);
998 INIT_LIST_HEAD(¶m->fault_param->faults);
999
1000 done_unlock:
1001 mutex_unlock(¶m->lock);
1002
1003 return ret;
1004 }
1005 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1006
1007 /**
1008 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1009 * @dev: the device
1010 *
1011 * Remove the device fault handler installed with
1012 * iommu_register_device_fault_handler().
1013 *
1014 * Return 0 on success, or an error.
1015 */
iommu_unregister_device_fault_handler(struct device * dev)1016 int iommu_unregister_device_fault_handler(struct device *dev)
1017 {
1018 struct iommu_param *param = dev->iommu_param;
1019 int ret = 0;
1020
1021 if (!param)
1022 return -EINVAL;
1023
1024 mutex_lock(¶m->lock);
1025
1026 if (!param->fault_param)
1027 goto unlock;
1028
1029 /* we cannot unregister handler if there are pending faults */
1030 if (!list_empty(¶m->fault_param->faults)) {
1031 ret = -EBUSY;
1032 goto unlock;
1033 }
1034
1035 kfree(param->fault_param);
1036 param->fault_param = NULL;
1037 put_device(dev);
1038 unlock:
1039 mutex_unlock(¶m->lock);
1040
1041 return ret;
1042 }
1043 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1044
1045 /**
1046 * iommu_report_device_fault() - Report fault event to device driver
1047 * @dev: the device
1048 * @evt: fault event data
1049 *
1050 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1051 * handler. When this function fails and the fault is recoverable, it is the
1052 * caller's responsibility to complete the fault.
1053 *
1054 * Return 0 on success, or an error.
1055 */
iommu_report_device_fault(struct device * dev,struct iommu_fault_event * evt)1056 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1057 {
1058 struct iommu_param *param = dev->iommu_param;
1059 struct iommu_fault_event *evt_pending = NULL;
1060 struct iommu_fault_param *fparam;
1061 int ret = 0;
1062
1063 if (!param || !evt)
1064 return -EINVAL;
1065
1066 /* we only report device fault if there is a handler registered */
1067 mutex_lock(¶m->lock);
1068 fparam = param->fault_param;
1069 if (!fparam || !fparam->handler) {
1070 ret = -EINVAL;
1071 goto done_unlock;
1072 }
1073
1074 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1075 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1076 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1077 GFP_KERNEL);
1078 if (!evt_pending) {
1079 ret = -ENOMEM;
1080 goto done_unlock;
1081 }
1082 mutex_lock(&fparam->lock);
1083 list_add_tail(&evt_pending->list, &fparam->faults);
1084 mutex_unlock(&fparam->lock);
1085 }
1086
1087 ret = fparam->handler(&evt->fault, fparam->data);
1088 if (ret && evt_pending) {
1089 mutex_lock(&fparam->lock);
1090 list_del(&evt_pending->list);
1091 mutex_unlock(&fparam->lock);
1092 kfree(evt_pending);
1093 }
1094 done_unlock:
1095 mutex_unlock(¶m->lock);
1096 return ret;
1097 }
1098 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1099
iommu_page_response(struct device * dev,struct iommu_page_response * msg)1100 int iommu_page_response(struct device *dev,
1101 struct iommu_page_response *msg)
1102 {
1103 bool pasid_valid;
1104 int ret = -EINVAL;
1105 struct iommu_fault_event *evt;
1106 struct iommu_fault_page_request *prm;
1107 struct iommu_param *param = dev->iommu_param;
1108 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1109
1110 if (!domain || !domain->ops->page_response)
1111 return -ENODEV;
1112
1113 if (!param || !param->fault_param)
1114 return -EINVAL;
1115
1116 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1117 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1118 return -EINVAL;
1119
1120 /* Only send response if there is a fault report pending */
1121 mutex_lock(¶m->fault_param->lock);
1122 if (list_empty(¶m->fault_param->faults)) {
1123 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1124 goto done_unlock;
1125 }
1126 /*
1127 * Check if we have a matching page request pending to respond,
1128 * otherwise return -EINVAL
1129 */
1130 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1131 prm = &evt->fault.prm;
1132 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1133
1134 if ((pasid_valid && prm->pasid != msg->pasid) ||
1135 prm->grpid != msg->grpid)
1136 continue;
1137
1138 /* Sanitize the reply */
1139 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1140
1141 ret = domain->ops->page_response(dev, evt, msg);
1142 list_del(&evt->list);
1143 kfree(evt);
1144 break;
1145 }
1146
1147 done_unlock:
1148 mutex_unlock(¶m->fault_param->lock);
1149 return ret;
1150 }
1151 EXPORT_SYMBOL_GPL(iommu_page_response);
1152
1153 /**
1154 * iommu_group_id - Return ID for a group
1155 * @group: the group to ID
1156 *
1157 * Return the unique ID for the group matching the sysfs group number.
1158 */
iommu_group_id(struct iommu_group * group)1159 int iommu_group_id(struct iommu_group *group)
1160 {
1161 return group->id;
1162 }
1163 EXPORT_SYMBOL_GPL(iommu_group_id);
1164
1165 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1166 unsigned long *devfns);
1167
1168 /*
1169 * To consider a PCI device isolated, we require ACS to support Source
1170 * Validation, Request Redirection, Completer Redirection, and Upstream
1171 * Forwarding. This effectively means that devices cannot spoof their
1172 * requester ID, requests and completions cannot be redirected, and all
1173 * transactions are forwarded upstream, even as it passes through a
1174 * bridge where the target device is downstream.
1175 */
1176 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1177
1178 /*
1179 * For multifunction devices which are not isolated from each other, find
1180 * all the other non-isolated functions and look for existing groups. For
1181 * each function, we also need to look for aliases to or from other devices
1182 * that may already have a group.
1183 */
get_pci_function_alias_group(struct pci_dev * pdev,unsigned long * devfns)1184 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1185 unsigned long *devfns)
1186 {
1187 struct pci_dev *tmp = NULL;
1188 struct iommu_group *group;
1189
1190 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1191 return NULL;
1192
1193 for_each_pci_dev(tmp) {
1194 if (tmp == pdev || tmp->bus != pdev->bus ||
1195 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1196 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1197 continue;
1198
1199 group = get_pci_alias_group(tmp, devfns);
1200 if (group) {
1201 pci_dev_put(tmp);
1202 return group;
1203 }
1204 }
1205
1206 return NULL;
1207 }
1208
1209 /*
1210 * Look for aliases to or from the given device for existing groups. DMA
1211 * aliases are only supported on the same bus, therefore the search
1212 * space is quite small (especially since we're really only looking at pcie
1213 * device, and therefore only expect multiple slots on the root complex or
1214 * downstream switch ports). It's conceivable though that a pair of
1215 * multifunction devices could have aliases between them that would cause a
1216 * loop. To prevent this, we use a bitmap to track where we've been.
1217 */
get_pci_alias_group(struct pci_dev * pdev,unsigned long * devfns)1218 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1219 unsigned long *devfns)
1220 {
1221 struct pci_dev *tmp = NULL;
1222 struct iommu_group *group;
1223
1224 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1225 return NULL;
1226
1227 group = iommu_group_get(&pdev->dev);
1228 if (group)
1229 return group;
1230
1231 for_each_pci_dev(tmp) {
1232 if (tmp == pdev || tmp->bus != pdev->bus)
1233 continue;
1234
1235 /* We alias them or they alias us */
1236 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1237 group = get_pci_alias_group(tmp, devfns);
1238 if (group) {
1239 pci_dev_put(tmp);
1240 return group;
1241 }
1242
1243 group = get_pci_function_alias_group(tmp, devfns);
1244 if (group) {
1245 pci_dev_put(tmp);
1246 return group;
1247 }
1248 }
1249 }
1250
1251 return NULL;
1252 }
1253
1254 struct group_for_pci_data {
1255 struct pci_dev *pdev;
1256 struct iommu_group *group;
1257 };
1258
1259 /*
1260 * DMA alias iterator callback, return the last seen device. Stop and return
1261 * the IOMMU group if we find one along the way.
1262 */
get_pci_alias_or_group(struct pci_dev * pdev,u16 alias,void * opaque)1263 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1264 {
1265 struct group_for_pci_data *data = opaque;
1266
1267 data->pdev = pdev;
1268 data->group = iommu_group_get(&pdev->dev);
1269
1270 return data->group != NULL;
1271 }
1272
1273 /*
1274 * Generic device_group call-back function. It just allocates one
1275 * iommu-group per device.
1276 */
generic_device_group(struct device * dev)1277 struct iommu_group *generic_device_group(struct device *dev)
1278 {
1279 return iommu_group_alloc();
1280 }
1281 EXPORT_SYMBOL_GPL(generic_device_group);
1282
1283 /*
1284 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1285 * to find or create an IOMMU group for a device.
1286 */
pci_device_group(struct device * dev)1287 struct iommu_group *pci_device_group(struct device *dev)
1288 {
1289 struct pci_dev *pdev = to_pci_dev(dev);
1290 struct group_for_pci_data data;
1291 struct pci_bus *bus;
1292 struct iommu_group *group = NULL;
1293 u64 devfns[4] = { 0 };
1294
1295 if (WARN_ON(!dev_is_pci(dev)))
1296 return ERR_PTR(-EINVAL);
1297
1298 /*
1299 * Find the upstream DMA alias for the device. A device must not
1300 * be aliased due to topology in order to have its own IOMMU group.
1301 * If we find an alias along the way that already belongs to a
1302 * group, use it.
1303 */
1304 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1305 return data.group;
1306
1307 pdev = data.pdev;
1308
1309 /*
1310 * Continue upstream from the point of minimum IOMMU granularity
1311 * due to aliases to the point where devices are protected from
1312 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1313 * group, use it.
1314 */
1315 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1316 if (!bus->self)
1317 continue;
1318
1319 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1320 break;
1321
1322 pdev = bus->self;
1323
1324 group = iommu_group_get(&pdev->dev);
1325 if (group)
1326 return group;
1327 }
1328
1329 /*
1330 * Look for existing groups on device aliases. If we alias another
1331 * device or another device aliases us, use the same group.
1332 */
1333 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1334 if (group)
1335 return group;
1336
1337 /*
1338 * Look for existing groups on non-isolated functions on the same
1339 * slot and aliases of those funcions, if any. No need to clear
1340 * the search bitmap, the tested devfns are still valid.
1341 */
1342 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1343 if (group)
1344 return group;
1345
1346 /* No shared group found, allocate new */
1347 return iommu_group_alloc();
1348 }
1349 EXPORT_SYMBOL_GPL(pci_device_group);
1350
1351 /* Get the IOMMU group for device on fsl-mc bus */
fsl_mc_device_group(struct device * dev)1352 struct iommu_group *fsl_mc_device_group(struct device *dev)
1353 {
1354 struct device *cont_dev = fsl_mc_cont_dev(dev);
1355 struct iommu_group *group;
1356
1357 group = iommu_group_get(cont_dev);
1358 if (!group)
1359 group = iommu_group_alloc();
1360 return group;
1361 }
1362 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1363
1364 /**
1365 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1366 * @dev: target device
1367 *
1368 * This function is intended to be called by IOMMU drivers and extended to
1369 * support common, bus-defined algorithms when determining or creating the
1370 * IOMMU group for a device. On success, the caller will hold a reference
1371 * to the returned IOMMU group, which will already include the provided
1372 * device. The reference should be released with iommu_group_put().
1373 */
iommu_group_get_for_dev(struct device * dev)1374 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1375 {
1376 const struct iommu_ops *ops = dev->bus->iommu_ops;
1377 struct iommu_group *group;
1378 int ret;
1379
1380 group = iommu_group_get(dev);
1381 if (group)
1382 return group;
1383
1384 if (!ops)
1385 return ERR_PTR(-EINVAL);
1386
1387 group = ops->device_group(dev);
1388 if (WARN_ON_ONCE(group == NULL))
1389 return ERR_PTR(-EINVAL);
1390
1391 if (IS_ERR(group))
1392 return group;
1393
1394 /*
1395 * Try to allocate a default domain - needs support from the
1396 * IOMMU driver.
1397 */
1398 if (!group->default_domain) {
1399 struct iommu_domain *dom;
1400
1401 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1402 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1403 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1404 if (dom) {
1405 dev_warn(dev,
1406 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1407 iommu_def_domain_type);
1408 }
1409 }
1410
1411 group->default_domain = dom;
1412 if (!group->domain)
1413 group->domain = dom;
1414
1415 if (dom && !iommu_dma_strict) {
1416 int attr = 1;
1417 iommu_domain_set_attr(dom,
1418 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1419 &attr);
1420 }
1421 }
1422
1423 ret = iommu_group_add_device(group, dev);
1424 if (ret) {
1425 iommu_group_put(group);
1426 return ERR_PTR(ret);
1427 }
1428
1429 return group;
1430 }
1431 EXPORT_SYMBOL(iommu_group_get_for_dev);
1432
iommu_group_default_domain(struct iommu_group * group)1433 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1434 {
1435 return group->default_domain;
1436 }
1437
add_iommu_group(struct device * dev,void * data)1438 static int add_iommu_group(struct device *dev, void *data)
1439 {
1440 int ret = iommu_probe_device(dev);
1441
1442 /*
1443 * We ignore -ENODEV errors for now, as they just mean that the
1444 * device is not translated by an IOMMU. We still care about
1445 * other errors and fail to initialize when they happen.
1446 */
1447 if (ret == -ENODEV)
1448 ret = 0;
1449
1450 return ret;
1451 }
1452
remove_iommu_group(struct device * dev,void * data)1453 static int remove_iommu_group(struct device *dev, void *data)
1454 {
1455 iommu_release_device(dev);
1456
1457 return 0;
1458 }
1459
iommu_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1460 static int iommu_bus_notifier(struct notifier_block *nb,
1461 unsigned long action, void *data)
1462 {
1463 unsigned long group_action = 0;
1464 struct device *dev = data;
1465 struct iommu_group *group;
1466
1467 /*
1468 * ADD/DEL call into iommu driver ops if provided, which may
1469 * result in ADD/DEL notifiers to group->notifier
1470 */
1471 if (action == BUS_NOTIFY_ADD_DEVICE) {
1472 int ret;
1473
1474 ret = iommu_probe_device(dev);
1475 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1476 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1477 iommu_release_device(dev);
1478 return NOTIFY_OK;
1479 }
1480
1481 /*
1482 * Remaining BUS_NOTIFYs get filtered and republished to the
1483 * group, if anyone is listening
1484 */
1485 group = iommu_group_get(dev);
1486 if (!group)
1487 return 0;
1488
1489 switch (action) {
1490 case BUS_NOTIFY_BIND_DRIVER:
1491 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1492 break;
1493 case BUS_NOTIFY_BOUND_DRIVER:
1494 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1495 break;
1496 case BUS_NOTIFY_UNBIND_DRIVER:
1497 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1498 break;
1499 case BUS_NOTIFY_UNBOUND_DRIVER:
1500 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1501 break;
1502 }
1503
1504 if (group_action)
1505 blocking_notifier_call_chain(&group->notifier,
1506 group_action, dev);
1507
1508 iommu_group_put(group);
1509 return 0;
1510 }
1511
iommu_bus_init(struct bus_type * bus,const struct iommu_ops * ops)1512 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1513 {
1514 int err;
1515 struct notifier_block *nb;
1516
1517 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1518 if (!nb)
1519 return -ENOMEM;
1520
1521 nb->notifier_call = iommu_bus_notifier;
1522
1523 err = bus_register_notifier(bus, nb);
1524 if (err)
1525 goto out_free;
1526
1527 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
1528 if (err)
1529 goto out_err;
1530
1531
1532 return 0;
1533
1534 out_err:
1535 /* Clean up */
1536 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1537 bus_unregister_notifier(bus, nb);
1538
1539 out_free:
1540 kfree(nb);
1541
1542 return err;
1543 }
1544
1545 /**
1546 * bus_set_iommu - set iommu-callbacks for the bus
1547 * @bus: bus.
1548 * @ops: the callbacks provided by the iommu-driver
1549 *
1550 * This function is called by an iommu driver to set the iommu methods
1551 * used for a particular bus. Drivers for devices on that bus can use
1552 * the iommu-api after these ops are registered.
1553 * This special function is needed because IOMMUs are usually devices on
1554 * the bus itself, so the iommu drivers are not initialized when the bus
1555 * is set up. With this function the iommu-driver can set the iommu-ops
1556 * afterwards.
1557 */
bus_set_iommu(struct bus_type * bus,const struct iommu_ops * ops)1558 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1559 {
1560 int err;
1561
1562 if (ops == NULL) {
1563 bus->iommu_ops = NULL;
1564 return 0;
1565 }
1566
1567 if (bus->iommu_ops != NULL)
1568 return -EBUSY;
1569
1570 bus->iommu_ops = ops;
1571
1572 /* Do IOMMU specific setup for this bus-type */
1573 err = iommu_bus_init(bus, ops);
1574 if (err)
1575 bus->iommu_ops = NULL;
1576
1577 return err;
1578 }
1579 EXPORT_SYMBOL_GPL(bus_set_iommu);
1580
iommu_present(struct bus_type * bus)1581 bool iommu_present(struct bus_type *bus)
1582 {
1583 return bus->iommu_ops != NULL;
1584 }
1585 EXPORT_SYMBOL_GPL(iommu_present);
1586
iommu_capable(struct bus_type * bus,enum iommu_cap cap)1587 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1588 {
1589 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1590 return false;
1591
1592 return bus->iommu_ops->capable(cap);
1593 }
1594 EXPORT_SYMBOL_GPL(iommu_capable);
1595
1596 /**
1597 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1598 * @domain: iommu domain
1599 * @handler: fault handler
1600 * @token: user data, will be passed back to the fault handler
1601 *
1602 * This function should be used by IOMMU users which want to be notified
1603 * whenever an IOMMU fault happens.
1604 *
1605 * The fault handler itself should return 0 on success, and an appropriate
1606 * error code otherwise.
1607 */
iommu_set_fault_handler(struct iommu_domain * domain,iommu_fault_handler_t handler,void * token)1608 void iommu_set_fault_handler(struct iommu_domain *domain,
1609 iommu_fault_handler_t handler,
1610 void *token)
1611 {
1612 BUG_ON(!domain);
1613
1614 domain->handler = handler;
1615 domain->handler_token = token;
1616 }
1617 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1618
__iommu_domain_alloc(struct bus_type * bus,unsigned type)1619 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1620 unsigned type)
1621 {
1622 struct iommu_domain *domain;
1623
1624 if (bus == NULL || bus->iommu_ops == NULL)
1625 return NULL;
1626
1627 domain = bus->iommu_ops->domain_alloc(type);
1628 if (!domain)
1629 return NULL;
1630
1631 domain->ops = bus->iommu_ops;
1632 domain->type = type;
1633 /* Assume all sizes by default; the driver may override this later */
1634 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1635
1636 return domain;
1637 }
1638
iommu_domain_alloc(struct bus_type * bus)1639 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1640 {
1641 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1642 }
1643 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1644
iommu_domain_free(struct iommu_domain * domain)1645 void iommu_domain_free(struct iommu_domain *domain)
1646 {
1647 domain->ops->domain_free(domain);
1648 }
1649 EXPORT_SYMBOL_GPL(iommu_domain_free);
1650
__iommu_attach_device(struct iommu_domain * domain,struct device * dev)1651 static int __iommu_attach_device(struct iommu_domain *domain,
1652 struct device *dev)
1653 {
1654 int ret;
1655 if ((domain->ops->is_attach_deferred != NULL) &&
1656 domain->ops->is_attach_deferred(domain, dev))
1657 return 0;
1658
1659 if (unlikely(domain->ops->attach_dev == NULL))
1660 return -ENODEV;
1661
1662 ret = domain->ops->attach_dev(domain, dev);
1663 if (!ret)
1664 trace_attach_device_to_domain(dev);
1665 return ret;
1666 }
1667
iommu_attach_device(struct iommu_domain * domain,struct device * dev)1668 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1669 {
1670 struct iommu_group *group;
1671 int ret;
1672
1673 group = iommu_group_get(dev);
1674 if (!group)
1675 return -ENODEV;
1676
1677 /*
1678 * Lock the group to make sure the device-count doesn't
1679 * change while we are attaching
1680 */
1681 mutex_lock(&group->mutex);
1682 ret = -EINVAL;
1683 if (iommu_group_device_count(group) != 1)
1684 goto out_unlock;
1685
1686 ret = __iommu_attach_group(domain, group);
1687
1688 out_unlock:
1689 mutex_unlock(&group->mutex);
1690 iommu_group_put(group);
1691
1692 return ret;
1693 }
1694 EXPORT_SYMBOL_GPL(iommu_attach_device);
1695
__iommu_detach_device(struct iommu_domain * domain,struct device * dev)1696 static void __iommu_detach_device(struct iommu_domain *domain,
1697 struct device *dev)
1698 {
1699 if ((domain->ops->is_attach_deferred != NULL) &&
1700 domain->ops->is_attach_deferred(domain, dev))
1701 return;
1702
1703 if (unlikely(domain->ops->detach_dev == NULL))
1704 return;
1705
1706 domain->ops->detach_dev(domain, dev);
1707 trace_detach_device_from_domain(dev);
1708 }
1709
iommu_detach_device(struct iommu_domain * domain,struct device * dev)1710 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1711 {
1712 struct iommu_group *group;
1713
1714 group = iommu_group_get(dev);
1715 if (!group)
1716 return;
1717
1718 mutex_lock(&group->mutex);
1719 if (iommu_group_device_count(group) != 1) {
1720 WARN_ON(1);
1721 goto out_unlock;
1722 }
1723
1724 __iommu_detach_group(domain, group);
1725
1726 out_unlock:
1727 mutex_unlock(&group->mutex);
1728 iommu_group_put(group);
1729 }
1730 EXPORT_SYMBOL_GPL(iommu_detach_device);
1731
iommu_get_domain_for_dev(struct device * dev)1732 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1733 {
1734 struct iommu_domain *domain;
1735 struct iommu_group *group;
1736
1737 group = iommu_group_get(dev);
1738 if (!group)
1739 return NULL;
1740
1741 domain = group->domain;
1742
1743 iommu_group_put(group);
1744
1745 return domain;
1746 }
1747 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1748
1749 /*
1750 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1751 * guarantees that the group and its default domain are valid and correct.
1752 */
iommu_get_dma_domain(struct device * dev)1753 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1754 {
1755 return dev->iommu_group->default_domain;
1756 }
1757
1758 /*
1759 * IOMMU groups are really the natural working unit of the IOMMU, but
1760 * the IOMMU API works on domains and devices. Bridge that gap by
1761 * iterating over the devices in a group. Ideally we'd have a single
1762 * device which represents the requestor ID of the group, but we also
1763 * allow IOMMU drivers to create policy defined minimum sets, where
1764 * the physical hardware may be able to distiguish members, but we
1765 * wish to group them at a higher level (ex. untrusted multi-function
1766 * PCI devices). Thus we attach each device.
1767 */
iommu_group_do_attach_device(struct device * dev,void * data)1768 static int iommu_group_do_attach_device(struct device *dev, void *data)
1769 {
1770 struct iommu_domain *domain = data;
1771
1772 return __iommu_attach_device(domain, dev);
1773 }
1774
__iommu_attach_group(struct iommu_domain * domain,struct iommu_group * group)1775 static int __iommu_attach_group(struct iommu_domain *domain,
1776 struct iommu_group *group)
1777 {
1778 int ret;
1779
1780 if (group->default_domain && group->domain != group->default_domain)
1781 return -EBUSY;
1782
1783 ret = __iommu_group_for_each_dev(group, domain,
1784 iommu_group_do_attach_device);
1785 if (ret == 0)
1786 group->domain = domain;
1787
1788 return ret;
1789 }
1790
iommu_attach_group(struct iommu_domain * domain,struct iommu_group * group)1791 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1792 {
1793 int ret;
1794
1795 mutex_lock(&group->mutex);
1796 ret = __iommu_attach_group(domain, group);
1797 mutex_unlock(&group->mutex);
1798
1799 return ret;
1800 }
1801 EXPORT_SYMBOL_GPL(iommu_attach_group);
1802
iommu_group_do_detach_device(struct device * dev,void * data)1803 static int iommu_group_do_detach_device(struct device *dev, void *data)
1804 {
1805 struct iommu_domain *domain = data;
1806
1807 __iommu_detach_device(domain, dev);
1808
1809 return 0;
1810 }
1811
__iommu_detach_group(struct iommu_domain * domain,struct iommu_group * group)1812 static void __iommu_detach_group(struct iommu_domain *domain,
1813 struct iommu_group *group)
1814 {
1815 int ret;
1816
1817 if (!group->default_domain) {
1818 __iommu_group_for_each_dev(group, domain,
1819 iommu_group_do_detach_device);
1820 group->domain = NULL;
1821 return;
1822 }
1823
1824 if (group->domain == group->default_domain)
1825 return;
1826
1827 /* Detach by re-attaching to the default domain */
1828 ret = __iommu_group_for_each_dev(group, group->default_domain,
1829 iommu_group_do_attach_device);
1830 if (ret != 0)
1831 WARN_ON(1);
1832 else
1833 group->domain = group->default_domain;
1834 }
1835
iommu_detach_group(struct iommu_domain * domain,struct iommu_group * group)1836 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1837 {
1838 mutex_lock(&group->mutex);
1839 __iommu_detach_group(domain, group);
1840 mutex_unlock(&group->mutex);
1841 }
1842 EXPORT_SYMBOL_GPL(iommu_detach_group);
1843
iommu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)1844 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1845 {
1846 if (unlikely(domain->ops->iova_to_phys == NULL))
1847 return 0;
1848
1849 return domain->ops->iova_to_phys(domain, iova);
1850 }
1851 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1852
iommu_pgsize(struct iommu_domain * domain,unsigned long addr_merge,size_t size)1853 static size_t iommu_pgsize(struct iommu_domain *domain,
1854 unsigned long addr_merge, size_t size)
1855 {
1856 unsigned int pgsize_idx;
1857 size_t pgsize;
1858
1859 /* Max page size that still fits into 'size' */
1860 pgsize_idx = __fls(size);
1861
1862 /* need to consider alignment requirements ? */
1863 if (likely(addr_merge)) {
1864 /* Max page size allowed by address */
1865 unsigned int align_pgsize_idx = __ffs(addr_merge);
1866 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1867 }
1868
1869 /* build a mask of acceptable page sizes */
1870 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1871
1872 /* throw away page sizes not supported by the hardware */
1873 pgsize &= domain->pgsize_bitmap;
1874
1875 /* make sure we're still sane */
1876 BUG_ON(!pgsize);
1877
1878 /* pick the biggest page */
1879 pgsize_idx = __fls(pgsize);
1880 pgsize = 1UL << pgsize_idx;
1881
1882 return pgsize;
1883 }
1884
iommu_map(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,int prot)1885 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1886 phys_addr_t paddr, size_t size, int prot)
1887 {
1888 const struct iommu_ops *ops = domain->ops;
1889 unsigned long orig_iova = iova;
1890 unsigned int min_pagesz;
1891 size_t orig_size = size;
1892 phys_addr_t orig_paddr = paddr;
1893 int ret = 0;
1894
1895 if (unlikely(ops->map == NULL ||
1896 domain->pgsize_bitmap == 0UL))
1897 return -ENODEV;
1898
1899 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1900 return -EINVAL;
1901
1902 /* find out the minimum page size supported */
1903 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1904
1905 /*
1906 * both the virtual address and the physical one, as well as
1907 * the size of the mapping, must be aligned (at least) to the
1908 * size of the smallest page supported by the hardware
1909 */
1910 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1911 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1912 iova, &paddr, size, min_pagesz);
1913 return -EINVAL;
1914 }
1915
1916 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1917
1918 while (size) {
1919 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1920
1921 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1922 iova, &paddr, pgsize);
1923
1924 ret = ops->map(domain, iova, paddr, pgsize, prot);
1925 if (ret)
1926 break;
1927
1928 iova += pgsize;
1929 paddr += pgsize;
1930 size -= pgsize;
1931 }
1932
1933 if (ops->iotlb_sync_map)
1934 ops->iotlb_sync_map(domain);
1935
1936 /* unroll mapping in case something went wrong */
1937 if (ret)
1938 iommu_unmap(domain, orig_iova, orig_size - size);
1939 else
1940 trace_map(orig_iova, orig_paddr, orig_size);
1941
1942 return ret;
1943 }
1944 EXPORT_SYMBOL_GPL(iommu_map);
1945
__iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t size,struct iommu_iotlb_gather * iotlb_gather)1946 static size_t __iommu_unmap(struct iommu_domain *domain,
1947 unsigned long iova, size_t size,
1948 struct iommu_iotlb_gather *iotlb_gather)
1949 {
1950 const struct iommu_ops *ops = domain->ops;
1951 size_t unmapped_page, unmapped = 0;
1952 unsigned long orig_iova = iova;
1953 unsigned int min_pagesz;
1954
1955 if (unlikely(ops->unmap == NULL ||
1956 domain->pgsize_bitmap == 0UL))
1957 return 0;
1958
1959 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1960 return 0;
1961
1962 /* find out the minimum page size supported */
1963 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1964
1965 /*
1966 * The virtual address, as well as the size of the mapping, must be
1967 * aligned (at least) to the size of the smallest page supported
1968 * by the hardware
1969 */
1970 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1971 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1972 iova, size, min_pagesz);
1973 return 0;
1974 }
1975
1976 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1977
1978 /*
1979 * Keep iterating until we either unmap 'size' bytes (or more)
1980 * or we hit an area that isn't mapped.
1981 */
1982 while (unmapped < size) {
1983 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1984
1985 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
1986 if (!unmapped_page)
1987 break;
1988
1989 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1990 iova, unmapped_page);
1991
1992 iova += unmapped_page;
1993 unmapped += unmapped_page;
1994 }
1995
1996 trace_unmap(orig_iova, size, unmapped);
1997 return unmapped;
1998 }
1999
iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t size)2000 size_t iommu_unmap(struct iommu_domain *domain,
2001 unsigned long iova, size_t size)
2002 {
2003 struct iommu_iotlb_gather iotlb_gather;
2004 size_t ret;
2005
2006 iommu_iotlb_gather_init(&iotlb_gather);
2007 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2008 iommu_tlb_sync(domain, &iotlb_gather);
2009
2010 return ret;
2011 }
2012 EXPORT_SYMBOL_GPL(iommu_unmap);
2013
iommu_unmap_fast(struct iommu_domain * domain,unsigned long iova,size_t size,struct iommu_iotlb_gather * iotlb_gather)2014 size_t iommu_unmap_fast(struct iommu_domain *domain,
2015 unsigned long iova, size_t size,
2016 struct iommu_iotlb_gather *iotlb_gather)
2017 {
2018 return __iommu_unmap(domain, iova, size, iotlb_gather);
2019 }
2020 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2021
iommu_map_sg(struct iommu_domain * domain,unsigned long iova,struct scatterlist * sg,unsigned int nents,int prot)2022 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2023 struct scatterlist *sg, unsigned int nents, int prot)
2024 {
2025 size_t len = 0, mapped = 0;
2026 phys_addr_t start;
2027 unsigned int i = 0;
2028 int ret;
2029
2030 while (i <= nents) {
2031 phys_addr_t s_phys = sg_phys(sg);
2032
2033 if (len && s_phys != start + len) {
2034 ret = iommu_map(domain, iova + mapped, start, len, prot);
2035 if (ret)
2036 goto out_err;
2037
2038 mapped += len;
2039 len = 0;
2040 }
2041
2042 if (len) {
2043 len += sg->length;
2044 } else {
2045 len = sg->length;
2046 start = s_phys;
2047 }
2048
2049 if (++i < nents)
2050 sg = sg_next(sg);
2051 }
2052
2053 return mapped;
2054
2055 out_err:
2056 /* undo mappings already done */
2057 iommu_unmap(domain, iova, mapped);
2058
2059 return 0;
2060
2061 }
2062 EXPORT_SYMBOL_GPL(iommu_map_sg);
2063
iommu_domain_window_enable(struct iommu_domain * domain,u32 wnd_nr,phys_addr_t paddr,u64 size,int prot)2064 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
2065 phys_addr_t paddr, u64 size, int prot)
2066 {
2067 if (unlikely(domain->ops->domain_window_enable == NULL))
2068 return -ENODEV;
2069
2070 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2071 prot);
2072 }
2073 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2074
iommu_domain_window_disable(struct iommu_domain * domain,u32 wnd_nr)2075 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2076 {
2077 if (unlikely(domain->ops->domain_window_disable == NULL))
2078 return;
2079
2080 return domain->ops->domain_window_disable(domain, wnd_nr);
2081 }
2082 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2083
2084 /**
2085 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2086 * @domain: the iommu domain where the fault has happened
2087 * @dev: the device where the fault has happened
2088 * @iova: the faulting address
2089 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2090 *
2091 * This function should be called by the low-level IOMMU implementations
2092 * whenever IOMMU faults happen, to allow high-level users, that are
2093 * interested in such events, to know about them.
2094 *
2095 * This event may be useful for several possible use cases:
2096 * - mere logging of the event
2097 * - dynamic TLB/PTE loading
2098 * - if restarting of the faulting device is required
2099 *
2100 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2101 * PTE/TLB loading will one day be supported, implementations will be able
2102 * to tell whether it succeeded or not according to this return value).
2103 *
2104 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2105 * (though fault handlers can also return -ENOSYS, in case they want to
2106 * elicit the default behavior of the IOMMU drivers).
2107 */
report_iommu_fault(struct iommu_domain * domain,struct device * dev,unsigned long iova,int flags)2108 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2109 unsigned long iova, int flags)
2110 {
2111 int ret = -ENOSYS;
2112
2113 /*
2114 * if upper layers showed interest and installed a fault handler,
2115 * invoke it.
2116 */
2117 if (domain->handler)
2118 ret = domain->handler(domain, dev, iova, flags,
2119 domain->handler_token);
2120
2121 trace_io_page_fault(dev, iova, flags);
2122 return ret;
2123 }
2124 EXPORT_SYMBOL_GPL(report_iommu_fault);
2125
iommu_init(void)2126 static int __init iommu_init(void)
2127 {
2128 iommu_group_kset = kset_create_and_add("iommu_groups",
2129 NULL, kernel_kobj);
2130 BUG_ON(!iommu_group_kset);
2131
2132 iommu_debugfs_setup();
2133
2134 return 0;
2135 }
2136 core_initcall(iommu_init);
2137
iommu_domain_get_attr(struct iommu_domain * domain,enum iommu_attr attr,void * data)2138 int iommu_domain_get_attr(struct iommu_domain *domain,
2139 enum iommu_attr attr, void *data)
2140 {
2141 struct iommu_domain_geometry *geometry;
2142 bool *paging;
2143 int ret = 0;
2144
2145 switch (attr) {
2146 case DOMAIN_ATTR_GEOMETRY:
2147 geometry = data;
2148 *geometry = domain->geometry;
2149
2150 break;
2151 case DOMAIN_ATTR_PAGING:
2152 paging = data;
2153 *paging = (domain->pgsize_bitmap != 0UL);
2154 break;
2155 default:
2156 if (!domain->ops->domain_get_attr)
2157 return -EINVAL;
2158
2159 ret = domain->ops->domain_get_attr(domain, attr, data);
2160 }
2161
2162 return ret;
2163 }
2164 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2165
iommu_domain_set_attr(struct iommu_domain * domain,enum iommu_attr attr,void * data)2166 int iommu_domain_set_attr(struct iommu_domain *domain,
2167 enum iommu_attr attr, void *data)
2168 {
2169 int ret = 0;
2170
2171 switch (attr) {
2172 default:
2173 if (domain->ops->domain_set_attr == NULL)
2174 return -EINVAL;
2175
2176 ret = domain->ops->domain_set_attr(domain, attr, data);
2177 }
2178
2179 return ret;
2180 }
2181 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
2182
iommu_get_resv_regions(struct device * dev,struct list_head * list)2183 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2184 {
2185 const struct iommu_ops *ops = dev->bus->iommu_ops;
2186
2187 if (ops && ops->get_resv_regions)
2188 ops->get_resv_regions(dev, list);
2189 }
2190
iommu_put_resv_regions(struct device * dev,struct list_head * list)2191 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2192 {
2193 const struct iommu_ops *ops = dev->bus->iommu_ops;
2194
2195 if (ops && ops->put_resv_regions)
2196 ops->put_resv_regions(dev, list);
2197 }
2198
iommu_alloc_resv_region(phys_addr_t start,size_t length,int prot,enum iommu_resv_type type)2199 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2200 size_t length, int prot,
2201 enum iommu_resv_type type)
2202 {
2203 struct iommu_resv_region *region;
2204
2205 region = kzalloc(sizeof(*region), GFP_KERNEL);
2206 if (!region)
2207 return NULL;
2208
2209 INIT_LIST_HEAD(®ion->list);
2210 region->start = start;
2211 region->length = length;
2212 region->prot = prot;
2213 region->type = type;
2214 return region;
2215 }
2216 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2217
2218 static int
request_default_domain_for_dev(struct device * dev,unsigned long type)2219 request_default_domain_for_dev(struct device *dev, unsigned long type)
2220 {
2221 struct iommu_domain *domain;
2222 struct iommu_group *group;
2223 int ret;
2224
2225 /* Device must already be in a group before calling this function */
2226 group = iommu_group_get(dev);
2227 if (!group)
2228 return -EINVAL;
2229
2230 mutex_lock(&group->mutex);
2231
2232 ret = 0;
2233 if (group->default_domain && group->default_domain->type == type)
2234 goto out;
2235
2236 /* Don't change mappings of existing devices */
2237 ret = -EBUSY;
2238 if (iommu_group_device_count(group) != 1)
2239 goto out;
2240
2241 ret = -ENOMEM;
2242 domain = __iommu_domain_alloc(dev->bus, type);
2243 if (!domain)
2244 goto out;
2245
2246 /* Attach the device to the domain */
2247 ret = __iommu_attach_group(domain, group);
2248 if (ret) {
2249 iommu_domain_free(domain);
2250 goto out;
2251 }
2252
2253 /* Make the domain the default for this group */
2254 if (group->default_domain)
2255 iommu_domain_free(group->default_domain);
2256 group->default_domain = domain;
2257
2258 iommu_group_create_direct_mappings(group, dev);
2259
2260 dev_info(dev, "Using iommu %s mapping\n",
2261 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
2262
2263 ret = 0;
2264 out:
2265 mutex_unlock(&group->mutex);
2266 iommu_group_put(group);
2267
2268 return ret;
2269 }
2270
2271 /* Request that a device is direct mapped by the IOMMU */
iommu_request_dm_for_dev(struct device * dev)2272 int iommu_request_dm_for_dev(struct device *dev)
2273 {
2274 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2275 }
2276
2277 /* Request that a device can't be direct mapped by the IOMMU */
iommu_request_dma_domain_for_dev(struct device * dev)2278 int iommu_request_dma_domain_for_dev(struct device *dev)
2279 {
2280 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2281 }
2282
iommu_set_default_passthrough(bool cmd_line)2283 void iommu_set_default_passthrough(bool cmd_line)
2284 {
2285 if (cmd_line)
2286 iommu_set_cmd_line_dma_api();
2287
2288 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2289 }
2290
iommu_set_default_translated(bool cmd_line)2291 void iommu_set_default_translated(bool cmd_line)
2292 {
2293 if (cmd_line)
2294 iommu_set_cmd_line_dma_api();
2295
2296 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2297 }
2298
iommu_default_passthrough(void)2299 bool iommu_default_passthrough(void)
2300 {
2301 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2302 }
2303 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2304
iommu_ops_from_fwnode(struct fwnode_handle * fwnode)2305 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2306 {
2307 const struct iommu_ops *ops = NULL;
2308 struct iommu_device *iommu;
2309
2310 spin_lock(&iommu_device_lock);
2311 list_for_each_entry(iommu, &iommu_device_list, list)
2312 if (iommu->fwnode == fwnode) {
2313 ops = iommu->ops;
2314 break;
2315 }
2316 spin_unlock(&iommu_device_lock);
2317 return ops;
2318 }
2319
iommu_fwspec_init(struct device * dev,struct fwnode_handle * iommu_fwnode,const struct iommu_ops * ops)2320 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2321 const struct iommu_ops *ops)
2322 {
2323 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2324
2325 if (fwspec)
2326 return ops == fwspec->ops ? 0 : -EINVAL;
2327
2328 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2329 if (!fwspec)
2330 return -ENOMEM;
2331
2332 of_node_get(to_of_node(iommu_fwnode));
2333 fwspec->iommu_fwnode = iommu_fwnode;
2334 fwspec->ops = ops;
2335 dev_iommu_fwspec_set(dev, fwspec);
2336 return 0;
2337 }
2338 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2339
iommu_fwspec_free(struct device * dev)2340 void iommu_fwspec_free(struct device *dev)
2341 {
2342 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2343
2344 if (fwspec) {
2345 fwnode_handle_put(fwspec->iommu_fwnode);
2346 kfree(fwspec);
2347 dev_iommu_fwspec_set(dev, NULL);
2348 }
2349 }
2350 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2351
iommu_fwspec_add_ids(struct device * dev,u32 * ids,int num_ids)2352 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2353 {
2354 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2355 size_t size;
2356 int i;
2357
2358 if (!fwspec)
2359 return -EINVAL;
2360
2361 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2362 if (size > sizeof(*fwspec)) {
2363 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2364 if (!fwspec)
2365 return -ENOMEM;
2366
2367 dev_iommu_fwspec_set(dev, fwspec);
2368 }
2369
2370 for (i = 0; i < num_ids; i++)
2371 fwspec->ids[fwspec->num_ids + i] = ids[i];
2372
2373 fwspec->num_ids += num_ids;
2374 return 0;
2375 }
2376 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2377
2378 /*
2379 * Per device IOMMU features.
2380 */
iommu_dev_has_feature(struct device * dev,enum iommu_dev_features feat)2381 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2382 {
2383 const struct iommu_ops *ops = dev->bus->iommu_ops;
2384
2385 if (ops && ops->dev_has_feat)
2386 return ops->dev_has_feat(dev, feat);
2387
2388 return false;
2389 }
2390 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2391
iommu_dev_enable_feature(struct device * dev,enum iommu_dev_features feat)2392 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2393 {
2394 const struct iommu_ops *ops = dev->bus->iommu_ops;
2395
2396 if (ops && ops->dev_enable_feat)
2397 return ops->dev_enable_feat(dev, feat);
2398
2399 return -ENODEV;
2400 }
2401 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2402
2403 /*
2404 * The device drivers should do the necessary cleanups before calling this.
2405 * For example, before disabling the aux-domain feature, the device driver
2406 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2407 */
iommu_dev_disable_feature(struct device * dev,enum iommu_dev_features feat)2408 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2409 {
2410 const struct iommu_ops *ops = dev->bus->iommu_ops;
2411
2412 if (ops && ops->dev_disable_feat)
2413 return ops->dev_disable_feat(dev, feat);
2414
2415 return -EBUSY;
2416 }
2417 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2418
iommu_dev_feature_enabled(struct device * dev,enum iommu_dev_features feat)2419 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2420 {
2421 const struct iommu_ops *ops = dev->bus->iommu_ops;
2422
2423 if (ops && ops->dev_feat_enabled)
2424 return ops->dev_feat_enabled(dev, feat);
2425
2426 return false;
2427 }
2428 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2429
2430 /*
2431 * Aux-domain specific attach/detach.
2432 *
2433 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2434 * true. Also, as long as domains are attached to a device through this
2435 * interface, any tries to call iommu_attach_device() should fail
2436 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2437 * This should make us safe against a device being attached to a guest as a
2438 * whole while there are still pasid users on it (aux and sva).
2439 */
iommu_aux_attach_device(struct iommu_domain * domain,struct device * dev)2440 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2441 {
2442 int ret = -ENODEV;
2443
2444 if (domain->ops->aux_attach_dev)
2445 ret = domain->ops->aux_attach_dev(domain, dev);
2446
2447 if (!ret)
2448 trace_attach_device_to_domain(dev);
2449
2450 return ret;
2451 }
2452 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2453
iommu_aux_detach_device(struct iommu_domain * domain,struct device * dev)2454 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2455 {
2456 if (domain->ops->aux_detach_dev) {
2457 domain->ops->aux_detach_dev(domain, dev);
2458 trace_detach_device_from_domain(dev);
2459 }
2460 }
2461 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2462
iommu_aux_get_pasid(struct iommu_domain * domain,struct device * dev)2463 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2464 {
2465 int ret = -ENODEV;
2466
2467 if (domain->ops->aux_get_pasid)
2468 ret = domain->ops->aux_get_pasid(domain, dev);
2469
2470 return ret;
2471 }
2472 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2473
2474 /**
2475 * iommu_sva_bind_device() - Bind a process address space to a device
2476 * @dev: the device
2477 * @mm: the mm to bind, caller must hold a reference to it
2478 *
2479 * Create a bond between device and address space, allowing the device to access
2480 * the mm using the returned PASID. If a bond already exists between @device and
2481 * @mm, it is returned and an additional reference is taken. Caller must call
2482 * iommu_sva_unbind_device() to release each reference.
2483 *
2484 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2485 * initialize the required SVA features.
2486 *
2487 * On error, returns an ERR_PTR value.
2488 */
2489 struct iommu_sva *
iommu_sva_bind_device(struct device * dev,struct mm_struct * mm,void * drvdata)2490 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2491 {
2492 struct iommu_group *group;
2493 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2494 const struct iommu_ops *ops = dev->bus->iommu_ops;
2495
2496 if (!ops || !ops->sva_bind)
2497 return ERR_PTR(-ENODEV);
2498
2499 group = iommu_group_get(dev);
2500 if (!group)
2501 return ERR_PTR(-ENODEV);
2502
2503 /* Ensure device count and domain don't change while we're binding */
2504 mutex_lock(&group->mutex);
2505
2506 /*
2507 * To keep things simple, SVA currently doesn't support IOMMU groups
2508 * with more than one device. Existing SVA-capable systems are not
2509 * affected by the problems that required IOMMU groups (lack of ACS
2510 * isolation, device ID aliasing and other hardware issues).
2511 */
2512 if (iommu_group_device_count(group) != 1)
2513 goto out_unlock;
2514
2515 handle = ops->sva_bind(dev, mm, drvdata);
2516
2517 out_unlock:
2518 mutex_unlock(&group->mutex);
2519 iommu_group_put(group);
2520
2521 return handle;
2522 }
2523 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2524
2525 /**
2526 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2527 * @handle: the handle returned by iommu_sva_bind_device()
2528 *
2529 * Put reference to a bond between device and address space. The device should
2530 * not be issuing any more transaction for this PASID. All outstanding page
2531 * requests for this PASID must have been flushed to the IOMMU.
2532 *
2533 * Returns 0 on success, or an error value
2534 */
iommu_sva_unbind_device(struct iommu_sva * handle)2535 void iommu_sva_unbind_device(struct iommu_sva *handle)
2536 {
2537 struct iommu_group *group;
2538 struct device *dev = handle->dev;
2539 const struct iommu_ops *ops = dev->bus->iommu_ops;
2540
2541 if (!ops || !ops->sva_unbind)
2542 return;
2543
2544 group = iommu_group_get(dev);
2545 if (!group)
2546 return;
2547
2548 mutex_lock(&group->mutex);
2549 ops->sva_unbind(handle);
2550 mutex_unlock(&group->mutex);
2551
2552 iommu_group_put(group);
2553 }
2554 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2555
iommu_sva_set_ops(struct iommu_sva * handle,const struct iommu_sva_ops * sva_ops)2556 int iommu_sva_set_ops(struct iommu_sva *handle,
2557 const struct iommu_sva_ops *sva_ops)
2558 {
2559 if (handle->ops && handle->ops != sva_ops)
2560 return -EEXIST;
2561
2562 handle->ops = sva_ops;
2563 return 0;
2564 }
2565 EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2566
iommu_sva_get_pasid(struct iommu_sva * handle)2567 int iommu_sva_get_pasid(struct iommu_sva *handle)
2568 {
2569 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2570
2571 if (!ops || !ops->sva_get_pasid)
2572 return IOMMU_PASID_INVALID;
2573
2574 return ops->sva_get_pasid(handle);
2575 }
2576 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
2577