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);
30 static unsigned int iommu_def_domain_type __read_mostly;
31 static bool iommu_dma_strict __read_mostly = true;
32 static u32 iommu_cmd_line __read_mostly;
36 struct kobject *devices_kobj;
37 struct list_head devices;
39 struct blocking_notifier_head notifier;
41 void (*iommu_data_release)(void *iommu_data);
44 struct iommu_domain *default_domain;
45 struct iommu_domain *domain;
49 struct list_head list;
54 struct iommu_group_attribute {
55 struct attribute attr;
56 ssize_t (*show)(struct iommu_group *group, char *buf);
57 ssize_t (*store)(struct iommu_group *group,
58 const char *buf, size_t count);
61 static const char * const iommu_group_resv_type_string[] = {
62 [IOMMU_RESV_DIRECT] = "direct",
63 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
64 [IOMMU_RESV_RESERVED] = "reserved",
65 [IOMMU_RESV_MSI] = "msi",
66 [IOMMU_RESV_SW_MSI] = "msi",
69 #define IOMMU_CMD_LINE_DMA_API BIT(0)
71 static void iommu_set_cmd_line_dma_api(void)
73 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
76 static bool iommu_cmd_line_dma_api(void)
78 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
81 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
82 struct iommu_group_attribute iommu_group_attr_##_name = \
83 __ATTR(_name, _mode, _show, _store)
85 #define to_iommu_group_attr(_attr) \
86 container_of(_attr, struct iommu_group_attribute, attr)
87 #define to_iommu_group(_kobj) \
88 container_of(_kobj, struct iommu_group, kobj)
90 static LIST_HEAD(iommu_device_list);
91 static DEFINE_SPINLOCK(iommu_device_lock);
94 * Use a function instead of an array here because the domain-type is a
95 * bit-field, so an array would waste memory.
97 static const char *iommu_domain_type_str(unsigned int t)
100 case IOMMU_DOMAIN_BLOCKED:
102 case IOMMU_DOMAIN_IDENTITY:
103 return "Passthrough";
104 case IOMMU_DOMAIN_UNMANAGED:
106 case IOMMU_DOMAIN_DMA:
113 static int __init iommu_subsys_init(void)
115 bool cmd_line = iommu_cmd_line_dma_api();
118 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
119 iommu_set_default_passthrough(false);
121 iommu_set_default_translated(false);
124 pr_info("Default domain type: %s %s\n",
125 iommu_domain_type_str(iommu_def_domain_type),
126 cmd_line ? "(set via kernel command line)" : "");
130 subsys_initcall(iommu_subsys_init);
132 int iommu_device_register(struct iommu_device *iommu)
134 spin_lock(&iommu_device_lock);
135 list_add_tail(&iommu->list, &iommu_device_list);
136 spin_unlock(&iommu_device_lock);
140 void iommu_device_unregister(struct iommu_device *iommu)
142 spin_lock(&iommu_device_lock);
143 list_del(&iommu->list);
144 spin_unlock(&iommu_device_lock);
147 static struct iommu_param *iommu_get_dev_param(struct device *dev)
149 struct iommu_param *param = dev->iommu_param;
154 param = kzalloc(sizeof(*param), GFP_KERNEL);
158 mutex_init(¶m->lock);
159 dev->iommu_param = param;
163 static void iommu_free_dev_param(struct device *dev)
165 kfree(dev->iommu_param);
166 dev->iommu_param = NULL;
169 int iommu_probe_device(struct device *dev)
171 const struct iommu_ops *ops = dev->bus->iommu_ops;
174 WARN_ON(dev->iommu_group);
178 if (!iommu_get_dev_param(dev))
181 ret = ops->add_device(dev);
183 iommu_free_dev_param(dev);
188 void iommu_release_device(struct device *dev)
190 const struct iommu_ops *ops = dev->bus->iommu_ops;
192 if (dev->iommu_group)
193 ops->remove_device(dev);
195 iommu_free_dev_param(dev);
198 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
200 static int __iommu_attach_device(struct iommu_domain *domain,
202 static int __iommu_attach_group(struct iommu_domain *domain,
203 struct iommu_group *group);
204 static void __iommu_detach_group(struct iommu_domain *domain,
205 struct iommu_group *group);
207 static int __init iommu_set_def_domain_type(char *str)
212 ret = kstrtobool(str, &pt);
217 iommu_set_default_passthrough(true);
219 iommu_set_default_translated(true);
223 early_param("iommu.passthrough", iommu_set_def_domain_type);
225 static int __init iommu_dma_setup(char *str)
227 return kstrtobool(str, &iommu_dma_strict);
229 early_param("iommu.strict", iommu_dma_setup);
231 static ssize_t iommu_group_attr_show(struct kobject *kobj,
232 struct attribute *__attr, char *buf)
234 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
235 struct iommu_group *group = to_iommu_group(kobj);
239 ret = attr->show(group, buf);
243 static ssize_t iommu_group_attr_store(struct kobject *kobj,
244 struct attribute *__attr,
245 const char *buf, size_t count)
247 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
248 struct iommu_group *group = to_iommu_group(kobj);
252 ret = attr->store(group, buf, count);
256 static const struct sysfs_ops iommu_group_sysfs_ops = {
257 .show = iommu_group_attr_show,
258 .store = iommu_group_attr_store,
261 static int iommu_group_create_file(struct iommu_group *group,
262 struct iommu_group_attribute *attr)
264 return sysfs_create_file(&group->kobj, &attr->attr);
267 static void iommu_group_remove_file(struct iommu_group *group,
268 struct iommu_group_attribute *attr)
270 sysfs_remove_file(&group->kobj, &attr->attr);
273 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
275 return sprintf(buf, "%s\n", group->name);
279 * iommu_insert_resv_region - Insert a new region in the
280 * list of reserved regions.
281 * @new: new region to insert
282 * @regions: list of regions
284 * The new element is sorted by address with respect to the other
285 * regions of the same type. In case it overlaps with another
286 * region of the same type, regions are merged. In case it
287 * overlaps with another region of different type, regions are
290 static int iommu_insert_resv_region(struct iommu_resv_region *new,
291 struct list_head *regions)
293 struct iommu_resv_region *region;
294 phys_addr_t start = new->start;
295 phys_addr_t end = new->start + new->length - 1;
296 struct list_head *pos = regions->next;
298 while (pos != regions) {
299 struct iommu_resv_region *entry =
300 list_entry(pos, struct iommu_resv_region, list);
301 phys_addr_t a = entry->start;
302 phys_addr_t b = entry->start + entry->length - 1;
303 int type = entry->type;
307 } else if (start > b) {
309 } else if ((start >= a) && (end <= b)) {
310 if (new->type == type)
315 if (new->type == type) {
316 phys_addr_t new_start = min(a, start);
317 phys_addr_t new_end = max(b, end);
320 list_del(&entry->list);
321 entry->start = new_start;
322 entry->length = new_end - new_start + 1;
323 ret = iommu_insert_resv_region(entry, regions);
332 region = iommu_alloc_resv_region(new->start, new->length,
333 new->prot, new->type);
337 list_add_tail(®ion->list, pos);
342 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
343 struct list_head *group_resv_regions)
345 struct iommu_resv_region *entry;
348 list_for_each_entry(entry, dev_resv_regions, list) {
349 ret = iommu_insert_resv_region(entry, group_resv_regions);
356 int iommu_get_group_resv_regions(struct iommu_group *group,
357 struct list_head *head)
359 struct group_device *device;
362 mutex_lock(&group->mutex);
363 list_for_each_entry(device, &group->devices, list) {
364 struct list_head dev_resv_regions;
366 INIT_LIST_HEAD(&dev_resv_regions);
367 iommu_get_resv_regions(device->dev, &dev_resv_regions);
368 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
369 iommu_put_resv_regions(device->dev, &dev_resv_regions);
373 mutex_unlock(&group->mutex);
376 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
378 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
381 struct iommu_resv_region *region, *next;
382 struct list_head group_resv_regions;
385 INIT_LIST_HEAD(&group_resv_regions);
386 iommu_get_group_resv_regions(group, &group_resv_regions);
388 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
389 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
390 (long long int)region->start,
391 (long long int)(region->start +
393 iommu_group_resv_type_string[region->type]);
400 static ssize_t iommu_group_show_type(struct iommu_group *group,
403 char *type = "unknown\n";
405 if (group->default_domain) {
406 switch (group->default_domain->type) {
407 case IOMMU_DOMAIN_BLOCKED:
410 case IOMMU_DOMAIN_IDENTITY:
413 case IOMMU_DOMAIN_UNMANAGED:
414 type = "unmanaged\n";
416 case IOMMU_DOMAIN_DMA:
426 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
428 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
429 iommu_group_show_resv_regions, NULL);
431 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
433 static void iommu_group_release(struct kobject *kobj)
435 struct iommu_group *group = to_iommu_group(kobj);
437 pr_debug("Releasing group %d\n", group->id);
439 if (group->iommu_data_release)
440 group->iommu_data_release(group->iommu_data);
442 ida_simple_remove(&iommu_group_ida, group->id);
444 if (group->default_domain)
445 iommu_domain_free(group->default_domain);
451 static struct kobj_type iommu_group_ktype = {
452 .sysfs_ops = &iommu_group_sysfs_ops,
453 .release = iommu_group_release,
457 * iommu_group_alloc - Allocate a new group
459 * This function is called by an iommu driver to allocate a new iommu
460 * group. The iommu group represents the minimum granularity of the iommu.
461 * Upon successful return, the caller holds a reference to the supplied
462 * group in order to hold the group until devices are added. Use
463 * iommu_group_put() to release this extra reference count, allowing the
464 * group to be automatically reclaimed once it has no devices or external
467 struct iommu_group *iommu_group_alloc(void)
469 struct iommu_group *group;
472 group = kzalloc(sizeof(*group), GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
476 group->kobj.kset = iommu_group_kset;
477 mutex_init(&group->mutex);
478 INIT_LIST_HEAD(&group->devices);
479 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
481 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
488 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
489 NULL, "%d", group->id);
491 ida_simple_remove(&iommu_group_ida, group->id);
496 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
497 if (!group->devices_kobj) {
498 kobject_put(&group->kobj); /* triggers .release & free */
499 return ERR_PTR(-ENOMEM);
503 * The devices_kobj holds a reference on the group kobject, so
504 * as long as that exists so will the group. We can therefore
505 * use the devices_kobj for reference counting.
507 kobject_put(&group->kobj);
509 ret = iommu_group_create_file(group,
510 &iommu_group_attr_reserved_regions);
514 ret = iommu_group_create_file(group, &iommu_group_attr_type);
518 pr_debug("Allocated group %d\n", group->id);
522 EXPORT_SYMBOL_GPL(iommu_group_alloc);
524 struct iommu_group *iommu_group_get_by_id(int id)
526 struct kobject *group_kobj;
527 struct iommu_group *group;
530 if (!iommu_group_kset)
533 name = kasprintf(GFP_KERNEL, "%d", id);
537 group_kobj = kset_find_obj(iommu_group_kset, name);
543 group = container_of(group_kobj, struct iommu_group, kobj);
544 BUG_ON(group->id != id);
546 kobject_get(group->devices_kobj);
547 kobject_put(&group->kobj);
551 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
554 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
557 * iommu drivers can store data in the group for use when doing iommu
558 * operations. This function provides a way to retrieve it. Caller
559 * should hold a group reference.
561 void *iommu_group_get_iommudata(struct iommu_group *group)
563 return group->iommu_data;
565 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
568 * iommu_group_set_iommudata - set iommu_data for a group
570 * @iommu_data: new data
571 * @release: release function for iommu_data
573 * iommu drivers can store data in the group for use when doing iommu
574 * operations. This function provides a way to set the data after
575 * the group has been allocated. Caller should hold a group reference.
577 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
578 void (*release)(void *iommu_data))
580 group->iommu_data = iommu_data;
581 group->iommu_data_release = release;
583 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
586 * iommu_group_set_name - set name for a group
590 * Allow iommu driver to set a name for a group. When set it will
591 * appear in a name attribute file under the group in sysfs.
593 int iommu_group_set_name(struct iommu_group *group, const char *name)
598 iommu_group_remove_file(group, &iommu_group_attr_name);
605 group->name = kstrdup(name, GFP_KERNEL);
609 ret = iommu_group_create_file(group, &iommu_group_attr_name);
618 EXPORT_SYMBOL_GPL(iommu_group_set_name);
620 static int iommu_group_create_direct_mappings(struct iommu_group *group,
623 struct iommu_domain *domain = group->default_domain;
624 struct iommu_resv_region *entry;
625 struct list_head mappings;
626 unsigned long pg_size;
629 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
632 BUG_ON(!domain->pgsize_bitmap);
634 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
635 INIT_LIST_HEAD(&mappings);
637 iommu_get_resv_regions(dev, &mappings);
639 /* We need to consider overlapping regions for different devices */
640 list_for_each_entry(entry, &mappings, list) {
641 dma_addr_t start, end, addr;
643 if (domain->ops->apply_resv_region)
644 domain->ops->apply_resv_region(dev, domain, entry);
646 start = ALIGN(entry->start, pg_size);
647 end = ALIGN(entry->start + entry->length, pg_size);
649 if (entry->type != IOMMU_RESV_DIRECT &&
650 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
653 for (addr = start; addr < end; addr += pg_size) {
654 phys_addr_t phys_addr;
656 phys_addr = iommu_iova_to_phys(domain, addr);
660 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
667 iommu_flush_tlb_all(domain);
670 iommu_put_resv_regions(dev, &mappings);
676 * iommu_group_add_device - add a device to an iommu group
677 * @group: the group into which to add the device (reference should be held)
680 * This function is called by an iommu driver to add a device into a
681 * group. Adding a device increments the group reference count.
683 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
686 struct group_device *device;
688 device = kzalloc(sizeof(*device), GFP_KERNEL);
694 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
696 goto err_free_device;
698 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
702 goto err_remove_link;
705 ret = sysfs_create_link_nowarn(group->devices_kobj,
706 &dev->kobj, device->name);
708 if (ret == -EEXIST && i >= 0) {
710 * Account for the slim chance of collision
711 * and append an instance to the name.
714 device->name = kasprintf(GFP_KERNEL, "%s.%d",
715 kobject_name(&dev->kobj), i++);
721 kobject_get(group->devices_kobj);
723 dev->iommu_group = group;
725 iommu_group_create_direct_mappings(group, dev);
727 mutex_lock(&group->mutex);
728 list_add_tail(&device->list, &group->devices);
730 ret = __iommu_attach_device(group->domain, dev);
731 mutex_unlock(&group->mutex);
735 /* Notify any listeners about change to group. */
736 blocking_notifier_call_chain(&group->notifier,
737 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
739 trace_add_device_to_group(group->id, dev);
741 dev_info(dev, "Adding to iommu group %d\n", group->id);
746 mutex_lock(&group->mutex);
747 list_del(&device->list);
748 mutex_unlock(&group->mutex);
749 dev->iommu_group = NULL;
750 kobject_put(group->devices_kobj);
754 sysfs_remove_link(&dev->kobj, "iommu_group");
757 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
760 EXPORT_SYMBOL_GPL(iommu_group_add_device);
763 * iommu_group_remove_device - remove a device from it's current group
764 * @dev: device to be removed
766 * This function is called by an iommu driver to remove the device from
767 * it's current group. This decrements the iommu group reference count.
769 void iommu_group_remove_device(struct device *dev)
771 struct iommu_group *group = dev->iommu_group;
772 struct group_device *tmp_device, *device = NULL;
774 dev_info(dev, "Removing from iommu group %d\n", group->id);
776 /* Pre-notify listeners that a device is being removed. */
777 blocking_notifier_call_chain(&group->notifier,
778 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
780 mutex_lock(&group->mutex);
781 list_for_each_entry(tmp_device, &group->devices, list) {
782 if (tmp_device->dev == dev) {
784 list_del(&device->list);
788 mutex_unlock(&group->mutex);
793 sysfs_remove_link(group->devices_kobj, device->name);
794 sysfs_remove_link(&dev->kobj, "iommu_group");
796 trace_remove_device_from_group(group->id, dev);
800 dev->iommu_group = NULL;
801 kobject_put(group->devices_kobj);
803 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
805 static int iommu_group_device_count(struct iommu_group *group)
807 struct group_device *entry;
810 list_for_each_entry(entry, &group->devices, list)
817 * iommu_group_for_each_dev - iterate over each device in the group
819 * @data: caller opaque data to be passed to callback function
820 * @fn: caller supplied callback function
822 * This function is called by group users to iterate over group devices.
823 * Callers should hold a reference count to the group during callback.
824 * The group->mutex is held across callbacks, which will block calls to
825 * iommu_group_add/remove_device.
827 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
828 int (*fn)(struct device *, void *))
830 struct group_device *device;
833 list_for_each_entry(device, &group->devices, list) {
834 ret = fn(device->dev, data);
842 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
843 int (*fn)(struct device *, void *))
847 mutex_lock(&group->mutex);
848 ret = __iommu_group_for_each_dev(group, data, fn);
849 mutex_unlock(&group->mutex);
853 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
856 * iommu_group_get - Return the group for a device and increment reference
857 * @dev: get the group that this device belongs to
859 * This function is called by iommu drivers and users to get the group
860 * for the specified device. If found, the group is returned and the group
861 * reference in incremented, else NULL.
863 struct iommu_group *iommu_group_get(struct device *dev)
865 struct iommu_group *group = dev->iommu_group;
868 kobject_get(group->devices_kobj);
872 EXPORT_SYMBOL_GPL(iommu_group_get);
875 * iommu_group_ref_get - Increment reference on a group
876 * @group: the group to use, must not be NULL
878 * This function is called by iommu drivers to take additional references on an
879 * existing group. Returns the given group for convenience.
881 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
883 kobject_get(group->devices_kobj);
888 * iommu_group_put - Decrement group reference
889 * @group: the group to use
891 * This function is called by iommu drivers and users to release the
892 * iommu group. Once the reference count is zero, the group is released.
894 void iommu_group_put(struct iommu_group *group)
897 kobject_put(group->devices_kobj);
899 EXPORT_SYMBOL_GPL(iommu_group_put);
902 * iommu_group_register_notifier - Register a notifier for group changes
903 * @group: the group to watch
904 * @nb: notifier block to signal
906 * This function allows iommu group users to track changes in a group.
907 * See include/linux/iommu.h for actions sent via this notifier. Caller
908 * should hold a reference to the group throughout notifier registration.
910 int iommu_group_register_notifier(struct iommu_group *group,
911 struct notifier_block *nb)
913 return blocking_notifier_chain_register(&group->notifier, nb);
915 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
918 * iommu_group_unregister_notifier - Unregister a notifier
919 * @group: the group to watch
920 * @nb: notifier block to signal
922 * Unregister a previously registered group notifier block.
924 int iommu_group_unregister_notifier(struct iommu_group *group,
925 struct notifier_block *nb)
927 return blocking_notifier_chain_unregister(&group->notifier, nb);
929 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
932 * iommu_register_device_fault_handler() - Register a device fault handler
934 * @handler: the fault handler
935 * @data: private data passed as argument to the handler
937 * When an IOMMU fault event is received, this handler gets called with the
938 * fault event and data as argument. The handler should return 0 on success. If
939 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
940 * complete the fault by calling iommu_page_response() with one of the following
942 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
943 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
944 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
945 * page faults if possible.
947 * Return 0 if the fault handler was installed successfully, or an error.
949 int iommu_register_device_fault_handler(struct device *dev,
950 iommu_dev_fault_handler_t handler,
953 struct iommu_param *param = dev->iommu_param;
959 mutex_lock(¶m->lock);
960 /* Only allow one fault handler registered for each device */
961 if (param->fault_param) {
967 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
968 if (!param->fault_param) {
973 param->fault_param->handler = handler;
974 param->fault_param->data = data;
975 mutex_init(¶m->fault_param->lock);
976 INIT_LIST_HEAD(¶m->fault_param->faults);
979 mutex_unlock(¶m->lock);
983 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
986 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
989 * Remove the device fault handler installed with
990 * iommu_register_device_fault_handler().
992 * Return 0 on success, or an error.
994 int iommu_unregister_device_fault_handler(struct device *dev)
996 struct iommu_param *param = dev->iommu_param;
1002 mutex_lock(¶m->lock);
1004 if (!param->fault_param)
1007 /* we cannot unregister handler if there are pending faults */
1008 if (!list_empty(¶m->fault_param->faults)) {
1013 kfree(param->fault_param);
1014 param->fault_param = NULL;
1017 mutex_unlock(¶m->lock);
1021 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1024 * iommu_report_device_fault() - Report fault event to device driver
1026 * @evt: fault event data
1028 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1029 * handler. When this function fails and the fault is recoverable, it is the
1030 * caller's responsibility to complete the fault.
1032 * Return 0 on success, or an error.
1034 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1036 struct iommu_param *param = dev->iommu_param;
1037 struct iommu_fault_event *evt_pending = NULL;
1038 struct iommu_fault_param *fparam;
1044 /* we only report device fault if there is a handler registered */
1045 mutex_lock(¶m->lock);
1046 fparam = param->fault_param;
1047 if (!fparam || !fparam->handler) {
1052 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1053 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1054 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1060 mutex_lock(&fparam->lock);
1061 list_add_tail(&evt_pending->list, &fparam->faults);
1062 mutex_unlock(&fparam->lock);
1065 ret = fparam->handler(&evt->fault, fparam->data);
1066 if (ret && evt_pending) {
1067 mutex_lock(&fparam->lock);
1068 list_del(&evt_pending->list);
1069 mutex_unlock(&fparam->lock);
1073 mutex_unlock(¶m->lock);
1076 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1078 int iommu_page_response(struct device *dev,
1079 struct iommu_page_response *msg)
1083 struct iommu_fault_event *evt;
1084 struct iommu_fault_page_request *prm;
1085 struct iommu_param *param = dev->iommu_param;
1086 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1088 if (!domain || !domain->ops->page_response)
1091 if (!param || !param->fault_param)
1094 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1095 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1098 /* Only send response if there is a fault report pending */
1099 mutex_lock(¶m->fault_param->lock);
1100 if (list_empty(¶m->fault_param->faults)) {
1101 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1105 * Check if we have a matching page request pending to respond,
1106 * otherwise return -EINVAL
1108 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1109 prm = &evt->fault.prm;
1110 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1112 if ((pasid_valid && prm->pasid != msg->pasid) ||
1113 prm->grpid != msg->grpid)
1116 /* Sanitize the reply */
1117 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1119 ret = domain->ops->page_response(dev, evt, msg);
1120 list_del(&evt->list);
1126 mutex_unlock(¶m->fault_param->lock);
1129 EXPORT_SYMBOL_GPL(iommu_page_response);
1132 * iommu_group_id - Return ID for a group
1133 * @group: the group to ID
1135 * Return the unique ID for the group matching the sysfs group number.
1137 int iommu_group_id(struct iommu_group *group)
1141 EXPORT_SYMBOL_GPL(iommu_group_id);
1143 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1144 unsigned long *devfns);
1147 * To consider a PCI device isolated, we require ACS to support Source
1148 * Validation, Request Redirection, Completer Redirection, and Upstream
1149 * Forwarding. This effectively means that devices cannot spoof their
1150 * requester ID, requests and completions cannot be redirected, and all
1151 * transactions are forwarded upstream, even as it passes through a
1152 * bridge where the target device is downstream.
1154 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1157 * For multifunction devices which are not isolated from each other, find
1158 * all the other non-isolated functions and look for existing groups. For
1159 * each function, we also need to look for aliases to or from other devices
1160 * that may already have a group.
1162 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1163 unsigned long *devfns)
1165 struct pci_dev *tmp = NULL;
1166 struct iommu_group *group;
1168 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1171 for_each_pci_dev(tmp) {
1172 if (tmp == pdev || tmp->bus != pdev->bus ||
1173 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1174 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1177 group = get_pci_alias_group(tmp, devfns);
1188 * Look for aliases to or from the given device for existing groups. DMA
1189 * aliases are only supported on the same bus, therefore the search
1190 * space is quite small (especially since we're really only looking at pcie
1191 * device, and therefore only expect multiple slots on the root complex or
1192 * downstream switch ports). It's conceivable though that a pair of
1193 * multifunction devices could have aliases between them that would cause a
1194 * loop. To prevent this, we use a bitmap to track where we've been.
1196 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1197 unsigned long *devfns)
1199 struct pci_dev *tmp = NULL;
1200 struct iommu_group *group;
1202 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1205 group = iommu_group_get(&pdev->dev);
1209 for_each_pci_dev(tmp) {
1210 if (tmp == pdev || tmp->bus != pdev->bus)
1213 /* We alias them or they alias us */
1214 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1215 group = get_pci_alias_group(tmp, devfns);
1221 group = get_pci_function_alias_group(tmp, devfns);
1232 struct group_for_pci_data {
1233 struct pci_dev *pdev;
1234 struct iommu_group *group;
1238 * DMA alias iterator callback, return the last seen device. Stop and return
1239 * the IOMMU group if we find one along the way.
1241 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1243 struct group_for_pci_data *data = opaque;
1246 data->group = iommu_group_get(&pdev->dev);
1248 return data->group != NULL;
1252 * Generic device_group call-back function. It just allocates one
1253 * iommu-group per device.
1255 struct iommu_group *generic_device_group(struct device *dev)
1257 return iommu_group_alloc();
1261 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1262 * to find or create an IOMMU group for a device.
1264 struct iommu_group *pci_device_group(struct device *dev)
1266 struct pci_dev *pdev = to_pci_dev(dev);
1267 struct group_for_pci_data data;
1268 struct pci_bus *bus;
1269 struct iommu_group *group = NULL;
1270 u64 devfns[4] = { 0 };
1272 if (WARN_ON(!dev_is_pci(dev)))
1273 return ERR_PTR(-EINVAL);
1276 * Find the upstream DMA alias for the device. A device must not
1277 * be aliased due to topology in order to have its own IOMMU group.
1278 * If we find an alias along the way that already belongs to a
1281 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1287 * Continue upstream from the point of minimum IOMMU granularity
1288 * due to aliases to the point where devices are protected from
1289 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1292 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1296 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1301 group = iommu_group_get(&pdev->dev);
1307 * Look for existing groups on device aliases. If we alias another
1308 * device or another device aliases us, use the same group.
1310 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1315 * Look for existing groups on non-isolated functions on the same
1316 * slot and aliases of those funcions, if any. No need to clear
1317 * the search bitmap, the tested devfns are still valid.
1319 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1323 /* No shared group found, allocate new */
1324 return iommu_group_alloc();
1327 /* Get the IOMMU group for device on fsl-mc bus */
1328 struct iommu_group *fsl_mc_device_group(struct device *dev)
1330 struct device *cont_dev = fsl_mc_cont_dev(dev);
1331 struct iommu_group *group;
1333 group = iommu_group_get(cont_dev);
1335 group = iommu_group_alloc();
1340 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1341 * @dev: target device
1343 * This function is intended to be called by IOMMU drivers and extended to
1344 * support common, bus-defined algorithms when determining or creating the
1345 * IOMMU group for a device. On success, the caller will hold a reference
1346 * to the returned IOMMU group, which will already include the provided
1347 * device. The reference should be released with iommu_group_put().
1349 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1351 const struct iommu_ops *ops = dev->bus->iommu_ops;
1352 struct iommu_group *group;
1355 group = iommu_group_get(dev);
1360 return ERR_PTR(-EINVAL);
1362 group = ops->device_group(dev);
1363 if (WARN_ON_ONCE(group == NULL))
1364 return ERR_PTR(-EINVAL);
1370 * Try to allocate a default domain - needs support from the
1373 if (!group->default_domain) {
1374 struct iommu_domain *dom;
1376 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1377 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1378 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1381 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1382 iommu_def_domain_type);
1386 group->default_domain = dom;
1388 group->domain = dom;
1390 if (dom && !iommu_dma_strict) {
1392 iommu_domain_set_attr(dom,
1393 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1398 ret = iommu_group_add_device(group, dev);
1400 iommu_group_put(group);
1401 return ERR_PTR(ret);
1407 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1409 return group->default_domain;
1412 static int add_iommu_group(struct device *dev, void *data)
1414 int ret = iommu_probe_device(dev);
1417 * We ignore -ENODEV errors for now, as they just mean that the
1418 * device is not translated by an IOMMU. We still care about
1419 * other errors and fail to initialize when they happen.
1427 static int remove_iommu_group(struct device *dev, void *data)
1429 iommu_release_device(dev);
1434 static int iommu_bus_notifier(struct notifier_block *nb,
1435 unsigned long action, void *data)
1437 unsigned long group_action = 0;
1438 struct device *dev = data;
1439 struct iommu_group *group;
1442 * ADD/DEL call into iommu driver ops if provided, which may
1443 * result in ADD/DEL notifiers to group->notifier
1445 if (action == BUS_NOTIFY_ADD_DEVICE) {
1448 ret = iommu_probe_device(dev);
1449 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1450 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1451 iommu_release_device(dev);
1456 * Remaining BUS_NOTIFYs get filtered and republished to the
1457 * group, if anyone is listening
1459 group = iommu_group_get(dev);
1464 case BUS_NOTIFY_BIND_DRIVER:
1465 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1467 case BUS_NOTIFY_BOUND_DRIVER:
1468 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1470 case BUS_NOTIFY_UNBIND_DRIVER:
1471 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1473 case BUS_NOTIFY_UNBOUND_DRIVER:
1474 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1479 blocking_notifier_call_chain(&group->notifier,
1482 iommu_group_put(group);
1486 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1489 struct notifier_block *nb;
1491 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1495 nb->notifier_call = iommu_bus_notifier;
1497 err = bus_register_notifier(bus, nb);
1501 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
1510 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1511 bus_unregister_notifier(bus, nb);
1520 * bus_set_iommu - set iommu-callbacks for the bus
1522 * @ops: the callbacks provided by the iommu-driver
1524 * This function is called by an iommu driver to set the iommu methods
1525 * used for a particular bus. Drivers for devices on that bus can use
1526 * the iommu-api after these ops are registered.
1527 * This special function is needed because IOMMUs are usually devices on
1528 * the bus itself, so the iommu drivers are not initialized when the bus
1529 * is set up. With this function the iommu-driver can set the iommu-ops
1532 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1536 if (bus->iommu_ops != NULL)
1539 bus->iommu_ops = ops;
1541 /* Do IOMMU specific setup for this bus-type */
1542 err = iommu_bus_init(bus, ops);
1544 bus->iommu_ops = NULL;
1548 EXPORT_SYMBOL_GPL(bus_set_iommu);
1550 bool iommu_present(struct bus_type *bus)
1552 return bus->iommu_ops != NULL;
1554 EXPORT_SYMBOL_GPL(iommu_present);
1556 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1558 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1561 return bus->iommu_ops->capable(cap);
1563 EXPORT_SYMBOL_GPL(iommu_capable);
1566 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1567 * @domain: iommu domain
1568 * @handler: fault handler
1569 * @token: user data, will be passed back to the fault handler
1571 * This function should be used by IOMMU users which want to be notified
1572 * whenever an IOMMU fault happens.
1574 * The fault handler itself should return 0 on success, and an appropriate
1575 * error code otherwise.
1577 void iommu_set_fault_handler(struct iommu_domain *domain,
1578 iommu_fault_handler_t handler,
1583 domain->handler = handler;
1584 domain->handler_token = token;
1586 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1588 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1591 struct iommu_domain *domain;
1593 if (bus == NULL || bus->iommu_ops == NULL)
1596 domain = bus->iommu_ops->domain_alloc(type);
1600 domain->ops = bus->iommu_ops;
1601 domain->type = type;
1602 /* Assume all sizes by default; the driver may override this later */
1603 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1608 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1610 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1612 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1614 void iommu_domain_free(struct iommu_domain *domain)
1616 domain->ops->domain_free(domain);
1618 EXPORT_SYMBOL_GPL(iommu_domain_free);
1620 static int __iommu_attach_device(struct iommu_domain *domain,
1624 if ((domain->ops->is_attach_deferred != NULL) &&
1625 domain->ops->is_attach_deferred(domain, dev))
1628 if (unlikely(domain->ops->attach_dev == NULL))
1631 ret = domain->ops->attach_dev(domain, dev);
1633 trace_attach_device_to_domain(dev);
1637 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1639 struct iommu_group *group;
1642 group = iommu_group_get(dev);
1647 * Lock the group to make sure the device-count doesn't
1648 * change while we are attaching
1650 mutex_lock(&group->mutex);
1652 if (iommu_group_device_count(group) != 1)
1655 ret = __iommu_attach_group(domain, group);
1658 mutex_unlock(&group->mutex);
1659 iommu_group_put(group);
1663 EXPORT_SYMBOL_GPL(iommu_attach_device);
1665 static void __iommu_detach_device(struct iommu_domain *domain,
1668 if ((domain->ops->is_attach_deferred != NULL) &&
1669 domain->ops->is_attach_deferred(domain, dev))
1672 if (unlikely(domain->ops->detach_dev == NULL))
1675 domain->ops->detach_dev(domain, dev);
1676 trace_detach_device_from_domain(dev);
1679 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1681 struct iommu_group *group;
1683 group = iommu_group_get(dev);
1687 mutex_lock(&group->mutex);
1688 if (iommu_group_device_count(group) != 1) {
1693 __iommu_detach_group(domain, group);
1696 mutex_unlock(&group->mutex);
1697 iommu_group_put(group);
1699 EXPORT_SYMBOL_GPL(iommu_detach_device);
1701 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1703 struct iommu_domain *domain;
1704 struct iommu_group *group;
1706 group = iommu_group_get(dev);
1710 domain = group->domain;
1712 iommu_group_put(group);
1716 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1719 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1720 * guarantees that the group and its default domain are valid and correct.
1722 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1724 return dev->iommu_group->default_domain;
1728 * IOMMU groups are really the natural working unit of the IOMMU, but
1729 * the IOMMU API works on domains and devices. Bridge that gap by
1730 * iterating over the devices in a group. Ideally we'd have a single
1731 * device which represents the requestor ID of the group, but we also
1732 * allow IOMMU drivers to create policy defined minimum sets, where
1733 * the physical hardware may be able to distiguish members, but we
1734 * wish to group them at a higher level (ex. untrusted multi-function
1735 * PCI devices). Thus we attach each device.
1737 static int iommu_group_do_attach_device(struct device *dev, void *data)
1739 struct iommu_domain *domain = data;
1741 return __iommu_attach_device(domain, dev);
1744 static int __iommu_attach_group(struct iommu_domain *domain,
1745 struct iommu_group *group)
1749 if (group->default_domain && group->domain != group->default_domain)
1752 ret = __iommu_group_for_each_dev(group, domain,
1753 iommu_group_do_attach_device);
1755 group->domain = domain;
1760 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1764 mutex_lock(&group->mutex);
1765 ret = __iommu_attach_group(domain, group);
1766 mutex_unlock(&group->mutex);
1770 EXPORT_SYMBOL_GPL(iommu_attach_group);
1772 static int iommu_group_do_detach_device(struct device *dev, void *data)
1774 struct iommu_domain *domain = data;
1776 __iommu_detach_device(domain, dev);
1781 static void __iommu_detach_group(struct iommu_domain *domain,
1782 struct iommu_group *group)
1786 if (!group->default_domain) {
1787 __iommu_group_for_each_dev(group, domain,
1788 iommu_group_do_detach_device);
1789 group->domain = NULL;
1793 if (group->domain == group->default_domain)
1796 /* Detach by re-attaching to the default domain */
1797 ret = __iommu_group_for_each_dev(group, group->default_domain,
1798 iommu_group_do_attach_device);
1802 group->domain = group->default_domain;
1805 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1807 mutex_lock(&group->mutex);
1808 __iommu_detach_group(domain, group);
1809 mutex_unlock(&group->mutex);
1811 EXPORT_SYMBOL_GPL(iommu_detach_group);
1813 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1815 if (unlikely(domain->ops->iova_to_phys == NULL))
1818 return domain->ops->iova_to_phys(domain, iova);
1820 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1822 static size_t iommu_pgsize(struct iommu_domain *domain,
1823 unsigned long addr_merge, size_t size)
1825 unsigned int pgsize_idx;
1828 /* Max page size that still fits into 'size' */
1829 pgsize_idx = __fls(size);
1831 /* need to consider alignment requirements ? */
1832 if (likely(addr_merge)) {
1833 /* Max page size allowed by address */
1834 unsigned int align_pgsize_idx = __ffs(addr_merge);
1835 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1838 /* build a mask of acceptable page sizes */
1839 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1841 /* throw away page sizes not supported by the hardware */
1842 pgsize &= domain->pgsize_bitmap;
1844 /* make sure we're still sane */
1847 /* pick the biggest page */
1848 pgsize_idx = __fls(pgsize);
1849 pgsize = 1UL << pgsize_idx;
1854 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1855 phys_addr_t paddr, size_t size, int prot)
1857 const struct iommu_ops *ops = domain->ops;
1858 unsigned long orig_iova = iova;
1859 unsigned int min_pagesz;
1860 size_t orig_size = size;
1861 phys_addr_t orig_paddr = paddr;
1864 if (unlikely(ops->map == NULL ||
1865 domain->pgsize_bitmap == 0UL))
1868 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1871 /* find out the minimum page size supported */
1872 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1875 * both the virtual address and the physical one, as well as
1876 * the size of the mapping, must be aligned (at least) to the
1877 * size of the smallest page supported by the hardware
1879 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1880 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1881 iova, &paddr, size, min_pagesz);
1885 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1888 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1890 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1891 iova, &paddr, pgsize);
1893 ret = ops->map(domain, iova, paddr, pgsize, prot);
1902 if (ops->iotlb_sync_map)
1903 ops->iotlb_sync_map(domain);
1905 /* unroll mapping in case something went wrong */
1907 iommu_unmap(domain, orig_iova, orig_size - size);
1909 trace_map(orig_iova, orig_paddr, orig_size);
1913 EXPORT_SYMBOL_GPL(iommu_map);
1915 static size_t __iommu_unmap(struct iommu_domain *domain,
1916 unsigned long iova, size_t size,
1917 struct iommu_iotlb_gather *iotlb_gather)
1919 const struct iommu_ops *ops = domain->ops;
1920 size_t unmapped_page, unmapped = 0;
1921 unsigned long orig_iova = iova;
1922 unsigned int min_pagesz;
1924 if (unlikely(ops->unmap == NULL ||
1925 domain->pgsize_bitmap == 0UL))
1928 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1931 /* find out the minimum page size supported */
1932 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1935 * The virtual address, as well as the size of the mapping, must be
1936 * aligned (at least) to the size of the smallest page supported
1939 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1940 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1941 iova, size, min_pagesz);
1945 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1948 * Keep iterating until we either unmap 'size' bytes (or more)
1949 * or we hit an area that isn't mapped.
1951 while (unmapped < size) {
1952 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1954 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
1958 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1959 iova, unmapped_page);
1961 iova += unmapped_page;
1962 unmapped += unmapped_page;
1965 trace_unmap(orig_iova, size, unmapped);
1969 size_t iommu_unmap(struct iommu_domain *domain,
1970 unsigned long iova, size_t size)
1972 struct iommu_iotlb_gather iotlb_gather;
1975 iommu_iotlb_gather_init(&iotlb_gather);
1976 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
1977 iommu_tlb_sync(domain, &iotlb_gather);
1981 EXPORT_SYMBOL_GPL(iommu_unmap);
1983 size_t iommu_unmap_fast(struct iommu_domain *domain,
1984 unsigned long iova, size_t size,
1985 struct iommu_iotlb_gather *iotlb_gather)
1987 return __iommu_unmap(domain, iova, size, iotlb_gather);
1989 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1991 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1992 struct scatterlist *sg, unsigned int nents, int prot)
1994 size_t len = 0, mapped = 0;
1999 while (i <= nents) {
2000 phys_addr_t s_phys = sg_phys(sg);
2002 if (len && s_phys != start + len) {
2003 ret = iommu_map(domain, iova + mapped, start, len, prot);
2025 /* undo mappings already done */
2026 iommu_unmap(domain, iova, mapped);
2031 EXPORT_SYMBOL_GPL(iommu_map_sg);
2033 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
2034 phys_addr_t paddr, u64 size, int prot)
2036 if (unlikely(domain->ops->domain_window_enable == NULL))
2039 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2042 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2044 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2046 if (unlikely(domain->ops->domain_window_disable == NULL))
2049 return domain->ops->domain_window_disable(domain, wnd_nr);
2051 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2054 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2055 * @domain: the iommu domain where the fault has happened
2056 * @dev: the device where the fault has happened
2057 * @iova: the faulting address
2058 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2060 * This function should be called by the low-level IOMMU implementations
2061 * whenever IOMMU faults happen, to allow high-level users, that are
2062 * interested in such events, to know about them.
2064 * This event may be useful for several possible use cases:
2065 * - mere logging of the event
2066 * - dynamic TLB/PTE loading
2067 * - if restarting of the faulting device is required
2069 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2070 * PTE/TLB loading will one day be supported, implementations will be able
2071 * to tell whether it succeeded or not according to this return value).
2073 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2074 * (though fault handlers can also return -ENOSYS, in case they want to
2075 * elicit the default behavior of the IOMMU drivers).
2077 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2078 unsigned long iova, int flags)
2083 * if upper layers showed interest and installed a fault handler,
2086 if (domain->handler)
2087 ret = domain->handler(domain, dev, iova, flags,
2088 domain->handler_token);
2090 trace_io_page_fault(dev, iova, flags);
2093 EXPORT_SYMBOL_GPL(report_iommu_fault);
2095 static int __init iommu_init(void)
2097 iommu_group_kset = kset_create_and_add("iommu_groups",
2099 BUG_ON(!iommu_group_kset);
2101 iommu_debugfs_setup();
2105 core_initcall(iommu_init);
2107 int iommu_domain_get_attr(struct iommu_domain *domain,
2108 enum iommu_attr attr, void *data)
2110 struct iommu_domain_geometry *geometry;
2115 case DOMAIN_ATTR_GEOMETRY:
2117 *geometry = domain->geometry;
2120 case DOMAIN_ATTR_PAGING:
2122 *paging = (domain->pgsize_bitmap != 0UL);
2125 if (!domain->ops->domain_get_attr)
2128 ret = domain->ops->domain_get_attr(domain, attr, data);
2133 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2135 int iommu_domain_set_attr(struct iommu_domain *domain,
2136 enum iommu_attr attr, void *data)
2142 if (domain->ops->domain_set_attr == NULL)
2145 ret = domain->ops->domain_set_attr(domain, attr, data);
2150 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
2152 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2154 const struct iommu_ops *ops = dev->bus->iommu_ops;
2156 if (ops && ops->get_resv_regions)
2157 ops->get_resv_regions(dev, list);
2160 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2162 const struct iommu_ops *ops = dev->bus->iommu_ops;
2164 if (ops && ops->put_resv_regions)
2165 ops->put_resv_regions(dev, list);
2168 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2169 size_t length, int prot,
2170 enum iommu_resv_type type)
2172 struct iommu_resv_region *region;
2174 region = kzalloc(sizeof(*region), GFP_KERNEL);
2178 INIT_LIST_HEAD(®ion->list);
2179 region->start = start;
2180 region->length = length;
2181 region->prot = prot;
2182 region->type = type;
2187 request_default_domain_for_dev(struct device *dev, unsigned long type)
2189 struct iommu_domain *domain;
2190 struct iommu_group *group;
2193 /* Device must already be in a group before calling this function */
2194 group = iommu_group_get(dev);
2198 mutex_lock(&group->mutex);
2200 /* Check if the default domain is already direct mapped */
2202 if (group->default_domain && group->default_domain->type == type)
2205 /* Don't change mappings of existing devices */
2207 if (iommu_group_device_count(group) != 1)
2210 /* Allocate a direct mapped domain */
2212 domain = __iommu_domain_alloc(dev->bus, type);
2216 /* Attach the device to the domain */
2217 ret = __iommu_attach_group(domain, group);
2219 iommu_domain_free(domain);
2223 iommu_group_create_direct_mappings(group, dev);
2225 /* Make the direct mapped domain the default for this group */
2226 if (group->default_domain)
2227 iommu_domain_free(group->default_domain);
2228 group->default_domain = domain;
2230 dev_info(dev, "Using iommu %s mapping\n",
2231 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
2235 mutex_unlock(&group->mutex);
2236 iommu_group_put(group);
2241 /* Request that a device is direct mapped by the IOMMU */
2242 int iommu_request_dm_for_dev(struct device *dev)
2244 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2247 /* Request that a device can't be direct mapped by the IOMMU */
2248 int iommu_request_dma_domain_for_dev(struct device *dev)
2250 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2253 void iommu_set_default_passthrough(bool cmd_line)
2256 iommu_set_cmd_line_dma_api();
2258 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2261 void iommu_set_default_translated(bool cmd_line)
2264 iommu_set_cmd_line_dma_api();
2266 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2269 bool iommu_default_passthrough(void)
2271 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2273 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2275 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2277 const struct iommu_ops *ops = NULL;
2278 struct iommu_device *iommu;
2280 spin_lock(&iommu_device_lock);
2281 list_for_each_entry(iommu, &iommu_device_list, list)
2282 if (iommu->fwnode == fwnode) {
2286 spin_unlock(&iommu_device_lock);
2290 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2291 const struct iommu_ops *ops)
2293 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2296 return ops == fwspec->ops ? 0 : -EINVAL;
2298 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2302 of_node_get(to_of_node(iommu_fwnode));
2303 fwspec->iommu_fwnode = iommu_fwnode;
2305 dev_iommu_fwspec_set(dev, fwspec);
2308 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2310 void iommu_fwspec_free(struct device *dev)
2312 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2315 fwnode_handle_put(fwspec->iommu_fwnode);
2317 dev_iommu_fwspec_set(dev, NULL);
2320 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2322 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2324 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2331 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2332 if (size > sizeof(*fwspec)) {
2333 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2337 dev_iommu_fwspec_set(dev, fwspec);
2340 for (i = 0; i < num_ids; i++)
2341 fwspec->ids[fwspec->num_ids + i] = ids[i];
2343 fwspec->num_ids += num_ids;
2346 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2349 * Per device IOMMU features.
2351 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2353 const struct iommu_ops *ops = dev->bus->iommu_ops;
2355 if (ops && ops->dev_has_feat)
2356 return ops->dev_has_feat(dev, feat);
2360 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2362 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2364 const struct iommu_ops *ops = dev->bus->iommu_ops;
2366 if (ops && ops->dev_enable_feat)
2367 return ops->dev_enable_feat(dev, feat);
2371 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2374 * The device drivers should do the necessary cleanups before calling this.
2375 * For example, before disabling the aux-domain feature, the device driver
2376 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2378 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2380 const struct iommu_ops *ops = dev->bus->iommu_ops;
2382 if (ops && ops->dev_disable_feat)
2383 return ops->dev_disable_feat(dev, feat);
2387 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2389 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2391 const struct iommu_ops *ops = dev->bus->iommu_ops;
2393 if (ops && ops->dev_feat_enabled)
2394 return ops->dev_feat_enabled(dev, feat);
2398 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2401 * Aux-domain specific attach/detach.
2403 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2404 * true. Also, as long as domains are attached to a device through this
2405 * interface, any tries to call iommu_attach_device() should fail
2406 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2407 * This should make us safe against a device being attached to a guest as a
2408 * whole while there are still pasid users on it (aux and sva).
2410 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2414 if (domain->ops->aux_attach_dev)
2415 ret = domain->ops->aux_attach_dev(domain, dev);
2418 trace_attach_device_to_domain(dev);
2422 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2424 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2426 if (domain->ops->aux_detach_dev) {
2427 domain->ops->aux_detach_dev(domain, dev);
2428 trace_detach_device_from_domain(dev);
2431 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2433 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2437 if (domain->ops->aux_get_pasid)
2438 ret = domain->ops->aux_get_pasid(domain, dev);
2442 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2445 * iommu_sva_bind_device() - Bind a process address space to a device
2447 * @mm: the mm to bind, caller must hold a reference to it
2449 * Create a bond between device and address space, allowing the device to access
2450 * the mm using the returned PASID. If a bond already exists between @device and
2451 * @mm, it is returned and an additional reference is taken. Caller must call
2452 * iommu_sva_unbind_device() to release each reference.
2454 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2455 * initialize the required SVA features.
2457 * On error, returns an ERR_PTR value.
2460 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2462 struct iommu_group *group;
2463 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2464 const struct iommu_ops *ops = dev->bus->iommu_ops;
2466 if (!ops || !ops->sva_bind)
2467 return ERR_PTR(-ENODEV);
2469 group = iommu_group_get(dev);
2471 return ERR_PTR(-ENODEV);
2473 /* Ensure device count and domain don't change while we're binding */
2474 mutex_lock(&group->mutex);
2477 * To keep things simple, SVA currently doesn't support IOMMU groups
2478 * with more than one device. Existing SVA-capable systems are not
2479 * affected by the problems that required IOMMU groups (lack of ACS
2480 * isolation, device ID aliasing and other hardware issues).
2482 if (iommu_group_device_count(group) != 1)
2485 handle = ops->sva_bind(dev, mm, drvdata);
2488 mutex_unlock(&group->mutex);
2489 iommu_group_put(group);
2493 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2496 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2497 * @handle: the handle returned by iommu_sva_bind_device()
2499 * Put reference to a bond between device and address space. The device should
2500 * not be issuing any more transaction for this PASID. All outstanding page
2501 * requests for this PASID must have been flushed to the IOMMU.
2503 * Returns 0 on success, or an error value
2505 void iommu_sva_unbind_device(struct iommu_sva *handle)
2507 struct iommu_group *group;
2508 struct device *dev = handle->dev;
2509 const struct iommu_ops *ops = dev->bus->iommu_ops;
2511 if (!ops || !ops->sva_unbind)
2514 group = iommu_group_get(dev);
2518 mutex_lock(&group->mutex);
2519 ops->sva_unbind(handle);
2520 mutex_unlock(&group->mutex);
2522 iommu_group_put(group);
2524 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2526 int iommu_sva_set_ops(struct iommu_sva *handle,
2527 const struct iommu_sva_ops *sva_ops)
2529 if (handle->ops && handle->ops != sva_ops)
2532 handle->ops = sva_ops;
2535 EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2537 int iommu_sva_get_pasid(struct iommu_sva *handle)
2539 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2541 if (!ops || !ops->sva_get_pasid)
2542 return IOMMU_PASID_INVALID;
2544 return ops->sva_get_pasid(handle);
2546 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);