1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
7 #define pr_fmt(fmt) "iommu: " fmt
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/iommu.h>
18 #include <linux/idr.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/pci.h>
22 #include <linux/bitops.h>
23 #include <linux/property.h>
24 #include <linux/fsl/mc.h>
25 #include <linux/module.h>
26 #include <trace/events/iommu.h>
28 static struct kset
*iommu_group_kset
;
29 static DEFINE_IDA(iommu_group_ida
);
31 static unsigned int iommu_def_domain_type __read_mostly
;
32 static bool iommu_dma_strict __read_mostly
= true;
33 static u32 iommu_cmd_line __read_mostly
;
37 struct kobject
*devices_kobj
;
38 struct list_head devices
;
40 struct blocking_notifier_head notifier
;
42 void (*iommu_data_release
)(void *iommu_data
);
45 struct iommu_domain
*default_domain
;
46 struct iommu_domain
*domain
;
50 struct list_head list
;
55 struct iommu_group_attribute
{
56 struct attribute attr
;
57 ssize_t (*show
)(struct iommu_group
*group
, char *buf
);
58 ssize_t (*store
)(struct iommu_group
*group
,
59 const char *buf
, size_t count
);
62 static const char * const iommu_group_resv_type_string
[] = {
63 [IOMMU_RESV_DIRECT
] = "direct",
64 [IOMMU_RESV_DIRECT_RELAXABLE
] = "direct-relaxable",
65 [IOMMU_RESV_RESERVED
] = "reserved",
66 [IOMMU_RESV_MSI
] = "msi",
67 [IOMMU_RESV_SW_MSI
] = "msi",
70 #define IOMMU_CMD_LINE_DMA_API BIT(0)
72 static void iommu_set_cmd_line_dma_api(void)
74 iommu_cmd_line
|= IOMMU_CMD_LINE_DMA_API
;
77 static bool iommu_cmd_line_dma_api(void)
79 return !!(iommu_cmd_line
& IOMMU_CMD_LINE_DMA_API
);
82 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
83 struct iommu_group_attribute iommu_group_attr_##_name = \
84 __ATTR(_name, _mode, _show, _store)
86 #define to_iommu_group_attr(_attr) \
87 container_of(_attr, struct iommu_group_attribute, attr)
88 #define to_iommu_group(_kobj) \
89 container_of(_kobj, struct iommu_group, kobj)
91 static LIST_HEAD(iommu_device_list
);
92 static DEFINE_SPINLOCK(iommu_device_lock
);
95 * Use a function instead of an array here because the domain-type is a
96 * bit-field, so an array would waste memory.
98 static const char *iommu_domain_type_str(unsigned int t
)
101 case IOMMU_DOMAIN_BLOCKED
:
103 case IOMMU_DOMAIN_IDENTITY
:
104 return "Passthrough";
105 case IOMMU_DOMAIN_UNMANAGED
:
107 case IOMMU_DOMAIN_DMA
:
114 static int __init
iommu_subsys_init(void)
116 bool cmd_line
= iommu_cmd_line_dma_api();
119 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH
))
120 iommu_set_default_passthrough(false);
122 iommu_set_default_translated(false);
124 if (iommu_default_passthrough() && mem_encrypt_active()) {
125 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
126 iommu_set_default_translated(false);
130 pr_info("Default domain type: %s %s\n",
131 iommu_domain_type_str(iommu_def_domain_type
),
132 cmd_line
? "(set via kernel command line)" : "");
136 subsys_initcall(iommu_subsys_init
);
138 int iommu_device_register(struct iommu_device
*iommu
)
140 spin_lock(&iommu_device_lock
);
141 list_add_tail(&iommu
->list
, &iommu_device_list
);
142 spin_unlock(&iommu_device_lock
);
145 EXPORT_SYMBOL_GPL(iommu_device_register
);
147 void iommu_device_unregister(struct iommu_device
*iommu
)
149 spin_lock(&iommu_device_lock
);
150 list_del(&iommu
->list
);
151 spin_unlock(&iommu_device_lock
);
153 EXPORT_SYMBOL_GPL(iommu_device_unregister
);
155 static struct dev_iommu
*dev_iommu_get(struct device
*dev
)
157 struct dev_iommu
*param
= dev
->iommu
;
162 param
= kzalloc(sizeof(*param
), GFP_KERNEL
);
166 mutex_init(¶m
->lock
);
171 static void dev_iommu_free(struct device
*dev
)
173 iommu_fwspec_free(dev
);
178 int iommu_probe_device(struct device
*dev
)
180 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
183 WARN_ON(dev
->iommu_group
);
187 if (!dev_iommu_get(dev
))
190 if (!try_module_get(ops
->owner
)) {
192 goto err_free_dev_param
;
195 ret
= ops
->add_device(dev
);
202 module_put(ops
->owner
);
208 void iommu_release_device(struct device
*dev
)
210 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
212 if (dev
->iommu_group
)
213 ops
->remove_device(dev
);
216 module_put(ops
->owner
);
221 static struct iommu_domain
*__iommu_domain_alloc(struct bus_type
*bus
,
223 static int __iommu_attach_device(struct iommu_domain
*domain
,
225 static int __iommu_attach_group(struct iommu_domain
*domain
,
226 struct iommu_group
*group
);
227 static void __iommu_detach_group(struct iommu_domain
*domain
,
228 struct iommu_group
*group
);
230 static int __init
iommu_set_def_domain_type(char *str
)
235 ret
= kstrtobool(str
, &pt
);
240 iommu_set_default_passthrough(true);
242 iommu_set_default_translated(true);
246 early_param("iommu.passthrough", iommu_set_def_domain_type
);
248 static int __init
iommu_dma_setup(char *str
)
250 return kstrtobool(str
, &iommu_dma_strict
);
252 early_param("iommu.strict", iommu_dma_setup
);
254 static ssize_t
iommu_group_attr_show(struct kobject
*kobj
,
255 struct attribute
*__attr
, char *buf
)
257 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
258 struct iommu_group
*group
= to_iommu_group(kobj
);
262 ret
= attr
->show(group
, buf
);
266 static ssize_t
iommu_group_attr_store(struct kobject
*kobj
,
267 struct attribute
*__attr
,
268 const char *buf
, size_t count
)
270 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
271 struct iommu_group
*group
= to_iommu_group(kobj
);
275 ret
= attr
->store(group
, buf
, count
);
279 static const struct sysfs_ops iommu_group_sysfs_ops
= {
280 .show
= iommu_group_attr_show
,
281 .store
= iommu_group_attr_store
,
284 static int iommu_group_create_file(struct iommu_group
*group
,
285 struct iommu_group_attribute
*attr
)
287 return sysfs_create_file(&group
->kobj
, &attr
->attr
);
290 static void iommu_group_remove_file(struct iommu_group
*group
,
291 struct iommu_group_attribute
*attr
)
293 sysfs_remove_file(&group
->kobj
, &attr
->attr
);
296 static ssize_t
iommu_group_show_name(struct iommu_group
*group
, char *buf
)
298 return sprintf(buf
, "%s\n", group
->name
);
302 * iommu_insert_resv_region - Insert a new region in the
303 * list of reserved regions.
304 * @new: new region to insert
305 * @regions: list of regions
307 * Elements are sorted by start address and overlapping segments
308 * of the same type are merged.
310 int iommu_insert_resv_region(struct iommu_resv_region
*new,
311 struct list_head
*regions
)
313 struct iommu_resv_region
*iter
, *tmp
, *nr
, *top
;
316 nr
= iommu_alloc_resv_region(new->start
, new->length
,
317 new->prot
, new->type
);
321 /* First add the new element based on start address sorting */
322 list_for_each_entry(iter
, regions
, list
) {
323 if (nr
->start
< iter
->start
||
324 (nr
->start
== iter
->start
&& nr
->type
<= iter
->type
))
327 list_add_tail(&nr
->list
, &iter
->list
);
329 /* Merge overlapping segments of type nr->type in @regions, if any */
330 list_for_each_entry_safe(iter
, tmp
, regions
, list
) {
331 phys_addr_t top_end
, iter_end
= iter
->start
+ iter
->length
- 1;
333 /* no merge needed on elements of different types than @new */
334 if (iter
->type
!= new->type
) {
335 list_move_tail(&iter
->list
, &stack
);
339 /* look for the last stack element of same type as @iter */
340 list_for_each_entry_reverse(top
, &stack
, list
)
341 if (top
->type
== iter
->type
)
344 list_move_tail(&iter
->list
, &stack
);
348 top_end
= top
->start
+ top
->length
- 1;
350 if (iter
->start
> top_end
+ 1) {
351 list_move_tail(&iter
->list
, &stack
);
353 top
->length
= max(top_end
, iter_end
) - top
->start
+ 1;
354 list_del(&iter
->list
);
358 list_splice(&stack
, regions
);
363 iommu_insert_device_resv_regions(struct list_head
*dev_resv_regions
,
364 struct list_head
*group_resv_regions
)
366 struct iommu_resv_region
*entry
;
369 list_for_each_entry(entry
, dev_resv_regions
, list
) {
370 ret
= iommu_insert_resv_region(entry
, group_resv_regions
);
377 int iommu_get_group_resv_regions(struct iommu_group
*group
,
378 struct list_head
*head
)
380 struct group_device
*device
;
383 mutex_lock(&group
->mutex
);
384 list_for_each_entry(device
, &group
->devices
, list
) {
385 struct list_head dev_resv_regions
;
387 INIT_LIST_HEAD(&dev_resv_regions
);
388 iommu_get_resv_regions(device
->dev
, &dev_resv_regions
);
389 ret
= iommu_insert_device_resv_regions(&dev_resv_regions
, head
);
390 iommu_put_resv_regions(device
->dev
, &dev_resv_regions
);
394 mutex_unlock(&group
->mutex
);
397 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions
);
399 static ssize_t
iommu_group_show_resv_regions(struct iommu_group
*group
,
402 struct iommu_resv_region
*region
, *next
;
403 struct list_head group_resv_regions
;
406 INIT_LIST_HEAD(&group_resv_regions
);
407 iommu_get_group_resv_regions(group
, &group_resv_regions
);
409 list_for_each_entry_safe(region
, next
, &group_resv_regions
, list
) {
410 str
+= sprintf(str
, "0x%016llx 0x%016llx %s\n",
411 (long long int)region
->start
,
412 (long long int)(region
->start
+
414 iommu_group_resv_type_string
[region
->type
]);
421 static ssize_t
iommu_group_show_type(struct iommu_group
*group
,
424 char *type
= "unknown\n";
426 if (group
->default_domain
) {
427 switch (group
->default_domain
->type
) {
428 case IOMMU_DOMAIN_BLOCKED
:
431 case IOMMU_DOMAIN_IDENTITY
:
434 case IOMMU_DOMAIN_UNMANAGED
:
435 type
= "unmanaged\n";
437 case IOMMU_DOMAIN_DMA
:
447 static IOMMU_GROUP_ATTR(name
, S_IRUGO
, iommu_group_show_name
, NULL
);
449 static IOMMU_GROUP_ATTR(reserved_regions
, 0444,
450 iommu_group_show_resv_regions
, NULL
);
452 static IOMMU_GROUP_ATTR(type
, 0444, iommu_group_show_type
, NULL
);
454 static void iommu_group_release(struct kobject
*kobj
)
456 struct iommu_group
*group
= to_iommu_group(kobj
);
458 pr_debug("Releasing group %d\n", group
->id
);
460 if (group
->iommu_data_release
)
461 group
->iommu_data_release(group
->iommu_data
);
463 ida_simple_remove(&iommu_group_ida
, group
->id
);
465 if (group
->default_domain
)
466 iommu_domain_free(group
->default_domain
);
472 static struct kobj_type iommu_group_ktype
= {
473 .sysfs_ops
= &iommu_group_sysfs_ops
,
474 .release
= iommu_group_release
,
478 * iommu_group_alloc - Allocate a new group
480 * This function is called by an iommu driver to allocate a new iommu
481 * group. The iommu group represents the minimum granularity of the iommu.
482 * Upon successful return, the caller holds a reference to the supplied
483 * group in order to hold the group until devices are added. Use
484 * iommu_group_put() to release this extra reference count, allowing the
485 * group to be automatically reclaimed once it has no devices or external
488 struct iommu_group
*iommu_group_alloc(void)
490 struct iommu_group
*group
;
493 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
495 return ERR_PTR(-ENOMEM
);
497 group
->kobj
.kset
= iommu_group_kset
;
498 mutex_init(&group
->mutex
);
499 INIT_LIST_HEAD(&group
->devices
);
500 BLOCKING_INIT_NOTIFIER_HEAD(&group
->notifier
);
502 ret
= ida_simple_get(&iommu_group_ida
, 0, 0, GFP_KERNEL
);
509 ret
= kobject_init_and_add(&group
->kobj
, &iommu_group_ktype
,
510 NULL
, "%d", group
->id
);
512 ida_simple_remove(&iommu_group_ida
, group
->id
);
517 group
->devices_kobj
= kobject_create_and_add("devices", &group
->kobj
);
518 if (!group
->devices_kobj
) {
519 kobject_put(&group
->kobj
); /* triggers .release & free */
520 return ERR_PTR(-ENOMEM
);
524 * The devices_kobj holds a reference on the group kobject, so
525 * as long as that exists so will the group. We can therefore
526 * use the devices_kobj for reference counting.
528 kobject_put(&group
->kobj
);
530 ret
= iommu_group_create_file(group
,
531 &iommu_group_attr_reserved_regions
);
535 ret
= iommu_group_create_file(group
, &iommu_group_attr_type
);
539 pr_debug("Allocated group %d\n", group
->id
);
543 EXPORT_SYMBOL_GPL(iommu_group_alloc
);
545 struct iommu_group
*iommu_group_get_by_id(int id
)
547 struct kobject
*group_kobj
;
548 struct iommu_group
*group
;
551 if (!iommu_group_kset
)
554 name
= kasprintf(GFP_KERNEL
, "%d", id
);
558 group_kobj
= kset_find_obj(iommu_group_kset
, name
);
564 group
= container_of(group_kobj
, struct iommu_group
, kobj
);
565 BUG_ON(group
->id
!= id
);
567 kobject_get(group
->devices_kobj
);
568 kobject_put(&group
->kobj
);
572 EXPORT_SYMBOL_GPL(iommu_group_get_by_id
);
575 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
578 * iommu drivers can store data in the group for use when doing iommu
579 * operations. This function provides a way to retrieve it. Caller
580 * should hold a group reference.
582 void *iommu_group_get_iommudata(struct iommu_group
*group
)
584 return group
->iommu_data
;
586 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata
);
589 * iommu_group_set_iommudata - set iommu_data for a group
591 * @iommu_data: new data
592 * @release: release function for iommu_data
594 * iommu drivers can store data in the group for use when doing iommu
595 * operations. This function provides a way to set the data after
596 * the group has been allocated. Caller should hold a group reference.
598 void iommu_group_set_iommudata(struct iommu_group
*group
, void *iommu_data
,
599 void (*release
)(void *iommu_data
))
601 group
->iommu_data
= iommu_data
;
602 group
->iommu_data_release
= release
;
604 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata
);
607 * iommu_group_set_name - set name for a group
611 * Allow iommu driver to set a name for a group. When set it will
612 * appear in a name attribute file under the group in sysfs.
614 int iommu_group_set_name(struct iommu_group
*group
, const char *name
)
619 iommu_group_remove_file(group
, &iommu_group_attr_name
);
626 group
->name
= kstrdup(name
, GFP_KERNEL
);
630 ret
= iommu_group_create_file(group
, &iommu_group_attr_name
);
639 EXPORT_SYMBOL_GPL(iommu_group_set_name
);
641 static int iommu_group_create_direct_mappings(struct iommu_group
*group
,
644 struct iommu_domain
*domain
= group
->default_domain
;
645 struct iommu_resv_region
*entry
;
646 struct list_head mappings
;
647 unsigned long pg_size
;
650 if (!domain
|| domain
->type
!= IOMMU_DOMAIN_DMA
)
653 BUG_ON(!domain
->pgsize_bitmap
);
655 pg_size
= 1UL << __ffs(domain
->pgsize_bitmap
);
656 INIT_LIST_HEAD(&mappings
);
658 iommu_get_resv_regions(dev
, &mappings
);
660 /* We need to consider overlapping regions for different devices */
661 list_for_each_entry(entry
, &mappings
, list
) {
662 dma_addr_t start
, end
, addr
;
664 if (domain
->ops
->apply_resv_region
)
665 domain
->ops
->apply_resv_region(dev
, domain
, entry
);
667 start
= ALIGN(entry
->start
, pg_size
);
668 end
= ALIGN(entry
->start
+ entry
->length
, pg_size
);
670 if (entry
->type
!= IOMMU_RESV_DIRECT
&&
671 entry
->type
!= IOMMU_RESV_DIRECT_RELAXABLE
)
674 for (addr
= start
; addr
< end
; addr
+= pg_size
) {
675 phys_addr_t phys_addr
;
677 phys_addr
= iommu_iova_to_phys(domain
, addr
);
681 ret
= iommu_map(domain
, addr
, addr
, pg_size
, entry
->prot
);
688 iommu_flush_tlb_all(domain
);
691 iommu_put_resv_regions(dev
, &mappings
);
697 * iommu_group_add_device - add a device to an iommu group
698 * @group: the group into which to add the device (reference should be held)
701 * This function is called by an iommu driver to add a device into a
702 * group. Adding a device increments the group reference count.
704 int iommu_group_add_device(struct iommu_group
*group
, struct device
*dev
)
707 struct group_device
*device
;
709 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
715 ret
= sysfs_create_link(&dev
->kobj
, &group
->kobj
, "iommu_group");
717 goto err_free_device
;
719 device
->name
= kasprintf(GFP_KERNEL
, "%s", kobject_name(&dev
->kobj
));
723 goto err_remove_link
;
726 ret
= sysfs_create_link_nowarn(group
->devices_kobj
,
727 &dev
->kobj
, device
->name
);
729 if (ret
== -EEXIST
&& i
>= 0) {
731 * Account for the slim chance of collision
732 * and append an instance to the name.
735 device
->name
= kasprintf(GFP_KERNEL
, "%s.%d",
736 kobject_name(&dev
->kobj
), i
++);
742 kobject_get(group
->devices_kobj
);
744 dev
->iommu_group
= group
;
746 iommu_group_create_direct_mappings(group
, dev
);
748 mutex_lock(&group
->mutex
);
749 list_add_tail(&device
->list
, &group
->devices
);
751 ret
= __iommu_attach_device(group
->domain
, dev
);
752 mutex_unlock(&group
->mutex
);
756 /* Notify any listeners about change to group. */
757 blocking_notifier_call_chain(&group
->notifier
,
758 IOMMU_GROUP_NOTIFY_ADD_DEVICE
, dev
);
760 trace_add_device_to_group(group
->id
, dev
);
762 dev_info(dev
, "Adding to iommu group %d\n", group
->id
);
767 mutex_lock(&group
->mutex
);
768 list_del(&device
->list
);
769 mutex_unlock(&group
->mutex
);
770 dev
->iommu_group
= NULL
;
771 kobject_put(group
->devices_kobj
);
772 sysfs_remove_link(group
->devices_kobj
, device
->name
);
776 sysfs_remove_link(&dev
->kobj
, "iommu_group");
779 dev_err(dev
, "Failed to add to iommu group %d: %d\n", group
->id
, ret
);
782 EXPORT_SYMBOL_GPL(iommu_group_add_device
);
785 * iommu_group_remove_device - remove a device from it's current group
786 * @dev: device to be removed
788 * This function is called by an iommu driver to remove the device from
789 * it's current group. This decrements the iommu group reference count.
791 void iommu_group_remove_device(struct device
*dev
)
793 struct iommu_group
*group
= dev
->iommu_group
;
794 struct group_device
*tmp_device
, *device
= NULL
;
796 dev_info(dev
, "Removing from iommu group %d\n", group
->id
);
798 /* Pre-notify listeners that a device is being removed. */
799 blocking_notifier_call_chain(&group
->notifier
,
800 IOMMU_GROUP_NOTIFY_DEL_DEVICE
, dev
);
802 mutex_lock(&group
->mutex
);
803 list_for_each_entry(tmp_device
, &group
->devices
, list
) {
804 if (tmp_device
->dev
== dev
) {
806 list_del(&device
->list
);
810 mutex_unlock(&group
->mutex
);
815 sysfs_remove_link(group
->devices_kobj
, device
->name
);
816 sysfs_remove_link(&dev
->kobj
, "iommu_group");
818 trace_remove_device_from_group(group
->id
, dev
);
822 dev
->iommu_group
= NULL
;
823 kobject_put(group
->devices_kobj
);
825 EXPORT_SYMBOL_GPL(iommu_group_remove_device
);
827 static int iommu_group_device_count(struct iommu_group
*group
)
829 struct group_device
*entry
;
832 list_for_each_entry(entry
, &group
->devices
, list
)
839 * iommu_group_for_each_dev - iterate over each device in the group
841 * @data: caller opaque data to be passed to callback function
842 * @fn: caller supplied callback function
844 * This function is called by group users to iterate over group devices.
845 * Callers should hold a reference count to the group during callback.
846 * The group->mutex is held across callbacks, which will block calls to
847 * iommu_group_add/remove_device.
849 static int __iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
850 int (*fn
)(struct device
*, void *))
852 struct group_device
*device
;
855 list_for_each_entry(device
, &group
->devices
, list
) {
856 ret
= fn(device
->dev
, data
);
864 int iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
865 int (*fn
)(struct device
*, void *))
869 mutex_lock(&group
->mutex
);
870 ret
= __iommu_group_for_each_dev(group
, data
, fn
);
871 mutex_unlock(&group
->mutex
);
875 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev
);
878 * iommu_group_get - Return the group for a device and increment reference
879 * @dev: get the group that this device belongs to
881 * This function is called by iommu drivers and users to get the group
882 * for the specified device. If found, the group is returned and the group
883 * reference in incremented, else NULL.
885 struct iommu_group
*iommu_group_get(struct device
*dev
)
887 struct iommu_group
*group
= dev
->iommu_group
;
890 kobject_get(group
->devices_kobj
);
894 EXPORT_SYMBOL_GPL(iommu_group_get
);
897 * iommu_group_ref_get - Increment reference on a group
898 * @group: the group to use, must not be NULL
900 * This function is called by iommu drivers to take additional references on an
901 * existing group. Returns the given group for convenience.
903 struct iommu_group
*iommu_group_ref_get(struct iommu_group
*group
)
905 kobject_get(group
->devices_kobj
);
908 EXPORT_SYMBOL_GPL(iommu_group_ref_get
);
911 * iommu_group_put - Decrement group reference
912 * @group: the group to use
914 * This function is called by iommu drivers and users to release the
915 * iommu group. Once the reference count is zero, the group is released.
917 void iommu_group_put(struct iommu_group
*group
)
920 kobject_put(group
->devices_kobj
);
922 EXPORT_SYMBOL_GPL(iommu_group_put
);
925 * iommu_group_register_notifier - Register a notifier for group changes
926 * @group: the group to watch
927 * @nb: notifier block to signal
929 * This function allows iommu group users to track changes in a group.
930 * See include/linux/iommu.h for actions sent via this notifier. Caller
931 * should hold a reference to the group throughout notifier registration.
933 int iommu_group_register_notifier(struct iommu_group
*group
,
934 struct notifier_block
*nb
)
936 return blocking_notifier_chain_register(&group
->notifier
, nb
);
938 EXPORT_SYMBOL_GPL(iommu_group_register_notifier
);
941 * iommu_group_unregister_notifier - Unregister a notifier
942 * @group: the group to watch
943 * @nb: notifier block to signal
945 * Unregister a previously registered group notifier block.
947 int iommu_group_unregister_notifier(struct iommu_group
*group
,
948 struct notifier_block
*nb
)
950 return blocking_notifier_chain_unregister(&group
->notifier
, nb
);
952 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier
);
955 * iommu_register_device_fault_handler() - Register a device fault handler
957 * @handler: the fault handler
958 * @data: private data passed as argument to the handler
960 * When an IOMMU fault event is received, this handler gets called with the
961 * fault event and data as argument. The handler should return 0 on success. If
962 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
963 * complete the fault by calling iommu_page_response() with one of the following
965 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
966 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
967 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
968 * page faults if possible.
970 * Return 0 if the fault handler was installed successfully, or an error.
972 int iommu_register_device_fault_handler(struct device
*dev
,
973 iommu_dev_fault_handler_t handler
,
976 struct dev_iommu
*param
= dev
->iommu
;
982 mutex_lock(¶m
->lock
);
983 /* Only allow one fault handler registered for each device */
984 if (param
->fault_param
) {
990 param
->fault_param
= kzalloc(sizeof(*param
->fault_param
), GFP_KERNEL
);
991 if (!param
->fault_param
) {
996 param
->fault_param
->handler
= handler
;
997 param
->fault_param
->data
= data
;
998 mutex_init(¶m
->fault_param
->lock
);
999 INIT_LIST_HEAD(¶m
->fault_param
->faults
);
1002 mutex_unlock(¶m
->lock
);
1006 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler
);
1009 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1012 * Remove the device fault handler installed with
1013 * iommu_register_device_fault_handler().
1015 * Return 0 on success, or an error.
1017 int iommu_unregister_device_fault_handler(struct device
*dev
)
1019 struct dev_iommu
*param
= dev
->iommu
;
1025 mutex_lock(¶m
->lock
);
1027 if (!param
->fault_param
)
1030 /* we cannot unregister handler if there are pending faults */
1031 if (!list_empty(¶m
->fault_param
->faults
)) {
1036 kfree(param
->fault_param
);
1037 param
->fault_param
= NULL
;
1040 mutex_unlock(¶m
->lock
);
1044 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler
);
1047 * iommu_report_device_fault() - Report fault event to device driver
1049 * @evt: fault event data
1051 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1052 * handler. When this function fails and the fault is recoverable, it is the
1053 * caller's responsibility to complete the fault.
1055 * Return 0 on success, or an error.
1057 int iommu_report_device_fault(struct device
*dev
, struct iommu_fault_event
*evt
)
1059 struct dev_iommu
*param
= dev
->iommu
;
1060 struct iommu_fault_event
*evt_pending
= NULL
;
1061 struct iommu_fault_param
*fparam
;
1067 /* we only report device fault if there is a handler registered */
1068 mutex_lock(¶m
->lock
);
1069 fparam
= param
->fault_param
;
1070 if (!fparam
|| !fparam
->handler
) {
1075 if (evt
->fault
.type
== IOMMU_FAULT_PAGE_REQ
&&
1076 (evt
->fault
.prm
.flags
& IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE
)) {
1077 evt_pending
= kmemdup(evt
, sizeof(struct iommu_fault_event
),
1083 mutex_lock(&fparam
->lock
);
1084 list_add_tail(&evt_pending
->list
, &fparam
->faults
);
1085 mutex_unlock(&fparam
->lock
);
1088 ret
= fparam
->handler(&evt
->fault
, fparam
->data
);
1089 if (ret
&& evt_pending
) {
1090 mutex_lock(&fparam
->lock
);
1091 list_del(&evt_pending
->list
);
1092 mutex_unlock(&fparam
->lock
);
1096 mutex_unlock(¶m
->lock
);
1099 EXPORT_SYMBOL_GPL(iommu_report_device_fault
);
1101 int iommu_page_response(struct device
*dev
,
1102 struct iommu_page_response
*msg
)
1106 struct iommu_fault_event
*evt
;
1107 struct iommu_fault_page_request
*prm
;
1108 struct dev_iommu
*param
= dev
->iommu
;
1109 struct iommu_domain
*domain
= iommu_get_domain_for_dev(dev
);
1111 if (!domain
|| !domain
->ops
->page_response
)
1114 if (!param
|| !param
->fault_param
)
1117 if (msg
->version
!= IOMMU_PAGE_RESP_VERSION_1
||
1118 msg
->flags
& ~IOMMU_PAGE_RESP_PASID_VALID
)
1121 /* Only send response if there is a fault report pending */
1122 mutex_lock(¶m
->fault_param
->lock
);
1123 if (list_empty(¶m
->fault_param
->faults
)) {
1124 dev_warn_ratelimited(dev
, "no pending PRQ, drop response\n");
1128 * Check if we have a matching page request pending to respond,
1129 * otherwise return -EINVAL
1131 list_for_each_entry(evt
, ¶m
->fault_param
->faults
, list
) {
1132 prm
= &evt
->fault
.prm
;
1133 pasid_valid
= prm
->flags
& IOMMU_FAULT_PAGE_REQUEST_PASID_VALID
;
1135 if ((pasid_valid
&& prm
->pasid
!= msg
->pasid
) ||
1136 prm
->grpid
!= msg
->grpid
)
1139 /* Sanitize the reply */
1140 msg
->flags
= pasid_valid
? IOMMU_PAGE_RESP_PASID_VALID
: 0;
1142 ret
= domain
->ops
->page_response(dev
, evt
, msg
);
1143 list_del(&evt
->list
);
1149 mutex_unlock(¶m
->fault_param
->lock
);
1152 EXPORT_SYMBOL_GPL(iommu_page_response
);
1155 * iommu_group_id - Return ID for a group
1156 * @group: the group to ID
1158 * Return the unique ID for the group matching the sysfs group number.
1160 int iommu_group_id(struct iommu_group
*group
)
1164 EXPORT_SYMBOL_GPL(iommu_group_id
);
1166 static struct iommu_group
*get_pci_alias_group(struct pci_dev
*pdev
,
1167 unsigned long *devfns
);
1170 * To consider a PCI device isolated, we require ACS to support Source
1171 * Validation, Request Redirection, Completer Redirection, and Upstream
1172 * Forwarding. This effectively means that devices cannot spoof their
1173 * requester ID, requests and completions cannot be redirected, and all
1174 * transactions are forwarded upstream, even as it passes through a
1175 * bridge where the target device is downstream.
1177 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1180 * For multifunction devices which are not isolated from each other, find
1181 * all the other non-isolated functions and look for existing groups. For
1182 * each function, we also need to look for aliases to or from other devices
1183 * that may already have a group.
1185 static struct iommu_group
*get_pci_function_alias_group(struct pci_dev
*pdev
,
1186 unsigned long *devfns
)
1188 struct pci_dev
*tmp
= NULL
;
1189 struct iommu_group
*group
;
1191 if (!pdev
->multifunction
|| pci_acs_enabled(pdev
, REQ_ACS_FLAGS
))
1194 for_each_pci_dev(tmp
) {
1195 if (tmp
== pdev
|| tmp
->bus
!= pdev
->bus
||
1196 PCI_SLOT(tmp
->devfn
) != PCI_SLOT(pdev
->devfn
) ||
1197 pci_acs_enabled(tmp
, REQ_ACS_FLAGS
))
1200 group
= get_pci_alias_group(tmp
, devfns
);
1211 * Look for aliases to or from the given device for existing groups. DMA
1212 * aliases are only supported on the same bus, therefore the search
1213 * space is quite small (especially since we're really only looking at pcie
1214 * device, and therefore only expect multiple slots on the root complex or
1215 * downstream switch ports). It's conceivable though that a pair of
1216 * multifunction devices could have aliases between them that would cause a
1217 * loop. To prevent this, we use a bitmap to track where we've been.
1219 static struct iommu_group
*get_pci_alias_group(struct pci_dev
*pdev
,
1220 unsigned long *devfns
)
1222 struct pci_dev
*tmp
= NULL
;
1223 struct iommu_group
*group
;
1225 if (test_and_set_bit(pdev
->devfn
& 0xff, devfns
))
1228 group
= iommu_group_get(&pdev
->dev
);
1232 for_each_pci_dev(tmp
) {
1233 if (tmp
== pdev
|| tmp
->bus
!= pdev
->bus
)
1236 /* We alias them or they alias us */
1237 if (pci_devs_are_dma_aliases(pdev
, tmp
)) {
1238 group
= get_pci_alias_group(tmp
, devfns
);
1244 group
= get_pci_function_alias_group(tmp
, devfns
);
1255 struct group_for_pci_data
{
1256 struct pci_dev
*pdev
;
1257 struct iommu_group
*group
;
1261 * DMA alias iterator callback, return the last seen device. Stop and return
1262 * the IOMMU group if we find one along the way.
1264 static int get_pci_alias_or_group(struct pci_dev
*pdev
, u16 alias
, void *opaque
)
1266 struct group_for_pci_data
*data
= opaque
;
1269 data
->group
= iommu_group_get(&pdev
->dev
);
1271 return data
->group
!= NULL
;
1275 * Generic device_group call-back function. It just allocates one
1276 * iommu-group per device.
1278 struct iommu_group
*generic_device_group(struct device
*dev
)
1280 return iommu_group_alloc();
1282 EXPORT_SYMBOL_GPL(generic_device_group
);
1285 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1286 * to find or create an IOMMU group for a device.
1288 struct iommu_group
*pci_device_group(struct device
*dev
)
1290 struct pci_dev
*pdev
= to_pci_dev(dev
);
1291 struct group_for_pci_data data
;
1292 struct pci_bus
*bus
;
1293 struct iommu_group
*group
= NULL
;
1294 u64 devfns
[4] = { 0 };
1296 if (WARN_ON(!dev_is_pci(dev
)))
1297 return ERR_PTR(-EINVAL
);
1300 * Find the upstream DMA alias for the device. A device must not
1301 * be aliased due to topology in order to have its own IOMMU group.
1302 * If we find an alias along the way that already belongs to a
1305 if (pci_for_each_dma_alias(pdev
, get_pci_alias_or_group
, &data
))
1311 * Continue upstream from the point of minimum IOMMU granularity
1312 * due to aliases to the point where devices are protected from
1313 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1316 for (bus
= pdev
->bus
; !pci_is_root_bus(bus
); bus
= bus
->parent
) {
1320 if (pci_acs_path_enabled(bus
->self
, NULL
, REQ_ACS_FLAGS
))
1325 group
= iommu_group_get(&pdev
->dev
);
1331 * Look for existing groups on device aliases. If we alias another
1332 * device or another device aliases us, use the same group.
1334 group
= get_pci_alias_group(pdev
, (unsigned long *)devfns
);
1339 * Look for existing groups on non-isolated functions on the same
1340 * slot and aliases of those funcions, if any. No need to clear
1341 * the search bitmap, the tested devfns are still valid.
1343 group
= get_pci_function_alias_group(pdev
, (unsigned long *)devfns
);
1347 /* No shared group found, allocate new */
1348 return iommu_group_alloc();
1350 EXPORT_SYMBOL_GPL(pci_device_group
);
1352 /* Get the IOMMU group for device on fsl-mc bus */
1353 struct iommu_group
*fsl_mc_device_group(struct device
*dev
)
1355 struct device
*cont_dev
= fsl_mc_cont_dev(dev
);
1356 struct iommu_group
*group
;
1358 group
= iommu_group_get(cont_dev
);
1360 group
= iommu_group_alloc();
1363 EXPORT_SYMBOL_GPL(fsl_mc_device_group
);
1366 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1367 * @dev: target device
1369 * This function is intended to be called by IOMMU drivers and extended to
1370 * support common, bus-defined algorithms when determining or creating the
1371 * IOMMU group for a device. On success, the caller will hold a reference
1372 * to the returned IOMMU group, which will already include the provided
1373 * device. The reference should be released with iommu_group_put().
1375 struct iommu_group
*iommu_group_get_for_dev(struct device
*dev
)
1377 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
1378 struct iommu_group
*group
;
1381 group
= iommu_group_get(dev
);
1386 return ERR_PTR(-EINVAL
);
1388 group
= ops
->device_group(dev
);
1389 if (WARN_ON_ONCE(group
== NULL
))
1390 return ERR_PTR(-EINVAL
);
1396 * Try to allocate a default domain - needs support from the
1399 if (!group
->default_domain
) {
1400 struct iommu_domain
*dom
;
1402 dom
= __iommu_domain_alloc(dev
->bus
, iommu_def_domain_type
);
1403 if (!dom
&& iommu_def_domain_type
!= IOMMU_DOMAIN_DMA
) {
1404 dom
= __iommu_domain_alloc(dev
->bus
, IOMMU_DOMAIN_DMA
);
1407 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1408 iommu_def_domain_type
);
1412 group
->default_domain
= dom
;
1414 group
->domain
= dom
;
1416 if (dom
&& !iommu_dma_strict
) {
1418 iommu_domain_set_attr(dom
,
1419 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
,
1424 ret
= iommu_group_add_device(group
, dev
);
1426 iommu_group_put(group
);
1427 return ERR_PTR(ret
);
1432 EXPORT_SYMBOL_GPL(iommu_group_get_for_dev
);
1434 struct iommu_domain
*iommu_group_default_domain(struct iommu_group
*group
)
1436 return group
->default_domain
;
1439 static int add_iommu_group(struct device
*dev
, void *data
)
1441 int ret
= iommu_probe_device(dev
);
1444 * We ignore -ENODEV errors for now, as they just mean that the
1445 * device is not translated by an IOMMU. We still care about
1446 * other errors and fail to initialize when they happen.
1454 static int remove_iommu_group(struct device
*dev
, void *data
)
1456 iommu_release_device(dev
);
1461 static int iommu_bus_notifier(struct notifier_block
*nb
,
1462 unsigned long action
, void *data
)
1464 unsigned long group_action
= 0;
1465 struct device
*dev
= data
;
1466 struct iommu_group
*group
;
1469 * ADD/DEL call into iommu driver ops if provided, which may
1470 * result in ADD/DEL notifiers to group->notifier
1472 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
1475 ret
= iommu_probe_device(dev
);
1476 return (ret
) ? NOTIFY_DONE
: NOTIFY_OK
;
1477 } else if (action
== BUS_NOTIFY_REMOVED_DEVICE
) {
1478 iommu_release_device(dev
);
1483 * Remaining BUS_NOTIFYs get filtered and republished to the
1484 * group, if anyone is listening
1486 group
= iommu_group_get(dev
);
1491 case BUS_NOTIFY_BIND_DRIVER
:
1492 group_action
= IOMMU_GROUP_NOTIFY_BIND_DRIVER
;
1494 case BUS_NOTIFY_BOUND_DRIVER
:
1495 group_action
= IOMMU_GROUP_NOTIFY_BOUND_DRIVER
;
1497 case BUS_NOTIFY_UNBIND_DRIVER
:
1498 group_action
= IOMMU_GROUP_NOTIFY_UNBIND_DRIVER
;
1500 case BUS_NOTIFY_UNBOUND_DRIVER
:
1501 group_action
= IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER
;
1506 blocking_notifier_call_chain(&group
->notifier
,
1509 iommu_group_put(group
);
1513 static int iommu_bus_init(struct bus_type
*bus
, const struct iommu_ops
*ops
)
1516 struct notifier_block
*nb
;
1518 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
1522 nb
->notifier_call
= iommu_bus_notifier
;
1524 err
= bus_register_notifier(bus
, nb
);
1528 err
= bus_for_each_dev(bus
, NULL
, NULL
, add_iommu_group
);
1537 bus_for_each_dev(bus
, NULL
, NULL
, remove_iommu_group
);
1538 bus_unregister_notifier(bus
, nb
);
1547 * bus_set_iommu - set iommu-callbacks for the bus
1549 * @ops: the callbacks provided by the iommu-driver
1551 * This function is called by an iommu driver to set the iommu methods
1552 * used for a particular bus. Drivers for devices on that bus can use
1553 * the iommu-api after these ops are registered.
1554 * This special function is needed because IOMMUs are usually devices on
1555 * the bus itself, so the iommu drivers are not initialized when the bus
1556 * is set up. With this function the iommu-driver can set the iommu-ops
1559 int bus_set_iommu(struct bus_type
*bus
, const struct iommu_ops
*ops
)
1564 bus
->iommu_ops
= NULL
;
1568 if (bus
->iommu_ops
!= NULL
)
1571 bus
->iommu_ops
= ops
;
1573 /* Do IOMMU specific setup for this bus-type */
1574 err
= iommu_bus_init(bus
, ops
);
1576 bus
->iommu_ops
= NULL
;
1580 EXPORT_SYMBOL_GPL(bus_set_iommu
);
1582 bool iommu_present(struct bus_type
*bus
)
1584 return bus
->iommu_ops
!= NULL
;
1586 EXPORT_SYMBOL_GPL(iommu_present
);
1588 bool iommu_capable(struct bus_type
*bus
, enum iommu_cap cap
)
1590 if (!bus
->iommu_ops
|| !bus
->iommu_ops
->capable
)
1593 return bus
->iommu_ops
->capable(cap
);
1595 EXPORT_SYMBOL_GPL(iommu_capable
);
1598 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1599 * @domain: iommu domain
1600 * @handler: fault handler
1601 * @token: user data, will be passed back to the fault handler
1603 * This function should be used by IOMMU users which want to be notified
1604 * whenever an IOMMU fault happens.
1606 * The fault handler itself should return 0 on success, and an appropriate
1607 * error code otherwise.
1609 void iommu_set_fault_handler(struct iommu_domain
*domain
,
1610 iommu_fault_handler_t handler
,
1615 domain
->handler
= handler
;
1616 domain
->handler_token
= token
;
1618 EXPORT_SYMBOL_GPL(iommu_set_fault_handler
);
1620 static struct iommu_domain
*__iommu_domain_alloc(struct bus_type
*bus
,
1623 struct iommu_domain
*domain
;
1625 if (bus
== NULL
|| bus
->iommu_ops
== NULL
)
1628 domain
= bus
->iommu_ops
->domain_alloc(type
);
1632 domain
->ops
= bus
->iommu_ops
;
1633 domain
->type
= type
;
1634 /* Assume all sizes by default; the driver may override this later */
1635 domain
->pgsize_bitmap
= bus
->iommu_ops
->pgsize_bitmap
;
1640 struct iommu_domain
*iommu_domain_alloc(struct bus_type
*bus
)
1642 return __iommu_domain_alloc(bus
, IOMMU_DOMAIN_UNMANAGED
);
1644 EXPORT_SYMBOL_GPL(iommu_domain_alloc
);
1646 void iommu_domain_free(struct iommu_domain
*domain
)
1648 domain
->ops
->domain_free(domain
);
1650 EXPORT_SYMBOL_GPL(iommu_domain_free
);
1652 static int __iommu_attach_device(struct iommu_domain
*domain
,
1656 if ((domain
->ops
->is_attach_deferred
!= NULL
) &&
1657 domain
->ops
->is_attach_deferred(domain
, dev
))
1660 if (unlikely(domain
->ops
->attach_dev
== NULL
))
1663 ret
= domain
->ops
->attach_dev(domain
, dev
);
1665 trace_attach_device_to_domain(dev
);
1669 int iommu_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
1671 struct iommu_group
*group
;
1674 group
= iommu_group_get(dev
);
1679 * Lock the group to make sure the device-count doesn't
1680 * change while we are attaching
1682 mutex_lock(&group
->mutex
);
1684 if (iommu_group_device_count(group
) != 1)
1687 ret
= __iommu_attach_group(domain
, group
);
1690 mutex_unlock(&group
->mutex
);
1691 iommu_group_put(group
);
1695 EXPORT_SYMBOL_GPL(iommu_attach_device
);
1697 int iommu_cache_invalidate(struct iommu_domain
*domain
, struct device
*dev
,
1698 struct iommu_cache_invalidate_info
*inv_info
)
1700 if (unlikely(!domain
->ops
->cache_invalidate
))
1703 return domain
->ops
->cache_invalidate(domain
, dev
, inv_info
);
1705 EXPORT_SYMBOL_GPL(iommu_cache_invalidate
);
1707 int iommu_sva_bind_gpasid(struct iommu_domain
*domain
,
1708 struct device
*dev
, struct iommu_gpasid_bind_data
*data
)
1710 if (unlikely(!domain
->ops
->sva_bind_gpasid
))
1713 return domain
->ops
->sva_bind_gpasid(domain
, dev
, data
);
1715 EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid
);
1717 int iommu_sva_unbind_gpasid(struct iommu_domain
*domain
, struct device
*dev
,
1720 if (unlikely(!domain
->ops
->sva_unbind_gpasid
))
1723 return domain
->ops
->sva_unbind_gpasid(dev
, pasid
);
1725 EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid
);
1727 static void __iommu_detach_device(struct iommu_domain
*domain
,
1730 if ((domain
->ops
->is_attach_deferred
!= NULL
) &&
1731 domain
->ops
->is_attach_deferred(domain
, dev
))
1734 if (unlikely(domain
->ops
->detach_dev
== NULL
))
1737 domain
->ops
->detach_dev(domain
, dev
);
1738 trace_detach_device_from_domain(dev
);
1741 void iommu_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
1743 struct iommu_group
*group
;
1745 group
= iommu_group_get(dev
);
1749 mutex_lock(&group
->mutex
);
1750 if (iommu_group_device_count(group
) != 1) {
1755 __iommu_detach_group(domain
, group
);
1758 mutex_unlock(&group
->mutex
);
1759 iommu_group_put(group
);
1761 EXPORT_SYMBOL_GPL(iommu_detach_device
);
1763 struct iommu_domain
*iommu_get_domain_for_dev(struct device
*dev
)
1765 struct iommu_domain
*domain
;
1766 struct iommu_group
*group
;
1768 group
= iommu_group_get(dev
);
1772 domain
= group
->domain
;
1774 iommu_group_put(group
);
1778 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev
);
1781 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1782 * guarantees that the group and its default domain are valid and correct.
1784 struct iommu_domain
*iommu_get_dma_domain(struct device
*dev
)
1786 return dev
->iommu_group
->default_domain
;
1790 * IOMMU groups are really the natural working unit of the IOMMU, but
1791 * the IOMMU API works on domains and devices. Bridge that gap by
1792 * iterating over the devices in a group. Ideally we'd have a single
1793 * device which represents the requestor ID of the group, but we also
1794 * allow IOMMU drivers to create policy defined minimum sets, where
1795 * the physical hardware may be able to distiguish members, but we
1796 * wish to group them at a higher level (ex. untrusted multi-function
1797 * PCI devices). Thus we attach each device.
1799 static int iommu_group_do_attach_device(struct device
*dev
, void *data
)
1801 struct iommu_domain
*domain
= data
;
1803 return __iommu_attach_device(domain
, dev
);
1806 static int __iommu_attach_group(struct iommu_domain
*domain
,
1807 struct iommu_group
*group
)
1811 if (group
->default_domain
&& group
->domain
!= group
->default_domain
)
1814 ret
= __iommu_group_for_each_dev(group
, domain
,
1815 iommu_group_do_attach_device
);
1817 group
->domain
= domain
;
1822 int iommu_attach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
1826 mutex_lock(&group
->mutex
);
1827 ret
= __iommu_attach_group(domain
, group
);
1828 mutex_unlock(&group
->mutex
);
1832 EXPORT_SYMBOL_GPL(iommu_attach_group
);
1834 static int iommu_group_do_detach_device(struct device
*dev
, void *data
)
1836 struct iommu_domain
*domain
= data
;
1838 __iommu_detach_device(domain
, dev
);
1843 static void __iommu_detach_group(struct iommu_domain
*domain
,
1844 struct iommu_group
*group
)
1848 if (!group
->default_domain
) {
1849 __iommu_group_for_each_dev(group
, domain
,
1850 iommu_group_do_detach_device
);
1851 group
->domain
= NULL
;
1855 if (group
->domain
== group
->default_domain
)
1858 /* Detach by re-attaching to the default domain */
1859 ret
= __iommu_group_for_each_dev(group
, group
->default_domain
,
1860 iommu_group_do_attach_device
);
1864 group
->domain
= group
->default_domain
;
1867 void iommu_detach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
1869 mutex_lock(&group
->mutex
);
1870 __iommu_detach_group(domain
, group
);
1871 mutex_unlock(&group
->mutex
);
1873 EXPORT_SYMBOL_GPL(iommu_detach_group
);
1875 phys_addr_t
iommu_iova_to_phys(struct iommu_domain
*domain
, dma_addr_t iova
)
1877 if (unlikely(domain
->ops
->iova_to_phys
== NULL
))
1880 return domain
->ops
->iova_to_phys(domain
, iova
);
1882 EXPORT_SYMBOL_GPL(iommu_iova_to_phys
);
1884 static size_t iommu_pgsize(struct iommu_domain
*domain
,
1885 unsigned long addr_merge
, size_t size
)
1887 unsigned int pgsize_idx
;
1890 /* Max page size that still fits into 'size' */
1891 pgsize_idx
= __fls(size
);
1893 /* need to consider alignment requirements ? */
1894 if (likely(addr_merge
)) {
1895 /* Max page size allowed by address */
1896 unsigned int align_pgsize_idx
= __ffs(addr_merge
);
1897 pgsize_idx
= min(pgsize_idx
, align_pgsize_idx
);
1900 /* build a mask of acceptable page sizes */
1901 pgsize
= (1UL << (pgsize_idx
+ 1)) - 1;
1903 /* throw away page sizes not supported by the hardware */
1904 pgsize
&= domain
->pgsize_bitmap
;
1906 /* make sure we're still sane */
1909 /* pick the biggest page */
1910 pgsize_idx
= __fls(pgsize
);
1911 pgsize
= 1UL << pgsize_idx
;
1916 int __iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
1917 phys_addr_t paddr
, size_t size
, int prot
, gfp_t gfp
)
1919 const struct iommu_ops
*ops
= domain
->ops
;
1920 unsigned long orig_iova
= iova
;
1921 unsigned int min_pagesz
;
1922 size_t orig_size
= size
;
1923 phys_addr_t orig_paddr
= paddr
;
1926 if (unlikely(ops
->map
== NULL
||
1927 domain
->pgsize_bitmap
== 0UL))
1930 if (unlikely(!(domain
->type
& __IOMMU_DOMAIN_PAGING
)))
1933 /* find out the minimum page size supported */
1934 min_pagesz
= 1 << __ffs(domain
->pgsize_bitmap
);
1937 * both the virtual address and the physical one, as well as
1938 * the size of the mapping, must be aligned (at least) to the
1939 * size of the smallest page supported by the hardware
1941 if (!IS_ALIGNED(iova
| paddr
| size
, min_pagesz
)) {
1942 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1943 iova
, &paddr
, size
, min_pagesz
);
1947 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova
, &paddr
, size
);
1950 size_t pgsize
= iommu_pgsize(domain
, iova
| paddr
, size
);
1952 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1953 iova
, &paddr
, pgsize
);
1954 ret
= ops
->map(domain
, iova
, paddr
, pgsize
, prot
, gfp
);
1964 if (ops
->iotlb_sync_map
)
1965 ops
->iotlb_sync_map(domain
);
1967 /* unroll mapping in case something went wrong */
1969 iommu_unmap(domain
, orig_iova
, orig_size
- size
);
1971 trace_map(orig_iova
, orig_paddr
, orig_size
);
1976 int iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
1977 phys_addr_t paddr
, size_t size
, int prot
)
1980 return __iommu_map(domain
, iova
, paddr
, size
, prot
, GFP_KERNEL
);
1982 EXPORT_SYMBOL_GPL(iommu_map
);
1984 int iommu_map_atomic(struct iommu_domain
*domain
, unsigned long iova
,
1985 phys_addr_t paddr
, size_t size
, int prot
)
1987 return __iommu_map(domain
, iova
, paddr
, size
, prot
, GFP_ATOMIC
);
1989 EXPORT_SYMBOL_GPL(iommu_map_atomic
);
1991 static size_t __iommu_unmap(struct iommu_domain
*domain
,
1992 unsigned long iova
, size_t size
,
1993 struct iommu_iotlb_gather
*iotlb_gather
)
1995 const struct iommu_ops
*ops
= domain
->ops
;
1996 size_t unmapped_page
, unmapped
= 0;
1997 unsigned long orig_iova
= iova
;
1998 unsigned int min_pagesz
;
2000 if (unlikely(ops
->unmap
== NULL
||
2001 domain
->pgsize_bitmap
== 0UL))
2004 if (unlikely(!(domain
->type
& __IOMMU_DOMAIN_PAGING
)))
2007 /* find out the minimum page size supported */
2008 min_pagesz
= 1 << __ffs(domain
->pgsize_bitmap
);
2011 * The virtual address, as well as the size of the mapping, must be
2012 * aligned (at least) to the size of the smallest page supported
2015 if (!IS_ALIGNED(iova
| size
, min_pagesz
)) {
2016 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2017 iova
, size
, min_pagesz
);
2021 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova
, size
);
2024 * Keep iterating until we either unmap 'size' bytes (or more)
2025 * or we hit an area that isn't mapped.
2027 while (unmapped
< size
) {
2028 size_t pgsize
= iommu_pgsize(domain
, iova
, size
- unmapped
);
2030 unmapped_page
= ops
->unmap(domain
, iova
, pgsize
, iotlb_gather
);
2034 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2035 iova
, unmapped_page
);
2037 iova
+= unmapped_page
;
2038 unmapped
+= unmapped_page
;
2041 trace_unmap(orig_iova
, size
, unmapped
);
2045 size_t iommu_unmap(struct iommu_domain
*domain
,
2046 unsigned long iova
, size_t size
)
2048 struct iommu_iotlb_gather iotlb_gather
;
2051 iommu_iotlb_gather_init(&iotlb_gather
);
2052 ret
= __iommu_unmap(domain
, iova
, size
, &iotlb_gather
);
2053 iommu_tlb_sync(domain
, &iotlb_gather
);
2057 EXPORT_SYMBOL_GPL(iommu_unmap
);
2059 size_t iommu_unmap_fast(struct iommu_domain
*domain
,
2060 unsigned long iova
, size_t size
,
2061 struct iommu_iotlb_gather
*iotlb_gather
)
2063 return __iommu_unmap(domain
, iova
, size
, iotlb_gather
);
2065 EXPORT_SYMBOL_GPL(iommu_unmap_fast
);
2067 size_t __iommu_map_sg(struct iommu_domain
*domain
, unsigned long iova
,
2068 struct scatterlist
*sg
, unsigned int nents
, int prot
,
2071 size_t len
= 0, mapped
= 0;
2076 while (i
<= nents
) {
2077 phys_addr_t s_phys
= sg_phys(sg
);
2079 if (len
&& s_phys
!= start
+ len
) {
2080 ret
= __iommu_map(domain
, iova
+ mapped
, start
,
2104 /* undo mappings already done */
2105 iommu_unmap(domain
, iova
, mapped
);
2111 size_t iommu_map_sg(struct iommu_domain
*domain
, unsigned long iova
,
2112 struct scatterlist
*sg
, unsigned int nents
, int prot
)
2115 return __iommu_map_sg(domain
, iova
, sg
, nents
, prot
, GFP_KERNEL
);
2117 EXPORT_SYMBOL_GPL(iommu_map_sg
);
2119 size_t iommu_map_sg_atomic(struct iommu_domain
*domain
, unsigned long iova
,
2120 struct scatterlist
*sg
, unsigned int nents
, int prot
)
2122 return __iommu_map_sg(domain
, iova
, sg
, nents
, prot
, GFP_ATOMIC
);
2124 EXPORT_SYMBOL_GPL(iommu_map_sg_atomic
);
2126 int iommu_domain_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
2127 phys_addr_t paddr
, u64 size
, int prot
)
2129 if (unlikely(domain
->ops
->domain_window_enable
== NULL
))
2132 return domain
->ops
->domain_window_enable(domain
, wnd_nr
, paddr
, size
,
2135 EXPORT_SYMBOL_GPL(iommu_domain_window_enable
);
2137 void iommu_domain_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
2139 if (unlikely(domain
->ops
->domain_window_disable
== NULL
))
2142 return domain
->ops
->domain_window_disable(domain
, wnd_nr
);
2144 EXPORT_SYMBOL_GPL(iommu_domain_window_disable
);
2147 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2148 * @domain: the iommu domain where the fault has happened
2149 * @dev: the device where the fault has happened
2150 * @iova: the faulting address
2151 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2153 * This function should be called by the low-level IOMMU implementations
2154 * whenever IOMMU faults happen, to allow high-level users, that are
2155 * interested in such events, to know about them.
2157 * This event may be useful for several possible use cases:
2158 * - mere logging of the event
2159 * - dynamic TLB/PTE loading
2160 * - if restarting of the faulting device is required
2162 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2163 * PTE/TLB loading will one day be supported, implementations will be able
2164 * to tell whether it succeeded or not according to this return value).
2166 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2167 * (though fault handlers can also return -ENOSYS, in case they want to
2168 * elicit the default behavior of the IOMMU drivers).
2170 int report_iommu_fault(struct iommu_domain
*domain
, struct device
*dev
,
2171 unsigned long iova
, int flags
)
2176 * if upper layers showed interest and installed a fault handler,
2179 if (domain
->handler
)
2180 ret
= domain
->handler(domain
, dev
, iova
, flags
,
2181 domain
->handler_token
);
2183 trace_io_page_fault(dev
, iova
, flags
);
2186 EXPORT_SYMBOL_GPL(report_iommu_fault
);
2188 static int __init
iommu_init(void)
2190 iommu_group_kset
= kset_create_and_add("iommu_groups",
2192 BUG_ON(!iommu_group_kset
);
2194 iommu_debugfs_setup();
2198 core_initcall(iommu_init
);
2200 int iommu_domain_get_attr(struct iommu_domain
*domain
,
2201 enum iommu_attr attr
, void *data
)
2203 struct iommu_domain_geometry
*geometry
;
2208 case DOMAIN_ATTR_GEOMETRY
:
2210 *geometry
= domain
->geometry
;
2213 case DOMAIN_ATTR_PAGING
:
2215 *paging
= (domain
->pgsize_bitmap
!= 0UL);
2218 if (!domain
->ops
->domain_get_attr
)
2221 ret
= domain
->ops
->domain_get_attr(domain
, attr
, data
);
2226 EXPORT_SYMBOL_GPL(iommu_domain_get_attr
);
2228 int iommu_domain_set_attr(struct iommu_domain
*domain
,
2229 enum iommu_attr attr
, void *data
)
2235 if (domain
->ops
->domain_set_attr
== NULL
)
2238 ret
= domain
->ops
->domain_set_attr(domain
, attr
, data
);
2243 EXPORT_SYMBOL_GPL(iommu_domain_set_attr
);
2245 void iommu_get_resv_regions(struct device
*dev
, struct list_head
*list
)
2247 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2249 if (ops
&& ops
->get_resv_regions
)
2250 ops
->get_resv_regions(dev
, list
);
2253 void iommu_put_resv_regions(struct device
*dev
, struct list_head
*list
)
2255 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2257 if (ops
&& ops
->put_resv_regions
)
2258 ops
->put_resv_regions(dev
, list
);
2262 * generic_iommu_put_resv_regions - Reserved region driver helper
2263 * @dev: device for which to free reserved regions
2264 * @list: reserved region list for device
2266 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2267 * for simple reservations. Memory allocated for each reserved region will be
2268 * freed. If an IOMMU driver allocates additional resources per region, it is
2269 * going to have to implement a custom callback.
2271 void generic_iommu_put_resv_regions(struct device
*dev
, struct list_head
*list
)
2273 struct iommu_resv_region
*entry
, *next
;
2275 list_for_each_entry_safe(entry
, next
, list
, list
)
2278 EXPORT_SYMBOL(generic_iommu_put_resv_regions
);
2280 struct iommu_resv_region
*iommu_alloc_resv_region(phys_addr_t start
,
2281 size_t length
, int prot
,
2282 enum iommu_resv_type type
)
2284 struct iommu_resv_region
*region
;
2286 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
2290 INIT_LIST_HEAD(®ion
->list
);
2291 region
->start
= start
;
2292 region
->length
= length
;
2293 region
->prot
= prot
;
2294 region
->type
= type
;
2297 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region
);
2300 request_default_domain_for_dev(struct device
*dev
, unsigned long type
)
2302 struct iommu_domain
*domain
;
2303 struct iommu_group
*group
;
2306 /* Device must already be in a group before calling this function */
2307 group
= iommu_group_get(dev
);
2311 mutex_lock(&group
->mutex
);
2314 if (group
->default_domain
&& group
->default_domain
->type
== type
)
2317 /* Don't change mappings of existing devices */
2319 if (iommu_group_device_count(group
) != 1)
2323 domain
= __iommu_domain_alloc(dev
->bus
, type
);
2327 /* Attach the device to the domain */
2328 ret
= __iommu_attach_group(domain
, group
);
2330 iommu_domain_free(domain
);
2334 /* Make the domain the default for this group */
2335 if (group
->default_domain
)
2336 iommu_domain_free(group
->default_domain
);
2337 group
->default_domain
= domain
;
2339 iommu_group_create_direct_mappings(group
, dev
);
2341 dev_info(dev
, "Using iommu %s mapping\n",
2342 type
== IOMMU_DOMAIN_DMA
? "dma" : "direct");
2346 mutex_unlock(&group
->mutex
);
2347 iommu_group_put(group
);
2352 /* Request that a device is direct mapped by the IOMMU */
2353 int iommu_request_dm_for_dev(struct device
*dev
)
2355 return request_default_domain_for_dev(dev
, IOMMU_DOMAIN_IDENTITY
);
2358 /* Request that a device can't be direct mapped by the IOMMU */
2359 int iommu_request_dma_domain_for_dev(struct device
*dev
)
2361 return request_default_domain_for_dev(dev
, IOMMU_DOMAIN_DMA
);
2364 void iommu_set_default_passthrough(bool cmd_line
)
2367 iommu_set_cmd_line_dma_api();
2369 iommu_def_domain_type
= IOMMU_DOMAIN_IDENTITY
;
2372 void iommu_set_default_translated(bool cmd_line
)
2375 iommu_set_cmd_line_dma_api();
2377 iommu_def_domain_type
= IOMMU_DOMAIN_DMA
;
2380 bool iommu_default_passthrough(void)
2382 return iommu_def_domain_type
== IOMMU_DOMAIN_IDENTITY
;
2384 EXPORT_SYMBOL_GPL(iommu_default_passthrough
);
2386 const struct iommu_ops
*iommu_ops_from_fwnode(struct fwnode_handle
*fwnode
)
2388 const struct iommu_ops
*ops
= NULL
;
2389 struct iommu_device
*iommu
;
2391 spin_lock(&iommu_device_lock
);
2392 list_for_each_entry(iommu
, &iommu_device_list
, list
)
2393 if (iommu
->fwnode
== fwnode
) {
2397 spin_unlock(&iommu_device_lock
);
2401 int iommu_fwspec_init(struct device
*dev
, struct fwnode_handle
*iommu_fwnode
,
2402 const struct iommu_ops
*ops
)
2404 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
2407 return ops
== fwspec
->ops
? 0 : -EINVAL
;
2409 if (!dev_iommu_get(dev
))
2412 /* Preallocate for the overwhelmingly common case of 1 ID */
2413 fwspec
= kzalloc(struct_size(fwspec
, ids
, 1), GFP_KERNEL
);
2417 of_node_get(to_of_node(iommu_fwnode
));
2418 fwspec
->iommu_fwnode
= iommu_fwnode
;
2420 dev_iommu_fwspec_set(dev
, fwspec
);
2423 EXPORT_SYMBOL_GPL(iommu_fwspec_init
);
2425 void iommu_fwspec_free(struct device
*dev
)
2427 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
2430 fwnode_handle_put(fwspec
->iommu_fwnode
);
2432 dev_iommu_fwspec_set(dev
, NULL
);
2435 EXPORT_SYMBOL_GPL(iommu_fwspec_free
);
2437 int iommu_fwspec_add_ids(struct device
*dev
, u32
*ids
, int num_ids
)
2439 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
2445 new_num
= fwspec
->num_ids
+ num_ids
;
2447 fwspec
= krealloc(fwspec
, struct_size(fwspec
, ids
, new_num
),
2452 dev_iommu_fwspec_set(dev
, fwspec
);
2455 for (i
= 0; i
< num_ids
; i
++)
2456 fwspec
->ids
[fwspec
->num_ids
+ i
] = ids
[i
];
2458 fwspec
->num_ids
= new_num
;
2461 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids
);
2464 * Per device IOMMU features.
2466 bool iommu_dev_has_feature(struct device
*dev
, enum iommu_dev_features feat
)
2468 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2470 if (ops
&& ops
->dev_has_feat
)
2471 return ops
->dev_has_feat(dev
, feat
);
2475 EXPORT_SYMBOL_GPL(iommu_dev_has_feature
);
2477 int iommu_dev_enable_feature(struct device
*dev
, enum iommu_dev_features feat
)
2479 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2481 if (ops
&& ops
->dev_enable_feat
)
2482 return ops
->dev_enable_feat(dev
, feat
);
2486 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature
);
2489 * The device drivers should do the necessary cleanups before calling this.
2490 * For example, before disabling the aux-domain feature, the device driver
2491 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2493 int iommu_dev_disable_feature(struct device
*dev
, enum iommu_dev_features feat
)
2495 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2497 if (ops
&& ops
->dev_disable_feat
)
2498 return ops
->dev_disable_feat(dev
, feat
);
2502 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature
);
2504 bool iommu_dev_feature_enabled(struct device
*dev
, enum iommu_dev_features feat
)
2506 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2508 if (ops
&& ops
->dev_feat_enabled
)
2509 return ops
->dev_feat_enabled(dev
, feat
);
2513 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled
);
2516 * Aux-domain specific attach/detach.
2518 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2519 * true. Also, as long as domains are attached to a device through this
2520 * interface, any tries to call iommu_attach_device() should fail
2521 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2522 * This should make us safe against a device being attached to a guest as a
2523 * whole while there are still pasid users on it (aux and sva).
2525 int iommu_aux_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
2529 if (domain
->ops
->aux_attach_dev
)
2530 ret
= domain
->ops
->aux_attach_dev(domain
, dev
);
2533 trace_attach_device_to_domain(dev
);
2537 EXPORT_SYMBOL_GPL(iommu_aux_attach_device
);
2539 void iommu_aux_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
2541 if (domain
->ops
->aux_detach_dev
) {
2542 domain
->ops
->aux_detach_dev(domain
, dev
);
2543 trace_detach_device_from_domain(dev
);
2546 EXPORT_SYMBOL_GPL(iommu_aux_detach_device
);
2548 int iommu_aux_get_pasid(struct iommu_domain
*domain
, struct device
*dev
)
2552 if (domain
->ops
->aux_get_pasid
)
2553 ret
= domain
->ops
->aux_get_pasid(domain
, dev
);
2557 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid
);
2560 * iommu_sva_bind_device() - Bind a process address space to a device
2562 * @mm: the mm to bind, caller must hold a reference to it
2564 * Create a bond between device and address space, allowing the device to access
2565 * the mm using the returned PASID. If a bond already exists between @device and
2566 * @mm, it is returned and an additional reference is taken. Caller must call
2567 * iommu_sva_unbind_device() to release each reference.
2569 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2570 * initialize the required SVA features.
2572 * On error, returns an ERR_PTR value.
2575 iommu_sva_bind_device(struct device
*dev
, struct mm_struct
*mm
, void *drvdata
)
2577 struct iommu_group
*group
;
2578 struct iommu_sva
*handle
= ERR_PTR(-EINVAL
);
2579 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2581 if (!ops
|| !ops
->sva_bind
)
2582 return ERR_PTR(-ENODEV
);
2584 group
= iommu_group_get(dev
);
2586 return ERR_PTR(-ENODEV
);
2588 /* Ensure device count and domain don't change while we're binding */
2589 mutex_lock(&group
->mutex
);
2592 * To keep things simple, SVA currently doesn't support IOMMU groups
2593 * with more than one device. Existing SVA-capable systems are not
2594 * affected by the problems that required IOMMU groups (lack of ACS
2595 * isolation, device ID aliasing and other hardware issues).
2597 if (iommu_group_device_count(group
) != 1)
2600 handle
= ops
->sva_bind(dev
, mm
, drvdata
);
2603 mutex_unlock(&group
->mutex
);
2604 iommu_group_put(group
);
2608 EXPORT_SYMBOL_GPL(iommu_sva_bind_device
);
2611 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2612 * @handle: the handle returned by iommu_sva_bind_device()
2614 * Put reference to a bond between device and address space. The device should
2615 * not be issuing any more transaction for this PASID. All outstanding page
2616 * requests for this PASID must have been flushed to the IOMMU.
2618 * Returns 0 on success, or an error value
2620 void iommu_sva_unbind_device(struct iommu_sva
*handle
)
2622 struct iommu_group
*group
;
2623 struct device
*dev
= handle
->dev
;
2624 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
2626 if (!ops
|| !ops
->sva_unbind
)
2629 group
= iommu_group_get(dev
);
2633 mutex_lock(&group
->mutex
);
2634 ops
->sva_unbind(handle
);
2635 mutex_unlock(&group
->mutex
);
2637 iommu_group_put(group
);
2639 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device
);
2641 int iommu_sva_set_ops(struct iommu_sva
*handle
,
2642 const struct iommu_sva_ops
*sva_ops
)
2644 if (handle
->ops
&& handle
->ops
!= sva_ops
)
2647 handle
->ops
= sva_ops
;
2650 EXPORT_SYMBOL_GPL(iommu_sva_set_ops
);
2652 int iommu_sva_get_pasid(struct iommu_sva
*handle
)
2654 const struct iommu_ops
*ops
= handle
->dev
->bus
->iommu_ops
;
2656 if (!ops
|| !ops
->sva_get_pasid
)
2657 return IOMMU_PASID_INVALID
;
2659 return ops
->sva_get_pasid(handle
);
2661 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid
);