gpio: of: Break out OF-only code
[linux/fpc-iii.git] / drivers / iommu / iommu.c
blob0c674d80c37fd5768fa62296f6be72f97c5dd245
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 */
7 #define pr_fmt(fmt) "iommu: " fmt
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/iommu.h>
18 #include <linux/idr.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/pci.h>
22 #include <linux/bitops.h>
23 #include <linux/property.h>
24 #include <linux/fsl/mc.h>
25 #include <trace/events/iommu.h>
27 static struct kset *iommu_group_kset;
28 static DEFINE_IDA(iommu_group_ida);
29 #ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
30 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
31 #else
32 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
33 #endif
34 static bool iommu_dma_strict __read_mostly = true;
36 struct iommu_group {
37 struct kobject kobj;
38 struct kobject *devices_kobj;
39 struct list_head devices;
40 struct mutex mutex;
41 struct blocking_notifier_head notifier;
42 void *iommu_data;
43 void (*iommu_data_release)(void *iommu_data);
44 char *name;
45 int id;
46 struct iommu_domain *default_domain;
47 struct iommu_domain *domain;
50 struct group_device {
51 struct list_head list;
52 struct device *dev;
53 char *name;
56 struct iommu_group_attribute {
57 struct attribute attr;
58 ssize_t (*show)(struct iommu_group *group, char *buf);
59 ssize_t (*store)(struct iommu_group *group,
60 const char *buf, size_t count);
63 static const char * const iommu_group_resv_type_string[] = {
64 [IOMMU_RESV_DIRECT] = "direct",
65 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
66 [IOMMU_RESV_RESERVED] = "reserved",
67 [IOMMU_RESV_MSI] = "msi",
68 [IOMMU_RESV_SW_MSI] = "msi",
71 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
72 struct iommu_group_attribute iommu_group_attr_##_name = \
73 __ATTR(_name, _mode, _show, _store)
75 #define to_iommu_group_attr(_attr) \
76 container_of(_attr, struct iommu_group_attribute, attr)
77 #define to_iommu_group(_kobj) \
78 container_of(_kobj, struct iommu_group, kobj)
80 static LIST_HEAD(iommu_device_list);
81 static DEFINE_SPINLOCK(iommu_device_lock);
83 int iommu_device_register(struct iommu_device *iommu)
85 spin_lock(&iommu_device_lock);
86 list_add_tail(&iommu->list, &iommu_device_list);
87 spin_unlock(&iommu_device_lock);
89 return 0;
92 void iommu_device_unregister(struct iommu_device *iommu)
94 spin_lock(&iommu_device_lock);
95 list_del(&iommu->list);
96 spin_unlock(&iommu_device_lock);
99 static struct iommu_param *iommu_get_dev_param(struct device *dev)
101 struct iommu_param *param = dev->iommu_param;
103 if (param)
104 return param;
106 param = kzalloc(sizeof(*param), GFP_KERNEL);
107 if (!param)
108 return NULL;
110 mutex_init(&param->lock);
111 dev->iommu_param = param;
112 return param;
115 static void iommu_free_dev_param(struct device *dev)
117 kfree(dev->iommu_param);
118 dev->iommu_param = NULL;
121 int iommu_probe_device(struct device *dev)
123 const struct iommu_ops *ops = dev->bus->iommu_ops;
124 int ret;
126 WARN_ON(dev->iommu_group);
127 if (!ops)
128 return -EINVAL;
130 if (!iommu_get_dev_param(dev))
131 return -ENOMEM;
133 ret = ops->add_device(dev);
134 if (ret)
135 iommu_free_dev_param(dev);
137 return ret;
140 void iommu_release_device(struct device *dev)
142 const struct iommu_ops *ops = dev->bus->iommu_ops;
144 if (dev->iommu_group)
145 ops->remove_device(dev);
147 iommu_free_dev_param(dev);
150 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
151 unsigned type);
152 static int __iommu_attach_device(struct iommu_domain *domain,
153 struct device *dev);
154 static int __iommu_attach_group(struct iommu_domain *domain,
155 struct iommu_group *group);
156 static void __iommu_detach_group(struct iommu_domain *domain,
157 struct iommu_group *group);
159 static int __init iommu_set_def_domain_type(char *str)
161 bool pt;
162 int ret;
164 ret = kstrtobool(str, &pt);
165 if (ret)
166 return ret;
168 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
169 return 0;
171 early_param("iommu.passthrough", iommu_set_def_domain_type);
173 static int __init iommu_dma_setup(char *str)
175 return kstrtobool(str, &iommu_dma_strict);
177 early_param("iommu.strict", iommu_dma_setup);
179 static ssize_t iommu_group_attr_show(struct kobject *kobj,
180 struct attribute *__attr, char *buf)
182 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
183 struct iommu_group *group = to_iommu_group(kobj);
184 ssize_t ret = -EIO;
186 if (attr->show)
187 ret = attr->show(group, buf);
188 return ret;
191 static ssize_t iommu_group_attr_store(struct kobject *kobj,
192 struct attribute *__attr,
193 const char *buf, size_t count)
195 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
196 struct iommu_group *group = to_iommu_group(kobj);
197 ssize_t ret = -EIO;
199 if (attr->store)
200 ret = attr->store(group, buf, count);
201 return ret;
204 static const struct sysfs_ops iommu_group_sysfs_ops = {
205 .show = iommu_group_attr_show,
206 .store = iommu_group_attr_store,
209 static int iommu_group_create_file(struct iommu_group *group,
210 struct iommu_group_attribute *attr)
212 return sysfs_create_file(&group->kobj, &attr->attr);
215 static void iommu_group_remove_file(struct iommu_group *group,
216 struct iommu_group_attribute *attr)
218 sysfs_remove_file(&group->kobj, &attr->attr);
221 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
223 return sprintf(buf, "%s\n", group->name);
227 * iommu_insert_resv_region - Insert a new region in the
228 * list of reserved regions.
229 * @new: new region to insert
230 * @regions: list of regions
232 * The new element is sorted by address with respect to the other
233 * regions of the same type. In case it overlaps with another
234 * region of the same type, regions are merged. In case it
235 * overlaps with another region of different type, regions are
236 * not merged.
238 static int iommu_insert_resv_region(struct iommu_resv_region *new,
239 struct list_head *regions)
241 struct iommu_resv_region *region;
242 phys_addr_t start = new->start;
243 phys_addr_t end = new->start + new->length - 1;
244 struct list_head *pos = regions->next;
246 while (pos != regions) {
247 struct iommu_resv_region *entry =
248 list_entry(pos, struct iommu_resv_region, list);
249 phys_addr_t a = entry->start;
250 phys_addr_t b = entry->start + entry->length - 1;
251 int type = entry->type;
253 if (end < a) {
254 goto insert;
255 } else if (start > b) {
256 pos = pos->next;
257 } else if ((start >= a) && (end <= b)) {
258 if (new->type == type)
259 return 0;
260 else
261 pos = pos->next;
262 } else {
263 if (new->type == type) {
264 phys_addr_t new_start = min(a, start);
265 phys_addr_t new_end = max(b, end);
266 int ret;
268 list_del(&entry->list);
269 entry->start = new_start;
270 entry->length = new_end - new_start + 1;
271 ret = iommu_insert_resv_region(entry, regions);
272 kfree(entry);
273 return ret;
274 } else {
275 pos = pos->next;
279 insert:
280 region = iommu_alloc_resv_region(new->start, new->length,
281 new->prot, new->type);
282 if (!region)
283 return -ENOMEM;
285 list_add_tail(&region->list, pos);
286 return 0;
289 static int
290 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
291 struct list_head *group_resv_regions)
293 struct iommu_resv_region *entry;
294 int ret = 0;
296 list_for_each_entry(entry, dev_resv_regions, list) {
297 ret = iommu_insert_resv_region(entry, group_resv_regions);
298 if (ret)
299 break;
301 return ret;
304 int iommu_get_group_resv_regions(struct iommu_group *group,
305 struct list_head *head)
307 struct group_device *device;
308 int ret = 0;
310 mutex_lock(&group->mutex);
311 list_for_each_entry(device, &group->devices, list) {
312 struct list_head dev_resv_regions;
314 INIT_LIST_HEAD(&dev_resv_regions);
315 iommu_get_resv_regions(device->dev, &dev_resv_regions);
316 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
317 iommu_put_resv_regions(device->dev, &dev_resv_regions);
318 if (ret)
319 break;
321 mutex_unlock(&group->mutex);
322 return ret;
324 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
326 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
327 char *buf)
329 struct iommu_resv_region *region, *next;
330 struct list_head group_resv_regions;
331 char *str = buf;
333 INIT_LIST_HEAD(&group_resv_regions);
334 iommu_get_group_resv_regions(group, &group_resv_regions);
336 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
337 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
338 (long long int)region->start,
339 (long long int)(region->start +
340 region->length - 1),
341 iommu_group_resv_type_string[region->type]);
342 kfree(region);
345 return (str - buf);
348 static ssize_t iommu_group_show_type(struct iommu_group *group,
349 char *buf)
351 char *type = "unknown\n";
353 if (group->default_domain) {
354 switch (group->default_domain->type) {
355 case IOMMU_DOMAIN_BLOCKED:
356 type = "blocked\n";
357 break;
358 case IOMMU_DOMAIN_IDENTITY:
359 type = "identity\n";
360 break;
361 case IOMMU_DOMAIN_UNMANAGED:
362 type = "unmanaged\n";
363 break;
364 case IOMMU_DOMAIN_DMA:
365 type = "DMA\n";
366 break;
369 strcpy(buf, type);
371 return strlen(type);
374 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
376 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
377 iommu_group_show_resv_regions, NULL);
379 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
381 static void iommu_group_release(struct kobject *kobj)
383 struct iommu_group *group = to_iommu_group(kobj);
385 pr_debug("Releasing group %d\n", group->id);
387 if (group->iommu_data_release)
388 group->iommu_data_release(group->iommu_data);
390 ida_simple_remove(&iommu_group_ida, group->id);
392 if (group->default_domain)
393 iommu_domain_free(group->default_domain);
395 kfree(group->name);
396 kfree(group);
399 static struct kobj_type iommu_group_ktype = {
400 .sysfs_ops = &iommu_group_sysfs_ops,
401 .release = iommu_group_release,
405 * iommu_group_alloc - Allocate a new group
407 * This function is called by an iommu driver to allocate a new iommu
408 * group. The iommu group represents the minimum granularity of the iommu.
409 * Upon successful return, the caller holds a reference to the supplied
410 * group in order to hold the group until devices are added. Use
411 * iommu_group_put() to release this extra reference count, allowing the
412 * group to be automatically reclaimed once it has no devices or external
413 * references.
415 struct iommu_group *iommu_group_alloc(void)
417 struct iommu_group *group;
418 int ret;
420 group = kzalloc(sizeof(*group), GFP_KERNEL);
421 if (!group)
422 return ERR_PTR(-ENOMEM);
424 group->kobj.kset = iommu_group_kset;
425 mutex_init(&group->mutex);
426 INIT_LIST_HEAD(&group->devices);
427 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
429 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
430 if (ret < 0) {
431 kfree(group);
432 return ERR_PTR(ret);
434 group->id = ret;
436 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
437 NULL, "%d", group->id);
438 if (ret) {
439 ida_simple_remove(&iommu_group_ida, group->id);
440 kfree(group);
441 return ERR_PTR(ret);
444 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
445 if (!group->devices_kobj) {
446 kobject_put(&group->kobj); /* triggers .release & free */
447 return ERR_PTR(-ENOMEM);
451 * The devices_kobj holds a reference on the group kobject, so
452 * as long as that exists so will the group. We can therefore
453 * use the devices_kobj for reference counting.
455 kobject_put(&group->kobj);
457 ret = iommu_group_create_file(group,
458 &iommu_group_attr_reserved_regions);
459 if (ret)
460 return ERR_PTR(ret);
462 ret = iommu_group_create_file(group, &iommu_group_attr_type);
463 if (ret)
464 return ERR_PTR(ret);
466 pr_debug("Allocated group %d\n", group->id);
468 return group;
470 EXPORT_SYMBOL_GPL(iommu_group_alloc);
472 struct iommu_group *iommu_group_get_by_id(int id)
474 struct kobject *group_kobj;
475 struct iommu_group *group;
476 const char *name;
478 if (!iommu_group_kset)
479 return NULL;
481 name = kasprintf(GFP_KERNEL, "%d", id);
482 if (!name)
483 return NULL;
485 group_kobj = kset_find_obj(iommu_group_kset, name);
486 kfree(name);
488 if (!group_kobj)
489 return NULL;
491 group = container_of(group_kobj, struct iommu_group, kobj);
492 BUG_ON(group->id != id);
494 kobject_get(group->devices_kobj);
495 kobject_put(&group->kobj);
497 return group;
499 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
502 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
503 * @group: the group
505 * iommu drivers can store data in the group for use when doing iommu
506 * operations. This function provides a way to retrieve it. Caller
507 * should hold a group reference.
509 void *iommu_group_get_iommudata(struct iommu_group *group)
511 return group->iommu_data;
513 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
516 * iommu_group_set_iommudata - set iommu_data for a group
517 * @group: the group
518 * @iommu_data: new data
519 * @release: release function for iommu_data
521 * iommu drivers can store data in the group for use when doing iommu
522 * operations. This function provides a way to set the data after
523 * the group has been allocated. Caller should hold a group reference.
525 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
526 void (*release)(void *iommu_data))
528 group->iommu_data = iommu_data;
529 group->iommu_data_release = release;
531 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
534 * iommu_group_set_name - set name for a group
535 * @group: the group
536 * @name: name
538 * Allow iommu driver to set a name for a group. When set it will
539 * appear in a name attribute file under the group in sysfs.
541 int iommu_group_set_name(struct iommu_group *group, const char *name)
543 int ret;
545 if (group->name) {
546 iommu_group_remove_file(group, &iommu_group_attr_name);
547 kfree(group->name);
548 group->name = NULL;
549 if (!name)
550 return 0;
553 group->name = kstrdup(name, GFP_KERNEL);
554 if (!group->name)
555 return -ENOMEM;
557 ret = iommu_group_create_file(group, &iommu_group_attr_name);
558 if (ret) {
559 kfree(group->name);
560 group->name = NULL;
561 return ret;
564 return 0;
566 EXPORT_SYMBOL_GPL(iommu_group_set_name);
568 static int iommu_group_create_direct_mappings(struct iommu_group *group,
569 struct device *dev)
571 struct iommu_domain *domain = group->default_domain;
572 struct iommu_resv_region *entry;
573 struct list_head mappings;
574 unsigned long pg_size;
575 int ret = 0;
577 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
578 return 0;
580 BUG_ON(!domain->pgsize_bitmap);
582 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
583 INIT_LIST_HEAD(&mappings);
585 iommu_get_resv_regions(dev, &mappings);
587 /* We need to consider overlapping regions for different devices */
588 list_for_each_entry(entry, &mappings, list) {
589 dma_addr_t start, end, addr;
591 if (domain->ops->apply_resv_region)
592 domain->ops->apply_resv_region(dev, domain, entry);
594 start = ALIGN(entry->start, pg_size);
595 end = ALIGN(entry->start + entry->length, pg_size);
597 if (entry->type != IOMMU_RESV_DIRECT &&
598 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
599 continue;
601 for (addr = start; addr < end; addr += pg_size) {
602 phys_addr_t phys_addr;
604 phys_addr = iommu_iova_to_phys(domain, addr);
605 if (phys_addr)
606 continue;
608 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
609 if (ret)
610 goto out;
615 iommu_flush_tlb_all(domain);
617 out:
618 iommu_put_resv_regions(dev, &mappings);
620 return ret;
624 * iommu_group_add_device - add a device to an iommu group
625 * @group: the group into which to add the device (reference should be held)
626 * @dev: the device
628 * This function is called by an iommu driver to add a device into a
629 * group. Adding a device increments the group reference count.
631 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
633 int ret, i = 0;
634 struct group_device *device;
636 device = kzalloc(sizeof(*device), GFP_KERNEL);
637 if (!device)
638 return -ENOMEM;
640 device->dev = dev;
642 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
643 if (ret)
644 goto err_free_device;
646 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
647 rename:
648 if (!device->name) {
649 ret = -ENOMEM;
650 goto err_remove_link;
653 ret = sysfs_create_link_nowarn(group->devices_kobj,
654 &dev->kobj, device->name);
655 if (ret) {
656 if (ret == -EEXIST && i >= 0) {
658 * Account for the slim chance of collision
659 * and append an instance to the name.
661 kfree(device->name);
662 device->name = kasprintf(GFP_KERNEL, "%s.%d",
663 kobject_name(&dev->kobj), i++);
664 goto rename;
666 goto err_free_name;
669 kobject_get(group->devices_kobj);
671 dev->iommu_group = group;
673 iommu_group_create_direct_mappings(group, dev);
675 mutex_lock(&group->mutex);
676 list_add_tail(&device->list, &group->devices);
677 if (group->domain)
678 ret = __iommu_attach_device(group->domain, dev);
679 mutex_unlock(&group->mutex);
680 if (ret)
681 goto err_put_group;
683 /* Notify any listeners about change to group. */
684 blocking_notifier_call_chain(&group->notifier,
685 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
687 trace_add_device_to_group(group->id, dev);
689 dev_info(dev, "Adding to iommu group %d\n", group->id);
691 return 0;
693 err_put_group:
694 mutex_lock(&group->mutex);
695 list_del(&device->list);
696 mutex_unlock(&group->mutex);
697 dev->iommu_group = NULL;
698 kobject_put(group->devices_kobj);
699 err_free_name:
700 kfree(device->name);
701 err_remove_link:
702 sysfs_remove_link(&dev->kobj, "iommu_group");
703 err_free_device:
704 kfree(device);
705 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
706 return ret;
708 EXPORT_SYMBOL_GPL(iommu_group_add_device);
711 * iommu_group_remove_device - remove a device from it's current group
712 * @dev: device to be removed
714 * This function is called by an iommu driver to remove the device from
715 * it's current group. This decrements the iommu group reference count.
717 void iommu_group_remove_device(struct device *dev)
719 struct iommu_group *group = dev->iommu_group;
720 struct group_device *tmp_device, *device = NULL;
722 dev_info(dev, "Removing from iommu group %d\n", group->id);
724 /* Pre-notify listeners that a device is being removed. */
725 blocking_notifier_call_chain(&group->notifier,
726 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
728 mutex_lock(&group->mutex);
729 list_for_each_entry(tmp_device, &group->devices, list) {
730 if (tmp_device->dev == dev) {
731 device = tmp_device;
732 list_del(&device->list);
733 break;
736 mutex_unlock(&group->mutex);
738 if (!device)
739 return;
741 sysfs_remove_link(group->devices_kobj, device->name);
742 sysfs_remove_link(&dev->kobj, "iommu_group");
744 trace_remove_device_from_group(group->id, dev);
746 kfree(device->name);
747 kfree(device);
748 dev->iommu_group = NULL;
749 kobject_put(group->devices_kobj);
751 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
753 static int iommu_group_device_count(struct iommu_group *group)
755 struct group_device *entry;
756 int ret = 0;
758 list_for_each_entry(entry, &group->devices, list)
759 ret++;
761 return ret;
765 * iommu_group_for_each_dev - iterate over each device in the group
766 * @group: the group
767 * @data: caller opaque data to be passed to callback function
768 * @fn: caller supplied callback function
770 * This function is called by group users to iterate over group devices.
771 * Callers should hold a reference count to the group during callback.
772 * The group->mutex is held across callbacks, which will block calls to
773 * iommu_group_add/remove_device.
775 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
776 int (*fn)(struct device *, void *))
778 struct group_device *device;
779 int ret = 0;
781 list_for_each_entry(device, &group->devices, list) {
782 ret = fn(device->dev, data);
783 if (ret)
784 break;
786 return ret;
790 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
791 int (*fn)(struct device *, void *))
793 int ret;
795 mutex_lock(&group->mutex);
796 ret = __iommu_group_for_each_dev(group, data, fn);
797 mutex_unlock(&group->mutex);
799 return ret;
801 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
804 * iommu_group_get - Return the group for a device and increment reference
805 * @dev: get the group that this device belongs to
807 * This function is called by iommu drivers and users to get the group
808 * for the specified device. If found, the group is returned and the group
809 * reference in incremented, else NULL.
811 struct iommu_group *iommu_group_get(struct device *dev)
813 struct iommu_group *group = dev->iommu_group;
815 if (group)
816 kobject_get(group->devices_kobj);
818 return group;
820 EXPORT_SYMBOL_GPL(iommu_group_get);
823 * iommu_group_ref_get - Increment reference on a group
824 * @group: the group to use, must not be NULL
826 * This function is called by iommu drivers to take additional references on an
827 * existing group. Returns the given group for convenience.
829 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
831 kobject_get(group->devices_kobj);
832 return group;
836 * iommu_group_put - Decrement group reference
837 * @group: the group to use
839 * This function is called by iommu drivers and users to release the
840 * iommu group. Once the reference count is zero, the group is released.
842 void iommu_group_put(struct iommu_group *group)
844 if (group)
845 kobject_put(group->devices_kobj);
847 EXPORT_SYMBOL_GPL(iommu_group_put);
850 * iommu_group_register_notifier - Register a notifier for group changes
851 * @group: the group to watch
852 * @nb: notifier block to signal
854 * This function allows iommu group users to track changes in a group.
855 * See include/linux/iommu.h for actions sent via this notifier. Caller
856 * should hold a reference to the group throughout notifier registration.
858 int iommu_group_register_notifier(struct iommu_group *group,
859 struct notifier_block *nb)
861 return blocking_notifier_chain_register(&group->notifier, nb);
863 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
866 * iommu_group_unregister_notifier - Unregister a notifier
867 * @group: the group to watch
868 * @nb: notifier block to signal
870 * Unregister a previously registered group notifier block.
872 int iommu_group_unregister_notifier(struct iommu_group *group,
873 struct notifier_block *nb)
875 return blocking_notifier_chain_unregister(&group->notifier, nb);
877 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
880 * iommu_register_device_fault_handler() - Register a device fault handler
881 * @dev: the device
882 * @handler: the fault handler
883 * @data: private data passed as argument to the handler
885 * When an IOMMU fault event is received, this handler gets called with the
886 * fault event and data as argument. The handler should return 0 on success. If
887 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
888 * complete the fault by calling iommu_page_response() with one of the following
889 * response code:
890 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
891 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
892 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
893 * page faults if possible.
895 * Return 0 if the fault handler was installed successfully, or an error.
897 int iommu_register_device_fault_handler(struct device *dev,
898 iommu_dev_fault_handler_t handler,
899 void *data)
901 struct iommu_param *param = dev->iommu_param;
902 int ret = 0;
904 if (!param)
905 return -EINVAL;
907 mutex_lock(&param->lock);
908 /* Only allow one fault handler registered for each device */
909 if (param->fault_param) {
910 ret = -EBUSY;
911 goto done_unlock;
914 get_device(dev);
915 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
916 if (!param->fault_param) {
917 put_device(dev);
918 ret = -ENOMEM;
919 goto done_unlock;
921 param->fault_param->handler = handler;
922 param->fault_param->data = data;
923 mutex_init(&param->fault_param->lock);
924 INIT_LIST_HEAD(&param->fault_param->faults);
926 done_unlock:
927 mutex_unlock(&param->lock);
929 return ret;
931 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
934 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
935 * @dev: the device
937 * Remove the device fault handler installed with
938 * iommu_register_device_fault_handler().
940 * Return 0 on success, or an error.
942 int iommu_unregister_device_fault_handler(struct device *dev)
944 struct iommu_param *param = dev->iommu_param;
945 int ret = 0;
947 if (!param)
948 return -EINVAL;
950 mutex_lock(&param->lock);
952 if (!param->fault_param)
953 goto unlock;
955 /* we cannot unregister handler if there are pending faults */
956 if (!list_empty(&param->fault_param->faults)) {
957 ret = -EBUSY;
958 goto unlock;
961 kfree(param->fault_param);
962 param->fault_param = NULL;
963 put_device(dev);
964 unlock:
965 mutex_unlock(&param->lock);
967 return ret;
969 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
972 * iommu_report_device_fault() - Report fault event to device driver
973 * @dev: the device
974 * @evt: fault event data
976 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
977 * handler. When this function fails and the fault is recoverable, it is the
978 * caller's responsibility to complete the fault.
980 * Return 0 on success, or an error.
982 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
984 struct iommu_param *param = dev->iommu_param;
985 struct iommu_fault_event *evt_pending = NULL;
986 struct iommu_fault_param *fparam;
987 int ret = 0;
989 if (!param || !evt)
990 return -EINVAL;
992 /* we only report device fault if there is a handler registered */
993 mutex_lock(&param->lock);
994 fparam = param->fault_param;
995 if (!fparam || !fparam->handler) {
996 ret = -EINVAL;
997 goto done_unlock;
1000 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1001 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1002 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1003 GFP_KERNEL);
1004 if (!evt_pending) {
1005 ret = -ENOMEM;
1006 goto done_unlock;
1008 mutex_lock(&fparam->lock);
1009 list_add_tail(&evt_pending->list, &fparam->faults);
1010 mutex_unlock(&fparam->lock);
1013 ret = fparam->handler(&evt->fault, fparam->data);
1014 if (ret && evt_pending) {
1015 mutex_lock(&fparam->lock);
1016 list_del(&evt_pending->list);
1017 mutex_unlock(&fparam->lock);
1018 kfree(evt_pending);
1020 done_unlock:
1021 mutex_unlock(&param->lock);
1022 return ret;
1024 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1026 int iommu_page_response(struct device *dev,
1027 struct iommu_page_response *msg)
1029 bool pasid_valid;
1030 int ret = -EINVAL;
1031 struct iommu_fault_event *evt;
1032 struct iommu_fault_page_request *prm;
1033 struct iommu_param *param = dev->iommu_param;
1034 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1036 if (!domain || !domain->ops->page_response)
1037 return -ENODEV;
1039 if (!param || !param->fault_param)
1040 return -EINVAL;
1042 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1043 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1044 return -EINVAL;
1046 /* Only send response if there is a fault report pending */
1047 mutex_lock(&param->fault_param->lock);
1048 if (list_empty(&param->fault_param->faults)) {
1049 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1050 goto done_unlock;
1053 * Check if we have a matching page request pending to respond,
1054 * otherwise return -EINVAL
1056 list_for_each_entry(evt, &param->fault_param->faults, list) {
1057 prm = &evt->fault.prm;
1058 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1060 if ((pasid_valid && prm->pasid != msg->pasid) ||
1061 prm->grpid != msg->grpid)
1062 continue;
1064 /* Sanitize the reply */
1065 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1067 ret = domain->ops->page_response(dev, evt, msg);
1068 list_del(&evt->list);
1069 kfree(evt);
1070 break;
1073 done_unlock:
1074 mutex_unlock(&param->fault_param->lock);
1075 return ret;
1077 EXPORT_SYMBOL_GPL(iommu_page_response);
1080 * iommu_group_id - Return ID for a group
1081 * @group: the group to ID
1083 * Return the unique ID for the group matching the sysfs group number.
1085 int iommu_group_id(struct iommu_group *group)
1087 return group->id;
1089 EXPORT_SYMBOL_GPL(iommu_group_id);
1091 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1092 unsigned long *devfns);
1095 * To consider a PCI device isolated, we require ACS to support Source
1096 * Validation, Request Redirection, Completer Redirection, and Upstream
1097 * Forwarding. This effectively means that devices cannot spoof their
1098 * requester ID, requests and completions cannot be redirected, and all
1099 * transactions are forwarded upstream, even as it passes through a
1100 * bridge where the target device is downstream.
1102 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1105 * For multifunction devices which are not isolated from each other, find
1106 * all the other non-isolated functions and look for existing groups. For
1107 * each function, we also need to look for aliases to or from other devices
1108 * that may already have a group.
1110 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1111 unsigned long *devfns)
1113 struct pci_dev *tmp = NULL;
1114 struct iommu_group *group;
1116 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1117 return NULL;
1119 for_each_pci_dev(tmp) {
1120 if (tmp == pdev || tmp->bus != pdev->bus ||
1121 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1122 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1123 continue;
1125 group = get_pci_alias_group(tmp, devfns);
1126 if (group) {
1127 pci_dev_put(tmp);
1128 return group;
1132 return NULL;
1136 * Look for aliases to or from the given device for existing groups. DMA
1137 * aliases are only supported on the same bus, therefore the search
1138 * space is quite small (especially since we're really only looking at pcie
1139 * device, and therefore only expect multiple slots on the root complex or
1140 * downstream switch ports). It's conceivable though that a pair of
1141 * multifunction devices could have aliases between them that would cause a
1142 * loop. To prevent this, we use a bitmap to track where we've been.
1144 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1145 unsigned long *devfns)
1147 struct pci_dev *tmp = NULL;
1148 struct iommu_group *group;
1150 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1151 return NULL;
1153 group = iommu_group_get(&pdev->dev);
1154 if (group)
1155 return group;
1157 for_each_pci_dev(tmp) {
1158 if (tmp == pdev || tmp->bus != pdev->bus)
1159 continue;
1161 /* We alias them or they alias us */
1162 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1163 group = get_pci_alias_group(tmp, devfns);
1164 if (group) {
1165 pci_dev_put(tmp);
1166 return group;
1169 group = get_pci_function_alias_group(tmp, devfns);
1170 if (group) {
1171 pci_dev_put(tmp);
1172 return group;
1177 return NULL;
1180 struct group_for_pci_data {
1181 struct pci_dev *pdev;
1182 struct iommu_group *group;
1186 * DMA alias iterator callback, return the last seen device. Stop and return
1187 * the IOMMU group if we find one along the way.
1189 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1191 struct group_for_pci_data *data = opaque;
1193 data->pdev = pdev;
1194 data->group = iommu_group_get(&pdev->dev);
1196 return data->group != NULL;
1200 * Generic device_group call-back function. It just allocates one
1201 * iommu-group per device.
1203 struct iommu_group *generic_device_group(struct device *dev)
1205 return iommu_group_alloc();
1209 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1210 * to find or create an IOMMU group for a device.
1212 struct iommu_group *pci_device_group(struct device *dev)
1214 struct pci_dev *pdev = to_pci_dev(dev);
1215 struct group_for_pci_data data;
1216 struct pci_bus *bus;
1217 struct iommu_group *group = NULL;
1218 u64 devfns[4] = { 0 };
1220 if (WARN_ON(!dev_is_pci(dev)))
1221 return ERR_PTR(-EINVAL);
1224 * Find the upstream DMA alias for the device. A device must not
1225 * be aliased due to topology in order to have its own IOMMU group.
1226 * If we find an alias along the way that already belongs to a
1227 * group, use it.
1229 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1230 return data.group;
1232 pdev = data.pdev;
1235 * Continue upstream from the point of minimum IOMMU granularity
1236 * due to aliases to the point where devices are protected from
1237 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1238 * group, use it.
1240 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1241 if (!bus->self)
1242 continue;
1244 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1245 break;
1247 pdev = bus->self;
1249 group = iommu_group_get(&pdev->dev);
1250 if (group)
1251 return group;
1255 * Look for existing groups on device aliases. If we alias another
1256 * device or another device aliases us, use the same group.
1258 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1259 if (group)
1260 return group;
1263 * Look for existing groups on non-isolated functions on the same
1264 * slot and aliases of those funcions, if any. No need to clear
1265 * the search bitmap, the tested devfns are still valid.
1267 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1268 if (group)
1269 return group;
1271 /* No shared group found, allocate new */
1272 return iommu_group_alloc();
1275 /* Get the IOMMU group for device on fsl-mc bus */
1276 struct iommu_group *fsl_mc_device_group(struct device *dev)
1278 struct device *cont_dev = fsl_mc_cont_dev(dev);
1279 struct iommu_group *group;
1281 group = iommu_group_get(cont_dev);
1282 if (!group)
1283 group = iommu_group_alloc();
1284 return group;
1288 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1289 * @dev: target device
1291 * This function is intended to be called by IOMMU drivers and extended to
1292 * support common, bus-defined algorithms when determining or creating the
1293 * IOMMU group for a device. On success, the caller will hold a reference
1294 * to the returned IOMMU group, which will already include the provided
1295 * device. The reference should be released with iommu_group_put().
1297 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1299 const struct iommu_ops *ops = dev->bus->iommu_ops;
1300 struct iommu_group *group;
1301 int ret;
1303 group = iommu_group_get(dev);
1304 if (group)
1305 return group;
1307 if (!ops)
1308 return ERR_PTR(-EINVAL);
1310 group = ops->device_group(dev);
1311 if (WARN_ON_ONCE(group == NULL))
1312 return ERR_PTR(-EINVAL);
1314 if (IS_ERR(group))
1315 return group;
1318 * Try to allocate a default domain - needs support from the
1319 * IOMMU driver.
1321 if (!group->default_domain) {
1322 struct iommu_domain *dom;
1324 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1325 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1326 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1327 if (dom) {
1328 dev_warn(dev,
1329 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1330 iommu_def_domain_type);
1334 group->default_domain = dom;
1335 if (!group->domain)
1336 group->domain = dom;
1338 if (dom && !iommu_dma_strict) {
1339 int attr = 1;
1340 iommu_domain_set_attr(dom,
1341 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1342 &attr);
1346 ret = iommu_group_add_device(group, dev);
1347 if (ret) {
1348 iommu_group_put(group);
1349 return ERR_PTR(ret);
1352 return group;
1355 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1357 return group->default_domain;
1360 static int add_iommu_group(struct device *dev, void *data)
1362 int ret = iommu_probe_device(dev);
1365 * We ignore -ENODEV errors for now, as they just mean that the
1366 * device is not translated by an IOMMU. We still care about
1367 * other errors and fail to initialize when they happen.
1369 if (ret == -ENODEV)
1370 ret = 0;
1372 return ret;
1375 static int remove_iommu_group(struct device *dev, void *data)
1377 iommu_release_device(dev);
1379 return 0;
1382 static int iommu_bus_notifier(struct notifier_block *nb,
1383 unsigned long action, void *data)
1385 unsigned long group_action = 0;
1386 struct device *dev = data;
1387 struct iommu_group *group;
1390 * ADD/DEL call into iommu driver ops if provided, which may
1391 * result in ADD/DEL notifiers to group->notifier
1393 if (action == BUS_NOTIFY_ADD_DEVICE) {
1394 int ret;
1396 ret = iommu_probe_device(dev);
1397 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1398 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1399 iommu_release_device(dev);
1400 return NOTIFY_OK;
1404 * Remaining BUS_NOTIFYs get filtered and republished to the
1405 * group, if anyone is listening
1407 group = iommu_group_get(dev);
1408 if (!group)
1409 return 0;
1411 switch (action) {
1412 case BUS_NOTIFY_BIND_DRIVER:
1413 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1414 break;
1415 case BUS_NOTIFY_BOUND_DRIVER:
1416 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1417 break;
1418 case BUS_NOTIFY_UNBIND_DRIVER:
1419 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1420 break;
1421 case BUS_NOTIFY_UNBOUND_DRIVER:
1422 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1423 break;
1426 if (group_action)
1427 blocking_notifier_call_chain(&group->notifier,
1428 group_action, dev);
1430 iommu_group_put(group);
1431 return 0;
1434 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1436 int err;
1437 struct notifier_block *nb;
1439 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1440 if (!nb)
1441 return -ENOMEM;
1443 nb->notifier_call = iommu_bus_notifier;
1445 err = bus_register_notifier(bus, nb);
1446 if (err)
1447 goto out_free;
1449 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
1450 if (err)
1451 goto out_err;
1454 return 0;
1456 out_err:
1457 /* Clean up */
1458 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1459 bus_unregister_notifier(bus, nb);
1461 out_free:
1462 kfree(nb);
1464 return err;
1468 * bus_set_iommu - set iommu-callbacks for the bus
1469 * @bus: bus.
1470 * @ops: the callbacks provided by the iommu-driver
1472 * This function is called by an iommu driver to set the iommu methods
1473 * used for a particular bus. Drivers for devices on that bus can use
1474 * the iommu-api after these ops are registered.
1475 * This special function is needed because IOMMUs are usually devices on
1476 * the bus itself, so the iommu drivers are not initialized when the bus
1477 * is set up. With this function the iommu-driver can set the iommu-ops
1478 * afterwards.
1480 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1482 int err;
1484 if (bus->iommu_ops != NULL)
1485 return -EBUSY;
1487 bus->iommu_ops = ops;
1489 /* Do IOMMU specific setup for this bus-type */
1490 err = iommu_bus_init(bus, ops);
1491 if (err)
1492 bus->iommu_ops = NULL;
1494 return err;
1496 EXPORT_SYMBOL_GPL(bus_set_iommu);
1498 bool iommu_present(struct bus_type *bus)
1500 return bus->iommu_ops != NULL;
1502 EXPORT_SYMBOL_GPL(iommu_present);
1504 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1506 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1507 return false;
1509 return bus->iommu_ops->capable(cap);
1511 EXPORT_SYMBOL_GPL(iommu_capable);
1514 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1515 * @domain: iommu domain
1516 * @handler: fault handler
1517 * @token: user data, will be passed back to the fault handler
1519 * This function should be used by IOMMU users which want to be notified
1520 * whenever an IOMMU fault happens.
1522 * The fault handler itself should return 0 on success, and an appropriate
1523 * error code otherwise.
1525 void iommu_set_fault_handler(struct iommu_domain *domain,
1526 iommu_fault_handler_t handler,
1527 void *token)
1529 BUG_ON(!domain);
1531 domain->handler = handler;
1532 domain->handler_token = token;
1534 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1536 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1537 unsigned type)
1539 struct iommu_domain *domain;
1541 if (bus == NULL || bus->iommu_ops == NULL)
1542 return NULL;
1544 domain = bus->iommu_ops->domain_alloc(type);
1545 if (!domain)
1546 return NULL;
1548 domain->ops = bus->iommu_ops;
1549 domain->type = type;
1550 /* Assume all sizes by default; the driver may override this later */
1551 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1553 return domain;
1556 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1558 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1560 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1562 void iommu_domain_free(struct iommu_domain *domain)
1564 domain->ops->domain_free(domain);
1566 EXPORT_SYMBOL_GPL(iommu_domain_free);
1568 static int __iommu_attach_device(struct iommu_domain *domain,
1569 struct device *dev)
1571 int ret;
1572 if ((domain->ops->is_attach_deferred != NULL) &&
1573 domain->ops->is_attach_deferred(domain, dev))
1574 return 0;
1576 if (unlikely(domain->ops->attach_dev == NULL))
1577 return -ENODEV;
1579 ret = domain->ops->attach_dev(domain, dev);
1580 if (!ret)
1581 trace_attach_device_to_domain(dev);
1582 return ret;
1585 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1587 struct iommu_group *group;
1588 int ret;
1590 group = iommu_group_get(dev);
1591 if (!group)
1592 return -ENODEV;
1595 * Lock the group to make sure the device-count doesn't
1596 * change while we are attaching
1598 mutex_lock(&group->mutex);
1599 ret = -EINVAL;
1600 if (iommu_group_device_count(group) != 1)
1601 goto out_unlock;
1603 ret = __iommu_attach_group(domain, group);
1605 out_unlock:
1606 mutex_unlock(&group->mutex);
1607 iommu_group_put(group);
1609 return ret;
1611 EXPORT_SYMBOL_GPL(iommu_attach_device);
1613 static void __iommu_detach_device(struct iommu_domain *domain,
1614 struct device *dev)
1616 if ((domain->ops->is_attach_deferred != NULL) &&
1617 domain->ops->is_attach_deferred(domain, dev))
1618 return;
1620 if (unlikely(domain->ops->detach_dev == NULL))
1621 return;
1623 domain->ops->detach_dev(domain, dev);
1624 trace_detach_device_from_domain(dev);
1627 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1629 struct iommu_group *group;
1631 group = iommu_group_get(dev);
1632 if (!group)
1633 return;
1635 mutex_lock(&group->mutex);
1636 if (iommu_group_device_count(group) != 1) {
1637 WARN_ON(1);
1638 goto out_unlock;
1641 __iommu_detach_group(domain, group);
1643 out_unlock:
1644 mutex_unlock(&group->mutex);
1645 iommu_group_put(group);
1647 EXPORT_SYMBOL_GPL(iommu_detach_device);
1649 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1651 struct iommu_domain *domain;
1652 struct iommu_group *group;
1654 group = iommu_group_get(dev);
1655 if (!group)
1656 return NULL;
1658 domain = group->domain;
1660 iommu_group_put(group);
1662 return domain;
1664 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1667 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1668 * guarantees that the group and its default domain are valid and correct.
1670 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1672 return dev->iommu_group->default_domain;
1676 * IOMMU groups are really the natural working unit of the IOMMU, but
1677 * the IOMMU API works on domains and devices. Bridge that gap by
1678 * iterating over the devices in a group. Ideally we'd have a single
1679 * device which represents the requestor ID of the group, but we also
1680 * allow IOMMU drivers to create policy defined minimum sets, where
1681 * the physical hardware may be able to distiguish members, but we
1682 * wish to group them at a higher level (ex. untrusted multi-function
1683 * PCI devices). Thus we attach each device.
1685 static int iommu_group_do_attach_device(struct device *dev, void *data)
1687 struct iommu_domain *domain = data;
1689 return __iommu_attach_device(domain, dev);
1692 static int __iommu_attach_group(struct iommu_domain *domain,
1693 struct iommu_group *group)
1695 int ret;
1697 if (group->default_domain && group->domain != group->default_domain)
1698 return -EBUSY;
1700 ret = __iommu_group_for_each_dev(group, domain,
1701 iommu_group_do_attach_device);
1702 if (ret == 0)
1703 group->domain = domain;
1705 return ret;
1708 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1710 int ret;
1712 mutex_lock(&group->mutex);
1713 ret = __iommu_attach_group(domain, group);
1714 mutex_unlock(&group->mutex);
1716 return ret;
1718 EXPORT_SYMBOL_GPL(iommu_attach_group);
1720 static int iommu_group_do_detach_device(struct device *dev, void *data)
1722 struct iommu_domain *domain = data;
1724 __iommu_detach_device(domain, dev);
1726 return 0;
1729 static void __iommu_detach_group(struct iommu_domain *domain,
1730 struct iommu_group *group)
1732 int ret;
1734 if (!group->default_domain) {
1735 __iommu_group_for_each_dev(group, domain,
1736 iommu_group_do_detach_device);
1737 group->domain = NULL;
1738 return;
1741 if (group->domain == group->default_domain)
1742 return;
1744 /* Detach by re-attaching to the default domain */
1745 ret = __iommu_group_for_each_dev(group, group->default_domain,
1746 iommu_group_do_attach_device);
1747 if (ret != 0)
1748 WARN_ON(1);
1749 else
1750 group->domain = group->default_domain;
1753 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1755 mutex_lock(&group->mutex);
1756 __iommu_detach_group(domain, group);
1757 mutex_unlock(&group->mutex);
1759 EXPORT_SYMBOL_GPL(iommu_detach_group);
1761 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1763 if (unlikely(domain->ops->iova_to_phys == NULL))
1764 return 0;
1766 return domain->ops->iova_to_phys(domain, iova);
1768 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1770 static size_t iommu_pgsize(struct iommu_domain *domain,
1771 unsigned long addr_merge, size_t size)
1773 unsigned int pgsize_idx;
1774 size_t pgsize;
1776 /* Max page size that still fits into 'size' */
1777 pgsize_idx = __fls(size);
1779 /* need to consider alignment requirements ? */
1780 if (likely(addr_merge)) {
1781 /* Max page size allowed by address */
1782 unsigned int align_pgsize_idx = __ffs(addr_merge);
1783 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1786 /* build a mask of acceptable page sizes */
1787 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1789 /* throw away page sizes not supported by the hardware */
1790 pgsize &= domain->pgsize_bitmap;
1792 /* make sure we're still sane */
1793 BUG_ON(!pgsize);
1795 /* pick the biggest page */
1796 pgsize_idx = __fls(pgsize);
1797 pgsize = 1UL << pgsize_idx;
1799 return pgsize;
1802 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1803 phys_addr_t paddr, size_t size, int prot)
1805 const struct iommu_ops *ops = domain->ops;
1806 unsigned long orig_iova = iova;
1807 unsigned int min_pagesz;
1808 size_t orig_size = size;
1809 phys_addr_t orig_paddr = paddr;
1810 int ret = 0;
1812 if (unlikely(ops->map == NULL ||
1813 domain->pgsize_bitmap == 0UL))
1814 return -ENODEV;
1816 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1817 return -EINVAL;
1819 /* find out the minimum page size supported */
1820 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1823 * both the virtual address and the physical one, as well as
1824 * the size of the mapping, must be aligned (at least) to the
1825 * size of the smallest page supported by the hardware
1827 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1828 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1829 iova, &paddr, size, min_pagesz);
1830 return -EINVAL;
1833 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1835 while (size) {
1836 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1838 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1839 iova, &paddr, pgsize);
1841 ret = ops->map(domain, iova, paddr, pgsize, prot);
1842 if (ret)
1843 break;
1845 iova += pgsize;
1846 paddr += pgsize;
1847 size -= pgsize;
1850 if (ops->iotlb_sync_map)
1851 ops->iotlb_sync_map(domain);
1853 /* unroll mapping in case something went wrong */
1854 if (ret)
1855 iommu_unmap(domain, orig_iova, orig_size - size);
1856 else
1857 trace_map(orig_iova, orig_paddr, orig_size);
1859 return ret;
1861 EXPORT_SYMBOL_GPL(iommu_map);
1863 static size_t __iommu_unmap(struct iommu_domain *domain,
1864 unsigned long iova, size_t size,
1865 bool sync)
1867 const struct iommu_ops *ops = domain->ops;
1868 size_t unmapped_page, unmapped = 0;
1869 unsigned long orig_iova = iova;
1870 unsigned int min_pagesz;
1872 if (unlikely(ops->unmap == NULL ||
1873 domain->pgsize_bitmap == 0UL))
1874 return 0;
1876 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1877 return 0;
1879 /* find out the minimum page size supported */
1880 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1883 * The virtual address, as well as the size of the mapping, must be
1884 * aligned (at least) to the size of the smallest page supported
1885 * by the hardware
1887 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1888 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1889 iova, size, min_pagesz);
1890 return 0;
1893 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1896 * Keep iterating until we either unmap 'size' bytes (or more)
1897 * or we hit an area that isn't mapped.
1899 while (unmapped < size) {
1900 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1902 unmapped_page = ops->unmap(domain, iova, pgsize);
1903 if (!unmapped_page)
1904 break;
1906 if (sync && ops->iotlb_range_add)
1907 ops->iotlb_range_add(domain, iova, pgsize);
1909 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1910 iova, unmapped_page);
1912 iova += unmapped_page;
1913 unmapped += unmapped_page;
1916 if (sync && ops->iotlb_sync)
1917 ops->iotlb_sync(domain);
1919 trace_unmap(orig_iova, size, unmapped);
1920 return unmapped;
1923 size_t iommu_unmap(struct iommu_domain *domain,
1924 unsigned long iova, size_t size)
1926 return __iommu_unmap(domain, iova, size, true);
1928 EXPORT_SYMBOL_GPL(iommu_unmap);
1930 size_t iommu_unmap_fast(struct iommu_domain *domain,
1931 unsigned long iova, size_t size)
1933 return __iommu_unmap(domain, iova, size, false);
1935 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1937 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1938 struct scatterlist *sg, unsigned int nents, int prot)
1940 size_t len = 0, mapped = 0;
1941 phys_addr_t start;
1942 unsigned int i = 0;
1943 int ret;
1945 while (i <= nents) {
1946 phys_addr_t s_phys = sg_phys(sg);
1948 if (len && s_phys != start + len) {
1949 ret = iommu_map(domain, iova + mapped, start, len, prot);
1950 if (ret)
1951 goto out_err;
1953 mapped += len;
1954 len = 0;
1957 if (len) {
1958 len += sg->length;
1959 } else {
1960 len = sg->length;
1961 start = s_phys;
1964 if (++i < nents)
1965 sg = sg_next(sg);
1968 return mapped;
1970 out_err:
1971 /* undo mappings already done */
1972 iommu_unmap(domain, iova, mapped);
1974 return 0;
1977 EXPORT_SYMBOL_GPL(iommu_map_sg);
1979 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
1980 phys_addr_t paddr, u64 size, int prot)
1982 if (unlikely(domain->ops->domain_window_enable == NULL))
1983 return -ENODEV;
1985 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1986 prot);
1988 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1990 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1992 if (unlikely(domain->ops->domain_window_disable == NULL))
1993 return;
1995 return domain->ops->domain_window_disable(domain, wnd_nr);
1997 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2000 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2001 * @domain: the iommu domain where the fault has happened
2002 * @dev: the device where the fault has happened
2003 * @iova: the faulting address
2004 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2006 * This function should be called by the low-level IOMMU implementations
2007 * whenever IOMMU faults happen, to allow high-level users, that are
2008 * interested in such events, to know about them.
2010 * This event may be useful for several possible use cases:
2011 * - mere logging of the event
2012 * - dynamic TLB/PTE loading
2013 * - if restarting of the faulting device is required
2015 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2016 * PTE/TLB loading will one day be supported, implementations will be able
2017 * to tell whether it succeeded or not according to this return value).
2019 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2020 * (though fault handlers can also return -ENOSYS, in case they want to
2021 * elicit the default behavior of the IOMMU drivers).
2023 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2024 unsigned long iova, int flags)
2026 int ret = -ENOSYS;
2029 * if upper layers showed interest and installed a fault handler,
2030 * invoke it.
2032 if (domain->handler)
2033 ret = domain->handler(domain, dev, iova, flags,
2034 domain->handler_token);
2036 trace_io_page_fault(dev, iova, flags);
2037 return ret;
2039 EXPORT_SYMBOL_GPL(report_iommu_fault);
2041 static int __init iommu_init(void)
2043 iommu_group_kset = kset_create_and_add("iommu_groups",
2044 NULL, kernel_kobj);
2045 BUG_ON(!iommu_group_kset);
2047 iommu_debugfs_setup();
2049 return 0;
2051 core_initcall(iommu_init);
2053 int iommu_domain_get_attr(struct iommu_domain *domain,
2054 enum iommu_attr attr, void *data)
2056 struct iommu_domain_geometry *geometry;
2057 bool *paging;
2058 int ret = 0;
2060 switch (attr) {
2061 case DOMAIN_ATTR_GEOMETRY:
2062 geometry = data;
2063 *geometry = domain->geometry;
2065 break;
2066 case DOMAIN_ATTR_PAGING:
2067 paging = data;
2068 *paging = (domain->pgsize_bitmap != 0UL);
2069 break;
2070 default:
2071 if (!domain->ops->domain_get_attr)
2072 return -EINVAL;
2074 ret = domain->ops->domain_get_attr(domain, attr, data);
2077 return ret;
2079 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2081 int iommu_domain_set_attr(struct iommu_domain *domain,
2082 enum iommu_attr attr, void *data)
2084 int ret = 0;
2086 switch (attr) {
2087 default:
2088 if (domain->ops->domain_set_attr == NULL)
2089 return -EINVAL;
2091 ret = domain->ops->domain_set_attr(domain, attr, data);
2094 return ret;
2096 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
2098 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2100 const struct iommu_ops *ops = dev->bus->iommu_ops;
2102 if (ops && ops->get_resv_regions)
2103 ops->get_resv_regions(dev, list);
2106 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2108 const struct iommu_ops *ops = dev->bus->iommu_ops;
2110 if (ops && ops->put_resv_regions)
2111 ops->put_resv_regions(dev, list);
2114 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2115 size_t length, int prot,
2116 enum iommu_resv_type type)
2118 struct iommu_resv_region *region;
2120 region = kzalloc(sizeof(*region), GFP_KERNEL);
2121 if (!region)
2122 return NULL;
2124 INIT_LIST_HEAD(&region->list);
2125 region->start = start;
2126 region->length = length;
2127 region->prot = prot;
2128 region->type = type;
2129 return region;
2132 static int
2133 request_default_domain_for_dev(struct device *dev, unsigned long type)
2135 struct iommu_domain *domain;
2136 struct iommu_group *group;
2137 int ret;
2139 /* Device must already be in a group before calling this function */
2140 group = iommu_group_get(dev);
2141 if (!group)
2142 return -EINVAL;
2144 mutex_lock(&group->mutex);
2146 /* Check if the default domain is already direct mapped */
2147 ret = 0;
2148 if (group->default_domain && group->default_domain->type == type)
2149 goto out;
2151 /* Don't change mappings of existing devices */
2152 ret = -EBUSY;
2153 if (iommu_group_device_count(group) != 1)
2154 goto out;
2156 /* Allocate a direct mapped domain */
2157 ret = -ENOMEM;
2158 domain = __iommu_domain_alloc(dev->bus, type);
2159 if (!domain)
2160 goto out;
2162 /* Attach the device to the domain */
2163 ret = __iommu_attach_group(domain, group);
2164 if (ret) {
2165 iommu_domain_free(domain);
2166 goto out;
2169 iommu_group_create_direct_mappings(group, dev);
2171 /* Make the direct mapped domain the default for this group */
2172 if (group->default_domain)
2173 iommu_domain_free(group->default_domain);
2174 group->default_domain = domain;
2176 dev_info(dev, "Using iommu %s mapping\n",
2177 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
2179 ret = 0;
2180 out:
2181 mutex_unlock(&group->mutex);
2182 iommu_group_put(group);
2184 return ret;
2187 /* Request that a device is direct mapped by the IOMMU */
2188 int iommu_request_dm_for_dev(struct device *dev)
2190 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2193 /* Request that a device can't be direct mapped by the IOMMU */
2194 int iommu_request_dma_domain_for_dev(struct device *dev)
2196 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2199 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2201 const struct iommu_ops *ops = NULL;
2202 struct iommu_device *iommu;
2204 spin_lock(&iommu_device_lock);
2205 list_for_each_entry(iommu, &iommu_device_list, list)
2206 if (iommu->fwnode == fwnode) {
2207 ops = iommu->ops;
2208 break;
2210 spin_unlock(&iommu_device_lock);
2211 return ops;
2214 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2215 const struct iommu_ops *ops)
2217 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2219 if (fwspec)
2220 return ops == fwspec->ops ? 0 : -EINVAL;
2222 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2223 if (!fwspec)
2224 return -ENOMEM;
2226 of_node_get(to_of_node(iommu_fwnode));
2227 fwspec->iommu_fwnode = iommu_fwnode;
2228 fwspec->ops = ops;
2229 dev_iommu_fwspec_set(dev, fwspec);
2230 return 0;
2232 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2234 void iommu_fwspec_free(struct device *dev)
2236 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2238 if (fwspec) {
2239 fwnode_handle_put(fwspec->iommu_fwnode);
2240 kfree(fwspec);
2241 dev_iommu_fwspec_set(dev, NULL);
2244 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2246 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2248 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2249 size_t size;
2250 int i;
2252 if (!fwspec)
2253 return -EINVAL;
2255 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2256 if (size > sizeof(*fwspec)) {
2257 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2258 if (!fwspec)
2259 return -ENOMEM;
2261 dev_iommu_fwspec_set(dev, fwspec);
2264 for (i = 0; i < num_ids; i++)
2265 fwspec->ids[fwspec->num_ids + i] = ids[i];
2267 fwspec->num_ids += num_ids;
2268 return 0;
2270 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2273 * Per device IOMMU features.
2275 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2277 const struct iommu_ops *ops = dev->bus->iommu_ops;
2279 if (ops && ops->dev_has_feat)
2280 return ops->dev_has_feat(dev, feat);
2282 return false;
2284 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2286 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2288 const struct iommu_ops *ops = dev->bus->iommu_ops;
2290 if (ops && ops->dev_enable_feat)
2291 return ops->dev_enable_feat(dev, feat);
2293 return -ENODEV;
2295 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2298 * The device drivers should do the necessary cleanups before calling this.
2299 * For example, before disabling the aux-domain feature, the device driver
2300 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2302 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2304 const struct iommu_ops *ops = dev->bus->iommu_ops;
2306 if (ops && ops->dev_disable_feat)
2307 return ops->dev_disable_feat(dev, feat);
2309 return -EBUSY;
2311 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2313 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2315 const struct iommu_ops *ops = dev->bus->iommu_ops;
2317 if (ops && ops->dev_feat_enabled)
2318 return ops->dev_feat_enabled(dev, feat);
2320 return false;
2322 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2325 * Aux-domain specific attach/detach.
2327 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2328 * true. Also, as long as domains are attached to a device through this
2329 * interface, any tries to call iommu_attach_device() should fail
2330 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2331 * This should make us safe against a device being attached to a guest as a
2332 * whole while there are still pasid users on it (aux and sva).
2334 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2336 int ret = -ENODEV;
2338 if (domain->ops->aux_attach_dev)
2339 ret = domain->ops->aux_attach_dev(domain, dev);
2341 if (!ret)
2342 trace_attach_device_to_domain(dev);
2344 return ret;
2346 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2348 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2350 if (domain->ops->aux_detach_dev) {
2351 domain->ops->aux_detach_dev(domain, dev);
2352 trace_detach_device_from_domain(dev);
2355 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2357 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2359 int ret = -ENODEV;
2361 if (domain->ops->aux_get_pasid)
2362 ret = domain->ops->aux_get_pasid(domain, dev);
2364 return ret;
2366 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2369 * iommu_sva_bind_device() - Bind a process address space to a device
2370 * @dev: the device
2371 * @mm: the mm to bind, caller must hold a reference to it
2373 * Create a bond between device and address space, allowing the device to access
2374 * the mm using the returned PASID. If a bond already exists between @device and
2375 * @mm, it is returned and an additional reference is taken. Caller must call
2376 * iommu_sva_unbind_device() to release each reference.
2378 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2379 * initialize the required SVA features.
2381 * On error, returns an ERR_PTR value.
2383 struct iommu_sva *
2384 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2386 struct iommu_group *group;
2387 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2388 const struct iommu_ops *ops = dev->bus->iommu_ops;
2390 if (!ops || !ops->sva_bind)
2391 return ERR_PTR(-ENODEV);
2393 group = iommu_group_get(dev);
2394 if (!group)
2395 return ERR_PTR(-ENODEV);
2397 /* Ensure device count and domain don't change while we're binding */
2398 mutex_lock(&group->mutex);
2401 * To keep things simple, SVA currently doesn't support IOMMU groups
2402 * with more than one device. Existing SVA-capable systems are not
2403 * affected by the problems that required IOMMU groups (lack of ACS
2404 * isolation, device ID aliasing and other hardware issues).
2406 if (iommu_group_device_count(group) != 1)
2407 goto out_unlock;
2409 handle = ops->sva_bind(dev, mm, drvdata);
2411 out_unlock:
2412 mutex_unlock(&group->mutex);
2413 iommu_group_put(group);
2415 return handle;
2417 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2420 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2421 * @handle: the handle returned by iommu_sva_bind_device()
2423 * Put reference to a bond between device and address space. The device should
2424 * not be issuing any more transaction for this PASID. All outstanding page
2425 * requests for this PASID must have been flushed to the IOMMU.
2427 * Returns 0 on success, or an error value
2429 void iommu_sva_unbind_device(struct iommu_sva *handle)
2431 struct iommu_group *group;
2432 struct device *dev = handle->dev;
2433 const struct iommu_ops *ops = dev->bus->iommu_ops;
2435 if (!ops || !ops->sva_unbind)
2436 return;
2438 group = iommu_group_get(dev);
2439 if (!group)
2440 return;
2442 mutex_lock(&group->mutex);
2443 ops->sva_unbind(handle);
2444 mutex_unlock(&group->mutex);
2446 iommu_group_put(group);
2448 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2450 int iommu_sva_set_ops(struct iommu_sva *handle,
2451 const struct iommu_sva_ops *sva_ops)
2453 if (handle->ops && handle->ops != sva_ops)
2454 return -EEXIST;
2456 handle->ops = sva_ops;
2457 return 0;
2459 EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2461 int iommu_sva_get_pasid(struct iommu_sva *handle)
2463 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2465 if (!ops || !ops->sva_get_pasid)
2466 return IOMMU_PASID_INVALID;
2468 return ops->sva_get_pasid(handle);
2470 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);