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 <trace/events/iommu.h>
27 static struct kset *iommu_group_kset;
28 static DEFINE_IDA(iommu_group_ida);
29 #ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
30 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
32 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
34 static bool iommu_dma_strict __read_mostly = true;
35 static u32 iommu_cmd_line __read_mostly;
39 struct kobject *devices_kobj;
40 struct list_head devices;
42 struct blocking_notifier_head notifier;
44 void (*iommu_data_release)(void *iommu_data);
47 struct iommu_domain *default_domain;
48 struct iommu_domain *domain;
52 struct list_head list;
57 struct iommu_group_attribute {
58 struct attribute attr;
59 ssize_t (*show)(struct iommu_group *group, char *buf);
60 ssize_t (*store)(struct iommu_group *group,
61 const char *buf, size_t count);
64 static const char * const iommu_group_resv_type_string[] = {
65 [IOMMU_RESV_DIRECT] = "direct",
66 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
67 [IOMMU_RESV_RESERVED] = "reserved",
68 [IOMMU_RESV_MSI] = "msi",
69 [IOMMU_RESV_SW_MSI] = "msi",
72 #define IOMMU_CMD_LINE_DMA_API BIT(0)
74 static void iommu_set_cmd_line_dma_api(void)
76 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
79 static bool __maybe_unused iommu_cmd_line_dma_api(void)
81 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
84 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
85 struct iommu_group_attribute iommu_group_attr_##_name = \
86 __ATTR(_name, _mode, _show, _store)
88 #define to_iommu_group_attr(_attr) \
89 container_of(_attr, struct iommu_group_attribute, attr)
90 #define to_iommu_group(_kobj) \
91 container_of(_kobj, struct iommu_group, kobj)
93 static LIST_HEAD(iommu_device_list);
94 static DEFINE_SPINLOCK(iommu_device_lock);
97 * Use a function instead of an array here because the domain-type is a
98 * bit-field, so an array would waste memory.
100 static const char *iommu_domain_type_str(unsigned int t)
103 case IOMMU_DOMAIN_BLOCKED:
105 case IOMMU_DOMAIN_IDENTITY:
106 return "Passthrough";
107 case IOMMU_DOMAIN_UNMANAGED:
109 case IOMMU_DOMAIN_DMA:
116 static int __init iommu_subsys_init(void)
118 pr_info("Default domain type: %s\n",
119 iommu_domain_type_str(iommu_def_domain_type));
123 subsys_initcall(iommu_subsys_init);
125 int iommu_device_register(struct iommu_device *iommu)
127 spin_lock(&iommu_device_lock);
128 list_add_tail(&iommu->list, &iommu_device_list);
129 spin_unlock(&iommu_device_lock);
133 void iommu_device_unregister(struct iommu_device *iommu)
135 spin_lock(&iommu_device_lock);
136 list_del(&iommu->list);
137 spin_unlock(&iommu_device_lock);
140 static struct iommu_param *iommu_get_dev_param(struct device *dev)
142 struct iommu_param *param = dev->iommu_param;
147 param = kzalloc(sizeof(*param), GFP_KERNEL);
151 mutex_init(¶m->lock);
152 dev->iommu_param = param;
156 static void iommu_free_dev_param(struct device *dev)
158 kfree(dev->iommu_param);
159 dev->iommu_param = NULL;
162 int iommu_probe_device(struct device *dev)
164 const struct iommu_ops *ops = dev->bus->iommu_ops;
167 WARN_ON(dev->iommu_group);
171 if (!iommu_get_dev_param(dev))
174 ret = ops->add_device(dev);
176 iommu_free_dev_param(dev);
181 void iommu_release_device(struct device *dev)
183 const struct iommu_ops *ops = dev->bus->iommu_ops;
185 if (dev->iommu_group)
186 ops->remove_device(dev);
188 iommu_free_dev_param(dev);
191 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
193 static int __iommu_attach_device(struct iommu_domain *domain,
195 static int __iommu_attach_group(struct iommu_domain *domain,
196 struct iommu_group *group);
197 static void __iommu_detach_group(struct iommu_domain *domain,
198 struct iommu_group *group);
200 static int __init iommu_set_def_domain_type(char *str)
205 ret = kstrtobool(str, &pt);
210 iommu_set_default_passthrough(true);
212 iommu_set_default_translated(true);
216 early_param("iommu.passthrough", iommu_set_def_domain_type);
218 static int __init iommu_dma_setup(char *str)
220 return kstrtobool(str, &iommu_dma_strict);
222 early_param("iommu.strict", iommu_dma_setup);
224 static ssize_t iommu_group_attr_show(struct kobject *kobj,
225 struct attribute *__attr, char *buf)
227 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
228 struct iommu_group *group = to_iommu_group(kobj);
232 ret = attr->show(group, buf);
236 static ssize_t iommu_group_attr_store(struct kobject *kobj,
237 struct attribute *__attr,
238 const char *buf, size_t count)
240 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
241 struct iommu_group *group = to_iommu_group(kobj);
245 ret = attr->store(group, buf, count);
249 static const struct sysfs_ops iommu_group_sysfs_ops = {
250 .show = iommu_group_attr_show,
251 .store = iommu_group_attr_store,
254 static int iommu_group_create_file(struct iommu_group *group,
255 struct iommu_group_attribute *attr)
257 return sysfs_create_file(&group->kobj, &attr->attr);
260 static void iommu_group_remove_file(struct iommu_group *group,
261 struct iommu_group_attribute *attr)
263 sysfs_remove_file(&group->kobj, &attr->attr);
266 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
268 return sprintf(buf, "%s\n", group->name);
272 * iommu_insert_resv_region - Insert a new region in the
273 * list of reserved regions.
274 * @new: new region to insert
275 * @regions: list of regions
277 * The new element is sorted by address with respect to the other
278 * regions of the same type. In case it overlaps with another
279 * region of the same type, regions are merged. In case it
280 * overlaps with another region of different type, regions are
283 static int iommu_insert_resv_region(struct iommu_resv_region *new,
284 struct list_head *regions)
286 struct iommu_resv_region *region;
287 phys_addr_t start = new->start;
288 phys_addr_t end = new->start + new->length - 1;
289 struct list_head *pos = regions->next;
291 while (pos != regions) {
292 struct iommu_resv_region *entry =
293 list_entry(pos, struct iommu_resv_region, list);
294 phys_addr_t a = entry->start;
295 phys_addr_t b = entry->start + entry->length - 1;
296 int type = entry->type;
300 } else if (start > b) {
302 } else if ((start >= a) && (end <= b)) {
303 if (new->type == type)
308 if (new->type == type) {
309 phys_addr_t new_start = min(a, start);
310 phys_addr_t new_end = max(b, end);
313 list_del(&entry->list);
314 entry->start = new_start;
315 entry->length = new_end - new_start + 1;
316 ret = iommu_insert_resv_region(entry, regions);
325 region = iommu_alloc_resv_region(new->start, new->length,
326 new->prot, new->type);
330 list_add_tail(®ion->list, pos);
335 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
336 struct list_head *group_resv_regions)
338 struct iommu_resv_region *entry;
341 list_for_each_entry(entry, dev_resv_regions, list) {
342 ret = iommu_insert_resv_region(entry, group_resv_regions);
349 int iommu_get_group_resv_regions(struct iommu_group *group,
350 struct list_head *head)
352 struct group_device *device;
355 mutex_lock(&group->mutex);
356 list_for_each_entry(device, &group->devices, list) {
357 struct list_head dev_resv_regions;
359 INIT_LIST_HEAD(&dev_resv_regions);
360 iommu_get_resv_regions(device->dev, &dev_resv_regions);
361 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
362 iommu_put_resv_regions(device->dev, &dev_resv_regions);
366 mutex_unlock(&group->mutex);
369 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
371 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
374 struct iommu_resv_region *region, *next;
375 struct list_head group_resv_regions;
378 INIT_LIST_HEAD(&group_resv_regions);
379 iommu_get_group_resv_regions(group, &group_resv_regions);
381 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
382 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
383 (long long int)region->start,
384 (long long int)(region->start +
386 iommu_group_resv_type_string[region->type]);
393 static ssize_t iommu_group_show_type(struct iommu_group *group,
396 char *type = "unknown\n";
398 if (group->default_domain) {
399 switch (group->default_domain->type) {
400 case IOMMU_DOMAIN_BLOCKED:
403 case IOMMU_DOMAIN_IDENTITY:
406 case IOMMU_DOMAIN_UNMANAGED:
407 type = "unmanaged\n";
409 case IOMMU_DOMAIN_DMA:
419 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
421 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
422 iommu_group_show_resv_regions, NULL);
424 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
426 static void iommu_group_release(struct kobject *kobj)
428 struct iommu_group *group = to_iommu_group(kobj);
430 pr_debug("Releasing group %d\n", group->id);
432 if (group->iommu_data_release)
433 group->iommu_data_release(group->iommu_data);
435 ida_simple_remove(&iommu_group_ida, group->id);
437 if (group->default_domain)
438 iommu_domain_free(group->default_domain);
444 static struct kobj_type iommu_group_ktype = {
445 .sysfs_ops = &iommu_group_sysfs_ops,
446 .release = iommu_group_release,
450 * iommu_group_alloc - Allocate a new group
452 * This function is called by an iommu driver to allocate a new iommu
453 * group. The iommu group represents the minimum granularity of the iommu.
454 * Upon successful return, the caller holds a reference to the supplied
455 * group in order to hold the group until devices are added. Use
456 * iommu_group_put() to release this extra reference count, allowing the
457 * group to be automatically reclaimed once it has no devices or external
460 struct iommu_group *iommu_group_alloc(void)
462 struct iommu_group *group;
465 group = kzalloc(sizeof(*group), GFP_KERNEL);
467 return ERR_PTR(-ENOMEM);
469 group->kobj.kset = iommu_group_kset;
470 mutex_init(&group->mutex);
471 INIT_LIST_HEAD(&group->devices);
472 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
474 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
481 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
482 NULL, "%d", group->id);
484 ida_simple_remove(&iommu_group_ida, group->id);
489 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
490 if (!group->devices_kobj) {
491 kobject_put(&group->kobj); /* triggers .release & free */
492 return ERR_PTR(-ENOMEM);
496 * The devices_kobj holds a reference on the group kobject, so
497 * as long as that exists so will the group. We can therefore
498 * use the devices_kobj for reference counting.
500 kobject_put(&group->kobj);
502 ret = iommu_group_create_file(group,
503 &iommu_group_attr_reserved_regions);
507 ret = iommu_group_create_file(group, &iommu_group_attr_type);
511 pr_debug("Allocated group %d\n", group->id);
515 EXPORT_SYMBOL_GPL(iommu_group_alloc);
517 struct iommu_group *iommu_group_get_by_id(int id)
519 struct kobject *group_kobj;
520 struct iommu_group *group;
523 if (!iommu_group_kset)
526 name = kasprintf(GFP_KERNEL, "%d", id);
530 group_kobj = kset_find_obj(iommu_group_kset, name);
536 group = container_of(group_kobj, struct iommu_group, kobj);
537 BUG_ON(group->id != id);
539 kobject_get(group->devices_kobj);
540 kobject_put(&group->kobj);
544 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
547 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
550 * iommu drivers can store data in the group for use when doing iommu
551 * operations. This function provides a way to retrieve it. Caller
552 * should hold a group reference.
554 void *iommu_group_get_iommudata(struct iommu_group *group)
556 return group->iommu_data;
558 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
561 * iommu_group_set_iommudata - set iommu_data for a group
563 * @iommu_data: new data
564 * @release: release function for iommu_data
566 * iommu drivers can store data in the group for use when doing iommu
567 * operations. This function provides a way to set the data after
568 * the group has been allocated. Caller should hold a group reference.
570 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
571 void (*release)(void *iommu_data))
573 group->iommu_data = iommu_data;
574 group->iommu_data_release = release;
576 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
579 * iommu_group_set_name - set name for a group
583 * Allow iommu driver to set a name for a group. When set it will
584 * appear in a name attribute file under the group in sysfs.
586 int iommu_group_set_name(struct iommu_group *group, const char *name)
591 iommu_group_remove_file(group, &iommu_group_attr_name);
598 group->name = kstrdup(name, GFP_KERNEL);
602 ret = iommu_group_create_file(group, &iommu_group_attr_name);
611 EXPORT_SYMBOL_GPL(iommu_group_set_name);
613 static int iommu_group_create_direct_mappings(struct iommu_group *group,
616 struct iommu_domain *domain = group->default_domain;
617 struct iommu_resv_region *entry;
618 struct list_head mappings;
619 unsigned long pg_size;
622 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
625 BUG_ON(!domain->pgsize_bitmap);
627 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
628 INIT_LIST_HEAD(&mappings);
630 iommu_get_resv_regions(dev, &mappings);
632 /* We need to consider overlapping regions for different devices */
633 list_for_each_entry(entry, &mappings, list) {
634 dma_addr_t start, end, addr;
636 if (domain->ops->apply_resv_region)
637 domain->ops->apply_resv_region(dev, domain, entry);
639 start = ALIGN(entry->start, pg_size);
640 end = ALIGN(entry->start + entry->length, pg_size);
642 if (entry->type != IOMMU_RESV_DIRECT &&
643 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
646 for (addr = start; addr < end; addr += pg_size) {
647 phys_addr_t phys_addr;
649 phys_addr = iommu_iova_to_phys(domain, addr);
653 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
660 iommu_flush_tlb_all(domain);
663 iommu_put_resv_regions(dev, &mappings);
669 * iommu_group_add_device - add a device to an iommu group
670 * @group: the group into which to add the device (reference should be held)
673 * This function is called by an iommu driver to add a device into a
674 * group. Adding a device increments the group reference count.
676 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
679 struct group_device *device;
681 device = kzalloc(sizeof(*device), GFP_KERNEL);
687 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
689 goto err_free_device;
691 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
695 goto err_remove_link;
698 ret = sysfs_create_link_nowarn(group->devices_kobj,
699 &dev->kobj, device->name);
701 if (ret == -EEXIST && i >= 0) {
703 * Account for the slim chance of collision
704 * and append an instance to the name.
707 device->name = kasprintf(GFP_KERNEL, "%s.%d",
708 kobject_name(&dev->kobj), i++);
714 kobject_get(group->devices_kobj);
716 dev->iommu_group = group;
718 iommu_group_create_direct_mappings(group, dev);
720 mutex_lock(&group->mutex);
721 list_add_tail(&device->list, &group->devices);
723 ret = __iommu_attach_device(group->domain, dev);
724 mutex_unlock(&group->mutex);
728 /* Notify any listeners about change to group. */
729 blocking_notifier_call_chain(&group->notifier,
730 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
732 trace_add_device_to_group(group->id, dev);
734 dev_info(dev, "Adding to iommu group %d\n", group->id);
739 mutex_lock(&group->mutex);
740 list_del(&device->list);
741 mutex_unlock(&group->mutex);
742 dev->iommu_group = NULL;
743 kobject_put(group->devices_kobj);
747 sysfs_remove_link(&dev->kobj, "iommu_group");
750 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
753 EXPORT_SYMBOL_GPL(iommu_group_add_device);
756 * iommu_group_remove_device - remove a device from it's current group
757 * @dev: device to be removed
759 * This function is called by an iommu driver to remove the device from
760 * it's current group. This decrements the iommu group reference count.
762 void iommu_group_remove_device(struct device *dev)
764 struct iommu_group *group = dev->iommu_group;
765 struct group_device *tmp_device, *device = NULL;
767 dev_info(dev, "Removing from iommu group %d\n", group->id);
769 /* Pre-notify listeners that a device is being removed. */
770 blocking_notifier_call_chain(&group->notifier,
771 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
773 mutex_lock(&group->mutex);
774 list_for_each_entry(tmp_device, &group->devices, list) {
775 if (tmp_device->dev == dev) {
777 list_del(&device->list);
781 mutex_unlock(&group->mutex);
786 sysfs_remove_link(group->devices_kobj, device->name);
787 sysfs_remove_link(&dev->kobj, "iommu_group");
789 trace_remove_device_from_group(group->id, dev);
793 dev->iommu_group = NULL;
794 kobject_put(group->devices_kobj);
796 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
798 static int iommu_group_device_count(struct iommu_group *group)
800 struct group_device *entry;
803 list_for_each_entry(entry, &group->devices, list)
810 * iommu_group_for_each_dev - iterate over each device in the group
812 * @data: caller opaque data to be passed to callback function
813 * @fn: caller supplied callback function
815 * This function is called by group users to iterate over group devices.
816 * Callers should hold a reference count to the group during callback.
817 * The group->mutex is held across callbacks, which will block calls to
818 * iommu_group_add/remove_device.
820 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
821 int (*fn)(struct device *, void *))
823 struct group_device *device;
826 list_for_each_entry(device, &group->devices, list) {
827 ret = fn(device->dev, data);
835 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
836 int (*fn)(struct device *, void *))
840 mutex_lock(&group->mutex);
841 ret = __iommu_group_for_each_dev(group, data, fn);
842 mutex_unlock(&group->mutex);
846 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
849 * iommu_group_get - Return the group for a device and increment reference
850 * @dev: get the group that this device belongs to
852 * This function is called by iommu drivers and users to get the group
853 * for the specified device. If found, the group is returned and the group
854 * reference in incremented, else NULL.
856 struct iommu_group *iommu_group_get(struct device *dev)
858 struct iommu_group *group = dev->iommu_group;
861 kobject_get(group->devices_kobj);
865 EXPORT_SYMBOL_GPL(iommu_group_get);
868 * iommu_group_ref_get - Increment reference on a group
869 * @group: the group to use, must not be NULL
871 * This function is called by iommu drivers to take additional references on an
872 * existing group. Returns the given group for convenience.
874 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
876 kobject_get(group->devices_kobj);
881 * iommu_group_put - Decrement group reference
882 * @group: the group to use
884 * This function is called by iommu drivers and users to release the
885 * iommu group. Once the reference count is zero, the group is released.
887 void iommu_group_put(struct iommu_group *group)
890 kobject_put(group->devices_kobj);
892 EXPORT_SYMBOL_GPL(iommu_group_put);
895 * iommu_group_register_notifier - Register a notifier for group changes
896 * @group: the group to watch
897 * @nb: notifier block to signal
899 * This function allows iommu group users to track changes in a group.
900 * See include/linux/iommu.h for actions sent via this notifier. Caller
901 * should hold a reference to the group throughout notifier registration.
903 int iommu_group_register_notifier(struct iommu_group *group,
904 struct notifier_block *nb)
906 return blocking_notifier_chain_register(&group->notifier, nb);
908 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
911 * iommu_group_unregister_notifier - Unregister a notifier
912 * @group: the group to watch
913 * @nb: notifier block to signal
915 * Unregister a previously registered group notifier block.
917 int iommu_group_unregister_notifier(struct iommu_group *group,
918 struct notifier_block *nb)
920 return blocking_notifier_chain_unregister(&group->notifier, nb);
922 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
925 * iommu_register_device_fault_handler() - Register a device fault handler
927 * @handler: the fault handler
928 * @data: private data passed as argument to the handler
930 * When an IOMMU fault event is received, this handler gets called with the
931 * fault event and data as argument. The handler should return 0 on success. If
932 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
933 * complete the fault by calling iommu_page_response() with one of the following
935 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
936 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
937 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
938 * page faults if possible.
940 * Return 0 if the fault handler was installed successfully, or an error.
942 int iommu_register_device_fault_handler(struct device *dev,
943 iommu_dev_fault_handler_t handler,
946 struct iommu_param *param = dev->iommu_param;
952 mutex_lock(¶m->lock);
953 /* Only allow one fault handler registered for each device */
954 if (param->fault_param) {
960 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
961 if (!param->fault_param) {
966 param->fault_param->handler = handler;
967 param->fault_param->data = data;
968 mutex_init(¶m->fault_param->lock);
969 INIT_LIST_HEAD(¶m->fault_param->faults);
972 mutex_unlock(¶m->lock);
976 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
979 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
982 * Remove the device fault handler installed with
983 * iommu_register_device_fault_handler().
985 * Return 0 on success, or an error.
987 int iommu_unregister_device_fault_handler(struct device *dev)
989 struct iommu_param *param = dev->iommu_param;
995 mutex_lock(¶m->lock);
997 if (!param->fault_param)
1000 /* we cannot unregister handler if there are pending faults */
1001 if (!list_empty(¶m->fault_param->faults)) {
1006 kfree(param->fault_param);
1007 param->fault_param = NULL;
1010 mutex_unlock(¶m->lock);
1014 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1017 * iommu_report_device_fault() - Report fault event to device driver
1019 * @evt: fault event data
1021 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1022 * handler. When this function fails and the fault is recoverable, it is the
1023 * caller's responsibility to complete the fault.
1025 * Return 0 on success, or an error.
1027 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1029 struct iommu_param *param = dev->iommu_param;
1030 struct iommu_fault_event *evt_pending = NULL;
1031 struct iommu_fault_param *fparam;
1037 /* we only report device fault if there is a handler registered */
1038 mutex_lock(¶m->lock);
1039 fparam = param->fault_param;
1040 if (!fparam || !fparam->handler) {
1045 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1046 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1047 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1053 mutex_lock(&fparam->lock);
1054 list_add_tail(&evt_pending->list, &fparam->faults);
1055 mutex_unlock(&fparam->lock);
1058 ret = fparam->handler(&evt->fault, fparam->data);
1059 if (ret && evt_pending) {
1060 mutex_lock(&fparam->lock);
1061 list_del(&evt_pending->list);
1062 mutex_unlock(&fparam->lock);
1066 mutex_unlock(¶m->lock);
1069 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1071 int iommu_page_response(struct device *dev,
1072 struct iommu_page_response *msg)
1076 struct iommu_fault_event *evt;
1077 struct iommu_fault_page_request *prm;
1078 struct iommu_param *param = dev->iommu_param;
1079 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1081 if (!domain || !domain->ops->page_response)
1084 if (!param || !param->fault_param)
1087 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1088 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1091 /* Only send response if there is a fault report pending */
1092 mutex_lock(¶m->fault_param->lock);
1093 if (list_empty(¶m->fault_param->faults)) {
1094 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1098 * Check if we have a matching page request pending to respond,
1099 * otherwise return -EINVAL
1101 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1102 prm = &evt->fault.prm;
1103 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1105 if ((pasid_valid && prm->pasid != msg->pasid) ||
1106 prm->grpid != msg->grpid)
1109 /* Sanitize the reply */
1110 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1112 ret = domain->ops->page_response(dev, evt, msg);
1113 list_del(&evt->list);
1119 mutex_unlock(¶m->fault_param->lock);
1122 EXPORT_SYMBOL_GPL(iommu_page_response);
1125 * iommu_group_id - Return ID for a group
1126 * @group: the group to ID
1128 * Return the unique ID for the group matching the sysfs group number.
1130 int iommu_group_id(struct iommu_group *group)
1134 EXPORT_SYMBOL_GPL(iommu_group_id);
1136 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1137 unsigned long *devfns);
1140 * To consider a PCI device isolated, we require ACS to support Source
1141 * Validation, Request Redirection, Completer Redirection, and Upstream
1142 * Forwarding. This effectively means that devices cannot spoof their
1143 * requester ID, requests and completions cannot be redirected, and all
1144 * transactions are forwarded upstream, even as it passes through a
1145 * bridge where the target device is downstream.
1147 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1150 * For multifunction devices which are not isolated from each other, find
1151 * all the other non-isolated functions and look for existing groups. For
1152 * each function, we also need to look for aliases to or from other devices
1153 * that may already have a group.
1155 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1156 unsigned long *devfns)
1158 struct pci_dev *tmp = NULL;
1159 struct iommu_group *group;
1161 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1164 for_each_pci_dev(tmp) {
1165 if (tmp == pdev || tmp->bus != pdev->bus ||
1166 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1167 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1170 group = get_pci_alias_group(tmp, devfns);
1181 * Look for aliases to or from the given device for existing groups. DMA
1182 * aliases are only supported on the same bus, therefore the search
1183 * space is quite small (especially since we're really only looking at pcie
1184 * device, and therefore only expect multiple slots on the root complex or
1185 * downstream switch ports). It's conceivable though that a pair of
1186 * multifunction devices could have aliases between them that would cause a
1187 * loop. To prevent this, we use a bitmap to track where we've been.
1189 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1190 unsigned long *devfns)
1192 struct pci_dev *tmp = NULL;
1193 struct iommu_group *group;
1195 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1198 group = iommu_group_get(&pdev->dev);
1202 for_each_pci_dev(tmp) {
1203 if (tmp == pdev || tmp->bus != pdev->bus)
1206 /* We alias them or they alias us */
1207 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1208 group = get_pci_alias_group(tmp, devfns);
1214 group = get_pci_function_alias_group(tmp, devfns);
1225 struct group_for_pci_data {
1226 struct pci_dev *pdev;
1227 struct iommu_group *group;
1231 * DMA alias iterator callback, return the last seen device. Stop and return
1232 * the IOMMU group if we find one along the way.
1234 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1236 struct group_for_pci_data *data = opaque;
1239 data->group = iommu_group_get(&pdev->dev);
1241 return data->group != NULL;
1245 * Generic device_group call-back function. It just allocates one
1246 * iommu-group per device.
1248 struct iommu_group *generic_device_group(struct device *dev)
1250 return iommu_group_alloc();
1254 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1255 * to find or create an IOMMU group for a device.
1257 struct iommu_group *pci_device_group(struct device *dev)
1259 struct pci_dev *pdev = to_pci_dev(dev);
1260 struct group_for_pci_data data;
1261 struct pci_bus *bus;
1262 struct iommu_group *group = NULL;
1263 u64 devfns[4] = { 0 };
1265 if (WARN_ON(!dev_is_pci(dev)))
1266 return ERR_PTR(-EINVAL);
1269 * Find the upstream DMA alias for the device. A device must not
1270 * be aliased due to topology in order to have its own IOMMU group.
1271 * If we find an alias along the way that already belongs to a
1274 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1280 * Continue upstream from the point of minimum IOMMU granularity
1281 * due to aliases to the point where devices are protected from
1282 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1285 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1289 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1294 group = iommu_group_get(&pdev->dev);
1300 * Look for existing groups on device aliases. If we alias another
1301 * device or another device aliases us, use the same group.
1303 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1308 * Look for existing groups on non-isolated functions on the same
1309 * slot and aliases of those funcions, if any. No need to clear
1310 * the search bitmap, the tested devfns are still valid.
1312 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1316 /* No shared group found, allocate new */
1317 return iommu_group_alloc();
1320 /* Get the IOMMU group for device on fsl-mc bus */
1321 struct iommu_group *fsl_mc_device_group(struct device *dev)
1323 struct device *cont_dev = fsl_mc_cont_dev(dev);
1324 struct iommu_group *group;
1326 group = iommu_group_get(cont_dev);
1328 group = iommu_group_alloc();
1333 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1334 * @dev: target device
1336 * This function is intended to be called by IOMMU drivers and extended to
1337 * support common, bus-defined algorithms when determining or creating the
1338 * IOMMU group for a device. On success, the caller will hold a reference
1339 * to the returned IOMMU group, which will already include the provided
1340 * device. The reference should be released with iommu_group_put().
1342 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1344 const struct iommu_ops *ops = dev->bus->iommu_ops;
1345 struct iommu_group *group;
1348 group = iommu_group_get(dev);
1353 return ERR_PTR(-EINVAL);
1355 group = ops->device_group(dev);
1356 if (WARN_ON_ONCE(group == NULL))
1357 return ERR_PTR(-EINVAL);
1363 * Try to allocate a default domain - needs support from the
1366 if (!group->default_domain) {
1367 struct iommu_domain *dom;
1369 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1370 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1371 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1374 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1375 iommu_def_domain_type);
1379 group->default_domain = dom;
1381 group->domain = dom;
1383 if (dom && !iommu_dma_strict) {
1385 iommu_domain_set_attr(dom,
1386 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1391 ret = iommu_group_add_device(group, dev);
1393 iommu_group_put(group);
1394 return ERR_PTR(ret);
1400 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1402 return group->default_domain;
1405 static int add_iommu_group(struct device *dev, void *data)
1407 int ret = iommu_probe_device(dev);
1410 * We ignore -ENODEV errors for now, as they just mean that the
1411 * device is not translated by an IOMMU. We still care about
1412 * other errors and fail to initialize when they happen.
1420 static int remove_iommu_group(struct device *dev, void *data)
1422 iommu_release_device(dev);
1427 static int iommu_bus_notifier(struct notifier_block *nb,
1428 unsigned long action, void *data)
1430 unsigned long group_action = 0;
1431 struct device *dev = data;
1432 struct iommu_group *group;
1435 * ADD/DEL call into iommu driver ops if provided, which may
1436 * result in ADD/DEL notifiers to group->notifier
1438 if (action == BUS_NOTIFY_ADD_DEVICE) {
1441 ret = iommu_probe_device(dev);
1442 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1443 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1444 iommu_release_device(dev);
1449 * Remaining BUS_NOTIFYs get filtered and republished to the
1450 * group, if anyone is listening
1452 group = iommu_group_get(dev);
1457 case BUS_NOTIFY_BIND_DRIVER:
1458 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1460 case BUS_NOTIFY_BOUND_DRIVER:
1461 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1463 case BUS_NOTIFY_UNBIND_DRIVER:
1464 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1466 case BUS_NOTIFY_UNBOUND_DRIVER:
1467 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1472 blocking_notifier_call_chain(&group->notifier,
1475 iommu_group_put(group);
1479 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1482 struct notifier_block *nb;
1484 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1488 nb->notifier_call = iommu_bus_notifier;
1490 err = bus_register_notifier(bus, nb);
1494 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
1503 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1504 bus_unregister_notifier(bus, nb);
1513 * bus_set_iommu - set iommu-callbacks for the bus
1515 * @ops: the callbacks provided by the iommu-driver
1517 * This function is called by an iommu driver to set the iommu methods
1518 * used for a particular bus. Drivers for devices on that bus can use
1519 * the iommu-api after these ops are registered.
1520 * This special function is needed because IOMMUs are usually devices on
1521 * the bus itself, so the iommu drivers are not initialized when the bus
1522 * is set up. With this function the iommu-driver can set the iommu-ops
1525 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1529 if (bus->iommu_ops != NULL)
1532 bus->iommu_ops = ops;
1534 /* Do IOMMU specific setup for this bus-type */
1535 err = iommu_bus_init(bus, ops);
1537 bus->iommu_ops = NULL;
1541 EXPORT_SYMBOL_GPL(bus_set_iommu);
1543 bool iommu_present(struct bus_type *bus)
1545 return bus->iommu_ops != NULL;
1547 EXPORT_SYMBOL_GPL(iommu_present);
1549 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1551 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1554 return bus->iommu_ops->capable(cap);
1556 EXPORT_SYMBOL_GPL(iommu_capable);
1559 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1560 * @domain: iommu domain
1561 * @handler: fault handler
1562 * @token: user data, will be passed back to the fault handler
1564 * This function should be used by IOMMU users which want to be notified
1565 * whenever an IOMMU fault happens.
1567 * The fault handler itself should return 0 on success, and an appropriate
1568 * error code otherwise.
1570 void iommu_set_fault_handler(struct iommu_domain *domain,
1571 iommu_fault_handler_t handler,
1576 domain->handler = handler;
1577 domain->handler_token = token;
1579 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1581 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1584 struct iommu_domain *domain;
1586 if (bus == NULL || bus->iommu_ops == NULL)
1589 domain = bus->iommu_ops->domain_alloc(type);
1593 domain->ops = bus->iommu_ops;
1594 domain->type = type;
1595 /* Assume all sizes by default; the driver may override this later */
1596 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1601 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1603 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1605 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1607 void iommu_domain_free(struct iommu_domain *domain)
1609 domain->ops->domain_free(domain);
1611 EXPORT_SYMBOL_GPL(iommu_domain_free);
1613 static int __iommu_attach_device(struct iommu_domain *domain,
1617 if ((domain->ops->is_attach_deferred != NULL) &&
1618 domain->ops->is_attach_deferred(domain, dev))
1621 if (unlikely(domain->ops->attach_dev == NULL))
1624 ret = domain->ops->attach_dev(domain, dev);
1626 trace_attach_device_to_domain(dev);
1630 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1632 struct iommu_group *group;
1635 group = iommu_group_get(dev);
1640 * Lock the group to make sure the device-count doesn't
1641 * change while we are attaching
1643 mutex_lock(&group->mutex);
1645 if (iommu_group_device_count(group) != 1)
1648 ret = __iommu_attach_group(domain, group);
1651 mutex_unlock(&group->mutex);
1652 iommu_group_put(group);
1656 EXPORT_SYMBOL_GPL(iommu_attach_device);
1658 static void __iommu_detach_device(struct iommu_domain *domain,
1661 if ((domain->ops->is_attach_deferred != NULL) &&
1662 domain->ops->is_attach_deferred(domain, dev))
1665 if (unlikely(domain->ops->detach_dev == NULL))
1668 domain->ops->detach_dev(domain, dev);
1669 trace_detach_device_from_domain(dev);
1672 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1674 struct iommu_group *group;
1676 group = iommu_group_get(dev);
1680 mutex_lock(&group->mutex);
1681 if (iommu_group_device_count(group) != 1) {
1686 __iommu_detach_group(domain, group);
1689 mutex_unlock(&group->mutex);
1690 iommu_group_put(group);
1692 EXPORT_SYMBOL_GPL(iommu_detach_device);
1694 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1696 struct iommu_domain *domain;
1697 struct iommu_group *group;
1699 group = iommu_group_get(dev);
1703 domain = group->domain;
1705 iommu_group_put(group);
1709 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1712 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1713 * guarantees that the group and its default domain are valid and correct.
1715 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1717 return dev->iommu_group->default_domain;
1721 * IOMMU groups are really the natural working unit of the IOMMU, but
1722 * the IOMMU API works on domains and devices. Bridge that gap by
1723 * iterating over the devices in a group. Ideally we'd have a single
1724 * device which represents the requestor ID of the group, but we also
1725 * allow IOMMU drivers to create policy defined minimum sets, where
1726 * the physical hardware may be able to distiguish members, but we
1727 * wish to group them at a higher level (ex. untrusted multi-function
1728 * PCI devices). Thus we attach each device.
1730 static int iommu_group_do_attach_device(struct device *dev, void *data)
1732 struct iommu_domain *domain = data;
1734 return __iommu_attach_device(domain, dev);
1737 static int __iommu_attach_group(struct iommu_domain *domain,
1738 struct iommu_group *group)
1742 if (group->default_domain && group->domain != group->default_domain)
1745 ret = __iommu_group_for_each_dev(group, domain,
1746 iommu_group_do_attach_device);
1748 group->domain = domain;
1753 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1757 mutex_lock(&group->mutex);
1758 ret = __iommu_attach_group(domain, group);
1759 mutex_unlock(&group->mutex);
1763 EXPORT_SYMBOL_GPL(iommu_attach_group);
1765 static int iommu_group_do_detach_device(struct device *dev, void *data)
1767 struct iommu_domain *domain = data;
1769 __iommu_detach_device(domain, dev);
1774 static void __iommu_detach_group(struct iommu_domain *domain,
1775 struct iommu_group *group)
1779 if (!group->default_domain) {
1780 __iommu_group_for_each_dev(group, domain,
1781 iommu_group_do_detach_device);
1782 group->domain = NULL;
1786 if (group->domain == group->default_domain)
1789 /* Detach by re-attaching to the default domain */
1790 ret = __iommu_group_for_each_dev(group, group->default_domain,
1791 iommu_group_do_attach_device);
1795 group->domain = group->default_domain;
1798 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1800 mutex_lock(&group->mutex);
1801 __iommu_detach_group(domain, group);
1802 mutex_unlock(&group->mutex);
1804 EXPORT_SYMBOL_GPL(iommu_detach_group);
1806 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1808 if (unlikely(domain->ops->iova_to_phys == NULL))
1811 return domain->ops->iova_to_phys(domain, iova);
1813 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1815 static size_t iommu_pgsize(struct iommu_domain *domain,
1816 unsigned long addr_merge, size_t size)
1818 unsigned int pgsize_idx;
1821 /* Max page size that still fits into 'size' */
1822 pgsize_idx = __fls(size);
1824 /* need to consider alignment requirements ? */
1825 if (likely(addr_merge)) {
1826 /* Max page size allowed by address */
1827 unsigned int align_pgsize_idx = __ffs(addr_merge);
1828 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1831 /* build a mask of acceptable page sizes */
1832 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1834 /* throw away page sizes not supported by the hardware */
1835 pgsize &= domain->pgsize_bitmap;
1837 /* make sure we're still sane */
1840 /* pick the biggest page */
1841 pgsize_idx = __fls(pgsize);
1842 pgsize = 1UL << pgsize_idx;
1847 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1848 phys_addr_t paddr, size_t size, int prot)
1850 const struct iommu_ops *ops = domain->ops;
1851 unsigned long orig_iova = iova;
1852 unsigned int min_pagesz;
1853 size_t orig_size = size;
1854 phys_addr_t orig_paddr = paddr;
1857 if (unlikely(ops->map == NULL ||
1858 domain->pgsize_bitmap == 0UL))
1861 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1864 /* find out the minimum page size supported */
1865 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1868 * both the virtual address and the physical one, as well as
1869 * the size of the mapping, must be aligned (at least) to the
1870 * size of the smallest page supported by the hardware
1872 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1873 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1874 iova, &paddr, size, min_pagesz);
1878 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1881 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1883 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1884 iova, &paddr, pgsize);
1886 ret = ops->map(domain, iova, paddr, pgsize, prot);
1895 if (ops->iotlb_sync_map)
1896 ops->iotlb_sync_map(domain);
1898 /* unroll mapping in case something went wrong */
1900 iommu_unmap(domain, orig_iova, orig_size - size);
1902 trace_map(orig_iova, orig_paddr, orig_size);
1906 EXPORT_SYMBOL_GPL(iommu_map);
1908 static size_t __iommu_unmap(struct iommu_domain *domain,
1909 unsigned long iova, size_t size,
1910 struct iommu_iotlb_gather *iotlb_gather)
1912 const struct iommu_ops *ops = domain->ops;
1913 size_t unmapped_page, unmapped = 0;
1914 unsigned long orig_iova = iova;
1915 unsigned int min_pagesz;
1917 if (unlikely(ops->unmap == NULL ||
1918 domain->pgsize_bitmap == 0UL))
1921 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1924 /* find out the minimum page size supported */
1925 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1928 * The virtual address, as well as the size of the mapping, must be
1929 * aligned (at least) to the size of the smallest page supported
1932 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1933 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1934 iova, size, min_pagesz);
1938 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1941 * Keep iterating until we either unmap 'size' bytes (or more)
1942 * or we hit an area that isn't mapped.
1944 while (unmapped < size) {
1945 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1947 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
1951 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1952 iova, unmapped_page);
1954 iova += unmapped_page;
1955 unmapped += unmapped_page;
1958 trace_unmap(orig_iova, size, unmapped);
1962 size_t iommu_unmap(struct iommu_domain *domain,
1963 unsigned long iova, size_t size)
1965 struct iommu_iotlb_gather iotlb_gather;
1968 iommu_iotlb_gather_init(&iotlb_gather);
1969 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
1970 iommu_tlb_sync(domain, &iotlb_gather);
1974 EXPORT_SYMBOL_GPL(iommu_unmap);
1976 size_t iommu_unmap_fast(struct iommu_domain *domain,
1977 unsigned long iova, size_t size,
1978 struct iommu_iotlb_gather *iotlb_gather)
1980 return __iommu_unmap(domain, iova, size, iotlb_gather);
1982 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1984 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1985 struct scatterlist *sg, unsigned int nents, int prot)
1987 size_t len = 0, mapped = 0;
1992 while (i <= nents) {
1993 phys_addr_t s_phys = sg_phys(sg);
1995 if (len && s_phys != start + len) {
1996 ret = iommu_map(domain, iova + mapped, start, len, prot);
2018 /* undo mappings already done */
2019 iommu_unmap(domain, iova, mapped);
2024 EXPORT_SYMBOL_GPL(iommu_map_sg);
2026 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
2027 phys_addr_t paddr, u64 size, int prot)
2029 if (unlikely(domain->ops->domain_window_enable == NULL))
2032 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2035 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2037 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2039 if (unlikely(domain->ops->domain_window_disable == NULL))
2042 return domain->ops->domain_window_disable(domain, wnd_nr);
2044 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2047 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2048 * @domain: the iommu domain where the fault has happened
2049 * @dev: the device where the fault has happened
2050 * @iova: the faulting address
2051 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2053 * This function should be called by the low-level IOMMU implementations
2054 * whenever IOMMU faults happen, to allow high-level users, that are
2055 * interested in such events, to know about them.
2057 * This event may be useful for several possible use cases:
2058 * - mere logging of the event
2059 * - dynamic TLB/PTE loading
2060 * - if restarting of the faulting device is required
2062 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2063 * PTE/TLB loading will one day be supported, implementations will be able
2064 * to tell whether it succeeded or not according to this return value).
2066 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2067 * (though fault handlers can also return -ENOSYS, in case they want to
2068 * elicit the default behavior of the IOMMU drivers).
2070 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2071 unsigned long iova, int flags)
2076 * if upper layers showed interest and installed a fault handler,
2079 if (domain->handler)
2080 ret = domain->handler(domain, dev, iova, flags,
2081 domain->handler_token);
2083 trace_io_page_fault(dev, iova, flags);
2086 EXPORT_SYMBOL_GPL(report_iommu_fault);
2088 static int __init iommu_init(void)
2090 iommu_group_kset = kset_create_and_add("iommu_groups",
2092 BUG_ON(!iommu_group_kset);
2094 iommu_debugfs_setup();
2098 core_initcall(iommu_init);
2100 int iommu_domain_get_attr(struct iommu_domain *domain,
2101 enum iommu_attr attr, void *data)
2103 struct iommu_domain_geometry *geometry;
2108 case DOMAIN_ATTR_GEOMETRY:
2110 *geometry = domain->geometry;
2113 case DOMAIN_ATTR_PAGING:
2115 *paging = (domain->pgsize_bitmap != 0UL);
2118 if (!domain->ops->domain_get_attr)
2121 ret = domain->ops->domain_get_attr(domain, attr, data);
2126 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2128 int iommu_domain_set_attr(struct iommu_domain *domain,
2129 enum iommu_attr attr, void *data)
2135 if (domain->ops->domain_set_attr == NULL)
2138 ret = domain->ops->domain_set_attr(domain, attr, data);
2143 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
2145 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2147 const struct iommu_ops *ops = dev->bus->iommu_ops;
2149 if (ops && ops->get_resv_regions)
2150 ops->get_resv_regions(dev, list);
2153 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2155 const struct iommu_ops *ops = dev->bus->iommu_ops;
2157 if (ops && ops->put_resv_regions)
2158 ops->put_resv_regions(dev, list);
2161 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2162 size_t length, int prot,
2163 enum iommu_resv_type type)
2165 struct iommu_resv_region *region;
2167 region = kzalloc(sizeof(*region), GFP_KERNEL);
2171 INIT_LIST_HEAD(®ion->list);
2172 region->start = start;
2173 region->length = length;
2174 region->prot = prot;
2175 region->type = type;
2180 request_default_domain_for_dev(struct device *dev, unsigned long type)
2182 struct iommu_domain *domain;
2183 struct iommu_group *group;
2186 /* Device must already be in a group before calling this function */
2187 group = iommu_group_get(dev);
2191 mutex_lock(&group->mutex);
2193 /* Check if the default domain is already direct mapped */
2195 if (group->default_domain && group->default_domain->type == type)
2198 /* Don't change mappings of existing devices */
2200 if (iommu_group_device_count(group) != 1)
2203 /* Allocate a direct mapped domain */
2205 domain = __iommu_domain_alloc(dev->bus, type);
2209 /* Attach the device to the domain */
2210 ret = __iommu_attach_group(domain, group);
2212 iommu_domain_free(domain);
2216 iommu_group_create_direct_mappings(group, dev);
2218 /* Make the direct mapped domain the default for this group */
2219 if (group->default_domain)
2220 iommu_domain_free(group->default_domain);
2221 group->default_domain = domain;
2223 dev_info(dev, "Using iommu %s mapping\n",
2224 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
2228 mutex_unlock(&group->mutex);
2229 iommu_group_put(group);
2234 /* Request that a device is direct mapped by the IOMMU */
2235 int iommu_request_dm_for_dev(struct device *dev)
2237 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2240 /* Request that a device can't be direct mapped by the IOMMU */
2241 int iommu_request_dma_domain_for_dev(struct device *dev)
2243 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2246 void iommu_set_default_passthrough(bool cmd_line)
2249 iommu_set_cmd_line_dma_api();
2251 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2254 void iommu_set_default_translated(bool cmd_line)
2257 iommu_set_cmd_line_dma_api();
2259 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2262 bool iommu_default_passthrough(void)
2264 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2266 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2268 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2270 const struct iommu_ops *ops = NULL;
2271 struct iommu_device *iommu;
2273 spin_lock(&iommu_device_lock);
2274 list_for_each_entry(iommu, &iommu_device_list, list)
2275 if (iommu->fwnode == fwnode) {
2279 spin_unlock(&iommu_device_lock);
2283 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2284 const struct iommu_ops *ops)
2286 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2289 return ops == fwspec->ops ? 0 : -EINVAL;
2291 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2295 of_node_get(to_of_node(iommu_fwnode));
2296 fwspec->iommu_fwnode = iommu_fwnode;
2298 dev_iommu_fwspec_set(dev, fwspec);
2301 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2303 void iommu_fwspec_free(struct device *dev)
2305 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2308 fwnode_handle_put(fwspec->iommu_fwnode);
2310 dev_iommu_fwspec_set(dev, NULL);
2313 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2315 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2317 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2324 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2325 if (size > sizeof(*fwspec)) {
2326 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2330 dev_iommu_fwspec_set(dev, fwspec);
2333 for (i = 0; i < num_ids; i++)
2334 fwspec->ids[fwspec->num_ids + i] = ids[i];
2336 fwspec->num_ids += num_ids;
2339 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2342 * Per device IOMMU features.
2344 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2346 const struct iommu_ops *ops = dev->bus->iommu_ops;
2348 if (ops && ops->dev_has_feat)
2349 return ops->dev_has_feat(dev, feat);
2353 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2355 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2357 const struct iommu_ops *ops = dev->bus->iommu_ops;
2359 if (ops && ops->dev_enable_feat)
2360 return ops->dev_enable_feat(dev, feat);
2364 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2367 * The device drivers should do the necessary cleanups before calling this.
2368 * For example, before disabling the aux-domain feature, the device driver
2369 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2371 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2373 const struct iommu_ops *ops = dev->bus->iommu_ops;
2375 if (ops && ops->dev_disable_feat)
2376 return ops->dev_disable_feat(dev, feat);
2380 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2382 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2384 const struct iommu_ops *ops = dev->bus->iommu_ops;
2386 if (ops && ops->dev_feat_enabled)
2387 return ops->dev_feat_enabled(dev, feat);
2391 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2394 * Aux-domain specific attach/detach.
2396 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2397 * true. Also, as long as domains are attached to a device through this
2398 * interface, any tries to call iommu_attach_device() should fail
2399 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2400 * This should make us safe against a device being attached to a guest as a
2401 * whole while there are still pasid users on it (aux and sva).
2403 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2407 if (domain->ops->aux_attach_dev)
2408 ret = domain->ops->aux_attach_dev(domain, dev);
2411 trace_attach_device_to_domain(dev);
2415 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2417 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2419 if (domain->ops->aux_detach_dev) {
2420 domain->ops->aux_detach_dev(domain, dev);
2421 trace_detach_device_from_domain(dev);
2424 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2426 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2430 if (domain->ops->aux_get_pasid)
2431 ret = domain->ops->aux_get_pasid(domain, dev);
2435 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2438 * iommu_sva_bind_device() - Bind a process address space to a device
2440 * @mm: the mm to bind, caller must hold a reference to it
2442 * Create a bond between device and address space, allowing the device to access
2443 * the mm using the returned PASID. If a bond already exists between @device and
2444 * @mm, it is returned and an additional reference is taken. Caller must call
2445 * iommu_sva_unbind_device() to release each reference.
2447 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2448 * initialize the required SVA features.
2450 * On error, returns an ERR_PTR value.
2453 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2455 struct iommu_group *group;
2456 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2457 const struct iommu_ops *ops = dev->bus->iommu_ops;
2459 if (!ops || !ops->sva_bind)
2460 return ERR_PTR(-ENODEV);
2462 group = iommu_group_get(dev);
2464 return ERR_PTR(-ENODEV);
2466 /* Ensure device count and domain don't change while we're binding */
2467 mutex_lock(&group->mutex);
2470 * To keep things simple, SVA currently doesn't support IOMMU groups
2471 * with more than one device. Existing SVA-capable systems are not
2472 * affected by the problems that required IOMMU groups (lack of ACS
2473 * isolation, device ID aliasing and other hardware issues).
2475 if (iommu_group_device_count(group) != 1)
2478 handle = ops->sva_bind(dev, mm, drvdata);
2481 mutex_unlock(&group->mutex);
2482 iommu_group_put(group);
2486 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2489 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2490 * @handle: the handle returned by iommu_sva_bind_device()
2492 * Put reference to a bond between device and address space. The device should
2493 * not be issuing any more transaction for this PASID. All outstanding page
2494 * requests for this PASID must have been flushed to the IOMMU.
2496 * Returns 0 on success, or an error value
2498 void iommu_sva_unbind_device(struct iommu_sva *handle)
2500 struct iommu_group *group;
2501 struct device *dev = handle->dev;
2502 const struct iommu_ops *ops = dev->bus->iommu_ops;
2504 if (!ops || !ops->sva_unbind)
2507 group = iommu_group_get(dev);
2511 mutex_lock(&group->mutex);
2512 ops->sva_unbind(handle);
2513 mutex_unlock(&group->mutex);
2515 iommu_group_put(group);
2517 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2519 int iommu_sva_set_ops(struct iommu_sva *handle,
2520 const struct iommu_sva_ops *sva_ops)
2522 if (handle->ops && handle->ops != sva_ops)
2525 handle->ops = sva_ops;
2528 EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2530 int iommu_sva_get_pasid(struct iommu_sva *handle)
2532 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2534 if (!ops || !ops->sva_get_pasid)
2535 return IOMMU_PASID_INVALID;
2537 return ops->sva_get_pasid(handle);
2539 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);