2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <jroedel@suse.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define pr_fmt(fmt) "iommu: " fmt
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/bug.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/iommu.h>
29 #include <linux/idr.h>
30 #include <linux/notifier.h>
31 #include <linux/err.h>
32 #include <linux/pci.h>
33 #include <linux/bitops.h>
34 #include <linux/property.h>
35 #include <trace/events/iommu.h>
37 static struct kset
*iommu_group_kset
;
38 static DEFINE_IDA(iommu_group_ida
);
39 static unsigned int iommu_def_domain_type
= IOMMU_DOMAIN_DMA
;
41 struct iommu_callback_data
{
42 const struct iommu_ops
*ops
;
47 struct kobject
*devices_kobj
;
48 struct list_head devices
;
50 struct blocking_notifier_head notifier
;
52 void (*iommu_data_release
)(void *iommu_data
);
55 struct iommu_domain
*default_domain
;
56 struct iommu_domain
*domain
;
60 struct list_head list
;
65 struct iommu_group_attribute
{
66 struct attribute attr
;
67 ssize_t (*show
)(struct iommu_group
*group
, char *buf
);
68 ssize_t (*store
)(struct iommu_group
*group
,
69 const char *buf
, size_t count
);
72 static const char * const iommu_group_resv_type_string
[] = {
73 [IOMMU_RESV_DIRECT
] = "direct",
74 [IOMMU_RESV_RESERVED
] = "reserved",
75 [IOMMU_RESV_MSI
] = "msi",
76 [IOMMU_RESV_SW_MSI
] = "msi",
79 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
80 struct iommu_group_attribute iommu_group_attr_##_name = \
81 __ATTR(_name, _mode, _show, _store)
83 #define to_iommu_group_attr(_attr) \
84 container_of(_attr, struct iommu_group_attribute, attr)
85 #define to_iommu_group(_kobj) \
86 container_of(_kobj, struct iommu_group, kobj)
88 static LIST_HEAD(iommu_device_list
);
89 static DEFINE_SPINLOCK(iommu_device_lock
);
91 int iommu_device_register(struct iommu_device
*iommu
)
93 spin_lock(&iommu_device_lock
);
94 list_add_tail(&iommu
->list
, &iommu_device_list
);
95 spin_unlock(&iommu_device_lock
);
100 void iommu_device_unregister(struct iommu_device
*iommu
)
102 spin_lock(&iommu_device_lock
);
103 list_del(&iommu
->list
);
104 spin_unlock(&iommu_device_lock
);
107 static struct iommu_domain
*__iommu_domain_alloc(struct bus_type
*bus
,
109 static int __iommu_attach_device(struct iommu_domain
*domain
,
111 static int __iommu_attach_group(struct iommu_domain
*domain
,
112 struct iommu_group
*group
);
113 static void __iommu_detach_group(struct iommu_domain
*domain
,
114 struct iommu_group
*group
);
116 static int __init
iommu_set_def_domain_type(char *str
)
120 if (!str
|| strtobool(str
, &pt
))
123 iommu_def_domain_type
= pt
? IOMMU_DOMAIN_IDENTITY
: IOMMU_DOMAIN_DMA
;
126 early_param("iommu.passthrough", iommu_set_def_domain_type
);
128 static ssize_t
iommu_group_attr_show(struct kobject
*kobj
,
129 struct attribute
*__attr
, char *buf
)
131 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
132 struct iommu_group
*group
= to_iommu_group(kobj
);
136 ret
= attr
->show(group
, buf
);
140 static ssize_t
iommu_group_attr_store(struct kobject
*kobj
,
141 struct attribute
*__attr
,
142 const char *buf
, size_t count
)
144 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
145 struct iommu_group
*group
= to_iommu_group(kobj
);
149 ret
= attr
->store(group
, buf
, count
);
153 static const struct sysfs_ops iommu_group_sysfs_ops
= {
154 .show
= iommu_group_attr_show
,
155 .store
= iommu_group_attr_store
,
158 static int iommu_group_create_file(struct iommu_group
*group
,
159 struct iommu_group_attribute
*attr
)
161 return sysfs_create_file(&group
->kobj
, &attr
->attr
);
164 static void iommu_group_remove_file(struct iommu_group
*group
,
165 struct iommu_group_attribute
*attr
)
167 sysfs_remove_file(&group
->kobj
, &attr
->attr
);
170 static ssize_t
iommu_group_show_name(struct iommu_group
*group
, char *buf
)
172 return sprintf(buf
, "%s\n", group
->name
);
176 * iommu_insert_resv_region - Insert a new region in the
177 * list of reserved regions.
178 * @new: new region to insert
179 * @regions: list of regions
181 * The new element is sorted by address with respect to the other
182 * regions of the same type. In case it overlaps with another
183 * region of the same type, regions are merged. In case it
184 * overlaps with another region of different type, regions are
187 static int iommu_insert_resv_region(struct iommu_resv_region
*new,
188 struct list_head
*regions
)
190 struct iommu_resv_region
*region
;
191 phys_addr_t start
= new->start
;
192 phys_addr_t end
= new->start
+ new->length
- 1;
193 struct list_head
*pos
= regions
->next
;
195 while (pos
!= regions
) {
196 struct iommu_resv_region
*entry
=
197 list_entry(pos
, struct iommu_resv_region
, list
);
198 phys_addr_t a
= entry
->start
;
199 phys_addr_t b
= entry
->start
+ entry
->length
- 1;
200 int type
= entry
->type
;
204 } else if (start
> b
) {
206 } else if ((start
>= a
) && (end
<= b
)) {
207 if (new->type
== type
)
212 if (new->type
== type
) {
213 phys_addr_t new_start
= min(a
, start
);
214 phys_addr_t new_end
= max(b
, end
);
216 list_del(&entry
->list
);
217 entry
->start
= new_start
;
218 entry
->length
= new_end
- new_start
+ 1;
219 iommu_insert_resv_region(entry
, regions
);
226 region
= iommu_alloc_resv_region(new->start
, new->length
,
227 new->prot
, new->type
);
231 list_add_tail(®ion
->list
, pos
);
237 iommu_insert_device_resv_regions(struct list_head
*dev_resv_regions
,
238 struct list_head
*group_resv_regions
)
240 struct iommu_resv_region
*entry
;
243 list_for_each_entry(entry
, dev_resv_regions
, list
) {
244 ret
= iommu_insert_resv_region(entry
, group_resv_regions
);
251 int iommu_get_group_resv_regions(struct iommu_group
*group
,
252 struct list_head
*head
)
254 struct group_device
*device
;
257 mutex_lock(&group
->mutex
);
258 list_for_each_entry(device
, &group
->devices
, list
) {
259 struct list_head dev_resv_regions
;
261 INIT_LIST_HEAD(&dev_resv_regions
);
262 iommu_get_resv_regions(device
->dev
, &dev_resv_regions
);
263 ret
= iommu_insert_device_resv_regions(&dev_resv_regions
, head
);
264 iommu_put_resv_regions(device
->dev
, &dev_resv_regions
);
268 mutex_unlock(&group
->mutex
);
271 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions
);
273 static ssize_t
iommu_group_show_resv_regions(struct iommu_group
*group
,
276 struct iommu_resv_region
*region
, *next
;
277 struct list_head group_resv_regions
;
280 INIT_LIST_HEAD(&group_resv_regions
);
281 iommu_get_group_resv_regions(group
, &group_resv_regions
);
283 list_for_each_entry_safe(region
, next
, &group_resv_regions
, list
) {
284 str
+= sprintf(str
, "0x%016llx 0x%016llx %s\n",
285 (long long int)region
->start
,
286 (long long int)(region
->start
+
288 iommu_group_resv_type_string
[region
->type
]);
295 static IOMMU_GROUP_ATTR(name
, S_IRUGO
, iommu_group_show_name
, NULL
);
297 static IOMMU_GROUP_ATTR(reserved_regions
, 0444,
298 iommu_group_show_resv_regions
, NULL
);
300 static void iommu_group_release(struct kobject
*kobj
)
302 struct iommu_group
*group
= to_iommu_group(kobj
);
304 pr_debug("Releasing group %d\n", group
->id
);
306 if (group
->iommu_data_release
)
307 group
->iommu_data_release(group
->iommu_data
);
309 ida_simple_remove(&iommu_group_ida
, group
->id
);
311 if (group
->default_domain
)
312 iommu_domain_free(group
->default_domain
);
318 static struct kobj_type iommu_group_ktype
= {
319 .sysfs_ops
= &iommu_group_sysfs_ops
,
320 .release
= iommu_group_release
,
324 * iommu_group_alloc - Allocate a new group
325 * @name: Optional name to associate with group, visible in sysfs
327 * This function is called by an iommu driver to allocate a new iommu
328 * group. The iommu group represents the minimum granularity of the iommu.
329 * Upon successful return, the caller holds a reference to the supplied
330 * group in order to hold the group until devices are added. Use
331 * iommu_group_put() to release this extra reference count, allowing the
332 * group to be automatically reclaimed once it has no devices or external
335 struct iommu_group
*iommu_group_alloc(void)
337 struct iommu_group
*group
;
340 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
342 return ERR_PTR(-ENOMEM
);
344 group
->kobj
.kset
= iommu_group_kset
;
345 mutex_init(&group
->mutex
);
346 INIT_LIST_HEAD(&group
->devices
);
347 BLOCKING_INIT_NOTIFIER_HEAD(&group
->notifier
);
349 ret
= ida_simple_get(&iommu_group_ida
, 0, 0, GFP_KERNEL
);
356 ret
= kobject_init_and_add(&group
->kobj
, &iommu_group_ktype
,
357 NULL
, "%d", group
->id
);
359 ida_simple_remove(&iommu_group_ida
, group
->id
);
364 group
->devices_kobj
= kobject_create_and_add("devices", &group
->kobj
);
365 if (!group
->devices_kobj
) {
366 kobject_put(&group
->kobj
); /* triggers .release & free */
367 return ERR_PTR(-ENOMEM
);
371 * The devices_kobj holds a reference on the group kobject, so
372 * as long as that exists so will the group. We can therefore
373 * use the devices_kobj for reference counting.
375 kobject_put(&group
->kobj
);
377 ret
= iommu_group_create_file(group
,
378 &iommu_group_attr_reserved_regions
);
382 pr_debug("Allocated group %d\n", group
->id
);
386 EXPORT_SYMBOL_GPL(iommu_group_alloc
);
388 struct iommu_group
*iommu_group_get_by_id(int id
)
390 struct kobject
*group_kobj
;
391 struct iommu_group
*group
;
394 if (!iommu_group_kset
)
397 name
= kasprintf(GFP_KERNEL
, "%d", id
);
401 group_kobj
= kset_find_obj(iommu_group_kset
, name
);
407 group
= container_of(group_kobj
, struct iommu_group
, kobj
);
408 BUG_ON(group
->id
!= id
);
410 kobject_get(group
->devices_kobj
);
411 kobject_put(&group
->kobj
);
415 EXPORT_SYMBOL_GPL(iommu_group_get_by_id
);
418 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
421 * iommu drivers can store data in the group for use when doing iommu
422 * operations. This function provides a way to retrieve it. Caller
423 * should hold a group reference.
425 void *iommu_group_get_iommudata(struct iommu_group
*group
)
427 return group
->iommu_data
;
429 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata
);
432 * iommu_group_set_iommudata - set iommu_data for a group
434 * @iommu_data: new data
435 * @release: release function for iommu_data
437 * iommu drivers can store data in the group for use when doing iommu
438 * operations. This function provides a way to set the data after
439 * the group has been allocated. Caller should hold a group reference.
441 void iommu_group_set_iommudata(struct iommu_group
*group
, void *iommu_data
,
442 void (*release
)(void *iommu_data
))
444 group
->iommu_data
= iommu_data
;
445 group
->iommu_data_release
= release
;
447 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata
);
450 * iommu_group_set_name - set name for a group
454 * Allow iommu driver to set a name for a group. When set it will
455 * appear in a name attribute file under the group in sysfs.
457 int iommu_group_set_name(struct iommu_group
*group
, const char *name
)
462 iommu_group_remove_file(group
, &iommu_group_attr_name
);
469 group
->name
= kstrdup(name
, GFP_KERNEL
);
473 ret
= iommu_group_create_file(group
, &iommu_group_attr_name
);
482 EXPORT_SYMBOL_GPL(iommu_group_set_name
);
484 static int iommu_group_create_direct_mappings(struct iommu_group
*group
,
487 struct iommu_domain
*domain
= group
->default_domain
;
488 struct iommu_resv_region
*entry
;
489 struct list_head mappings
;
490 unsigned long pg_size
;
493 if (!domain
|| domain
->type
!= IOMMU_DOMAIN_DMA
)
496 BUG_ON(!domain
->pgsize_bitmap
);
498 pg_size
= 1UL << __ffs(domain
->pgsize_bitmap
);
499 INIT_LIST_HEAD(&mappings
);
501 iommu_get_resv_regions(dev
, &mappings
);
503 /* We need to consider overlapping regions for different devices */
504 list_for_each_entry(entry
, &mappings
, list
) {
505 dma_addr_t start
, end
, addr
;
507 if (domain
->ops
->apply_resv_region
)
508 domain
->ops
->apply_resv_region(dev
, domain
, entry
);
510 start
= ALIGN(entry
->start
, pg_size
);
511 end
= ALIGN(entry
->start
+ entry
->length
, pg_size
);
513 if (entry
->type
!= IOMMU_RESV_DIRECT
)
516 for (addr
= start
; addr
< end
; addr
+= pg_size
) {
517 phys_addr_t phys_addr
;
519 phys_addr
= iommu_iova_to_phys(domain
, addr
);
523 ret
= iommu_map(domain
, addr
, addr
, pg_size
, entry
->prot
);
531 iommu_put_resv_regions(dev
, &mappings
);
537 * iommu_group_add_device - add a device to an iommu group
538 * @group: the group into which to add the device (reference should be held)
541 * This function is called by an iommu driver to add a device into a
542 * group. Adding a device increments the group reference count.
544 int iommu_group_add_device(struct iommu_group
*group
, struct device
*dev
)
547 struct group_device
*device
;
549 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
555 ret
= sysfs_create_link(&dev
->kobj
, &group
->kobj
, "iommu_group");
557 goto err_free_device
;
559 device
->name
= kasprintf(GFP_KERNEL
, "%s", kobject_name(&dev
->kobj
));
563 goto err_remove_link
;
566 ret
= sysfs_create_link_nowarn(group
->devices_kobj
,
567 &dev
->kobj
, device
->name
);
569 if (ret
== -EEXIST
&& i
>= 0) {
571 * Account for the slim chance of collision
572 * and append an instance to the name.
575 device
->name
= kasprintf(GFP_KERNEL
, "%s.%d",
576 kobject_name(&dev
->kobj
), i
++);
582 kobject_get(group
->devices_kobj
);
584 dev
->iommu_group
= group
;
586 iommu_group_create_direct_mappings(group
, dev
);
588 mutex_lock(&group
->mutex
);
589 list_add_tail(&device
->list
, &group
->devices
);
591 ret
= __iommu_attach_device(group
->domain
, dev
);
592 mutex_unlock(&group
->mutex
);
596 /* Notify any listeners about change to group. */
597 blocking_notifier_call_chain(&group
->notifier
,
598 IOMMU_GROUP_NOTIFY_ADD_DEVICE
, dev
);
600 trace_add_device_to_group(group
->id
, dev
);
602 pr_info("Adding device %s to group %d\n", dev_name(dev
), group
->id
);
607 mutex_lock(&group
->mutex
);
608 list_del(&device
->list
);
609 mutex_unlock(&group
->mutex
);
610 dev
->iommu_group
= NULL
;
611 kobject_put(group
->devices_kobj
);
615 sysfs_remove_link(&dev
->kobj
, "iommu_group");
618 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev
), group
->id
, ret
);
621 EXPORT_SYMBOL_GPL(iommu_group_add_device
);
624 * iommu_group_remove_device - remove a device from it's current group
625 * @dev: device to be removed
627 * This function is called by an iommu driver to remove the device from
628 * it's current group. This decrements the iommu group reference count.
630 void iommu_group_remove_device(struct device
*dev
)
632 struct iommu_group
*group
= dev
->iommu_group
;
633 struct group_device
*tmp_device
, *device
= NULL
;
635 pr_info("Removing device %s from group %d\n", dev_name(dev
), group
->id
);
637 /* Pre-notify listeners that a device is being removed. */
638 blocking_notifier_call_chain(&group
->notifier
,
639 IOMMU_GROUP_NOTIFY_DEL_DEVICE
, dev
);
641 mutex_lock(&group
->mutex
);
642 list_for_each_entry(tmp_device
, &group
->devices
, list
) {
643 if (tmp_device
->dev
== dev
) {
645 list_del(&device
->list
);
649 mutex_unlock(&group
->mutex
);
654 sysfs_remove_link(group
->devices_kobj
, device
->name
);
655 sysfs_remove_link(&dev
->kobj
, "iommu_group");
657 trace_remove_device_from_group(group
->id
, dev
);
661 dev
->iommu_group
= NULL
;
662 kobject_put(group
->devices_kobj
);
664 EXPORT_SYMBOL_GPL(iommu_group_remove_device
);
666 static int iommu_group_device_count(struct iommu_group
*group
)
668 struct group_device
*entry
;
671 list_for_each_entry(entry
, &group
->devices
, list
)
678 * iommu_group_for_each_dev - iterate over each device in the group
680 * @data: caller opaque data to be passed to callback function
681 * @fn: caller supplied callback function
683 * This function is called by group users to iterate over group devices.
684 * Callers should hold a reference count to the group during callback.
685 * The group->mutex is held across callbacks, which will block calls to
686 * iommu_group_add/remove_device.
688 static int __iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
689 int (*fn
)(struct device
*, void *))
691 struct group_device
*device
;
694 list_for_each_entry(device
, &group
->devices
, list
) {
695 ret
= fn(device
->dev
, data
);
703 int iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
704 int (*fn
)(struct device
*, void *))
708 mutex_lock(&group
->mutex
);
709 ret
= __iommu_group_for_each_dev(group
, data
, fn
);
710 mutex_unlock(&group
->mutex
);
714 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev
);
717 * iommu_group_get - Return the group for a device and increment reference
718 * @dev: get the group that this device belongs to
720 * This function is called by iommu drivers and users to get the group
721 * for the specified device. If found, the group is returned and the group
722 * reference in incremented, else NULL.
724 struct iommu_group
*iommu_group_get(struct device
*dev
)
726 struct iommu_group
*group
= dev
->iommu_group
;
729 kobject_get(group
->devices_kobj
);
733 EXPORT_SYMBOL_GPL(iommu_group_get
);
736 * iommu_group_ref_get - Increment reference on a group
737 * @group: the group to use, must not be NULL
739 * This function is called by iommu drivers to take additional references on an
740 * existing group. Returns the given group for convenience.
742 struct iommu_group
*iommu_group_ref_get(struct iommu_group
*group
)
744 kobject_get(group
->devices_kobj
);
749 * iommu_group_put - Decrement group reference
750 * @group: the group to use
752 * This function is called by iommu drivers and users to release the
753 * iommu group. Once the reference count is zero, the group is released.
755 void iommu_group_put(struct iommu_group
*group
)
758 kobject_put(group
->devices_kobj
);
760 EXPORT_SYMBOL_GPL(iommu_group_put
);
763 * iommu_group_register_notifier - Register a notifier for group changes
764 * @group: the group to watch
765 * @nb: notifier block to signal
767 * This function allows iommu group users to track changes in a group.
768 * See include/linux/iommu.h for actions sent via this notifier. Caller
769 * should hold a reference to the group throughout notifier registration.
771 int iommu_group_register_notifier(struct iommu_group
*group
,
772 struct notifier_block
*nb
)
774 return blocking_notifier_chain_register(&group
->notifier
, nb
);
776 EXPORT_SYMBOL_GPL(iommu_group_register_notifier
);
779 * iommu_group_unregister_notifier - Unregister a notifier
780 * @group: the group to watch
781 * @nb: notifier block to signal
783 * Unregister a previously registered group notifier block.
785 int iommu_group_unregister_notifier(struct iommu_group
*group
,
786 struct notifier_block
*nb
)
788 return blocking_notifier_chain_unregister(&group
->notifier
, nb
);
790 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier
);
793 * iommu_group_id - Return ID for a group
794 * @group: the group to ID
796 * Return the unique ID for the group matching the sysfs group number.
798 int iommu_group_id(struct iommu_group
*group
)
802 EXPORT_SYMBOL_GPL(iommu_group_id
);
804 static struct iommu_group
*get_pci_alias_group(struct pci_dev
*pdev
,
805 unsigned long *devfns
);
808 * To consider a PCI device isolated, we require ACS to support Source
809 * Validation, Request Redirection, Completer Redirection, and Upstream
810 * Forwarding. This effectively means that devices cannot spoof their
811 * requester ID, requests and completions cannot be redirected, and all
812 * transactions are forwarded upstream, even as it passes through a
813 * bridge where the target device is downstream.
815 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
818 * For multifunction devices which are not isolated from each other, find
819 * all the other non-isolated functions and look for existing groups. For
820 * each function, we also need to look for aliases to or from other devices
821 * that may already have a group.
823 static struct iommu_group
*get_pci_function_alias_group(struct pci_dev
*pdev
,
824 unsigned long *devfns
)
826 struct pci_dev
*tmp
= NULL
;
827 struct iommu_group
*group
;
829 if (!pdev
->multifunction
|| pci_acs_enabled(pdev
, REQ_ACS_FLAGS
))
832 for_each_pci_dev(tmp
) {
833 if (tmp
== pdev
|| tmp
->bus
!= pdev
->bus
||
834 PCI_SLOT(tmp
->devfn
) != PCI_SLOT(pdev
->devfn
) ||
835 pci_acs_enabled(tmp
, REQ_ACS_FLAGS
))
838 group
= get_pci_alias_group(tmp
, devfns
);
849 * Look for aliases to or from the given device for existing groups. DMA
850 * aliases are only supported on the same bus, therefore the search
851 * space is quite small (especially since we're really only looking at pcie
852 * device, and therefore only expect multiple slots on the root complex or
853 * downstream switch ports). It's conceivable though that a pair of
854 * multifunction devices could have aliases between them that would cause a
855 * loop. To prevent this, we use a bitmap to track where we've been.
857 static struct iommu_group
*get_pci_alias_group(struct pci_dev
*pdev
,
858 unsigned long *devfns
)
860 struct pci_dev
*tmp
= NULL
;
861 struct iommu_group
*group
;
863 if (test_and_set_bit(pdev
->devfn
& 0xff, devfns
))
866 group
= iommu_group_get(&pdev
->dev
);
870 for_each_pci_dev(tmp
) {
871 if (tmp
== pdev
|| tmp
->bus
!= pdev
->bus
)
874 /* We alias them or they alias us */
875 if (pci_devs_are_dma_aliases(pdev
, tmp
)) {
876 group
= get_pci_alias_group(tmp
, devfns
);
882 group
= get_pci_function_alias_group(tmp
, devfns
);
893 struct group_for_pci_data
{
894 struct pci_dev
*pdev
;
895 struct iommu_group
*group
;
899 * DMA alias iterator callback, return the last seen device. Stop and return
900 * the IOMMU group if we find one along the way.
902 static int get_pci_alias_or_group(struct pci_dev
*pdev
, u16 alias
, void *opaque
)
904 struct group_for_pci_data
*data
= opaque
;
907 data
->group
= iommu_group_get(&pdev
->dev
);
909 return data
->group
!= NULL
;
913 * Generic device_group call-back function. It just allocates one
914 * iommu-group per device.
916 struct iommu_group
*generic_device_group(struct device
*dev
)
918 return iommu_group_alloc();
922 * Use standard PCI bus topology, isolation features, and DMA alias quirks
923 * to find or create an IOMMU group for a device.
925 struct iommu_group
*pci_device_group(struct device
*dev
)
927 struct pci_dev
*pdev
= to_pci_dev(dev
);
928 struct group_for_pci_data data
;
930 struct iommu_group
*group
= NULL
;
931 u64 devfns
[4] = { 0 };
933 if (WARN_ON(!dev_is_pci(dev
)))
934 return ERR_PTR(-EINVAL
);
937 * Find the upstream DMA alias for the device. A device must not
938 * be aliased due to topology in order to have its own IOMMU group.
939 * If we find an alias along the way that already belongs to a
942 if (pci_for_each_dma_alias(pdev
, get_pci_alias_or_group
, &data
))
948 * Continue upstream from the point of minimum IOMMU granularity
949 * due to aliases to the point where devices are protected from
950 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
953 for (bus
= pdev
->bus
; !pci_is_root_bus(bus
); bus
= bus
->parent
) {
957 if (pci_acs_path_enabled(bus
->self
, NULL
, REQ_ACS_FLAGS
))
962 group
= iommu_group_get(&pdev
->dev
);
968 * Look for existing groups on device aliases. If we alias another
969 * device or another device aliases us, use the same group.
971 group
= get_pci_alias_group(pdev
, (unsigned long *)devfns
);
976 * Look for existing groups on non-isolated functions on the same
977 * slot and aliases of those funcions, if any. No need to clear
978 * the search bitmap, the tested devfns are still valid.
980 group
= get_pci_function_alias_group(pdev
, (unsigned long *)devfns
);
984 /* No shared group found, allocate new */
985 return iommu_group_alloc();
989 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
990 * @dev: target device
992 * This function is intended to be called by IOMMU drivers and extended to
993 * support common, bus-defined algorithms when determining or creating the
994 * IOMMU group for a device. On success, the caller will hold a reference
995 * to the returned IOMMU group, which will already include the provided
996 * device. The reference should be released with iommu_group_put().
998 struct iommu_group
*iommu_group_get_for_dev(struct device
*dev
)
1000 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
1001 struct iommu_group
*group
;
1004 group
= iommu_group_get(dev
);
1009 return ERR_PTR(-EINVAL
);
1011 group
= ops
->device_group(dev
);
1012 if (WARN_ON_ONCE(group
== NULL
))
1013 return ERR_PTR(-EINVAL
);
1019 * Try to allocate a default domain - needs support from the
1022 if (!group
->default_domain
) {
1023 struct iommu_domain
*dom
;
1025 dom
= __iommu_domain_alloc(dev
->bus
, iommu_def_domain_type
);
1026 if (!dom
&& iommu_def_domain_type
!= IOMMU_DOMAIN_DMA
) {
1028 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1029 iommu_def_domain_type
);
1030 dom
= __iommu_domain_alloc(dev
->bus
, IOMMU_DOMAIN_DMA
);
1033 group
->default_domain
= dom
;
1035 group
->domain
= dom
;
1038 ret
= iommu_group_add_device(group
, dev
);
1040 iommu_group_put(group
);
1041 return ERR_PTR(ret
);
1047 struct iommu_domain
*iommu_group_default_domain(struct iommu_group
*group
)
1049 return group
->default_domain
;
1052 static int add_iommu_group(struct device
*dev
, void *data
)
1054 struct iommu_callback_data
*cb
= data
;
1055 const struct iommu_ops
*ops
= cb
->ops
;
1058 if (!ops
->add_device
)
1061 WARN_ON(dev
->iommu_group
);
1063 ret
= ops
->add_device(dev
);
1066 * We ignore -ENODEV errors for now, as they just mean that the
1067 * device is not translated by an IOMMU. We still care about
1068 * other errors and fail to initialize when they happen.
1076 static int remove_iommu_group(struct device
*dev
, void *data
)
1078 struct iommu_callback_data
*cb
= data
;
1079 const struct iommu_ops
*ops
= cb
->ops
;
1081 if (ops
->remove_device
&& dev
->iommu_group
)
1082 ops
->remove_device(dev
);
1087 static int iommu_bus_notifier(struct notifier_block
*nb
,
1088 unsigned long action
, void *data
)
1090 struct device
*dev
= data
;
1091 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
1092 struct iommu_group
*group
;
1093 unsigned long group_action
= 0;
1096 * ADD/DEL call into iommu driver ops if provided, which may
1097 * result in ADD/DEL notifiers to group->notifier
1099 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
1100 if (ops
->add_device
) {
1103 ret
= ops
->add_device(dev
);
1104 return (ret
) ? NOTIFY_DONE
: NOTIFY_OK
;
1106 } else if (action
== BUS_NOTIFY_REMOVED_DEVICE
) {
1107 if (ops
->remove_device
&& dev
->iommu_group
) {
1108 ops
->remove_device(dev
);
1114 * Remaining BUS_NOTIFYs get filtered and republished to the
1115 * group, if anyone is listening
1117 group
= iommu_group_get(dev
);
1122 case BUS_NOTIFY_BIND_DRIVER
:
1123 group_action
= IOMMU_GROUP_NOTIFY_BIND_DRIVER
;
1125 case BUS_NOTIFY_BOUND_DRIVER
:
1126 group_action
= IOMMU_GROUP_NOTIFY_BOUND_DRIVER
;
1128 case BUS_NOTIFY_UNBIND_DRIVER
:
1129 group_action
= IOMMU_GROUP_NOTIFY_UNBIND_DRIVER
;
1131 case BUS_NOTIFY_UNBOUND_DRIVER
:
1132 group_action
= IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER
;
1137 blocking_notifier_call_chain(&group
->notifier
,
1140 iommu_group_put(group
);
1144 static int iommu_bus_init(struct bus_type
*bus
, const struct iommu_ops
*ops
)
1147 struct notifier_block
*nb
;
1148 struct iommu_callback_data cb
= {
1152 nb
= kzalloc(sizeof(struct notifier_block
), GFP_KERNEL
);
1156 nb
->notifier_call
= iommu_bus_notifier
;
1158 err
= bus_register_notifier(bus
, nb
);
1162 err
= bus_for_each_dev(bus
, NULL
, &cb
, add_iommu_group
);
1171 bus_for_each_dev(bus
, NULL
, &cb
, remove_iommu_group
);
1172 bus_unregister_notifier(bus
, nb
);
1181 * bus_set_iommu - set iommu-callbacks for the bus
1183 * @ops: the callbacks provided by the iommu-driver
1185 * This function is called by an iommu driver to set the iommu methods
1186 * used for a particular bus. Drivers for devices on that bus can use
1187 * the iommu-api after these ops are registered.
1188 * This special function is needed because IOMMUs are usually devices on
1189 * the bus itself, so the iommu drivers are not initialized when the bus
1190 * is set up. With this function the iommu-driver can set the iommu-ops
1193 int bus_set_iommu(struct bus_type
*bus
, const struct iommu_ops
*ops
)
1197 if (bus
->iommu_ops
!= NULL
)
1200 bus
->iommu_ops
= ops
;
1202 /* Do IOMMU specific setup for this bus-type */
1203 err
= iommu_bus_init(bus
, ops
);
1205 bus
->iommu_ops
= NULL
;
1209 EXPORT_SYMBOL_GPL(bus_set_iommu
);
1211 bool iommu_present(struct bus_type
*bus
)
1213 return bus
->iommu_ops
!= NULL
;
1215 EXPORT_SYMBOL_GPL(iommu_present
);
1217 bool iommu_capable(struct bus_type
*bus
, enum iommu_cap cap
)
1219 if (!bus
->iommu_ops
|| !bus
->iommu_ops
->capable
)
1222 return bus
->iommu_ops
->capable(cap
);
1224 EXPORT_SYMBOL_GPL(iommu_capable
);
1227 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1228 * @domain: iommu domain
1229 * @handler: fault handler
1230 * @token: user data, will be passed back to the fault handler
1232 * This function should be used by IOMMU users which want to be notified
1233 * whenever an IOMMU fault happens.
1235 * The fault handler itself should return 0 on success, and an appropriate
1236 * error code otherwise.
1238 void iommu_set_fault_handler(struct iommu_domain
*domain
,
1239 iommu_fault_handler_t handler
,
1244 domain
->handler
= handler
;
1245 domain
->handler_token
= token
;
1247 EXPORT_SYMBOL_GPL(iommu_set_fault_handler
);
1249 static struct iommu_domain
*__iommu_domain_alloc(struct bus_type
*bus
,
1252 struct iommu_domain
*domain
;
1254 if (bus
== NULL
|| bus
->iommu_ops
== NULL
)
1257 domain
= bus
->iommu_ops
->domain_alloc(type
);
1261 domain
->ops
= bus
->iommu_ops
;
1262 domain
->type
= type
;
1263 /* Assume all sizes by default; the driver may override this later */
1264 domain
->pgsize_bitmap
= bus
->iommu_ops
->pgsize_bitmap
;
1269 struct iommu_domain
*iommu_domain_alloc(struct bus_type
*bus
)
1271 return __iommu_domain_alloc(bus
, IOMMU_DOMAIN_UNMANAGED
);
1273 EXPORT_SYMBOL_GPL(iommu_domain_alloc
);
1275 void iommu_domain_free(struct iommu_domain
*domain
)
1277 domain
->ops
->domain_free(domain
);
1279 EXPORT_SYMBOL_GPL(iommu_domain_free
);
1281 static int __iommu_attach_device(struct iommu_domain
*domain
,
1285 if (unlikely(domain
->ops
->attach_dev
== NULL
))
1288 ret
= domain
->ops
->attach_dev(domain
, dev
);
1290 trace_attach_device_to_domain(dev
);
1294 int iommu_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
1296 struct iommu_group
*group
;
1299 group
= iommu_group_get(dev
);
1301 * Lock the group to make sure the device-count doesn't
1302 * change while we are attaching
1304 mutex_lock(&group
->mutex
);
1306 if (iommu_group_device_count(group
) != 1)
1309 ret
= __iommu_attach_group(domain
, group
);
1312 mutex_unlock(&group
->mutex
);
1313 iommu_group_put(group
);
1317 EXPORT_SYMBOL_GPL(iommu_attach_device
);
1319 static void __iommu_detach_device(struct iommu_domain
*domain
,
1322 if (unlikely(domain
->ops
->detach_dev
== NULL
))
1325 domain
->ops
->detach_dev(domain
, dev
);
1326 trace_detach_device_from_domain(dev
);
1329 void iommu_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
1331 struct iommu_group
*group
;
1333 group
= iommu_group_get(dev
);
1335 mutex_lock(&group
->mutex
);
1336 if (iommu_group_device_count(group
) != 1) {
1341 __iommu_detach_group(domain
, group
);
1344 mutex_unlock(&group
->mutex
);
1345 iommu_group_put(group
);
1347 EXPORT_SYMBOL_GPL(iommu_detach_device
);
1349 struct iommu_domain
*iommu_get_domain_for_dev(struct device
*dev
)
1351 struct iommu_domain
*domain
;
1352 struct iommu_group
*group
;
1354 group
= iommu_group_get(dev
);
1358 domain
= group
->domain
;
1360 iommu_group_put(group
);
1364 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev
);
1367 * IOMMU groups are really the natrual working unit of the IOMMU, but
1368 * the IOMMU API works on domains and devices. Bridge that gap by
1369 * iterating over the devices in a group. Ideally we'd have a single
1370 * device which represents the requestor ID of the group, but we also
1371 * allow IOMMU drivers to create policy defined minimum sets, where
1372 * the physical hardware may be able to distiguish members, but we
1373 * wish to group them at a higher level (ex. untrusted multi-function
1374 * PCI devices). Thus we attach each device.
1376 static int iommu_group_do_attach_device(struct device
*dev
, void *data
)
1378 struct iommu_domain
*domain
= data
;
1380 return __iommu_attach_device(domain
, dev
);
1383 static int __iommu_attach_group(struct iommu_domain
*domain
,
1384 struct iommu_group
*group
)
1388 if (group
->default_domain
&& group
->domain
!= group
->default_domain
)
1391 ret
= __iommu_group_for_each_dev(group
, domain
,
1392 iommu_group_do_attach_device
);
1394 group
->domain
= domain
;
1399 int iommu_attach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
1403 mutex_lock(&group
->mutex
);
1404 ret
= __iommu_attach_group(domain
, group
);
1405 mutex_unlock(&group
->mutex
);
1409 EXPORT_SYMBOL_GPL(iommu_attach_group
);
1411 static int iommu_group_do_detach_device(struct device
*dev
, void *data
)
1413 struct iommu_domain
*domain
= data
;
1415 __iommu_detach_device(domain
, dev
);
1420 static void __iommu_detach_group(struct iommu_domain
*domain
,
1421 struct iommu_group
*group
)
1425 if (!group
->default_domain
) {
1426 __iommu_group_for_each_dev(group
, domain
,
1427 iommu_group_do_detach_device
);
1428 group
->domain
= NULL
;
1432 if (group
->domain
== group
->default_domain
)
1435 /* Detach by re-attaching to the default domain */
1436 ret
= __iommu_group_for_each_dev(group
, group
->default_domain
,
1437 iommu_group_do_attach_device
);
1441 group
->domain
= group
->default_domain
;
1444 void iommu_detach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
1446 mutex_lock(&group
->mutex
);
1447 __iommu_detach_group(domain
, group
);
1448 mutex_unlock(&group
->mutex
);
1450 EXPORT_SYMBOL_GPL(iommu_detach_group
);
1452 phys_addr_t
iommu_iova_to_phys(struct iommu_domain
*domain
, dma_addr_t iova
)
1454 if (unlikely(domain
->ops
->iova_to_phys
== NULL
))
1457 return domain
->ops
->iova_to_phys(domain
, iova
);
1459 EXPORT_SYMBOL_GPL(iommu_iova_to_phys
);
1461 static size_t iommu_pgsize(struct iommu_domain
*domain
,
1462 unsigned long addr_merge
, size_t size
)
1464 unsigned int pgsize_idx
;
1467 /* Max page size that still fits into 'size' */
1468 pgsize_idx
= __fls(size
);
1470 /* need to consider alignment requirements ? */
1471 if (likely(addr_merge
)) {
1472 /* Max page size allowed by address */
1473 unsigned int align_pgsize_idx
= __ffs(addr_merge
);
1474 pgsize_idx
= min(pgsize_idx
, align_pgsize_idx
);
1477 /* build a mask of acceptable page sizes */
1478 pgsize
= (1UL << (pgsize_idx
+ 1)) - 1;
1480 /* throw away page sizes not supported by the hardware */
1481 pgsize
&= domain
->pgsize_bitmap
;
1483 /* make sure we're still sane */
1486 /* pick the biggest page */
1487 pgsize_idx
= __fls(pgsize
);
1488 pgsize
= 1UL << pgsize_idx
;
1493 int iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
1494 phys_addr_t paddr
, size_t size
, int prot
)
1496 unsigned long orig_iova
= iova
;
1497 unsigned int min_pagesz
;
1498 size_t orig_size
= size
;
1499 phys_addr_t orig_paddr
= paddr
;
1502 if (unlikely(domain
->ops
->map
== NULL
||
1503 domain
->pgsize_bitmap
== 0UL))
1506 if (unlikely(!(domain
->type
& __IOMMU_DOMAIN_PAGING
)))
1509 /* find out the minimum page size supported */
1510 min_pagesz
= 1 << __ffs(domain
->pgsize_bitmap
);
1513 * both the virtual address and the physical one, as well as
1514 * the size of the mapping, must be aligned (at least) to the
1515 * size of the smallest page supported by the hardware
1517 if (!IS_ALIGNED(iova
| paddr
| size
, min_pagesz
)) {
1518 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1519 iova
, &paddr
, size
, min_pagesz
);
1523 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova
, &paddr
, size
);
1526 size_t pgsize
= iommu_pgsize(domain
, iova
| paddr
, size
);
1528 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1529 iova
, &paddr
, pgsize
);
1531 ret
= domain
->ops
->map(domain
, iova
, paddr
, pgsize
, prot
);
1540 /* unroll mapping in case something went wrong */
1542 iommu_unmap(domain
, orig_iova
, orig_size
- size
);
1544 trace_map(orig_iova
, orig_paddr
, orig_size
);
1548 EXPORT_SYMBOL_GPL(iommu_map
);
1550 size_t iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
, size_t size
)
1552 size_t unmapped_page
, unmapped
= 0;
1553 unsigned int min_pagesz
;
1554 unsigned long orig_iova
= iova
;
1556 if (unlikely(domain
->ops
->unmap
== NULL
||
1557 domain
->pgsize_bitmap
== 0UL))
1560 if (unlikely(!(domain
->type
& __IOMMU_DOMAIN_PAGING
)))
1563 /* find out the minimum page size supported */
1564 min_pagesz
= 1 << __ffs(domain
->pgsize_bitmap
);
1567 * The virtual address, as well as the size of the mapping, must be
1568 * aligned (at least) to the size of the smallest page supported
1571 if (!IS_ALIGNED(iova
| size
, min_pagesz
)) {
1572 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1573 iova
, size
, min_pagesz
);
1577 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova
, size
);
1580 * Keep iterating until we either unmap 'size' bytes (or more)
1581 * or we hit an area that isn't mapped.
1583 while (unmapped
< size
) {
1584 size_t pgsize
= iommu_pgsize(domain
, iova
, size
- unmapped
);
1586 unmapped_page
= domain
->ops
->unmap(domain
, iova
, pgsize
);
1590 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1591 iova
, unmapped_page
);
1593 iova
+= unmapped_page
;
1594 unmapped
+= unmapped_page
;
1597 trace_unmap(orig_iova
, size
, unmapped
);
1600 EXPORT_SYMBOL_GPL(iommu_unmap
);
1602 size_t default_iommu_map_sg(struct iommu_domain
*domain
, unsigned long iova
,
1603 struct scatterlist
*sg
, unsigned int nents
, int prot
)
1605 struct scatterlist
*s
;
1607 unsigned int i
, min_pagesz
;
1610 if (unlikely(domain
->pgsize_bitmap
== 0UL))
1613 min_pagesz
= 1 << __ffs(domain
->pgsize_bitmap
);
1615 for_each_sg(sg
, s
, nents
, i
) {
1616 phys_addr_t phys
= page_to_phys(sg_page(s
)) + s
->offset
;
1619 * We are mapping on IOMMU page boundaries, so offset within
1620 * the page must be 0. However, the IOMMU may support pages
1621 * smaller than PAGE_SIZE, so s->offset may still represent
1622 * an offset of that boundary within the CPU page.
1624 if (!IS_ALIGNED(s
->offset
, min_pagesz
))
1627 ret
= iommu_map(domain
, iova
+ mapped
, phys
, s
->length
, prot
);
1631 mapped
+= s
->length
;
1637 /* undo mappings already done */
1638 iommu_unmap(domain
, iova
, mapped
);
1643 EXPORT_SYMBOL_GPL(default_iommu_map_sg
);
1645 int iommu_domain_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
1646 phys_addr_t paddr
, u64 size
, int prot
)
1648 if (unlikely(domain
->ops
->domain_window_enable
== NULL
))
1651 return domain
->ops
->domain_window_enable(domain
, wnd_nr
, paddr
, size
,
1654 EXPORT_SYMBOL_GPL(iommu_domain_window_enable
);
1656 void iommu_domain_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
1658 if (unlikely(domain
->ops
->domain_window_disable
== NULL
))
1661 return domain
->ops
->domain_window_disable(domain
, wnd_nr
);
1663 EXPORT_SYMBOL_GPL(iommu_domain_window_disable
);
1666 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1667 * @domain: the iommu domain where the fault has happened
1668 * @dev: the device where the fault has happened
1669 * @iova: the faulting address
1670 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1672 * This function should be called by the low-level IOMMU implementations
1673 * whenever IOMMU faults happen, to allow high-level users, that are
1674 * interested in such events, to know about them.
1676 * This event may be useful for several possible use cases:
1677 * - mere logging of the event
1678 * - dynamic TLB/PTE loading
1679 * - if restarting of the faulting device is required
1681 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1682 * PTE/TLB loading will one day be supported, implementations will be able
1683 * to tell whether it succeeded or not according to this return value).
1685 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1686 * (though fault handlers can also return -ENOSYS, in case they want to
1687 * elicit the default behavior of the IOMMU drivers).
1689 int report_iommu_fault(struct iommu_domain
*domain
, struct device
*dev
,
1690 unsigned long iova
, int flags
)
1695 * if upper layers showed interest and installed a fault handler,
1698 if (domain
->handler
)
1699 ret
= domain
->handler(domain
, dev
, iova
, flags
,
1700 domain
->handler_token
);
1702 trace_io_page_fault(dev
, iova
, flags
);
1705 EXPORT_SYMBOL_GPL(report_iommu_fault
);
1707 static int __init
iommu_init(void)
1709 iommu_group_kset
= kset_create_and_add("iommu_groups",
1711 BUG_ON(!iommu_group_kset
);
1715 core_initcall(iommu_init
);
1717 int iommu_domain_get_attr(struct iommu_domain
*domain
,
1718 enum iommu_attr attr
, void *data
)
1720 struct iommu_domain_geometry
*geometry
;
1726 case DOMAIN_ATTR_GEOMETRY
:
1728 *geometry
= domain
->geometry
;
1731 case DOMAIN_ATTR_PAGING
:
1733 *paging
= (domain
->pgsize_bitmap
!= 0UL);
1735 case DOMAIN_ATTR_WINDOWS
:
1738 if (domain
->ops
->domain_get_windows
!= NULL
)
1739 *count
= domain
->ops
->domain_get_windows(domain
);
1745 if (!domain
->ops
->domain_get_attr
)
1748 ret
= domain
->ops
->domain_get_attr(domain
, attr
, data
);
1753 EXPORT_SYMBOL_GPL(iommu_domain_get_attr
);
1755 int iommu_domain_set_attr(struct iommu_domain
*domain
,
1756 enum iommu_attr attr
, void *data
)
1762 case DOMAIN_ATTR_WINDOWS
:
1765 if (domain
->ops
->domain_set_windows
!= NULL
)
1766 ret
= domain
->ops
->domain_set_windows(domain
, *count
);
1772 if (domain
->ops
->domain_set_attr
== NULL
)
1775 ret
= domain
->ops
->domain_set_attr(domain
, attr
, data
);
1780 EXPORT_SYMBOL_GPL(iommu_domain_set_attr
);
1782 void iommu_get_resv_regions(struct device
*dev
, struct list_head
*list
)
1784 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
1786 if (ops
&& ops
->get_resv_regions
)
1787 ops
->get_resv_regions(dev
, list
);
1790 void iommu_put_resv_regions(struct device
*dev
, struct list_head
*list
)
1792 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
1794 if (ops
&& ops
->put_resv_regions
)
1795 ops
->put_resv_regions(dev
, list
);
1798 struct iommu_resv_region
*iommu_alloc_resv_region(phys_addr_t start
,
1799 size_t length
, int prot
,
1800 enum iommu_resv_type type
)
1802 struct iommu_resv_region
*region
;
1804 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1808 INIT_LIST_HEAD(®ion
->list
);
1809 region
->start
= start
;
1810 region
->length
= length
;
1811 region
->prot
= prot
;
1812 region
->type
= type
;
1816 /* Request that a device is direct mapped by the IOMMU */
1817 int iommu_request_dm_for_dev(struct device
*dev
)
1819 struct iommu_domain
*dm_domain
;
1820 struct iommu_group
*group
;
1823 /* Device must already be in a group before calling this function */
1824 group
= iommu_group_get_for_dev(dev
);
1826 return PTR_ERR(group
);
1828 mutex_lock(&group
->mutex
);
1830 /* Check if the default domain is already direct mapped */
1832 if (group
->default_domain
&&
1833 group
->default_domain
->type
== IOMMU_DOMAIN_IDENTITY
)
1836 /* Don't change mappings of existing devices */
1838 if (iommu_group_device_count(group
) != 1)
1841 /* Allocate a direct mapped domain */
1843 dm_domain
= __iommu_domain_alloc(dev
->bus
, IOMMU_DOMAIN_IDENTITY
);
1847 /* Attach the device to the domain */
1848 ret
= __iommu_attach_group(dm_domain
, group
);
1850 iommu_domain_free(dm_domain
);
1854 /* Make the direct mapped domain the default for this group */
1855 if (group
->default_domain
)
1856 iommu_domain_free(group
->default_domain
);
1857 group
->default_domain
= dm_domain
;
1859 pr_info("Using direct mapping for device %s\n", dev_name(dev
));
1863 mutex_unlock(&group
->mutex
);
1864 iommu_group_put(group
);
1869 const struct iommu_ops
*iommu_ops_from_fwnode(struct fwnode_handle
*fwnode
)
1871 const struct iommu_ops
*ops
= NULL
;
1872 struct iommu_device
*iommu
;
1874 spin_lock(&iommu_device_lock
);
1875 list_for_each_entry(iommu
, &iommu_device_list
, list
)
1876 if (iommu
->fwnode
== fwnode
) {
1880 spin_unlock(&iommu_device_lock
);
1884 int iommu_fwspec_init(struct device
*dev
, struct fwnode_handle
*iommu_fwnode
,
1885 const struct iommu_ops
*ops
)
1887 struct iommu_fwspec
*fwspec
= dev
->iommu_fwspec
;
1890 return ops
== fwspec
->ops
? 0 : -EINVAL
;
1892 fwspec
= kzalloc(sizeof(*fwspec
), GFP_KERNEL
);
1896 of_node_get(to_of_node(iommu_fwnode
));
1897 fwspec
->iommu_fwnode
= iommu_fwnode
;
1899 dev
->iommu_fwspec
= fwspec
;
1902 EXPORT_SYMBOL_GPL(iommu_fwspec_init
);
1904 void iommu_fwspec_free(struct device
*dev
)
1906 struct iommu_fwspec
*fwspec
= dev
->iommu_fwspec
;
1909 fwnode_handle_put(fwspec
->iommu_fwnode
);
1911 dev
->iommu_fwspec
= NULL
;
1914 EXPORT_SYMBOL_GPL(iommu_fwspec_free
);
1916 int iommu_fwspec_add_ids(struct device
*dev
, u32
*ids
, int num_ids
)
1918 struct iommu_fwspec
*fwspec
= dev
->iommu_fwspec
;
1925 size
= offsetof(struct iommu_fwspec
, ids
[fwspec
->num_ids
+ num_ids
]);
1926 if (size
> sizeof(*fwspec
)) {
1927 fwspec
= krealloc(dev
->iommu_fwspec
, size
, GFP_KERNEL
);
1931 dev
->iommu_fwspec
= fwspec
;
1934 for (i
= 0; i
< num_ids
; i
++)
1935 fwspec
->ids
[fwspec
->num_ids
+ i
] = ids
[i
];
1937 fwspec
->num_ids
+= num_ids
;
1940 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids
);