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/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>
28 static struct kset *iommu_group_kset;
29 static DEFINE_IDA(iommu_group_ida);
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;
37 struct kobject *devices_kobj;
38 struct list_head devices;
40 struct blocking_notifier_head notifier;
42 void (*iommu_data_release)(void *iommu_data);
45 struct iommu_domain *default_domain;
46 struct iommu_domain *domain;
47 struct list_head entry;
51 struct list_head list;
56 struct iommu_group_attribute {
57 struct attribute attr;
58 ssize_t (*show)(struct iommu_group *group, char *buf);
59 ssize_t (*store)(struct iommu_group *group,
60 const char *buf, size_t count);
63 static const char * const iommu_group_resv_type_string[] = {
64 [IOMMU_RESV_DIRECT] = "direct",
65 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
66 [IOMMU_RESV_RESERVED] = "reserved",
67 [IOMMU_RESV_MSI] = "msi",
68 [IOMMU_RESV_SW_MSI] = "msi",
71 #define IOMMU_CMD_LINE_DMA_API BIT(0)
72 #define IOMMU_CMD_LINE_STRICT BIT(1)
74 static int iommu_alloc_default_domain(struct iommu_group *group,
76 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
78 static int __iommu_attach_device(struct iommu_domain *domain,
80 static int __iommu_attach_group(struct iommu_domain *domain,
81 struct iommu_group *group);
82 static void __iommu_detach_group(struct iommu_domain *domain,
83 struct iommu_group *group);
84 static int iommu_create_device_direct_mappings(struct iommu_group *group,
86 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
87 static ssize_t iommu_group_store_type(struct iommu_group *group,
88 const char *buf, size_t count);
90 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
91 struct iommu_group_attribute iommu_group_attr_##_name = \
92 __ATTR(_name, _mode, _show, _store)
94 #define to_iommu_group_attr(_attr) \
95 container_of(_attr, struct iommu_group_attribute, attr)
96 #define to_iommu_group(_kobj) \
97 container_of(_kobj, struct iommu_group, kobj)
99 static LIST_HEAD(iommu_device_list);
100 static DEFINE_SPINLOCK(iommu_device_lock);
103 * Use a function instead of an array here because the domain-type is a
104 * bit-field, so an array would waste memory.
106 static const char *iommu_domain_type_str(unsigned int t)
109 case IOMMU_DOMAIN_BLOCKED:
111 case IOMMU_DOMAIN_IDENTITY:
112 return "Passthrough";
113 case IOMMU_DOMAIN_UNMANAGED:
115 case IOMMU_DOMAIN_DMA:
122 static int __init iommu_subsys_init(void)
124 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
125 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
126 iommu_set_default_passthrough(false);
128 iommu_set_default_translated(false);
130 if (iommu_default_passthrough() && mem_encrypt_active()) {
131 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
132 iommu_set_default_translated(false);
136 pr_info("Default domain type: %s %s\n",
137 iommu_domain_type_str(iommu_def_domain_type),
138 (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
139 "(set via kernel command line)" : "");
143 subsys_initcall(iommu_subsys_init);
146 * iommu_device_register() - Register an IOMMU hardware instance
147 * @iommu: IOMMU handle for the instance
148 * @ops: IOMMU ops to associate with the instance
149 * @hwdev: (optional) actual instance device, used for fwnode lookup
151 * Return: 0 on success, or an error.
153 int iommu_device_register(struct iommu_device *iommu,
154 const struct iommu_ops *ops, struct device *hwdev)
156 /* We need to be able to take module references appropriately */
157 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
162 iommu->fwnode = hwdev->fwnode;
164 spin_lock(&iommu_device_lock);
165 list_add_tail(&iommu->list, &iommu_device_list);
166 spin_unlock(&iommu_device_lock);
169 EXPORT_SYMBOL_GPL(iommu_device_register);
171 void iommu_device_unregister(struct iommu_device *iommu)
173 spin_lock(&iommu_device_lock);
174 list_del(&iommu->list);
175 spin_unlock(&iommu_device_lock);
177 EXPORT_SYMBOL_GPL(iommu_device_unregister);
179 static struct dev_iommu *dev_iommu_get(struct device *dev)
181 struct dev_iommu *param = dev->iommu;
186 param = kzalloc(sizeof(*param), GFP_KERNEL);
190 mutex_init(¶m->lock);
195 static void dev_iommu_free(struct device *dev)
197 iommu_fwspec_free(dev);
202 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
204 const struct iommu_ops *ops = dev->bus->iommu_ops;
205 struct iommu_device *iommu_dev;
206 struct iommu_group *group;
212 if (!dev_iommu_get(dev))
215 if (!try_module_get(ops->owner)) {
220 iommu_dev = ops->probe_device(dev);
221 if (IS_ERR(iommu_dev)) {
222 ret = PTR_ERR(iommu_dev);
226 dev->iommu->iommu_dev = iommu_dev;
228 group = iommu_group_get_for_dev(dev);
230 ret = PTR_ERR(group);
233 iommu_group_put(group);
235 if (group_list && !group->default_domain && list_empty(&group->entry))
236 list_add_tail(&group->entry, group_list);
238 iommu_device_link(iommu_dev, dev);
243 ops->release_device(dev);
246 module_put(ops->owner);
254 int iommu_probe_device(struct device *dev)
256 const struct iommu_ops *ops = dev->bus->iommu_ops;
257 struct iommu_group *group;
260 ret = __iommu_probe_device(dev, NULL);
264 group = iommu_group_get(dev);
271 * Try to allocate a default domain - needs support from the
272 * IOMMU driver. There are still some drivers which don't
273 * support default domains, so the return value is not yet
276 iommu_alloc_default_domain(group, dev);
278 if (group->default_domain) {
279 ret = __iommu_attach_device(group->default_domain, dev);
281 iommu_group_put(group);
286 iommu_create_device_direct_mappings(group, dev);
288 iommu_group_put(group);
290 if (ops->probe_finalize)
291 ops->probe_finalize(dev);
296 iommu_release_device(dev);
303 void iommu_release_device(struct device *dev)
305 const struct iommu_ops *ops = dev->bus->iommu_ops;
310 iommu_device_unlink(dev->iommu->iommu_dev, dev);
312 ops->release_device(dev);
314 iommu_group_remove_device(dev);
315 module_put(ops->owner);
319 static int __init iommu_set_def_domain_type(char *str)
324 ret = kstrtobool(str, &pt);
329 iommu_set_default_passthrough(true);
331 iommu_set_default_translated(true);
335 early_param("iommu.passthrough", iommu_set_def_domain_type);
337 static int __init iommu_dma_setup(char *str)
339 int ret = kstrtobool(str, &iommu_dma_strict);
342 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
345 early_param("iommu.strict", iommu_dma_setup);
347 void iommu_set_dma_strict(bool strict)
349 if (strict || !(iommu_cmd_line & IOMMU_CMD_LINE_STRICT))
350 iommu_dma_strict = strict;
353 bool iommu_get_dma_strict(struct iommu_domain *domain)
355 /* only allow lazy flushing for DMA domains */
356 if (domain->type == IOMMU_DOMAIN_DMA)
357 return iommu_dma_strict;
360 EXPORT_SYMBOL_GPL(iommu_get_dma_strict);
362 static ssize_t iommu_group_attr_show(struct kobject *kobj,
363 struct attribute *__attr, char *buf)
365 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
366 struct iommu_group *group = to_iommu_group(kobj);
370 ret = attr->show(group, buf);
374 static ssize_t iommu_group_attr_store(struct kobject *kobj,
375 struct attribute *__attr,
376 const char *buf, size_t count)
378 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
379 struct iommu_group *group = to_iommu_group(kobj);
383 ret = attr->store(group, buf, count);
387 static const struct sysfs_ops iommu_group_sysfs_ops = {
388 .show = iommu_group_attr_show,
389 .store = iommu_group_attr_store,
392 static int iommu_group_create_file(struct iommu_group *group,
393 struct iommu_group_attribute *attr)
395 return sysfs_create_file(&group->kobj, &attr->attr);
398 static void iommu_group_remove_file(struct iommu_group *group,
399 struct iommu_group_attribute *attr)
401 sysfs_remove_file(&group->kobj, &attr->attr);
404 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
406 return sprintf(buf, "%s\n", group->name);
410 * iommu_insert_resv_region - Insert a new region in the
411 * list of reserved regions.
412 * @new: new region to insert
413 * @regions: list of regions
415 * Elements are sorted by start address and overlapping segments
416 * of the same type are merged.
418 static int iommu_insert_resv_region(struct iommu_resv_region *new,
419 struct list_head *regions)
421 struct iommu_resv_region *iter, *tmp, *nr, *top;
424 nr = iommu_alloc_resv_region(new->start, new->length,
425 new->prot, new->type);
429 /* First add the new element based on start address sorting */
430 list_for_each_entry(iter, regions, list) {
431 if (nr->start < iter->start ||
432 (nr->start == iter->start && nr->type <= iter->type))
435 list_add_tail(&nr->list, &iter->list);
437 /* Merge overlapping segments of type nr->type in @regions, if any */
438 list_for_each_entry_safe(iter, tmp, regions, list) {
439 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
441 /* no merge needed on elements of different types than @new */
442 if (iter->type != new->type) {
443 list_move_tail(&iter->list, &stack);
447 /* look for the last stack element of same type as @iter */
448 list_for_each_entry_reverse(top, &stack, list)
449 if (top->type == iter->type)
452 list_move_tail(&iter->list, &stack);
456 top_end = top->start + top->length - 1;
458 if (iter->start > top_end + 1) {
459 list_move_tail(&iter->list, &stack);
461 top->length = max(top_end, iter_end) - top->start + 1;
462 list_del(&iter->list);
466 list_splice(&stack, regions);
471 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
472 struct list_head *group_resv_regions)
474 struct iommu_resv_region *entry;
477 list_for_each_entry(entry, dev_resv_regions, list) {
478 ret = iommu_insert_resv_region(entry, group_resv_regions);
485 int iommu_get_group_resv_regions(struct iommu_group *group,
486 struct list_head *head)
488 struct group_device *device;
491 mutex_lock(&group->mutex);
492 list_for_each_entry(device, &group->devices, list) {
493 struct list_head dev_resv_regions;
495 INIT_LIST_HEAD(&dev_resv_regions);
496 iommu_get_resv_regions(device->dev, &dev_resv_regions);
497 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
498 iommu_put_resv_regions(device->dev, &dev_resv_regions);
502 mutex_unlock(&group->mutex);
505 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
507 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
510 struct iommu_resv_region *region, *next;
511 struct list_head group_resv_regions;
514 INIT_LIST_HEAD(&group_resv_regions);
515 iommu_get_group_resv_regions(group, &group_resv_regions);
517 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
518 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
519 (long long int)region->start,
520 (long long int)(region->start +
522 iommu_group_resv_type_string[region->type]);
529 static ssize_t iommu_group_show_type(struct iommu_group *group,
532 char *type = "unknown\n";
534 mutex_lock(&group->mutex);
535 if (group->default_domain) {
536 switch (group->default_domain->type) {
537 case IOMMU_DOMAIN_BLOCKED:
540 case IOMMU_DOMAIN_IDENTITY:
543 case IOMMU_DOMAIN_UNMANAGED:
544 type = "unmanaged\n";
546 case IOMMU_DOMAIN_DMA:
551 mutex_unlock(&group->mutex);
557 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
559 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
560 iommu_group_show_resv_regions, NULL);
562 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
563 iommu_group_store_type);
565 static void iommu_group_release(struct kobject *kobj)
567 struct iommu_group *group = to_iommu_group(kobj);
569 pr_debug("Releasing group %d\n", group->id);
571 if (group->iommu_data_release)
572 group->iommu_data_release(group->iommu_data);
574 ida_simple_remove(&iommu_group_ida, group->id);
576 if (group->default_domain)
577 iommu_domain_free(group->default_domain);
583 static struct kobj_type iommu_group_ktype = {
584 .sysfs_ops = &iommu_group_sysfs_ops,
585 .release = iommu_group_release,
589 * iommu_group_alloc - Allocate a new group
591 * This function is called by an iommu driver to allocate a new iommu
592 * group. The iommu group represents the minimum granularity of the iommu.
593 * Upon successful return, the caller holds a reference to the supplied
594 * group in order to hold the group until devices are added. Use
595 * iommu_group_put() to release this extra reference count, allowing the
596 * group to be automatically reclaimed once it has no devices or external
599 struct iommu_group *iommu_group_alloc(void)
601 struct iommu_group *group;
604 group = kzalloc(sizeof(*group), GFP_KERNEL);
606 return ERR_PTR(-ENOMEM);
608 group->kobj.kset = iommu_group_kset;
609 mutex_init(&group->mutex);
610 INIT_LIST_HEAD(&group->devices);
611 INIT_LIST_HEAD(&group->entry);
612 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
614 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
621 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
622 NULL, "%d", group->id);
624 ida_simple_remove(&iommu_group_ida, group->id);
625 kobject_put(&group->kobj);
629 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
630 if (!group->devices_kobj) {
631 kobject_put(&group->kobj); /* triggers .release & free */
632 return ERR_PTR(-ENOMEM);
636 * The devices_kobj holds a reference on the group kobject, so
637 * as long as that exists so will the group. We can therefore
638 * use the devices_kobj for reference counting.
640 kobject_put(&group->kobj);
642 ret = iommu_group_create_file(group,
643 &iommu_group_attr_reserved_regions);
647 ret = iommu_group_create_file(group, &iommu_group_attr_type);
651 pr_debug("Allocated group %d\n", group->id);
655 EXPORT_SYMBOL_GPL(iommu_group_alloc);
657 struct iommu_group *iommu_group_get_by_id(int id)
659 struct kobject *group_kobj;
660 struct iommu_group *group;
663 if (!iommu_group_kset)
666 name = kasprintf(GFP_KERNEL, "%d", id);
670 group_kobj = kset_find_obj(iommu_group_kset, name);
676 group = container_of(group_kobj, struct iommu_group, kobj);
677 BUG_ON(group->id != id);
679 kobject_get(group->devices_kobj);
680 kobject_put(&group->kobj);
684 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
687 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
690 * iommu drivers can store data in the group for use when doing iommu
691 * operations. This function provides a way to retrieve it. Caller
692 * should hold a group reference.
694 void *iommu_group_get_iommudata(struct iommu_group *group)
696 return group->iommu_data;
698 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
701 * iommu_group_set_iommudata - set iommu_data for a group
703 * @iommu_data: new data
704 * @release: release function for iommu_data
706 * iommu drivers can store data in the group for use when doing iommu
707 * operations. This function provides a way to set the data after
708 * the group has been allocated. Caller should hold a group reference.
710 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
711 void (*release)(void *iommu_data))
713 group->iommu_data = iommu_data;
714 group->iommu_data_release = release;
716 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
719 * iommu_group_set_name - set name for a group
723 * Allow iommu driver to set a name for a group. When set it will
724 * appear in a name attribute file under the group in sysfs.
726 int iommu_group_set_name(struct iommu_group *group, const char *name)
731 iommu_group_remove_file(group, &iommu_group_attr_name);
738 group->name = kstrdup(name, GFP_KERNEL);
742 ret = iommu_group_create_file(group, &iommu_group_attr_name);
751 EXPORT_SYMBOL_GPL(iommu_group_set_name);
753 static int iommu_create_device_direct_mappings(struct iommu_group *group,
756 struct iommu_domain *domain = group->default_domain;
757 struct iommu_resv_region *entry;
758 struct list_head mappings;
759 unsigned long pg_size;
762 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
765 BUG_ON(!domain->pgsize_bitmap);
767 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
768 INIT_LIST_HEAD(&mappings);
770 iommu_get_resv_regions(dev, &mappings);
772 /* We need to consider overlapping regions for different devices */
773 list_for_each_entry(entry, &mappings, list) {
774 dma_addr_t start, end, addr;
777 if (domain->ops->apply_resv_region)
778 domain->ops->apply_resv_region(dev, domain, entry);
780 start = ALIGN(entry->start, pg_size);
781 end = ALIGN(entry->start + entry->length, pg_size);
783 if (entry->type != IOMMU_RESV_DIRECT &&
784 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
787 for (addr = start; addr <= end; addr += pg_size) {
788 phys_addr_t phys_addr;
793 phys_addr = iommu_iova_to_phys(domain, addr);
801 ret = iommu_map(domain, addr - map_size,
802 addr - map_size, map_size,
812 iommu_flush_iotlb_all(domain);
815 iommu_put_resv_regions(dev, &mappings);
820 static bool iommu_is_attach_deferred(struct iommu_domain *domain,
823 if (domain->ops->is_attach_deferred)
824 return domain->ops->is_attach_deferred(domain, dev);
830 * iommu_group_add_device - add a device to an iommu group
831 * @group: the group into which to add the device (reference should be held)
834 * This function is called by an iommu driver to add a device into a
835 * group. Adding a device increments the group reference count.
837 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
840 struct group_device *device;
842 device = kzalloc(sizeof(*device), GFP_KERNEL);
848 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
850 goto err_free_device;
852 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
856 goto err_remove_link;
859 ret = sysfs_create_link_nowarn(group->devices_kobj,
860 &dev->kobj, device->name);
862 if (ret == -EEXIST && i >= 0) {
864 * Account for the slim chance of collision
865 * and append an instance to the name.
868 device->name = kasprintf(GFP_KERNEL, "%s.%d",
869 kobject_name(&dev->kobj), i++);
875 kobject_get(group->devices_kobj);
877 dev->iommu_group = group;
879 mutex_lock(&group->mutex);
880 list_add_tail(&device->list, &group->devices);
881 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
882 ret = __iommu_attach_device(group->domain, dev);
883 mutex_unlock(&group->mutex);
887 /* Notify any listeners about change to group. */
888 blocking_notifier_call_chain(&group->notifier,
889 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
891 trace_add_device_to_group(group->id, dev);
893 dev_info(dev, "Adding to iommu group %d\n", group->id);
898 mutex_lock(&group->mutex);
899 list_del(&device->list);
900 mutex_unlock(&group->mutex);
901 dev->iommu_group = NULL;
902 kobject_put(group->devices_kobj);
903 sysfs_remove_link(group->devices_kobj, device->name);
907 sysfs_remove_link(&dev->kobj, "iommu_group");
910 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
913 EXPORT_SYMBOL_GPL(iommu_group_add_device);
916 * iommu_group_remove_device - remove a device from it's current group
917 * @dev: device to be removed
919 * This function is called by an iommu driver to remove the device from
920 * it's current group. This decrements the iommu group reference count.
922 void iommu_group_remove_device(struct device *dev)
924 struct iommu_group *group = dev->iommu_group;
925 struct group_device *tmp_device, *device = NULL;
930 dev_info(dev, "Removing from iommu group %d\n", group->id);
932 /* Pre-notify listeners that a device is being removed. */
933 blocking_notifier_call_chain(&group->notifier,
934 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
936 mutex_lock(&group->mutex);
937 list_for_each_entry(tmp_device, &group->devices, list) {
938 if (tmp_device->dev == dev) {
940 list_del(&device->list);
944 mutex_unlock(&group->mutex);
949 sysfs_remove_link(group->devices_kobj, device->name);
950 sysfs_remove_link(&dev->kobj, "iommu_group");
952 trace_remove_device_from_group(group->id, dev);
956 dev->iommu_group = NULL;
957 kobject_put(group->devices_kobj);
959 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
961 static int iommu_group_device_count(struct iommu_group *group)
963 struct group_device *entry;
966 list_for_each_entry(entry, &group->devices, list)
973 * iommu_group_for_each_dev - iterate over each device in the group
975 * @data: caller opaque data to be passed to callback function
976 * @fn: caller supplied callback function
978 * This function is called by group users to iterate over group devices.
979 * Callers should hold a reference count to the group during callback.
980 * The group->mutex is held across callbacks, which will block calls to
981 * iommu_group_add/remove_device.
983 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
984 int (*fn)(struct device *, void *))
986 struct group_device *device;
989 list_for_each_entry(device, &group->devices, list) {
990 ret = fn(device->dev, data);
998 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
999 int (*fn)(struct device *, void *))
1003 mutex_lock(&group->mutex);
1004 ret = __iommu_group_for_each_dev(group, data, fn);
1005 mutex_unlock(&group->mutex);
1009 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1012 * iommu_group_get - Return the group for a device and increment reference
1013 * @dev: get the group that this device belongs to
1015 * This function is called by iommu drivers and users to get the group
1016 * for the specified device. If found, the group is returned and the group
1017 * reference in incremented, else NULL.
1019 struct iommu_group *iommu_group_get(struct device *dev)
1021 struct iommu_group *group = dev->iommu_group;
1024 kobject_get(group->devices_kobj);
1028 EXPORT_SYMBOL_GPL(iommu_group_get);
1031 * iommu_group_ref_get - Increment reference on a group
1032 * @group: the group to use, must not be NULL
1034 * This function is called by iommu drivers to take additional references on an
1035 * existing group. Returns the given group for convenience.
1037 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1039 kobject_get(group->devices_kobj);
1042 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1045 * iommu_group_put - Decrement group reference
1046 * @group: the group to use
1048 * This function is called by iommu drivers and users to release the
1049 * iommu group. Once the reference count is zero, the group is released.
1051 void iommu_group_put(struct iommu_group *group)
1054 kobject_put(group->devices_kobj);
1056 EXPORT_SYMBOL_GPL(iommu_group_put);
1059 * iommu_group_register_notifier - Register a notifier for group changes
1060 * @group: the group to watch
1061 * @nb: notifier block to signal
1063 * This function allows iommu group users to track changes in a group.
1064 * See include/linux/iommu.h for actions sent via this notifier. Caller
1065 * should hold a reference to the group throughout notifier registration.
1067 int iommu_group_register_notifier(struct iommu_group *group,
1068 struct notifier_block *nb)
1070 return blocking_notifier_chain_register(&group->notifier, nb);
1072 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1075 * iommu_group_unregister_notifier - Unregister a notifier
1076 * @group: the group to watch
1077 * @nb: notifier block to signal
1079 * Unregister a previously registered group notifier block.
1081 int iommu_group_unregister_notifier(struct iommu_group *group,
1082 struct notifier_block *nb)
1084 return blocking_notifier_chain_unregister(&group->notifier, nb);
1086 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1089 * iommu_register_device_fault_handler() - Register a device fault handler
1091 * @handler: the fault handler
1092 * @data: private data passed as argument to the handler
1094 * When an IOMMU fault event is received, this handler gets called with the
1095 * fault event and data as argument. The handler should return 0 on success. If
1096 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1097 * complete the fault by calling iommu_page_response() with one of the following
1099 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1100 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1101 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1102 * page faults if possible.
1104 * Return 0 if the fault handler was installed successfully, or an error.
1106 int iommu_register_device_fault_handler(struct device *dev,
1107 iommu_dev_fault_handler_t handler,
1110 struct dev_iommu *param = dev->iommu;
1116 mutex_lock(¶m->lock);
1117 /* Only allow one fault handler registered for each device */
1118 if (param->fault_param) {
1124 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1125 if (!param->fault_param) {
1130 param->fault_param->handler = handler;
1131 param->fault_param->data = data;
1132 mutex_init(¶m->fault_param->lock);
1133 INIT_LIST_HEAD(¶m->fault_param->faults);
1136 mutex_unlock(¶m->lock);
1140 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1143 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1146 * Remove the device fault handler installed with
1147 * iommu_register_device_fault_handler().
1149 * Return 0 on success, or an error.
1151 int iommu_unregister_device_fault_handler(struct device *dev)
1153 struct dev_iommu *param = dev->iommu;
1159 mutex_lock(¶m->lock);
1161 if (!param->fault_param)
1164 /* we cannot unregister handler if there are pending faults */
1165 if (!list_empty(¶m->fault_param->faults)) {
1170 kfree(param->fault_param);
1171 param->fault_param = NULL;
1174 mutex_unlock(¶m->lock);
1178 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1181 * iommu_report_device_fault() - Report fault event to device driver
1183 * @evt: fault event data
1185 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1186 * handler. When this function fails and the fault is recoverable, it is the
1187 * caller's responsibility to complete the fault.
1189 * Return 0 on success, or an error.
1191 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1193 struct dev_iommu *param = dev->iommu;
1194 struct iommu_fault_event *evt_pending = NULL;
1195 struct iommu_fault_param *fparam;
1201 /* we only report device fault if there is a handler registered */
1202 mutex_lock(¶m->lock);
1203 fparam = param->fault_param;
1204 if (!fparam || !fparam->handler) {
1209 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1210 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1211 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1217 mutex_lock(&fparam->lock);
1218 list_add_tail(&evt_pending->list, &fparam->faults);
1219 mutex_unlock(&fparam->lock);
1222 ret = fparam->handler(&evt->fault, fparam->data);
1223 if (ret && evt_pending) {
1224 mutex_lock(&fparam->lock);
1225 list_del(&evt_pending->list);
1226 mutex_unlock(&fparam->lock);
1230 mutex_unlock(¶m->lock);
1233 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1235 int iommu_page_response(struct device *dev,
1236 struct iommu_page_response *msg)
1240 struct iommu_fault_event *evt;
1241 struct iommu_fault_page_request *prm;
1242 struct dev_iommu *param = dev->iommu;
1243 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1244 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1246 if (!domain || !domain->ops->page_response)
1249 if (!param || !param->fault_param)
1252 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1253 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1256 /* Only send response if there is a fault report pending */
1257 mutex_lock(¶m->fault_param->lock);
1258 if (list_empty(¶m->fault_param->faults)) {
1259 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1263 * Check if we have a matching page request pending to respond,
1264 * otherwise return -EINVAL
1266 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1267 prm = &evt->fault.prm;
1268 if (prm->grpid != msg->grpid)
1272 * If the PASID is required, the corresponding request is
1273 * matched using the group ID, the PASID valid bit and the PASID
1274 * value. Otherwise only the group ID matches request and
1277 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1278 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1281 if (!needs_pasid && has_pasid) {
1282 /* No big deal, just clear it. */
1283 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1287 ret = domain->ops->page_response(dev, evt, msg);
1288 list_del(&evt->list);
1294 mutex_unlock(¶m->fault_param->lock);
1297 EXPORT_SYMBOL_GPL(iommu_page_response);
1300 * iommu_group_id - Return ID for a group
1301 * @group: the group to ID
1303 * Return the unique ID for the group matching the sysfs group number.
1305 int iommu_group_id(struct iommu_group *group)
1309 EXPORT_SYMBOL_GPL(iommu_group_id);
1311 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1312 unsigned long *devfns);
1315 * To consider a PCI device isolated, we require ACS to support Source
1316 * Validation, Request Redirection, Completer Redirection, and Upstream
1317 * Forwarding. This effectively means that devices cannot spoof their
1318 * requester ID, requests and completions cannot be redirected, and all
1319 * transactions are forwarded upstream, even as it passes through a
1320 * bridge where the target device is downstream.
1322 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1325 * For multifunction devices which are not isolated from each other, find
1326 * all the other non-isolated functions and look for existing groups. For
1327 * each function, we also need to look for aliases to or from other devices
1328 * that may already have a group.
1330 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1331 unsigned long *devfns)
1333 struct pci_dev *tmp = NULL;
1334 struct iommu_group *group;
1336 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1339 for_each_pci_dev(tmp) {
1340 if (tmp == pdev || tmp->bus != pdev->bus ||
1341 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1342 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1345 group = get_pci_alias_group(tmp, devfns);
1356 * Look for aliases to or from the given device for existing groups. DMA
1357 * aliases are only supported on the same bus, therefore the search
1358 * space is quite small (especially since we're really only looking at pcie
1359 * device, and therefore only expect multiple slots on the root complex or
1360 * downstream switch ports). It's conceivable though that a pair of
1361 * multifunction devices could have aliases between them that would cause a
1362 * loop. To prevent this, we use a bitmap to track where we've been.
1364 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1365 unsigned long *devfns)
1367 struct pci_dev *tmp = NULL;
1368 struct iommu_group *group;
1370 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1373 group = iommu_group_get(&pdev->dev);
1377 for_each_pci_dev(tmp) {
1378 if (tmp == pdev || tmp->bus != pdev->bus)
1381 /* We alias them or they alias us */
1382 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1383 group = get_pci_alias_group(tmp, devfns);
1389 group = get_pci_function_alias_group(tmp, devfns);
1400 struct group_for_pci_data {
1401 struct pci_dev *pdev;
1402 struct iommu_group *group;
1406 * DMA alias iterator callback, return the last seen device. Stop and return
1407 * the IOMMU group if we find one along the way.
1409 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1411 struct group_for_pci_data *data = opaque;
1414 data->group = iommu_group_get(&pdev->dev);
1416 return data->group != NULL;
1420 * Generic device_group call-back function. It just allocates one
1421 * iommu-group per device.
1423 struct iommu_group *generic_device_group(struct device *dev)
1425 return iommu_group_alloc();
1427 EXPORT_SYMBOL_GPL(generic_device_group);
1430 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1431 * to find or create an IOMMU group for a device.
1433 struct iommu_group *pci_device_group(struct device *dev)
1435 struct pci_dev *pdev = to_pci_dev(dev);
1436 struct group_for_pci_data data;
1437 struct pci_bus *bus;
1438 struct iommu_group *group = NULL;
1439 u64 devfns[4] = { 0 };
1441 if (WARN_ON(!dev_is_pci(dev)))
1442 return ERR_PTR(-EINVAL);
1445 * Find the upstream DMA alias for the device. A device must not
1446 * be aliased due to topology in order to have its own IOMMU group.
1447 * If we find an alias along the way that already belongs to a
1450 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1456 * Continue upstream from the point of minimum IOMMU granularity
1457 * due to aliases to the point where devices are protected from
1458 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1461 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1465 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1470 group = iommu_group_get(&pdev->dev);
1476 * Look for existing groups on device aliases. If we alias another
1477 * device or another device aliases us, use the same group.
1479 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1484 * Look for existing groups on non-isolated functions on the same
1485 * slot and aliases of those funcions, if any. No need to clear
1486 * the search bitmap, the tested devfns are still valid.
1488 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1492 /* No shared group found, allocate new */
1493 return iommu_group_alloc();
1495 EXPORT_SYMBOL_GPL(pci_device_group);
1497 /* Get the IOMMU group for device on fsl-mc bus */
1498 struct iommu_group *fsl_mc_device_group(struct device *dev)
1500 struct device *cont_dev = fsl_mc_cont_dev(dev);
1501 struct iommu_group *group;
1503 group = iommu_group_get(cont_dev);
1505 group = iommu_group_alloc();
1508 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1510 static int iommu_get_def_domain_type(struct device *dev)
1512 const struct iommu_ops *ops = dev->bus->iommu_ops;
1514 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
1515 return IOMMU_DOMAIN_DMA;
1517 if (ops->def_domain_type)
1518 return ops->def_domain_type(dev);
1523 static int iommu_group_alloc_default_domain(struct bus_type *bus,
1524 struct iommu_group *group,
1527 struct iommu_domain *dom;
1529 dom = __iommu_domain_alloc(bus, type);
1530 if (!dom && type != IOMMU_DOMAIN_DMA) {
1531 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1533 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1540 group->default_domain = dom;
1542 group->domain = dom;
1546 static int iommu_alloc_default_domain(struct iommu_group *group,
1551 if (group->default_domain)
1554 type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
1556 return iommu_group_alloc_default_domain(dev->bus, group, type);
1560 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1561 * @dev: target device
1563 * This function is intended to be called by IOMMU drivers and extended to
1564 * support common, bus-defined algorithms when determining or creating the
1565 * IOMMU group for a device. On success, the caller will hold a reference
1566 * to the returned IOMMU group, which will already include the provided
1567 * device. The reference should be released with iommu_group_put().
1569 static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1571 const struct iommu_ops *ops = dev->bus->iommu_ops;
1572 struct iommu_group *group;
1575 group = iommu_group_get(dev);
1580 return ERR_PTR(-EINVAL);
1582 group = ops->device_group(dev);
1583 if (WARN_ON_ONCE(group == NULL))
1584 return ERR_PTR(-EINVAL);
1589 ret = iommu_group_add_device(group, dev);
1596 iommu_group_put(group);
1598 return ERR_PTR(ret);
1601 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1603 return group->default_domain;
1606 static int probe_iommu_group(struct device *dev, void *data)
1608 struct list_head *group_list = data;
1609 struct iommu_group *group;
1612 /* Device is probed already if in a group */
1613 group = iommu_group_get(dev);
1615 iommu_group_put(group);
1619 ret = __iommu_probe_device(dev, group_list);
1626 static int remove_iommu_group(struct device *dev, void *data)
1628 iommu_release_device(dev);
1633 static int iommu_bus_notifier(struct notifier_block *nb,
1634 unsigned long action, void *data)
1636 unsigned long group_action = 0;
1637 struct device *dev = data;
1638 struct iommu_group *group;
1641 * ADD/DEL call into iommu driver ops if provided, which may
1642 * result in ADD/DEL notifiers to group->notifier
1644 if (action == BUS_NOTIFY_ADD_DEVICE) {
1647 ret = iommu_probe_device(dev);
1648 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1649 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1650 iommu_release_device(dev);
1655 * Remaining BUS_NOTIFYs get filtered and republished to the
1656 * group, if anyone is listening
1658 group = iommu_group_get(dev);
1663 case BUS_NOTIFY_BIND_DRIVER:
1664 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1666 case BUS_NOTIFY_BOUND_DRIVER:
1667 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1669 case BUS_NOTIFY_UNBIND_DRIVER:
1670 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1672 case BUS_NOTIFY_UNBOUND_DRIVER:
1673 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1678 blocking_notifier_call_chain(&group->notifier,
1681 iommu_group_put(group);
1685 struct __group_domain_type {
1690 static int probe_get_default_domain_type(struct device *dev, void *data)
1692 struct __group_domain_type *gtype = data;
1693 unsigned int type = iommu_get_def_domain_type(dev);
1696 if (gtype->type && gtype->type != type) {
1697 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1698 iommu_domain_type_str(type),
1699 dev_name(gtype->dev),
1700 iommu_domain_type_str(gtype->type));
1713 static void probe_alloc_default_domain(struct bus_type *bus,
1714 struct iommu_group *group)
1716 struct __group_domain_type gtype;
1718 memset(>ype, 0, sizeof(gtype));
1720 /* Ask for default domain requirements of all devices in the group */
1721 __iommu_group_for_each_dev(group, >ype,
1722 probe_get_default_domain_type);
1725 gtype.type = iommu_def_domain_type;
1727 iommu_group_alloc_default_domain(bus, group, gtype.type);
1731 static int iommu_group_do_dma_attach(struct device *dev, void *data)
1733 struct iommu_domain *domain = data;
1736 if (!iommu_is_attach_deferred(domain, dev))
1737 ret = __iommu_attach_device(domain, dev);
1742 static int __iommu_group_dma_attach(struct iommu_group *group)
1744 return __iommu_group_for_each_dev(group, group->default_domain,
1745 iommu_group_do_dma_attach);
1748 static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1750 struct iommu_domain *domain = data;
1752 if (domain->ops->probe_finalize)
1753 domain->ops->probe_finalize(dev);
1758 static void __iommu_group_dma_finalize(struct iommu_group *group)
1760 __iommu_group_for_each_dev(group, group->default_domain,
1761 iommu_group_do_probe_finalize);
1764 static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1766 struct iommu_group *group = data;
1768 iommu_create_device_direct_mappings(group, dev);
1773 static int iommu_group_create_direct_mappings(struct iommu_group *group)
1775 return __iommu_group_for_each_dev(group, group,
1776 iommu_do_create_direct_mappings);
1779 int bus_iommu_probe(struct bus_type *bus)
1781 struct iommu_group *group, *next;
1782 LIST_HEAD(group_list);
1786 * This code-path does not allocate the default domain when
1787 * creating the iommu group, so do it after the groups are
1790 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1794 list_for_each_entry_safe(group, next, &group_list, entry) {
1795 /* Remove item from the list */
1796 list_del_init(&group->entry);
1798 mutex_lock(&group->mutex);
1800 /* Try to allocate default domain */
1801 probe_alloc_default_domain(bus, group);
1803 if (!group->default_domain) {
1804 mutex_unlock(&group->mutex);
1808 iommu_group_create_direct_mappings(group);
1810 ret = __iommu_group_dma_attach(group);
1812 mutex_unlock(&group->mutex);
1817 __iommu_group_dma_finalize(group);
1823 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1825 struct notifier_block *nb;
1828 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1832 nb->notifier_call = iommu_bus_notifier;
1834 err = bus_register_notifier(bus, nb);
1838 err = bus_iommu_probe(bus);
1847 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1848 bus_unregister_notifier(bus, nb);
1857 * bus_set_iommu - set iommu-callbacks for the bus
1859 * @ops: the callbacks provided by the iommu-driver
1861 * This function is called by an iommu driver to set the iommu methods
1862 * used for a particular bus. Drivers for devices on that bus can use
1863 * the iommu-api after these ops are registered.
1864 * This special function is needed because IOMMUs are usually devices on
1865 * the bus itself, so the iommu drivers are not initialized when the bus
1866 * is set up. With this function the iommu-driver can set the iommu-ops
1869 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1874 bus->iommu_ops = NULL;
1878 if (bus->iommu_ops != NULL)
1881 bus->iommu_ops = ops;
1883 /* Do IOMMU specific setup for this bus-type */
1884 err = iommu_bus_init(bus, ops);
1886 bus->iommu_ops = NULL;
1890 EXPORT_SYMBOL_GPL(bus_set_iommu);
1892 bool iommu_present(struct bus_type *bus)
1894 return bus->iommu_ops != NULL;
1896 EXPORT_SYMBOL_GPL(iommu_present);
1898 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1900 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1903 return bus->iommu_ops->capable(cap);
1905 EXPORT_SYMBOL_GPL(iommu_capable);
1908 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1909 * @domain: iommu domain
1910 * @handler: fault handler
1911 * @token: user data, will be passed back to the fault handler
1913 * This function should be used by IOMMU users which want to be notified
1914 * whenever an IOMMU fault happens.
1916 * The fault handler itself should return 0 on success, and an appropriate
1917 * error code otherwise.
1919 void iommu_set_fault_handler(struct iommu_domain *domain,
1920 iommu_fault_handler_t handler,
1925 domain->handler = handler;
1926 domain->handler_token = token;
1928 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1930 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1933 struct iommu_domain *domain;
1935 if (bus == NULL || bus->iommu_ops == NULL)
1938 domain = bus->iommu_ops->domain_alloc(type);
1942 domain->ops = bus->iommu_ops;
1943 domain->type = type;
1944 /* Assume all sizes by default; the driver may override this later */
1945 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1950 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1952 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1954 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1956 void iommu_domain_free(struct iommu_domain *domain)
1958 domain->ops->domain_free(domain);
1960 EXPORT_SYMBOL_GPL(iommu_domain_free);
1962 static int __iommu_attach_device(struct iommu_domain *domain,
1967 if (unlikely(domain->ops->attach_dev == NULL))
1970 ret = domain->ops->attach_dev(domain, dev);
1972 trace_attach_device_to_domain(dev);
1976 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1978 struct iommu_group *group;
1981 group = iommu_group_get(dev);
1986 * Lock the group to make sure the device-count doesn't
1987 * change while we are attaching
1989 mutex_lock(&group->mutex);
1991 if (iommu_group_device_count(group) != 1)
1994 ret = __iommu_attach_group(domain, group);
1997 mutex_unlock(&group->mutex);
1998 iommu_group_put(group);
2002 EXPORT_SYMBOL_GPL(iommu_attach_device);
2004 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2006 const struct iommu_ops *ops = domain->ops;
2008 if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
2009 return __iommu_attach_device(domain, dev);
2015 * Check flags and other user provided data for valid combinations. We also
2016 * make sure no reserved fields or unused flags are set. This is to ensure
2017 * not breaking userspace in the future when these fields or flags are used.
2019 static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)
2024 if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)
2027 mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;
2028 if (info->cache & ~mask)
2031 if (info->granularity >= IOMMU_INV_GRANU_NR)
2034 switch (info->granularity) {
2035 case IOMMU_INV_GRANU_ADDR:
2036 if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)
2039 mask = IOMMU_INV_ADDR_FLAGS_PASID |
2040 IOMMU_INV_ADDR_FLAGS_ARCHID |
2041 IOMMU_INV_ADDR_FLAGS_LEAF;
2043 if (info->granu.addr_info.flags & ~mask)
2046 case IOMMU_INV_GRANU_PASID:
2047 mask = IOMMU_INV_PASID_FLAGS_PASID |
2048 IOMMU_INV_PASID_FLAGS_ARCHID;
2049 if (info->granu.pasid_info.flags & ~mask)
2053 case IOMMU_INV_GRANU_DOMAIN:
2054 if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)
2061 /* Check reserved padding fields */
2062 for (i = 0; i < sizeof(info->padding); i++) {
2063 if (info->padding[i])
2070 int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,
2073 struct iommu_cache_invalidate_info inv_info = { 0 };
2077 if (unlikely(!domain->ops->cache_invalidate))
2081 * No new spaces can be added before the variable sized union, the
2082 * minimum size is the offset to the union.
2084 minsz = offsetof(struct iommu_cache_invalidate_info, granu);
2086 /* Copy minsz from user to get flags and argsz */
2087 if (copy_from_user(&inv_info, uinfo, minsz))
2090 /* Fields before the variable size union are mandatory */
2091 if (inv_info.argsz < minsz)
2094 /* PASID and address granu require additional info beyond minsz */
2095 if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&
2096 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))
2099 if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&
2100 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))
2104 * User might be using a newer UAPI header which has a larger data
2105 * size, we shall support the existing flags within the current
2106 * size. Copy the remaining user data _after_ minsz but not more
2107 * than the current kernel supported size.
2109 if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,
2110 min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))
2113 /* Now the argsz is validated, check the content */
2114 ret = iommu_check_cache_invl_data(&inv_info);
2118 return domain->ops->cache_invalidate(domain, dev, &inv_info);
2120 EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);
2122 static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)
2127 if (data->version != IOMMU_GPASID_BIND_VERSION_1)
2130 /* Check the range of supported formats */
2131 if (data->format >= IOMMU_PASID_FORMAT_LAST)
2134 /* Check all flags */
2135 mask = IOMMU_SVA_GPASID_VAL;
2136 if (data->flags & ~mask)
2139 /* Check reserved padding fields */
2140 for (i = 0; i < sizeof(data->padding); i++) {
2141 if (data->padding[i])
2148 static int iommu_sva_prepare_bind_data(void __user *udata,
2149 struct iommu_gpasid_bind_data *data)
2154 * No new spaces can be added before the variable sized union, the
2155 * minimum size is the offset to the union.
2157 minsz = offsetof(struct iommu_gpasid_bind_data, vendor);
2159 /* Copy minsz from user to get flags and argsz */
2160 if (copy_from_user(data, udata, minsz))
2163 /* Fields before the variable size union are mandatory */
2164 if (data->argsz < minsz)
2167 * User might be using a newer UAPI header, we shall let IOMMU vendor
2168 * driver decide on what size it needs. Since the guest PASID bind data
2169 * can be vendor specific, larger argsz could be the result of extension
2170 * for one vendor but it should not affect another vendor.
2171 * Copy the remaining user data _after_ minsz
2173 if (copy_from_user((void *)data + minsz, udata + minsz,
2174 min_t(u32, data->argsz, sizeof(*data)) - minsz))
2177 return iommu_check_bind_data(data);
2180 int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
2183 struct iommu_gpasid_bind_data data = { 0 };
2186 if (unlikely(!domain->ops->sva_bind_gpasid))
2189 ret = iommu_sva_prepare_bind_data(udata, &data);
2193 return domain->ops->sva_bind_gpasid(domain, dev, &data);
2195 EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
2197 int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2200 if (unlikely(!domain->ops->sva_unbind_gpasid))
2203 return domain->ops->sva_unbind_gpasid(dev, pasid);
2205 EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
2207 int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2210 struct iommu_gpasid_bind_data data = { 0 };
2213 if (unlikely(!domain->ops->sva_bind_gpasid))
2216 ret = iommu_sva_prepare_bind_data(udata, &data);
2220 return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
2222 EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
2224 static void __iommu_detach_device(struct iommu_domain *domain,
2227 if (iommu_is_attach_deferred(domain, dev))
2230 if (unlikely(domain->ops->detach_dev == NULL))
2233 domain->ops->detach_dev(domain, dev);
2234 trace_detach_device_from_domain(dev);
2237 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2239 struct iommu_group *group;
2241 group = iommu_group_get(dev);
2245 mutex_lock(&group->mutex);
2246 if (iommu_group_device_count(group) != 1) {
2251 __iommu_detach_group(domain, group);
2254 mutex_unlock(&group->mutex);
2255 iommu_group_put(group);
2257 EXPORT_SYMBOL_GPL(iommu_detach_device);
2259 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2261 struct iommu_domain *domain;
2262 struct iommu_group *group;
2264 group = iommu_group_get(dev);
2268 domain = group->domain;
2270 iommu_group_put(group);
2274 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2277 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2278 * guarantees that the group and its default domain are valid and correct.
2280 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2282 return dev->iommu_group->default_domain;
2286 * IOMMU groups are really the natural working unit of the IOMMU, but
2287 * the IOMMU API works on domains and devices. Bridge that gap by
2288 * iterating over the devices in a group. Ideally we'd have a single
2289 * device which represents the requestor ID of the group, but we also
2290 * allow IOMMU drivers to create policy defined minimum sets, where
2291 * the physical hardware may be able to distiguish members, but we
2292 * wish to group them at a higher level (ex. untrusted multi-function
2293 * PCI devices). Thus we attach each device.
2295 static int iommu_group_do_attach_device(struct device *dev, void *data)
2297 struct iommu_domain *domain = data;
2299 return __iommu_attach_device(domain, dev);
2302 static int __iommu_attach_group(struct iommu_domain *domain,
2303 struct iommu_group *group)
2307 if (group->default_domain && group->domain != group->default_domain)
2310 ret = __iommu_group_for_each_dev(group, domain,
2311 iommu_group_do_attach_device);
2313 group->domain = domain;
2318 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2322 mutex_lock(&group->mutex);
2323 ret = __iommu_attach_group(domain, group);
2324 mutex_unlock(&group->mutex);
2328 EXPORT_SYMBOL_GPL(iommu_attach_group);
2330 static int iommu_group_do_detach_device(struct device *dev, void *data)
2332 struct iommu_domain *domain = data;
2334 __iommu_detach_device(domain, dev);
2339 static void __iommu_detach_group(struct iommu_domain *domain,
2340 struct iommu_group *group)
2344 if (!group->default_domain) {
2345 __iommu_group_for_each_dev(group, domain,
2346 iommu_group_do_detach_device);
2347 group->domain = NULL;
2351 if (group->domain == group->default_domain)
2354 /* Detach by re-attaching to the default domain */
2355 ret = __iommu_group_for_each_dev(group, group->default_domain,
2356 iommu_group_do_attach_device);
2360 group->domain = group->default_domain;
2363 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2365 mutex_lock(&group->mutex);
2366 __iommu_detach_group(domain, group);
2367 mutex_unlock(&group->mutex);
2369 EXPORT_SYMBOL_GPL(iommu_detach_group);
2371 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2373 if (unlikely(domain->ops->iova_to_phys == NULL))
2376 return domain->ops->iova_to_phys(domain, iova);
2378 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2380 static size_t iommu_pgsize(struct iommu_domain *domain,
2381 unsigned long addr_merge, size_t size)
2383 unsigned int pgsize_idx;
2386 /* Max page size that still fits into 'size' */
2387 pgsize_idx = __fls(size);
2389 /* need to consider alignment requirements ? */
2390 if (likely(addr_merge)) {
2391 /* Max page size allowed by address */
2392 unsigned int align_pgsize_idx = __ffs(addr_merge);
2393 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2396 /* build a mask of acceptable page sizes */
2397 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2399 /* throw away page sizes not supported by the hardware */
2400 pgsize &= domain->pgsize_bitmap;
2402 /* make sure we're still sane */
2405 /* pick the biggest page */
2406 pgsize_idx = __fls(pgsize);
2407 pgsize = 1UL << pgsize_idx;
2412 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2413 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2415 const struct iommu_ops *ops = domain->ops;
2416 unsigned long orig_iova = iova;
2417 unsigned int min_pagesz;
2418 size_t orig_size = size;
2419 phys_addr_t orig_paddr = paddr;
2422 if (unlikely(ops->map == NULL ||
2423 domain->pgsize_bitmap == 0UL))
2426 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2429 /* find out the minimum page size supported */
2430 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2433 * both the virtual address and the physical one, as well as
2434 * the size of the mapping, must be aligned (at least) to the
2435 * size of the smallest page supported by the hardware
2437 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2438 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2439 iova, &paddr, size, min_pagesz);
2443 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2446 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
2448 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
2449 iova, &paddr, pgsize);
2450 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2460 /* unroll mapping in case something went wrong */
2462 iommu_unmap(domain, orig_iova, orig_size - size);
2464 trace_map(orig_iova, orig_paddr, orig_size);
2469 static int _iommu_map(struct iommu_domain *domain, unsigned long iova,
2470 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2472 const struct iommu_ops *ops = domain->ops;
2475 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2476 if (ret == 0 && ops->iotlb_sync_map)
2477 ops->iotlb_sync_map(domain, iova, size);
2482 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2483 phys_addr_t paddr, size_t size, int prot)
2486 return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2488 EXPORT_SYMBOL_GPL(iommu_map);
2490 int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2491 phys_addr_t paddr, size_t size, int prot)
2493 return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2495 EXPORT_SYMBOL_GPL(iommu_map_atomic);
2497 static size_t __iommu_unmap(struct iommu_domain *domain,
2498 unsigned long iova, size_t size,
2499 struct iommu_iotlb_gather *iotlb_gather)
2501 const struct iommu_ops *ops = domain->ops;
2502 size_t unmapped_page, unmapped = 0;
2503 unsigned long orig_iova = iova;
2504 unsigned int min_pagesz;
2506 if (unlikely(ops->unmap == NULL ||
2507 domain->pgsize_bitmap == 0UL))
2510 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2513 /* find out the minimum page size supported */
2514 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2517 * The virtual address, as well as the size of the mapping, must be
2518 * aligned (at least) to the size of the smallest page supported
2521 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2522 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2523 iova, size, min_pagesz);
2527 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2530 * Keep iterating until we either unmap 'size' bytes (or more)
2531 * or we hit an area that isn't mapped.
2533 while (unmapped < size) {
2534 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
2536 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
2540 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2541 iova, unmapped_page);
2543 iova += unmapped_page;
2544 unmapped += unmapped_page;
2547 trace_unmap(orig_iova, size, unmapped);
2551 size_t iommu_unmap(struct iommu_domain *domain,
2552 unsigned long iova, size_t size)
2554 struct iommu_iotlb_gather iotlb_gather;
2557 iommu_iotlb_gather_init(&iotlb_gather);
2558 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2559 iommu_iotlb_sync(domain, &iotlb_gather);
2563 EXPORT_SYMBOL_GPL(iommu_unmap);
2565 size_t iommu_unmap_fast(struct iommu_domain *domain,
2566 unsigned long iova, size_t size,
2567 struct iommu_iotlb_gather *iotlb_gather)
2569 return __iommu_unmap(domain, iova, size, iotlb_gather);
2571 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2573 static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2574 struct scatterlist *sg, unsigned int nents, int prot,
2577 const struct iommu_ops *ops = domain->ops;
2578 size_t len = 0, mapped = 0;
2583 while (i <= nents) {
2584 phys_addr_t s_phys = sg_phys(sg);
2586 if (len && s_phys != start + len) {
2587 ret = __iommu_map(domain, iova + mapped, start,
2608 if (ops->iotlb_sync_map)
2609 ops->iotlb_sync_map(domain, iova, mapped);
2613 /* undo mappings already done */
2614 iommu_unmap(domain, iova, mapped);
2620 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2621 struct scatterlist *sg, unsigned int nents, int prot)
2624 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2626 EXPORT_SYMBOL_GPL(iommu_map_sg);
2628 size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2629 struct scatterlist *sg, unsigned int nents, int prot)
2631 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2635 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2636 * @domain: the iommu domain where the fault has happened
2637 * @dev: the device where the fault has happened
2638 * @iova: the faulting address
2639 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2641 * This function should be called by the low-level IOMMU implementations
2642 * whenever IOMMU faults happen, to allow high-level users, that are
2643 * interested in such events, to know about them.
2645 * This event may be useful for several possible use cases:
2646 * - mere logging of the event
2647 * - dynamic TLB/PTE loading
2648 * - if restarting of the faulting device is required
2650 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2651 * PTE/TLB loading will one day be supported, implementations will be able
2652 * to tell whether it succeeded or not according to this return value).
2654 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2655 * (though fault handlers can also return -ENOSYS, in case they want to
2656 * elicit the default behavior of the IOMMU drivers).
2658 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2659 unsigned long iova, int flags)
2664 * if upper layers showed interest and installed a fault handler,
2667 if (domain->handler)
2668 ret = domain->handler(domain, dev, iova, flags,
2669 domain->handler_token);
2671 trace_io_page_fault(dev, iova, flags);
2674 EXPORT_SYMBOL_GPL(report_iommu_fault);
2676 static int __init iommu_init(void)
2678 iommu_group_kset = kset_create_and_add("iommu_groups",
2680 BUG_ON(!iommu_group_kset);
2682 iommu_debugfs_setup();
2686 core_initcall(iommu_init);
2688 int iommu_enable_nesting(struct iommu_domain *domain)
2690 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2692 if (!domain->ops->enable_nesting)
2694 return domain->ops->enable_nesting(domain);
2696 EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2698 int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2699 unsigned long quirk)
2701 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2703 if (!domain->ops->set_pgtable_quirks)
2705 return domain->ops->set_pgtable_quirks(domain, quirk);
2707 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2709 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2711 const struct iommu_ops *ops = dev->bus->iommu_ops;
2713 if (ops && ops->get_resv_regions)
2714 ops->get_resv_regions(dev, list);
2717 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2719 const struct iommu_ops *ops = dev->bus->iommu_ops;
2721 if (ops && ops->put_resv_regions)
2722 ops->put_resv_regions(dev, list);
2726 * generic_iommu_put_resv_regions - Reserved region driver helper
2727 * @dev: device for which to free reserved regions
2728 * @list: reserved region list for device
2730 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2731 * for simple reservations. Memory allocated for each reserved region will be
2732 * freed. If an IOMMU driver allocates additional resources per region, it is
2733 * going to have to implement a custom callback.
2735 void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2737 struct iommu_resv_region *entry, *next;
2739 list_for_each_entry_safe(entry, next, list, list)
2742 EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2744 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2745 size_t length, int prot,
2746 enum iommu_resv_type type)
2748 struct iommu_resv_region *region;
2750 region = kzalloc(sizeof(*region), GFP_KERNEL);
2754 INIT_LIST_HEAD(®ion->list);
2755 region->start = start;
2756 region->length = length;
2757 region->prot = prot;
2758 region->type = type;
2761 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2763 void iommu_set_default_passthrough(bool cmd_line)
2766 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2767 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2770 void iommu_set_default_translated(bool cmd_line)
2773 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2774 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2777 bool iommu_default_passthrough(void)
2779 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2781 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2783 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2785 const struct iommu_ops *ops = NULL;
2786 struct iommu_device *iommu;
2788 spin_lock(&iommu_device_lock);
2789 list_for_each_entry(iommu, &iommu_device_list, list)
2790 if (iommu->fwnode == fwnode) {
2794 spin_unlock(&iommu_device_lock);
2798 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2799 const struct iommu_ops *ops)
2801 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2804 return ops == fwspec->ops ? 0 : -EINVAL;
2806 if (!dev_iommu_get(dev))
2809 /* Preallocate for the overwhelmingly common case of 1 ID */
2810 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2814 of_node_get(to_of_node(iommu_fwnode));
2815 fwspec->iommu_fwnode = iommu_fwnode;
2817 dev_iommu_fwspec_set(dev, fwspec);
2820 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2822 void iommu_fwspec_free(struct device *dev)
2824 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2827 fwnode_handle_put(fwspec->iommu_fwnode);
2829 dev_iommu_fwspec_set(dev, NULL);
2832 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2834 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2836 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2842 new_num = fwspec->num_ids + num_ids;
2844 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2849 dev_iommu_fwspec_set(dev, fwspec);
2852 for (i = 0; i < num_ids; i++)
2853 fwspec->ids[fwspec->num_ids + i] = ids[i];
2855 fwspec->num_ids = new_num;
2858 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2861 * Per device IOMMU features.
2863 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2865 if (dev->iommu && dev->iommu->iommu_dev) {
2866 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2868 if (ops->dev_enable_feat)
2869 return ops->dev_enable_feat(dev, feat);
2874 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2877 * The device drivers should do the necessary cleanups before calling this.
2878 * For example, before disabling the aux-domain feature, the device driver
2879 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2881 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2883 if (dev->iommu && dev->iommu->iommu_dev) {
2884 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2886 if (ops->dev_disable_feat)
2887 return ops->dev_disable_feat(dev, feat);
2892 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2894 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2896 if (dev->iommu && dev->iommu->iommu_dev) {
2897 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2899 if (ops->dev_feat_enabled)
2900 return ops->dev_feat_enabled(dev, feat);
2905 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2908 * Aux-domain specific attach/detach.
2910 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2911 * true. Also, as long as domains are attached to a device through this
2912 * interface, any tries to call iommu_attach_device() should fail
2913 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2914 * This should make us safe against a device being attached to a guest as a
2915 * whole while there are still pasid users on it (aux and sva).
2917 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2921 if (domain->ops->aux_attach_dev)
2922 ret = domain->ops->aux_attach_dev(domain, dev);
2925 trace_attach_device_to_domain(dev);
2929 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2931 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2933 if (domain->ops->aux_detach_dev) {
2934 domain->ops->aux_detach_dev(domain, dev);
2935 trace_detach_device_from_domain(dev);
2938 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2940 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2944 if (domain->ops->aux_get_pasid)
2945 ret = domain->ops->aux_get_pasid(domain, dev);
2949 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2952 * iommu_sva_bind_device() - Bind a process address space to a device
2954 * @mm: the mm to bind, caller must hold a reference to it
2956 * Create a bond between device and address space, allowing the device to access
2957 * the mm using the returned PASID. If a bond already exists between @device and
2958 * @mm, it is returned and an additional reference is taken. Caller must call
2959 * iommu_sva_unbind_device() to release each reference.
2961 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2962 * initialize the required SVA features.
2964 * On error, returns an ERR_PTR value.
2967 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2969 struct iommu_group *group;
2970 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2971 const struct iommu_ops *ops = dev->bus->iommu_ops;
2973 if (!ops || !ops->sva_bind)
2974 return ERR_PTR(-ENODEV);
2976 group = iommu_group_get(dev);
2978 return ERR_PTR(-ENODEV);
2980 /* Ensure device count and domain don't change while we're binding */
2981 mutex_lock(&group->mutex);
2984 * To keep things simple, SVA currently doesn't support IOMMU groups
2985 * with more than one device. Existing SVA-capable systems are not
2986 * affected by the problems that required IOMMU groups (lack of ACS
2987 * isolation, device ID aliasing and other hardware issues).
2989 if (iommu_group_device_count(group) != 1)
2992 handle = ops->sva_bind(dev, mm, drvdata);
2995 mutex_unlock(&group->mutex);
2996 iommu_group_put(group);
3000 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
3003 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
3004 * @handle: the handle returned by iommu_sva_bind_device()
3006 * Put reference to a bond between device and address space. The device should
3007 * not be issuing any more transaction for this PASID. All outstanding page
3008 * requests for this PASID must have been flushed to the IOMMU.
3010 void iommu_sva_unbind_device(struct iommu_sva *handle)
3012 struct iommu_group *group;
3013 struct device *dev = handle->dev;
3014 const struct iommu_ops *ops = dev->bus->iommu_ops;
3016 if (!ops || !ops->sva_unbind)
3019 group = iommu_group_get(dev);
3023 mutex_lock(&group->mutex);
3024 ops->sva_unbind(handle);
3025 mutex_unlock(&group->mutex);
3027 iommu_group_put(group);
3029 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
3031 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
3033 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
3035 if (!ops || !ops->sva_get_pasid)
3036 return IOMMU_PASID_INVALID;
3038 return ops->sva_get_pasid(handle);
3040 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
3043 * Changes the default domain of an iommu group that has *only* one device
3045 * @group: The group for which the default domain should be changed
3046 * @prev_dev: The device in the group (this is used to make sure that the device
3047 * hasn't changed after the caller has called this function)
3048 * @type: The type of the new default domain that gets associated with the group
3050 * Returns 0 on success and error code on failure
3053 * 1. Presently, this function is called only when user requests to change the
3054 * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
3055 * Please take a closer look if intended to use for other purposes.
3057 static int iommu_change_dev_def_domain(struct iommu_group *group,
3058 struct device *prev_dev, int type)
3060 struct iommu_domain *prev_dom;
3061 struct group_device *grp_dev;
3062 int ret, dev_def_dom;
3065 mutex_lock(&group->mutex);
3067 if (group->default_domain != group->domain) {
3068 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n");
3074 * iommu group wasn't locked while acquiring device lock in
3075 * iommu_group_store_type(). So, make sure that the device count hasn't
3076 * changed while acquiring device lock.
3078 * Changing default domain of an iommu group with two or more devices
3079 * isn't supported because there could be a potential deadlock. Consider
3080 * the following scenario. T1 is trying to acquire device locks of all
3081 * the devices in the group and before it could acquire all of them,
3082 * there could be another thread T2 (from different sub-system and use
3083 * case) that has already acquired some of the device locks and might be
3084 * waiting for T1 to release other device locks.
3086 if (iommu_group_device_count(group) != 1) {
3087 dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n");
3092 /* Since group has only one device */
3093 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3096 if (prev_dev != dev) {
3097 dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n");
3102 prev_dom = group->default_domain;
3108 dev_def_dom = iommu_get_def_domain_type(dev);
3111 * If the user hasn't requested any specific type of domain and
3112 * if the device supports both the domains, then default to the
3113 * domain the device was booted with
3115 type = dev_def_dom ? : iommu_def_domain_type;
3116 } else if (dev_def_dom && type != dev_def_dom) {
3117 dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
3118 iommu_domain_type_str(type));
3124 * Switch to a new domain only if the requested domain type is different
3125 * from the existing default domain type
3127 if (prev_dom->type == type) {
3132 /* Sets group->default_domain to the newly allocated domain */
3133 ret = iommu_group_alloc_default_domain(dev->bus, group, type);
3137 ret = iommu_create_device_direct_mappings(group, dev);
3139 goto free_new_domain;
3141 ret = __iommu_attach_device(group->default_domain, dev);
3143 goto free_new_domain;
3145 group->domain = group->default_domain;
3148 * Release the mutex here because ops->probe_finalize() call-back of
3149 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3150 * in-turn might call back into IOMMU core code, where it tries to take
3151 * group->mutex, resulting in a deadlock.
3153 mutex_unlock(&group->mutex);
3155 /* Make sure dma_ops is appropriatley set */
3156 iommu_group_do_probe_finalize(dev, group->default_domain);
3157 iommu_domain_free(prev_dom);
3161 iommu_domain_free(group->default_domain);
3162 group->default_domain = prev_dom;
3163 group->domain = prev_dom;
3166 mutex_unlock(&group->mutex);
3172 * Changing the default domain through sysfs requires the users to ubind the
3173 * drivers from the devices in the iommu group. Return failure if this doesn't
3176 * We need to consider the race between this and the device release path.
3177 * device_lock(dev) is used here to guarantee that the device release path
3178 * will not be entered at the same time.
3180 static ssize_t iommu_group_store_type(struct iommu_group *group,
3181 const char *buf, size_t count)
3183 struct group_device *grp_dev;
3187 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3190 if (WARN_ON(!group))
3193 if (sysfs_streq(buf, "identity"))
3194 req_type = IOMMU_DOMAIN_IDENTITY;
3195 else if (sysfs_streq(buf, "DMA"))
3196 req_type = IOMMU_DOMAIN_DMA;
3197 else if (sysfs_streq(buf, "auto"))
3203 * Lock/Unlock the group mutex here before device lock to
3204 * 1. Make sure that the iommu group has only one device (this is a
3205 * prerequisite for step 2)
3206 * 2. Get struct *dev which is needed to lock device
3208 mutex_lock(&group->mutex);
3209 if (iommu_group_device_count(group) != 1) {
3210 mutex_unlock(&group->mutex);
3211 pr_err_ratelimited("Cannot change default domain: Group has more than one device\n");
3215 /* Since group has only one device */
3216 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3221 * Don't hold the group mutex because taking group mutex first and then
3222 * the device lock could potentially cause a deadlock as below. Assume
3223 * two threads T1 and T2. T1 is trying to change default domain of an
3224 * iommu group and T2 is trying to hot unplug a device or release [1] VF
3225 * of a PCIe device which is in the same iommu group. T1 takes group
3226 * mutex and before it could take device lock assume T2 has taken device
3227 * lock and is yet to take group mutex. Now, both the threads will be
3228 * waiting for the other thread to release lock. Below, lock order was
3231 * mutex_lock(&group->mutex);
3232 * iommu_change_dev_def_domain();
3233 * mutex_unlock(&group->mutex);
3234 * device_unlock(dev);
3236 * [1] Typical device release path
3237 * device_lock() from device/driver core code
3239 * -> iommu_bus_notifier()
3240 * -> iommu_release_device()
3241 * -> ops->release_device() vendor driver calls back iommu core code
3242 * -> mutex_lock() from iommu core code
3244 mutex_unlock(&group->mutex);
3246 /* Check if the device in the group still has a driver bound to it */
3248 if (device_is_bound(dev)) {
3249 pr_err_ratelimited("Device is still bound to driver\n");
3254 ret = iommu_change_dev_def_domain(group, dev, req_type);