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 <trace/events/iommu.h>
34 static struct kset
*iommu_group_kset
;
35 static struct ida iommu_group_ida
;
36 static struct mutex iommu_group_mutex
;
40 struct kobject
*devices_kobj
;
41 struct list_head devices
;
43 struct blocking_notifier_head notifier
;
45 void (*iommu_data_release
)(void *iommu_data
);
51 struct list_head list
;
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 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
64 struct iommu_group_attribute iommu_group_attr_##_name = \
65 __ATTR(_name, _mode, _show, _store)
67 #define to_iommu_group_attr(_attr) \
68 container_of(_attr, struct iommu_group_attribute, attr)
69 #define to_iommu_group(_kobj) \
70 container_of(_kobj, struct iommu_group, kobj)
72 static ssize_t
iommu_group_attr_show(struct kobject
*kobj
,
73 struct attribute
*__attr
, char *buf
)
75 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
76 struct iommu_group
*group
= to_iommu_group(kobj
);
80 ret
= attr
->show(group
, buf
);
84 static ssize_t
iommu_group_attr_store(struct kobject
*kobj
,
85 struct attribute
*__attr
,
86 const char *buf
, size_t count
)
88 struct iommu_group_attribute
*attr
= to_iommu_group_attr(__attr
);
89 struct iommu_group
*group
= to_iommu_group(kobj
);
93 ret
= attr
->store(group
, buf
, count
);
97 static const struct sysfs_ops iommu_group_sysfs_ops
= {
98 .show
= iommu_group_attr_show
,
99 .store
= iommu_group_attr_store
,
102 static int iommu_group_create_file(struct iommu_group
*group
,
103 struct iommu_group_attribute
*attr
)
105 return sysfs_create_file(&group
->kobj
, &attr
->attr
);
108 static void iommu_group_remove_file(struct iommu_group
*group
,
109 struct iommu_group_attribute
*attr
)
111 sysfs_remove_file(&group
->kobj
, &attr
->attr
);
114 static ssize_t
iommu_group_show_name(struct iommu_group
*group
, char *buf
)
116 return sprintf(buf
, "%s\n", group
->name
);
119 static IOMMU_GROUP_ATTR(name
, S_IRUGO
, iommu_group_show_name
, NULL
);
121 static void iommu_group_release(struct kobject
*kobj
)
123 struct iommu_group
*group
= to_iommu_group(kobj
);
125 if (group
->iommu_data_release
)
126 group
->iommu_data_release(group
->iommu_data
);
128 mutex_lock(&iommu_group_mutex
);
129 ida_remove(&iommu_group_ida
, group
->id
);
130 mutex_unlock(&iommu_group_mutex
);
136 static struct kobj_type iommu_group_ktype
= {
137 .sysfs_ops
= &iommu_group_sysfs_ops
,
138 .release
= iommu_group_release
,
142 * iommu_group_alloc - Allocate a new group
143 * @name: Optional name to associate with group, visible in sysfs
145 * This function is called by an iommu driver to allocate a new iommu
146 * group. The iommu group represents the minimum granularity of the iommu.
147 * Upon successful return, the caller holds a reference to the supplied
148 * group in order to hold the group until devices are added. Use
149 * iommu_group_put() to release this extra reference count, allowing the
150 * group to be automatically reclaimed once it has no devices or external
153 struct iommu_group
*iommu_group_alloc(void)
155 struct iommu_group
*group
;
158 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
160 return ERR_PTR(-ENOMEM
);
162 group
->kobj
.kset
= iommu_group_kset
;
163 mutex_init(&group
->mutex
);
164 INIT_LIST_HEAD(&group
->devices
);
165 BLOCKING_INIT_NOTIFIER_HEAD(&group
->notifier
);
167 mutex_lock(&iommu_group_mutex
);
170 if (unlikely(0 == ida_pre_get(&iommu_group_ida
, GFP_KERNEL
))) {
172 mutex_unlock(&iommu_group_mutex
);
173 return ERR_PTR(-ENOMEM
);
176 if (-EAGAIN
== ida_get_new(&iommu_group_ida
, &group
->id
))
179 mutex_unlock(&iommu_group_mutex
);
181 ret
= kobject_init_and_add(&group
->kobj
, &iommu_group_ktype
,
182 NULL
, "%d", group
->id
);
184 mutex_lock(&iommu_group_mutex
);
185 ida_remove(&iommu_group_ida
, group
->id
);
186 mutex_unlock(&iommu_group_mutex
);
191 group
->devices_kobj
= kobject_create_and_add("devices", &group
->kobj
);
192 if (!group
->devices_kobj
) {
193 kobject_put(&group
->kobj
); /* triggers .release & free */
194 return ERR_PTR(-ENOMEM
);
198 * The devices_kobj holds a reference on the group kobject, so
199 * as long as that exists so will the group. We can therefore
200 * use the devices_kobj for reference counting.
202 kobject_put(&group
->kobj
);
206 EXPORT_SYMBOL_GPL(iommu_group_alloc
);
208 struct iommu_group
*iommu_group_get_by_id(int id
)
210 struct kobject
*group_kobj
;
211 struct iommu_group
*group
;
214 if (!iommu_group_kset
)
217 name
= kasprintf(GFP_KERNEL
, "%d", id
);
221 group_kobj
= kset_find_obj(iommu_group_kset
, name
);
227 group
= container_of(group_kobj
, struct iommu_group
, kobj
);
228 BUG_ON(group
->id
!= id
);
230 kobject_get(group
->devices_kobj
);
231 kobject_put(&group
->kobj
);
235 EXPORT_SYMBOL_GPL(iommu_group_get_by_id
);
238 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
241 * iommu drivers can store data in the group for use when doing iommu
242 * operations. This function provides a way to retrieve it. Caller
243 * should hold a group reference.
245 void *iommu_group_get_iommudata(struct iommu_group
*group
)
247 return group
->iommu_data
;
249 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata
);
252 * iommu_group_set_iommudata - set iommu_data for a group
254 * @iommu_data: new data
255 * @release: release function for iommu_data
257 * iommu drivers can store data in the group for use when doing iommu
258 * operations. This function provides a way to set the data after
259 * the group has been allocated. Caller should hold a group reference.
261 void iommu_group_set_iommudata(struct iommu_group
*group
, void *iommu_data
,
262 void (*release
)(void *iommu_data
))
264 group
->iommu_data
= iommu_data
;
265 group
->iommu_data_release
= release
;
267 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata
);
270 * iommu_group_set_name - set name for a group
274 * Allow iommu driver to set a name for a group. When set it will
275 * appear in a name attribute file under the group in sysfs.
277 int iommu_group_set_name(struct iommu_group
*group
, const char *name
)
282 iommu_group_remove_file(group
, &iommu_group_attr_name
);
289 group
->name
= kstrdup(name
, GFP_KERNEL
);
293 ret
= iommu_group_create_file(group
, &iommu_group_attr_name
);
302 EXPORT_SYMBOL_GPL(iommu_group_set_name
);
305 * iommu_group_add_device - add a device to an iommu group
306 * @group: the group into which to add the device (reference should be held)
309 * This function is called by an iommu driver to add a device into a
310 * group. Adding a device increments the group reference count.
312 int iommu_group_add_device(struct iommu_group
*group
, struct device
*dev
)
315 struct iommu_device
*device
;
317 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
323 ret
= sysfs_create_link(&dev
->kobj
, &group
->kobj
, "iommu_group");
329 device
->name
= kasprintf(GFP_KERNEL
, "%s", kobject_name(&dev
->kobj
));
332 sysfs_remove_link(&dev
->kobj
, "iommu_group");
337 ret
= sysfs_create_link_nowarn(group
->devices_kobj
,
338 &dev
->kobj
, device
->name
);
341 if (ret
== -EEXIST
&& i
>= 0) {
343 * Account for the slim chance of collision
344 * and append an instance to the name.
346 device
->name
= kasprintf(GFP_KERNEL
, "%s.%d",
347 kobject_name(&dev
->kobj
), i
++);
351 sysfs_remove_link(&dev
->kobj
, "iommu_group");
356 kobject_get(group
->devices_kobj
);
358 dev
->iommu_group
= group
;
360 mutex_lock(&group
->mutex
);
361 list_add_tail(&device
->list
, &group
->devices
);
362 mutex_unlock(&group
->mutex
);
364 /* Notify any listeners about change to group. */
365 blocking_notifier_call_chain(&group
->notifier
,
366 IOMMU_GROUP_NOTIFY_ADD_DEVICE
, dev
);
368 trace_add_device_to_group(group
->id
, dev
);
371 EXPORT_SYMBOL_GPL(iommu_group_add_device
);
374 * iommu_group_remove_device - remove a device from it's current group
375 * @dev: device to be removed
377 * This function is called by an iommu driver to remove the device from
378 * it's current group. This decrements the iommu group reference count.
380 void iommu_group_remove_device(struct device
*dev
)
382 struct iommu_group
*group
= dev
->iommu_group
;
383 struct iommu_device
*tmp_device
, *device
= NULL
;
385 /* Pre-notify listeners that a device is being removed. */
386 blocking_notifier_call_chain(&group
->notifier
,
387 IOMMU_GROUP_NOTIFY_DEL_DEVICE
, dev
);
389 mutex_lock(&group
->mutex
);
390 list_for_each_entry(tmp_device
, &group
->devices
, list
) {
391 if (tmp_device
->dev
== dev
) {
393 list_del(&device
->list
);
397 mutex_unlock(&group
->mutex
);
402 sysfs_remove_link(group
->devices_kobj
, device
->name
);
403 sysfs_remove_link(&dev
->kobj
, "iommu_group");
405 trace_remove_device_from_group(group
->id
, dev
);
409 dev
->iommu_group
= NULL
;
410 kobject_put(group
->devices_kobj
);
412 EXPORT_SYMBOL_GPL(iommu_group_remove_device
);
415 * iommu_group_for_each_dev - iterate over each device in the group
417 * @data: caller opaque data to be passed to callback function
418 * @fn: caller supplied callback function
420 * This function is called by group users to iterate over group devices.
421 * Callers should hold a reference count to the group during callback.
422 * The group->mutex is held across callbacks, which will block calls to
423 * iommu_group_add/remove_device.
425 int iommu_group_for_each_dev(struct iommu_group
*group
, void *data
,
426 int (*fn
)(struct device
*, void *))
428 struct iommu_device
*device
;
431 mutex_lock(&group
->mutex
);
432 list_for_each_entry(device
, &group
->devices
, list
) {
433 ret
= fn(device
->dev
, data
);
437 mutex_unlock(&group
->mutex
);
440 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev
);
443 * iommu_group_get - Return the group for a device and increment reference
444 * @dev: get the group that this device belongs to
446 * This function is called by iommu drivers and users to get the group
447 * for the specified device. If found, the group is returned and the group
448 * reference in incremented, else NULL.
450 struct iommu_group
*iommu_group_get(struct device
*dev
)
452 struct iommu_group
*group
= dev
->iommu_group
;
455 kobject_get(group
->devices_kobj
);
459 EXPORT_SYMBOL_GPL(iommu_group_get
);
462 * iommu_group_put - Decrement group reference
463 * @group: the group to use
465 * This function is called by iommu drivers and users to release the
466 * iommu group. Once the reference count is zero, the group is released.
468 void iommu_group_put(struct iommu_group
*group
)
471 kobject_put(group
->devices_kobj
);
473 EXPORT_SYMBOL_GPL(iommu_group_put
);
476 * iommu_group_register_notifier - Register a notifier for group changes
477 * @group: the group to watch
478 * @nb: notifier block to signal
480 * This function allows iommu group users to track changes in a group.
481 * See include/linux/iommu.h for actions sent via this notifier. Caller
482 * should hold a reference to the group throughout notifier registration.
484 int iommu_group_register_notifier(struct iommu_group
*group
,
485 struct notifier_block
*nb
)
487 return blocking_notifier_chain_register(&group
->notifier
, nb
);
489 EXPORT_SYMBOL_GPL(iommu_group_register_notifier
);
492 * iommu_group_unregister_notifier - Unregister a notifier
493 * @group: the group to watch
494 * @nb: notifier block to signal
496 * Unregister a previously registered group notifier block.
498 int iommu_group_unregister_notifier(struct iommu_group
*group
,
499 struct notifier_block
*nb
)
501 return blocking_notifier_chain_unregister(&group
->notifier
, nb
);
503 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier
);
506 * iommu_group_id - Return ID for a group
507 * @group: the group to ID
509 * Return the unique ID for the group matching the sysfs group number.
511 int iommu_group_id(struct iommu_group
*group
)
515 EXPORT_SYMBOL_GPL(iommu_group_id
);
517 static int add_iommu_group(struct device
*dev
, void *data
)
519 struct iommu_ops
*ops
= data
;
521 if (!ops
->add_device
)
524 WARN_ON(dev
->iommu_group
);
526 ops
->add_device(dev
);
531 static int iommu_bus_notifier(struct notifier_block
*nb
,
532 unsigned long action
, void *data
)
534 struct device
*dev
= data
;
535 struct iommu_ops
*ops
= dev
->bus
->iommu_ops
;
536 struct iommu_group
*group
;
537 unsigned long group_action
= 0;
540 * ADD/DEL call into iommu driver ops if provided, which may
541 * result in ADD/DEL notifiers to group->notifier
543 if (action
== BUS_NOTIFY_ADD_DEVICE
) {
545 return ops
->add_device(dev
);
546 } else if (action
== BUS_NOTIFY_DEL_DEVICE
) {
547 if (ops
->remove_device
&& dev
->iommu_group
) {
548 ops
->remove_device(dev
);
554 * Remaining BUS_NOTIFYs get filtered and republished to the
555 * group, if anyone is listening
557 group
= iommu_group_get(dev
);
562 case BUS_NOTIFY_BIND_DRIVER
:
563 group_action
= IOMMU_GROUP_NOTIFY_BIND_DRIVER
;
565 case BUS_NOTIFY_BOUND_DRIVER
:
566 group_action
= IOMMU_GROUP_NOTIFY_BOUND_DRIVER
;
568 case BUS_NOTIFY_UNBIND_DRIVER
:
569 group_action
= IOMMU_GROUP_NOTIFY_UNBIND_DRIVER
;
571 case BUS_NOTIFY_UNBOUND_DRIVER
:
572 group_action
= IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER
;
577 blocking_notifier_call_chain(&group
->notifier
,
580 iommu_group_put(group
);
584 static struct notifier_block iommu_bus_nb
= {
585 .notifier_call
= iommu_bus_notifier
,
588 static void iommu_bus_init(struct bus_type
*bus
, struct iommu_ops
*ops
)
590 bus_register_notifier(bus
, &iommu_bus_nb
);
591 bus_for_each_dev(bus
, NULL
, ops
, add_iommu_group
);
595 * bus_set_iommu - set iommu-callbacks for the bus
597 * @ops: the callbacks provided by the iommu-driver
599 * This function is called by an iommu driver to set the iommu methods
600 * used for a particular bus. Drivers for devices on that bus can use
601 * the iommu-api after these ops are registered.
602 * This special function is needed because IOMMUs are usually devices on
603 * the bus itself, so the iommu drivers are not initialized when the bus
604 * is set up. With this function the iommu-driver can set the iommu-ops
607 int bus_set_iommu(struct bus_type
*bus
, struct iommu_ops
*ops
)
609 if (bus
->iommu_ops
!= NULL
)
612 bus
->iommu_ops
= ops
;
614 /* Do IOMMU specific setup for this bus-type */
615 iommu_bus_init(bus
, ops
);
619 EXPORT_SYMBOL_GPL(bus_set_iommu
);
621 bool iommu_present(struct bus_type
*bus
)
623 return bus
->iommu_ops
!= NULL
;
625 EXPORT_SYMBOL_GPL(iommu_present
);
628 * iommu_set_fault_handler() - set a fault handler for an iommu domain
629 * @domain: iommu domain
630 * @handler: fault handler
631 * @token: user data, will be passed back to the fault handler
633 * This function should be used by IOMMU users which want to be notified
634 * whenever an IOMMU fault happens.
636 * The fault handler itself should return 0 on success, and an appropriate
637 * error code otherwise.
639 void iommu_set_fault_handler(struct iommu_domain
*domain
,
640 iommu_fault_handler_t handler
,
645 domain
->handler
= handler
;
646 domain
->handler_token
= token
;
648 EXPORT_SYMBOL_GPL(iommu_set_fault_handler
);
650 struct iommu_domain
*iommu_domain_alloc(struct bus_type
*bus
)
652 struct iommu_domain
*domain
;
655 if (bus
== NULL
|| bus
->iommu_ops
== NULL
)
658 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
662 domain
->ops
= bus
->iommu_ops
;
664 ret
= domain
->ops
->domain_init(domain
);
675 EXPORT_SYMBOL_GPL(iommu_domain_alloc
);
677 void iommu_domain_free(struct iommu_domain
*domain
)
679 if (likely(domain
->ops
->domain_destroy
!= NULL
))
680 domain
->ops
->domain_destroy(domain
);
684 EXPORT_SYMBOL_GPL(iommu_domain_free
);
686 int iommu_attach_device(struct iommu_domain
*domain
, struct device
*dev
)
689 if (unlikely(domain
->ops
->attach_dev
== NULL
))
692 ret
= domain
->ops
->attach_dev(domain
, dev
);
694 trace_attach_device_to_domain(dev
);
697 EXPORT_SYMBOL_GPL(iommu_attach_device
);
699 void iommu_detach_device(struct iommu_domain
*domain
, struct device
*dev
)
701 if (unlikely(domain
->ops
->detach_dev
== NULL
))
704 domain
->ops
->detach_dev(domain
, dev
);
705 trace_detach_device_from_domain(dev
);
707 EXPORT_SYMBOL_GPL(iommu_detach_device
);
710 * IOMMU groups are really the natrual working unit of the IOMMU, but
711 * the IOMMU API works on domains and devices. Bridge that gap by
712 * iterating over the devices in a group. Ideally we'd have a single
713 * device which represents the requestor ID of the group, but we also
714 * allow IOMMU drivers to create policy defined minimum sets, where
715 * the physical hardware may be able to distiguish members, but we
716 * wish to group them at a higher level (ex. untrusted multi-function
717 * PCI devices). Thus we attach each device.
719 static int iommu_group_do_attach_device(struct device
*dev
, void *data
)
721 struct iommu_domain
*domain
= data
;
723 return iommu_attach_device(domain
, dev
);
726 int iommu_attach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
728 return iommu_group_for_each_dev(group
, domain
,
729 iommu_group_do_attach_device
);
731 EXPORT_SYMBOL_GPL(iommu_attach_group
);
733 static int iommu_group_do_detach_device(struct device
*dev
, void *data
)
735 struct iommu_domain
*domain
= data
;
737 iommu_detach_device(domain
, dev
);
742 void iommu_detach_group(struct iommu_domain
*domain
, struct iommu_group
*group
)
744 iommu_group_for_each_dev(group
, domain
, iommu_group_do_detach_device
);
746 EXPORT_SYMBOL_GPL(iommu_detach_group
);
748 phys_addr_t
iommu_iova_to_phys(struct iommu_domain
*domain
, dma_addr_t iova
)
750 if (unlikely(domain
->ops
->iova_to_phys
== NULL
))
753 return domain
->ops
->iova_to_phys(domain
, iova
);
755 EXPORT_SYMBOL_GPL(iommu_iova_to_phys
);
757 int iommu_domain_has_cap(struct iommu_domain
*domain
,
760 if (unlikely(domain
->ops
->domain_has_cap
== NULL
))
763 return domain
->ops
->domain_has_cap(domain
, cap
);
765 EXPORT_SYMBOL_GPL(iommu_domain_has_cap
);
767 static size_t iommu_pgsize(struct iommu_domain
*domain
,
768 unsigned long addr_merge
, size_t size
)
770 unsigned int pgsize_idx
;
773 /* Max page size that still fits into 'size' */
774 pgsize_idx
= __fls(size
);
776 /* need to consider alignment requirements ? */
777 if (likely(addr_merge
)) {
778 /* Max page size allowed by address */
779 unsigned int align_pgsize_idx
= __ffs(addr_merge
);
780 pgsize_idx
= min(pgsize_idx
, align_pgsize_idx
);
783 /* build a mask of acceptable page sizes */
784 pgsize
= (1UL << (pgsize_idx
+ 1)) - 1;
786 /* throw away page sizes not supported by the hardware */
787 pgsize
&= domain
->ops
->pgsize_bitmap
;
789 /* make sure we're still sane */
792 /* pick the biggest page */
793 pgsize_idx
= __fls(pgsize
);
794 pgsize
= 1UL << pgsize_idx
;
799 int iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
800 phys_addr_t paddr
, size_t size
, int prot
)
802 unsigned long orig_iova
= iova
;
803 unsigned int min_pagesz
;
804 size_t orig_size
= size
;
807 if (unlikely(domain
->ops
->unmap
== NULL
||
808 domain
->ops
->pgsize_bitmap
== 0UL))
811 /* find out the minimum page size supported */
812 min_pagesz
= 1 << __ffs(domain
->ops
->pgsize_bitmap
);
815 * both the virtual address and the physical one, as well as
816 * the size of the mapping, must be aligned (at least) to the
817 * size of the smallest page supported by the hardware
819 if (!IS_ALIGNED(iova
| paddr
| size
, min_pagesz
)) {
820 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
821 iova
, &paddr
, size
, min_pagesz
);
825 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova
, &paddr
, size
);
828 size_t pgsize
= iommu_pgsize(domain
, iova
| paddr
, size
);
830 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
831 iova
, &paddr
, pgsize
);
833 ret
= domain
->ops
->map(domain
, iova
, paddr
, pgsize
, prot
);
842 /* unroll mapping in case something went wrong */
844 iommu_unmap(domain
, orig_iova
, orig_size
- size
);
846 trace_map(iova
, paddr
, size
);
850 EXPORT_SYMBOL_GPL(iommu_map
);
852 size_t iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
, size_t size
)
854 size_t unmapped_page
, unmapped
= 0;
855 unsigned int min_pagesz
;
857 if (unlikely(domain
->ops
->unmap
== NULL
||
858 domain
->ops
->pgsize_bitmap
== 0UL))
861 /* find out the minimum page size supported */
862 min_pagesz
= 1 << __ffs(domain
->ops
->pgsize_bitmap
);
865 * The virtual address, as well as the size of the mapping, must be
866 * aligned (at least) to the size of the smallest page supported
869 if (!IS_ALIGNED(iova
| size
, min_pagesz
)) {
870 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
871 iova
, size
, min_pagesz
);
875 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova
, size
);
878 * Keep iterating until we either unmap 'size' bytes (or more)
879 * or we hit an area that isn't mapped.
881 while (unmapped
< size
) {
882 size_t pgsize
= iommu_pgsize(domain
, iova
, size
- unmapped
);
884 unmapped_page
= domain
->ops
->unmap(domain
, iova
, pgsize
);
888 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
889 iova
, unmapped_page
);
891 iova
+= unmapped_page
;
892 unmapped
+= unmapped_page
;
895 trace_unmap(iova
, 0, size
);
898 EXPORT_SYMBOL_GPL(iommu_unmap
);
901 int iommu_domain_window_enable(struct iommu_domain
*domain
, u32 wnd_nr
,
902 phys_addr_t paddr
, u64 size
, int prot
)
904 if (unlikely(domain
->ops
->domain_window_enable
== NULL
))
907 return domain
->ops
->domain_window_enable(domain
, wnd_nr
, paddr
, size
,
910 EXPORT_SYMBOL_GPL(iommu_domain_window_enable
);
912 void iommu_domain_window_disable(struct iommu_domain
*domain
, u32 wnd_nr
)
914 if (unlikely(domain
->ops
->domain_window_disable
== NULL
))
917 return domain
->ops
->domain_window_disable(domain
, wnd_nr
);
919 EXPORT_SYMBOL_GPL(iommu_domain_window_disable
);
921 static int __init
iommu_init(void)
923 iommu_group_kset
= kset_create_and_add("iommu_groups",
925 ida_init(&iommu_group_ida
);
926 mutex_init(&iommu_group_mutex
);
928 BUG_ON(!iommu_group_kset
);
932 arch_initcall(iommu_init
);
934 int iommu_domain_get_attr(struct iommu_domain
*domain
,
935 enum iommu_attr attr
, void *data
)
937 struct iommu_domain_geometry
*geometry
;
943 case DOMAIN_ATTR_GEOMETRY
:
945 *geometry
= domain
->geometry
;
948 case DOMAIN_ATTR_PAGING
:
950 *paging
= (domain
->ops
->pgsize_bitmap
!= 0UL);
952 case DOMAIN_ATTR_WINDOWS
:
955 if (domain
->ops
->domain_get_windows
!= NULL
)
956 *count
= domain
->ops
->domain_get_windows(domain
);
962 if (!domain
->ops
->domain_get_attr
)
965 ret
= domain
->ops
->domain_get_attr(domain
, attr
, data
);
970 EXPORT_SYMBOL_GPL(iommu_domain_get_attr
);
972 int iommu_domain_set_attr(struct iommu_domain
*domain
,
973 enum iommu_attr attr
, void *data
)
979 case DOMAIN_ATTR_WINDOWS
:
982 if (domain
->ops
->domain_set_windows
!= NULL
)
983 ret
= domain
->ops
->domain_set_windows(domain
, *count
);
989 if (domain
->ops
->domain_set_attr
== NULL
)
992 ret
= domain
->ops
->domain_set_attr(domain
, attr
, data
);
997 EXPORT_SYMBOL_GPL(iommu_domain_set_attr
);