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)
73 static void iommu_set_cmd_line_dma_api(void)
75 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
78 static bool iommu_cmd_line_dma_api(void)
80 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
83 static int iommu_alloc_default_domain(struct iommu_group *group,
85 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
87 static int __iommu_attach_device(struct iommu_domain *domain,
89 static int __iommu_attach_group(struct iommu_domain *domain,
90 struct iommu_group *group);
91 static void __iommu_detach_group(struct iommu_domain *domain,
92 struct iommu_group *group);
93 static int iommu_create_device_direct_mappings(struct iommu_group *group,
95 static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
97 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
98 struct iommu_group_attribute iommu_group_attr_##_name = \
99 __ATTR(_name, _mode, _show, _store)
101 #define to_iommu_group_attr(_attr) \
102 container_of(_attr, struct iommu_group_attribute, attr)
103 #define to_iommu_group(_kobj) \
104 container_of(_kobj, struct iommu_group, kobj)
106 static LIST_HEAD(iommu_device_list);
107 static DEFINE_SPINLOCK(iommu_device_lock);
110 * Use a function instead of an array here because the domain-type is a
111 * bit-field, so an array would waste memory.
113 static const char *iommu_domain_type_str(unsigned int t)
116 case IOMMU_DOMAIN_BLOCKED:
118 case IOMMU_DOMAIN_IDENTITY:
119 return "Passthrough";
120 case IOMMU_DOMAIN_UNMANAGED:
122 case IOMMU_DOMAIN_DMA:
129 static int __init iommu_subsys_init(void)
131 bool cmd_line = iommu_cmd_line_dma_api();
134 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
135 iommu_set_default_passthrough(false);
137 iommu_set_default_translated(false);
139 if (iommu_default_passthrough() && mem_encrypt_active()) {
140 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
141 iommu_set_default_translated(false);
145 pr_info("Default domain type: %s %s\n",
146 iommu_domain_type_str(iommu_def_domain_type),
147 cmd_line ? "(set via kernel command line)" : "");
151 subsys_initcall(iommu_subsys_init);
153 int iommu_device_register(struct iommu_device *iommu)
155 spin_lock(&iommu_device_lock);
156 list_add_tail(&iommu->list, &iommu_device_list);
157 spin_unlock(&iommu_device_lock);
160 EXPORT_SYMBOL_GPL(iommu_device_register);
162 void iommu_device_unregister(struct iommu_device *iommu)
164 spin_lock(&iommu_device_lock);
165 list_del(&iommu->list);
166 spin_unlock(&iommu_device_lock);
168 EXPORT_SYMBOL_GPL(iommu_device_unregister);
170 static struct dev_iommu *dev_iommu_get(struct device *dev)
172 struct dev_iommu *param = dev->iommu;
177 param = kzalloc(sizeof(*param), GFP_KERNEL);
181 mutex_init(¶m->lock);
186 static void dev_iommu_free(struct device *dev)
188 iommu_fwspec_free(dev);
193 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
195 const struct iommu_ops *ops = dev->bus->iommu_ops;
196 struct iommu_device *iommu_dev;
197 struct iommu_group *group;
203 if (!dev_iommu_get(dev))
206 if (!try_module_get(ops->owner)) {
211 iommu_dev = ops->probe_device(dev);
212 if (IS_ERR(iommu_dev)) {
213 ret = PTR_ERR(iommu_dev);
217 dev->iommu->iommu_dev = iommu_dev;
219 group = iommu_group_get_for_dev(dev);
221 ret = PTR_ERR(group);
224 iommu_group_put(group);
226 if (group_list && !group->default_domain && list_empty(&group->entry))
227 list_add_tail(&group->entry, group_list);
229 iommu_device_link(iommu_dev, dev);
234 ops->release_device(dev);
237 module_put(ops->owner);
245 int iommu_probe_device(struct device *dev)
247 const struct iommu_ops *ops = dev->bus->iommu_ops;
248 struct iommu_group *group;
251 ret = __iommu_probe_device(dev, NULL);
255 group = iommu_group_get(dev);
260 * Try to allocate a default domain - needs support from the
261 * IOMMU driver. There are still some drivers which don't
262 * support default domains, so the return value is not yet
265 iommu_alloc_default_domain(group, dev);
267 if (group->default_domain)
268 ret = __iommu_attach_device(group->default_domain, dev);
270 iommu_create_device_direct_mappings(group, dev);
272 iommu_group_put(group);
277 if (ops->probe_finalize)
278 ops->probe_finalize(dev);
283 iommu_release_device(dev);
290 void iommu_release_device(struct device *dev)
292 const struct iommu_ops *ops = dev->bus->iommu_ops;
297 iommu_device_unlink(dev->iommu->iommu_dev, dev);
299 ops->release_device(dev);
301 iommu_group_remove_device(dev);
302 module_put(ops->owner);
306 static int __init iommu_set_def_domain_type(char *str)
311 ret = kstrtobool(str, &pt);
316 iommu_set_default_passthrough(true);
318 iommu_set_default_translated(true);
322 early_param("iommu.passthrough", iommu_set_def_domain_type);
324 static int __init iommu_dma_setup(char *str)
326 return kstrtobool(str, &iommu_dma_strict);
328 early_param("iommu.strict", iommu_dma_setup);
330 static ssize_t iommu_group_attr_show(struct kobject *kobj,
331 struct attribute *__attr, char *buf)
333 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
334 struct iommu_group *group = to_iommu_group(kobj);
338 ret = attr->show(group, buf);
342 static ssize_t iommu_group_attr_store(struct kobject *kobj,
343 struct attribute *__attr,
344 const char *buf, size_t count)
346 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
347 struct iommu_group *group = to_iommu_group(kobj);
351 ret = attr->store(group, buf, count);
355 static const struct sysfs_ops iommu_group_sysfs_ops = {
356 .show = iommu_group_attr_show,
357 .store = iommu_group_attr_store,
360 static int iommu_group_create_file(struct iommu_group *group,
361 struct iommu_group_attribute *attr)
363 return sysfs_create_file(&group->kobj, &attr->attr);
366 static void iommu_group_remove_file(struct iommu_group *group,
367 struct iommu_group_attribute *attr)
369 sysfs_remove_file(&group->kobj, &attr->attr);
372 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
374 return sprintf(buf, "%s\n", group->name);
378 * iommu_insert_resv_region - Insert a new region in the
379 * list of reserved regions.
380 * @new: new region to insert
381 * @regions: list of regions
383 * Elements are sorted by start address and overlapping segments
384 * of the same type are merged.
386 static int iommu_insert_resv_region(struct iommu_resv_region *new,
387 struct list_head *regions)
389 struct iommu_resv_region *iter, *tmp, *nr, *top;
392 nr = iommu_alloc_resv_region(new->start, new->length,
393 new->prot, new->type);
397 /* First add the new element based on start address sorting */
398 list_for_each_entry(iter, regions, list) {
399 if (nr->start < iter->start ||
400 (nr->start == iter->start && nr->type <= iter->type))
403 list_add_tail(&nr->list, &iter->list);
405 /* Merge overlapping segments of type nr->type in @regions, if any */
406 list_for_each_entry_safe(iter, tmp, regions, list) {
407 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
409 /* no merge needed on elements of different types than @new */
410 if (iter->type != new->type) {
411 list_move_tail(&iter->list, &stack);
415 /* look for the last stack element of same type as @iter */
416 list_for_each_entry_reverse(top, &stack, list)
417 if (top->type == iter->type)
420 list_move_tail(&iter->list, &stack);
424 top_end = top->start + top->length - 1;
426 if (iter->start > top_end + 1) {
427 list_move_tail(&iter->list, &stack);
429 top->length = max(top_end, iter_end) - top->start + 1;
430 list_del(&iter->list);
434 list_splice(&stack, regions);
439 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
440 struct list_head *group_resv_regions)
442 struct iommu_resv_region *entry;
445 list_for_each_entry(entry, dev_resv_regions, list) {
446 ret = iommu_insert_resv_region(entry, group_resv_regions);
453 int iommu_get_group_resv_regions(struct iommu_group *group,
454 struct list_head *head)
456 struct group_device *device;
459 mutex_lock(&group->mutex);
460 list_for_each_entry(device, &group->devices, list) {
461 struct list_head dev_resv_regions;
463 INIT_LIST_HEAD(&dev_resv_regions);
464 iommu_get_resv_regions(device->dev, &dev_resv_regions);
465 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
466 iommu_put_resv_regions(device->dev, &dev_resv_regions);
470 mutex_unlock(&group->mutex);
473 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
475 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
478 struct iommu_resv_region *region, *next;
479 struct list_head group_resv_regions;
482 INIT_LIST_HEAD(&group_resv_regions);
483 iommu_get_group_resv_regions(group, &group_resv_regions);
485 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
486 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
487 (long long int)region->start,
488 (long long int)(region->start +
490 iommu_group_resv_type_string[region->type]);
497 static ssize_t iommu_group_show_type(struct iommu_group *group,
500 char *type = "unknown\n";
502 if (group->default_domain) {
503 switch (group->default_domain->type) {
504 case IOMMU_DOMAIN_BLOCKED:
507 case IOMMU_DOMAIN_IDENTITY:
510 case IOMMU_DOMAIN_UNMANAGED:
511 type = "unmanaged\n";
513 case IOMMU_DOMAIN_DMA:
523 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
525 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
526 iommu_group_show_resv_regions, NULL);
528 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
530 static void iommu_group_release(struct kobject *kobj)
532 struct iommu_group *group = to_iommu_group(kobj);
534 pr_debug("Releasing group %d\n", group->id);
536 if (group->iommu_data_release)
537 group->iommu_data_release(group->iommu_data);
539 ida_simple_remove(&iommu_group_ida, group->id);
541 if (group->default_domain)
542 iommu_domain_free(group->default_domain);
548 static struct kobj_type iommu_group_ktype = {
549 .sysfs_ops = &iommu_group_sysfs_ops,
550 .release = iommu_group_release,
554 * iommu_group_alloc - Allocate a new group
556 * This function is called by an iommu driver to allocate a new iommu
557 * group. The iommu group represents the minimum granularity of the iommu.
558 * Upon successful return, the caller holds a reference to the supplied
559 * group in order to hold the group until devices are added. Use
560 * iommu_group_put() to release this extra reference count, allowing the
561 * group to be automatically reclaimed once it has no devices or external
564 struct iommu_group *iommu_group_alloc(void)
566 struct iommu_group *group;
569 group = kzalloc(sizeof(*group), GFP_KERNEL);
571 return ERR_PTR(-ENOMEM);
573 group->kobj.kset = iommu_group_kset;
574 mutex_init(&group->mutex);
575 INIT_LIST_HEAD(&group->devices);
576 INIT_LIST_HEAD(&group->entry);
577 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
579 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
586 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
587 NULL, "%d", group->id);
589 ida_simple_remove(&iommu_group_ida, group->id);
590 kobject_put(&group->kobj);
594 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
595 if (!group->devices_kobj) {
596 kobject_put(&group->kobj); /* triggers .release & free */
597 return ERR_PTR(-ENOMEM);
601 * The devices_kobj holds a reference on the group kobject, so
602 * as long as that exists so will the group. We can therefore
603 * use the devices_kobj for reference counting.
605 kobject_put(&group->kobj);
607 ret = iommu_group_create_file(group,
608 &iommu_group_attr_reserved_regions);
612 ret = iommu_group_create_file(group, &iommu_group_attr_type);
616 pr_debug("Allocated group %d\n", group->id);
620 EXPORT_SYMBOL_GPL(iommu_group_alloc);
622 struct iommu_group *iommu_group_get_by_id(int id)
624 struct kobject *group_kobj;
625 struct iommu_group *group;
628 if (!iommu_group_kset)
631 name = kasprintf(GFP_KERNEL, "%d", id);
635 group_kobj = kset_find_obj(iommu_group_kset, name);
641 group = container_of(group_kobj, struct iommu_group, kobj);
642 BUG_ON(group->id != id);
644 kobject_get(group->devices_kobj);
645 kobject_put(&group->kobj);
649 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
652 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
655 * iommu drivers can store data in the group for use when doing iommu
656 * operations. This function provides a way to retrieve it. Caller
657 * should hold a group reference.
659 void *iommu_group_get_iommudata(struct iommu_group *group)
661 return group->iommu_data;
663 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
666 * iommu_group_set_iommudata - set iommu_data for a group
668 * @iommu_data: new data
669 * @release: release function for iommu_data
671 * iommu drivers can store data in the group for use when doing iommu
672 * operations. This function provides a way to set the data after
673 * the group has been allocated. Caller should hold a group reference.
675 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
676 void (*release)(void *iommu_data))
678 group->iommu_data = iommu_data;
679 group->iommu_data_release = release;
681 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
684 * iommu_group_set_name - set name for a group
688 * Allow iommu driver to set a name for a group. When set it will
689 * appear in a name attribute file under the group in sysfs.
691 int iommu_group_set_name(struct iommu_group *group, const char *name)
696 iommu_group_remove_file(group, &iommu_group_attr_name);
703 group->name = kstrdup(name, GFP_KERNEL);
707 ret = iommu_group_create_file(group, &iommu_group_attr_name);
716 EXPORT_SYMBOL_GPL(iommu_group_set_name);
718 static int iommu_create_device_direct_mappings(struct iommu_group *group,
721 struct iommu_domain *domain = group->default_domain;
722 struct iommu_resv_region *entry;
723 struct list_head mappings;
724 unsigned long pg_size;
727 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
730 BUG_ON(!domain->pgsize_bitmap);
732 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
733 INIT_LIST_HEAD(&mappings);
735 iommu_get_resv_regions(dev, &mappings);
737 /* We need to consider overlapping regions for different devices */
738 list_for_each_entry(entry, &mappings, list) {
739 dma_addr_t start, end, addr;
741 if (domain->ops->apply_resv_region)
742 domain->ops->apply_resv_region(dev, domain, entry);
744 start = ALIGN(entry->start, pg_size);
745 end = ALIGN(entry->start + entry->length, pg_size);
747 if (entry->type != IOMMU_RESV_DIRECT &&
748 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
751 for (addr = start; addr < end; addr += pg_size) {
752 phys_addr_t phys_addr;
754 phys_addr = iommu_iova_to_phys(domain, addr);
758 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
765 iommu_flush_tlb_all(domain);
768 iommu_put_resv_regions(dev, &mappings);
773 static bool iommu_is_attach_deferred(struct iommu_domain *domain,
776 if (domain->ops->is_attach_deferred)
777 return domain->ops->is_attach_deferred(domain, dev);
783 * iommu_group_add_device - add a device to an iommu group
784 * @group: the group into which to add the device (reference should be held)
787 * This function is called by an iommu driver to add a device into a
788 * group. Adding a device increments the group reference count.
790 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
793 struct group_device *device;
795 device = kzalloc(sizeof(*device), GFP_KERNEL);
801 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
803 goto err_free_device;
805 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
809 goto err_remove_link;
812 ret = sysfs_create_link_nowarn(group->devices_kobj,
813 &dev->kobj, device->name);
815 if (ret == -EEXIST && i >= 0) {
817 * Account for the slim chance of collision
818 * and append an instance to the name.
821 device->name = kasprintf(GFP_KERNEL, "%s.%d",
822 kobject_name(&dev->kobj), i++);
828 kobject_get(group->devices_kobj);
830 dev->iommu_group = group;
832 mutex_lock(&group->mutex);
833 list_add_tail(&device->list, &group->devices);
834 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
835 ret = __iommu_attach_device(group->domain, dev);
836 mutex_unlock(&group->mutex);
840 /* Notify any listeners about change to group. */
841 blocking_notifier_call_chain(&group->notifier,
842 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
844 trace_add_device_to_group(group->id, dev);
846 dev_info(dev, "Adding to iommu group %d\n", group->id);
851 mutex_lock(&group->mutex);
852 list_del(&device->list);
853 mutex_unlock(&group->mutex);
854 dev->iommu_group = NULL;
855 kobject_put(group->devices_kobj);
856 sysfs_remove_link(group->devices_kobj, device->name);
860 sysfs_remove_link(&dev->kobj, "iommu_group");
863 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
866 EXPORT_SYMBOL_GPL(iommu_group_add_device);
869 * iommu_group_remove_device - remove a device from it's current group
870 * @dev: device to be removed
872 * This function is called by an iommu driver to remove the device from
873 * it's current group. This decrements the iommu group reference count.
875 void iommu_group_remove_device(struct device *dev)
877 struct iommu_group *group = dev->iommu_group;
878 struct group_device *tmp_device, *device = NULL;
880 dev_info(dev, "Removing from iommu group %d\n", group->id);
882 /* Pre-notify listeners that a device is being removed. */
883 blocking_notifier_call_chain(&group->notifier,
884 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
886 mutex_lock(&group->mutex);
887 list_for_each_entry(tmp_device, &group->devices, list) {
888 if (tmp_device->dev == dev) {
890 list_del(&device->list);
894 mutex_unlock(&group->mutex);
899 sysfs_remove_link(group->devices_kobj, device->name);
900 sysfs_remove_link(&dev->kobj, "iommu_group");
902 trace_remove_device_from_group(group->id, dev);
906 dev->iommu_group = NULL;
907 kobject_put(group->devices_kobj);
909 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
911 static int iommu_group_device_count(struct iommu_group *group)
913 struct group_device *entry;
916 list_for_each_entry(entry, &group->devices, list)
923 * iommu_group_for_each_dev - iterate over each device in the group
925 * @data: caller opaque data to be passed to callback function
926 * @fn: caller supplied callback function
928 * This function is called by group users to iterate over group devices.
929 * Callers should hold a reference count to the group during callback.
930 * The group->mutex is held across callbacks, which will block calls to
931 * iommu_group_add/remove_device.
933 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
934 int (*fn)(struct device *, void *))
936 struct group_device *device;
939 list_for_each_entry(device, &group->devices, list) {
940 ret = fn(device->dev, data);
948 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
949 int (*fn)(struct device *, void *))
953 mutex_lock(&group->mutex);
954 ret = __iommu_group_for_each_dev(group, data, fn);
955 mutex_unlock(&group->mutex);
959 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
962 * iommu_group_get - Return the group for a device and increment reference
963 * @dev: get the group that this device belongs to
965 * This function is called by iommu drivers and users to get the group
966 * for the specified device. If found, the group is returned and the group
967 * reference in incremented, else NULL.
969 struct iommu_group *iommu_group_get(struct device *dev)
971 struct iommu_group *group = dev->iommu_group;
974 kobject_get(group->devices_kobj);
978 EXPORT_SYMBOL_GPL(iommu_group_get);
981 * iommu_group_ref_get - Increment reference on a group
982 * @group: the group to use, must not be NULL
984 * This function is called by iommu drivers to take additional references on an
985 * existing group. Returns the given group for convenience.
987 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
989 kobject_get(group->devices_kobj);
992 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
995 * iommu_group_put - Decrement group reference
996 * @group: the group to use
998 * This function is called by iommu drivers and users to release the
999 * iommu group. Once the reference count is zero, the group is released.
1001 void iommu_group_put(struct iommu_group *group)
1004 kobject_put(group->devices_kobj);
1006 EXPORT_SYMBOL_GPL(iommu_group_put);
1009 * iommu_group_register_notifier - Register a notifier for group changes
1010 * @group: the group to watch
1011 * @nb: notifier block to signal
1013 * This function allows iommu group users to track changes in a group.
1014 * See include/linux/iommu.h for actions sent via this notifier. Caller
1015 * should hold a reference to the group throughout notifier registration.
1017 int iommu_group_register_notifier(struct iommu_group *group,
1018 struct notifier_block *nb)
1020 return blocking_notifier_chain_register(&group->notifier, nb);
1022 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1025 * iommu_group_unregister_notifier - Unregister a notifier
1026 * @group: the group to watch
1027 * @nb: notifier block to signal
1029 * Unregister a previously registered group notifier block.
1031 int iommu_group_unregister_notifier(struct iommu_group *group,
1032 struct notifier_block *nb)
1034 return blocking_notifier_chain_unregister(&group->notifier, nb);
1036 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1039 * iommu_register_device_fault_handler() - Register a device fault handler
1041 * @handler: the fault handler
1042 * @data: private data passed as argument to the handler
1044 * When an IOMMU fault event is received, this handler gets called with the
1045 * fault event and data as argument. The handler should return 0 on success. If
1046 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1047 * complete the fault by calling iommu_page_response() with one of the following
1049 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1050 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1051 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1052 * page faults if possible.
1054 * Return 0 if the fault handler was installed successfully, or an error.
1056 int iommu_register_device_fault_handler(struct device *dev,
1057 iommu_dev_fault_handler_t handler,
1060 struct dev_iommu *param = dev->iommu;
1066 mutex_lock(¶m->lock);
1067 /* Only allow one fault handler registered for each device */
1068 if (param->fault_param) {
1074 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1075 if (!param->fault_param) {
1080 param->fault_param->handler = handler;
1081 param->fault_param->data = data;
1082 mutex_init(¶m->fault_param->lock);
1083 INIT_LIST_HEAD(¶m->fault_param->faults);
1086 mutex_unlock(¶m->lock);
1090 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1093 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1096 * Remove the device fault handler installed with
1097 * iommu_register_device_fault_handler().
1099 * Return 0 on success, or an error.
1101 int iommu_unregister_device_fault_handler(struct device *dev)
1103 struct dev_iommu *param = dev->iommu;
1109 mutex_lock(¶m->lock);
1111 if (!param->fault_param)
1114 /* we cannot unregister handler if there are pending faults */
1115 if (!list_empty(¶m->fault_param->faults)) {
1120 kfree(param->fault_param);
1121 param->fault_param = NULL;
1124 mutex_unlock(¶m->lock);
1128 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1131 * iommu_report_device_fault() - Report fault event to device driver
1133 * @evt: fault event data
1135 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1136 * handler. When this function fails and the fault is recoverable, it is the
1137 * caller's responsibility to complete the fault.
1139 * Return 0 on success, or an error.
1141 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1143 struct dev_iommu *param = dev->iommu;
1144 struct iommu_fault_event *evt_pending = NULL;
1145 struct iommu_fault_param *fparam;
1151 /* we only report device fault if there is a handler registered */
1152 mutex_lock(¶m->lock);
1153 fparam = param->fault_param;
1154 if (!fparam || !fparam->handler) {
1159 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1160 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1161 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1167 mutex_lock(&fparam->lock);
1168 list_add_tail(&evt_pending->list, &fparam->faults);
1169 mutex_unlock(&fparam->lock);
1172 ret = fparam->handler(&evt->fault, fparam->data);
1173 if (ret && evt_pending) {
1174 mutex_lock(&fparam->lock);
1175 list_del(&evt_pending->list);
1176 mutex_unlock(&fparam->lock);
1180 mutex_unlock(¶m->lock);
1183 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1185 int iommu_page_response(struct device *dev,
1186 struct iommu_page_response *msg)
1190 struct iommu_fault_event *evt;
1191 struct iommu_fault_page_request *prm;
1192 struct dev_iommu *param = dev->iommu;
1193 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1194 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1196 if (!domain || !domain->ops->page_response)
1199 if (!param || !param->fault_param)
1202 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1203 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1206 /* Only send response if there is a fault report pending */
1207 mutex_lock(¶m->fault_param->lock);
1208 if (list_empty(¶m->fault_param->faults)) {
1209 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1213 * Check if we have a matching page request pending to respond,
1214 * otherwise return -EINVAL
1216 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1217 prm = &evt->fault.prm;
1218 if (prm->grpid != msg->grpid)
1222 * If the PASID is required, the corresponding request is
1223 * matched using the group ID, the PASID valid bit and the PASID
1224 * value. Otherwise only the group ID matches request and
1227 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1228 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1231 if (!needs_pasid && has_pasid) {
1232 /* No big deal, just clear it. */
1233 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1237 ret = domain->ops->page_response(dev, evt, msg);
1238 list_del(&evt->list);
1244 mutex_unlock(¶m->fault_param->lock);
1247 EXPORT_SYMBOL_GPL(iommu_page_response);
1250 * iommu_group_id - Return ID for a group
1251 * @group: the group to ID
1253 * Return the unique ID for the group matching the sysfs group number.
1255 int iommu_group_id(struct iommu_group *group)
1259 EXPORT_SYMBOL_GPL(iommu_group_id);
1261 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1262 unsigned long *devfns);
1265 * To consider a PCI device isolated, we require ACS to support Source
1266 * Validation, Request Redirection, Completer Redirection, and Upstream
1267 * Forwarding. This effectively means that devices cannot spoof their
1268 * requester ID, requests and completions cannot be redirected, and all
1269 * transactions are forwarded upstream, even as it passes through a
1270 * bridge where the target device is downstream.
1272 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1275 * For multifunction devices which are not isolated from each other, find
1276 * all the other non-isolated functions and look for existing groups. For
1277 * each function, we also need to look for aliases to or from other devices
1278 * that may already have a group.
1280 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1281 unsigned long *devfns)
1283 struct pci_dev *tmp = NULL;
1284 struct iommu_group *group;
1286 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1289 for_each_pci_dev(tmp) {
1290 if (tmp == pdev || tmp->bus != pdev->bus ||
1291 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1292 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1295 group = get_pci_alias_group(tmp, devfns);
1306 * Look for aliases to or from the given device for existing groups. DMA
1307 * aliases are only supported on the same bus, therefore the search
1308 * space is quite small (especially since we're really only looking at pcie
1309 * device, and therefore only expect multiple slots on the root complex or
1310 * downstream switch ports). It's conceivable though that a pair of
1311 * multifunction devices could have aliases between them that would cause a
1312 * loop. To prevent this, we use a bitmap to track where we've been.
1314 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1315 unsigned long *devfns)
1317 struct pci_dev *tmp = NULL;
1318 struct iommu_group *group;
1320 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1323 group = iommu_group_get(&pdev->dev);
1327 for_each_pci_dev(tmp) {
1328 if (tmp == pdev || tmp->bus != pdev->bus)
1331 /* We alias them or they alias us */
1332 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1333 group = get_pci_alias_group(tmp, devfns);
1339 group = get_pci_function_alias_group(tmp, devfns);
1350 struct group_for_pci_data {
1351 struct pci_dev *pdev;
1352 struct iommu_group *group;
1356 * DMA alias iterator callback, return the last seen device. Stop and return
1357 * the IOMMU group if we find one along the way.
1359 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1361 struct group_for_pci_data *data = opaque;
1364 data->group = iommu_group_get(&pdev->dev);
1366 return data->group != NULL;
1370 * Generic device_group call-back function. It just allocates one
1371 * iommu-group per device.
1373 struct iommu_group *generic_device_group(struct device *dev)
1375 return iommu_group_alloc();
1377 EXPORT_SYMBOL_GPL(generic_device_group);
1380 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1381 * to find or create an IOMMU group for a device.
1383 struct iommu_group *pci_device_group(struct device *dev)
1385 struct pci_dev *pdev = to_pci_dev(dev);
1386 struct group_for_pci_data data;
1387 struct pci_bus *bus;
1388 struct iommu_group *group = NULL;
1389 u64 devfns[4] = { 0 };
1391 if (WARN_ON(!dev_is_pci(dev)))
1392 return ERR_PTR(-EINVAL);
1395 * Find the upstream DMA alias for the device. A device must not
1396 * be aliased due to topology in order to have its own IOMMU group.
1397 * If we find an alias along the way that already belongs to a
1400 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1406 * Continue upstream from the point of minimum IOMMU granularity
1407 * due to aliases to the point where devices are protected from
1408 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1411 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1415 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1420 group = iommu_group_get(&pdev->dev);
1426 * Look for existing groups on device aliases. If we alias another
1427 * device or another device aliases us, use the same group.
1429 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1434 * Look for existing groups on non-isolated functions on the same
1435 * slot and aliases of those funcions, if any. No need to clear
1436 * the search bitmap, the tested devfns are still valid.
1438 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1442 /* No shared group found, allocate new */
1443 return iommu_group_alloc();
1445 EXPORT_SYMBOL_GPL(pci_device_group);
1447 /* Get the IOMMU group for device on fsl-mc bus */
1448 struct iommu_group *fsl_mc_device_group(struct device *dev)
1450 struct device *cont_dev = fsl_mc_cont_dev(dev);
1451 struct iommu_group *group;
1453 group = iommu_group_get(cont_dev);
1455 group = iommu_group_alloc();
1458 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1460 static int iommu_get_def_domain_type(struct device *dev)
1462 const struct iommu_ops *ops = dev->bus->iommu_ops;
1463 unsigned int type = 0;
1465 if (ops->def_domain_type)
1466 type = ops->def_domain_type(dev);
1468 return (type == 0) ? iommu_def_domain_type : type;
1471 static int iommu_group_alloc_default_domain(struct bus_type *bus,
1472 struct iommu_group *group,
1475 struct iommu_domain *dom;
1477 dom = __iommu_domain_alloc(bus, type);
1478 if (!dom && type != IOMMU_DOMAIN_DMA) {
1479 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1481 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1488 group->default_domain = dom;
1490 group->domain = dom;
1492 if (!iommu_dma_strict) {
1494 iommu_domain_set_attr(dom,
1495 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1502 static int iommu_alloc_default_domain(struct iommu_group *group,
1507 if (group->default_domain)
1510 type = iommu_get_def_domain_type(dev);
1512 return iommu_group_alloc_default_domain(dev->bus, group, type);
1516 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1517 * @dev: target device
1519 * This function is intended to be called by IOMMU drivers and extended to
1520 * support common, bus-defined algorithms when determining or creating the
1521 * IOMMU group for a device. On success, the caller will hold a reference
1522 * to the returned IOMMU group, which will already include the provided
1523 * device. The reference should be released with iommu_group_put().
1525 static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1527 const struct iommu_ops *ops = dev->bus->iommu_ops;
1528 struct iommu_group *group;
1531 group = iommu_group_get(dev);
1536 return ERR_PTR(-EINVAL);
1538 group = ops->device_group(dev);
1539 if (WARN_ON_ONCE(group == NULL))
1540 return ERR_PTR(-EINVAL);
1545 ret = iommu_group_add_device(group, dev);
1552 iommu_group_put(group);
1554 return ERR_PTR(ret);
1557 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1559 return group->default_domain;
1562 static int probe_iommu_group(struct device *dev, void *data)
1564 struct list_head *group_list = data;
1565 struct iommu_group *group;
1568 /* Device is probed already if in a group */
1569 group = iommu_group_get(dev);
1571 iommu_group_put(group);
1575 ret = __iommu_probe_device(dev, group_list);
1582 static int remove_iommu_group(struct device *dev, void *data)
1584 iommu_release_device(dev);
1589 static int iommu_bus_notifier(struct notifier_block *nb,
1590 unsigned long action, void *data)
1592 unsigned long group_action = 0;
1593 struct device *dev = data;
1594 struct iommu_group *group;
1597 * ADD/DEL call into iommu driver ops if provided, which may
1598 * result in ADD/DEL notifiers to group->notifier
1600 if (action == BUS_NOTIFY_ADD_DEVICE) {
1603 ret = iommu_probe_device(dev);
1604 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1605 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1606 iommu_release_device(dev);
1611 * Remaining BUS_NOTIFYs get filtered and republished to the
1612 * group, if anyone is listening
1614 group = iommu_group_get(dev);
1619 case BUS_NOTIFY_BIND_DRIVER:
1620 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1622 case BUS_NOTIFY_BOUND_DRIVER:
1623 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1625 case BUS_NOTIFY_UNBIND_DRIVER:
1626 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1628 case BUS_NOTIFY_UNBOUND_DRIVER:
1629 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1634 blocking_notifier_call_chain(&group->notifier,
1637 iommu_group_put(group);
1641 struct __group_domain_type {
1646 static int probe_get_default_domain_type(struct device *dev, void *data)
1648 const struct iommu_ops *ops = dev->bus->iommu_ops;
1649 struct __group_domain_type *gtype = data;
1650 unsigned int type = 0;
1652 if (ops->def_domain_type)
1653 type = ops->def_domain_type(dev);
1656 if (gtype->type && gtype->type != type) {
1657 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1658 iommu_domain_type_str(type),
1659 dev_name(gtype->dev),
1660 iommu_domain_type_str(gtype->type));
1673 static void probe_alloc_default_domain(struct bus_type *bus,
1674 struct iommu_group *group)
1676 struct __group_domain_type gtype;
1678 memset(>ype, 0, sizeof(gtype));
1680 /* Ask for default domain requirements of all devices in the group */
1681 __iommu_group_for_each_dev(group, >ype,
1682 probe_get_default_domain_type);
1685 gtype.type = iommu_def_domain_type;
1687 iommu_group_alloc_default_domain(bus, group, gtype.type);
1691 static int iommu_group_do_dma_attach(struct device *dev, void *data)
1693 struct iommu_domain *domain = data;
1696 if (!iommu_is_attach_deferred(domain, dev))
1697 ret = __iommu_attach_device(domain, dev);
1702 static int __iommu_group_dma_attach(struct iommu_group *group)
1704 return __iommu_group_for_each_dev(group, group->default_domain,
1705 iommu_group_do_dma_attach);
1708 static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1710 struct iommu_domain *domain = data;
1712 if (domain->ops->probe_finalize)
1713 domain->ops->probe_finalize(dev);
1718 static void __iommu_group_dma_finalize(struct iommu_group *group)
1720 __iommu_group_for_each_dev(group, group->default_domain,
1721 iommu_group_do_probe_finalize);
1724 static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1726 struct iommu_group *group = data;
1728 iommu_create_device_direct_mappings(group, dev);
1733 static int iommu_group_create_direct_mappings(struct iommu_group *group)
1735 return __iommu_group_for_each_dev(group, group,
1736 iommu_do_create_direct_mappings);
1739 int bus_iommu_probe(struct bus_type *bus)
1741 struct iommu_group *group, *next;
1742 LIST_HEAD(group_list);
1746 * This code-path does not allocate the default domain when
1747 * creating the iommu group, so do it after the groups are
1750 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1754 list_for_each_entry_safe(group, next, &group_list, entry) {
1755 /* Remove item from the list */
1756 list_del_init(&group->entry);
1758 mutex_lock(&group->mutex);
1760 /* Try to allocate default domain */
1761 probe_alloc_default_domain(bus, group);
1763 if (!group->default_domain) {
1764 mutex_unlock(&group->mutex);
1768 iommu_group_create_direct_mappings(group);
1770 ret = __iommu_group_dma_attach(group);
1772 mutex_unlock(&group->mutex);
1777 __iommu_group_dma_finalize(group);
1783 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1785 struct notifier_block *nb;
1788 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1792 nb->notifier_call = iommu_bus_notifier;
1794 err = bus_register_notifier(bus, nb);
1798 err = bus_iommu_probe(bus);
1807 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1808 bus_unregister_notifier(bus, nb);
1817 * bus_set_iommu - set iommu-callbacks for the bus
1819 * @ops: the callbacks provided by the iommu-driver
1821 * This function is called by an iommu driver to set the iommu methods
1822 * used for a particular bus. Drivers for devices on that bus can use
1823 * the iommu-api after these ops are registered.
1824 * This special function is needed because IOMMUs are usually devices on
1825 * the bus itself, so the iommu drivers are not initialized when the bus
1826 * is set up. With this function the iommu-driver can set the iommu-ops
1829 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1834 bus->iommu_ops = NULL;
1838 if (bus->iommu_ops != NULL)
1841 bus->iommu_ops = ops;
1843 /* Do IOMMU specific setup for this bus-type */
1844 err = iommu_bus_init(bus, ops);
1846 bus->iommu_ops = NULL;
1850 EXPORT_SYMBOL_GPL(bus_set_iommu);
1852 bool iommu_present(struct bus_type *bus)
1854 return bus->iommu_ops != NULL;
1856 EXPORT_SYMBOL_GPL(iommu_present);
1858 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1860 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1863 return bus->iommu_ops->capable(cap);
1865 EXPORT_SYMBOL_GPL(iommu_capable);
1868 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1869 * @domain: iommu domain
1870 * @handler: fault handler
1871 * @token: user data, will be passed back to the fault handler
1873 * This function should be used by IOMMU users which want to be notified
1874 * whenever an IOMMU fault happens.
1876 * The fault handler itself should return 0 on success, and an appropriate
1877 * error code otherwise.
1879 void iommu_set_fault_handler(struct iommu_domain *domain,
1880 iommu_fault_handler_t handler,
1885 domain->handler = handler;
1886 domain->handler_token = token;
1888 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1890 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1893 struct iommu_domain *domain;
1895 if (bus == NULL || bus->iommu_ops == NULL)
1898 domain = bus->iommu_ops->domain_alloc(type);
1902 domain->ops = bus->iommu_ops;
1903 domain->type = type;
1904 /* Assume all sizes by default; the driver may override this later */
1905 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1910 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1912 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1914 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1916 void iommu_domain_free(struct iommu_domain *domain)
1918 domain->ops->domain_free(domain);
1920 EXPORT_SYMBOL_GPL(iommu_domain_free);
1922 static int __iommu_attach_device(struct iommu_domain *domain,
1927 if (unlikely(domain->ops->attach_dev == NULL))
1930 ret = domain->ops->attach_dev(domain, dev);
1932 trace_attach_device_to_domain(dev);
1936 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1938 struct iommu_group *group;
1941 group = iommu_group_get(dev);
1946 * Lock the group to make sure the device-count doesn't
1947 * change while we are attaching
1949 mutex_lock(&group->mutex);
1951 if (iommu_group_device_count(group) != 1)
1954 ret = __iommu_attach_group(domain, group);
1957 mutex_unlock(&group->mutex);
1958 iommu_group_put(group);
1962 EXPORT_SYMBOL_GPL(iommu_attach_device);
1964 int iommu_cache_invalidate(struct iommu_domain *domain, struct device *dev,
1965 struct iommu_cache_invalidate_info *inv_info)
1967 if (unlikely(!domain->ops->cache_invalidate))
1970 return domain->ops->cache_invalidate(domain, dev, inv_info);
1972 EXPORT_SYMBOL_GPL(iommu_cache_invalidate);
1974 int iommu_sva_bind_gpasid(struct iommu_domain *domain,
1975 struct device *dev, struct iommu_gpasid_bind_data *data)
1977 if (unlikely(!domain->ops->sva_bind_gpasid))
1980 return domain->ops->sva_bind_gpasid(domain, dev, data);
1982 EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid);
1984 int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
1987 if (unlikely(!domain->ops->sva_unbind_gpasid))
1990 return domain->ops->sva_unbind_gpasid(dev, pasid);
1992 EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
1994 static void __iommu_detach_device(struct iommu_domain *domain,
1997 if (iommu_is_attach_deferred(domain, dev))
2000 if (unlikely(domain->ops->detach_dev == NULL))
2003 domain->ops->detach_dev(domain, dev);
2004 trace_detach_device_from_domain(dev);
2007 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2009 struct iommu_group *group;
2011 group = iommu_group_get(dev);
2015 mutex_lock(&group->mutex);
2016 if (iommu_group_device_count(group) != 1) {
2021 __iommu_detach_group(domain, group);
2024 mutex_unlock(&group->mutex);
2025 iommu_group_put(group);
2027 EXPORT_SYMBOL_GPL(iommu_detach_device);
2029 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2031 struct iommu_domain *domain;
2032 struct iommu_group *group;
2034 group = iommu_group_get(dev);
2038 domain = group->domain;
2040 iommu_group_put(group);
2044 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2047 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2048 * guarantees that the group and its default domain are valid and correct.
2050 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2052 return dev->iommu_group->default_domain;
2056 * IOMMU groups are really the natural working unit of the IOMMU, but
2057 * the IOMMU API works on domains and devices. Bridge that gap by
2058 * iterating over the devices in a group. Ideally we'd have a single
2059 * device which represents the requestor ID of the group, but we also
2060 * allow IOMMU drivers to create policy defined minimum sets, where
2061 * the physical hardware may be able to distiguish members, but we
2062 * wish to group them at a higher level (ex. untrusted multi-function
2063 * PCI devices). Thus we attach each device.
2065 static int iommu_group_do_attach_device(struct device *dev, void *data)
2067 struct iommu_domain *domain = data;
2069 return __iommu_attach_device(domain, dev);
2072 static int __iommu_attach_group(struct iommu_domain *domain,
2073 struct iommu_group *group)
2077 if (group->default_domain && group->domain != group->default_domain)
2080 ret = __iommu_group_for_each_dev(group, domain,
2081 iommu_group_do_attach_device);
2083 group->domain = domain;
2088 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2092 mutex_lock(&group->mutex);
2093 ret = __iommu_attach_group(domain, group);
2094 mutex_unlock(&group->mutex);
2098 EXPORT_SYMBOL_GPL(iommu_attach_group);
2100 static int iommu_group_do_detach_device(struct device *dev, void *data)
2102 struct iommu_domain *domain = data;
2104 __iommu_detach_device(domain, dev);
2109 static void __iommu_detach_group(struct iommu_domain *domain,
2110 struct iommu_group *group)
2114 if (!group->default_domain) {
2115 __iommu_group_for_each_dev(group, domain,
2116 iommu_group_do_detach_device);
2117 group->domain = NULL;
2121 if (group->domain == group->default_domain)
2124 /* Detach by re-attaching to the default domain */
2125 ret = __iommu_group_for_each_dev(group, group->default_domain,
2126 iommu_group_do_attach_device);
2130 group->domain = group->default_domain;
2133 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2135 mutex_lock(&group->mutex);
2136 __iommu_detach_group(domain, group);
2137 mutex_unlock(&group->mutex);
2139 EXPORT_SYMBOL_GPL(iommu_detach_group);
2141 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2143 if (unlikely(domain->ops->iova_to_phys == NULL))
2146 return domain->ops->iova_to_phys(domain, iova);
2148 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2150 static size_t iommu_pgsize(struct iommu_domain *domain,
2151 unsigned long addr_merge, size_t size)
2153 unsigned int pgsize_idx;
2156 /* Max page size that still fits into 'size' */
2157 pgsize_idx = __fls(size);
2159 /* need to consider alignment requirements ? */
2160 if (likely(addr_merge)) {
2161 /* Max page size allowed by address */
2162 unsigned int align_pgsize_idx = __ffs(addr_merge);
2163 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2166 /* build a mask of acceptable page sizes */
2167 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2169 /* throw away page sizes not supported by the hardware */
2170 pgsize &= domain->pgsize_bitmap;
2172 /* make sure we're still sane */
2175 /* pick the biggest page */
2176 pgsize_idx = __fls(pgsize);
2177 pgsize = 1UL << pgsize_idx;
2182 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2183 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2185 const struct iommu_ops *ops = domain->ops;
2186 unsigned long orig_iova = iova;
2187 unsigned int min_pagesz;
2188 size_t orig_size = size;
2189 phys_addr_t orig_paddr = paddr;
2192 if (unlikely(ops->map == NULL ||
2193 domain->pgsize_bitmap == 0UL))
2196 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2199 /* find out the minimum page size supported */
2200 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2203 * both the virtual address and the physical one, as well as
2204 * the size of the mapping, must be aligned (at least) to the
2205 * size of the smallest page supported by the hardware
2207 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2208 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2209 iova, &paddr, size, min_pagesz);
2213 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2216 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
2218 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
2219 iova, &paddr, pgsize);
2220 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2230 if (ops->iotlb_sync_map)
2231 ops->iotlb_sync_map(domain);
2233 /* unroll mapping in case something went wrong */
2235 iommu_unmap(domain, orig_iova, orig_size - size);
2237 trace_map(orig_iova, orig_paddr, orig_size);
2242 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2243 phys_addr_t paddr, size_t size, int prot)
2246 return __iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2248 EXPORT_SYMBOL_GPL(iommu_map);
2250 int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2251 phys_addr_t paddr, size_t size, int prot)
2253 return __iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2255 EXPORT_SYMBOL_GPL(iommu_map_atomic);
2257 static size_t __iommu_unmap(struct iommu_domain *domain,
2258 unsigned long iova, size_t size,
2259 struct iommu_iotlb_gather *iotlb_gather)
2261 const struct iommu_ops *ops = domain->ops;
2262 size_t unmapped_page, unmapped = 0;
2263 unsigned long orig_iova = iova;
2264 unsigned int min_pagesz;
2266 if (unlikely(ops->unmap == NULL ||
2267 domain->pgsize_bitmap == 0UL))
2270 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2273 /* find out the minimum page size supported */
2274 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2277 * The virtual address, as well as the size of the mapping, must be
2278 * aligned (at least) to the size of the smallest page supported
2281 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2282 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2283 iova, size, min_pagesz);
2287 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2290 * Keep iterating until we either unmap 'size' bytes (or more)
2291 * or we hit an area that isn't mapped.
2293 while (unmapped < size) {
2294 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
2296 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
2300 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2301 iova, unmapped_page);
2303 iova += unmapped_page;
2304 unmapped += unmapped_page;
2307 trace_unmap(orig_iova, size, unmapped);
2311 size_t iommu_unmap(struct iommu_domain *domain,
2312 unsigned long iova, size_t size)
2314 struct iommu_iotlb_gather iotlb_gather;
2317 iommu_iotlb_gather_init(&iotlb_gather);
2318 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2319 iommu_tlb_sync(domain, &iotlb_gather);
2323 EXPORT_SYMBOL_GPL(iommu_unmap);
2325 size_t iommu_unmap_fast(struct iommu_domain *domain,
2326 unsigned long iova, size_t size,
2327 struct iommu_iotlb_gather *iotlb_gather)
2329 return __iommu_unmap(domain, iova, size, iotlb_gather);
2331 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2333 static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2334 struct scatterlist *sg, unsigned int nents, int prot,
2337 size_t len = 0, mapped = 0;
2342 while (i <= nents) {
2343 phys_addr_t s_phys = sg_phys(sg);
2345 if (len && s_phys != start + len) {
2346 ret = __iommu_map(domain, iova + mapped, start,
2370 /* undo mappings already done */
2371 iommu_unmap(domain, iova, mapped);
2377 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2378 struct scatterlist *sg, unsigned int nents, int prot)
2381 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2383 EXPORT_SYMBOL_GPL(iommu_map_sg);
2385 size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2386 struct scatterlist *sg, unsigned int nents, int prot)
2388 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2390 EXPORT_SYMBOL_GPL(iommu_map_sg_atomic);
2392 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
2393 phys_addr_t paddr, u64 size, int prot)
2395 if (unlikely(domain->ops->domain_window_enable == NULL))
2398 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2401 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2403 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2405 if (unlikely(domain->ops->domain_window_disable == NULL))
2408 return domain->ops->domain_window_disable(domain, wnd_nr);
2410 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2413 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2414 * @domain: the iommu domain where the fault has happened
2415 * @dev: the device where the fault has happened
2416 * @iova: the faulting address
2417 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2419 * This function should be called by the low-level IOMMU implementations
2420 * whenever IOMMU faults happen, to allow high-level users, that are
2421 * interested in such events, to know about them.
2423 * This event may be useful for several possible use cases:
2424 * - mere logging of the event
2425 * - dynamic TLB/PTE loading
2426 * - if restarting of the faulting device is required
2428 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2429 * PTE/TLB loading will one day be supported, implementations will be able
2430 * to tell whether it succeeded or not according to this return value).
2432 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2433 * (though fault handlers can also return -ENOSYS, in case they want to
2434 * elicit the default behavior of the IOMMU drivers).
2436 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2437 unsigned long iova, int flags)
2442 * if upper layers showed interest and installed a fault handler,
2445 if (domain->handler)
2446 ret = domain->handler(domain, dev, iova, flags,
2447 domain->handler_token);
2449 trace_io_page_fault(dev, iova, flags);
2452 EXPORT_SYMBOL_GPL(report_iommu_fault);
2454 static int __init iommu_init(void)
2456 iommu_group_kset = kset_create_and_add("iommu_groups",
2458 BUG_ON(!iommu_group_kset);
2460 iommu_debugfs_setup();
2464 core_initcall(iommu_init);
2466 int iommu_domain_get_attr(struct iommu_domain *domain,
2467 enum iommu_attr attr, void *data)
2469 struct iommu_domain_geometry *geometry;
2474 case DOMAIN_ATTR_GEOMETRY:
2476 *geometry = domain->geometry;
2479 case DOMAIN_ATTR_PAGING:
2481 *paging = (domain->pgsize_bitmap != 0UL);
2484 if (!domain->ops->domain_get_attr)
2487 ret = domain->ops->domain_get_attr(domain, attr, data);
2492 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2494 int iommu_domain_set_attr(struct iommu_domain *domain,
2495 enum iommu_attr attr, void *data)
2501 if (domain->ops->domain_set_attr == NULL)
2504 ret = domain->ops->domain_set_attr(domain, attr, data);
2509 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
2511 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2513 const struct iommu_ops *ops = dev->bus->iommu_ops;
2515 if (ops && ops->get_resv_regions)
2516 ops->get_resv_regions(dev, list);
2519 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2521 const struct iommu_ops *ops = dev->bus->iommu_ops;
2523 if (ops && ops->put_resv_regions)
2524 ops->put_resv_regions(dev, list);
2528 * generic_iommu_put_resv_regions - Reserved region driver helper
2529 * @dev: device for which to free reserved regions
2530 * @list: reserved region list for device
2532 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2533 * for simple reservations. Memory allocated for each reserved region will be
2534 * freed. If an IOMMU driver allocates additional resources per region, it is
2535 * going to have to implement a custom callback.
2537 void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2539 struct iommu_resv_region *entry, *next;
2541 list_for_each_entry_safe(entry, next, list, list)
2544 EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2546 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2547 size_t length, int prot,
2548 enum iommu_resv_type type)
2550 struct iommu_resv_region *region;
2552 region = kzalloc(sizeof(*region), GFP_KERNEL);
2556 INIT_LIST_HEAD(®ion->list);
2557 region->start = start;
2558 region->length = length;
2559 region->prot = prot;
2560 region->type = type;
2563 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2565 void iommu_set_default_passthrough(bool cmd_line)
2568 iommu_set_cmd_line_dma_api();
2570 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2573 void iommu_set_default_translated(bool cmd_line)
2576 iommu_set_cmd_line_dma_api();
2578 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2581 bool iommu_default_passthrough(void)
2583 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2585 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2587 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2589 const struct iommu_ops *ops = NULL;
2590 struct iommu_device *iommu;
2592 spin_lock(&iommu_device_lock);
2593 list_for_each_entry(iommu, &iommu_device_list, list)
2594 if (iommu->fwnode == fwnode) {
2598 spin_unlock(&iommu_device_lock);
2602 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2603 const struct iommu_ops *ops)
2605 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2608 return ops == fwspec->ops ? 0 : -EINVAL;
2610 if (!dev_iommu_get(dev))
2613 /* Preallocate for the overwhelmingly common case of 1 ID */
2614 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2618 of_node_get(to_of_node(iommu_fwnode));
2619 fwspec->iommu_fwnode = iommu_fwnode;
2621 dev_iommu_fwspec_set(dev, fwspec);
2624 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2626 void iommu_fwspec_free(struct device *dev)
2628 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2631 fwnode_handle_put(fwspec->iommu_fwnode);
2633 dev_iommu_fwspec_set(dev, NULL);
2636 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2638 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2640 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2646 new_num = fwspec->num_ids + num_ids;
2648 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2653 dev_iommu_fwspec_set(dev, fwspec);
2656 for (i = 0; i < num_ids; i++)
2657 fwspec->ids[fwspec->num_ids + i] = ids[i];
2659 fwspec->num_ids = new_num;
2662 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2665 * Per device IOMMU features.
2667 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2669 const struct iommu_ops *ops = dev->bus->iommu_ops;
2671 if (ops && ops->dev_has_feat)
2672 return ops->dev_has_feat(dev, feat);
2676 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2678 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2680 const struct iommu_ops *ops = dev->bus->iommu_ops;
2682 if (ops && ops->dev_enable_feat)
2683 return ops->dev_enable_feat(dev, feat);
2687 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2690 * The device drivers should do the necessary cleanups before calling this.
2691 * For example, before disabling the aux-domain feature, the device driver
2692 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2694 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2696 const struct iommu_ops *ops = dev->bus->iommu_ops;
2698 if (ops && ops->dev_disable_feat)
2699 return ops->dev_disable_feat(dev, feat);
2703 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2705 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2707 const struct iommu_ops *ops = dev->bus->iommu_ops;
2709 if (ops && ops->dev_feat_enabled)
2710 return ops->dev_feat_enabled(dev, feat);
2714 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2717 * Aux-domain specific attach/detach.
2719 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2720 * true. Also, as long as domains are attached to a device through this
2721 * interface, any tries to call iommu_attach_device() should fail
2722 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2723 * This should make us safe against a device being attached to a guest as a
2724 * whole while there are still pasid users on it (aux and sva).
2726 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2730 if (domain->ops->aux_attach_dev)
2731 ret = domain->ops->aux_attach_dev(domain, dev);
2734 trace_attach_device_to_domain(dev);
2738 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2740 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2742 if (domain->ops->aux_detach_dev) {
2743 domain->ops->aux_detach_dev(domain, dev);
2744 trace_detach_device_from_domain(dev);
2747 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2749 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2753 if (domain->ops->aux_get_pasid)
2754 ret = domain->ops->aux_get_pasid(domain, dev);
2758 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2761 * iommu_sva_bind_device() - Bind a process address space to a device
2763 * @mm: the mm to bind, caller must hold a reference to it
2765 * Create a bond between device and address space, allowing the device to access
2766 * the mm using the returned PASID. If a bond already exists between @device and
2767 * @mm, it is returned and an additional reference is taken. Caller must call
2768 * iommu_sva_unbind_device() to release each reference.
2770 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2771 * initialize the required SVA features.
2773 * On error, returns an ERR_PTR value.
2776 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2778 struct iommu_group *group;
2779 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2780 const struct iommu_ops *ops = dev->bus->iommu_ops;
2782 if (!ops || !ops->sva_bind)
2783 return ERR_PTR(-ENODEV);
2785 group = iommu_group_get(dev);
2787 return ERR_PTR(-ENODEV);
2789 /* Ensure device count and domain don't change while we're binding */
2790 mutex_lock(&group->mutex);
2793 * To keep things simple, SVA currently doesn't support IOMMU groups
2794 * with more than one device. Existing SVA-capable systems are not
2795 * affected by the problems that required IOMMU groups (lack of ACS
2796 * isolation, device ID aliasing and other hardware issues).
2798 if (iommu_group_device_count(group) != 1)
2801 handle = ops->sva_bind(dev, mm, drvdata);
2804 mutex_unlock(&group->mutex);
2805 iommu_group_put(group);
2809 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2812 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2813 * @handle: the handle returned by iommu_sva_bind_device()
2815 * Put reference to a bond between device and address space. The device should
2816 * not be issuing any more transaction for this PASID. All outstanding page
2817 * requests for this PASID must have been flushed to the IOMMU.
2819 * Returns 0 on success, or an error value
2821 void iommu_sva_unbind_device(struct iommu_sva *handle)
2823 struct iommu_group *group;
2824 struct device *dev = handle->dev;
2825 const struct iommu_ops *ops = dev->bus->iommu_ops;
2827 if (!ops || !ops->sva_unbind)
2830 group = iommu_group_get(dev);
2834 mutex_lock(&group->mutex);
2835 ops->sva_unbind(handle);
2836 mutex_unlock(&group->mutex);
2838 iommu_group_put(group);
2840 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2842 u32 iommu_sva_get_pasid(struct iommu_sva *handle)
2844 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2846 if (!ops || !ops->sva_get_pasid)
2847 return IOMMU_PASID_INVALID;
2849 return ops->sva_get_pasid(handle);
2851 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);