1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
7 #define pr_fmt(fmt) "iommu: " fmt
9 #include <linux/device.h>
10 #include <linux/dma-iommu.h>
11 #include <linux/kernel.h>
12 #include <linux/bits.h>
13 #include <linux/bug.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/iommu.h>
20 #include <linux/idr.h>
21 #include <linux/err.h>
22 #include <linux/pci.h>
23 #include <linux/bitops.h>
24 #include <linux/property.h>
25 #include <linux/fsl/mc.h>
26 #include <linux/module.h>
27 #include <linux/cc_platform.h>
28 #include <trace/events/iommu.h>
30 static struct kset *iommu_group_kset;
31 static DEFINE_IDA(iommu_group_ida);
33 static unsigned int iommu_def_domain_type __read_mostly;
34 static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
35 static u32 iommu_cmd_line __read_mostly;
39 struct kobject *devices_kobj;
40 struct list_head devices;
43 void (*iommu_data_release)(void *iommu_data);
46 struct iommu_domain *default_domain;
47 struct iommu_domain *blocking_domain;
48 struct iommu_domain *domain;
49 struct list_head entry;
50 unsigned int owner_cnt;
55 struct list_head list;
60 struct iommu_group_attribute {
61 struct attribute attr;
62 ssize_t (*show)(struct iommu_group *group, char *buf);
63 ssize_t (*store)(struct iommu_group *group,
64 const char *buf, size_t count);
67 static const char * const iommu_group_resv_type_string[] = {
68 [IOMMU_RESV_DIRECT] = "direct",
69 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
70 [IOMMU_RESV_RESERVED] = "reserved",
71 [IOMMU_RESV_MSI] = "msi",
72 [IOMMU_RESV_SW_MSI] = "msi",
75 #define IOMMU_CMD_LINE_DMA_API BIT(0)
76 #define IOMMU_CMD_LINE_STRICT BIT(1)
78 static int iommu_alloc_default_domain(struct iommu_group *group,
80 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
82 static int __iommu_attach_device(struct iommu_domain *domain,
84 static int __iommu_attach_group(struct iommu_domain *domain,
85 struct iommu_group *group);
86 static int __iommu_group_set_domain(struct iommu_group *group,
87 struct iommu_domain *new_domain);
88 static int iommu_create_device_direct_mappings(struct iommu_group *group,
90 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
91 static ssize_t iommu_group_store_type(struct iommu_group *group,
92 const char *buf, size_t count);
94 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
95 struct iommu_group_attribute iommu_group_attr_##_name = \
96 __ATTR(_name, _mode, _show, _store)
98 #define to_iommu_group_attr(_attr) \
99 container_of(_attr, struct iommu_group_attribute, attr)
100 #define to_iommu_group(_kobj) \
101 container_of(_kobj, struct iommu_group, kobj)
103 static LIST_HEAD(iommu_device_list);
104 static DEFINE_SPINLOCK(iommu_device_lock);
107 * Use a function instead of an array here because the domain-type is a
108 * bit-field, so an array would waste memory.
110 static const char *iommu_domain_type_str(unsigned int t)
113 case IOMMU_DOMAIN_BLOCKED:
115 case IOMMU_DOMAIN_IDENTITY:
116 return "Passthrough";
117 case IOMMU_DOMAIN_UNMANAGED:
119 case IOMMU_DOMAIN_DMA:
120 case IOMMU_DOMAIN_DMA_FQ:
127 static int __init iommu_subsys_init(void)
129 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
130 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
131 iommu_set_default_passthrough(false);
133 iommu_set_default_translated(false);
135 if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
136 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
137 iommu_set_default_translated(false);
141 if (!iommu_default_passthrough() && !iommu_dma_strict)
142 iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
144 pr_info("Default domain type: %s %s\n",
145 iommu_domain_type_str(iommu_def_domain_type),
146 (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
147 "(set via kernel command line)" : "");
149 if (!iommu_default_passthrough())
150 pr_info("DMA domain TLB invalidation policy: %s mode %s\n",
151 iommu_dma_strict ? "strict" : "lazy",
152 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
153 "(set via kernel command line)" : "");
157 subsys_initcall(iommu_subsys_init);
160 * iommu_device_register() - Register an IOMMU hardware instance
161 * @iommu: IOMMU handle for the instance
162 * @ops: IOMMU ops to associate with the instance
163 * @hwdev: (optional) actual instance device, used for fwnode lookup
165 * Return: 0 on success, or an error.
167 int iommu_device_register(struct iommu_device *iommu,
168 const struct iommu_ops *ops, struct device *hwdev)
170 /* We need to be able to take module references appropriately */
171 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
176 iommu->fwnode = hwdev->fwnode;
178 spin_lock(&iommu_device_lock);
179 list_add_tail(&iommu->list, &iommu_device_list);
180 spin_unlock(&iommu_device_lock);
183 EXPORT_SYMBOL_GPL(iommu_device_register);
185 void iommu_device_unregister(struct iommu_device *iommu)
187 spin_lock(&iommu_device_lock);
188 list_del(&iommu->list);
189 spin_unlock(&iommu_device_lock);
191 EXPORT_SYMBOL_GPL(iommu_device_unregister);
193 static struct dev_iommu *dev_iommu_get(struct device *dev)
195 struct dev_iommu *param = dev->iommu;
200 param = kzalloc(sizeof(*param), GFP_KERNEL);
204 mutex_init(¶m->lock);
209 static void dev_iommu_free(struct device *dev)
211 struct dev_iommu *param = dev->iommu;
215 fwnode_handle_put(param->fwspec->iommu_fwnode);
216 kfree(param->fwspec);
221 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
223 const struct iommu_ops *ops = dev->bus->iommu_ops;
224 struct iommu_device *iommu_dev;
225 struct iommu_group *group;
231 if (!dev_iommu_get(dev))
234 if (!try_module_get(ops->owner)) {
239 iommu_dev = ops->probe_device(dev);
240 if (IS_ERR(iommu_dev)) {
241 ret = PTR_ERR(iommu_dev);
245 dev->iommu->iommu_dev = iommu_dev;
247 group = iommu_group_get_for_dev(dev);
249 ret = PTR_ERR(group);
252 iommu_group_put(group);
254 if (group_list && !group->default_domain && list_empty(&group->entry))
255 list_add_tail(&group->entry, group_list);
257 iommu_device_link(iommu_dev, dev);
262 ops->release_device(dev);
265 module_put(ops->owner);
273 int iommu_probe_device(struct device *dev)
275 const struct iommu_ops *ops = dev->bus->iommu_ops;
276 struct iommu_group *group;
279 ret = __iommu_probe_device(dev, NULL);
283 group = iommu_group_get(dev);
290 * Try to allocate a default domain - needs support from the
291 * IOMMU driver. There are still some drivers which don't
292 * support default domains, so the return value is not yet
295 mutex_lock(&group->mutex);
296 iommu_alloc_default_domain(group, dev);
299 * If device joined an existing group which has been claimed, don't
300 * attach the default domain.
302 if (group->default_domain && !group->owner) {
303 ret = __iommu_attach_device(group->default_domain, dev);
305 mutex_unlock(&group->mutex);
306 iommu_group_put(group);
311 iommu_create_device_direct_mappings(group, dev);
313 mutex_unlock(&group->mutex);
314 iommu_group_put(group);
316 if (ops->probe_finalize)
317 ops->probe_finalize(dev);
322 iommu_release_device(dev);
329 void iommu_release_device(struct device *dev)
331 const struct iommu_ops *ops;
336 iommu_device_unlink(dev->iommu->iommu_dev, dev);
338 ops = dev_iommu_ops(dev);
339 ops->release_device(dev);
341 iommu_group_remove_device(dev);
342 module_put(ops->owner);
346 static int __init iommu_set_def_domain_type(char *str)
351 ret = kstrtobool(str, &pt);
356 iommu_set_default_passthrough(true);
358 iommu_set_default_translated(true);
362 early_param("iommu.passthrough", iommu_set_def_domain_type);
364 static int __init iommu_dma_setup(char *str)
366 int ret = kstrtobool(str, &iommu_dma_strict);
369 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
372 early_param("iommu.strict", iommu_dma_setup);
374 void iommu_set_dma_strict(void)
376 iommu_dma_strict = true;
377 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
378 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
381 static ssize_t iommu_group_attr_show(struct kobject *kobj,
382 struct attribute *__attr, char *buf)
384 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
385 struct iommu_group *group = to_iommu_group(kobj);
389 ret = attr->show(group, buf);
393 static ssize_t iommu_group_attr_store(struct kobject *kobj,
394 struct attribute *__attr,
395 const char *buf, size_t count)
397 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
398 struct iommu_group *group = to_iommu_group(kobj);
402 ret = attr->store(group, buf, count);
406 static const struct sysfs_ops iommu_group_sysfs_ops = {
407 .show = iommu_group_attr_show,
408 .store = iommu_group_attr_store,
411 static int iommu_group_create_file(struct iommu_group *group,
412 struct iommu_group_attribute *attr)
414 return sysfs_create_file(&group->kobj, &attr->attr);
417 static void iommu_group_remove_file(struct iommu_group *group,
418 struct iommu_group_attribute *attr)
420 sysfs_remove_file(&group->kobj, &attr->attr);
423 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
425 return sprintf(buf, "%s\n", group->name);
429 * iommu_insert_resv_region - Insert a new region in the
430 * list of reserved regions.
431 * @new: new region to insert
432 * @regions: list of regions
434 * Elements are sorted by start address and overlapping segments
435 * of the same type are merged.
437 static int iommu_insert_resv_region(struct iommu_resv_region *new,
438 struct list_head *regions)
440 struct iommu_resv_region *iter, *tmp, *nr, *top;
443 nr = iommu_alloc_resv_region(new->start, new->length,
444 new->prot, new->type);
448 /* First add the new element based on start address sorting */
449 list_for_each_entry(iter, regions, list) {
450 if (nr->start < iter->start ||
451 (nr->start == iter->start && nr->type <= iter->type))
454 list_add_tail(&nr->list, &iter->list);
456 /* Merge overlapping segments of type nr->type in @regions, if any */
457 list_for_each_entry_safe(iter, tmp, regions, list) {
458 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
460 /* no merge needed on elements of different types than @new */
461 if (iter->type != new->type) {
462 list_move_tail(&iter->list, &stack);
466 /* look for the last stack element of same type as @iter */
467 list_for_each_entry_reverse(top, &stack, list)
468 if (top->type == iter->type)
471 list_move_tail(&iter->list, &stack);
475 top_end = top->start + top->length - 1;
477 if (iter->start > top_end + 1) {
478 list_move_tail(&iter->list, &stack);
480 top->length = max(top_end, iter_end) - top->start + 1;
481 list_del(&iter->list);
485 list_splice(&stack, regions);
490 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
491 struct list_head *group_resv_regions)
493 struct iommu_resv_region *entry;
496 list_for_each_entry(entry, dev_resv_regions, list) {
497 ret = iommu_insert_resv_region(entry, group_resv_regions);
504 int iommu_get_group_resv_regions(struct iommu_group *group,
505 struct list_head *head)
507 struct group_device *device;
510 mutex_lock(&group->mutex);
511 list_for_each_entry(device, &group->devices, list) {
512 struct list_head dev_resv_regions;
515 * Non-API groups still expose reserved_regions in sysfs,
516 * so filter out calls that get here that way.
518 if (!device->dev->iommu)
521 INIT_LIST_HEAD(&dev_resv_regions);
522 iommu_get_resv_regions(device->dev, &dev_resv_regions);
523 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
524 iommu_put_resv_regions(device->dev, &dev_resv_regions);
528 mutex_unlock(&group->mutex);
531 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
533 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
536 struct iommu_resv_region *region, *next;
537 struct list_head group_resv_regions;
540 INIT_LIST_HEAD(&group_resv_regions);
541 iommu_get_group_resv_regions(group, &group_resv_regions);
543 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
544 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
545 (long long int)region->start,
546 (long long int)(region->start +
548 iommu_group_resv_type_string[region->type]);
555 static ssize_t iommu_group_show_type(struct iommu_group *group,
558 char *type = "unknown\n";
560 mutex_lock(&group->mutex);
561 if (group->default_domain) {
562 switch (group->default_domain->type) {
563 case IOMMU_DOMAIN_BLOCKED:
566 case IOMMU_DOMAIN_IDENTITY:
569 case IOMMU_DOMAIN_UNMANAGED:
570 type = "unmanaged\n";
572 case IOMMU_DOMAIN_DMA:
575 case IOMMU_DOMAIN_DMA_FQ:
580 mutex_unlock(&group->mutex);
586 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
588 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
589 iommu_group_show_resv_regions, NULL);
591 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
592 iommu_group_store_type);
594 static void iommu_group_release(struct kobject *kobj)
596 struct iommu_group *group = to_iommu_group(kobj);
598 pr_debug("Releasing group %d\n", group->id);
600 if (group->iommu_data_release)
601 group->iommu_data_release(group->iommu_data);
603 ida_simple_remove(&iommu_group_ida, group->id);
605 if (group->default_domain)
606 iommu_domain_free(group->default_domain);
607 if (group->blocking_domain)
608 iommu_domain_free(group->blocking_domain);
614 static struct kobj_type iommu_group_ktype = {
615 .sysfs_ops = &iommu_group_sysfs_ops,
616 .release = iommu_group_release,
620 * iommu_group_alloc - Allocate a new group
622 * This function is called by an iommu driver to allocate a new iommu
623 * group. The iommu group represents the minimum granularity of the iommu.
624 * Upon successful return, the caller holds a reference to the supplied
625 * group in order to hold the group until devices are added. Use
626 * iommu_group_put() to release this extra reference count, allowing the
627 * group to be automatically reclaimed once it has no devices or external
630 struct iommu_group *iommu_group_alloc(void)
632 struct iommu_group *group;
635 group = kzalloc(sizeof(*group), GFP_KERNEL);
637 return ERR_PTR(-ENOMEM);
639 group->kobj.kset = iommu_group_kset;
640 mutex_init(&group->mutex);
641 INIT_LIST_HEAD(&group->devices);
642 INIT_LIST_HEAD(&group->entry);
644 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
651 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
652 NULL, "%d", group->id);
654 ida_simple_remove(&iommu_group_ida, group->id);
655 kobject_put(&group->kobj);
659 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
660 if (!group->devices_kobj) {
661 kobject_put(&group->kobj); /* triggers .release & free */
662 return ERR_PTR(-ENOMEM);
666 * The devices_kobj holds a reference on the group kobject, so
667 * as long as that exists so will the group. We can therefore
668 * use the devices_kobj for reference counting.
670 kobject_put(&group->kobj);
672 ret = iommu_group_create_file(group,
673 &iommu_group_attr_reserved_regions);
677 ret = iommu_group_create_file(group, &iommu_group_attr_type);
681 pr_debug("Allocated group %d\n", group->id);
685 EXPORT_SYMBOL_GPL(iommu_group_alloc);
687 struct iommu_group *iommu_group_get_by_id(int id)
689 struct kobject *group_kobj;
690 struct iommu_group *group;
693 if (!iommu_group_kset)
696 name = kasprintf(GFP_KERNEL, "%d", id);
700 group_kobj = kset_find_obj(iommu_group_kset, name);
706 group = container_of(group_kobj, struct iommu_group, kobj);
707 BUG_ON(group->id != id);
709 kobject_get(group->devices_kobj);
710 kobject_put(&group->kobj);
714 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
717 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
720 * iommu drivers can store data in the group for use when doing iommu
721 * operations. This function provides a way to retrieve it. Caller
722 * should hold a group reference.
724 void *iommu_group_get_iommudata(struct iommu_group *group)
726 return group->iommu_data;
728 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
731 * iommu_group_set_iommudata - set iommu_data for a group
733 * @iommu_data: new data
734 * @release: release function for iommu_data
736 * iommu drivers can store data in the group for use when doing iommu
737 * operations. This function provides a way to set the data after
738 * the group has been allocated. Caller should hold a group reference.
740 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
741 void (*release)(void *iommu_data))
743 group->iommu_data = iommu_data;
744 group->iommu_data_release = release;
746 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
749 * iommu_group_set_name - set name for a group
753 * Allow iommu driver to set a name for a group. When set it will
754 * appear in a name attribute file under the group in sysfs.
756 int iommu_group_set_name(struct iommu_group *group, const char *name)
761 iommu_group_remove_file(group, &iommu_group_attr_name);
768 group->name = kstrdup(name, GFP_KERNEL);
772 ret = iommu_group_create_file(group, &iommu_group_attr_name);
781 EXPORT_SYMBOL_GPL(iommu_group_set_name);
783 static int iommu_create_device_direct_mappings(struct iommu_group *group,
786 struct iommu_domain *domain = group->default_domain;
787 struct iommu_resv_region *entry;
788 struct list_head mappings;
789 unsigned long pg_size;
792 if (!domain || !iommu_is_dma_domain(domain))
795 BUG_ON(!domain->pgsize_bitmap);
797 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
798 INIT_LIST_HEAD(&mappings);
800 iommu_get_resv_regions(dev, &mappings);
802 /* We need to consider overlapping regions for different devices */
803 list_for_each_entry(entry, &mappings, list) {
804 dma_addr_t start, end, addr;
807 start = ALIGN(entry->start, pg_size);
808 end = ALIGN(entry->start + entry->length, pg_size);
810 if (entry->type != IOMMU_RESV_DIRECT &&
811 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
814 for (addr = start; addr <= end; addr += pg_size) {
815 phys_addr_t phys_addr;
820 phys_addr = iommu_iova_to_phys(domain, addr);
828 ret = iommu_map(domain, addr - map_size,
829 addr - map_size, map_size,
839 iommu_flush_iotlb_all(domain);
842 iommu_put_resv_regions(dev, &mappings);
847 static bool iommu_is_attach_deferred(struct device *dev)
849 const struct iommu_ops *ops = dev_iommu_ops(dev);
851 if (ops->is_attach_deferred)
852 return ops->is_attach_deferred(dev);
858 * iommu_group_add_device - add a device to an iommu group
859 * @group: the group into which to add the device (reference should be held)
862 * This function is called by an iommu driver to add a device into a
863 * group. Adding a device increments the group reference count.
865 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
868 struct group_device *device;
870 device = kzalloc(sizeof(*device), GFP_KERNEL);
876 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
878 goto err_free_device;
880 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
884 goto err_remove_link;
887 ret = sysfs_create_link_nowarn(group->devices_kobj,
888 &dev->kobj, device->name);
890 if (ret == -EEXIST && i >= 0) {
892 * Account for the slim chance of collision
893 * and append an instance to the name.
896 device->name = kasprintf(GFP_KERNEL, "%s.%d",
897 kobject_name(&dev->kobj), i++);
903 kobject_get(group->devices_kobj);
905 dev->iommu_group = group;
907 mutex_lock(&group->mutex);
908 list_add_tail(&device->list, &group->devices);
909 if (group->domain && !iommu_is_attach_deferred(dev))
910 ret = __iommu_attach_device(group->domain, dev);
911 mutex_unlock(&group->mutex);
915 trace_add_device_to_group(group->id, dev);
917 dev_info(dev, "Adding to iommu group %d\n", group->id);
922 mutex_lock(&group->mutex);
923 list_del(&device->list);
924 mutex_unlock(&group->mutex);
925 dev->iommu_group = NULL;
926 kobject_put(group->devices_kobj);
927 sysfs_remove_link(group->devices_kobj, device->name);
931 sysfs_remove_link(&dev->kobj, "iommu_group");
934 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
937 EXPORT_SYMBOL_GPL(iommu_group_add_device);
940 * iommu_group_remove_device - remove a device from it's current group
941 * @dev: device to be removed
943 * This function is called by an iommu driver to remove the device from
944 * it's current group. This decrements the iommu group reference count.
946 void iommu_group_remove_device(struct device *dev)
948 struct iommu_group *group = dev->iommu_group;
949 struct group_device *tmp_device, *device = NULL;
954 dev_info(dev, "Removing from iommu group %d\n", group->id);
956 mutex_lock(&group->mutex);
957 list_for_each_entry(tmp_device, &group->devices, list) {
958 if (tmp_device->dev == dev) {
960 list_del(&device->list);
964 mutex_unlock(&group->mutex);
969 sysfs_remove_link(group->devices_kobj, device->name);
970 sysfs_remove_link(&dev->kobj, "iommu_group");
972 trace_remove_device_from_group(group->id, dev);
976 dev->iommu_group = NULL;
977 kobject_put(group->devices_kobj);
979 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
981 static int iommu_group_device_count(struct iommu_group *group)
983 struct group_device *entry;
986 list_for_each_entry(entry, &group->devices, list)
992 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
993 int (*fn)(struct device *, void *))
995 struct group_device *device;
998 list_for_each_entry(device, &group->devices, list) {
999 ret = fn(device->dev, data);
1007 * iommu_group_for_each_dev - iterate over each device in the group
1009 * @data: caller opaque data to be passed to callback function
1010 * @fn: caller supplied callback function
1012 * This function is called by group users to iterate over group devices.
1013 * Callers should hold a reference count to the group during callback.
1014 * The group->mutex is held across callbacks, which will block calls to
1015 * iommu_group_add/remove_device.
1017 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1018 int (*fn)(struct device *, void *))
1022 mutex_lock(&group->mutex);
1023 ret = __iommu_group_for_each_dev(group, data, fn);
1024 mutex_unlock(&group->mutex);
1028 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1031 * iommu_group_get - Return the group for a device and increment reference
1032 * @dev: get the group that this device belongs to
1034 * This function is called by iommu drivers and users to get the group
1035 * for the specified device. If found, the group is returned and the group
1036 * reference in incremented, else NULL.
1038 struct iommu_group *iommu_group_get(struct device *dev)
1040 struct iommu_group *group = dev->iommu_group;
1043 kobject_get(group->devices_kobj);
1047 EXPORT_SYMBOL_GPL(iommu_group_get);
1050 * iommu_group_ref_get - Increment reference on a group
1051 * @group: the group to use, must not be NULL
1053 * This function is called by iommu drivers to take additional references on an
1054 * existing group. Returns the given group for convenience.
1056 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1058 kobject_get(group->devices_kobj);
1061 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1064 * iommu_group_put - Decrement group reference
1065 * @group: the group to use
1067 * This function is called by iommu drivers and users to release the
1068 * iommu group. Once the reference count is zero, the group is released.
1070 void iommu_group_put(struct iommu_group *group)
1073 kobject_put(group->devices_kobj);
1075 EXPORT_SYMBOL_GPL(iommu_group_put);
1078 * iommu_register_device_fault_handler() - Register a device fault handler
1080 * @handler: the fault handler
1081 * @data: private data passed as argument to the handler
1083 * When an IOMMU fault event is received, this handler gets called with the
1084 * fault event and data as argument. The handler should return 0 on success. If
1085 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1086 * complete the fault by calling iommu_page_response() with one of the following
1088 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1089 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1090 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1091 * page faults if possible.
1093 * Return 0 if the fault handler was installed successfully, or an error.
1095 int iommu_register_device_fault_handler(struct device *dev,
1096 iommu_dev_fault_handler_t handler,
1099 struct dev_iommu *param = dev->iommu;
1105 mutex_lock(¶m->lock);
1106 /* Only allow one fault handler registered for each device */
1107 if (param->fault_param) {
1113 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1114 if (!param->fault_param) {
1119 param->fault_param->handler = handler;
1120 param->fault_param->data = data;
1121 mutex_init(¶m->fault_param->lock);
1122 INIT_LIST_HEAD(¶m->fault_param->faults);
1125 mutex_unlock(¶m->lock);
1129 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1132 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1135 * Remove the device fault handler installed with
1136 * iommu_register_device_fault_handler().
1138 * Return 0 on success, or an error.
1140 int iommu_unregister_device_fault_handler(struct device *dev)
1142 struct dev_iommu *param = dev->iommu;
1148 mutex_lock(¶m->lock);
1150 if (!param->fault_param)
1153 /* we cannot unregister handler if there are pending faults */
1154 if (!list_empty(¶m->fault_param->faults)) {
1159 kfree(param->fault_param);
1160 param->fault_param = NULL;
1163 mutex_unlock(¶m->lock);
1167 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1170 * iommu_report_device_fault() - Report fault event to device driver
1172 * @evt: fault event data
1174 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1175 * handler. When this function fails and the fault is recoverable, it is the
1176 * caller's responsibility to complete the fault.
1178 * Return 0 on success, or an error.
1180 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1182 struct dev_iommu *param = dev->iommu;
1183 struct iommu_fault_event *evt_pending = NULL;
1184 struct iommu_fault_param *fparam;
1190 /* we only report device fault if there is a handler registered */
1191 mutex_lock(¶m->lock);
1192 fparam = param->fault_param;
1193 if (!fparam || !fparam->handler) {
1198 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1199 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1200 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1206 mutex_lock(&fparam->lock);
1207 list_add_tail(&evt_pending->list, &fparam->faults);
1208 mutex_unlock(&fparam->lock);
1211 ret = fparam->handler(&evt->fault, fparam->data);
1212 if (ret && evt_pending) {
1213 mutex_lock(&fparam->lock);
1214 list_del(&evt_pending->list);
1215 mutex_unlock(&fparam->lock);
1219 mutex_unlock(¶m->lock);
1222 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1224 int iommu_page_response(struct device *dev,
1225 struct iommu_page_response *msg)
1229 struct iommu_fault_event *evt;
1230 struct iommu_fault_page_request *prm;
1231 struct dev_iommu *param = dev->iommu;
1232 const struct iommu_ops *ops = dev_iommu_ops(dev);
1233 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1235 if (!ops->page_response)
1238 if (!param || !param->fault_param)
1241 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1242 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1245 /* Only send response if there is a fault report pending */
1246 mutex_lock(¶m->fault_param->lock);
1247 if (list_empty(¶m->fault_param->faults)) {
1248 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1252 * Check if we have a matching page request pending to respond,
1253 * otherwise return -EINVAL
1255 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1256 prm = &evt->fault.prm;
1257 if (prm->grpid != msg->grpid)
1261 * If the PASID is required, the corresponding request is
1262 * matched using the group ID, the PASID valid bit and the PASID
1263 * value. Otherwise only the group ID matches request and
1266 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1267 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1270 if (!needs_pasid && has_pasid) {
1271 /* No big deal, just clear it. */
1272 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1276 ret = ops->page_response(dev, evt, msg);
1277 list_del(&evt->list);
1283 mutex_unlock(¶m->fault_param->lock);
1286 EXPORT_SYMBOL_GPL(iommu_page_response);
1289 * iommu_group_id - Return ID for a group
1290 * @group: the group to ID
1292 * Return the unique ID for the group matching the sysfs group number.
1294 int iommu_group_id(struct iommu_group *group)
1298 EXPORT_SYMBOL_GPL(iommu_group_id);
1300 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1301 unsigned long *devfns);
1304 * To consider a PCI device isolated, we require ACS to support Source
1305 * Validation, Request Redirection, Completer Redirection, and Upstream
1306 * Forwarding. This effectively means that devices cannot spoof their
1307 * requester ID, requests and completions cannot be redirected, and all
1308 * transactions are forwarded upstream, even as it passes through a
1309 * bridge where the target device is downstream.
1311 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1314 * For multifunction devices which are not isolated from each other, find
1315 * all the other non-isolated functions and look for existing groups. For
1316 * each function, we also need to look for aliases to or from other devices
1317 * that may already have a group.
1319 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1320 unsigned long *devfns)
1322 struct pci_dev *tmp = NULL;
1323 struct iommu_group *group;
1325 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1328 for_each_pci_dev(tmp) {
1329 if (tmp == pdev || tmp->bus != pdev->bus ||
1330 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1331 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1334 group = get_pci_alias_group(tmp, devfns);
1345 * Look for aliases to or from the given device for existing groups. DMA
1346 * aliases are only supported on the same bus, therefore the search
1347 * space is quite small (especially since we're really only looking at pcie
1348 * device, and therefore only expect multiple slots on the root complex or
1349 * downstream switch ports). It's conceivable though that a pair of
1350 * multifunction devices could have aliases between them that would cause a
1351 * loop. To prevent this, we use a bitmap to track where we've been.
1353 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1354 unsigned long *devfns)
1356 struct pci_dev *tmp = NULL;
1357 struct iommu_group *group;
1359 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1362 group = iommu_group_get(&pdev->dev);
1366 for_each_pci_dev(tmp) {
1367 if (tmp == pdev || tmp->bus != pdev->bus)
1370 /* We alias them or they alias us */
1371 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1372 group = get_pci_alias_group(tmp, devfns);
1378 group = get_pci_function_alias_group(tmp, devfns);
1389 struct group_for_pci_data {
1390 struct pci_dev *pdev;
1391 struct iommu_group *group;
1395 * DMA alias iterator callback, return the last seen device. Stop and return
1396 * the IOMMU group if we find one along the way.
1398 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1400 struct group_for_pci_data *data = opaque;
1403 data->group = iommu_group_get(&pdev->dev);
1405 return data->group != NULL;
1409 * Generic device_group call-back function. It just allocates one
1410 * iommu-group per device.
1412 struct iommu_group *generic_device_group(struct device *dev)
1414 return iommu_group_alloc();
1416 EXPORT_SYMBOL_GPL(generic_device_group);
1419 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1420 * to find or create an IOMMU group for a device.
1422 struct iommu_group *pci_device_group(struct device *dev)
1424 struct pci_dev *pdev = to_pci_dev(dev);
1425 struct group_for_pci_data data;
1426 struct pci_bus *bus;
1427 struct iommu_group *group = NULL;
1428 u64 devfns[4] = { 0 };
1430 if (WARN_ON(!dev_is_pci(dev)))
1431 return ERR_PTR(-EINVAL);
1434 * Find the upstream DMA alias for the device. A device must not
1435 * be aliased due to topology in order to have its own IOMMU group.
1436 * If we find an alias along the way that already belongs to a
1439 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1445 * Continue upstream from the point of minimum IOMMU granularity
1446 * due to aliases to the point where devices are protected from
1447 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1450 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1454 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1459 group = iommu_group_get(&pdev->dev);
1465 * Look for existing groups on device aliases. If we alias another
1466 * device or another device aliases us, use the same group.
1468 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1473 * Look for existing groups on non-isolated functions on the same
1474 * slot and aliases of those funcions, if any. No need to clear
1475 * the search bitmap, the tested devfns are still valid.
1477 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1481 /* No shared group found, allocate new */
1482 return iommu_group_alloc();
1484 EXPORT_SYMBOL_GPL(pci_device_group);
1486 /* Get the IOMMU group for device on fsl-mc bus */
1487 struct iommu_group *fsl_mc_device_group(struct device *dev)
1489 struct device *cont_dev = fsl_mc_cont_dev(dev);
1490 struct iommu_group *group;
1492 group = iommu_group_get(cont_dev);
1494 group = iommu_group_alloc();
1497 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1499 static int iommu_get_def_domain_type(struct device *dev)
1501 const struct iommu_ops *ops = dev_iommu_ops(dev);
1503 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
1504 return IOMMU_DOMAIN_DMA;
1506 if (ops->def_domain_type)
1507 return ops->def_domain_type(dev);
1512 static int iommu_group_alloc_default_domain(struct bus_type *bus,
1513 struct iommu_group *group,
1516 struct iommu_domain *dom;
1518 dom = __iommu_domain_alloc(bus, type);
1519 if (!dom && type != IOMMU_DOMAIN_DMA) {
1520 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1522 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1529 group->default_domain = dom;
1531 group->domain = dom;
1535 static int iommu_alloc_default_domain(struct iommu_group *group,
1540 if (group->default_domain)
1543 type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
1545 return iommu_group_alloc_default_domain(dev->bus, group, type);
1549 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1550 * @dev: target device
1552 * This function is intended to be called by IOMMU drivers and extended to
1553 * support common, bus-defined algorithms when determining or creating the
1554 * IOMMU group for a device. On success, the caller will hold a reference
1555 * to the returned IOMMU group, which will already include the provided
1556 * device. The reference should be released with iommu_group_put().
1558 static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1560 const struct iommu_ops *ops = dev_iommu_ops(dev);
1561 struct iommu_group *group;
1564 group = iommu_group_get(dev);
1568 group = ops->device_group(dev);
1569 if (WARN_ON_ONCE(group == NULL))
1570 return ERR_PTR(-EINVAL);
1575 ret = iommu_group_add_device(group, dev);
1582 iommu_group_put(group);
1584 return ERR_PTR(ret);
1587 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1589 return group->default_domain;
1592 static int probe_iommu_group(struct device *dev, void *data)
1594 struct list_head *group_list = data;
1595 struct iommu_group *group;
1598 /* Device is probed already if in a group */
1599 group = iommu_group_get(dev);
1601 iommu_group_put(group);
1605 ret = __iommu_probe_device(dev, group_list);
1612 static int remove_iommu_group(struct device *dev, void *data)
1614 iommu_release_device(dev);
1619 static int iommu_bus_notifier(struct notifier_block *nb,
1620 unsigned long action, void *data)
1622 struct device *dev = data;
1624 if (action == BUS_NOTIFY_ADD_DEVICE) {
1627 ret = iommu_probe_device(dev);
1628 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1629 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1630 iommu_release_device(dev);
1637 struct __group_domain_type {
1642 static int probe_get_default_domain_type(struct device *dev, void *data)
1644 struct __group_domain_type *gtype = data;
1645 unsigned int type = iommu_get_def_domain_type(dev);
1648 if (gtype->type && gtype->type != type) {
1649 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1650 iommu_domain_type_str(type),
1651 dev_name(gtype->dev),
1652 iommu_domain_type_str(gtype->type));
1665 static void probe_alloc_default_domain(struct bus_type *bus,
1666 struct iommu_group *group)
1668 struct __group_domain_type gtype;
1670 memset(>ype, 0, sizeof(gtype));
1672 /* Ask for default domain requirements of all devices in the group */
1673 __iommu_group_for_each_dev(group, >ype,
1674 probe_get_default_domain_type);
1677 gtype.type = iommu_def_domain_type;
1679 iommu_group_alloc_default_domain(bus, group, gtype.type);
1683 static int iommu_group_do_dma_attach(struct device *dev, void *data)
1685 struct iommu_domain *domain = data;
1688 if (!iommu_is_attach_deferred(dev))
1689 ret = __iommu_attach_device(domain, dev);
1694 static int __iommu_group_dma_attach(struct iommu_group *group)
1696 return __iommu_group_for_each_dev(group, group->default_domain,
1697 iommu_group_do_dma_attach);
1700 static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1702 const struct iommu_ops *ops = dev_iommu_ops(dev);
1704 if (ops->probe_finalize)
1705 ops->probe_finalize(dev);
1710 static void __iommu_group_dma_finalize(struct iommu_group *group)
1712 __iommu_group_for_each_dev(group, group->default_domain,
1713 iommu_group_do_probe_finalize);
1716 static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1718 struct iommu_group *group = data;
1720 iommu_create_device_direct_mappings(group, dev);
1725 static int iommu_group_create_direct_mappings(struct iommu_group *group)
1727 return __iommu_group_for_each_dev(group, group,
1728 iommu_do_create_direct_mappings);
1731 int bus_iommu_probe(struct bus_type *bus)
1733 struct iommu_group *group, *next;
1734 LIST_HEAD(group_list);
1738 * This code-path does not allocate the default domain when
1739 * creating the iommu group, so do it after the groups are
1742 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1746 list_for_each_entry_safe(group, next, &group_list, entry) {
1747 /* Remove item from the list */
1748 list_del_init(&group->entry);
1750 mutex_lock(&group->mutex);
1752 /* Try to allocate default domain */
1753 probe_alloc_default_domain(bus, group);
1755 if (!group->default_domain) {
1756 mutex_unlock(&group->mutex);
1760 iommu_group_create_direct_mappings(group);
1762 ret = __iommu_group_dma_attach(group);
1764 mutex_unlock(&group->mutex);
1769 __iommu_group_dma_finalize(group);
1775 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1777 struct notifier_block *nb;
1780 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1784 nb->notifier_call = iommu_bus_notifier;
1786 err = bus_register_notifier(bus, nb);
1790 err = bus_iommu_probe(bus);
1799 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1800 bus_unregister_notifier(bus, nb);
1809 * bus_set_iommu - set iommu-callbacks for the bus
1811 * @ops: the callbacks provided by the iommu-driver
1813 * This function is called by an iommu driver to set the iommu methods
1814 * used for a particular bus. Drivers for devices on that bus can use
1815 * the iommu-api after these ops are registered.
1816 * This special function is needed because IOMMUs are usually devices on
1817 * the bus itself, so the iommu drivers are not initialized when the bus
1818 * is set up. With this function the iommu-driver can set the iommu-ops
1821 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1826 bus->iommu_ops = NULL;
1830 if (bus->iommu_ops != NULL)
1833 bus->iommu_ops = ops;
1835 /* Do IOMMU specific setup for this bus-type */
1836 err = iommu_bus_init(bus, ops);
1838 bus->iommu_ops = NULL;
1842 EXPORT_SYMBOL_GPL(bus_set_iommu);
1844 bool iommu_present(struct bus_type *bus)
1846 return bus->iommu_ops != NULL;
1848 EXPORT_SYMBOL_GPL(iommu_present);
1851 * device_iommu_capable() - check for a general IOMMU capability
1852 * @dev: device to which the capability would be relevant, if available
1853 * @cap: IOMMU capability
1855 * Return: true if an IOMMU is present and supports the given capability
1856 * for the given device, otherwise false.
1858 bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
1860 const struct iommu_ops *ops;
1862 if (!dev->iommu || !dev->iommu->iommu_dev)
1865 ops = dev_iommu_ops(dev);
1869 return ops->capable(cap);
1871 EXPORT_SYMBOL_GPL(device_iommu_capable);
1873 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1875 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1878 return bus->iommu_ops->capable(cap);
1880 EXPORT_SYMBOL_GPL(iommu_capable);
1883 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1884 * @domain: iommu domain
1885 * @handler: fault handler
1886 * @token: user data, will be passed back to the fault handler
1888 * This function should be used by IOMMU users which want to be notified
1889 * whenever an IOMMU fault happens.
1891 * The fault handler itself should return 0 on success, and an appropriate
1892 * error code otherwise.
1894 void iommu_set_fault_handler(struct iommu_domain *domain,
1895 iommu_fault_handler_t handler,
1900 domain->handler = handler;
1901 domain->handler_token = token;
1903 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1905 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1908 struct iommu_domain *domain;
1910 if (bus == NULL || bus->iommu_ops == NULL)
1913 domain = bus->iommu_ops->domain_alloc(type);
1917 domain->type = type;
1918 /* Assume all sizes by default; the driver may override this later */
1919 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1921 domain->ops = bus->iommu_ops->default_domain_ops;
1923 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {
1924 iommu_domain_free(domain);
1930 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1932 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1934 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1936 void iommu_domain_free(struct iommu_domain *domain)
1938 iommu_put_dma_cookie(domain);
1939 domain->ops->free(domain);
1941 EXPORT_SYMBOL_GPL(iommu_domain_free);
1944 * Put the group's domain back to the appropriate core-owned domain - either the
1945 * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
1947 static void __iommu_group_set_core_domain(struct iommu_group *group)
1949 struct iommu_domain *new_domain;
1953 new_domain = group->blocking_domain;
1955 new_domain = group->default_domain;
1957 ret = __iommu_group_set_domain(group, new_domain);
1958 WARN(ret, "iommu driver failed to attach the default/blocking domain");
1961 static int __iommu_attach_device(struct iommu_domain *domain,
1966 if (unlikely(domain->ops->attach_dev == NULL))
1969 ret = domain->ops->attach_dev(domain, dev);
1971 trace_attach_device_to_domain(dev);
1975 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1977 struct iommu_group *group;
1980 group = iommu_group_get(dev);
1985 * Lock the group to make sure the device-count doesn't
1986 * change while we are attaching
1988 mutex_lock(&group->mutex);
1990 if (iommu_group_device_count(group) != 1)
1993 ret = __iommu_attach_group(domain, group);
1996 mutex_unlock(&group->mutex);
1997 iommu_group_put(group);
2001 EXPORT_SYMBOL_GPL(iommu_attach_device);
2003 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2005 if (iommu_is_attach_deferred(dev))
2006 return __iommu_attach_device(domain, dev);
2011 static void __iommu_detach_device(struct iommu_domain *domain,
2014 if (iommu_is_attach_deferred(dev))
2017 domain->ops->detach_dev(domain, dev);
2018 trace_detach_device_from_domain(dev);
2021 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2023 struct iommu_group *group;
2025 group = iommu_group_get(dev);
2029 mutex_lock(&group->mutex);
2030 if (WARN_ON(domain != group->domain) ||
2031 WARN_ON(iommu_group_device_count(group) != 1))
2033 __iommu_group_set_core_domain(group);
2036 mutex_unlock(&group->mutex);
2037 iommu_group_put(group);
2039 EXPORT_SYMBOL_GPL(iommu_detach_device);
2041 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2043 struct iommu_domain *domain;
2044 struct iommu_group *group;
2046 group = iommu_group_get(dev);
2050 domain = group->domain;
2052 iommu_group_put(group);
2056 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2059 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2060 * guarantees that the group and its default domain are valid and correct.
2062 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2064 return dev->iommu_group->default_domain;
2068 * IOMMU groups are really the natural working unit of the IOMMU, but
2069 * the IOMMU API works on domains and devices. Bridge that gap by
2070 * iterating over the devices in a group. Ideally we'd have a single
2071 * device which represents the requestor ID of the group, but we also
2072 * allow IOMMU drivers to create policy defined minimum sets, where
2073 * the physical hardware may be able to distiguish members, but we
2074 * wish to group them at a higher level (ex. untrusted multi-function
2075 * PCI devices). Thus we attach each device.
2077 static int iommu_group_do_attach_device(struct device *dev, void *data)
2079 struct iommu_domain *domain = data;
2081 return __iommu_attach_device(domain, dev);
2084 static int __iommu_attach_group(struct iommu_domain *domain,
2085 struct iommu_group *group)
2089 if (group->domain && group->domain != group->default_domain &&
2090 group->domain != group->blocking_domain)
2093 ret = __iommu_group_for_each_dev(group, domain,
2094 iommu_group_do_attach_device);
2096 group->domain = domain;
2101 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2105 mutex_lock(&group->mutex);
2106 ret = __iommu_attach_group(domain, group);
2107 mutex_unlock(&group->mutex);
2111 EXPORT_SYMBOL_GPL(iommu_attach_group);
2113 static int iommu_group_do_detach_device(struct device *dev, void *data)
2115 struct iommu_domain *domain = data;
2117 __iommu_detach_device(domain, dev);
2122 static int __iommu_group_set_domain(struct iommu_group *group,
2123 struct iommu_domain *new_domain)
2127 if (group->domain == new_domain)
2131 * New drivers should support default domains and so the detach_dev() op
2132 * will never be called. Otherwise the NULL domain represents some
2133 * platform specific behavior.
2136 if (WARN_ON(!group->domain->ops->detach_dev))
2138 __iommu_group_for_each_dev(group, group->domain,
2139 iommu_group_do_detach_device);
2140 group->domain = NULL;
2145 * Changing the domain is done by calling attach_dev() on the new
2146 * domain. This switch does not have to be atomic and DMA can be
2147 * discarded during the transition. DMA must only be able to access
2148 * either new_domain or group->domain, never something else.
2150 * Note that this is called in error unwind paths, attaching to a
2151 * domain that has already been attached cannot fail.
2153 ret = __iommu_group_for_each_dev(group, new_domain,
2154 iommu_group_do_attach_device);
2157 group->domain = new_domain;
2161 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2163 mutex_lock(&group->mutex);
2164 __iommu_group_set_core_domain(group);
2165 mutex_unlock(&group->mutex);
2167 EXPORT_SYMBOL_GPL(iommu_detach_group);
2169 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2171 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2174 if (domain->type == IOMMU_DOMAIN_BLOCKED)
2177 return domain->ops->iova_to_phys(domain, iova);
2179 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2181 static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2182 phys_addr_t paddr, size_t size, size_t *count)
2184 unsigned int pgsize_idx, pgsize_idx_next;
2185 unsigned long pgsizes;
2186 size_t offset, pgsize, pgsize_next;
2187 unsigned long addr_merge = paddr | iova;
2189 /* Page sizes supported by the hardware and small enough for @size */
2190 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2192 /* Constrain the page sizes further based on the maximum alignment */
2193 if (likely(addr_merge))
2194 pgsizes &= GENMASK(__ffs(addr_merge), 0);
2196 /* Make sure we have at least one suitable page size */
2199 /* Pick the biggest page size remaining */
2200 pgsize_idx = __fls(pgsizes);
2201 pgsize = BIT(pgsize_idx);
2205 /* Find the next biggest support page size, if it exists */
2206 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2210 pgsize_idx_next = __ffs(pgsizes);
2211 pgsize_next = BIT(pgsize_idx_next);
2214 * There's no point trying a bigger page size unless the virtual
2215 * and physical addresses are similarly offset within the larger page.
2217 if ((iova ^ paddr) & (pgsize_next - 1))
2220 /* Calculate the offset to the next page size alignment boundary */
2221 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2224 * If size is big enough to accommodate the larger page, reduce
2225 * the number of smaller pages.
2227 if (offset + pgsize_next <= size)
2231 *count = size >> pgsize_idx;
2235 static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova,
2236 phys_addr_t paddr, size_t size, int prot,
2237 gfp_t gfp, size_t *mapped)
2239 const struct iommu_domain_ops *ops = domain->ops;
2240 size_t pgsize, count;
2243 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2245 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2246 iova, &paddr, pgsize, count);
2248 if (ops->map_pages) {
2249 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2252 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2253 *mapped = ret ? 0 : pgsize;
2259 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2260 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2262 const struct iommu_domain_ops *ops = domain->ops;
2263 unsigned long orig_iova = iova;
2264 unsigned int min_pagesz;
2265 size_t orig_size = size;
2266 phys_addr_t orig_paddr = paddr;
2269 if (unlikely(!(ops->map || ops->map_pages) ||
2270 domain->pgsize_bitmap == 0UL))
2273 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2276 /* find out the minimum page size supported */
2277 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2280 * both the virtual address and the physical one, as well as
2281 * the size of the mapping, must be aligned (at least) to the
2282 * size of the smallest page supported by the hardware
2284 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2285 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2286 iova, &paddr, size, min_pagesz);
2290 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2295 ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp,
2298 * Some pages may have been mapped, even if an error occurred,
2299 * so we should account for those so they can be unmapped.
2310 /* unroll mapping in case something went wrong */
2312 iommu_unmap(domain, orig_iova, orig_size - size);
2314 trace_map(orig_iova, orig_paddr, orig_size);
2319 static int _iommu_map(struct iommu_domain *domain, unsigned long iova,
2320 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2322 const struct iommu_domain_ops *ops = domain->ops;
2325 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2326 if (ret == 0 && ops->iotlb_sync_map)
2327 ops->iotlb_sync_map(domain, iova, size);
2332 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2333 phys_addr_t paddr, size_t size, int prot)
2336 return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2338 EXPORT_SYMBOL_GPL(iommu_map);
2340 int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2341 phys_addr_t paddr, size_t size, int prot)
2343 return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2345 EXPORT_SYMBOL_GPL(iommu_map_atomic);
2347 static size_t __iommu_unmap_pages(struct iommu_domain *domain,
2348 unsigned long iova, size_t size,
2349 struct iommu_iotlb_gather *iotlb_gather)
2351 const struct iommu_domain_ops *ops = domain->ops;
2352 size_t pgsize, count;
2354 pgsize = iommu_pgsize(domain, iova, iova, size, &count);
2355 return ops->unmap_pages ?
2356 ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) :
2357 ops->unmap(domain, iova, pgsize, iotlb_gather);
2360 static size_t __iommu_unmap(struct iommu_domain *domain,
2361 unsigned long iova, size_t size,
2362 struct iommu_iotlb_gather *iotlb_gather)
2364 const struct iommu_domain_ops *ops = domain->ops;
2365 size_t unmapped_page, unmapped = 0;
2366 unsigned long orig_iova = iova;
2367 unsigned int min_pagesz;
2369 if (unlikely(!(ops->unmap || ops->unmap_pages) ||
2370 domain->pgsize_bitmap == 0UL))
2373 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2376 /* find out the minimum page size supported */
2377 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2380 * The virtual address, as well as the size of the mapping, must be
2381 * aligned (at least) to the size of the smallest page supported
2384 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2385 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2386 iova, size, min_pagesz);
2390 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2393 * Keep iterating until we either unmap 'size' bytes (or more)
2394 * or we hit an area that isn't mapped.
2396 while (unmapped < size) {
2397 unmapped_page = __iommu_unmap_pages(domain, iova,
2403 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2404 iova, unmapped_page);
2406 iova += unmapped_page;
2407 unmapped += unmapped_page;
2410 trace_unmap(orig_iova, size, unmapped);
2414 size_t iommu_unmap(struct iommu_domain *domain,
2415 unsigned long iova, size_t size)
2417 struct iommu_iotlb_gather iotlb_gather;
2420 iommu_iotlb_gather_init(&iotlb_gather);
2421 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2422 iommu_iotlb_sync(domain, &iotlb_gather);
2426 EXPORT_SYMBOL_GPL(iommu_unmap);
2428 size_t iommu_unmap_fast(struct iommu_domain *domain,
2429 unsigned long iova, size_t size,
2430 struct iommu_iotlb_gather *iotlb_gather)
2432 return __iommu_unmap(domain, iova, size, iotlb_gather);
2434 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2436 static ssize_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2437 struct scatterlist *sg, unsigned int nents, int prot,
2440 const struct iommu_domain_ops *ops = domain->ops;
2441 size_t len = 0, mapped = 0;
2446 while (i <= nents) {
2447 phys_addr_t s_phys = sg_phys(sg);
2449 if (len && s_phys != start + len) {
2450 ret = __iommu_map(domain, iova + mapped, start,
2471 if (ops->iotlb_sync_map)
2472 ops->iotlb_sync_map(domain, iova, mapped);
2476 /* undo mappings already done */
2477 iommu_unmap(domain, iova, mapped);
2482 ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2483 struct scatterlist *sg, unsigned int nents, int prot)
2486 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2488 EXPORT_SYMBOL_GPL(iommu_map_sg);
2490 ssize_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2491 struct scatterlist *sg, unsigned int nents, int prot)
2493 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2497 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2498 * @domain: the iommu domain where the fault has happened
2499 * @dev: the device where the fault has happened
2500 * @iova: the faulting address
2501 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2503 * This function should be called by the low-level IOMMU implementations
2504 * whenever IOMMU faults happen, to allow high-level users, that are
2505 * interested in such events, to know about them.
2507 * This event may be useful for several possible use cases:
2508 * - mere logging of the event
2509 * - dynamic TLB/PTE loading
2510 * - if restarting of the faulting device is required
2512 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2513 * PTE/TLB loading will one day be supported, implementations will be able
2514 * to tell whether it succeeded or not according to this return value).
2516 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2517 * (though fault handlers can also return -ENOSYS, in case they want to
2518 * elicit the default behavior of the IOMMU drivers).
2520 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2521 unsigned long iova, int flags)
2526 * if upper layers showed interest and installed a fault handler,
2529 if (domain->handler)
2530 ret = domain->handler(domain, dev, iova, flags,
2531 domain->handler_token);
2533 trace_io_page_fault(dev, iova, flags);
2536 EXPORT_SYMBOL_GPL(report_iommu_fault);
2538 static int __init iommu_init(void)
2540 iommu_group_kset = kset_create_and_add("iommu_groups",
2542 BUG_ON(!iommu_group_kset);
2544 iommu_debugfs_setup();
2548 core_initcall(iommu_init);
2550 int iommu_enable_nesting(struct iommu_domain *domain)
2552 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2554 if (!domain->ops->enable_nesting)
2556 return domain->ops->enable_nesting(domain);
2558 EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2560 int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2561 unsigned long quirk)
2563 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2565 if (!domain->ops->set_pgtable_quirks)
2567 return domain->ops->set_pgtable_quirks(domain, quirk);
2569 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2571 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2573 const struct iommu_ops *ops = dev_iommu_ops(dev);
2575 if (ops->get_resv_regions)
2576 ops->get_resv_regions(dev, list);
2579 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2581 const struct iommu_ops *ops = dev_iommu_ops(dev);
2583 if (ops->put_resv_regions)
2584 ops->put_resv_regions(dev, list);
2588 * generic_iommu_put_resv_regions - Reserved region driver helper
2589 * @dev: device for which to free reserved regions
2590 * @list: reserved region list for device
2592 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2593 * for simple reservations. Memory allocated for each reserved region will be
2594 * freed. If an IOMMU driver allocates additional resources per region, it is
2595 * going to have to implement a custom callback.
2597 void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2599 struct iommu_resv_region *entry, *next;
2601 list_for_each_entry_safe(entry, next, list, list)
2604 EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2606 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2607 size_t length, int prot,
2608 enum iommu_resv_type type)
2610 struct iommu_resv_region *region;
2612 region = kzalloc(sizeof(*region), GFP_KERNEL);
2616 INIT_LIST_HEAD(®ion->list);
2617 region->start = start;
2618 region->length = length;
2619 region->prot = prot;
2620 region->type = type;
2623 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2625 void iommu_set_default_passthrough(bool cmd_line)
2628 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2629 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2632 void iommu_set_default_translated(bool cmd_line)
2635 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2636 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2639 bool iommu_default_passthrough(void)
2641 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2643 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2645 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2647 const struct iommu_ops *ops = NULL;
2648 struct iommu_device *iommu;
2650 spin_lock(&iommu_device_lock);
2651 list_for_each_entry(iommu, &iommu_device_list, list)
2652 if (iommu->fwnode == fwnode) {
2656 spin_unlock(&iommu_device_lock);
2660 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2661 const struct iommu_ops *ops)
2663 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2666 return ops == fwspec->ops ? 0 : -EINVAL;
2668 if (!dev_iommu_get(dev))
2671 /* Preallocate for the overwhelmingly common case of 1 ID */
2672 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2676 of_node_get(to_of_node(iommu_fwnode));
2677 fwspec->iommu_fwnode = iommu_fwnode;
2679 dev_iommu_fwspec_set(dev, fwspec);
2682 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2684 void iommu_fwspec_free(struct device *dev)
2686 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2689 fwnode_handle_put(fwspec->iommu_fwnode);
2691 dev_iommu_fwspec_set(dev, NULL);
2694 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2696 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2698 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2704 new_num = fwspec->num_ids + num_ids;
2706 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2711 dev_iommu_fwspec_set(dev, fwspec);
2714 for (i = 0; i < num_ids; i++)
2715 fwspec->ids[fwspec->num_ids + i] = ids[i];
2717 fwspec->num_ids = new_num;
2720 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2723 * Per device IOMMU features.
2725 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2727 if (dev->iommu && dev->iommu->iommu_dev) {
2728 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2730 if (ops->dev_enable_feat)
2731 return ops->dev_enable_feat(dev, feat);
2736 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2739 * The device drivers should do the necessary cleanups before calling this.
2741 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2743 if (dev->iommu && dev->iommu->iommu_dev) {
2744 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2746 if (ops->dev_disable_feat)
2747 return ops->dev_disable_feat(dev, feat);
2752 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2754 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2756 if (dev->iommu && dev->iommu->iommu_dev) {
2757 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2759 if (ops->dev_feat_enabled)
2760 return ops->dev_feat_enabled(dev, feat);
2765 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2768 * iommu_sva_bind_device() - Bind a process address space to a device
2770 * @mm: the mm to bind, caller must hold a reference to it
2771 * @drvdata: opaque data pointer to pass to bind callback
2773 * Create a bond between device and address space, allowing the device to access
2774 * the mm using the returned PASID. If a bond already exists between @device and
2775 * @mm, it is returned and an additional reference is taken. Caller must call
2776 * iommu_sva_unbind_device() to release each reference.
2778 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2779 * initialize the required SVA features.
2781 * On error, returns an ERR_PTR value.
2784 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2786 struct iommu_group *group;
2787 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2788 const struct iommu_ops *ops = dev_iommu_ops(dev);
2791 return ERR_PTR(-ENODEV);
2793 group = iommu_group_get(dev);
2795 return ERR_PTR(-ENODEV);
2797 /* Ensure device count and domain don't change while we're binding */
2798 mutex_lock(&group->mutex);
2801 * To keep things simple, SVA currently doesn't support IOMMU groups
2802 * with more than one device. Existing SVA-capable systems are not
2803 * affected by the problems that required IOMMU groups (lack of ACS
2804 * isolation, device ID aliasing and other hardware issues).
2806 if (iommu_group_device_count(group) != 1)
2809 handle = ops->sva_bind(dev, mm, drvdata);
2812 mutex_unlock(&group->mutex);
2813 iommu_group_put(group);
2817 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2820 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2821 * @handle: the handle returned by iommu_sva_bind_device()
2823 * Put reference to a bond between device and address space. The device should
2824 * not be issuing any more transaction for this PASID. All outstanding page
2825 * requests for this PASID must have been flushed to the IOMMU.
2827 void iommu_sva_unbind_device(struct iommu_sva *handle)
2829 struct iommu_group *group;
2830 struct device *dev = handle->dev;
2831 const struct iommu_ops *ops = dev_iommu_ops(dev);
2833 if (!ops->sva_unbind)
2836 group = iommu_group_get(dev);
2840 mutex_lock(&group->mutex);
2841 ops->sva_unbind(handle);
2842 mutex_unlock(&group->mutex);
2844 iommu_group_put(group);
2846 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2848 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
2850 const struct iommu_ops *ops = dev_iommu_ops(handle->dev);
2852 if (!ops->sva_get_pasid)
2853 return IOMMU_PASID_INVALID;
2855 return ops->sva_get_pasid(handle);
2857 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
2860 * Changes the default domain of an iommu group that has *only* one device
2862 * @group: The group for which the default domain should be changed
2863 * @prev_dev: The device in the group (this is used to make sure that the device
2864 * hasn't changed after the caller has called this function)
2865 * @type: The type of the new default domain that gets associated with the group
2867 * Returns 0 on success and error code on failure
2870 * 1. Presently, this function is called only when user requests to change the
2871 * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
2872 * Please take a closer look if intended to use for other purposes.
2874 static int iommu_change_dev_def_domain(struct iommu_group *group,
2875 struct device *prev_dev, int type)
2877 struct iommu_domain *prev_dom;
2878 struct group_device *grp_dev;
2879 int ret, dev_def_dom;
2882 mutex_lock(&group->mutex);
2884 if (group->default_domain != group->domain) {
2885 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n");
2891 * iommu group wasn't locked while acquiring device lock in
2892 * iommu_group_store_type(). So, make sure that the device count hasn't
2893 * changed while acquiring device lock.
2895 * Changing default domain of an iommu group with two or more devices
2896 * isn't supported because there could be a potential deadlock. Consider
2897 * the following scenario. T1 is trying to acquire device locks of all
2898 * the devices in the group and before it could acquire all of them,
2899 * there could be another thread T2 (from different sub-system and use
2900 * case) that has already acquired some of the device locks and might be
2901 * waiting for T1 to release other device locks.
2903 if (iommu_group_device_count(group) != 1) {
2904 dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n");
2909 /* Since group has only one device */
2910 grp_dev = list_first_entry(&group->devices, struct group_device, list);
2913 if (prev_dev != dev) {
2914 dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n");
2919 prev_dom = group->default_domain;
2925 dev_def_dom = iommu_get_def_domain_type(dev);
2928 * If the user hasn't requested any specific type of domain and
2929 * if the device supports both the domains, then default to the
2930 * domain the device was booted with
2932 type = dev_def_dom ? : iommu_def_domain_type;
2933 } else if (dev_def_dom && type != dev_def_dom) {
2934 dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
2935 iommu_domain_type_str(type));
2941 * Switch to a new domain only if the requested domain type is different
2942 * from the existing default domain type
2944 if (prev_dom->type == type) {
2949 /* We can bring up a flush queue without tearing down the domain */
2950 if (type == IOMMU_DOMAIN_DMA_FQ && prev_dom->type == IOMMU_DOMAIN_DMA) {
2951 ret = iommu_dma_init_fq(prev_dom);
2953 prev_dom->type = IOMMU_DOMAIN_DMA_FQ;
2957 /* Sets group->default_domain to the newly allocated domain */
2958 ret = iommu_group_alloc_default_domain(dev->bus, group, type);
2962 ret = iommu_create_device_direct_mappings(group, dev);
2964 goto free_new_domain;
2966 ret = __iommu_attach_device(group->default_domain, dev);
2968 goto free_new_domain;
2970 group->domain = group->default_domain;
2973 * Release the mutex here because ops->probe_finalize() call-back of
2974 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
2975 * in-turn might call back into IOMMU core code, where it tries to take
2976 * group->mutex, resulting in a deadlock.
2978 mutex_unlock(&group->mutex);
2980 /* Make sure dma_ops is appropriatley set */
2981 iommu_group_do_probe_finalize(dev, group->default_domain);
2982 iommu_domain_free(prev_dom);
2986 iommu_domain_free(group->default_domain);
2987 group->default_domain = prev_dom;
2988 group->domain = prev_dom;
2991 mutex_unlock(&group->mutex);
2997 * Changing the default domain through sysfs requires the users to unbind the
2998 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
2999 * transition. Return failure if this isn't met.
3001 * We need to consider the race between this and the device release path.
3002 * device_lock(dev) is used here to guarantee that the device release path
3003 * will not be entered at the same time.
3005 static ssize_t iommu_group_store_type(struct iommu_group *group,
3006 const char *buf, size_t count)
3008 struct group_device *grp_dev;
3012 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3015 if (WARN_ON(!group) || !group->default_domain)
3018 if (sysfs_streq(buf, "identity"))
3019 req_type = IOMMU_DOMAIN_IDENTITY;
3020 else if (sysfs_streq(buf, "DMA"))
3021 req_type = IOMMU_DOMAIN_DMA;
3022 else if (sysfs_streq(buf, "DMA-FQ"))
3023 req_type = IOMMU_DOMAIN_DMA_FQ;
3024 else if (sysfs_streq(buf, "auto"))
3030 * Lock/Unlock the group mutex here before device lock to
3031 * 1. Make sure that the iommu group has only one device (this is a
3032 * prerequisite for step 2)
3033 * 2. Get struct *dev which is needed to lock device
3035 mutex_lock(&group->mutex);
3036 if (iommu_group_device_count(group) != 1) {
3037 mutex_unlock(&group->mutex);
3038 pr_err_ratelimited("Cannot change default domain: Group has more than one device\n");
3042 /* Since group has only one device */
3043 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3048 * Don't hold the group mutex because taking group mutex first and then
3049 * the device lock could potentially cause a deadlock as below. Assume
3050 * two threads T1 and T2. T1 is trying to change default domain of an
3051 * iommu group and T2 is trying to hot unplug a device or release [1] VF
3052 * of a PCIe device which is in the same iommu group. T1 takes group
3053 * mutex and before it could take device lock assume T2 has taken device
3054 * lock and is yet to take group mutex. Now, both the threads will be
3055 * waiting for the other thread to release lock. Below, lock order was
3058 * mutex_lock(&group->mutex);
3059 * iommu_change_dev_def_domain();
3060 * mutex_unlock(&group->mutex);
3061 * device_unlock(dev);
3063 * [1] Typical device release path
3064 * device_lock() from device/driver core code
3066 * -> iommu_bus_notifier()
3067 * -> iommu_release_device()
3068 * -> ops->release_device() vendor driver calls back iommu core code
3069 * -> mutex_lock() from iommu core code
3071 mutex_unlock(&group->mutex);
3073 /* Check if the device in the group still has a driver bound to it */
3075 if (device_is_bound(dev) && !(req_type == IOMMU_DOMAIN_DMA_FQ &&
3076 group->default_domain->type == IOMMU_DOMAIN_DMA)) {
3077 pr_err_ratelimited("Device is still bound to driver\n");
3082 ret = iommu_change_dev_def_domain(group, dev, req_type);
3093 * iommu_device_use_default_domain() - Device driver wants to handle device
3094 * DMA through the kernel DMA API.
3097 * The device driver about to bind @dev wants to do DMA through the kernel
3098 * DMA API. Return 0 if it is allowed, otherwise an error.
3100 int iommu_device_use_default_domain(struct device *dev)
3102 struct iommu_group *group = iommu_group_get(dev);
3108 mutex_lock(&group->mutex);
3109 if (group->owner_cnt) {
3110 if (group->domain != group->default_domain ||
3120 mutex_unlock(&group->mutex);
3121 iommu_group_put(group);
3127 * iommu_device_unuse_default_domain() - Device driver stops handling device
3128 * DMA through the kernel DMA API.
3131 * The device driver doesn't want to do DMA through kernel DMA API anymore.
3132 * It must be called after iommu_device_use_default_domain().
3134 void iommu_device_unuse_default_domain(struct device *dev)
3136 struct iommu_group *group = iommu_group_get(dev);
3141 mutex_lock(&group->mutex);
3142 if (!WARN_ON(!group->owner_cnt))
3145 mutex_unlock(&group->mutex);
3146 iommu_group_put(group);
3149 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
3151 struct group_device *dev =
3152 list_first_entry(&group->devices, struct group_device, list);
3154 if (group->blocking_domain)
3157 group->blocking_domain =
3158 __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED);
3159 if (!group->blocking_domain) {
3161 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
3162 * create an empty domain instead.
3164 group->blocking_domain = __iommu_domain_alloc(
3165 dev->dev->bus, IOMMU_DOMAIN_UNMANAGED);
3166 if (!group->blocking_domain)
3173 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3174 * @group: The group.
3175 * @owner: Caller specified pointer. Used for exclusive ownership.
3177 * This is to support backward compatibility for vfio which manages
3178 * the dma ownership in iommu_group level. New invocations on this
3179 * interface should be prohibited.
3181 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
3185 mutex_lock(&group->mutex);
3186 if (group->owner_cnt) {
3190 if (group->domain && group->domain != group->default_domain) {
3195 ret = __iommu_group_alloc_blocking_domain(group);
3199 ret = __iommu_group_set_domain(group, group->blocking_domain);
3202 group->owner = owner;
3207 mutex_unlock(&group->mutex);
3211 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
3214 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3215 * @group: The group.
3217 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
3219 void iommu_group_release_dma_owner(struct iommu_group *group)
3223 mutex_lock(&group->mutex);
3224 if (WARN_ON(!group->owner_cnt || !group->owner))
3227 group->owner_cnt = 0;
3228 group->owner = NULL;
3229 ret = __iommu_group_set_domain(group, group->default_domain);
3230 WARN(ret, "iommu driver failed to attach the default domain");
3233 mutex_unlock(&group->mutex);
3235 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
3238 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3239 * @group: The group.
3241 * This provides status query on a given group. It is racy and only for
3242 * non-binding status reporting.
3244 bool iommu_group_dma_owner_claimed(struct iommu_group *group)
3248 mutex_lock(&group->mutex);
3249 user = group->owner_cnt;
3250 mutex_unlock(&group->mutex);
3254 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);