2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
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) "%s: " fmt, __func__
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 <trace/events/iommu.h>
35 static struct kset
*iommu_group_kset
;
36 static struct ida iommu_group_ida
;
37 static struct mutex iommu_group_mutex
;
39 struct iommu_callback_data
{
40 const struct iommu_ops
*ops
;
45 struct kobject
*devices_kobj
;
46 struct list_head devices
;
48 struct blocking_notifier_head notifier
;
50 void (*iommu_data_release
)(void *iommu_data
);
56 struct list_head list
;
61 struct iommu_group_attribute
{
62 struct attribute attr
;
63 ssize_t (*show
)(struct iommu_group
*group
, char *buf
);
64 ssize_t (*store
)(struct iommu_group
*group
,
65 const char *buf
, size_t count
);
68 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
69 struct iommu_group_attribute iommu_group_attr_##_name = \
70 __ATTR(_name, _mode, _show, _store)
72 #define to_iommu_group_attr(_attr) \
73 container_of(_attr, struct iommu_group_attribute, attr)
74 #define to_iommu_group(_kobj) \
75 container_of(_kobj, struct iommu_group, kobj)
77 static ssize_t
iommu_group_attr_show(struct kobject
*kobj
,
78 struct attribute
*__attr
, char *buf
)
80 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
81 struct iommu_group
*group
= to_iommu_group(kobj
);
85 ret
= attr
->show(group
, buf
);
89 static ssize_t
iommu_group_attr_store(struct kobject
*kobj
,
90 struct attribute
*__attr
,
91 const char *buf
, size_t count
)
93 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
94 struct iommu_group
*group
= to_iommu_group(kobj
);
98 ret
= attr
->store(group
, buf
, count
);
102 static const struct sysfs_ops iommu_group_sysfs_ops
= {
103 .show
= iommu_group_attr_show
,
104 .store
= iommu_group_attr_store
,
107 static int iommu_group_create_file(struct iommu_group
*group
,
108 struct iommu_group_attribute
*attr
)
110 return sysfs_create_file(&group
->kobj
, &attr
->attr
);
113 static void iommu_group_remove_file(struct iommu_group
*group
,
114 struct iommu_group_attribute
*attr
)
116 sysfs_remove_file(&group
->kobj
, &attr
->attr
);
119 static ssize_t
iommu_group_show_name(struct iommu_group
*group
, char *buf
)
121 return sprintf(buf
, "%s\n", group
->name
);
124 static IOMMU_GROUP_ATTR(name
, S_IRUGO
, iommu_group_show_name
, NULL
);
126 static void iommu_group_release(struct kobject
*kobj
)
128 struct iommu_group
*group
= to_iommu_group(kobj
);
130 if (group
->iommu_data_release
)
131 group
->iommu_data_release(group
->iommu_data
);
133 mutex_lock(&iommu_group_mutex
);
134 ida_remove(&iommu_group_ida
, group
->id
);
135 mutex_unlock(&iommu_group_mutex
);
141 static struct kobj_type iommu_group_ktype
= {
142 .sysfs_ops
= &iommu_group_sysfs_ops
,
143 .release
= iommu_group_release
,
147 * iommu_group_alloc - Allocate a new group
148 * @name: Optional name to associate with group, visible in sysfs
150 * This function is called by an iommu driver to allocate a new iommu
151 * group. The iommu group represents the minimum granularity of the iommu.
152 * Upon successful return, the caller holds a reference to the supplied
153 * group in order to hold the group until devices are added. Use
154 * iommu_group_put() to release this extra reference count, allowing the
155 * group to be automatically reclaimed once it has no devices or external
158 struct iommu_group
*iommu_group_alloc(void)
160 struct iommu_group
*group
;
163 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
165 return ERR_PTR(-ENOMEM
);
167 group
->kobj
.kset
= iommu_group_kset
;
168 mutex_init(&group
->mutex
);
169 INIT_LIST_HEAD(&group
->devices
);
170 BLOCKING_INIT_NOTIFIER_HEAD(&group
->notifier
);
172 mutex_lock(&iommu_group_mutex
);
175 if (unlikely(0 == ida_pre_get(&iommu_group_ida
, GFP_KERNEL
))) {
177 mutex_unlock(&iommu_group_mutex
);
178 return ERR_PTR(-ENOMEM
);
181 if (-EAGAIN
== ida_get_new(&iommu_group_ida
, &group
->id
))
184 mutex_unlock(&iommu_group_mutex
);
186 ret
= kobject_init_and_add(&group
->kobj
, &iommu_group_ktype
,
187 NULL
, "%d", group
->id
);
189 mutex_lock(&iommu_group_mutex
);
190 ida_remove(&iommu_group_ida
, group
->id
);
191 mutex_unlock(&iommu_group_mutex
);
196 group
->devices_kobj
= kobject_create_and_add("devices", &group
->kobj
);
197 if (!group
->devices_kobj
) {
198 kobject_put(&group
->kobj
); /* triggers .release & free */
199 return ERR_PTR(-ENOMEM
);
203 * The devices_kobj holds a reference on the group kobject, so
204 * as long as that exists so will the group. We can therefore
205 * use the devices_kobj for reference counting.
207 kobject_put(&group
->kobj
);
211 EXPORT_SYMBOL_GPL(iommu_group_alloc
);
213 struct iommu_group
*iommu_group_get_by_id(int id
)
215 struct kobject
*group_kobj
;
216 struct iommu_group
*group
;
219 if (!iommu_group_kset
)
222 name
= kasprintf(GFP_KERNEL
, "%d", id
);
226 group_kobj
= kset_find_obj(iommu_group_kset
, name
);
232 group
= container_of(group_kobj
, struct iommu_group
, kobj
);
233 BUG_ON(group
->id
!= id
);
235 kobject_get(group
->devices_kobj
);
236 kobject_put(&group
->kobj
);
240 EXPORT_SYMBOL_GPL(iommu_group_get_by_id
);
243 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
246 * iommu drivers can store data in the group for use when doing iommu
247 * operations. This function provides a way to retrieve it. Caller
248 * should hold a group reference.
250 void *iommu_group_get_iommudata(struct iommu_group
*group
)
252 return group
->iommu_data
;
254 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata
);
257 * iommu_group_set_iommudata - set iommu_data for a group
259 * @iommu_data: new data
260 * @release: release function for iommu_data
262 * iommu drivers can store data in the group for use when doing iommu
263 * operations. This function provides a way to set the data after
264 * the group has been allocated. Caller should hold a group reference.
266 void iommu_group_set_iommudata(struct iommu_group
*group
, void *iommu_data
,
267 void (*release
)(void *iommu_data
))
269 group
->iommu_data
= iommu_data
;
270 group
->iommu_data_release
= release
;
272 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata
);
275 * iommu_group_set_name - set name for a group
279 * Allow iommu driver to set a name for a group. When set it will
280 * appear in a name attribute file under the group in sysfs.
282 int iommu_group_set_name(struct iommu_group
*group
, const char *name
)
287 iommu_group_remove_file(group
, &iommu_group_attr_name
);
294 group
->name
= kstrdup(name
, GFP_KERNEL
);
298 ret
= iommu_group_create_file(group
, &iommu_group_attr_name
);
307 EXPORT_SYMBOL_GPL(iommu_group_set_name
);
310 * iommu_group_add_device - add a device to an iommu group
311 * @group: the group into which to add the device (reference should be held)
314 * This function is called by an iommu driver to add a device into a
315 * group. Adding a device increments the group reference count.
317 int iommu_group_add_device(struct iommu_group
*group
, struct device
*dev
)
320 struct iommu_device
*device
;
322 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
328 ret
= sysfs_create_link(&dev
->kobj
, &group
->kobj
, "iommu_group");
334 device
->name
= kasprintf(GFP_KERNEL
, "%s", kobject_name(&dev
->kobj
));
337 sysfs_remove_link(&dev
->kobj
, "iommu_group");
342 ret
= sysfs_create_link_nowarn(group
->devices_kobj
,
343 &dev
->kobj
, device
->name
);
346 if (ret
== -EEXIST
&& i
>= 0) {
348 * Account for the slim chance of collision
349 * and append an instance to the name.
351 device
->name
= kasprintf(GFP_KERNEL
, "%s.%d",
352 kobject_name(&dev
->kobj
), i
++);
356 sysfs_remove_link(&dev
->kobj
, "iommu_group");
361 kobject_get(group
->devices_kobj
);
363 dev
->iommu_group
= group
;
365 mutex_lock(&group
->mutex
);
366 list_add_tail(&device
->list
, &group
->devices
);
367 mutex_unlock(&group
->mutex
);
369 /* Notify any listeners about change to group. */
370 blocking_notifier_call_chain(&group
->notifier
,
371 IOMMU_GROUP_NOTIFY_ADD_DEVICE
, dev
);
373 trace_add_device_to_group(group
->id
, dev
);
376 EXPORT_SYMBOL_GPL(iommu_group_add_device
);
379 * iommu_group_remove_device - remove a device from it's current group
380 * @dev: device to be removed
382 * This function is called by an iommu driver to remove the device from
383 * it's current group. This decrements the iommu group reference count.
385 void iommu_group_remove_device(struct device
*dev
)
387 struct iommu_group
*group
= dev
->iommu_group
;
388 struct iommu_device
*tmp_device
, *device
= NULL
;
390 /* Pre-notify listeners that a device is being removed. */
391 blocking_notifier_call_chain(&group
->notifier
,
392 IOMMU_GROUP_NOTIFY_DEL_DEVICE
, dev
);
394 mutex_lock(&group
->mutex
);
395 list_for_each_entry(tmp_device
, &group
->devices
, list
) {
396 if (tmp_device
->dev
== dev
) {
398 list_del(&device
->list
);
402 mutex_unlock(&group
->mutex
);
407 sysfs_remove_link(group
->devices_kobj
, device
->name
);
408 sysfs_remove_link(&dev
->kobj
, "iommu_group");
410 trace_remove_device_from_group(group
->id
, dev
);
414 dev
->iommu_group
= NULL
;
415 kobject_put(group
->devices_kobj
);
417 EXPORT_SYMBOL_GPL(iommu_group_remove_device
);
420 * iommu_group_for_each_dev - iterate over each device in the group
422 * @data: caller opaque data to be passed to callback function
423 * @fn: caller supplied callback function
425 * This function is called by group users to iterate over group devices.
426 * Callers should hold a reference count to the group during callback.
427 * The group->mutex is held across callbacks, which will block calls to
428 * iommu_group_add/remove_device.
430 int iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
431 int (*fn
)(struct device
*, void *))
433 struct iommu_device
*device
;
436 mutex_lock(&group
->mutex
);
437 list_for_each_entry(device
, &group
->devices
, list
) {
438 ret
= fn(device
->dev
, data
);
442 mutex_unlock(&group
->mutex
);
445 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev
);
448 * iommu_group_get - Return the group for a device and increment reference
449 * @dev: get the group that this device belongs to
451 * This function is called by iommu drivers and users to get the group
452 * for the specified device. If found, the group is returned and the group
453 * reference in incremented, else NULL.
455 struct iommu_group
*iommu_group_get(struct device
*dev
)
457 struct iommu_group
*group
= dev
->iommu_group
;
460 kobject_get(group
->devices_kobj
);
464 EXPORT_SYMBOL_GPL(iommu_group_get
);
467 * iommu_group_put - Decrement group reference
468 * @group: the group to use
470 * This function is called by iommu drivers and users to release the
471 * iommu group. Once the reference count is zero, the group is released.
473 void iommu_group_put(struct iommu_group
*group
)
476 kobject_put(group
->devices_kobj
);
478 EXPORT_SYMBOL_GPL(iommu_group_put
);
481 * iommu_group_register_notifier - Register a notifier for group changes
482 * @group: the group to watch
483 * @nb: notifier block to signal
485 * This function allows iommu group users to track changes in a group.
486 * See include/linux/iommu.h for actions sent via this notifier. Caller
487 * should hold a reference to the group throughout notifier registration.
489 int iommu_group_register_notifier(struct iommu_group
*group
,
490 struct notifier_block
*nb
)
492 return blocking_notifier_chain_register(&group
->notifier
, nb
);
494 EXPORT_SYMBOL_GPL(iommu_group_register_notifier
);
497 * iommu_group_unregister_notifier - Unregister a notifier
498 * @group: the group to watch
499 * @nb: notifier block to signal
501 * Unregister a previously registered group notifier block.
503 int iommu_group_unregister_notifier(struct iommu_group
*group
,
504 struct notifier_block
*nb
)
506 return blocking_notifier_chain_unregister(&group
->notifier
, nb
);
508 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier
);
511 * iommu_group_id - Return ID for a group
512 * @group: the group to ID
514 * Return the unique ID for the group matching the sysfs group number.
516 int iommu_group_id(struct iommu_group
*group
)
520 EXPORT_SYMBOL_GPL(iommu_group_id
);
523 * To consider a PCI device isolated, we require ACS to support Source
524 * Validation, Request Redirection, Completer Redirection, and Upstream
525 * Forwarding. This effectively means that devices cannot spoof their
526 * requester ID, requests and completions cannot be redirected, and all
527 * transactions are forwarded upstream, even as it passes through a
528 * bridge where the target device is downstream.
530 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
532 struct group_for_pci_data
{
533 struct pci_dev
*pdev
;
534 struct iommu_group
*group
;
538 * DMA alias iterator callback, return the last seen device. Stop and return
539 * the IOMMU group if we find one along the way.
541 static int get_pci_alias_or_group(struct pci_dev
*pdev
, u16 alias
, void *opaque
)
543 struct group_for_pci_data
*data
= opaque
;
546 data
->group
= iommu_group_get(&pdev
->dev
);
548 return data
->group
!= NULL
;
552 * Use standard PCI bus topology, isolation features, and DMA alias quirks
553 * to find or create an IOMMU group for a device.
555 static struct iommu_group
*iommu_group_get_for_pci_dev(struct pci_dev
*pdev
)
557 struct group_for_pci_data data
;
559 struct iommu_group
*group
= NULL
;
563 * Find the upstream DMA alias for the device. A device must not
564 * be aliased due to topology in order to have its own IOMMU group.
565 * If we find an alias along the way that already belongs to a
568 if (pci_for_each_dma_alias(pdev
, get_pci_alias_or_group
, &data
))
574 * Continue upstream from the point of minimum IOMMU granularity
575 * due to aliases to the point where devices are protected from
576 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
579 for (bus
= pdev
->bus
; !pci_is_root_bus(bus
); bus
= bus
->parent
) {
583 if (pci_acs_path_enabled(bus
->self
, NULL
, REQ_ACS_FLAGS
))
588 group
= iommu_group_get(&pdev
->dev
);
594 * Next we need to consider DMA alias quirks. If one device aliases
595 * to another, they should be grouped together. It's theoretically
596 * possible that aliases could create chains of devices where each
597 * device aliases another device. If we then factor in multifunction
598 * ACS grouping requirements, each alias could incorporate a new slot
599 * with multiple functions, each with aliases. This is all extremely
600 * unlikely as DMA alias quirks are typically only used for PCIe
601 * devices where we usually have a single slot per bus. Furthermore,
602 * the alias quirk is usually to another function within the slot
603 * (and ACS multifunction is not supported) or to a different slot
604 * that doesn't physically exist. The likely scenario is therefore
605 * that everything on the bus gets grouped together. To reduce the
606 * problem space, share the IOMMU group for all devices on the bus
607 * if a DMA alias quirk is present on the bus.
610 for_each_pci_dev(tmp
) {
611 if (tmp
->bus
!= pdev
->bus
||
612 !(tmp
->dev_flags
& PCI_DEV_FLAGS_DMA_ALIAS_DEVFN
))
618 /* We have an alias quirk, search for an existing group */
619 for_each_pci_dev(tmp
) {
620 struct iommu_group
*group_tmp
;
622 if (tmp
->bus
!= pdev
->bus
)
625 group_tmp
= iommu_group_get(&tmp
->dev
);
632 WARN_ON(group
!= group_tmp
);
633 iommu_group_put(group_tmp
);
637 return group
? group
: iommu_group_alloc();
641 * Non-multifunction devices or multifunction devices supporting
642 * ACS get their own group.
644 if (!pdev
->multifunction
|| pci_acs_enabled(pdev
, REQ_ACS_FLAGS
))
645 return iommu_group_alloc();
648 * Multifunction devices not supporting ACS share a group with other
649 * similar devices in the same slot.
652 for_each_pci_dev(tmp
) {
653 if (tmp
== pdev
|| tmp
->bus
!= pdev
->bus
||
654 PCI_SLOT(tmp
->devfn
) != PCI_SLOT(pdev
->devfn
) ||
655 pci_acs_enabled(tmp
, REQ_ACS_FLAGS
))
658 group
= iommu_group_get(&tmp
->dev
);
665 /* No shared group found, allocate new */
666 return iommu_group_alloc();
670 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
671 * @dev: target device
673 * This function is intended to be called by IOMMU drivers and extended to
674 * support common, bus-defined algorithms when determining or creating the
675 * IOMMU group for a device. On success, the caller will hold a reference
676 * to the returned IOMMU group, which will already include the provided
677 * device. The reference should be released with iommu_group_put().
679 struct iommu_group
*iommu_group_get_for_dev(struct device
*dev
)
681 struct iommu_group
*group
= ERR_PTR(-EIO
);
684 group
= iommu_group_get(dev
);
689 group
= iommu_group_get_for_pci_dev(to_pci_dev(dev
));
694 ret
= iommu_group_add_device(group
, dev
);
696 iommu_group_put(group
);
703 static int add_iommu_group(struct device
*dev
, void *data
)
705 struct iommu_callback_data
*cb
= data
;
706 const struct iommu_ops
*ops
= cb
->ops
;
708 if (!ops
->add_device
)
711 WARN_ON(dev
->iommu_group
);
713 ops
->add_device(dev
);
718 static int iommu_bus_notifier(struct notifier_block
*nb
,
719 unsigned long action
, void *data
)
721 struct device
*dev
= data
;
722 const struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
723 struct iommu_group
*group
;
724 unsigned long group_action
= 0;
727 * ADD/DEL call into iommu driver ops if provided, which may
728 * result in ADD/DEL notifiers to group->notifier
730 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
732 return ops
->add_device(dev
);
733 } else if (action
== BUS_NOTIFY_DEL_DEVICE
) {
734 if (ops
->remove_device
&& dev
->iommu_group
) {
735 ops
->remove_device(dev
);
741 * Remaining BUS_NOTIFYs get filtered and republished to the
742 * group, if anyone is listening
744 group
= iommu_group_get(dev
);
749 case BUS_NOTIFY_BIND_DRIVER
:
750 group_action
= IOMMU_GROUP_NOTIFY_BIND_DRIVER
;
752 case BUS_NOTIFY_BOUND_DRIVER
:
753 group_action
= IOMMU_GROUP_NOTIFY_BOUND_DRIVER
;
755 case BUS_NOTIFY_UNBIND_DRIVER
:
756 group_action
= IOMMU_GROUP_NOTIFY_UNBIND_DRIVER
;
758 case BUS_NOTIFY_UNBOUND_DRIVER
:
759 group_action
= IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER
;
764 blocking_notifier_call_chain(&group
->notifier
,
767 iommu_group_put(group
);
771 static struct notifier_block iommu_bus_nb
= {
772 .notifier_call
= iommu_bus_notifier
,
775 static void iommu_bus_init(struct bus_type
*bus
, const struct iommu_ops
*ops
)
777 struct iommu_callback_data cb
= {
781 bus_register_notifier(bus
, &iommu_bus_nb
);
782 bus_for_each_dev(bus
, NULL
, &cb
, add_iommu_group
);
786 * bus_set_iommu - set iommu-callbacks for the bus
788 * @ops: the callbacks provided by the iommu-driver
790 * This function is called by an iommu driver to set the iommu methods
791 * used for a particular bus. Drivers for devices on that bus can use
792 * the iommu-api after these ops are registered.
793 * This special function is needed because IOMMUs are usually devices on
794 * the bus itself, so the iommu drivers are not initialized when the bus
795 * is set up. With this function the iommu-driver can set the iommu-ops
798 int bus_set_iommu(struct bus_type
*bus
, const struct iommu_ops
*ops
)
800 if (bus
->iommu_ops
!= NULL
)
803 bus
->iommu_ops
= ops
;
805 /* Do IOMMU specific setup for this bus-type */
806 iommu_bus_init(bus
, ops
);
810 EXPORT_SYMBOL_GPL(bus_set_iommu
);
812 bool iommu_present(struct bus_type
*bus
)
814 return bus
->iommu_ops
!= NULL
;
816 EXPORT_SYMBOL_GPL(iommu_present
);
819 * iommu_set_fault_handler() - set a fault handler for an iommu domain
820 * @domain: iommu domain
821 * @handler: fault handler
822 * @token: user data, will be passed back to the fault handler
824 * This function should be used by IOMMU users which want to be notified
825 * whenever an IOMMU fault happens.
827 * The fault handler itself should return 0 on success, and an appropriate
828 * error code otherwise.
830 void iommu_set_fault_handler(struct iommu_domain
*domain
,
831 iommu_fault_handler_t handler
,
836 domain
->handler
= handler
;
837 domain
->handler_token
= token
;
839 EXPORT_SYMBOL_GPL(iommu_set_fault_handler
);
841 struct iommu_domain
*iommu_domain_alloc(struct bus_type
*bus
)
843 struct iommu_domain
*domain
;
846 if (bus
== NULL
|| bus
->iommu_ops
== NULL
)
849 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
853 domain
->ops
= bus
->iommu_ops
;
855 ret
= domain
->ops
->domain_init(domain
);
866 EXPORT_SYMBOL_GPL(iommu_domain_alloc
);
868 void iommu_domain_free(struct iommu_domain
*domain
)
870 if (likely(domain
->ops
->domain_destroy
!= NULL
))
871 domain
->ops
->domain_destroy(domain
);
875 EXPORT_SYMBOL_GPL(iommu_domain_free
);
877 int iommu_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
880 if (unlikely(domain
->ops
->attach_dev
== NULL
))
883 ret
= domain
->ops
->attach_dev(domain
, dev
);
885 trace_attach_device_to_domain(dev
);
888 EXPORT_SYMBOL_GPL(iommu_attach_device
);
890 void iommu_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
892 if (unlikely(domain
->ops
->detach_dev
== NULL
))
895 domain
->ops
->detach_dev(domain
, dev
);
896 trace_detach_device_from_domain(dev
);
898 EXPORT_SYMBOL_GPL(iommu_detach_device
);
901 * IOMMU groups are really the natrual working unit of the IOMMU, but
902 * the IOMMU API works on domains and devices. Bridge that gap by
903 * iterating over the devices in a group. Ideally we'd have a single
904 * device which represents the requestor ID of the group, but we also
905 * allow IOMMU drivers to create policy defined minimum sets, where
906 * the physical hardware may be able to distiguish members, but we
907 * wish to group them at a higher level (ex. untrusted multi-function
908 * PCI devices). Thus we attach each device.
910 static int iommu_group_do_attach_device(struct device
*dev
, void *data
)
912 struct iommu_domain
*domain
= data
;
914 return iommu_attach_device(domain
, dev
);
917 int iommu_attach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
919 return iommu_group_for_each_dev(group
, domain
,
920 iommu_group_do_attach_device
);
922 EXPORT_SYMBOL_GPL(iommu_attach_group
);
924 static int iommu_group_do_detach_device(struct device
*dev
, void *data
)
926 struct iommu_domain
*domain
= data
;
928 iommu_detach_device(domain
, dev
);
933 void iommu_detach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
935 iommu_group_for_each_dev(group
, domain
, iommu_group_do_detach_device
);
937 EXPORT_SYMBOL_GPL(iommu_detach_group
);
939 phys_addr_t
iommu_iova_to_phys(struct iommu_domain
*domain
, dma_addr_t iova
)
941 if (unlikely(domain
->ops
->iova_to_phys
== NULL
))
944 return domain
->ops
->iova_to_phys(domain
, iova
);
946 EXPORT_SYMBOL_GPL(iommu_iova_to_phys
);
948 int iommu_domain_has_cap(struct iommu_domain
*domain
,
951 if (unlikely(domain
->ops
->domain_has_cap
== NULL
))
954 return domain
->ops
->domain_has_cap(domain
, cap
);
956 EXPORT_SYMBOL_GPL(iommu_domain_has_cap
);
958 static size_t iommu_pgsize(struct iommu_domain
*domain
,
959 unsigned long addr_merge
, size_t size
)
961 unsigned int pgsize_idx
;
964 /* Max page size that still fits into 'size' */
965 pgsize_idx
= __fls(size
);
967 /* need to consider alignment requirements ? */
968 if (likely(addr_merge
)) {
969 /* Max page size allowed by address */
970 unsigned int align_pgsize_idx
= __ffs(addr_merge
);
971 pgsize_idx
= min(pgsize_idx
, align_pgsize_idx
);
974 /* build a mask of acceptable page sizes */
975 pgsize
= (1UL << (pgsize_idx
+ 1)) - 1;
977 /* throw away page sizes not supported by the hardware */
978 pgsize
&= domain
->ops
->pgsize_bitmap
;
980 /* make sure we're still sane */
983 /* pick the biggest page */
984 pgsize_idx
= __fls(pgsize
);
985 pgsize
= 1UL << pgsize_idx
;
990 int iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
991 phys_addr_t paddr
, size_t size
, int prot
)
993 unsigned long orig_iova
= iova
;
994 unsigned int min_pagesz
;
995 size_t orig_size
= size
;
998 if (unlikely(domain
->ops
->unmap
== NULL
||
999 domain
->ops
->pgsize_bitmap
== 0UL))
1002 /* find out the minimum page size supported */
1003 min_pagesz
= 1 << __ffs(domain
->ops
->pgsize_bitmap
);
1006 * both the virtual address and the physical one, as well as
1007 * the size of the mapping, must be aligned (at least) to the
1008 * size of the smallest page supported by the hardware
1010 if (!IS_ALIGNED(iova
| paddr
| size
, min_pagesz
)) {
1011 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1012 iova
, &paddr
, size
, min_pagesz
);
1016 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova
, &paddr
, size
);
1019 size_t pgsize
= iommu_pgsize(domain
, iova
| paddr
, size
);
1021 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1022 iova
, &paddr
, pgsize
);
1024 ret
= domain
->ops
->map(domain
, iova
, paddr
, pgsize
, prot
);
1033 /* unroll mapping in case something went wrong */
1035 iommu_unmap(domain
, orig_iova
, orig_size
- size
);
1037 trace_map(iova
, paddr
, size
);
1041 EXPORT_SYMBOL_GPL(iommu_map
);
1043 size_t iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
, size_t size
)
1045 size_t unmapped_page
, unmapped
= 0;
1046 unsigned int min_pagesz
;
1048 if (unlikely(domain
->ops
->unmap
== NULL
||
1049 domain
->ops
->pgsize_bitmap
== 0UL))
1052 /* find out the minimum page size supported */
1053 min_pagesz
= 1 << __ffs(domain
->ops
->pgsize_bitmap
);
1056 * The virtual address, as well as the size of the mapping, must be
1057 * aligned (at least) to the size of the smallest page supported
1060 if (!IS_ALIGNED(iova
| size
, min_pagesz
)) {
1061 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1062 iova
, size
, min_pagesz
);
1066 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova
, size
);
1069 * Keep iterating until we either unmap 'size' bytes (or more)
1070 * or we hit an area that isn't mapped.
1072 while (unmapped
< size
) {
1073 size_t pgsize
= iommu_pgsize(domain
, iova
, size
- unmapped
);
1075 unmapped_page
= domain
->ops
->unmap(domain
, iova
, pgsize
);
1079 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1080 iova
, unmapped_page
);
1082 iova
+= unmapped_page
;
1083 unmapped
+= unmapped_page
;
1086 trace_unmap(iova
, 0, size
);
1089 EXPORT_SYMBOL_GPL(iommu_unmap
);
1092 int iommu_domain_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
1093 phys_addr_t paddr
, u64 size
, int prot
)
1095 if (unlikely(domain
->ops
->domain_window_enable
== NULL
))
1098 return domain
->ops
->domain_window_enable(domain
, wnd_nr
, paddr
, size
,
1101 EXPORT_SYMBOL_GPL(iommu_domain_window_enable
);
1103 void iommu_domain_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
1105 if (unlikely(domain
->ops
->domain_window_disable
== NULL
))
1108 return domain
->ops
->domain_window_disable(domain
, wnd_nr
);
1110 EXPORT_SYMBOL_GPL(iommu_domain_window_disable
);
1112 static int __init
iommu_init(void)
1114 iommu_group_kset
= kset_create_and_add("iommu_groups",
1116 ida_init(&iommu_group_ida
);
1117 mutex_init(&iommu_group_mutex
);
1119 BUG_ON(!iommu_group_kset
);
1123 arch_initcall(iommu_init
);
1125 int iommu_domain_get_attr(struct iommu_domain
*domain
,
1126 enum iommu_attr attr
, void *data
)
1128 struct iommu_domain_geometry
*geometry
;
1134 case DOMAIN_ATTR_GEOMETRY
:
1136 *geometry
= domain
->geometry
;
1139 case DOMAIN_ATTR_PAGING
:
1141 *paging
= (domain
->ops
->pgsize_bitmap
!= 0UL);
1143 case DOMAIN_ATTR_WINDOWS
:
1146 if (domain
->ops
->domain_get_windows
!= NULL
)
1147 *count
= domain
->ops
->domain_get_windows(domain
);
1153 if (!domain
->ops
->domain_get_attr
)
1156 ret
= domain
->ops
->domain_get_attr(domain
, attr
, data
);
1161 EXPORT_SYMBOL_GPL(iommu_domain_get_attr
);
1163 int iommu_domain_set_attr(struct iommu_domain
*domain
,
1164 enum iommu_attr attr
, void *data
)
1170 case DOMAIN_ATTR_WINDOWS
:
1173 if (domain
->ops
->domain_set_windows
!= NULL
)
1174 ret
= domain
->ops
->domain_set_windows(domain
, *count
);
1180 if (domain
->ops
->domain_set_attr
== NULL
)
1183 ret
= domain
->ops
->domain_set_attr(domain
, attr
, data
);
1188 EXPORT_SYMBOL_GPL(iommu_domain_set_attr
);