145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ab493a0fSOhad Ben-Cohen /*
3ab493a0fSOhad Ben-Cohen * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
463ce3ae8SJoerg Roedel * Author: Joerg Roedel <jroedel@suse.de>
5ab493a0fSOhad Ben-Cohen */
6ab493a0fSOhad Ben-Cohen
792e7066fSJoerg Roedel #define pr_fmt(fmt) "iommu: " fmt
87d3002ccSOhad Ben-Cohen
9c13dbc1eSRobin Murphy #include <linux/amba/bus.h>
10905d66c1SJoerg Roedel #include <linux/device.h>
1140998188SOhad Ben-Cohen #include <linux/kernel.h>
12e7d6fff6SWill Deacon #include <linux/bits.h>
13ab493a0fSOhad Ben-Cohen #include <linux/bug.h>
14ab493a0fSOhad Ben-Cohen #include <linux/types.h>
15c1af7b40SPaul Gortmaker #include <linux/init.h>
16c1af7b40SPaul Gortmaker #include <linux/export.h>
17ab493a0fSOhad Ben-Cohen #include <linux/slab.h>
18ab493a0fSOhad Ben-Cohen #include <linux/errno.h>
19c13dbc1eSRobin Murphy #include <linux/host1x_context_bus.h>
20ab493a0fSOhad Ben-Cohen #include <linux/iommu.h>
21d72e31c9SAlex Williamson #include <linux/idr.h>
22d72e31c9SAlex Williamson #include <linux/err.h>
23104a1c13SAlex Williamson #include <linux/pci.h>
2422d2c7afSLu Baolu #include <linux/pci-ats.h>
25f096c061SAlex Williamson #include <linux/bitops.h>
26c13dbc1eSRobin Murphy #include <linux/platform_device.h>
2757f98d2fSRobin Murphy #include <linux/property.h>
28eab03e2aSNipun Gupta #include <linux/fsl/mc.h>
2925f003deSWill Deacon #include <linux/module.h>
30e9d1d2bbSTom Lendacky #include <linux/cc_platform.h>
313f47d3e4SNipun Gupta #include <linux/cdx/cdx_bus.h>
327f6db171SShuah Khan #include <trace/events/iommu.h>
3313646796SLu Baolu #include <linux/sched/mm.h>
34efc30a8fSJason Gunthorpe #include <linux/msi.h>
35ab493a0fSOhad Ben-Cohen
36f2042ed2SRobin Murphy #include "dma-iommu.h"
37addb6659SNicolin Chen #include "iommu-priv.h"
38f2042ed2SRobin Murphy
39757636edSLu Baolu #include "iommu-sva.h"
4023a1b46fSJason Gunthorpe #include "iommu-priv.h"
418cc93159SLu Baolu
42d72e31c9SAlex Williamson static struct kset *iommu_group_kset;
43e38d1f13SHeiner Kallweit static DEFINE_IDA(iommu_group_ida);
442dcebc7dSJacob Pan static DEFINE_IDA(iommu_global_pasid_ida);
4522bb182cSJoerg Roedel
4622bb182cSJoerg Roedel static unsigned int iommu_def_domain_type __read_mostly;
47e96763ecSRobin Murphy static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
48faf14989SJoerg Roedel static u32 iommu_cmd_line __read_mostly;
49d72e31c9SAlex Williamson
50d72e31c9SAlex Williamson struct iommu_group {
51d72e31c9SAlex Williamson struct kobject kobj;
52d72e31c9SAlex Williamson struct kobject *devices_kobj;
53d72e31c9SAlex Williamson struct list_head devices;
5416603704SLu Baolu struct xarray pasid_array;
55d72e31c9SAlex Williamson struct mutex mutex;
56d72e31c9SAlex Williamson void *iommu_data;
57d72e31c9SAlex Williamson void (*iommu_data_release)(void *iommu_data);
58d72e31c9SAlex Williamson char *name;
59d72e31c9SAlex Williamson int id;
6053723dc5SJoerg Roedel struct iommu_domain *default_domain;
610286300eSJason Gunthorpe via iommu struct iommu_domain *blocking_domain;
62e39cb8a3SJoerg Roedel struct iommu_domain *domain;
6341df6dccSJoerg Roedel struct list_head entry;
641ea2a07aSLu Baolu unsigned int owner_cnt;
651ea2a07aSLu Baolu void *owner;
66d72e31c9SAlex Williamson };
67d72e31c9SAlex Williamson
68c09e22d5SJoerg Roedel struct group_device {
69d72e31c9SAlex Williamson struct list_head list;
70d72e31c9SAlex Williamson struct device *dev;
71d72e31c9SAlex Williamson char *name;
72d72e31c9SAlex Williamson };
73d72e31c9SAlex Williamson
743006b15bSJason Gunthorpe /* Iterate over each struct group_device in a struct iommu_group */
753006b15bSJason Gunthorpe #define for_each_group_device(group, pos) \
763006b15bSJason Gunthorpe list_for_each_entry(pos, &(group)->devices, list)
773006b15bSJason Gunthorpe
78d72e31c9SAlex Williamson struct iommu_group_attribute {
79d72e31c9SAlex Williamson struct attribute attr;
80d72e31c9SAlex Williamson ssize_t (*show)(struct iommu_group *group, char *buf);
81d72e31c9SAlex Williamson ssize_t (*store)(struct iommu_group *group,
82d72e31c9SAlex Williamson const char *buf, size_t count);
83d72e31c9SAlex Williamson };
84d72e31c9SAlex Williamson
85bc7d12b9SEric Auger static const char * const iommu_group_resv_type_string[] = {
86bc7d12b9SEric Auger [IOMMU_RESV_DIRECT] = "direct",
87adfd3738SEric Auger [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
88bc7d12b9SEric Auger [IOMMU_RESV_RESERVED] = "reserved",
89bc7d12b9SEric Auger [IOMMU_RESV_MSI] = "msi",
909d3a4de4SRobin Murphy [IOMMU_RESV_SW_MSI] = "msi",
91bc7d12b9SEric Auger };
92bc7d12b9SEric Auger
93faf14989SJoerg Roedel #define IOMMU_CMD_LINE_DMA_API BIT(0)
94a250c23fSRobin Murphy #define IOMMU_CMD_LINE_STRICT BIT(1)
95faf14989SJoerg Roedel
96c13dbc1eSRobin Murphy static int iommu_bus_notifier(struct notifier_block *nb,
97c13dbc1eSRobin Murphy unsigned long action, void *data);
98e223864fSJason Gunthorpe static void iommu_release_device(struct device *dev);
99b18d0a0fSGreg Kroah-Hartman static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
1006e1aa204SJoerg Roedel unsigned type);
1016e1aa204SJoerg Roedel static int __iommu_attach_device(struct iommu_domain *domain,
1026e1aa204SJoerg Roedel struct device *dev);
1036e1aa204SJoerg Roedel static int __iommu_attach_group(struct iommu_domain *domain,
1046e1aa204SJoerg Roedel struct iommu_group *group);
105dcf40ed3SJason Gunthorpe
106dcf40ed3SJason Gunthorpe enum {
107dcf40ed3SJason Gunthorpe IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
108dcf40ed3SJason Gunthorpe };
109dcf40ed3SJason Gunthorpe
1102f74198aSJason Gunthorpe static int __iommu_device_set_domain(struct iommu_group *group,
1112f74198aSJason Gunthorpe struct device *dev,
1122f74198aSJason Gunthorpe struct iommu_domain *new_domain,
1132f74198aSJason Gunthorpe unsigned int flags);
114dcf40ed3SJason Gunthorpe static int __iommu_group_set_domain_internal(struct iommu_group *group,
115dcf40ed3SJason Gunthorpe struct iommu_domain *new_domain,
116dcf40ed3SJason Gunthorpe unsigned int flags);
__iommu_group_set_domain(struct iommu_group * group,struct iommu_domain * new_domain)1170286300eSJason Gunthorpe via iommu static int __iommu_group_set_domain(struct iommu_group *group,
118dcf40ed3SJason Gunthorpe struct iommu_domain *new_domain)
119dcf40ed3SJason Gunthorpe {
120dcf40ed3SJason Gunthorpe return __iommu_group_set_domain_internal(group, new_domain, 0);
121dcf40ed3SJason Gunthorpe }
__iommu_group_set_domain_nofail(struct iommu_group * group,struct iommu_domain * new_domain)122dcf40ed3SJason Gunthorpe static void __iommu_group_set_domain_nofail(struct iommu_group *group,
123dcf40ed3SJason Gunthorpe struct iommu_domain *new_domain)
124dcf40ed3SJason Gunthorpe {
125dcf40ed3SJason Gunthorpe WARN_ON(__iommu_group_set_domain_internal(
126dcf40ed3SJason Gunthorpe group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
127dcf40ed3SJason Gunthorpe }
128dcf40ed3SJason Gunthorpe
129d99be00fSJason Gunthorpe static int iommu_setup_default_domain(struct iommu_group *group,
130d99be00fSJason Gunthorpe int target_type);
131d99be00fSJason Gunthorpe static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
132ce574c27SJoerg Roedel struct device *dev);
13308a27c1cSSai Praneeth Prakhya static ssize_t iommu_group_store_type(struct iommu_group *group,
13408a27c1cSSai Praneeth Prakhya const char *buf, size_t count);
135fa082803SJason Gunthorpe static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
136fa082803SJason Gunthorpe struct device *dev);
137f1880563SJason Gunthorpe static void __iommu_group_free_device(struct iommu_group *group,
138f1880563SJason Gunthorpe struct group_device *grp_dev);
1396e1aa204SJoerg Roedel
140d72e31c9SAlex Williamson #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
141d72e31c9SAlex Williamson struct iommu_group_attribute iommu_group_attr_##_name = \
142d72e31c9SAlex Williamson __ATTR(_name, _mode, _show, _store)
143d72e31c9SAlex Williamson
144d72e31c9SAlex Williamson #define to_iommu_group_attr(_attr) \
145d72e31c9SAlex Williamson container_of(_attr, struct iommu_group_attribute, attr)
146d72e31c9SAlex Williamson #define to_iommu_group(_kobj) \
147d72e31c9SAlex Williamson container_of(_kobj, struct iommu_group, kobj)
148d72e31c9SAlex Williamson
149b0119e87SJoerg Roedel static LIST_HEAD(iommu_device_list);
150b0119e87SJoerg Roedel static DEFINE_SPINLOCK(iommu_device_lock);
151b0119e87SJoerg Roedel
152c13dbc1eSRobin Murphy static struct bus_type * const iommu_buses[] = {
153c13dbc1eSRobin Murphy &platform_bus_type,
154c13dbc1eSRobin Murphy #ifdef CONFIG_PCI
155c13dbc1eSRobin Murphy &pci_bus_type,
156c13dbc1eSRobin Murphy #endif
157c13dbc1eSRobin Murphy #ifdef CONFIG_ARM_AMBA
158c13dbc1eSRobin Murphy &amba_bustype,
159c13dbc1eSRobin Murphy #endif
160c13dbc1eSRobin Murphy #ifdef CONFIG_FSL_MC_BUS
161c13dbc1eSRobin Murphy &fsl_mc_bus_type,
162c13dbc1eSRobin Murphy #endif
163c13dbc1eSRobin Murphy #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
164c13dbc1eSRobin Murphy &host1x_context_device_bus_type,
165c13dbc1eSRobin Murphy #endif
1663f47d3e4SNipun Gupta #ifdef CONFIG_CDX_BUS
1673f47d3e4SNipun Gupta &cdx_bus_type,
1683f47d3e4SNipun Gupta #endif
169c13dbc1eSRobin Murphy };
170c13dbc1eSRobin Murphy
1715fa9e7c5SJoerg Roedel /*
1725fa9e7c5SJoerg Roedel * Use a function instead of an array here because the domain-type is a
1735fa9e7c5SJoerg Roedel * bit-field, so an array would waste memory.
1745fa9e7c5SJoerg Roedel */
iommu_domain_type_str(unsigned int t)1755fa9e7c5SJoerg Roedel static const char *iommu_domain_type_str(unsigned int t)
1765fa9e7c5SJoerg Roedel {
1775fa9e7c5SJoerg Roedel switch (t) {
1785fa9e7c5SJoerg Roedel case IOMMU_DOMAIN_BLOCKED:
1795fa9e7c5SJoerg Roedel return "Blocked";
1805fa9e7c5SJoerg Roedel case IOMMU_DOMAIN_IDENTITY:
1815fa9e7c5SJoerg Roedel return "Passthrough";
1825fa9e7c5SJoerg Roedel case IOMMU_DOMAIN_UNMANAGED:
1835fa9e7c5SJoerg Roedel return "Unmanaged";
1845fa9e7c5SJoerg Roedel case IOMMU_DOMAIN_DMA:
185bf3aed46SRobin Murphy case IOMMU_DOMAIN_DMA_FQ:
1865fa9e7c5SJoerg Roedel return "Translated";
1875fa9e7c5SJoerg Roedel default:
1885fa9e7c5SJoerg Roedel return "Unknown";
1895fa9e7c5SJoerg Roedel }
1905fa9e7c5SJoerg Roedel }
1915fa9e7c5SJoerg Roedel
iommu_subsys_init(void)1925fa9e7c5SJoerg Roedel static int __init iommu_subsys_init(void)
1935fa9e7c5SJoerg Roedel {
194c13dbc1eSRobin Murphy struct notifier_block *nb;
195c13dbc1eSRobin Murphy
1963189713aSChristoph Hellwig if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
19722bb182cSJoerg Roedel if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
19822bb182cSJoerg Roedel iommu_set_default_passthrough(false);
19922bb182cSJoerg Roedel else
20022bb182cSJoerg Roedel iommu_set_default_translated(false);
2012cc13bb4SJoerg Roedel
202e9d1d2bbSTom Lendacky if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
2032896ba40SJoerg Roedel pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
2042cc13bb4SJoerg Roedel iommu_set_default_translated(false);
2052cc13bb4SJoerg Roedel }
20622bb182cSJoerg Roedel }
20722bb182cSJoerg Roedel
208c208916fSRobin Murphy if (!iommu_default_passthrough() && !iommu_dma_strict)
209c208916fSRobin Murphy iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
210c208916fSRobin Murphy
21122bb182cSJoerg Roedel pr_info("Default domain type: %s%s\n",
21222bb182cSJoerg Roedel iommu_domain_type_str(iommu_def_domain_type),
2133189713aSChristoph Hellwig (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
2143189713aSChristoph Hellwig " (set via kernel command line)" : "");
2155fa9e7c5SJoerg Roedel
2167cf8a638SRobin Murphy if (!iommu_default_passthrough())
217d8577d2eSJohn Garry pr_info("DMA domain TLB invalidation policy: %s mode%s\n",
218d8577d2eSJohn Garry iommu_dma_strict ? "strict" : "lazy",
219d8577d2eSJohn Garry (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
220d8577d2eSJohn Garry " (set via kernel command line)" : "");
221d8577d2eSJohn Garry
222c13dbc1eSRobin Murphy nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL);
223c13dbc1eSRobin Murphy if (!nb)
224c13dbc1eSRobin Murphy return -ENOMEM;
225c13dbc1eSRobin Murphy
226c13dbc1eSRobin Murphy for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
227c13dbc1eSRobin Murphy nb[i].notifier_call = iommu_bus_notifier;
228c13dbc1eSRobin Murphy bus_register_notifier(iommu_buses[i], &nb[i]);
229c13dbc1eSRobin Murphy }
230c13dbc1eSRobin Murphy
2315fa9e7c5SJoerg Roedel return 0;
2325fa9e7c5SJoerg Roedel }
2335fa9e7c5SJoerg Roedel subsys_initcall(iommu_subsys_init);
2345fa9e7c5SJoerg Roedel
remove_iommu_group(struct device * dev,void * data)23557365a04SRobin Murphy static int remove_iommu_group(struct device *dev, void *data)
23657365a04SRobin Murphy {
23757365a04SRobin Murphy if (dev->iommu && dev->iommu->iommu_dev == data)
23857365a04SRobin Murphy iommu_release_device(dev);
23957365a04SRobin Murphy
24057365a04SRobin Murphy return 0;
24157365a04SRobin Murphy }
24257365a04SRobin Murphy
2432d471b20SRobin Murphy /**
2442d471b20SRobin Murphy * iommu_device_register() - Register an IOMMU hardware instance
2452d471b20SRobin Murphy * @iommu: IOMMU handle for the instance
2462d471b20SRobin Murphy * @ops: IOMMU ops to associate with the instance
2472d471b20SRobin Murphy * @hwdev: (optional) actual instance device, used for fwnode lookup
2482d471b20SRobin Murphy *
2492d471b20SRobin Murphy * Return: 0 on success, or an error.
2502d471b20SRobin Murphy */
iommu_device_register(struct iommu_device * iommu,const struct iommu_ops * ops,struct device * hwdev)2512d471b20SRobin Murphy int iommu_device_register(struct iommu_device *iommu,
2522d471b20SRobin Murphy const struct iommu_ops *ops, struct device *hwdev)
253b0119e87SJoerg Roedel {
25457365a04SRobin Murphy int err = 0;
25557365a04SRobin Murphy
2562d471b20SRobin Murphy /* We need to be able to take module references appropriately */
2572d471b20SRobin Murphy if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
2582d471b20SRobin Murphy return -EINVAL;
25957365a04SRobin Murphy /*
26057365a04SRobin Murphy * Temporarily enforce global restriction to a single driver. This was
26157365a04SRobin Murphy * already the de-facto behaviour, since any possible combination of
26257365a04SRobin Murphy * existing drivers would compete for at least the PCI or platform bus.
26357365a04SRobin Murphy */
26457365a04SRobin Murphy if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops)
26557365a04SRobin Murphy return -EBUSY;
2662d471b20SRobin Murphy
2672d471b20SRobin Murphy iommu->ops = ops;
2682d471b20SRobin Murphy if (hwdev)
269927d8f27SAndy Shevchenko iommu->fwnode = dev_fwnode(hwdev);
2702d471b20SRobin Murphy
271b0119e87SJoerg Roedel spin_lock(&iommu_device_lock);
272b0119e87SJoerg Roedel list_add_tail(&iommu->list, &iommu_device_list);
273b0119e87SJoerg Roedel spin_unlock(&iommu_device_lock);
27457365a04SRobin Murphy
27557365a04SRobin Murphy for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) {
27657365a04SRobin Murphy iommu_buses[i]->iommu_ops = ops;
27757365a04SRobin Murphy err = bus_iommu_probe(iommu_buses[i]);
27857365a04SRobin Murphy }
27957365a04SRobin Murphy if (err)
28057365a04SRobin Murphy iommu_device_unregister(iommu);
28157365a04SRobin Murphy return err;
282b0119e87SJoerg Roedel }
283a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(iommu_device_register);
284b0119e87SJoerg Roedel
iommu_device_unregister(struct iommu_device * iommu)285b0119e87SJoerg Roedel void iommu_device_unregister(struct iommu_device *iommu)
286b0119e87SJoerg Roedel {
28757365a04SRobin Murphy for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
28857365a04SRobin Murphy bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group);
28957365a04SRobin Murphy
290b0119e87SJoerg Roedel spin_lock(&iommu_device_lock);
291b0119e87SJoerg Roedel list_del(&iommu->list);
292b0119e87SJoerg Roedel spin_unlock(&iommu_device_lock);
293b0119e87SJoerg Roedel }
294a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(iommu_device_unregister);
295b0119e87SJoerg Roedel
29623a1b46fSJason Gunthorpe #if IS_ENABLED(CONFIG_IOMMUFD_TEST)
iommu_device_unregister_bus(struct iommu_device * iommu,struct bus_type * bus,struct notifier_block * nb)29723a1b46fSJason Gunthorpe void iommu_device_unregister_bus(struct iommu_device *iommu,
29823a1b46fSJason Gunthorpe struct bus_type *bus,
29923a1b46fSJason Gunthorpe struct notifier_block *nb)
30023a1b46fSJason Gunthorpe {
30123a1b46fSJason Gunthorpe bus_unregister_notifier(bus, nb);
30223a1b46fSJason Gunthorpe iommu_device_unregister(iommu);
30323a1b46fSJason Gunthorpe }
30423a1b46fSJason Gunthorpe EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
30523a1b46fSJason Gunthorpe
30623a1b46fSJason Gunthorpe /*
30723a1b46fSJason Gunthorpe * Register an iommu driver against a single bus. This is only used by iommufd
30823a1b46fSJason Gunthorpe * selftest to create a mock iommu driver. The caller must provide
30923a1b46fSJason Gunthorpe * some memory to hold a notifier_block.
31023a1b46fSJason Gunthorpe */
iommu_device_register_bus(struct iommu_device * iommu,const struct iommu_ops * ops,struct bus_type * bus,struct notifier_block * nb)31123a1b46fSJason Gunthorpe int iommu_device_register_bus(struct iommu_device *iommu,
31223a1b46fSJason Gunthorpe const struct iommu_ops *ops, struct bus_type *bus,
31323a1b46fSJason Gunthorpe struct notifier_block *nb)
31423a1b46fSJason Gunthorpe {
31523a1b46fSJason Gunthorpe int err;
31623a1b46fSJason Gunthorpe
31723a1b46fSJason Gunthorpe iommu->ops = ops;
31823a1b46fSJason Gunthorpe nb->notifier_call = iommu_bus_notifier;
31923a1b46fSJason Gunthorpe err = bus_register_notifier(bus, nb);
32023a1b46fSJason Gunthorpe if (err)
32123a1b46fSJason Gunthorpe return err;
32223a1b46fSJason Gunthorpe
32323a1b46fSJason Gunthorpe spin_lock(&iommu_device_lock);
32423a1b46fSJason Gunthorpe list_add_tail(&iommu->list, &iommu_device_list);
32523a1b46fSJason Gunthorpe spin_unlock(&iommu_device_lock);
32623a1b46fSJason Gunthorpe
32723a1b46fSJason Gunthorpe bus->iommu_ops = ops;
32823a1b46fSJason Gunthorpe err = bus_iommu_probe(bus);
32923a1b46fSJason Gunthorpe if (err) {
33023a1b46fSJason Gunthorpe iommu_device_unregister_bus(iommu, bus, nb);
33123a1b46fSJason Gunthorpe return err;
33223a1b46fSJason Gunthorpe }
33323a1b46fSJason Gunthorpe return 0;
33423a1b46fSJason Gunthorpe }
33523a1b46fSJason Gunthorpe EXPORT_SYMBOL_GPL(iommu_device_register_bus);
33623a1b46fSJason Gunthorpe #endif
33723a1b46fSJason Gunthorpe
dev_iommu_get(struct device * dev)338045a7042SJoerg Roedel static struct dev_iommu *dev_iommu_get(struct device *dev)
3390c830e6bSJacob Pan {
340045a7042SJoerg Roedel struct dev_iommu *param = dev->iommu;
3410c830e6bSJacob Pan
3420c830e6bSJacob Pan if (param)
3430c830e6bSJacob Pan return param;
3440c830e6bSJacob Pan
3450c830e6bSJacob Pan param = kzalloc(sizeof(*param), GFP_KERNEL);
3460c830e6bSJacob Pan if (!param)
3470c830e6bSJacob Pan return NULL;
3480c830e6bSJacob Pan
3490c830e6bSJacob Pan mutex_init(¶m->lock);
350045a7042SJoerg Roedel dev->iommu = param;
3510c830e6bSJacob Pan return param;
3520c830e6bSJacob Pan }
3530c830e6bSJacob Pan
dev_iommu_free(struct device * dev)354045a7042SJoerg Roedel static void dev_iommu_free(struct device *dev)
3550c830e6bSJacob Pan {
356b54240adSVijayanand Jitta struct dev_iommu *param = dev->iommu;
357b54240adSVijayanand Jitta
358045a7042SJoerg Roedel dev->iommu = NULL;
359b54240adSVijayanand Jitta if (param->fwspec) {
360b54240adSVijayanand Jitta fwnode_handle_put(param->fwspec->iommu_fwnode);
361b54240adSVijayanand Jitta kfree(param->fwspec);
362b54240adSVijayanand Jitta }
363b54240adSVijayanand Jitta kfree(param);
3640c830e6bSJacob Pan }
3650c830e6bSJacob Pan
dev_iommu_get_max_pasids(struct device * dev)36622d2c7afSLu Baolu static u32 dev_iommu_get_max_pasids(struct device *dev)
36722d2c7afSLu Baolu {
36822d2c7afSLu Baolu u32 max_pasids = 0, bits = 0;
36922d2c7afSLu Baolu int ret;
37022d2c7afSLu Baolu
37122d2c7afSLu Baolu if (dev_is_pci(dev)) {
37222d2c7afSLu Baolu ret = pci_max_pasids(to_pci_dev(dev));
37322d2c7afSLu Baolu if (ret > 0)
37422d2c7afSLu Baolu max_pasids = ret;
37522d2c7afSLu Baolu } else {
37622d2c7afSLu Baolu ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
37722d2c7afSLu Baolu if (!ret)
37822d2c7afSLu Baolu max_pasids = 1UL << bits;
37922d2c7afSLu Baolu }
38022d2c7afSLu Baolu
38122d2c7afSLu Baolu return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
38222d2c7afSLu Baolu }
38322d2c7afSLu Baolu
384aa095857SJason Gunthorpe /*
385aa095857SJason Gunthorpe * Init the dev->iommu and dev->iommu_group in the struct device and get the
386aa095857SJason Gunthorpe * driver probed
387aa095857SJason Gunthorpe */
iommu_init_device(struct device * dev,const struct iommu_ops * ops)388aa095857SJason Gunthorpe static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
389aa095857SJason Gunthorpe {
390aa095857SJason Gunthorpe struct iommu_device *iommu_dev;
391aa095857SJason Gunthorpe struct iommu_group *group;
392aa095857SJason Gunthorpe int ret;
393aa095857SJason Gunthorpe
394aa095857SJason Gunthorpe if (!dev_iommu_get(dev))
395aa095857SJason Gunthorpe return -ENOMEM;
396aa095857SJason Gunthorpe
397aa095857SJason Gunthorpe if (!try_module_get(ops->owner)) {
398aa095857SJason Gunthorpe ret = -EINVAL;
399aa095857SJason Gunthorpe goto err_free;
400aa095857SJason Gunthorpe }
401aa095857SJason Gunthorpe
402aa095857SJason Gunthorpe iommu_dev = ops->probe_device(dev);
403aa095857SJason Gunthorpe if (IS_ERR(iommu_dev)) {
404aa095857SJason Gunthorpe ret = PTR_ERR(iommu_dev);
405aa095857SJason Gunthorpe goto err_module_put;
406aa095857SJason Gunthorpe }
407aa095857SJason Gunthorpe
40814891af3SJason Gunthorpe ret = iommu_device_link(iommu_dev, dev);
40914891af3SJason Gunthorpe if (ret)
41014891af3SJason Gunthorpe goto err_release;
41114891af3SJason Gunthorpe
412aa095857SJason Gunthorpe group = ops->device_group(dev);
413aa095857SJason Gunthorpe if (WARN_ON_ONCE(group == NULL))
414aa095857SJason Gunthorpe group = ERR_PTR(-EINVAL);
415aa095857SJason Gunthorpe if (IS_ERR(group)) {
416aa095857SJason Gunthorpe ret = PTR_ERR(group);
41714891af3SJason Gunthorpe goto err_unlink;
418aa095857SJason Gunthorpe }
419aa095857SJason Gunthorpe dev->iommu_group = group;
420aa095857SJason Gunthorpe
421aa095857SJason Gunthorpe dev->iommu->iommu_dev = iommu_dev;
422aa095857SJason Gunthorpe dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
423aa095857SJason Gunthorpe if (ops->is_attach_deferred)
424aa095857SJason Gunthorpe dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
425aa095857SJason Gunthorpe return 0;
426aa095857SJason Gunthorpe
42714891af3SJason Gunthorpe err_unlink:
42814891af3SJason Gunthorpe iommu_device_unlink(iommu_dev, dev);
429aa095857SJason Gunthorpe err_release:
430aa095857SJason Gunthorpe if (ops->release_device)
431aa095857SJason Gunthorpe ops->release_device(dev);
432aa095857SJason Gunthorpe err_module_put:
433aa095857SJason Gunthorpe module_put(ops->owner);
434aa095857SJason Gunthorpe err_free:
435aa095857SJason Gunthorpe dev_iommu_free(dev);
436aa095857SJason Gunthorpe return ret;
437aa095857SJason Gunthorpe }
438aa095857SJason Gunthorpe
iommu_deinit_device(struct device * dev)439aa095857SJason Gunthorpe static void iommu_deinit_device(struct device *dev)
440aa095857SJason Gunthorpe {
441aa095857SJason Gunthorpe struct iommu_group *group = dev->iommu_group;
442aa095857SJason Gunthorpe const struct iommu_ops *ops = dev_iommu_ops(dev);
443aa095857SJason Gunthorpe
444aa095857SJason Gunthorpe lockdep_assert_held(&group->mutex);
445aa095857SJason Gunthorpe
44614891af3SJason Gunthorpe iommu_device_unlink(dev->iommu->iommu_dev, dev);
44714891af3SJason Gunthorpe
448aa095857SJason Gunthorpe /*
449aa095857SJason Gunthorpe * release_device() must stop using any attached domain on the device.
450aa095857SJason Gunthorpe * If there are still other devices in the group they are not effected
451aa095857SJason Gunthorpe * by this callback.
452aa095857SJason Gunthorpe *
453aa095857SJason Gunthorpe * The IOMMU driver must set the device to either an identity or
454aa095857SJason Gunthorpe * blocking translation and stop using any domain pointer, as it is
455aa095857SJason Gunthorpe * going to be freed.
456aa095857SJason Gunthorpe */
457aa095857SJason Gunthorpe if (ops->release_device)
458aa095857SJason Gunthorpe ops->release_device(dev);
459aa095857SJason Gunthorpe
460aa095857SJason Gunthorpe /*
461aa095857SJason Gunthorpe * If this is the last driver to use the group then we must free the
462aa095857SJason Gunthorpe * domains before we do the module_put().
463aa095857SJason Gunthorpe */
464aa095857SJason Gunthorpe if (list_empty(&group->devices)) {
465aa095857SJason Gunthorpe if (group->default_domain) {
466aa095857SJason Gunthorpe iommu_domain_free(group->default_domain);
467aa095857SJason Gunthorpe group->default_domain = NULL;
468aa095857SJason Gunthorpe }
469aa095857SJason Gunthorpe if (group->blocking_domain) {
470aa095857SJason Gunthorpe iommu_domain_free(group->blocking_domain);
471aa095857SJason Gunthorpe group->blocking_domain = NULL;
472aa095857SJason Gunthorpe }
473aa095857SJason Gunthorpe group->domain = NULL;
474aa095857SJason Gunthorpe }
475aa095857SJason Gunthorpe
476aa095857SJason Gunthorpe /* Caller must put iommu_group */
477aa095857SJason Gunthorpe dev->iommu_group = NULL;
478aa095857SJason Gunthorpe module_put(ops->owner);
479aa095857SJason Gunthorpe dev_iommu_free(dev);
480aa095857SJason Gunthorpe }
481aa095857SJason Gunthorpe
482c1114090SRobin Murphy DEFINE_MUTEX(iommu_probe_device_lock);
483c1114090SRobin Murphy
__iommu_probe_device(struct device * dev,struct list_head * group_list)48441df6dccSJoerg Roedel static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
485cc5aed44SJoerg Roedel {
486cc5aed44SJoerg Roedel const struct iommu_ops *ops = dev->bus->iommu_ops;
487a6a4c7e2SJoerg Roedel struct iommu_group *group;
488fa082803SJason Gunthorpe struct group_device *gdev;
4890c830e6bSJacob Pan int ret;
490cc5aed44SJoerg Roedel
4910c830e6bSJacob Pan if (!ops)
492f38338cfSThierry Reding return -ENODEV;
49301657bc1SRobin Murphy /*
49401657bc1SRobin Murphy * Serialise to avoid races between IOMMU drivers registering in
49501657bc1SRobin Murphy * parallel and/or the "replay" calls from ACPI/OF code via client
49601657bc1SRobin Murphy * driver probe. Once the latter have been cleaned up we should
49701657bc1SRobin Murphy * probably be able to use device_lock() here to minimise the scope,
49801657bc1SRobin Murphy * but for now enforcing a simple global ordering is fine.
49901657bc1SRobin Murphy */
500c1114090SRobin Murphy lockdep_assert_held(&iommu_probe_device_lock);
5016eb4da8cSJason Gunthorpe
5026eb4da8cSJason Gunthorpe /* Device is probed already if in a group */
503c1114090SRobin Murphy if (dev->iommu_group)
504c1114090SRobin Murphy return 0;
5050c830e6bSJacob Pan
506aa095857SJason Gunthorpe ret = iommu_init_device(dev, ops);
507aa095857SJason Gunthorpe if (ret)
508c1114090SRobin Murphy return ret;
50925f003deSWill Deacon
510aa095857SJason Gunthorpe group = dev->iommu_group;
511fa082803SJason Gunthorpe gdev = iommu_group_alloc_device(group, dev);
51201657bc1SRobin Murphy mutex_lock(&group->mutex);
513fa082803SJason Gunthorpe if (IS_ERR(gdev)) {
514fa082803SJason Gunthorpe ret = PTR_ERR(gdev);
5157bdb9962SJason Gunthorpe goto err_put_group;
516fa082803SJason Gunthorpe }
51741df6dccSJoerg Roedel
518f1880563SJason Gunthorpe /*
519f1880563SJason Gunthorpe * The gdev must be in the list before calling
520f1880563SJason Gunthorpe * iommu_setup_default_domain()
521f1880563SJason Gunthorpe */
522fa082803SJason Gunthorpe list_add_tail(&gdev->list, &group->devices);
523f1880563SJason Gunthorpe WARN_ON(group->default_domain && !group->domain);
524f1880563SJason Gunthorpe if (group->default_domain)
525f1880563SJason Gunthorpe iommu_create_device_direct_mappings(group->default_domain, dev);
526f1880563SJason Gunthorpe if (group->domain) {
527f1880563SJason Gunthorpe ret = __iommu_device_set_domain(group, dev, group->domain, 0);
528f1880563SJason Gunthorpe if (ret)
529f1880563SJason Gunthorpe goto err_remove_gdev;
530f1880563SJason Gunthorpe } else if (!group->default_domain && !group_list) {
531f1880563SJason Gunthorpe ret = iommu_setup_default_domain(group, 0);
532f1880563SJason Gunthorpe if (ret)
533f1880563SJason Gunthorpe goto err_remove_gdev;
534f1880563SJason Gunthorpe } else if (!group->default_domain) {
535f1880563SJason Gunthorpe /*
536f1880563SJason Gunthorpe * With a group_list argument we defer the default_domain setup
537f1880563SJason Gunthorpe * to the caller by providing a de-duplicated list of groups
538f1880563SJason Gunthorpe * that need further setup.
539f1880563SJason Gunthorpe */
540f1880563SJason Gunthorpe if (list_empty(&group->entry))
541a6a4c7e2SJoerg Roedel list_add_tail(&group->entry, group_list);
542f1880563SJason Gunthorpe }
543a6a4c7e2SJoerg Roedel mutex_unlock(&group->mutex);
544dc9de8a2SJoerg Roedel
545791c2b17SRobin Murphy if (dev_is_pci(dev))
546791c2b17SRobin Murphy iommu_dma_set_pci_32bit_workaround(dev);
547dc9de8a2SJoerg Roedel
54825f003deSWill Deacon return 0;
54925f003deSWill Deacon
550f1880563SJason Gunthorpe err_remove_gdev:
551f1880563SJason Gunthorpe list_del(&gdev->list);
552f1880563SJason Gunthorpe __iommu_group_free_device(group, gdev);
5537bdb9962SJason Gunthorpe err_put_group:
554aa095857SJason Gunthorpe iommu_deinit_device(dev);
555aa095857SJason Gunthorpe mutex_unlock(&group->mutex);
5567bdb9962SJason Gunthorpe iommu_group_put(group);
55701657bc1SRobin Murphy
558dc9de8a2SJoerg Roedel return ret;
559cc5aed44SJoerg Roedel }
560cc5aed44SJoerg Roedel
iommu_probe_device(struct device * dev)5613eeeb45cSJoerg Roedel int iommu_probe_device(struct device *dev)
562cf193888SJoerg Roedel {
563e63cfb5fSRobin Murphy const struct iommu_ops *ops;
564cf193888SJoerg Roedel int ret;
565cf193888SJoerg Roedel
566c1114090SRobin Murphy mutex_lock(&iommu_probe_device_lock);
567cf193888SJoerg Roedel ret = __iommu_probe_device(dev, NULL);
568c1114090SRobin Murphy mutex_unlock(&iommu_probe_device_lock);
569cf193888SJoerg Roedel if (ret)
570f1880563SJason Gunthorpe return ret;
571cf193888SJoerg Roedel
572e63cfb5fSRobin Murphy ops = dev_iommu_ops(dev);
573cf193888SJoerg Roedel if (ops->probe_finalize)
574cf193888SJoerg Roedel ops->probe_finalize(dev);
575cf193888SJoerg Roedel
576cf193888SJoerg Roedel return 0;
577cf193888SJoerg Roedel }
578cf193888SJoerg Roedel
__iommu_group_free_device(struct iommu_group * group,struct group_device * grp_dev)579df15d76dSJason Gunthorpe static void __iommu_group_free_device(struct iommu_group *group,
580293f2564SLu Baolu struct group_device *grp_dev)
581293f2564SLu Baolu {
582293f2564SLu Baolu struct device *dev = grp_dev->dev;
583293f2564SLu Baolu
584293f2564SLu Baolu sysfs_remove_link(group->devices_kobj, grp_dev->name);
585293f2564SLu Baolu sysfs_remove_link(&dev->kobj, "iommu_group");
586293f2564SLu Baolu
587293f2564SLu Baolu trace_remove_device_from_group(group->id, dev);
588293f2564SLu Baolu
589dba9ca9dSLu Baolu /*
590df15d76dSJason Gunthorpe * If the group has become empty then ownership must have been
591df15d76dSJason Gunthorpe * released, and the current domain must be set back to NULL or
592df15d76dSJason Gunthorpe * the default domain.
593dba9ca9dSLu Baolu */
594dba9ca9dSLu Baolu if (list_empty(&group->devices))
595dba9ca9dSLu Baolu WARN_ON(group->owner_cnt ||
596dba9ca9dSLu Baolu group->domain != group->default_domain);
597dba9ca9dSLu Baolu
598cc5aed44SJoerg Roedel kfree(grp_dev->name);
599cc5aed44SJoerg Roedel kfree(grp_dev);
600df15d76dSJason Gunthorpe }
601df15d76dSJason Gunthorpe
602aa095857SJason Gunthorpe /* Remove the iommu_group from the struct device. */
__iommu_group_remove_device(struct device * dev)603df15d76dSJason Gunthorpe static void __iommu_group_remove_device(struct device *dev)
604df15d76dSJason Gunthorpe {
605df15d76dSJason Gunthorpe struct iommu_group *group = dev->iommu_group;
606df15d76dSJason Gunthorpe struct group_device *device;
607df15d76dSJason Gunthorpe
608aa095857SJason Gunthorpe mutex_lock(&group->mutex);
609df15d76dSJason Gunthorpe for_each_group_device(group, device) {
610df15d76dSJason Gunthorpe if (device->dev != dev)
611df15d76dSJason Gunthorpe continue;
612df15d76dSJason Gunthorpe
613df15d76dSJason Gunthorpe list_del(&device->list);
614df15d76dSJason Gunthorpe __iommu_group_free_device(group, device);
615aa095857SJason Gunthorpe if (dev->iommu && dev->iommu->iommu_dev)
616aa095857SJason Gunthorpe iommu_deinit_device(dev);
617aa095857SJason Gunthorpe else
618aa095857SJason Gunthorpe dev->iommu_group = NULL;
619cfb6ee65SJason Gunthorpe break;
620df15d76dSJason Gunthorpe }
621dba9ca9dSLu Baolu mutex_unlock(&group->mutex);
6220c830e6bSJacob Pan
623fa082803SJason Gunthorpe /*
624fa082803SJason Gunthorpe * Pairs with the get in iommu_init_device() or
625fa082803SJason Gunthorpe * iommu_group_add_device()
626fa082803SJason Gunthorpe */
627aa095857SJason Gunthorpe iommu_group_put(group);
628d72e31c9SAlex Williamson }
629dba9ca9dSLu Baolu
iommu_release_device(struct device * dev)630d72e31c9SAlex Williamson static void iommu_release_device(struct device *dev)
631d72e31c9SAlex Williamson {
632d72e31c9SAlex Williamson struct iommu_group *group = dev->iommu_group;
633d72e31c9SAlex Williamson
634cfb6ee65SJason Gunthorpe if (group)
635df15d76dSJason Gunthorpe __iommu_group_remove_device(dev);
636cfb6ee65SJason Gunthorpe
637cfb6ee65SJason Gunthorpe /* Free any fwspec if no iommu_driver was ever attached */
638cfb6ee65SJason Gunthorpe if (dev->iommu)
639045a7042SJoerg Roedel dev_iommu_free(dev);
640cc5aed44SJoerg Roedel }
64153723dc5SJoerg Roedel
iommu_set_def_domain_type(char * str)642fccb4e3bSWill Deacon static int __init iommu_set_def_domain_type(char *str)
643fccb4e3bSWill Deacon {
644fccb4e3bSWill Deacon bool pt;
6457f9584dfSAndy Shevchenko int ret;
646fccb4e3bSWill Deacon
6477f9584dfSAndy Shevchenko ret = kstrtobool(str, &pt);
6487f9584dfSAndy Shevchenko if (ret)
6497f9584dfSAndy Shevchenko return ret;
650fccb4e3bSWill Deacon
651adab0b07SJoerg Roedel if (pt)
652adab0b07SJoerg Roedel iommu_set_default_passthrough(true);
653adab0b07SJoerg Roedel else
654adab0b07SJoerg Roedel iommu_set_default_translated(true);
655faf14989SJoerg Roedel
656fccb4e3bSWill Deacon return 0;
657fccb4e3bSWill Deacon }
658fccb4e3bSWill Deacon early_param("iommu.passthrough", iommu_set_def_domain_type);
659fccb4e3bSWill Deacon
iommu_dma_setup(char * str)66068a6efe8SZhen Lei static int __init iommu_dma_setup(char *str)
66168a6efe8SZhen Lei {
662a250c23fSRobin Murphy int ret = kstrtobool(str, &iommu_dma_strict);
663a250c23fSRobin Murphy
664a250c23fSRobin Murphy if (!ret)
665a250c23fSRobin Murphy iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
666a250c23fSRobin Murphy return ret;
66768a6efe8SZhen Lei }
66868a6efe8SZhen Lei early_param("iommu.strict", iommu_dma_setup);
66968a6efe8SZhen Lei
iommu_set_dma_strict(void)670308723e3SJohn Garry void iommu_set_dma_strict(void)
671a250c23fSRobin Murphy {
672308723e3SJohn Garry iommu_dma_strict = true;
673c208916fSRobin Murphy if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
674c208916fSRobin Murphy iommu_def_domain_type = IOMMU_DOMAIN_DMA;
675a250c23fSRobin Murphy }
676a250c23fSRobin Murphy
iommu_group_attr_show(struct kobject * kobj,struct attribute * __attr,char * buf)677d72e31c9SAlex Williamson static ssize_t iommu_group_attr_show(struct kobject *kobj,
678d72e31c9SAlex Williamson struct attribute *__attr, char *buf)
6791460432cSAlex Williamson {
680d72e31c9SAlex Williamson struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
681d72e31c9SAlex Williamson struct iommu_group *group = to_iommu_group(kobj);
682d72e31c9SAlex Williamson ssize_t ret = -EIO;
6831460432cSAlex Williamson
684d72e31c9SAlex Williamson if (attr->show)
685d72e31c9SAlex Williamson ret = attr->show(group, buf);
686d72e31c9SAlex Williamson return ret;
6871460432cSAlex Williamson }
688d72e31c9SAlex Williamson
iommu_group_attr_store(struct kobject * kobj,struct attribute * __attr,const char * buf,size_t count)689d72e31c9SAlex Williamson static ssize_t iommu_group_attr_store(struct kobject *kobj,
690d72e31c9SAlex Williamson struct attribute *__attr,
691d72e31c9SAlex Williamson const char *buf, size_t count)
692d72e31c9SAlex Williamson {
693d72e31c9SAlex Williamson struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
694d72e31c9SAlex Williamson struct iommu_group *group = to_iommu_group(kobj);
695d72e31c9SAlex Williamson ssize_t ret = -EIO;
696d72e31c9SAlex Williamson
697d72e31c9SAlex Williamson if (attr->store)
698d72e31c9SAlex Williamson ret = attr->store(group, buf, count);
699d72e31c9SAlex Williamson return ret;
700d72e31c9SAlex Williamson }
701d72e31c9SAlex Williamson
702d72e31c9SAlex Williamson static const struct sysfs_ops iommu_group_sysfs_ops = {
703d72e31c9SAlex Williamson .show = iommu_group_attr_show,
704d72e31c9SAlex Williamson .store = iommu_group_attr_store,
705d72e31c9SAlex Williamson };
706d72e31c9SAlex Williamson
iommu_group_create_file(struct iommu_group * group,struct iommu_group_attribute * attr)707d72e31c9SAlex Williamson static int iommu_group_create_file(struct iommu_group *group,
708d72e31c9SAlex Williamson struct iommu_group_attribute *attr)
709d72e31c9SAlex Williamson {
710d72e31c9SAlex Williamson return sysfs_create_file(&group->kobj, &attr->attr);
711d72e31c9SAlex Williamson }
712d72e31c9SAlex Williamson
iommu_group_remove_file(struct iommu_group * group,struct iommu_group_attribute * attr)713d72e31c9SAlex Williamson static void iommu_group_remove_file(struct iommu_group *group,
714d72e31c9SAlex Williamson struct iommu_group_attribute *attr)
715d72e31c9SAlex Williamson {
716d72e31c9SAlex Williamson sysfs_remove_file(&group->kobj, &attr->attr);
717d72e31c9SAlex Williamson }
718d72e31c9SAlex Williamson
iommu_group_show_name(struct iommu_group * group,char * buf)719d72e31c9SAlex Williamson static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
720d72e31c9SAlex Williamson {
721c33fcc13SLu Baolu return sysfs_emit(buf, "%s\n", group->name);
722d72e31c9SAlex Williamson }
723d72e31c9SAlex Williamson
7246c65fb31SEric Auger /**
7256c65fb31SEric Auger * iommu_insert_resv_region - Insert a new region in the
7266c65fb31SEric Auger * list of reserved regions.
7276c65fb31SEric Auger * @new: new region to insert
7286c65fb31SEric Auger * @regions: list of regions
7296c65fb31SEric Auger *
7304dbd258fSEric Auger * Elements are sorted by start address and overlapping segments
7314dbd258fSEric Auger * of the same type are merged.
7326c65fb31SEric Auger */
iommu_insert_resv_region(struct iommu_resv_region * new,struct list_head * regions)7331b0b2a84SWei Yongjun static int iommu_insert_resv_region(struct iommu_resv_region *new,
7346c65fb31SEric Auger struct list_head *regions)
7356c65fb31SEric Auger {
7364dbd258fSEric Auger struct iommu_resv_region *iter, *tmp, *nr, *top;
7374dbd258fSEric Auger LIST_HEAD(stack);
7386c65fb31SEric Auger
7394dbd258fSEric Auger nr = iommu_alloc_resv_region(new->start, new->length,
7400251d010SLu Baolu new->prot, new->type, GFP_KERNEL);
7414dbd258fSEric Auger if (!nr)
7426c65fb31SEric Auger return -ENOMEM;
7436c65fb31SEric Auger
7444dbd258fSEric Auger /* First add the new element based on start address sorting */
7454dbd258fSEric Auger list_for_each_entry(iter, regions, list) {
7464dbd258fSEric Auger if (nr->start < iter->start ||
7474dbd258fSEric Auger (nr->start == iter->start && nr->type <= iter->type))
7484dbd258fSEric Auger break;
7494dbd258fSEric Auger }
7504dbd258fSEric Auger list_add_tail(&nr->list, &iter->list);
7514dbd258fSEric Auger
7524dbd258fSEric Auger /* Merge overlapping segments of type nr->type in @regions, if any */
7534dbd258fSEric Auger list_for_each_entry_safe(iter, tmp, regions, list) {
7544dbd258fSEric Auger phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
7554dbd258fSEric Auger
7564c80ba39SEric Auger /* no merge needed on elements of different types than @new */
7574c80ba39SEric Auger if (iter->type != new->type) {
7584dbd258fSEric Auger list_move_tail(&iter->list, &stack);
7594dbd258fSEric Auger continue;
7604dbd258fSEric Auger }
7614dbd258fSEric Auger
7624dbd258fSEric Auger /* look for the last stack element of same type as @iter */
7634dbd258fSEric Auger list_for_each_entry_reverse(top, &stack, list)
7644dbd258fSEric Auger if (top->type == iter->type)
7654dbd258fSEric Auger goto check_overlap;
7664dbd258fSEric Auger
7674dbd258fSEric Auger list_move_tail(&iter->list, &stack);
7684dbd258fSEric Auger continue;
7694dbd258fSEric Auger
7704dbd258fSEric Auger check_overlap:
7714dbd258fSEric Auger top_end = top->start + top->length - 1;
7724dbd258fSEric Auger
7734dbd258fSEric Auger if (iter->start > top_end + 1) {
7744dbd258fSEric Auger list_move_tail(&iter->list, &stack);
7754dbd258fSEric Auger } else {
7764dbd258fSEric Auger top->length = max(top_end, iter_end) - top->start + 1;
7774dbd258fSEric Auger list_del(&iter->list);
7784dbd258fSEric Auger kfree(iter);
7794dbd258fSEric Auger }
7804dbd258fSEric Auger }
7814dbd258fSEric Auger list_splice(&stack, regions);
7826c65fb31SEric Auger return 0;
7836c65fb31SEric Auger }
7846c65fb31SEric Auger
7856c65fb31SEric Auger static int
iommu_insert_device_resv_regions(struct list_head * dev_resv_regions,struct list_head * group_resv_regions)7866c65fb31SEric Auger iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
7876c65fb31SEric Auger struct list_head *group_resv_regions)
7886c65fb31SEric Auger {
7896c65fb31SEric Auger struct iommu_resv_region *entry;
790a514a6e2SEric Auger int ret = 0;
7916c65fb31SEric Auger
7926c65fb31SEric Auger list_for_each_entry(entry, dev_resv_regions, list) {
7936c65fb31SEric Auger ret = iommu_insert_resv_region(entry, group_resv_regions);
7946c65fb31SEric Auger if (ret)
7956c65fb31SEric Auger break;
7966c65fb31SEric Auger }
7976c65fb31SEric Auger return ret;
7986c65fb31SEric Auger }
7996c65fb31SEric Auger
iommu_get_group_resv_regions(struct iommu_group * group,struct list_head * head)8006c65fb31SEric Auger int iommu_get_group_resv_regions(struct iommu_group *group,
8016c65fb31SEric Auger struct list_head *head)
8026c65fb31SEric Auger {
8038d2932ddSJoerg Roedel struct group_device *device;
8046c65fb31SEric Auger int ret = 0;
8056c65fb31SEric Auger
8066c65fb31SEric Auger mutex_lock(&group->mutex);
8073006b15bSJason Gunthorpe for_each_group_device(group, device) {
8086c65fb31SEric Auger struct list_head dev_resv_regions;
8096c65fb31SEric Auger
810392bf519SRobin Murphy /*
811392bf519SRobin Murphy * Non-API groups still expose reserved_regions in sysfs,
812392bf519SRobin Murphy * so filter out calls that get here that way.
813392bf519SRobin Murphy */
814392bf519SRobin Murphy if (!device->dev->iommu)
815392bf519SRobin Murphy break;
816392bf519SRobin Murphy
8176c65fb31SEric Auger INIT_LIST_HEAD(&dev_resv_regions);
8186c65fb31SEric Auger iommu_get_resv_regions(device->dev, &dev_resv_regions);
8196c65fb31SEric Auger ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
8206c65fb31SEric Auger iommu_put_resv_regions(device->dev, &dev_resv_regions);
8216c65fb31SEric Auger if (ret)
8226c65fb31SEric Auger break;
8236c65fb31SEric Auger }
8246c65fb31SEric Auger mutex_unlock(&group->mutex);
8256c65fb31SEric Auger return ret;
8266c65fb31SEric Auger }
8276c65fb31SEric Auger EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
8286c65fb31SEric Auger
iommu_group_show_resv_regions(struct iommu_group * group,char * buf)829bc7d12b9SEric Auger static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
830bc7d12b9SEric Auger char *buf)
831bc7d12b9SEric Auger {
832bc7d12b9SEric Auger struct iommu_resv_region *region, *next;
833bc7d12b9SEric Auger struct list_head group_resv_regions;
834c33fcc13SLu Baolu int offset = 0;
835bc7d12b9SEric Auger
836bc7d12b9SEric Auger INIT_LIST_HEAD(&group_resv_regions);
837bc7d12b9SEric Auger iommu_get_group_resv_regions(group, &group_resv_regions);
838bc7d12b9SEric Auger
839bc7d12b9SEric Auger list_for_each_entry_safe(region, next, &group_resv_regions, list) {
840c33fcc13SLu Baolu offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
841c33fcc13SLu Baolu (long long)region->start,
842c33fcc13SLu Baolu (long long)(region->start +
843bc7d12b9SEric Auger region->length - 1),
844bc7d12b9SEric Auger iommu_group_resv_type_string[region->type]);
845bc7d12b9SEric Auger kfree(region);
846bc7d12b9SEric Auger }
847bc7d12b9SEric Auger
848c33fcc13SLu Baolu return offset;
849bc7d12b9SEric Auger }
850bc7d12b9SEric Auger
iommu_group_show_type(struct iommu_group * group,char * buf)851c52c72d3SOlof Johansson static ssize_t iommu_group_show_type(struct iommu_group *group,
852c52c72d3SOlof Johansson char *buf)
853c52c72d3SOlof Johansson {
854c33fcc13SLu Baolu char *type = "unknown";
855c52c72d3SOlof Johansson
8560b8a96a3SSai Praneeth Prakhya mutex_lock(&group->mutex);
857c52c72d3SOlof Johansson if (group->default_domain) {
858c52c72d3SOlof Johansson switch (group->default_domain->type) {
859c52c72d3SOlof Johansson case IOMMU_DOMAIN_BLOCKED:
860c33fcc13SLu Baolu type = "blocked";
861c52c72d3SOlof Johansson break;
862c52c72d3SOlof Johansson case IOMMU_DOMAIN_IDENTITY:
863c33fcc13SLu Baolu type = "identity";
864c52c72d3SOlof Johansson break;
865c52c72d3SOlof Johansson case IOMMU_DOMAIN_UNMANAGED:
866c33fcc13SLu Baolu type = "unmanaged";
867c52c72d3SOlof Johansson break;
868c52c72d3SOlof Johansson case IOMMU_DOMAIN_DMA:
869c33fcc13SLu Baolu type = "DMA";
870c52c72d3SOlof Johansson break;
871bf3aed46SRobin Murphy case IOMMU_DOMAIN_DMA_FQ:
872c33fcc13SLu Baolu type = "DMA-FQ";
873bf3aed46SRobin Murphy break;
874c52c72d3SOlof Johansson }
875c52c72d3SOlof Johansson }
8760b8a96a3SSai Praneeth Prakhya mutex_unlock(&group->mutex);
877c52c72d3SOlof Johansson
878c33fcc13SLu Baolu return sysfs_emit(buf, "%s\n", type);
879c52c72d3SOlof Johansson }
880c52c72d3SOlof Johansson
881d72e31c9SAlex Williamson static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
882d72e31c9SAlex Williamson
883bc7d12b9SEric Auger static IOMMU_GROUP_ATTR(reserved_regions, 0444,
884bc7d12b9SEric Auger iommu_group_show_resv_regions, NULL);
885bc7d12b9SEric Auger
88608a27c1cSSai Praneeth Prakhya static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
88708a27c1cSSai Praneeth Prakhya iommu_group_store_type);
888c52c72d3SOlof Johansson
iommu_group_release(struct kobject * kobj)889d72e31c9SAlex Williamson static void iommu_group_release(struct kobject *kobj)
890d72e31c9SAlex Williamson {
891d72e31c9SAlex Williamson struct iommu_group *group = to_iommu_group(kobj);
892d72e31c9SAlex Williamson
893269aa808SJoerg Roedel pr_debug("Releasing group %d\n", group->id);
894269aa808SJoerg Roedel
895d72e31c9SAlex Williamson if (group->iommu_data_release)
896d72e31c9SAlex Williamson group->iommu_data_release(group->iommu_data);
897d72e31c9SAlex Williamson
898822242e6SKe Liu ida_free(&iommu_group_ida, group->id);
899d72e31c9SAlex Williamson
900aa095857SJason Gunthorpe /* Domains are free'd by iommu_deinit_device() */
901aa095857SJason Gunthorpe WARN_ON(group->default_domain);
902aa095857SJason Gunthorpe WARN_ON(group->blocking_domain);
90353723dc5SJoerg Roedel
904d72e31c9SAlex Williamson kfree(group->name);
905d72e31c9SAlex Williamson kfree(group);
906d72e31c9SAlex Williamson }
907d72e31c9SAlex Williamson
908aa977833SThomas Weißschuh static const struct kobj_type iommu_group_ktype = {
909d72e31c9SAlex Williamson .sysfs_ops = &iommu_group_sysfs_ops,
910d72e31c9SAlex Williamson .release = iommu_group_release,
911d72e31c9SAlex Williamson };
912d72e31c9SAlex Williamson
913d72e31c9SAlex Williamson /**
914d72e31c9SAlex Williamson * iommu_group_alloc - Allocate a new group
915d72e31c9SAlex Williamson *
916d72e31c9SAlex Williamson * This function is called by an iommu driver to allocate a new iommu
917d72e31c9SAlex Williamson * group. The iommu group represents the minimum granularity of the iommu.
918d72e31c9SAlex Williamson * Upon successful return, the caller holds a reference to the supplied
919d72e31c9SAlex Williamson * group in order to hold the group until devices are added. Use
920d72e31c9SAlex Williamson * iommu_group_put() to release this extra reference count, allowing the
921d72e31c9SAlex Williamson * group to be automatically reclaimed once it has no devices or external
922d72e31c9SAlex Williamson * references.
923d72e31c9SAlex Williamson */
iommu_group_alloc(void)924d72e31c9SAlex Williamson struct iommu_group *iommu_group_alloc(void)
925d72e31c9SAlex Williamson {
926d72e31c9SAlex Williamson struct iommu_group *group;
927d72e31c9SAlex Williamson int ret;
928d72e31c9SAlex Williamson
929d72e31c9SAlex Williamson group = kzalloc(sizeof(*group), GFP_KERNEL);
930d72e31c9SAlex Williamson if (!group)
931d72e31c9SAlex Williamson return ERR_PTR(-ENOMEM);
932d72e31c9SAlex Williamson
933d72e31c9SAlex Williamson group->kobj.kset = iommu_group_kset;
934d72e31c9SAlex Williamson mutex_init(&group->mutex);
935d72e31c9SAlex Williamson INIT_LIST_HEAD(&group->devices);
93641df6dccSJoerg Roedel INIT_LIST_HEAD(&group->entry);
93716603704SLu Baolu xa_init(&group->pasid_array);
938d72e31c9SAlex Williamson
939822242e6SKe Liu ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
940feccf398SHeiner Kallweit if (ret < 0) {
941d72e31c9SAlex Williamson kfree(group);
942feccf398SHeiner Kallweit return ERR_PTR(ret);
943d72e31c9SAlex Williamson }
944feccf398SHeiner Kallweit group->id = ret;
945d72e31c9SAlex Williamson
946d72e31c9SAlex Williamson ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
947d72e31c9SAlex Williamson NULL, "%d", group->id);
948d72e31c9SAlex Williamson if (ret) {
9497cc31613SQiushi Wu kobject_put(&group->kobj);
950d72e31c9SAlex Williamson return ERR_PTR(ret);
951d72e31c9SAlex Williamson }
952d72e31c9SAlex Williamson
953d72e31c9SAlex Williamson group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
954d72e31c9SAlex Williamson if (!group->devices_kobj) {
955d72e31c9SAlex Williamson kobject_put(&group->kobj); /* triggers .release & free */
956d72e31c9SAlex Williamson return ERR_PTR(-ENOMEM);
957d72e31c9SAlex Williamson }
958d72e31c9SAlex Williamson
959d72e31c9SAlex Williamson /*
960d72e31c9SAlex Williamson * The devices_kobj holds a reference on the group kobject, so
961d72e31c9SAlex Williamson * as long as that exists so will the group. We can therefore
962d72e31c9SAlex Williamson * use the devices_kobj for reference counting.
963d72e31c9SAlex Williamson */
964d72e31c9SAlex Williamson kobject_put(&group->kobj);
965d72e31c9SAlex Williamson
966bc7d12b9SEric Auger ret = iommu_group_create_file(group,
967bc7d12b9SEric Auger &iommu_group_attr_reserved_regions);
9684daa8611SJason Gunthorpe if (ret) {
9694daa8611SJason Gunthorpe kobject_put(group->devices_kobj);
970bc7d12b9SEric Auger return ERR_PTR(ret);
9714daa8611SJason Gunthorpe }
972bc7d12b9SEric Auger
973c52c72d3SOlof Johansson ret = iommu_group_create_file(group, &iommu_group_attr_type);
9744daa8611SJason Gunthorpe if (ret) {
9754daa8611SJason Gunthorpe kobject_put(group->devices_kobj);
976c52c72d3SOlof Johansson return ERR_PTR(ret);
9774daa8611SJason Gunthorpe }
978c52c72d3SOlof Johansson
979269aa808SJoerg Roedel pr_debug("Allocated group %d\n", group->id);
980269aa808SJoerg Roedel
981d72e31c9SAlex Williamson return group;
982d72e31c9SAlex Williamson }
983d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_alloc);
984d72e31c9SAlex Williamson
985d72e31c9SAlex Williamson /**
986d72e31c9SAlex Williamson * iommu_group_get_iommudata - retrieve iommu_data registered for a group
987d72e31c9SAlex Williamson * @group: the group
988d72e31c9SAlex Williamson *
989d72e31c9SAlex Williamson * iommu drivers can store data in the group for use when doing iommu
990d72e31c9SAlex Williamson * operations. This function provides a way to retrieve it. Caller
991d72e31c9SAlex Williamson * should hold a group reference.
992d72e31c9SAlex Williamson */
iommu_group_get_iommudata(struct iommu_group * group)993d72e31c9SAlex Williamson void *iommu_group_get_iommudata(struct iommu_group *group)
994d72e31c9SAlex Williamson {
995d72e31c9SAlex Williamson return group->iommu_data;
996d72e31c9SAlex Williamson }
997d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
998d72e31c9SAlex Williamson
999d72e31c9SAlex Williamson /**
1000d72e31c9SAlex Williamson * iommu_group_set_iommudata - set iommu_data for a group
1001d72e31c9SAlex Williamson * @group: the group
1002d72e31c9SAlex Williamson * @iommu_data: new data
1003d72e31c9SAlex Williamson * @release: release function for iommu_data
1004d72e31c9SAlex Williamson *
1005d72e31c9SAlex Williamson * iommu drivers can store data in the group for use when doing iommu
1006d72e31c9SAlex Williamson * operations. This function provides a way to set the data after
1007d72e31c9SAlex Williamson * the group has been allocated. Caller should hold a group reference.
1008d72e31c9SAlex Williamson */
iommu_group_set_iommudata(struct iommu_group * group,void * iommu_data,void (* release)(void * iommu_data))1009d72e31c9SAlex Williamson void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
1010d72e31c9SAlex Williamson void (*release)(void *iommu_data))
1011d72e31c9SAlex Williamson {
1012d72e31c9SAlex Williamson group->iommu_data = iommu_data;
1013d72e31c9SAlex Williamson group->iommu_data_release = release;
1014d72e31c9SAlex Williamson }
1015d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1016d72e31c9SAlex Williamson
1017d72e31c9SAlex Williamson /**
1018d72e31c9SAlex Williamson * iommu_group_set_name - set name for a group
1019d72e31c9SAlex Williamson * @group: the group
1020d72e31c9SAlex Williamson * @name: name
1021d72e31c9SAlex Williamson *
1022d72e31c9SAlex Williamson * Allow iommu driver to set a name for a group. When set it will
1023d72e31c9SAlex Williamson * appear in a name attribute file under the group in sysfs.
1024d72e31c9SAlex Williamson */
iommu_group_set_name(struct iommu_group * group,const char * name)1025d72e31c9SAlex Williamson int iommu_group_set_name(struct iommu_group *group, const char *name)
1026d72e31c9SAlex Williamson {
1027d72e31c9SAlex Williamson int ret;
1028d72e31c9SAlex Williamson
1029d72e31c9SAlex Williamson if (group->name) {
1030d72e31c9SAlex Williamson iommu_group_remove_file(group, &iommu_group_attr_name);
1031d72e31c9SAlex Williamson kfree(group->name);
1032d72e31c9SAlex Williamson group->name = NULL;
1033d72e31c9SAlex Williamson if (!name)
1034d72e31c9SAlex Williamson return 0;
1035d72e31c9SAlex Williamson }
1036d72e31c9SAlex Williamson
1037d72e31c9SAlex Williamson group->name = kstrdup(name, GFP_KERNEL);
1038d72e31c9SAlex Williamson if (!group->name)
1039d72e31c9SAlex Williamson return -ENOMEM;
1040d72e31c9SAlex Williamson
1041d72e31c9SAlex Williamson ret = iommu_group_create_file(group, &iommu_group_attr_name);
1042d72e31c9SAlex Williamson if (ret) {
1043d72e31c9SAlex Williamson kfree(group->name);
1044d72e31c9SAlex Williamson group->name = NULL;
1045d72e31c9SAlex Williamson return ret;
1046d72e31c9SAlex Williamson }
1047d72e31c9SAlex Williamson
1048d72e31c9SAlex Williamson return 0;
1049d72e31c9SAlex Williamson }
1050d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_set_name);
1051d72e31c9SAlex Williamson
iommu_create_device_direct_mappings(struct iommu_domain * domain,struct device * dev)1052d99be00fSJason Gunthorpe static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1053beed2821SJoerg Roedel struct device *dev)
1054beed2821SJoerg Roedel {
1055e5b5234aSEric Auger struct iommu_resv_region *entry;
1056beed2821SJoerg Roedel struct list_head mappings;
1057beed2821SJoerg Roedel unsigned long pg_size;
1058beed2821SJoerg Roedel int ret = 0;
1059beed2821SJoerg Roedel
1060a48ce36eSLu Baolu pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
1061beed2821SJoerg Roedel INIT_LIST_HEAD(&mappings);
1062beed2821SJoerg Roedel
1063a48ce36eSLu Baolu if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
1064a48ce36eSLu Baolu return -EINVAL;
1065a48ce36eSLu Baolu
1066e5b5234aSEric Auger iommu_get_resv_regions(dev, &mappings);
1067beed2821SJoerg Roedel
1068beed2821SJoerg Roedel /* We need to consider overlapping regions for different devices */
1069beed2821SJoerg Roedel list_for_each_entry(entry, &mappings, list) {
1070beed2821SJoerg Roedel dma_addr_t start, end, addr;
1071093b32a8SYong Wu size_t map_size = 0;
1072beed2821SJoerg Roedel
1073a48ce36eSLu Baolu if (entry->type == IOMMU_RESV_DIRECT)
1074a48ce36eSLu Baolu dev->iommu->require_direct = 1;
1075a48ce36eSLu Baolu
1076a48ce36eSLu Baolu if ((entry->type != IOMMU_RESV_DIRECT &&
1077a48ce36eSLu Baolu entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1078a48ce36eSLu Baolu !iommu_is_dma_domain(domain))
1079a48ce36eSLu Baolu continue;
1080a48ce36eSLu Baolu
1081beed2821SJoerg Roedel start = ALIGN(entry->start, pg_size);
1082beed2821SJoerg Roedel end = ALIGN(entry->start + entry->length, pg_size);
1083beed2821SJoerg Roedel
1084093b32a8SYong Wu for (addr = start; addr <= end; addr += pg_size) {
1085beed2821SJoerg Roedel phys_addr_t phys_addr;
1086beed2821SJoerg Roedel
1087093b32a8SYong Wu if (addr == end)
1088093b32a8SYong Wu goto map_end;
1089beed2821SJoerg Roedel
1090093b32a8SYong Wu phys_addr = iommu_iova_to_phys(domain, addr);
1091093b32a8SYong Wu if (!phys_addr) {
1092093b32a8SYong Wu map_size += pg_size;
1093093b32a8SYong Wu continue;
1094093b32a8SYong Wu }
1095093b32a8SYong Wu
1096093b32a8SYong Wu map_end:
1097093b32a8SYong Wu if (map_size) {
1098093b32a8SYong Wu ret = iommu_map(domain, addr - map_size,
1099093b32a8SYong Wu addr - map_size, map_size,
11001369459bSJason Gunthorpe entry->prot, GFP_KERNEL);
1101beed2821SJoerg Roedel if (ret)
1102beed2821SJoerg Roedel goto out;
1103093b32a8SYong Wu map_size = 0;
1104093b32a8SYong Wu }
1105beed2821SJoerg Roedel }
1106beed2821SJoerg Roedel
1107beed2821SJoerg Roedel }
1108beed2821SJoerg Roedel
11096e6c6d6bSLu Baolu if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
1110aae4c8e2STom Murphy iommu_flush_iotlb_all(domain);
1111add02cfdSJoerg Roedel
1112beed2821SJoerg Roedel out:
1113e5b5234aSEric Auger iommu_put_resv_regions(dev, &mappings);
1114beed2821SJoerg Roedel
1115beed2821SJoerg Roedel return ret;
1116beed2821SJoerg Roedel }
1117beed2821SJoerg Roedel
1118fa082803SJason Gunthorpe /* This is undone by __iommu_group_free_device() */
iommu_group_alloc_device(struct iommu_group * group,struct device * dev)1119fa082803SJason Gunthorpe static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
1120fa082803SJason Gunthorpe struct device *dev)
1121d72e31c9SAlex Williamson {
1122d72e31c9SAlex Williamson int ret, i = 0;
1123c09e22d5SJoerg Roedel struct group_device *device;
1124d72e31c9SAlex Williamson
1125d72e31c9SAlex Williamson device = kzalloc(sizeof(*device), GFP_KERNEL);
1126d72e31c9SAlex Williamson if (!device)
1127fa082803SJason Gunthorpe return ERR_PTR(-ENOMEM);
1128d72e31c9SAlex Williamson
1129d72e31c9SAlex Williamson device->dev = dev;
1130d72e31c9SAlex Williamson
1131d72e31c9SAlex Williamson ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
1132797a8b4dSRobin Murphy if (ret)
1133797a8b4dSRobin Murphy goto err_free_device;
1134d72e31c9SAlex Williamson
1135d72e31c9SAlex Williamson device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
1136d72e31c9SAlex Williamson rename:
1137d72e31c9SAlex Williamson if (!device->name) {
1138797a8b4dSRobin Murphy ret = -ENOMEM;
1139797a8b4dSRobin Murphy goto err_remove_link;
1140d72e31c9SAlex Williamson }
1141d72e31c9SAlex Williamson
1142d72e31c9SAlex Williamson ret = sysfs_create_link_nowarn(group->devices_kobj,
1143d72e31c9SAlex Williamson &dev->kobj, device->name);
1144d72e31c9SAlex Williamson if (ret) {
1145d72e31c9SAlex Williamson if (ret == -EEXIST && i >= 0) {
1146d72e31c9SAlex Williamson /*
1147d72e31c9SAlex Williamson * Account for the slim chance of collision
1148d72e31c9SAlex Williamson * and append an instance to the name.
1149d72e31c9SAlex Williamson */
1150797a8b4dSRobin Murphy kfree(device->name);
1151d72e31c9SAlex Williamson device->name = kasprintf(GFP_KERNEL, "%s.%d",
1152d72e31c9SAlex Williamson kobject_name(&dev->kobj), i++);
1153d72e31c9SAlex Williamson goto rename;
1154d72e31c9SAlex Williamson }
1155797a8b4dSRobin Murphy goto err_free_name;
1156d72e31c9SAlex Williamson }
1157d72e31c9SAlex Williamson
1158d1cf7e82SShuah Khan trace_add_device_to_group(group->id, dev);
1159269aa808SJoerg Roedel
1160780da9e4SBjorn Helgaas dev_info(dev, "Adding to iommu group %d\n", group->id);
1161269aa808SJoerg Roedel
1162fa082803SJason Gunthorpe return device;
1163797a8b4dSRobin Murphy
1164797a8b4dSRobin Murphy err_free_name:
1165797a8b4dSRobin Murphy kfree(device->name);
1166797a8b4dSRobin Murphy err_remove_link:
1167797a8b4dSRobin Murphy sysfs_remove_link(&dev->kobj, "iommu_group");
1168797a8b4dSRobin Murphy err_free_device:
1169797a8b4dSRobin Murphy kfree(device);
1170780da9e4SBjorn Helgaas dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
1171fa082803SJason Gunthorpe return ERR_PTR(ret);
1172fa082803SJason Gunthorpe }
1173fa082803SJason Gunthorpe
1174fa082803SJason Gunthorpe /**
1175fa082803SJason Gunthorpe * iommu_group_add_device - add a device to an iommu group
1176fa082803SJason Gunthorpe * @group: the group into which to add the device (reference should be held)
1177fa082803SJason Gunthorpe * @dev: the device
1178fa082803SJason Gunthorpe *
1179fa082803SJason Gunthorpe * This function is called by an iommu driver to add a device into a
1180fa082803SJason Gunthorpe * group. Adding a device increments the group reference count.
1181fa082803SJason Gunthorpe */
iommu_group_add_device(struct iommu_group * group,struct device * dev)1182fa082803SJason Gunthorpe int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1183fa082803SJason Gunthorpe {
1184fa082803SJason Gunthorpe struct group_device *gdev;
1185fa082803SJason Gunthorpe
1186fa082803SJason Gunthorpe gdev = iommu_group_alloc_device(group, dev);
1187fa082803SJason Gunthorpe if (IS_ERR(gdev))
1188fa082803SJason Gunthorpe return PTR_ERR(gdev);
1189fa082803SJason Gunthorpe
1190fa082803SJason Gunthorpe iommu_group_ref_get(group);
1191fa082803SJason Gunthorpe dev->iommu_group = group;
1192fa082803SJason Gunthorpe
1193fa082803SJason Gunthorpe mutex_lock(&group->mutex);
1194fa082803SJason Gunthorpe list_add_tail(&gdev->list, &group->devices);
1195fa082803SJason Gunthorpe mutex_unlock(&group->mutex);
1196fa082803SJason Gunthorpe return 0;
1197d72e31c9SAlex Williamson }
1198d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_add_device);
1199d72e31c9SAlex Williamson
1200d72e31c9SAlex Williamson /**
1201d72e31c9SAlex Williamson * iommu_group_remove_device - remove a device from it's current group
1202d72e31c9SAlex Williamson * @dev: device to be removed
1203d72e31c9SAlex Williamson *
1204d72e31c9SAlex Williamson * This function is called by an iommu driver to remove the device from
1205d72e31c9SAlex Williamson * it's current group. This decrements the iommu group reference count.
1206d72e31c9SAlex Williamson */
iommu_group_remove_device(struct device * dev)1207d72e31c9SAlex Williamson void iommu_group_remove_device(struct device *dev)
1208d72e31c9SAlex Williamson {
1209d72e31c9SAlex Williamson struct iommu_group *group = dev->iommu_group;
1210d72e31c9SAlex Williamson
12115aa95d88SFrank Wunderlich if (!group)
12125aa95d88SFrank Wunderlich return;
12135aa95d88SFrank Wunderlich
1214780da9e4SBjorn Helgaas dev_info(dev, "Removing from iommu group %d\n", group->id);
1215269aa808SJoerg Roedel
1216df15d76dSJason Gunthorpe __iommu_group_remove_device(dev);
1217d72e31c9SAlex Williamson }
1218d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_remove_device);
1219d72e31c9SAlex Williamson
122030209b93SJohn Garry /**
122130209b93SJohn Garry * iommu_group_for_each_dev - iterate over each device in the group
122230209b93SJohn Garry * @group: the group
122330209b93SJohn Garry * @data: caller opaque data to be passed to callback function
122430209b93SJohn Garry * @fn: caller supplied callback function
122530209b93SJohn Garry *
122630209b93SJohn Garry * This function is called by group users to iterate over group devices.
122730209b93SJohn Garry * Callers should hold a reference count to the group during callback.
122830209b93SJohn Garry * The group->mutex is held across callbacks, which will block calls to
122930209b93SJohn Garry * iommu_group_add/remove_device.
123030209b93SJohn Garry */
iommu_group_for_each_dev(struct iommu_group * group,void * data,int (* fn)(struct device *,void *))1231e39cb8a3SJoerg Roedel int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1232e39cb8a3SJoerg Roedel int (*fn)(struct device *, void *))
1233e39cb8a3SJoerg Roedel {
1234e996c12dSJason Gunthorpe struct group_device *device;
1235e996c12dSJason Gunthorpe int ret = 0;
1236e39cb8a3SJoerg Roedel
1237e39cb8a3SJoerg Roedel mutex_lock(&group->mutex);
1238e996c12dSJason Gunthorpe for_each_group_device(group, device) {
1239e996c12dSJason Gunthorpe ret = fn(device->dev, data);
1240e996c12dSJason Gunthorpe if (ret)
1241e996c12dSJason Gunthorpe break;
1242e996c12dSJason Gunthorpe }
1243d72e31c9SAlex Williamson mutex_unlock(&group->mutex);
1244e39cb8a3SJoerg Roedel
1245d72e31c9SAlex Williamson return ret;
1246d72e31c9SAlex Williamson }
1247d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1248d72e31c9SAlex Williamson
1249d72e31c9SAlex Williamson /**
1250d72e31c9SAlex Williamson * iommu_group_get - Return the group for a device and increment reference
1251d72e31c9SAlex Williamson * @dev: get the group that this device belongs to
1252d72e31c9SAlex Williamson *
1253d72e31c9SAlex Williamson * This function is called by iommu drivers and users to get the group
1254d72e31c9SAlex Williamson * for the specified device. If found, the group is returned and the group
1255d72e31c9SAlex Williamson * reference in incremented, else NULL.
1256d72e31c9SAlex Williamson */
iommu_group_get(struct device * dev)1257d72e31c9SAlex Williamson struct iommu_group *iommu_group_get(struct device *dev)
1258d72e31c9SAlex Williamson {
1259d72e31c9SAlex Williamson struct iommu_group *group = dev->iommu_group;
1260d72e31c9SAlex Williamson
1261d72e31c9SAlex Williamson if (group)
1262d72e31c9SAlex Williamson kobject_get(group->devices_kobj);
1263d72e31c9SAlex Williamson
1264d72e31c9SAlex Williamson return group;
1265d72e31c9SAlex Williamson }
1266d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_get);
1267d72e31c9SAlex Williamson
1268d72e31c9SAlex Williamson /**
126913f59a78SRobin Murphy * iommu_group_ref_get - Increment reference on a group
127013f59a78SRobin Murphy * @group: the group to use, must not be NULL
127113f59a78SRobin Murphy *
127213f59a78SRobin Murphy * This function is called by iommu drivers to take additional references on an
127313f59a78SRobin Murphy * existing group. Returns the given group for convenience.
127413f59a78SRobin Murphy */
iommu_group_ref_get(struct iommu_group * group)127513f59a78SRobin Murphy struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
127613f59a78SRobin Murphy {
127713f59a78SRobin Murphy kobject_get(group->devices_kobj);
127813f59a78SRobin Murphy return group;
127913f59a78SRobin Murphy }
1280a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(iommu_group_ref_get);
128113f59a78SRobin Murphy
128213f59a78SRobin Murphy /**
1283d72e31c9SAlex Williamson * iommu_group_put - Decrement group reference
1284d72e31c9SAlex Williamson * @group: the group to use
1285d72e31c9SAlex Williamson *
1286d72e31c9SAlex Williamson * This function is called by iommu drivers and users to release the
1287d72e31c9SAlex Williamson * iommu group. Once the reference count is zero, the group is released.
1288d72e31c9SAlex Williamson */
iommu_group_put(struct iommu_group * group)1289d72e31c9SAlex Williamson void iommu_group_put(struct iommu_group *group)
1290d72e31c9SAlex Williamson {
1291d72e31c9SAlex Williamson if (group)
1292d72e31c9SAlex Williamson kobject_put(group->devices_kobj);
1293d72e31c9SAlex Williamson }
1294d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_put);
1295d72e31c9SAlex Williamson
1296d72e31c9SAlex Williamson /**
12970c830e6bSJacob Pan * iommu_register_device_fault_handler() - Register a device fault handler
12980c830e6bSJacob Pan * @dev: the device
12990c830e6bSJacob Pan * @handler: the fault handler
13000c830e6bSJacob Pan * @data: private data passed as argument to the handler
13010c830e6bSJacob Pan *
13020c830e6bSJacob Pan * When an IOMMU fault event is received, this handler gets called with the
1303bf3255b3SJean-Philippe Brucker * fault event and data as argument. The handler should return 0 on success. If
1304bf3255b3SJean-Philippe Brucker * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1305bf3255b3SJean-Philippe Brucker * complete the fault by calling iommu_page_response() with one of the following
1306bf3255b3SJean-Philippe Brucker * response code:
1307bf3255b3SJean-Philippe Brucker * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1308bf3255b3SJean-Philippe Brucker * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1309bf3255b3SJean-Philippe Brucker * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1310bf3255b3SJean-Philippe Brucker * page faults if possible.
13110c830e6bSJacob Pan *
13120c830e6bSJacob Pan * Return 0 if the fault handler was installed successfully, or an error.
13130c830e6bSJacob Pan */
iommu_register_device_fault_handler(struct device * dev,iommu_dev_fault_handler_t handler,void * data)13140c830e6bSJacob Pan int iommu_register_device_fault_handler(struct device *dev,
13150c830e6bSJacob Pan iommu_dev_fault_handler_t handler,
13160c830e6bSJacob Pan void *data)
13170c830e6bSJacob Pan {
1318045a7042SJoerg Roedel struct dev_iommu *param = dev->iommu;
13190c830e6bSJacob Pan int ret = 0;
13200c830e6bSJacob Pan
13210c830e6bSJacob Pan if (!param)
13220c830e6bSJacob Pan return -EINVAL;
13230c830e6bSJacob Pan
13240c830e6bSJacob Pan mutex_lock(¶m->lock);
13250c830e6bSJacob Pan /* Only allow one fault handler registered for each device */
13260c830e6bSJacob Pan if (param->fault_param) {
13270c830e6bSJacob Pan ret = -EBUSY;
13280c830e6bSJacob Pan goto done_unlock;
13290c830e6bSJacob Pan }
13300c830e6bSJacob Pan
13310c830e6bSJacob Pan get_device(dev);
13320c830e6bSJacob Pan param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
13330c830e6bSJacob Pan if (!param->fault_param) {
13340c830e6bSJacob Pan put_device(dev);
13350c830e6bSJacob Pan ret = -ENOMEM;
13360c830e6bSJacob Pan goto done_unlock;
13370c830e6bSJacob Pan }
13380c830e6bSJacob Pan param->fault_param->handler = handler;
13390c830e6bSJacob Pan param->fault_param->data = data;
1340bf3255b3SJean-Philippe Brucker mutex_init(¶m->fault_param->lock);
1341bf3255b3SJean-Philippe Brucker INIT_LIST_HEAD(¶m->fault_param->faults);
13420c830e6bSJacob Pan
13430c830e6bSJacob Pan done_unlock:
13440c830e6bSJacob Pan mutex_unlock(¶m->lock);
13450c830e6bSJacob Pan
13460c830e6bSJacob Pan return ret;
13470c830e6bSJacob Pan }
13480c830e6bSJacob Pan EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
13490c830e6bSJacob Pan
13500c830e6bSJacob Pan /**
13510c830e6bSJacob Pan * iommu_unregister_device_fault_handler() - Unregister the device fault handler
13520c830e6bSJacob Pan * @dev: the device
13530c830e6bSJacob Pan *
13540c830e6bSJacob Pan * Remove the device fault handler installed with
13550c830e6bSJacob Pan * iommu_register_device_fault_handler().
13560c830e6bSJacob Pan *
13570c830e6bSJacob Pan * Return 0 on success, or an error.
13580c830e6bSJacob Pan */
iommu_unregister_device_fault_handler(struct device * dev)13590c830e6bSJacob Pan int iommu_unregister_device_fault_handler(struct device *dev)
13600c830e6bSJacob Pan {
1361045a7042SJoerg Roedel struct dev_iommu *param = dev->iommu;
13620c830e6bSJacob Pan int ret = 0;
13630c830e6bSJacob Pan
13640c830e6bSJacob Pan if (!param)
13650c830e6bSJacob Pan return -EINVAL;
13660c830e6bSJacob Pan
13670c830e6bSJacob Pan mutex_lock(¶m->lock);
13680c830e6bSJacob Pan
13690c830e6bSJacob Pan if (!param->fault_param)
13700c830e6bSJacob Pan goto unlock;
13710c830e6bSJacob Pan
1372bf3255b3SJean-Philippe Brucker /* we cannot unregister handler if there are pending faults */
1373bf3255b3SJean-Philippe Brucker if (!list_empty(¶m->fault_param->faults)) {
1374bf3255b3SJean-Philippe Brucker ret = -EBUSY;
1375bf3255b3SJean-Philippe Brucker goto unlock;
1376bf3255b3SJean-Philippe Brucker }
1377bf3255b3SJean-Philippe Brucker
13780c830e6bSJacob Pan kfree(param->fault_param);
13790c830e6bSJacob Pan param->fault_param = NULL;
13800c830e6bSJacob Pan put_device(dev);
13810c830e6bSJacob Pan unlock:
13820c830e6bSJacob Pan mutex_unlock(¶m->lock);
13830c830e6bSJacob Pan
13840c830e6bSJacob Pan return ret;
13850c830e6bSJacob Pan }
13860c830e6bSJacob Pan EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
13870c830e6bSJacob Pan
13880c830e6bSJacob Pan /**
13890c830e6bSJacob Pan * iommu_report_device_fault() - Report fault event to device driver
13900c830e6bSJacob Pan * @dev: the device
13910c830e6bSJacob Pan * @evt: fault event data
13920c830e6bSJacob Pan *
13930c830e6bSJacob Pan * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1394bf3255b3SJean-Philippe Brucker * handler. When this function fails and the fault is recoverable, it is the
1395bf3255b3SJean-Philippe Brucker * caller's responsibility to complete the fault.
13960c830e6bSJacob Pan *
13970c830e6bSJacob Pan * Return 0 on success, or an error.
13980c830e6bSJacob Pan */
iommu_report_device_fault(struct device * dev,struct iommu_fault_event * evt)13990c830e6bSJacob Pan int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
14000c830e6bSJacob Pan {
1401045a7042SJoerg Roedel struct dev_iommu *param = dev->iommu;
1402bf3255b3SJean-Philippe Brucker struct iommu_fault_event *evt_pending = NULL;
14030c830e6bSJacob Pan struct iommu_fault_param *fparam;
14040c830e6bSJacob Pan int ret = 0;
14050c830e6bSJacob Pan
14060c830e6bSJacob Pan if (!param || !evt)
14070c830e6bSJacob Pan return -EINVAL;
14080c830e6bSJacob Pan
14090c830e6bSJacob Pan /* we only report device fault if there is a handler registered */
14100c830e6bSJacob Pan mutex_lock(¶m->lock);
14110c830e6bSJacob Pan fparam = param->fault_param;
14120c830e6bSJacob Pan if (!fparam || !fparam->handler) {
14130c830e6bSJacob Pan ret = -EINVAL;
14140c830e6bSJacob Pan goto done_unlock;
14150c830e6bSJacob Pan }
1416bf3255b3SJean-Philippe Brucker
1417bf3255b3SJean-Philippe Brucker if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1418bf3255b3SJean-Philippe Brucker (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1419bf3255b3SJean-Philippe Brucker evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1420bf3255b3SJean-Philippe Brucker GFP_KERNEL);
1421bf3255b3SJean-Philippe Brucker if (!evt_pending) {
1422bf3255b3SJean-Philippe Brucker ret = -ENOMEM;
1423bf3255b3SJean-Philippe Brucker goto done_unlock;
1424bf3255b3SJean-Philippe Brucker }
1425bf3255b3SJean-Philippe Brucker mutex_lock(&fparam->lock);
1426bf3255b3SJean-Philippe Brucker list_add_tail(&evt_pending->list, &fparam->faults);
1427bf3255b3SJean-Philippe Brucker mutex_unlock(&fparam->lock);
1428bf3255b3SJean-Philippe Brucker }
1429bf3255b3SJean-Philippe Brucker
14300c830e6bSJacob Pan ret = fparam->handler(&evt->fault, fparam->data);
1431bf3255b3SJean-Philippe Brucker if (ret && evt_pending) {
1432bf3255b3SJean-Philippe Brucker mutex_lock(&fparam->lock);
1433bf3255b3SJean-Philippe Brucker list_del(&evt_pending->list);
1434bf3255b3SJean-Philippe Brucker mutex_unlock(&fparam->lock);
1435bf3255b3SJean-Philippe Brucker kfree(evt_pending);
1436bf3255b3SJean-Philippe Brucker }
14370c830e6bSJacob Pan done_unlock:
14380c830e6bSJacob Pan mutex_unlock(¶m->lock);
14390c830e6bSJacob Pan return ret;
14400c830e6bSJacob Pan }
14410c830e6bSJacob Pan EXPORT_SYMBOL_GPL(iommu_report_device_fault);
14420c830e6bSJacob Pan
iommu_page_response(struct device * dev,struct iommu_page_response * msg)1443bf3255b3SJean-Philippe Brucker int iommu_page_response(struct device *dev,
1444bf3255b3SJean-Philippe Brucker struct iommu_page_response *msg)
1445bf3255b3SJean-Philippe Brucker {
144697047191SJean-Philippe Brucker bool needs_pasid;
1447bf3255b3SJean-Philippe Brucker int ret = -EINVAL;
1448bf3255b3SJean-Philippe Brucker struct iommu_fault_event *evt;
1449bf3255b3SJean-Philippe Brucker struct iommu_fault_page_request *prm;
1450045a7042SJoerg Roedel struct dev_iommu *param = dev->iommu;
14513f6634d9SLu Baolu const struct iommu_ops *ops = dev_iommu_ops(dev);
145297047191SJean-Philippe Brucker bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1453bf3255b3SJean-Philippe Brucker
14543f6634d9SLu Baolu if (!ops->page_response)
1455bf3255b3SJean-Philippe Brucker return -ENODEV;
1456bf3255b3SJean-Philippe Brucker
1457bf3255b3SJean-Philippe Brucker if (!param || !param->fault_param)
1458bf3255b3SJean-Philippe Brucker return -EINVAL;
1459bf3255b3SJean-Philippe Brucker
1460bf3255b3SJean-Philippe Brucker if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1461bf3255b3SJean-Philippe Brucker msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1462bf3255b3SJean-Philippe Brucker return -EINVAL;
1463bf3255b3SJean-Philippe Brucker
1464bf3255b3SJean-Philippe Brucker /* Only send response if there is a fault report pending */
1465bf3255b3SJean-Philippe Brucker mutex_lock(¶m->fault_param->lock);
1466bf3255b3SJean-Philippe Brucker if (list_empty(¶m->fault_param->faults)) {
1467bf3255b3SJean-Philippe Brucker dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1468bf3255b3SJean-Philippe Brucker goto done_unlock;
1469bf3255b3SJean-Philippe Brucker }
1470bf3255b3SJean-Philippe Brucker /*
1471bf3255b3SJean-Philippe Brucker * Check if we have a matching page request pending to respond,
1472bf3255b3SJean-Philippe Brucker * otherwise return -EINVAL
1473bf3255b3SJean-Philippe Brucker */
1474bf3255b3SJean-Philippe Brucker list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1475bf3255b3SJean-Philippe Brucker prm = &evt->fault.prm;
147697047191SJean-Philippe Brucker if (prm->grpid != msg->grpid)
1477bf3255b3SJean-Philippe Brucker continue;
1478bf3255b3SJean-Philippe Brucker
147997047191SJean-Philippe Brucker /*
148097047191SJean-Philippe Brucker * If the PASID is required, the corresponding request is
148197047191SJean-Philippe Brucker * matched using the group ID, the PASID valid bit and the PASID
148297047191SJean-Philippe Brucker * value. Otherwise only the group ID matches request and
148397047191SJean-Philippe Brucker * response.
148497047191SJean-Philippe Brucker */
148597047191SJean-Philippe Brucker needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
148697047191SJean-Philippe Brucker if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
148797047191SJean-Philippe Brucker continue;
148897047191SJean-Philippe Brucker
148997047191SJean-Philippe Brucker if (!needs_pasid && has_pasid) {
149097047191SJean-Philippe Brucker /* No big deal, just clear it. */
149197047191SJean-Philippe Brucker msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
149297047191SJean-Philippe Brucker msg->pasid = 0;
149397047191SJean-Philippe Brucker }
1494bf3255b3SJean-Philippe Brucker
14953f6634d9SLu Baolu ret = ops->page_response(dev, evt, msg);
1496bf3255b3SJean-Philippe Brucker list_del(&evt->list);
1497bf3255b3SJean-Philippe Brucker kfree(evt);
1498bf3255b3SJean-Philippe Brucker break;
1499bf3255b3SJean-Philippe Brucker }
1500bf3255b3SJean-Philippe Brucker
1501bf3255b3SJean-Philippe Brucker done_unlock:
1502bf3255b3SJean-Philippe Brucker mutex_unlock(¶m->fault_param->lock);
1503bf3255b3SJean-Philippe Brucker return ret;
1504bf3255b3SJean-Philippe Brucker }
1505bf3255b3SJean-Philippe Brucker EXPORT_SYMBOL_GPL(iommu_page_response);
1506bf3255b3SJean-Philippe Brucker
15070c830e6bSJacob Pan /**
1508d72e31c9SAlex Williamson * iommu_group_id - Return ID for a group
1509d72e31c9SAlex Williamson * @group: the group to ID
1510d72e31c9SAlex Williamson *
1511d72e31c9SAlex Williamson * Return the unique ID for the group matching the sysfs group number.
1512d72e31c9SAlex Williamson */
iommu_group_id(struct iommu_group * group)1513d72e31c9SAlex Williamson int iommu_group_id(struct iommu_group *group)
1514d72e31c9SAlex Williamson {
1515d72e31c9SAlex Williamson return group->id;
1516d72e31c9SAlex Williamson }
1517d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_group_id);
15181460432cSAlex Williamson
1519f096c061SAlex Williamson static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1520f096c061SAlex Williamson unsigned long *devfns);
1521f096c061SAlex Williamson
1522104a1c13SAlex Williamson /*
1523104a1c13SAlex Williamson * To consider a PCI device isolated, we require ACS to support Source
1524104a1c13SAlex Williamson * Validation, Request Redirection, Completer Redirection, and Upstream
1525104a1c13SAlex Williamson * Forwarding. This effectively means that devices cannot spoof their
1526104a1c13SAlex Williamson * requester ID, requests and completions cannot be redirected, and all
1527104a1c13SAlex Williamson * transactions are forwarded upstream, even as it passes through a
1528104a1c13SAlex Williamson * bridge where the target device is downstream.
1529104a1c13SAlex Williamson */
1530104a1c13SAlex Williamson #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1531104a1c13SAlex Williamson
1532f096c061SAlex Williamson /*
1533f096c061SAlex Williamson * For multifunction devices which are not isolated from each other, find
1534f096c061SAlex Williamson * all the other non-isolated functions and look for existing groups. For
1535f096c061SAlex Williamson * each function, we also need to look for aliases to or from other devices
1536f096c061SAlex Williamson * that may already have a group.
1537f096c061SAlex Williamson */
get_pci_function_alias_group(struct pci_dev * pdev,unsigned long * devfns)1538f096c061SAlex Williamson static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1539f096c061SAlex Williamson unsigned long *devfns)
1540f096c061SAlex Williamson {
1541f096c061SAlex Williamson struct pci_dev *tmp = NULL;
1542f096c061SAlex Williamson struct iommu_group *group;
1543f096c061SAlex Williamson
1544f096c061SAlex Williamson if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1545f096c061SAlex Williamson return NULL;
1546f096c061SAlex Williamson
1547f096c061SAlex Williamson for_each_pci_dev(tmp) {
1548f096c061SAlex Williamson if (tmp == pdev || tmp->bus != pdev->bus ||
1549f096c061SAlex Williamson PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1550f096c061SAlex Williamson pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1551f096c061SAlex Williamson continue;
1552f096c061SAlex Williamson
1553f096c061SAlex Williamson group = get_pci_alias_group(tmp, devfns);
1554f096c061SAlex Williamson if (group) {
1555f096c061SAlex Williamson pci_dev_put(tmp);
1556f096c061SAlex Williamson return group;
1557f096c061SAlex Williamson }
1558f096c061SAlex Williamson }
1559f096c061SAlex Williamson
1560f096c061SAlex Williamson return NULL;
1561f096c061SAlex Williamson }
1562f096c061SAlex Williamson
1563f096c061SAlex Williamson /*
1564338c3149SJacek Lawrynowicz * Look for aliases to or from the given device for existing groups. DMA
1565338c3149SJacek Lawrynowicz * aliases are only supported on the same bus, therefore the search
1566f096c061SAlex Williamson * space is quite small (especially since we're really only looking at pcie
1567f096c061SAlex Williamson * device, and therefore only expect multiple slots on the root complex or
1568f096c061SAlex Williamson * downstream switch ports). It's conceivable though that a pair of
1569f096c061SAlex Williamson * multifunction devices could have aliases between them that would cause a
1570f096c061SAlex Williamson * loop. To prevent this, we use a bitmap to track where we've been.
1571f096c061SAlex Williamson */
get_pci_alias_group(struct pci_dev * pdev,unsigned long * devfns)1572f096c061SAlex Williamson static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1573f096c061SAlex Williamson unsigned long *devfns)
1574f096c061SAlex Williamson {
1575f096c061SAlex Williamson struct pci_dev *tmp = NULL;
1576f096c061SAlex Williamson struct iommu_group *group;
1577f096c061SAlex Williamson
1578f096c061SAlex Williamson if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1579f096c061SAlex Williamson return NULL;
1580f096c061SAlex Williamson
1581f096c061SAlex Williamson group = iommu_group_get(&pdev->dev);
1582f096c061SAlex Williamson if (group)
1583f096c061SAlex Williamson return group;
1584f096c061SAlex Williamson
1585f096c061SAlex Williamson for_each_pci_dev(tmp) {
1586f096c061SAlex Williamson if (tmp == pdev || tmp->bus != pdev->bus)
1587f096c061SAlex Williamson continue;
1588f096c061SAlex Williamson
1589f096c061SAlex Williamson /* We alias them or they alias us */
1590338c3149SJacek Lawrynowicz if (pci_devs_are_dma_aliases(pdev, tmp)) {
1591f096c061SAlex Williamson group = get_pci_alias_group(tmp, devfns);
1592f096c061SAlex Williamson if (group) {
1593f096c061SAlex Williamson pci_dev_put(tmp);
1594f096c061SAlex Williamson return group;
1595f096c061SAlex Williamson }
1596f096c061SAlex Williamson
1597f096c061SAlex Williamson group = get_pci_function_alias_group(tmp, devfns);
1598f096c061SAlex Williamson if (group) {
1599f096c061SAlex Williamson pci_dev_put(tmp);
1600f096c061SAlex Williamson return group;
1601f096c061SAlex Williamson }
1602f096c061SAlex Williamson }
1603f096c061SAlex Williamson }
1604f096c061SAlex Williamson
1605f096c061SAlex Williamson return NULL;
1606f096c061SAlex Williamson }
1607f096c061SAlex Williamson
1608104a1c13SAlex Williamson struct group_for_pci_data {
1609104a1c13SAlex Williamson struct pci_dev *pdev;
1610104a1c13SAlex Williamson struct iommu_group *group;
1611104a1c13SAlex Williamson };
1612104a1c13SAlex Williamson
1613104a1c13SAlex Williamson /*
1614104a1c13SAlex Williamson * DMA alias iterator callback, return the last seen device. Stop and return
1615104a1c13SAlex Williamson * the IOMMU group if we find one along the way.
1616104a1c13SAlex Williamson */
get_pci_alias_or_group(struct pci_dev * pdev,u16 alias,void * opaque)1617104a1c13SAlex Williamson static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1618104a1c13SAlex Williamson {
1619104a1c13SAlex Williamson struct group_for_pci_data *data = opaque;
1620104a1c13SAlex Williamson
1621104a1c13SAlex Williamson data->pdev = pdev;
1622104a1c13SAlex Williamson data->group = iommu_group_get(&pdev->dev);
1623104a1c13SAlex Williamson
1624104a1c13SAlex Williamson return data->group != NULL;
1625104a1c13SAlex Williamson }
1626104a1c13SAlex Williamson
1627104a1c13SAlex Williamson /*
16286eab556aSJoerg Roedel * Generic device_group call-back function. It just allocates one
16296eab556aSJoerg Roedel * iommu-group per device.
16306eab556aSJoerg Roedel */
generic_device_group(struct device * dev)16316eab556aSJoerg Roedel struct iommu_group *generic_device_group(struct device *dev)
16326eab556aSJoerg Roedel {
16337f7a2304SJoerg Roedel return iommu_group_alloc();
16346eab556aSJoerg Roedel }
1635a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(generic_device_group);
16366eab556aSJoerg Roedel
16376eab556aSJoerg Roedel /*
1638104a1c13SAlex Williamson * Use standard PCI bus topology, isolation features, and DMA alias quirks
1639104a1c13SAlex Williamson * to find or create an IOMMU group for a device.
1640104a1c13SAlex Williamson */
pci_device_group(struct device * dev)16415e62292bSJoerg Roedel struct iommu_group *pci_device_group(struct device *dev)
1642104a1c13SAlex Williamson {
16435e62292bSJoerg Roedel struct pci_dev *pdev = to_pci_dev(dev);
1644104a1c13SAlex Williamson struct group_for_pci_data data;
1645104a1c13SAlex Williamson struct pci_bus *bus;
1646104a1c13SAlex Williamson struct iommu_group *group = NULL;
1647f096c061SAlex Williamson u64 devfns[4] = { 0 };
1648104a1c13SAlex Williamson
16495e62292bSJoerg Roedel if (WARN_ON(!dev_is_pci(dev)))
16505e62292bSJoerg Roedel return ERR_PTR(-EINVAL);
16515e62292bSJoerg Roedel
1652104a1c13SAlex Williamson /*
1653104a1c13SAlex Williamson * Find the upstream DMA alias for the device. A device must not
1654104a1c13SAlex Williamson * be aliased due to topology in order to have its own IOMMU group.
1655104a1c13SAlex Williamson * If we find an alias along the way that already belongs to a
1656104a1c13SAlex Williamson * group, use it.
1657104a1c13SAlex Williamson */
1658104a1c13SAlex Williamson if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1659104a1c13SAlex Williamson return data.group;
1660104a1c13SAlex Williamson
1661104a1c13SAlex Williamson pdev = data.pdev;
1662104a1c13SAlex Williamson
1663104a1c13SAlex Williamson /*
1664104a1c13SAlex Williamson * Continue upstream from the point of minimum IOMMU granularity
1665104a1c13SAlex Williamson * due to aliases to the point where devices are protected from
1666104a1c13SAlex Williamson * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1667104a1c13SAlex Williamson * group, use it.
1668104a1c13SAlex Williamson */
1669104a1c13SAlex Williamson for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1670104a1c13SAlex Williamson if (!bus->self)
1671104a1c13SAlex Williamson continue;
1672104a1c13SAlex Williamson
1673104a1c13SAlex Williamson if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1674104a1c13SAlex Williamson break;
1675104a1c13SAlex Williamson
1676104a1c13SAlex Williamson pdev = bus->self;
1677104a1c13SAlex Williamson
1678104a1c13SAlex Williamson group = iommu_group_get(&pdev->dev);
1679104a1c13SAlex Williamson if (group)
1680104a1c13SAlex Williamson return group;
1681104a1c13SAlex Williamson }
1682104a1c13SAlex Williamson
1683104a1c13SAlex Williamson /*
1684f096c061SAlex Williamson * Look for existing groups on device aliases. If we alias another
1685f096c061SAlex Williamson * device or another device aliases us, use the same group.
1686104a1c13SAlex Williamson */
1687f096c061SAlex Williamson group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1688f096c061SAlex Williamson if (group)
1689104a1c13SAlex Williamson return group;
1690f096c061SAlex Williamson
1691f096c061SAlex Williamson /*
1692f096c061SAlex Williamson * Look for existing groups on non-isolated functions on the same
1693f096c061SAlex Williamson * slot and aliases of those funcions, if any. No need to clear
1694f096c061SAlex Williamson * the search bitmap, the tested devfns are still valid.
1695f096c061SAlex Williamson */
1696f096c061SAlex Williamson group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1697f096c061SAlex Williamson if (group)
1698f096c061SAlex Williamson return group;
1699104a1c13SAlex Williamson
1700104a1c13SAlex Williamson /* No shared group found, allocate new */
17017f7a2304SJoerg Roedel return iommu_group_alloc();
1702104a1c13SAlex Williamson }
1703a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(pci_device_group);
1704104a1c13SAlex Williamson
1705eab03e2aSNipun Gupta /* Get the IOMMU group for device on fsl-mc bus */
fsl_mc_device_group(struct device * dev)1706eab03e2aSNipun Gupta struct iommu_group *fsl_mc_device_group(struct device *dev)
1707eab03e2aSNipun Gupta {
1708eab03e2aSNipun Gupta struct device *cont_dev = fsl_mc_cont_dev(dev);
1709eab03e2aSNipun Gupta struct iommu_group *group;
1710eab03e2aSNipun Gupta
1711eab03e2aSNipun Gupta group = iommu_group_get(cont_dev);
1712eab03e2aSNipun Gupta if (!group)
1713eab03e2aSNipun Gupta group = iommu_group_alloc();
1714eab03e2aSNipun Gupta return group;
1715eab03e2aSNipun Gupta }
1716a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1717eab03e2aSNipun Gupta
iommu_get_def_domain_type(struct device * dev)17184cbf3851SSai Praneeth Prakhya static int iommu_get_def_domain_type(struct device *dev)
17194cbf3851SSai Praneeth Prakhya {
17203f6634d9SLu Baolu const struct iommu_ops *ops = dev_iommu_ops(dev);
172128b41e2cSLu Baolu
172228b41e2cSLu Baolu if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
172328b41e2cSLu Baolu return IOMMU_DOMAIN_DMA;
17244cbf3851SSai Praneeth Prakhya
17254cbf3851SSai Praneeth Prakhya if (ops->def_domain_type)
172628b41e2cSLu Baolu return ops->def_domain_type(dev);
17274cbf3851SSai Praneeth Prakhya
172828b41e2cSLu Baolu return 0;
17294cbf3851SSai Praneeth Prakhya }
17304cbf3851SSai Praneeth Prakhya
1731d99be00fSJason Gunthorpe static struct iommu_domain *
__iommu_group_alloc_default_domain(const struct bus_type * bus,struct iommu_group * group,int req_type)1732d99be00fSJason Gunthorpe __iommu_group_alloc_default_domain(const struct bus_type *bus,
1733d99be00fSJason Gunthorpe struct iommu_group *group, int req_type)
1734ff2a08b3SJoerg Roedel {
1735d99be00fSJason Gunthorpe if (group->default_domain && group->default_domain->type == req_type)
1736d99be00fSJason Gunthorpe return group->default_domain;
1737d99be00fSJason Gunthorpe return __iommu_domain_alloc(bus, req_type);
1738d99be00fSJason Gunthorpe }
1739d99be00fSJason Gunthorpe
1740fcbb0a4dSJason Gunthorpe /*
1741fcbb0a4dSJason Gunthorpe * req_type of 0 means "auto" which means to select a domain based on
1742fcbb0a4dSJason Gunthorpe * iommu_def_domain_type or what the driver actually supports.
1743fcbb0a4dSJason Gunthorpe */
1744fcbb0a4dSJason Gunthorpe static struct iommu_domain *
iommu_group_alloc_default_domain(struct iommu_group * group,int req_type)1745fcbb0a4dSJason Gunthorpe iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1746ff2a08b3SJoerg Roedel {
1747fcbb0a4dSJason Gunthorpe const struct bus_type *bus =
1748fcbb0a4dSJason Gunthorpe list_first_entry(&group->devices, struct group_device, list)
1749fcbb0a4dSJason Gunthorpe ->dev->bus;
1750ff2a08b3SJoerg Roedel struct iommu_domain *dom;
1751ff2a08b3SJoerg Roedel
1752fcbb0a4dSJason Gunthorpe lockdep_assert_held(&group->mutex);
1753fcbb0a4dSJason Gunthorpe
1754fcbb0a4dSJason Gunthorpe if (req_type)
1755d99be00fSJason Gunthorpe return __iommu_group_alloc_default_domain(bus, group, req_type);
1756fcbb0a4dSJason Gunthorpe
1757fcbb0a4dSJason Gunthorpe /* The driver gave no guidance on what type to use, try the default */
1758d99be00fSJason Gunthorpe dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type);
17596e1aa204SJoerg Roedel if (dom)
1760fcbb0a4dSJason Gunthorpe return dom;
1761ff2a08b3SJoerg Roedel
1762fcbb0a4dSJason Gunthorpe /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1763fcbb0a4dSJason Gunthorpe if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1764fcbb0a4dSJason Gunthorpe return NULL;
1765d99be00fSJason Gunthorpe dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA);
1766ff2a08b3SJoerg Roedel if (!dom)
1767fcbb0a4dSJason Gunthorpe return NULL;
1768ff2a08b3SJoerg Roedel
1769fcbb0a4dSJason Gunthorpe pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1770fcbb0a4dSJason Gunthorpe iommu_def_domain_type, group->name);
1771fcbb0a4dSJason Gunthorpe return dom;
17726e1aa204SJoerg Roedel }
17736e1aa204SJoerg Roedel
iommu_group_default_domain(struct iommu_group * group)17746827ca83SJoerg Roedel struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
17756827ca83SJoerg Roedel {
17766827ca83SJoerg Roedel return group->default_domain;
17776827ca83SJoerg Roedel }
17786827ca83SJoerg Roedel
probe_iommu_group(struct device * dev,void * data)1779deac0b3bSJoerg Roedel static int probe_iommu_group(struct device *dev, void *data)
17801460432cSAlex Williamson {
1781deac0b3bSJoerg Roedel struct list_head *group_list = data;
1782deac0b3bSJoerg Roedel int ret;
178338667f18SJoerg Roedel
1784c1114090SRobin Murphy mutex_lock(&iommu_probe_device_lock);
1785deac0b3bSJoerg Roedel ret = __iommu_probe_device(dev, group_list);
1786c1114090SRobin Murphy mutex_unlock(&iommu_probe_device_lock);
178738667f18SJoerg Roedel if (ret == -ENODEV)
178838667f18SJoerg Roedel ret = 0;
178938667f18SJoerg Roedel
179038667f18SJoerg Roedel return ret;
17911460432cSAlex Williamson }
17921460432cSAlex Williamson
iommu_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1793d72e31c9SAlex Williamson static int iommu_bus_notifier(struct notifier_block *nb,
17941460432cSAlex Williamson unsigned long action, void *data)
17951460432cSAlex Williamson {
1796cc5aed44SJoerg Roedel struct device *dev = data;
17971460432cSAlex Williamson
1798d72e31c9SAlex Williamson if (action == BUS_NOTIFY_ADD_DEVICE) {
17993ba8775fSzhichang.yuan int ret;
18003ba8775fSzhichang.yuan
1801cc5aed44SJoerg Roedel ret = iommu_probe_device(dev);
18023ba8775fSzhichang.yuan return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1803843cb6dcSJoerg Roedel } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1804cc5aed44SJoerg Roedel iommu_release_device(dev);
1805cc5aed44SJoerg Roedel return NOTIFY_OK;
1806d72e31c9SAlex Williamson }
18071460432cSAlex Williamson
18081460432cSAlex Williamson return 0;
18091460432cSAlex Williamson }
18101460432cSAlex Williamson
18118b4eb75eSJason Gunthorpe /* A target_type of 0 will select the best domain type and cannot fail */
iommu_get_default_domain_type(struct iommu_group * group,int target_type)18128b4eb75eSJason Gunthorpe static int iommu_get_default_domain_type(struct iommu_group *group,
18138b4eb75eSJason Gunthorpe int target_type)
1814deac0b3bSJoerg Roedel {
18158b4eb75eSJason Gunthorpe int best_type = target_type;
18168b4eb75eSJason Gunthorpe struct group_device *gdev;
18178b4eb75eSJason Gunthorpe struct device *last_dev;
1818deac0b3bSJoerg Roedel
18198b4eb75eSJason Gunthorpe lockdep_assert_held(&group->mutex);
18208b4eb75eSJason Gunthorpe
18218b4eb75eSJason Gunthorpe for_each_group_device(group, gdev) {
18228b4eb75eSJason Gunthorpe unsigned int type = iommu_get_def_domain_type(gdev->dev);
18238b4eb75eSJason Gunthorpe
18248b4eb75eSJason Gunthorpe if (best_type && type && best_type != type) {
18258b4eb75eSJason Gunthorpe if (target_type) {
18268b4eb75eSJason Gunthorpe dev_err_ratelimited(
18278b4eb75eSJason Gunthorpe gdev->dev,
18288b4eb75eSJason Gunthorpe "Device cannot be in %s domain\n",
18298b4eb75eSJason Gunthorpe iommu_domain_type_str(target_type));
18308b4eb75eSJason Gunthorpe return -1;
1831deac0b3bSJoerg Roedel }
1832deac0b3bSJoerg Roedel
18338b4eb75eSJason Gunthorpe dev_warn(
18348b4eb75eSJason Gunthorpe gdev->dev,
18358b4eb75eSJason Gunthorpe "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
18368b4eb75eSJason Gunthorpe iommu_domain_type_str(type), dev_name(last_dev),
18378b4eb75eSJason Gunthorpe iommu_domain_type_str(best_type));
1838deac0b3bSJoerg Roedel return 0;
1839deac0b3bSJoerg Roedel }
18408b4eb75eSJason Gunthorpe if (!best_type)
18418b4eb75eSJason Gunthorpe best_type = type;
18428b4eb75eSJason Gunthorpe last_dev = gdev->dev;
1843deac0b3bSJoerg Roedel }
18448b4eb75eSJason Gunthorpe return best_type;
1845deac0b3bSJoerg Roedel }
1846deac0b3bSJoerg Roedel
iommu_group_do_probe_finalize(struct device * dev)1847e996c12dSJason Gunthorpe static void iommu_group_do_probe_finalize(struct device *dev)
184870b8170eSJoerg Roedel {
18493f6634d9SLu Baolu const struct iommu_ops *ops = dev_iommu_ops(dev);
185070b8170eSJoerg Roedel
18513f6634d9SLu Baolu if (ops->probe_finalize)
18523f6634d9SLu Baolu ops->probe_finalize(dev);
1853ce574c27SJoerg Roedel }
1854ce574c27SJoerg Roedel
bus_iommu_probe(const struct bus_type * bus)1855b18d0a0fSGreg Kroah-Hartman int bus_iommu_probe(const struct bus_type *bus)
1856deac0b3bSJoerg Roedel {
1857deac0b3bSJoerg Roedel struct iommu_group *group, *next;
1858deac0b3bSJoerg Roedel LIST_HEAD(group_list);
18593eeeb45cSJoerg Roedel int ret;
1860deac0b3bSJoerg Roedel
1861deac0b3bSJoerg Roedel ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1862deac0b3bSJoerg Roedel if (ret)
1863deac0b3bSJoerg Roedel return ret;
1864deac0b3bSJoerg Roedel
1865deac0b3bSJoerg Roedel list_for_each_entry_safe(group, next, &group_list, entry) {
1866e996c12dSJason Gunthorpe struct group_device *gdev;
1867e996c12dSJason Gunthorpe
186801657bc1SRobin Murphy mutex_lock(&group->mutex);
186901657bc1SRobin Murphy
1870deac0b3bSJoerg Roedel /* Remove item from the list */
1871deac0b3bSJoerg Roedel list_del_init(&group->entry);
1872deac0b3bSJoerg Roedel
1873f1880563SJason Gunthorpe /*
1874f1880563SJason Gunthorpe * We go to the trouble of deferred default domain creation so
1875f1880563SJason Gunthorpe * that the cross-group default domain type and the setup of the
1876f1880563SJason Gunthorpe * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
1877f1880563SJason Gunthorpe */
1878d99be00fSJason Gunthorpe ret = iommu_setup_default_domain(group, 0);
1879d99be00fSJason Gunthorpe if (ret) {
1880deac0b3bSJoerg Roedel mutex_unlock(&group->mutex);
1881deac0b3bSJoerg Roedel return ret;
1882deac0b3bSJoerg Roedel }
1883deac0b3bSJoerg Roedel mutex_unlock(&group->mutex);
1884e996c12dSJason Gunthorpe
1885e996c12dSJason Gunthorpe /*
1886e996c12dSJason Gunthorpe * FIXME: Mis-locked because the ops->probe_finalize() call-back
1887e996c12dSJason Gunthorpe * of some IOMMU drivers calls arm_iommu_attach_device() which
1888e996c12dSJason Gunthorpe * in-turn might call back into IOMMU core code, where it tries
1889e996c12dSJason Gunthorpe * to take group->mutex, resulting in a deadlock.
1890e996c12dSJason Gunthorpe */
1891e996c12dSJason Gunthorpe for_each_group_device(group, gdev)
1892e996c12dSJason Gunthorpe iommu_group_do_probe_finalize(gdev->dev);
1893deac0b3bSJoerg Roedel }
1894deac0b3bSJoerg Roedel
1895d99be00fSJason Gunthorpe return 0;
1896deac0b3bSJoerg Roedel }
1897deac0b3bSJoerg Roedel
iommu_present(const struct bus_type * bus)1898b18d0a0fSGreg Kroah-Hartman bool iommu_present(const struct bus_type *bus)
1899ab493a0fSOhad Ben-Cohen {
190094441c3bSJoerg Roedel return bus->iommu_ops != NULL;
1901ab493a0fSOhad Ben-Cohen }
1902a1b60c1cSJoerg Roedel EXPORT_SYMBOL_GPL(iommu_present);
1903ab493a0fSOhad Ben-Cohen
1904ed36d04eSRobin Murphy /**
1905ed36d04eSRobin Murphy * device_iommu_capable() - check for a general IOMMU capability
1906ed36d04eSRobin Murphy * @dev: device to which the capability would be relevant, if available
1907ed36d04eSRobin Murphy * @cap: IOMMU capability
1908ed36d04eSRobin Murphy *
1909ed36d04eSRobin Murphy * Return: true if an IOMMU is present and supports the given capability
1910ed36d04eSRobin Murphy * for the given device, otherwise false.
1911ed36d04eSRobin Murphy */
device_iommu_capable(struct device * dev,enum iommu_cap cap)1912ed36d04eSRobin Murphy bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
1913ed36d04eSRobin Murphy {
1914ed36d04eSRobin Murphy const struct iommu_ops *ops;
1915ed36d04eSRobin Murphy
1916ed36d04eSRobin Murphy if (!dev->iommu || !dev->iommu->iommu_dev)
1917ed36d04eSRobin Murphy return false;
1918ed36d04eSRobin Murphy
1919ed36d04eSRobin Murphy ops = dev_iommu_ops(dev);
1920ed36d04eSRobin Murphy if (!ops->capable)
1921ed36d04eSRobin Murphy return false;
1922ed36d04eSRobin Murphy
1923359ad157SRobin Murphy return ops->capable(dev, cap);
1924ed36d04eSRobin Murphy }
1925ed36d04eSRobin Murphy EXPORT_SYMBOL_GPL(device_iommu_capable);
1926ed36d04eSRobin Murphy
19274f3f8d9dSOhad Ben-Cohen /**
1928efc30a8fSJason Gunthorpe * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
1929efc30a8fSJason Gunthorpe * for a group
1930efc30a8fSJason Gunthorpe * @group: Group to query
1931efc30a8fSJason Gunthorpe *
1932efc30a8fSJason Gunthorpe * IOMMU groups should not have differing values of
1933efc30a8fSJason Gunthorpe * msi_device_has_isolated_msi() for devices in a group. However nothing
1934efc30a8fSJason Gunthorpe * directly prevents this, so ensure mistakes don't result in isolation failures
1935efc30a8fSJason Gunthorpe * by checking that all the devices are the same.
1936efc30a8fSJason Gunthorpe */
iommu_group_has_isolated_msi(struct iommu_group * group)1937efc30a8fSJason Gunthorpe bool iommu_group_has_isolated_msi(struct iommu_group *group)
1938efc30a8fSJason Gunthorpe {
1939efc30a8fSJason Gunthorpe struct group_device *group_dev;
1940efc30a8fSJason Gunthorpe bool ret = true;
1941efc30a8fSJason Gunthorpe
1942efc30a8fSJason Gunthorpe mutex_lock(&group->mutex);
19433006b15bSJason Gunthorpe for_each_group_device(group, group_dev)
1944b062007cSJason Gunthorpe ret &= msi_device_has_isolated_msi(group_dev->dev);
1945efc30a8fSJason Gunthorpe mutex_unlock(&group->mutex);
1946efc30a8fSJason Gunthorpe return ret;
1947efc30a8fSJason Gunthorpe }
1948efc30a8fSJason Gunthorpe EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
1949efc30a8fSJason Gunthorpe
1950efc30a8fSJason Gunthorpe /**
19514f3f8d9dSOhad Ben-Cohen * iommu_set_fault_handler() - set a fault handler for an iommu domain
19524f3f8d9dSOhad Ben-Cohen * @domain: iommu domain
19534f3f8d9dSOhad Ben-Cohen * @handler: fault handler
195477ca2332SOhad Ben-Cohen * @token: user data, will be passed back to the fault handler
19550ed6d2d2SOhad Ben-Cohen *
19560ed6d2d2SOhad Ben-Cohen * This function should be used by IOMMU users which want to be notified
19570ed6d2d2SOhad Ben-Cohen * whenever an IOMMU fault happens.
19580ed6d2d2SOhad Ben-Cohen *
19590ed6d2d2SOhad Ben-Cohen * The fault handler itself should return 0 on success, and an appropriate
19600ed6d2d2SOhad Ben-Cohen * error code otherwise.
19614f3f8d9dSOhad Ben-Cohen */
iommu_set_fault_handler(struct iommu_domain * domain,iommu_fault_handler_t handler,void * token)19624f3f8d9dSOhad Ben-Cohen void iommu_set_fault_handler(struct iommu_domain *domain,
196377ca2332SOhad Ben-Cohen iommu_fault_handler_t handler,
196477ca2332SOhad Ben-Cohen void *token)
19654f3f8d9dSOhad Ben-Cohen {
19664f3f8d9dSOhad Ben-Cohen BUG_ON(!domain);
19674f3f8d9dSOhad Ben-Cohen
19684f3f8d9dSOhad Ben-Cohen domain->handler = handler;
196977ca2332SOhad Ben-Cohen domain->handler_token = token;
19704f3f8d9dSOhad Ben-Cohen }
197130bd918cSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
19724f3f8d9dSOhad Ben-Cohen
__iommu_domain_alloc(const struct bus_type * bus,unsigned type)1973b18d0a0fSGreg Kroah-Hartman static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
197453723dc5SJoerg Roedel unsigned type)
1975ab493a0fSOhad Ben-Cohen {
1976ab493a0fSOhad Ben-Cohen struct iommu_domain *domain;
1977a4fdd976SRobin Murphy unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
1978ab493a0fSOhad Ben-Cohen
197994441c3bSJoerg Roedel if (bus == NULL || bus->iommu_ops == NULL)
1980905d66c1SJoerg Roedel return NULL;
1981905d66c1SJoerg Roedel
1982a4fdd976SRobin Murphy domain = bus->iommu_ops->domain_alloc(alloc_type);
1983ab493a0fSOhad Ben-Cohen if (!domain)
1984ab493a0fSOhad Ben-Cohen return NULL;
1985ab493a0fSOhad Ben-Cohen
198653723dc5SJoerg Roedel domain->type = type;
19878f880d19SJerry Snitselaar /*
19888f880d19SJerry Snitselaar * If not already set, assume all sizes by default; the driver
19898f880d19SJerry Snitselaar * may override this later
19908f880d19SJerry Snitselaar */
19918f880d19SJerry Snitselaar if (!domain->pgsize_bitmap)
1992d16e0faaSRobin Murphy domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
19938f880d19SJerry Snitselaar
19949a630a4bSLu Baolu if (!domain->ops)
19959a630a4bSLu Baolu domain->ops = bus->iommu_ops->default_domain_ops;
1996905d66c1SJoerg Roedel
19973d31d4e7SRobin Murphy if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {
199846983fcdSRobin Murphy iommu_domain_free(domain);
199946983fcdSRobin Murphy domain = NULL;
200046983fcdSRobin Murphy }
2001ab493a0fSOhad Ben-Cohen return domain;
2002ab493a0fSOhad Ben-Cohen }
2003ab493a0fSOhad Ben-Cohen
iommu_domain_alloc(const struct bus_type * bus)2004b18d0a0fSGreg Kroah-Hartman struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
200553723dc5SJoerg Roedel {
200653723dc5SJoerg Roedel return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
2007ab493a0fSOhad Ben-Cohen }
2008ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_domain_alloc);
2009ab493a0fSOhad Ben-Cohen
iommu_domain_free(struct iommu_domain * domain)2010ab493a0fSOhad Ben-Cohen void iommu_domain_free(struct iommu_domain *domain)
2011ab493a0fSOhad Ben-Cohen {
201213646796SLu Baolu if (domain->type == IOMMU_DOMAIN_SVA)
201313646796SLu Baolu mmdrop(domain->mm);
201446983fcdSRobin Murphy iommu_put_dma_cookie(domain);
20159a630a4bSLu Baolu domain->ops->free(domain);
2016ab493a0fSOhad Ben-Cohen }
2017ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_domain_free);
2018ab493a0fSOhad Ben-Cohen
20190286300eSJason Gunthorpe via iommu /*
20200286300eSJason Gunthorpe via iommu * Put the group's domain back to the appropriate core-owned domain - either the
20210286300eSJason Gunthorpe via iommu * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
20220286300eSJason Gunthorpe via iommu */
__iommu_group_set_core_domain(struct iommu_group * group)20230286300eSJason Gunthorpe via iommu static void __iommu_group_set_core_domain(struct iommu_group *group)
20240286300eSJason Gunthorpe via iommu {
20250286300eSJason Gunthorpe via iommu struct iommu_domain *new_domain;
20260286300eSJason Gunthorpe via iommu
20270286300eSJason Gunthorpe via iommu if (group->owner)
20280286300eSJason Gunthorpe via iommu new_domain = group->blocking_domain;
20290286300eSJason Gunthorpe via iommu else
20300286300eSJason Gunthorpe via iommu new_domain = group->default_domain;
20310286300eSJason Gunthorpe via iommu
2032dcf40ed3SJason Gunthorpe __iommu_group_set_domain_nofail(group, new_domain);
20330286300eSJason Gunthorpe via iommu }
20340286300eSJason Gunthorpe via iommu
__iommu_attach_device(struct iommu_domain * domain,struct device * dev)2035426a2738SJoerg Roedel static int __iommu_attach_device(struct iommu_domain *domain,
2036426a2738SJoerg Roedel struct device *dev)
2037ab493a0fSOhad Ben-Cohen {
2038b54db778SShuah Khan int ret;
2039e01d1913SBaoquan He
2040e5aa7f00SJoerg Roedel if (unlikely(domain->ops->attach_dev == NULL))
2041e5aa7f00SJoerg Roedel return -ENODEV;
2042e5aa7f00SJoerg Roedel
2043b54db778SShuah Khan ret = domain->ops->attach_dev(domain, dev);
2044dd8a25c5SJason Gunthorpe if (ret)
2045b54db778SShuah Khan return ret;
2046dd8a25c5SJason Gunthorpe dev->iommu->attach_deferred = 0;
2047dd8a25c5SJason Gunthorpe trace_attach_device_to_domain(dev);
2048dd8a25c5SJason Gunthorpe return 0;
2049ab493a0fSOhad Ben-Cohen }
2050426a2738SJoerg Roedel
205100208852SNicolin Chen /**
205200208852SNicolin Chen * iommu_attach_device - Attach an IOMMU domain to a device
205300208852SNicolin Chen * @domain: IOMMU domain to attach
205400208852SNicolin Chen * @dev: Device that will be attached
205500208852SNicolin Chen *
205600208852SNicolin Chen * Returns 0 on success and error code on failure
205700208852SNicolin Chen *
205800208852SNicolin Chen * Note that EINVAL can be treated as a soft failure, indicating
205900208852SNicolin Chen * that certain configuration of the domain is incompatible with
206000208852SNicolin Chen * the device. In this case attaching a different domain to the
206100208852SNicolin Chen * device may succeed.
206200208852SNicolin Chen */
iommu_attach_device(struct iommu_domain * domain,struct device * dev)2063426a2738SJoerg Roedel int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
2064426a2738SJoerg Roedel {
2065426a2738SJoerg Roedel struct iommu_group *group;
2066426a2738SJoerg Roedel int ret;
2067426a2738SJoerg Roedel
2068426a2738SJoerg Roedel group = iommu_group_get(dev);
20699ae9df03SJordan Crouse if (!group)
20709ae9df03SJordan Crouse return -ENODEV;
20719ae9df03SJordan Crouse
2072426a2738SJoerg Roedel /*
207305f80300SRobin Murphy * Lock the group to make sure the device-count doesn't
2074426a2738SJoerg Roedel * change while we are attaching
2075426a2738SJoerg Roedel */
2076426a2738SJoerg Roedel mutex_lock(&group->mutex);
2077426a2738SJoerg Roedel ret = -EINVAL;
20784db0e5f8SJason Gunthorpe if (list_count_nodes(&group->devices) != 1)
2079426a2738SJoerg Roedel goto out_unlock;
2080426a2738SJoerg Roedel
2081e39cb8a3SJoerg Roedel ret = __iommu_attach_group(domain, group);
2082426a2738SJoerg Roedel
2083426a2738SJoerg Roedel out_unlock:
2084426a2738SJoerg Roedel mutex_unlock(&group->mutex);
2085426a2738SJoerg Roedel iommu_group_put(group);
2086426a2738SJoerg Roedel
2087426a2738SJoerg Roedel return ret;
2088426a2738SJoerg Roedel }
2089ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_attach_device);
2090ab493a0fSOhad Ben-Cohen
iommu_deferred_attach(struct device * dev,struct iommu_domain * domain)20913ab65729SLianbo Jiang int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
20923ab65729SLianbo Jiang {
2093dd8a25c5SJason Gunthorpe if (dev->iommu && dev->iommu->attach_deferred)
20943ab65729SLianbo Jiang return __iommu_attach_device(domain, dev);
20953ab65729SLianbo Jiang
20963ab65729SLianbo Jiang return 0;
20973ab65729SLianbo Jiang }
20983ab65729SLianbo Jiang
iommu_detach_device(struct iommu_domain * domain,struct device * dev)2099426a2738SJoerg Roedel void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2100426a2738SJoerg Roedel {
2101426a2738SJoerg Roedel struct iommu_group *group;
2102426a2738SJoerg Roedel
2103426a2738SJoerg Roedel group = iommu_group_get(dev);
21049ae9df03SJordan Crouse if (!group)
21059ae9df03SJordan Crouse return;
2106426a2738SJoerg Roedel
2107426a2738SJoerg Roedel mutex_lock(&group->mutex);
21080286300eSJason Gunthorpe via iommu if (WARN_ON(domain != group->domain) ||
21094db0e5f8SJason Gunthorpe WARN_ON(list_count_nodes(&group->devices) != 1))
2110426a2738SJoerg Roedel goto out_unlock;
21110286300eSJason Gunthorpe via iommu __iommu_group_set_core_domain(group);
2112426a2738SJoerg Roedel
2113426a2738SJoerg Roedel out_unlock:
2114426a2738SJoerg Roedel mutex_unlock(&group->mutex);
2115426a2738SJoerg Roedel iommu_group_put(group);
2116426a2738SJoerg Roedel }
2117ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_detach_device);
2118ab493a0fSOhad Ben-Cohen
iommu_get_domain_for_dev(struct device * dev)21192c1296d9SJoerg Roedel struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
21202c1296d9SJoerg Roedel {
21212c1296d9SJoerg Roedel struct iommu_domain *domain;
21222c1296d9SJoerg Roedel struct iommu_group *group;
21232c1296d9SJoerg Roedel
21242c1296d9SJoerg Roedel group = iommu_group_get(dev);
21251464d0b1SRobin Murphy if (!group)
21262c1296d9SJoerg Roedel return NULL;
21272c1296d9SJoerg Roedel
21282c1296d9SJoerg Roedel domain = group->domain;
21292c1296d9SJoerg Roedel
21302c1296d9SJoerg Roedel iommu_group_put(group);
21312c1296d9SJoerg Roedel
21322c1296d9SJoerg Roedel return domain;
21332c1296d9SJoerg Roedel }
21342c1296d9SJoerg Roedel EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
21352c1296d9SJoerg Roedel
2136d72e31c9SAlex Williamson /*
21376af588feSRobin Murphy * For IOMMU_DOMAIN_DMA implementations which already provide their own
21386af588feSRobin Murphy * guarantees that the group and its default domain are valid and correct.
21396af588feSRobin Murphy */
iommu_get_dma_domain(struct device * dev)21406af588feSRobin Murphy struct iommu_domain *iommu_get_dma_domain(struct device *dev)
21416af588feSRobin Murphy {
21426af588feSRobin Murphy return dev->iommu_group->default_domain;
21436af588feSRobin Murphy }
21446af588feSRobin Murphy
__iommu_attach_group(struct iommu_domain * domain,struct iommu_group * group)2145e39cb8a3SJoerg Roedel static int __iommu_attach_group(struct iommu_domain *domain,
2146e39cb8a3SJoerg Roedel struct iommu_group *group)
2147e39cb8a3SJoerg Roedel {
21480286300eSJason Gunthorpe via iommu if (group->domain && group->domain != group->default_domain &&
21490286300eSJason Gunthorpe via iommu group->domain != group->blocking_domain)
2150e39cb8a3SJoerg Roedel return -EBUSY;
2151e39cb8a3SJoerg Roedel
2152ecd60dc5SJason Gunthorpe return __iommu_group_set_domain(group, domain);
2153d72e31c9SAlex Williamson }
2154d72e31c9SAlex Williamson
215500208852SNicolin Chen /**
215600208852SNicolin Chen * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
215700208852SNicolin Chen * @domain: IOMMU domain to attach
215800208852SNicolin Chen * @group: IOMMU group that will be attached
215900208852SNicolin Chen *
216000208852SNicolin Chen * Returns 0 on success and error code on failure
216100208852SNicolin Chen *
216200208852SNicolin Chen * Note that EINVAL can be treated as a soft failure, indicating
216300208852SNicolin Chen * that certain configuration of the domain is incompatible with
216400208852SNicolin Chen * the group. In this case attaching a different domain to the
216500208852SNicolin Chen * group may succeed.
216600208852SNicolin Chen */
iommu_attach_group(struct iommu_domain * domain,struct iommu_group * group)2167d72e31c9SAlex Williamson int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2168d72e31c9SAlex Williamson {
2169e39cb8a3SJoerg Roedel int ret;
2170e39cb8a3SJoerg Roedel
2171e39cb8a3SJoerg Roedel mutex_lock(&group->mutex);
2172e39cb8a3SJoerg Roedel ret = __iommu_attach_group(domain, group);
2173e39cb8a3SJoerg Roedel mutex_unlock(&group->mutex);
2174e39cb8a3SJoerg Roedel
2175e39cb8a3SJoerg Roedel return ret;
2176d72e31c9SAlex Williamson }
2177d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_attach_group);
2178d72e31c9SAlex Williamson
2179addb6659SNicolin Chen /**
2180addb6659SNicolin Chen * iommu_group_replace_domain - replace the domain that a group is attached to
2181addb6659SNicolin Chen * @new_domain: new IOMMU domain to replace with
2182addb6659SNicolin Chen * @group: IOMMU group that will be attached to the new domain
2183addb6659SNicolin Chen *
2184addb6659SNicolin Chen * This API allows the group to switch domains without being forced to go to
2185addb6659SNicolin Chen * the blocking domain in-between.
2186addb6659SNicolin Chen *
2187addb6659SNicolin Chen * If the currently attached domain is a core domain (e.g. a default_domain),
2188addb6659SNicolin Chen * it will act just like the iommu_attach_group().
2189addb6659SNicolin Chen */
iommu_group_replace_domain(struct iommu_group * group,struct iommu_domain * new_domain)2190addb6659SNicolin Chen int iommu_group_replace_domain(struct iommu_group *group,
2191addb6659SNicolin Chen struct iommu_domain *new_domain)
2192addb6659SNicolin Chen {
2193addb6659SNicolin Chen int ret;
2194addb6659SNicolin Chen
2195addb6659SNicolin Chen if (!new_domain)
2196addb6659SNicolin Chen return -EINVAL;
2197addb6659SNicolin Chen
2198addb6659SNicolin Chen mutex_lock(&group->mutex);
2199addb6659SNicolin Chen ret = __iommu_group_set_domain(group, new_domain);
2200addb6659SNicolin Chen mutex_unlock(&group->mutex);
2201addb6659SNicolin Chen return ret;
2202addb6659SNicolin Chen }
2203addb6659SNicolin Chen EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
2204addb6659SNicolin Chen
__iommu_device_set_domain(struct iommu_group * group,struct device * dev,struct iommu_domain * new_domain,unsigned int flags)2205dcf40ed3SJason Gunthorpe static int __iommu_device_set_domain(struct iommu_group *group,
2206dcf40ed3SJason Gunthorpe struct device *dev,
2207dcf40ed3SJason Gunthorpe struct iommu_domain *new_domain,
2208dcf40ed3SJason Gunthorpe unsigned int flags)
2209d72e31c9SAlex Williamson {
2210dcf40ed3SJason Gunthorpe int ret;
2211d72e31c9SAlex Williamson
2212a48ce36eSLu Baolu /*
2213a48ce36eSLu Baolu * If the device requires IOMMU_RESV_DIRECT then we cannot allow
2214a48ce36eSLu Baolu * the blocking domain to be attached as it does not contain the
2215a48ce36eSLu Baolu * required 1:1 mapping. This test effectively excludes the device
2216a48ce36eSLu Baolu * being used with iommu_group_claim_dma_owner() which will block
2217a48ce36eSLu Baolu * vfio and iommufd as well.
2218a48ce36eSLu Baolu */
2219a48ce36eSLu Baolu if (dev->iommu->require_direct &&
2220a48ce36eSLu Baolu (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2221a48ce36eSLu Baolu new_domain == group->blocking_domain)) {
2222a48ce36eSLu Baolu dev_warn(dev,
2223a48ce36eSLu Baolu "Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");
2224a48ce36eSLu Baolu return -EINVAL;
2225a48ce36eSLu Baolu }
2226a48ce36eSLu Baolu
2227d257344cSJason Gunthorpe if (dev->iommu->attach_deferred) {
2228d257344cSJason Gunthorpe if (new_domain == group->default_domain)
2229d257344cSJason Gunthorpe return 0;
2230d257344cSJason Gunthorpe dev->iommu->attach_deferred = 0;
2231d257344cSJason Gunthorpe }
2232d72e31c9SAlex Williamson
2233dcf40ed3SJason Gunthorpe ret = __iommu_attach_device(new_domain, dev);
2234dcf40ed3SJason Gunthorpe if (ret) {
2235dcf40ed3SJason Gunthorpe /*
2236dcf40ed3SJason Gunthorpe * If we have a blocking domain then try to attach that in hopes
2237dcf40ed3SJason Gunthorpe * of avoiding a UAF. Modern drivers should implement blocking
2238dcf40ed3SJason Gunthorpe * domains as global statics that cannot fail.
2239dcf40ed3SJason Gunthorpe */
2240dcf40ed3SJason Gunthorpe if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2241dcf40ed3SJason Gunthorpe group->blocking_domain &&
2242dcf40ed3SJason Gunthorpe group->blocking_domain != new_domain)
2243dcf40ed3SJason Gunthorpe __iommu_attach_device(group->blocking_domain, dev);
2244dcf40ed3SJason Gunthorpe return ret;
2245dcf40ed3SJason Gunthorpe }
2246d72e31c9SAlex Williamson return 0;
2247d72e31c9SAlex Williamson }
2248d72e31c9SAlex Williamson
2249dcf40ed3SJason Gunthorpe /*
2250dcf40ed3SJason Gunthorpe * If 0 is returned the group's domain is new_domain. If an error is returned
2251dcf40ed3SJason Gunthorpe * then the group's domain will be set back to the existing domain unless
2252dcf40ed3SJason Gunthorpe * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2253dcf40ed3SJason Gunthorpe * domains is left inconsistent. This is a driver bug to fail attach with a
2254dcf40ed3SJason Gunthorpe * previously good domain. We try to avoid a kernel UAF because of this.
2255dcf40ed3SJason Gunthorpe *
2256dcf40ed3SJason Gunthorpe * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2257dcf40ed3SJason Gunthorpe * API works on domains and devices. Bridge that gap by iterating over the
2258dcf40ed3SJason Gunthorpe * devices in a group. Ideally we'd have a single device which represents the
2259dcf40ed3SJason Gunthorpe * requestor ID of the group, but we also allow IOMMU drivers to create policy
2260dcf40ed3SJason Gunthorpe * defined minimum sets, where the physical hardware may be able to distiguish
2261dcf40ed3SJason Gunthorpe * members, but we wish to group them at a higher level (ex. untrusted
2262dcf40ed3SJason Gunthorpe * multi-function PCI devices). Thus we attach each device.
2263dcf40ed3SJason Gunthorpe */
__iommu_group_set_domain_internal(struct iommu_group * group,struct iommu_domain * new_domain,unsigned int flags)2264dcf40ed3SJason Gunthorpe static int __iommu_group_set_domain_internal(struct iommu_group *group,
2265dcf40ed3SJason Gunthorpe struct iommu_domain *new_domain,
2266dcf40ed3SJason Gunthorpe unsigned int flags)
2267e39cb8a3SJoerg Roedel {
2268dcf40ed3SJason Gunthorpe struct group_device *last_gdev;
2269dcf40ed3SJason Gunthorpe struct group_device *gdev;
2270dcf40ed3SJason Gunthorpe int result;
2271e39cb8a3SJoerg Roedel int ret;
2272e39cb8a3SJoerg Roedel
2273dcf40ed3SJason Gunthorpe lockdep_assert_held(&group->mutex);
2274dcf40ed3SJason Gunthorpe
22750286300eSJason Gunthorpe via iommu if (group->domain == new_domain)
22760286300eSJason Gunthorpe via iommu return 0;
22770286300eSJason Gunthorpe via iommu
22781ea2a07aSLu Baolu /*
22798f9930faSLu Baolu * New drivers should support default domains, so set_platform_dma()
22808f9930faSLu Baolu * op will never be called. Otherwise the NULL domain represents some
22810286300eSJason Gunthorpe via iommu * platform specific behavior.
22821ea2a07aSLu Baolu */
22830286300eSJason Gunthorpe via iommu if (!new_domain) {
2284dcf40ed3SJason Gunthorpe for_each_group_device(group, gdev) {
2285dcf40ed3SJason Gunthorpe const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);
2286dcf40ed3SJason Gunthorpe
2287dcf40ed3SJason Gunthorpe if (!WARN_ON(!ops->set_platform_dma_ops))
2288dcf40ed3SJason Gunthorpe ops->set_platform_dma_ops(gdev->dev);
2289dcf40ed3SJason Gunthorpe }
2290e39cb8a3SJoerg Roedel group->domain = NULL;
22910286300eSJason Gunthorpe via iommu return 0;
2292e39cb8a3SJoerg Roedel }
2293e39cb8a3SJoerg Roedel
22940286300eSJason Gunthorpe via iommu /*
22950286300eSJason Gunthorpe via iommu * Changing the domain is done by calling attach_dev() on the new
22960286300eSJason Gunthorpe via iommu * domain. This switch does not have to be atomic and DMA can be
22970286300eSJason Gunthorpe via iommu * discarded during the transition. DMA must only be able to access
22980286300eSJason Gunthorpe via iommu * either new_domain or group->domain, never something else.
22990286300eSJason Gunthorpe via iommu */
2300dcf40ed3SJason Gunthorpe result = 0;
2301dcf40ed3SJason Gunthorpe for_each_group_device(group, gdev) {
2302dcf40ed3SJason Gunthorpe ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2303dcf40ed3SJason Gunthorpe flags);
2304dcf40ed3SJason Gunthorpe if (ret) {
2305dcf40ed3SJason Gunthorpe result = ret;
2306dcf40ed3SJason Gunthorpe /*
2307dcf40ed3SJason Gunthorpe * Keep trying the other devices in the group. If a
2308dcf40ed3SJason Gunthorpe * driver fails attach to an otherwise good domain, and
2309dcf40ed3SJason Gunthorpe * does not support blocking domains, it should at least
2310dcf40ed3SJason Gunthorpe * drop its reference on the current domain so we don't
2311dcf40ed3SJason Gunthorpe * UAF.
2312dcf40ed3SJason Gunthorpe */
2313dcf40ed3SJason Gunthorpe if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2314dcf40ed3SJason Gunthorpe continue;
2315dcf40ed3SJason Gunthorpe goto err_revert;
2316dcf40ed3SJason Gunthorpe }
2317dcf40ed3SJason Gunthorpe }
23180286300eSJason Gunthorpe via iommu group->domain = new_domain;
2319dcf40ed3SJason Gunthorpe return result;
2320dcf40ed3SJason Gunthorpe
2321dcf40ed3SJason Gunthorpe err_revert:
2322dcf40ed3SJason Gunthorpe /*
2323dcf40ed3SJason Gunthorpe * This is called in error unwind paths. A well behaved driver should
2324dcf40ed3SJason Gunthorpe * always allow us to attach to a domain that was already attached.
2325dcf40ed3SJason Gunthorpe */
2326dcf40ed3SJason Gunthorpe last_gdev = gdev;
2327dcf40ed3SJason Gunthorpe for_each_group_device(group, gdev) {
2328dcf40ed3SJason Gunthorpe const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);
2329dcf40ed3SJason Gunthorpe
2330dcf40ed3SJason Gunthorpe /*
2331dcf40ed3SJason Gunthorpe * If set_platform_dma_ops is not present a NULL domain can
2332dcf40ed3SJason Gunthorpe * happen only for first probe, in which case we leave
2333dcf40ed3SJason Gunthorpe * group->domain as NULL and let release clean everything up.
2334dcf40ed3SJason Gunthorpe */
2335dcf40ed3SJason Gunthorpe if (group->domain)
2336dcf40ed3SJason Gunthorpe WARN_ON(__iommu_device_set_domain(
2337dcf40ed3SJason Gunthorpe group, gdev->dev, group->domain,
2338dcf40ed3SJason Gunthorpe IOMMU_SET_DOMAIN_MUST_SUCCEED));
2339dcf40ed3SJason Gunthorpe else if (ops->set_platform_dma_ops)
2340dcf40ed3SJason Gunthorpe ops->set_platform_dma_ops(gdev->dev);
2341dcf40ed3SJason Gunthorpe if (gdev == last_gdev)
2342dcf40ed3SJason Gunthorpe break;
2343dcf40ed3SJason Gunthorpe }
2344dcf40ed3SJason Gunthorpe return ret;
2345e39cb8a3SJoerg Roedel }
2346e39cb8a3SJoerg Roedel
iommu_detach_group(struct iommu_domain * domain,struct iommu_group * group)2347d72e31c9SAlex Williamson void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2348d72e31c9SAlex Williamson {
2349e39cb8a3SJoerg Roedel mutex_lock(&group->mutex);
23500286300eSJason Gunthorpe via iommu __iommu_group_set_core_domain(group);
2351e39cb8a3SJoerg Roedel mutex_unlock(&group->mutex);
2352d72e31c9SAlex Williamson }
2353d72e31c9SAlex Williamson EXPORT_SYMBOL_GPL(iommu_detach_group);
2354d72e31c9SAlex Williamson
iommu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)2355bb5547acSVarun Sethi phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2356ab493a0fSOhad Ben-Cohen {
235713b6eb6eSRobin Murphy if (domain->type == IOMMU_DOMAIN_IDENTITY)
235813b6eb6eSRobin Murphy return iova;
235913b6eb6eSRobin Murphy
236013b6eb6eSRobin Murphy if (domain->type == IOMMU_DOMAIN_BLOCKED)
2361e5aa7f00SJoerg Roedel return 0;
2362e5aa7f00SJoerg Roedel
2363e5aa7f00SJoerg Roedel return domain->ops->iova_to_phys(domain, iova);
2364ab493a0fSOhad Ben-Cohen }
2365ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2366ab493a0fSOhad Ben-Cohen
iommu_pgsize(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,size_t * count)236789d5b960SWill Deacon static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2368b1d99dc5SWill Deacon phys_addr_t paddr, size_t size, size_t *count)
2369bd13969bSAlex Williamson {
2370b1d99dc5SWill Deacon unsigned int pgsize_idx, pgsize_idx_next;
2371e7d6fff6SWill Deacon unsigned long pgsizes;
2372b1d99dc5SWill Deacon size_t offset, pgsize, pgsize_next;
237389d5b960SWill Deacon unsigned long addr_merge = paddr | iova;
2374bd13969bSAlex Williamson
2375e7d6fff6SWill Deacon /* Page sizes supported by the hardware and small enough for @size */
2376e7d6fff6SWill Deacon pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2377bd13969bSAlex Williamson
2378e7d6fff6SWill Deacon /* Constrain the page sizes further based on the maximum alignment */
2379e7d6fff6SWill Deacon if (likely(addr_merge))
2380e7d6fff6SWill Deacon pgsizes &= GENMASK(__ffs(addr_merge), 0);
2381bd13969bSAlex Williamson
2382e7d6fff6SWill Deacon /* Make sure we have at least one suitable page size */
2383e7d6fff6SWill Deacon BUG_ON(!pgsizes);
2384bd13969bSAlex Williamson
2385e7d6fff6SWill Deacon /* Pick the biggest page size remaining */
2386e7d6fff6SWill Deacon pgsize_idx = __fls(pgsizes);
2387e7d6fff6SWill Deacon pgsize = BIT(pgsize_idx);
2388b1d99dc5SWill Deacon if (!count)
2389b1d99dc5SWill Deacon return pgsize;
2390bd13969bSAlex Williamson
2391b1d99dc5SWill Deacon /* Find the next biggest support page size, if it exists */
2392b1d99dc5SWill Deacon pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2393b1d99dc5SWill Deacon if (!pgsizes)
2394b1d99dc5SWill Deacon goto out_set_count;
2395b1d99dc5SWill Deacon
2396b1d99dc5SWill Deacon pgsize_idx_next = __ffs(pgsizes);
2397b1d99dc5SWill Deacon pgsize_next = BIT(pgsize_idx_next);
2398b1d99dc5SWill Deacon
2399b1d99dc5SWill Deacon /*
2400b1d99dc5SWill Deacon * There's no point trying a bigger page size unless the virtual
2401b1d99dc5SWill Deacon * and physical addresses are similarly offset within the larger page.
2402b1d99dc5SWill Deacon */
2403b1d99dc5SWill Deacon if ((iova ^ paddr) & (pgsize_next - 1))
2404b1d99dc5SWill Deacon goto out_set_count;
2405b1d99dc5SWill Deacon
2406b1d99dc5SWill Deacon /* Calculate the offset to the next page size alignment boundary */
2407b1d99dc5SWill Deacon offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2408b1d99dc5SWill Deacon
2409b1d99dc5SWill Deacon /*
2410b1d99dc5SWill Deacon * If size is big enough to accommodate the larger page, reduce
2411b1d99dc5SWill Deacon * the number of smaller pages.
2412b1d99dc5SWill Deacon */
2413b1d99dc5SWill Deacon if (offset + pgsize_next <= size)
2414b1d99dc5SWill Deacon size = offset;
2415b1d99dc5SWill Deacon
2416b1d99dc5SWill Deacon out_set_count:
2417b1d99dc5SWill Deacon *count = size >> pgsize_idx;
2418bd13969bSAlex Williamson return pgsize;
2419bd13969bSAlex Williamson }
2420bd13969bSAlex Williamson
__iommu_map_pages(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,int prot,gfp_t gfp,size_t * mapped)2421647c5776SIsaac J. Manjarres static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova,
2422647c5776SIsaac J. Manjarres phys_addr_t paddr, size_t size, int prot,
2423647c5776SIsaac J. Manjarres gfp_t gfp, size_t *mapped)
2424647c5776SIsaac J. Manjarres {
24259a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
2426647c5776SIsaac J. Manjarres size_t pgsize, count;
2427647c5776SIsaac J. Manjarres int ret;
2428bd13969bSAlex Williamson
2429647c5776SIsaac J. Manjarres pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2430bd13969bSAlex Williamson
2431647c5776SIsaac J. Manjarres pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2432647c5776SIsaac J. Manjarres iova, &paddr, pgsize, count);
2433bd13969bSAlex Williamson
2434647c5776SIsaac J. Manjarres if (ops->map_pages) {
2435647c5776SIsaac J. Manjarres ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2436647c5776SIsaac J. Manjarres gfp, mapped);
2437647c5776SIsaac J. Manjarres } else {
2438647c5776SIsaac J. Manjarres ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2439647c5776SIsaac J. Manjarres *mapped = ret ? 0 : pgsize;
2440647c5776SIsaac J. Manjarres }
2441bd13969bSAlex Williamson
2442647c5776SIsaac J. Manjarres return ret;
2443bd13969bSAlex Williamson }
2444bd13969bSAlex Williamson
__iommu_map(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,int prot,gfp_t gfp)24451b0b2a84SWei Yongjun static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2446781ca2deSTom Murphy phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2447ab493a0fSOhad Ben-Cohen {
24489a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
24497d3002ccSOhad Ben-Cohen unsigned long orig_iova = iova;
24507d3002ccSOhad Ben-Cohen unsigned int min_pagesz;
24517d3002ccSOhad Ben-Cohen size_t orig_size = size;
245206bfcaa9SYoshihiro Shimoda phys_addr_t orig_paddr = paddr;
24537d3002ccSOhad Ben-Cohen int ret = 0;
2454ab493a0fSOhad Ben-Cohen
2455647c5776SIsaac J. Manjarres if (unlikely(!(ops->map || ops->map_pages) ||
2456d16e0faaSRobin Murphy domain->pgsize_bitmap == 0UL))
2457e5aa7f00SJoerg Roedel return -ENODEV;
2458ab493a0fSOhad Ben-Cohen
2459a10315e5SJoerg Roedel if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2460a10315e5SJoerg Roedel return -EINVAL;
2461a10315e5SJoerg Roedel
24627d3002ccSOhad Ben-Cohen /* find out the minimum page size supported */
2463d16e0faaSRobin Murphy min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2464ab493a0fSOhad Ben-Cohen
24657d3002ccSOhad Ben-Cohen /*
24667d3002ccSOhad Ben-Cohen * both the virtual address and the physical one, as well as
24677d3002ccSOhad Ben-Cohen * the size of the mapping, must be aligned (at least) to the
24687d3002ccSOhad Ben-Cohen * size of the smallest page supported by the hardware
24697d3002ccSOhad Ben-Cohen */
24707d3002ccSOhad Ben-Cohen if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2471abedb049SFabio Estevam pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
24726197ca82SJoe Perches iova, &paddr, size, min_pagesz);
24737d3002ccSOhad Ben-Cohen return -EINVAL;
24747d3002ccSOhad Ben-Cohen }
2475ab493a0fSOhad Ben-Cohen
2476abedb049SFabio Estevam pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
24777d3002ccSOhad Ben-Cohen
24787d3002ccSOhad Ben-Cohen while (size) {
2479647c5776SIsaac J. Manjarres size_t mapped = 0;
24807d3002ccSOhad Ben-Cohen
2481647c5776SIsaac J. Manjarres ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp,
2482647c5776SIsaac J. Manjarres &mapped);
2483647c5776SIsaac J. Manjarres /*
2484647c5776SIsaac J. Manjarres * Some pages may have been mapped, even if an error occurred,
2485647c5776SIsaac J. Manjarres * so we should account for those so they can be unmapped.
2486647c5776SIsaac J. Manjarres */
2487647c5776SIsaac J. Manjarres size -= mapped;
24887d3002ccSOhad Ben-Cohen
24897d3002ccSOhad Ben-Cohen if (ret)
24907d3002ccSOhad Ben-Cohen break;
24917d3002ccSOhad Ben-Cohen
2492647c5776SIsaac J. Manjarres iova += mapped;
2493647c5776SIsaac J. Manjarres paddr += mapped;
24947d3002ccSOhad Ben-Cohen }
24957d3002ccSOhad Ben-Cohen
24967d3002ccSOhad Ben-Cohen /* unroll mapping in case something went wrong */
24977d3002ccSOhad Ben-Cohen if (ret)
24987d3002ccSOhad Ben-Cohen iommu_unmap(domain, orig_iova, orig_size - size);
2499e0be7c86SShuah Khan else
250006bfcaa9SYoshihiro Shimoda trace_map(orig_iova, orig_paddr, orig_size);
25017d3002ccSOhad Ben-Cohen
25027d3002ccSOhad Ben-Cohen return ret;
2503ab493a0fSOhad Ben-Cohen }
2504781ca2deSTom Murphy
iommu_map(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t size,int prot,gfp_t gfp)25051369459bSJason Gunthorpe int iommu_map(struct iommu_domain *domain, unsigned long iova,
2506d8c1df02SYong Wu phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2507d8c1df02SYong Wu {
25089a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
2509d8c1df02SYong Wu int ret;
2510d8c1df02SYong Wu
25111369459bSJason Gunthorpe might_sleep_if(gfpflags_allow_blocking(gfp));
25121369459bSJason Gunthorpe
25131369459bSJason Gunthorpe /* Discourage passing strange GFP flags */
25141369459bSJason Gunthorpe if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
25151369459bSJason Gunthorpe __GFP_HIGHMEM)))
25161369459bSJason Gunthorpe return -EINVAL;
25171369459bSJason Gunthorpe
2518b8437a3eSDouglas Anderson ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2519d8c1df02SYong Wu if (ret == 0 && ops->iotlb_sync_map)
25202ebbd258SYong Wu ops->iotlb_sync_map(domain, iova, size);
2521d8c1df02SYong Wu
2522d8c1df02SYong Wu return ret;
2523d8c1df02SYong Wu }
2524ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_map);
2525ab493a0fSOhad Ben-Cohen
__iommu_unmap_pages(struct iommu_domain * domain,unsigned long iova,size_t size,struct iommu_iotlb_gather * iotlb_gather)2526b1d99dc5SWill Deacon static size_t __iommu_unmap_pages(struct iommu_domain *domain,
2527b1d99dc5SWill Deacon unsigned long iova, size_t size,
2528b1d99dc5SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
2529b1d99dc5SWill Deacon {
25309a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
2531b1d99dc5SWill Deacon size_t pgsize, count;
2532b1d99dc5SWill Deacon
2533b1d99dc5SWill Deacon pgsize = iommu_pgsize(domain, iova, iova, size, &count);
2534b1d99dc5SWill Deacon return ops->unmap_pages ?
2535b1d99dc5SWill Deacon ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) :
2536b1d99dc5SWill Deacon ops->unmap(domain, iova, pgsize, iotlb_gather);
2537b1d99dc5SWill Deacon }
2538b1d99dc5SWill Deacon
__iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t size,struct iommu_iotlb_gather * iotlb_gather)2539add02cfdSJoerg Roedel static size_t __iommu_unmap(struct iommu_domain *domain,
2540add02cfdSJoerg Roedel unsigned long iova, size_t size,
2541a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
2542ab493a0fSOhad Ben-Cohen {
25439a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
25447d3002ccSOhad Ben-Cohen size_t unmapped_page, unmapped = 0;
25456fd492fdSShuah Khan unsigned long orig_iova = iova;
2546add02cfdSJoerg Roedel unsigned int min_pagesz;
2547ab493a0fSOhad Ben-Cohen
2548b1d99dc5SWill Deacon if (unlikely(!(ops->unmap || ops->unmap_pages) ||
2549d16e0faaSRobin Murphy domain->pgsize_bitmap == 0UL))
2550c5611a87SSuravee Suthikulpanit return 0;
2551ab493a0fSOhad Ben-Cohen
2552a10315e5SJoerg Roedel if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2553c5611a87SSuravee Suthikulpanit return 0;
2554a10315e5SJoerg Roedel
25557d3002ccSOhad Ben-Cohen /* find out the minimum page size supported */
2556d16e0faaSRobin Murphy min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2557ab493a0fSOhad Ben-Cohen
25587d3002ccSOhad Ben-Cohen /*
25597d3002ccSOhad Ben-Cohen * The virtual address, as well as the size of the mapping, must be
25607d3002ccSOhad Ben-Cohen * aligned (at least) to the size of the smallest page supported
25617d3002ccSOhad Ben-Cohen * by the hardware
25627d3002ccSOhad Ben-Cohen */
25637d3002ccSOhad Ben-Cohen if (!IS_ALIGNED(iova | size, min_pagesz)) {
25646197ca82SJoe Perches pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
25656197ca82SJoe Perches iova, size, min_pagesz);
2566c5611a87SSuravee Suthikulpanit return 0;
25677d3002ccSOhad Ben-Cohen }
2568ab493a0fSOhad Ben-Cohen
25696197ca82SJoe Perches pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
25705009065dSOhad Ben-Cohen
25717d3002ccSOhad Ben-Cohen /*
25727d3002ccSOhad Ben-Cohen * Keep iterating until we either unmap 'size' bytes (or more)
25737d3002ccSOhad Ben-Cohen * or we hit an area that isn't mapped.
25747d3002ccSOhad Ben-Cohen */
25757d3002ccSOhad Ben-Cohen while (unmapped < size) {
2576b1d99dc5SWill Deacon unmapped_page = __iommu_unmap_pages(domain, iova,
2577b1d99dc5SWill Deacon size - unmapped,
2578b1d99dc5SWill Deacon iotlb_gather);
25797d3002ccSOhad Ben-Cohen if (!unmapped_page)
25807d3002ccSOhad Ben-Cohen break;
25817d3002ccSOhad Ben-Cohen
25826197ca82SJoe Perches pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
25836197ca82SJoe Perches iova, unmapped_page);
25847d3002ccSOhad Ben-Cohen
25857d3002ccSOhad Ben-Cohen iova += unmapped_page;
25867d3002ccSOhad Ben-Cohen unmapped += unmapped_page;
25877d3002ccSOhad Ben-Cohen }
25887d3002ccSOhad Ben-Cohen
2589db8614d3SShuah Khan trace_unmap(orig_iova, size, unmapped);
25907d3002ccSOhad Ben-Cohen return unmapped;
2591ab493a0fSOhad Ben-Cohen }
2592add02cfdSJoerg Roedel
iommu_unmap(struct iommu_domain * domain,unsigned long iova,size_t size)2593add02cfdSJoerg Roedel size_t iommu_unmap(struct iommu_domain *domain,
2594add02cfdSJoerg Roedel unsigned long iova, size_t size)
2595add02cfdSJoerg Roedel {
2596a7d20dc1SWill Deacon struct iommu_iotlb_gather iotlb_gather;
2597a7d20dc1SWill Deacon size_t ret;
2598a7d20dc1SWill Deacon
2599a7d20dc1SWill Deacon iommu_iotlb_gather_init(&iotlb_gather);
2600a7d20dc1SWill Deacon ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2601aae4c8e2STom Murphy iommu_iotlb_sync(domain, &iotlb_gather);
2602a7d20dc1SWill Deacon
2603a7d20dc1SWill Deacon return ret;
2604add02cfdSJoerg Roedel }
2605ab493a0fSOhad Ben-Cohen EXPORT_SYMBOL_GPL(iommu_unmap);
26061460432cSAlex Williamson
iommu_unmap_fast(struct iommu_domain * domain,unsigned long iova,size_t size,struct iommu_iotlb_gather * iotlb_gather)2607add02cfdSJoerg Roedel size_t iommu_unmap_fast(struct iommu_domain *domain,
2608a7d20dc1SWill Deacon unsigned long iova, size_t size,
2609a7d20dc1SWill Deacon struct iommu_iotlb_gather *iotlb_gather)
2610add02cfdSJoerg Roedel {
2611a7d20dc1SWill Deacon return __iommu_unmap(domain, iova, size, iotlb_gather);
2612add02cfdSJoerg Roedel }
2613add02cfdSJoerg Roedel EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2614add02cfdSJoerg Roedel
iommu_map_sg(struct iommu_domain * domain,unsigned long iova,struct scatterlist * sg,unsigned int nents,int prot,gfp_t gfp)2615f2b2c051SJason Gunthorpe ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2616781ca2deSTom Murphy struct scatterlist *sg, unsigned int nents, int prot,
2617781ca2deSTom Murphy gfp_t gfp)
2618315786ebSOlav Haugan {
26199a630a4bSLu Baolu const struct iommu_domain_ops *ops = domain->ops;
26205d95f40eSRobin Murphy size_t len = 0, mapped = 0;
26215d95f40eSRobin Murphy phys_addr_t start;
26225d95f40eSRobin Murphy unsigned int i = 0;
262338ec010dSJoerg Roedel int ret;
2624315786ebSOlav Haugan
2625f2b2c051SJason Gunthorpe might_sleep_if(gfpflags_allow_blocking(gfp));
2626f2b2c051SJason Gunthorpe
2627f2b2c051SJason Gunthorpe /* Discourage passing strange GFP flags */
2628f2b2c051SJason Gunthorpe if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2629f2b2c051SJason Gunthorpe __GFP_HIGHMEM)))
2630f2b2c051SJason Gunthorpe return -EINVAL;
2631f2b2c051SJason Gunthorpe
26325d95f40eSRobin Murphy while (i <= nents) {
26335d95f40eSRobin Murphy phys_addr_t s_phys = sg_phys(sg);
2634315786ebSOlav Haugan
26355d95f40eSRobin Murphy if (len && s_phys != start + len) {
2636781ca2deSTom Murphy ret = __iommu_map(domain, iova + mapped, start,
2637781ca2deSTom Murphy len, prot, gfp);
2638781ca2deSTom Murphy
263938ec010dSJoerg Roedel if (ret)
264038ec010dSJoerg Roedel goto out_err;
264138ec010dSJoerg Roedel
26425d95f40eSRobin Murphy mapped += len;
26435d95f40eSRobin Murphy len = 0;
26445d95f40eSRobin Murphy }
26455d95f40eSRobin Murphy
2646cb147bbeSRobin Murphy if (sg_dma_is_bus_address(sg))
2647c9632183SLogan Gunthorpe goto next;
2648c9632183SLogan Gunthorpe
26495d95f40eSRobin Murphy if (len) {
26505d95f40eSRobin Murphy len += sg->length;
26515d95f40eSRobin Murphy } else {
26525d95f40eSRobin Murphy len = sg->length;
26535d95f40eSRobin Murphy start = s_phys;
26545d95f40eSRobin Murphy }
26555d95f40eSRobin Murphy
2656c9632183SLogan Gunthorpe next:
26575d95f40eSRobin Murphy if (++i < nents)
26585d95f40eSRobin Murphy sg = sg_next(sg);
2659315786ebSOlav Haugan }
2660315786ebSOlav Haugan
2661d8c1df02SYong Wu if (ops->iotlb_sync_map)
26622ebbd258SYong Wu ops->iotlb_sync_map(domain, iova, mapped);
2663315786ebSOlav Haugan return mapped;
266438ec010dSJoerg Roedel
266538ec010dSJoerg Roedel out_err:
266638ec010dSJoerg Roedel /* undo mappings already done */
266738ec010dSJoerg Roedel iommu_unmap(domain, iova, mapped);
266838ec010dSJoerg Roedel
2669ad8f36e4SLogan Gunthorpe return ret;
2670315786ebSOlav Haugan }
2671d88e61faSChristoph Hellwig EXPORT_SYMBOL_GPL(iommu_map_sg);
2672d7787d57SJoerg Roedel
2673207c6e36SJoerg Roedel /**
2674207c6e36SJoerg Roedel * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2675207c6e36SJoerg Roedel * @domain: the iommu domain where the fault has happened
2676207c6e36SJoerg Roedel * @dev: the device where the fault has happened
2677207c6e36SJoerg Roedel * @iova: the faulting address
2678207c6e36SJoerg Roedel * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2679207c6e36SJoerg Roedel *
2680207c6e36SJoerg Roedel * This function should be called by the low-level IOMMU implementations
2681207c6e36SJoerg Roedel * whenever IOMMU faults happen, to allow high-level users, that are
2682207c6e36SJoerg Roedel * interested in such events, to know about them.
2683207c6e36SJoerg Roedel *
2684207c6e36SJoerg Roedel * This event may be useful for several possible use cases:
2685207c6e36SJoerg Roedel * - mere logging of the event
2686207c6e36SJoerg Roedel * - dynamic TLB/PTE loading
2687207c6e36SJoerg Roedel * - if restarting of the faulting device is required
2688207c6e36SJoerg Roedel *
2689207c6e36SJoerg Roedel * Returns 0 on success and an appropriate error code otherwise (if dynamic
2690207c6e36SJoerg Roedel * PTE/TLB loading will one day be supported, implementations will be able
2691207c6e36SJoerg Roedel * to tell whether it succeeded or not according to this return value).
2692207c6e36SJoerg Roedel *
2693207c6e36SJoerg Roedel * Specifically, -ENOSYS is returned if a fault handler isn't installed
2694207c6e36SJoerg Roedel * (though fault handlers can also return -ENOSYS, in case they want to
2695207c6e36SJoerg Roedel * elicit the default behavior of the IOMMU drivers).
2696207c6e36SJoerg Roedel */
report_iommu_fault(struct iommu_domain * domain,struct device * dev,unsigned long iova,int flags)2697207c6e36SJoerg Roedel int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2698207c6e36SJoerg Roedel unsigned long iova, int flags)
2699207c6e36SJoerg Roedel {
2700207c6e36SJoerg Roedel int ret = -ENOSYS;
2701207c6e36SJoerg Roedel
2702207c6e36SJoerg Roedel /*
2703207c6e36SJoerg Roedel * if upper layers showed interest and installed a fault handler,
2704207c6e36SJoerg Roedel * invoke it.
2705207c6e36SJoerg Roedel */
2706207c6e36SJoerg Roedel if (domain->handler)
2707207c6e36SJoerg Roedel ret = domain->handler(domain, dev, iova, flags,
2708207c6e36SJoerg Roedel domain->handler_token);
2709207c6e36SJoerg Roedel
2710207c6e36SJoerg Roedel trace_io_page_fault(dev, iova, flags);
2711207c6e36SJoerg Roedel return ret;
2712207c6e36SJoerg Roedel }
2713207c6e36SJoerg Roedel EXPORT_SYMBOL_GPL(report_iommu_fault);
2714207c6e36SJoerg Roedel
iommu_init(void)2715d72e31c9SAlex Williamson static int __init iommu_init(void)
27161460432cSAlex Williamson {
2717d72e31c9SAlex Williamson iommu_group_kset = kset_create_and_add("iommu_groups",
2718d72e31c9SAlex Williamson NULL, kernel_kobj);
2719d72e31c9SAlex Williamson BUG_ON(!iommu_group_kset);
2720d72e31c9SAlex Williamson
2721bad614b2SGary R Hook iommu_debugfs_setup();
2722bad614b2SGary R Hook
2723d72e31c9SAlex Williamson return 0;
27241460432cSAlex Williamson }
2725d7ef9995SMarek Szyprowski core_initcall(iommu_init);
27260cd76dd1SJoerg Roedel
iommu_enable_nesting(struct iommu_domain * domain)27277e147547SChristoph Hellwig int iommu_enable_nesting(struct iommu_domain *domain)
27287e147547SChristoph Hellwig {
27297e147547SChristoph Hellwig if (domain->type != IOMMU_DOMAIN_UNMANAGED)
27307e147547SChristoph Hellwig return -EINVAL;
27317e147547SChristoph Hellwig if (!domain->ops->enable_nesting)
27327e147547SChristoph Hellwig return -EINVAL;
27337e147547SChristoph Hellwig return domain->ops->enable_nesting(domain);
27347e147547SChristoph Hellwig }
27357e147547SChristoph Hellwig EXPORT_SYMBOL_GPL(iommu_enable_nesting);
27367e147547SChristoph Hellwig
iommu_set_pgtable_quirks(struct iommu_domain * domain,unsigned long quirk)27374fc52b81SChristoph Hellwig int iommu_set_pgtable_quirks(struct iommu_domain *domain,
27384fc52b81SChristoph Hellwig unsigned long quirk)
27394fc52b81SChristoph Hellwig {
27404fc52b81SChristoph Hellwig if (domain->type != IOMMU_DOMAIN_UNMANAGED)
27414fc52b81SChristoph Hellwig return -EINVAL;
27424fc52b81SChristoph Hellwig if (!domain->ops->set_pgtable_quirks)
27434fc52b81SChristoph Hellwig return -EINVAL;
27444fc52b81SChristoph Hellwig return domain->ops->set_pgtable_quirks(domain, quirk);
27454fc52b81SChristoph Hellwig }
27464fc52b81SChristoph Hellwig EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
27474fc52b81SChristoph Hellwig
27488d0e2e9dSJason Gunthorpe /**
27498d0e2e9dSJason Gunthorpe * iommu_get_resv_regions - get reserved regions
27508d0e2e9dSJason Gunthorpe * @dev: device for which to get reserved regions
27518d0e2e9dSJason Gunthorpe * @list: reserved region list for device
27528d0e2e9dSJason Gunthorpe *
27538d0e2e9dSJason Gunthorpe * This returns a list of reserved IOVA regions specific to this device.
27548d0e2e9dSJason Gunthorpe * A domain user should not map IOVA in these ranges.
27558d0e2e9dSJason Gunthorpe */
iommu_get_resv_regions(struct device * dev,struct list_head * list)2756e5b5234aSEric Auger void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2757a1015c2bSJoerg Roedel {
27583f6634d9SLu Baolu const struct iommu_ops *ops = dev_iommu_ops(dev);
2759a1015c2bSJoerg Roedel
27603f6634d9SLu Baolu if (ops->get_resv_regions)
2761e5b5234aSEric Auger ops->get_resv_regions(dev, list);
2762a1015c2bSJoerg Roedel }
27638d0e2e9dSJason Gunthorpe EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
2764a1015c2bSJoerg Roedel
2765f9f6971eSThierry Reding /**
27668d0e2e9dSJason Gunthorpe * iommu_put_resv_regions - release reserved regions
2767f9f6971eSThierry Reding * @dev: device for which to free reserved regions
2768f9f6971eSThierry Reding * @list: reserved region list for device
2769f9f6971eSThierry Reding *
2770ae3ff39aSChristoph Hellwig * This releases a reserved region list acquired by iommu_get_resv_regions().
2771f9f6971eSThierry Reding */
iommu_put_resv_regions(struct device * dev,struct list_head * list)2772ae3ff39aSChristoph Hellwig void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2773f9f6971eSThierry Reding {
2774f9f6971eSThierry Reding struct iommu_resv_region *entry, *next;
2775f9f6971eSThierry Reding
27763b7e2482SShameer Kolothum list_for_each_entry_safe(entry, next, list, list) {
27773b7e2482SShameer Kolothum if (entry->free)
27783b7e2482SShameer Kolothum entry->free(dev, entry);
27793b7e2482SShameer Kolothum else
2780f9f6971eSThierry Reding kfree(entry);
2781f9f6971eSThierry Reding }
27823b7e2482SShameer Kolothum }
2783ae3ff39aSChristoph Hellwig EXPORT_SYMBOL(iommu_put_resv_regions);
2784f9f6971eSThierry Reding
iommu_alloc_resv_region(phys_addr_t start,size_t length,int prot,enum iommu_resv_type type,gfp_t gfp)27852b20cbbaSEric Auger struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
27869d3a4de4SRobin Murphy size_t length, int prot,
27870251d010SLu Baolu enum iommu_resv_type type,
27880251d010SLu Baolu gfp_t gfp)
27892b20cbbaSEric Auger {
27902b20cbbaSEric Auger struct iommu_resv_region *region;
27912b20cbbaSEric Auger
27920251d010SLu Baolu region = kzalloc(sizeof(*region), gfp);
27932b20cbbaSEric Auger if (!region)
27942b20cbbaSEric Auger return NULL;
27952b20cbbaSEric Auger
27962b20cbbaSEric Auger INIT_LIST_HEAD(®ion->list);
27972b20cbbaSEric Auger region->start = start;
27982b20cbbaSEric Auger region->length = length;
27992b20cbbaSEric Auger region->prot = prot;
28002b20cbbaSEric Auger region->type = type;
28012b20cbbaSEric Auger return region;
2802a1015c2bSJoerg Roedel }
2803a7ba5c3dSWill Deacon EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2804d290f1e7SJoerg Roedel
iommu_set_default_passthrough(bool cmd_line)28058a69961cSJoerg Roedel void iommu_set_default_passthrough(bool cmd_line)
28068a69961cSJoerg Roedel {
28078a69961cSJoerg Roedel if (cmd_line)
28083189713aSChristoph Hellwig iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
28098a69961cSJoerg Roedel iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
28108a69961cSJoerg Roedel }
28118a69961cSJoerg Roedel
iommu_set_default_translated(bool cmd_line)28128a69961cSJoerg Roedel void iommu_set_default_translated(bool cmd_line)
28138a69961cSJoerg Roedel {
28148a69961cSJoerg Roedel if (cmd_line)
28153189713aSChristoph Hellwig iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
28168a69961cSJoerg Roedel iommu_def_domain_type = IOMMU_DOMAIN_DMA;
28178a69961cSJoerg Roedel }
28188a69961cSJoerg Roedel
iommu_default_passthrough(void)28198a69961cSJoerg Roedel bool iommu_default_passthrough(void)
28208a69961cSJoerg Roedel {
28218a69961cSJoerg Roedel return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
28228a69961cSJoerg Roedel }
28238a69961cSJoerg Roedel EXPORT_SYMBOL_GPL(iommu_default_passthrough);
28248a69961cSJoerg Roedel
iommu_ops_from_fwnode(struct fwnode_handle * fwnode)2825534766dfSJoerg Roedel const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2826e4f10ffeSLorenzo Pieralisi {
2827e4f10ffeSLorenzo Pieralisi const struct iommu_ops *ops = NULL;
2828d0f6f583SJoerg Roedel struct iommu_device *iommu;
2829e4f10ffeSLorenzo Pieralisi
2830d0f6f583SJoerg Roedel spin_lock(&iommu_device_lock);
2831d0f6f583SJoerg Roedel list_for_each_entry(iommu, &iommu_device_list, list)
2832d0f6f583SJoerg Roedel if (iommu->fwnode == fwnode) {
2833d0f6f583SJoerg Roedel ops = iommu->ops;
2834e4f10ffeSLorenzo Pieralisi break;
2835e4f10ffeSLorenzo Pieralisi }
2836d0f6f583SJoerg Roedel spin_unlock(&iommu_device_lock);
2837e4f10ffeSLorenzo Pieralisi return ops;
2838e4f10ffeSLorenzo Pieralisi }
2839e4f10ffeSLorenzo Pieralisi
iommu_fwspec_init(struct device * dev,struct fwnode_handle * iommu_fwnode,const struct iommu_ops * ops)284057f98d2fSRobin Murphy int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
284157f98d2fSRobin Murphy const struct iommu_ops *ops)
284257f98d2fSRobin Murphy {
2843b4ef725eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
284457f98d2fSRobin Murphy
284557f98d2fSRobin Murphy if (fwspec)
284657f98d2fSRobin Murphy return ops == fwspec->ops ? 0 : -EINVAL;
284757f98d2fSRobin Murphy
284872acd9dfSJoerg Roedel if (!dev_iommu_get(dev))
284972acd9dfSJoerg Roedel return -ENOMEM;
285072acd9dfSJoerg Roedel
2851098accf2SRobin Murphy /* Preallocate for the overwhelmingly common case of 1 ID */
2852098accf2SRobin Murphy fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
285357f98d2fSRobin Murphy if (!fwspec)
285457f98d2fSRobin Murphy return -ENOMEM;
285557f98d2fSRobin Murphy
285657f98d2fSRobin Murphy of_node_get(to_of_node(iommu_fwnode));
285757f98d2fSRobin Murphy fwspec->iommu_fwnode = iommu_fwnode;
285857f98d2fSRobin Murphy fwspec->ops = ops;
2859b4ef725eSJoerg Roedel dev_iommu_fwspec_set(dev, fwspec);
286057f98d2fSRobin Murphy return 0;
286157f98d2fSRobin Murphy }
286257f98d2fSRobin Murphy EXPORT_SYMBOL_GPL(iommu_fwspec_init);
286357f98d2fSRobin Murphy
iommu_fwspec_free(struct device * dev)286457f98d2fSRobin Murphy void iommu_fwspec_free(struct device *dev)
286557f98d2fSRobin Murphy {
2866b4ef725eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
286757f98d2fSRobin Murphy
286857f98d2fSRobin Murphy if (fwspec) {
286957f98d2fSRobin Murphy fwnode_handle_put(fwspec->iommu_fwnode);
287057f98d2fSRobin Murphy kfree(fwspec);
2871b4ef725eSJoerg Roedel dev_iommu_fwspec_set(dev, NULL);
287257f98d2fSRobin Murphy }
287357f98d2fSRobin Murphy }
287457f98d2fSRobin Murphy EXPORT_SYMBOL_GPL(iommu_fwspec_free);
287557f98d2fSRobin Murphy
iommu_fwspec_add_ids(struct device * dev,u32 * ids,int num_ids)287657f98d2fSRobin Murphy int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
287757f98d2fSRobin Murphy {
2878b4ef725eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2879098accf2SRobin Murphy int i, new_num;
288057f98d2fSRobin Murphy
288157f98d2fSRobin Murphy if (!fwspec)
288257f98d2fSRobin Murphy return -EINVAL;
288357f98d2fSRobin Murphy
2884098accf2SRobin Murphy new_num = fwspec->num_ids + num_ids;
2885098accf2SRobin Murphy if (new_num > 1) {
2886098accf2SRobin Murphy fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2887098accf2SRobin Murphy GFP_KERNEL);
288857f98d2fSRobin Murphy if (!fwspec)
288957f98d2fSRobin Murphy return -ENOMEM;
2890909111baSZhen Lei
2891b4ef725eSJoerg Roedel dev_iommu_fwspec_set(dev, fwspec);
289257f98d2fSRobin Murphy }
289357f98d2fSRobin Murphy
289457f98d2fSRobin Murphy for (i = 0; i < num_ids; i++)
289557f98d2fSRobin Murphy fwspec->ids[fwspec->num_ids + i] = ids[i];
289657f98d2fSRobin Murphy
2897098accf2SRobin Murphy fwspec->num_ids = new_num;
289857f98d2fSRobin Murphy return 0;
289957f98d2fSRobin Murphy }
290057f98d2fSRobin Murphy EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2901a3a19592SLu Baolu
2902a3a19592SLu Baolu /*
2903a3a19592SLu Baolu * Per device IOMMU features.
2904a3a19592SLu Baolu */
iommu_dev_enable_feature(struct device * dev,enum iommu_dev_features feat)2905a3a19592SLu Baolu int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2906a3a19592SLu Baolu {
2907b9abb19fSShameer Kolothum if (dev->iommu && dev->iommu->iommu_dev) {
2908b9abb19fSShameer Kolothum const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2909a3a19592SLu Baolu
2910b9abb19fSShameer Kolothum if (ops->dev_enable_feat)
2911a3a19592SLu Baolu return ops->dev_enable_feat(dev, feat);
2912b9abb19fSShameer Kolothum }
2913a3a19592SLu Baolu
2914a3a19592SLu Baolu return -ENODEV;
2915a3a19592SLu Baolu }
2916a3a19592SLu Baolu EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2917a3a19592SLu Baolu
2918a3a19592SLu Baolu /*
2919a3a19592SLu Baolu * The device drivers should do the necessary cleanups before calling this.
2920a3a19592SLu Baolu */
iommu_dev_disable_feature(struct device * dev,enum iommu_dev_features feat)2921a3a19592SLu Baolu int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2922a3a19592SLu Baolu {
2923b9abb19fSShameer Kolothum if (dev->iommu && dev->iommu->iommu_dev) {
2924b9abb19fSShameer Kolothum const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2925a3a19592SLu Baolu
2926b9abb19fSShameer Kolothum if (ops->dev_disable_feat)
2927a3a19592SLu Baolu return ops->dev_disable_feat(dev, feat);
2928b9abb19fSShameer Kolothum }
2929a3a19592SLu Baolu
2930a3a19592SLu Baolu return -EBUSY;
2931a3a19592SLu Baolu }
2932a3a19592SLu Baolu EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2933a3a19592SLu Baolu
2934d99be00fSJason Gunthorpe /**
2935d99be00fSJason Gunthorpe * iommu_setup_default_domain - Set the default_domain for the group
2936d99be00fSJason Gunthorpe * @group: Group to change
2937d99be00fSJason Gunthorpe * @target_type: Domain type to set as the default_domain
293808a27c1cSSai Praneeth Prakhya *
2939d99be00fSJason Gunthorpe * Allocate a default domain and set it as the current domain on the group. If
2940d99be00fSJason Gunthorpe * the group already has a default domain it will be changed to the target_type.
2941d99be00fSJason Gunthorpe * When target_type is 0 the default domain is selected based on driver and
2942d99be00fSJason Gunthorpe * system preferences.
294308a27c1cSSai Praneeth Prakhya */
iommu_setup_default_domain(struct iommu_group * group,int target_type)2944d99be00fSJason Gunthorpe static int iommu_setup_default_domain(struct iommu_group *group,
2945d99be00fSJason Gunthorpe int target_type)
294608a27c1cSSai Praneeth Prakhya {
2947d99be00fSJason Gunthorpe struct iommu_domain *old_dom = group->default_domain;
2948d99be00fSJason Gunthorpe struct group_device *gdev;
2949d99be00fSJason Gunthorpe struct iommu_domain *dom;
29501000dccdSJason Gunthorpe bool direct_failed;
2951d99be00fSJason Gunthorpe int req_type;
29524c8444f1SLu Baolu int ret;
295308a27c1cSSai Praneeth Prakhya
295433793748SLu Baolu lockdep_assert_held(&group->mutex);
295508a27c1cSSai Praneeth Prakhya
2956d99be00fSJason Gunthorpe req_type = iommu_get_default_domain_type(group, target_type);
2957d99be00fSJason Gunthorpe if (req_type < 0)
29584c8444f1SLu Baolu return -EINVAL;
295908a27c1cSSai Praneeth Prakhya
296008a27c1cSSai Praneeth Prakhya /*
2961d99be00fSJason Gunthorpe * There are still some drivers which don't support default domains, so
2962d99be00fSJason Gunthorpe * we ignore the failure and leave group->default_domain NULL.
2963d99be00fSJason Gunthorpe *
2964d99be00fSJason Gunthorpe * We assume that the iommu driver starts up the device in
2965d99be00fSJason Gunthorpe * 'set_platform_dma_ops' mode if it does not support default domains.
296608a27c1cSSai Praneeth Prakhya */
2967d99be00fSJason Gunthorpe dom = iommu_group_alloc_default_domain(group, req_type);
2968d99be00fSJason Gunthorpe if (!dom) {
2969d99be00fSJason Gunthorpe /* Once in default_domain mode we never leave */
2970d99be00fSJason Gunthorpe if (group->default_domain)
2971d99be00fSJason Gunthorpe return -ENODEV;
29724c8444f1SLu Baolu group->default_domain = NULL;
2973d99be00fSJason Gunthorpe return 0;
2974fcbb0a4dSJason Gunthorpe }
297508a27c1cSSai Praneeth Prakhya
2976d99be00fSJason Gunthorpe if (group->default_domain == dom)
297708a27c1cSSai Praneeth Prakhya return 0;
297808a27c1cSSai Praneeth Prakhya
2979d99be00fSJason Gunthorpe /*
2980d99be00fSJason Gunthorpe * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
2981d99be00fSJason Gunthorpe * mapped before their device is attached, in order to guarantee
2982d99be00fSJason Gunthorpe * continuity with any FW activity
2983d99be00fSJason Gunthorpe */
29841000dccdSJason Gunthorpe direct_failed = false;
29851000dccdSJason Gunthorpe for_each_group_device(group, gdev) {
29861000dccdSJason Gunthorpe if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
29871000dccdSJason Gunthorpe direct_failed = true;
29881000dccdSJason Gunthorpe dev_warn_once(
29891000dccdSJason Gunthorpe gdev->dev->iommu->iommu_dev->dev,
29901000dccdSJason Gunthorpe "IOMMU driver was not able to establish FW requested direct mapping.");
29911000dccdSJason Gunthorpe }
29921000dccdSJason Gunthorpe }
299308a27c1cSSai Praneeth Prakhya
2994d99be00fSJason Gunthorpe /* We must set default_domain early for __iommu_device_set_domain */
2995d99be00fSJason Gunthorpe group->default_domain = dom;
2996d99be00fSJason Gunthorpe if (!group->domain) {
2997d99be00fSJason Gunthorpe /*
2998d99be00fSJason Gunthorpe * Drivers are not allowed to fail the first domain attach.
2999d99be00fSJason Gunthorpe * The only way to recover from this is to fail attaching the
3000d99be00fSJason Gunthorpe * iommu driver and call ops->release_device. Put the domain
3001d99be00fSJason Gunthorpe * in group->default_domain so it is freed after.
3002d99be00fSJason Gunthorpe */
3003d99be00fSJason Gunthorpe ret = __iommu_group_set_domain_internal(
3004d99be00fSJason Gunthorpe group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3005d99be00fSJason Gunthorpe if (WARN_ON(ret))
3006911476efSJason Gunthorpe goto out_free_old;
3007d99be00fSJason Gunthorpe } else {
3008d99be00fSJason Gunthorpe ret = __iommu_group_set_domain(group, dom);
3009911476efSJason Gunthorpe if (ret)
3010911476efSJason Gunthorpe goto err_restore_def_domain;
301108a27c1cSSai Praneeth Prakhya }
301208a27c1cSSai Praneeth Prakhya
301308a27c1cSSai Praneeth Prakhya /*
30141000dccdSJason Gunthorpe * Drivers are supposed to allow mappings to be installed in a domain
30151000dccdSJason Gunthorpe * before device attachment, but some don't. Hack around this defect by
30161000dccdSJason Gunthorpe * trying again after attaching. If this happens it means the device
30171000dccdSJason Gunthorpe * will not continuously have the IOMMU_RESV_DIRECT map.
301808a27c1cSSai Praneeth Prakhya */
30191000dccdSJason Gunthorpe if (direct_failed) {
30201000dccdSJason Gunthorpe for_each_group_device(group, gdev) {
30211000dccdSJason Gunthorpe ret = iommu_create_device_direct_mappings(dom, gdev->dev);
302208a27c1cSSai Praneeth Prakhya if (ret)
3023911476efSJason Gunthorpe goto err_restore_domain;
30241000dccdSJason Gunthorpe }
30251000dccdSJason Gunthorpe }
302608a27c1cSSai Praneeth Prakhya
3027911476efSJason Gunthorpe out_free_old:
3028d99be00fSJason Gunthorpe if (old_dom)
3029d99be00fSJason Gunthorpe iommu_domain_free(old_dom);
303008a27c1cSSai Praneeth Prakhya return ret;
3031911476efSJason Gunthorpe
3032911476efSJason Gunthorpe err_restore_domain:
3033911476efSJason Gunthorpe if (old_dom)
3034911476efSJason Gunthorpe __iommu_group_set_domain_internal(
3035911476efSJason Gunthorpe group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3036911476efSJason Gunthorpe err_restore_def_domain:
3037911476efSJason Gunthorpe if (old_dom) {
3038911476efSJason Gunthorpe iommu_domain_free(dom);
3039911476efSJason Gunthorpe group->default_domain = old_dom;
3040911476efSJason Gunthorpe }
3041911476efSJason Gunthorpe return ret;
304208a27c1cSSai Praneeth Prakhya }
304308a27c1cSSai Praneeth Prakhya
304408a27c1cSSai Praneeth Prakhya /*
3045452e69b5SRobin Murphy * Changing the default domain through sysfs requires the users to unbind the
3046452e69b5SRobin Murphy * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3047452e69b5SRobin Murphy * transition. Return failure if this isn't met.
304808a27c1cSSai Praneeth Prakhya *
304908a27c1cSSai Praneeth Prakhya * We need to consider the race between this and the device release path.
305049a22aaeSLu Baolu * group->mutex is used here to guarantee that the device release path
305108a27c1cSSai Praneeth Prakhya * will not be entered at the same time.
305208a27c1cSSai Praneeth Prakhya */
iommu_group_store_type(struct iommu_group * group,const char * buf,size_t count)305308a27c1cSSai Praneeth Prakhya static ssize_t iommu_group_store_type(struct iommu_group *group,
305408a27c1cSSai Praneeth Prakhya const char *buf, size_t count)
305508a27c1cSSai Praneeth Prakhya {
30565957c193SJason Gunthorpe struct group_device *gdev;
305708a27c1cSSai Praneeth Prakhya int ret, req_type;
305808a27c1cSSai Praneeth Prakhya
305908a27c1cSSai Praneeth Prakhya if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
306008a27c1cSSai Praneeth Prakhya return -EACCES;
306108a27c1cSSai Praneeth Prakhya
3062392bf519SRobin Murphy if (WARN_ON(!group) || !group->default_domain)
306308a27c1cSSai Praneeth Prakhya return -EINVAL;
306408a27c1cSSai Praneeth Prakhya
306508a27c1cSSai Praneeth Prakhya if (sysfs_streq(buf, "identity"))
306608a27c1cSSai Praneeth Prakhya req_type = IOMMU_DOMAIN_IDENTITY;
306708a27c1cSSai Praneeth Prakhya else if (sysfs_streq(buf, "DMA"))
306808a27c1cSSai Praneeth Prakhya req_type = IOMMU_DOMAIN_DMA;
306926225beaSRobin Murphy else if (sysfs_streq(buf, "DMA-FQ"))
307026225beaSRobin Murphy req_type = IOMMU_DOMAIN_DMA_FQ;
307108a27c1cSSai Praneeth Prakhya else if (sysfs_streq(buf, "auto"))
307208a27c1cSSai Praneeth Prakhya req_type = 0;
307308a27c1cSSai Praneeth Prakhya else
307408a27c1cSSai Praneeth Prakhya return -EINVAL;
307508a27c1cSSai Praneeth Prakhya
307608a27c1cSSai Praneeth Prakhya mutex_lock(&group->mutex);
307749a22aaeSLu Baolu /* We can bring up a flush queue without tearing down the domain. */
307849a22aaeSLu Baolu if (req_type == IOMMU_DOMAIN_DMA_FQ &&
307949a22aaeSLu Baolu group->default_domain->type == IOMMU_DOMAIN_DMA) {
308049a22aaeSLu Baolu ret = iommu_dma_init_fq(group->default_domain);
30815957c193SJason Gunthorpe if (ret)
30825957c193SJason Gunthorpe goto out_unlock;
308349a22aaeSLu Baolu
30845957c193SJason Gunthorpe group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
30855957c193SJason Gunthorpe ret = count;
30865957c193SJason Gunthorpe goto out_unlock;
308708a27c1cSSai Praneeth Prakhya }
308808a27c1cSSai Praneeth Prakhya
308949a22aaeSLu Baolu /* Otherwise, ensure that device exists and no driver is bound. */
309049a22aaeSLu Baolu if (list_empty(&group->devices) || group->owner_cnt) {
30915957c193SJason Gunthorpe ret = -EPERM;
30925957c193SJason Gunthorpe goto out_unlock;
309349a22aaeSLu Baolu }
309449a22aaeSLu Baolu
3095d99be00fSJason Gunthorpe ret = iommu_setup_default_domain(group, req_type);
30965957c193SJason Gunthorpe if (ret)
30975957c193SJason Gunthorpe goto out_unlock;
309808a27c1cSSai Praneeth Prakhya
309908a27c1cSSai Praneeth Prakhya /*
310033793748SLu Baolu * Release the mutex here because ops->probe_finalize() call-back of
310133793748SLu Baolu * some vendor IOMMU drivers calls arm_iommu_attach_device() which
310233793748SLu Baolu * in-turn might call back into IOMMU core code, where it tries to take
310333793748SLu Baolu * group->mutex, resulting in a deadlock.
310408a27c1cSSai Praneeth Prakhya */
310508a27c1cSSai Praneeth Prakhya mutex_unlock(&group->mutex);
310608a27c1cSSai Praneeth Prakhya
310733793748SLu Baolu /* Make sure dma_ops is appropriatley set */
3108e996c12dSJason Gunthorpe for_each_group_device(group, gdev)
3109e996c12dSJason Gunthorpe iommu_group_do_probe_finalize(gdev->dev);
31105957c193SJason Gunthorpe return count;
311108a27c1cSSai Praneeth Prakhya
31125957c193SJason Gunthorpe out_unlock:
31135957c193SJason Gunthorpe mutex_unlock(&group->mutex);
311449a22aaeSLu Baolu return ret ?: count;
311508a27c1cSSai Praneeth Prakhya }
31161ea2a07aSLu Baolu
iommu_is_default_domain(struct iommu_group * group)31172380f1e8SJason Gunthorpe static bool iommu_is_default_domain(struct iommu_group *group)
31182380f1e8SJason Gunthorpe {
31192380f1e8SJason Gunthorpe if (group->domain == group->default_domain)
31202380f1e8SJason Gunthorpe return true;
31212380f1e8SJason Gunthorpe
31222380f1e8SJason Gunthorpe /*
31232380f1e8SJason Gunthorpe * If the default domain was set to identity and it is still an identity
31242380f1e8SJason Gunthorpe * domain then we consider this a pass. This happens because of
31252380f1e8SJason Gunthorpe * amd_iommu_init_device() replacing the default idenytity domain with an
31262380f1e8SJason Gunthorpe * identity domain that has a different configuration for AMDGPU.
31272380f1e8SJason Gunthorpe */
31282380f1e8SJason Gunthorpe if (group->default_domain &&
31292380f1e8SJason Gunthorpe group->default_domain->type == IOMMU_DOMAIN_IDENTITY &&
31302380f1e8SJason Gunthorpe group->domain && group->domain->type == IOMMU_DOMAIN_IDENTITY)
31312380f1e8SJason Gunthorpe return true;
31322380f1e8SJason Gunthorpe return false;
31332380f1e8SJason Gunthorpe }
31342380f1e8SJason Gunthorpe
31351ea2a07aSLu Baolu /**
31361ea2a07aSLu Baolu * iommu_device_use_default_domain() - Device driver wants to handle device
31371ea2a07aSLu Baolu * DMA through the kernel DMA API.
31381ea2a07aSLu Baolu * @dev: The device.
31391ea2a07aSLu Baolu *
31401ea2a07aSLu Baolu * The device driver about to bind @dev wants to do DMA through the kernel
31411ea2a07aSLu Baolu * DMA API. Return 0 if it is allowed, otherwise an error.
31421ea2a07aSLu Baolu */
iommu_device_use_default_domain(struct device * dev)31431ea2a07aSLu Baolu int iommu_device_use_default_domain(struct device *dev)
31441ea2a07aSLu Baolu {
31451ea2a07aSLu Baolu struct iommu_group *group = iommu_group_get(dev);
31461ea2a07aSLu Baolu int ret = 0;
31471ea2a07aSLu Baolu
31481ea2a07aSLu Baolu if (!group)
31491ea2a07aSLu Baolu return 0;
31501ea2a07aSLu Baolu
31511ea2a07aSLu Baolu mutex_lock(&group->mutex);
31521ea2a07aSLu Baolu if (group->owner_cnt) {
315316603704SLu Baolu if (group->owner || !iommu_is_default_domain(group) ||
315416603704SLu Baolu !xa_empty(&group->pasid_array)) {
31551ea2a07aSLu Baolu ret = -EBUSY;
31561ea2a07aSLu Baolu goto unlock_out;
31571ea2a07aSLu Baolu }
31581ea2a07aSLu Baolu }
31591ea2a07aSLu Baolu
31601ea2a07aSLu Baolu group->owner_cnt++;
31611ea2a07aSLu Baolu
31621ea2a07aSLu Baolu unlock_out:
31631ea2a07aSLu Baolu mutex_unlock(&group->mutex);
31641ea2a07aSLu Baolu iommu_group_put(group);
31651ea2a07aSLu Baolu
31661ea2a07aSLu Baolu return ret;
31671ea2a07aSLu Baolu }
31681ea2a07aSLu Baolu
31691ea2a07aSLu Baolu /**
31701ea2a07aSLu Baolu * iommu_device_unuse_default_domain() - Device driver stops handling device
31711ea2a07aSLu Baolu * DMA through the kernel DMA API.
31721ea2a07aSLu Baolu * @dev: The device.
31731ea2a07aSLu Baolu *
31741ea2a07aSLu Baolu * The device driver doesn't want to do DMA through kernel DMA API anymore.
31751ea2a07aSLu Baolu * It must be called after iommu_device_use_default_domain().
31761ea2a07aSLu Baolu */
iommu_device_unuse_default_domain(struct device * dev)31771ea2a07aSLu Baolu void iommu_device_unuse_default_domain(struct device *dev)
31781ea2a07aSLu Baolu {
31791ea2a07aSLu Baolu struct iommu_group *group = iommu_group_get(dev);
31801ea2a07aSLu Baolu
31811ea2a07aSLu Baolu if (!group)
31821ea2a07aSLu Baolu return;
31831ea2a07aSLu Baolu
31841ea2a07aSLu Baolu mutex_lock(&group->mutex);
318516603704SLu Baolu if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
31861ea2a07aSLu Baolu group->owner_cnt--;
31871ea2a07aSLu Baolu
31881ea2a07aSLu Baolu mutex_unlock(&group->mutex);
31891ea2a07aSLu Baolu iommu_group_put(group);
31901ea2a07aSLu Baolu }
31911ea2a07aSLu Baolu
__iommu_group_alloc_blocking_domain(struct iommu_group * group)31920286300eSJason Gunthorpe via iommu static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
31930286300eSJason Gunthorpe via iommu {
31940286300eSJason Gunthorpe via iommu struct group_device *dev =
31950286300eSJason Gunthorpe via iommu list_first_entry(&group->devices, struct group_device, list);
31960286300eSJason Gunthorpe via iommu
31970286300eSJason Gunthorpe via iommu if (group->blocking_domain)
31980286300eSJason Gunthorpe via iommu return 0;
31990286300eSJason Gunthorpe via iommu
32000286300eSJason Gunthorpe via iommu group->blocking_domain =
32010286300eSJason Gunthorpe via iommu __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED);
32020286300eSJason Gunthorpe via iommu if (!group->blocking_domain) {
32030286300eSJason Gunthorpe via iommu /*
32040286300eSJason Gunthorpe via iommu * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
32050286300eSJason Gunthorpe via iommu * create an empty domain instead.
32060286300eSJason Gunthorpe via iommu */
32070286300eSJason Gunthorpe via iommu group->blocking_domain = __iommu_domain_alloc(
32080286300eSJason Gunthorpe via iommu dev->dev->bus, IOMMU_DOMAIN_UNMANAGED);
32090286300eSJason Gunthorpe via iommu if (!group->blocking_domain)
32100286300eSJason Gunthorpe via iommu return -EINVAL;
32110286300eSJason Gunthorpe via iommu }
32120286300eSJason Gunthorpe via iommu return 0;
32130286300eSJason Gunthorpe via iommu }
32140286300eSJason Gunthorpe via iommu
__iommu_take_dma_ownership(struct iommu_group * group,void * owner)321589395cceSLu Baolu static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
321689395cceSLu Baolu {
321789395cceSLu Baolu int ret;
321889395cceSLu Baolu
321989395cceSLu Baolu if ((group->domain && group->domain != group->default_domain) ||
322089395cceSLu Baolu !xa_empty(&group->pasid_array))
322189395cceSLu Baolu return -EBUSY;
322289395cceSLu Baolu
322389395cceSLu Baolu ret = __iommu_group_alloc_blocking_domain(group);
322489395cceSLu Baolu if (ret)
322589395cceSLu Baolu return ret;
322689395cceSLu Baolu ret = __iommu_group_set_domain(group, group->blocking_domain);
322789395cceSLu Baolu if (ret)
322889395cceSLu Baolu return ret;
322989395cceSLu Baolu
323089395cceSLu Baolu group->owner = owner;
323189395cceSLu Baolu group->owner_cnt++;
323289395cceSLu Baolu return 0;
323389395cceSLu Baolu }
323489395cceSLu Baolu
32351ea2a07aSLu Baolu /**
32361ea2a07aSLu Baolu * iommu_group_claim_dma_owner() - Set DMA ownership of a group
32371ea2a07aSLu Baolu * @group: The group.
32381ea2a07aSLu Baolu * @owner: Caller specified pointer. Used for exclusive ownership.
32391ea2a07aSLu Baolu *
324089395cceSLu Baolu * This is to support backward compatibility for vfio which manages the dma
324189395cceSLu Baolu * ownership in iommu_group level. New invocations on this interface should be
324289395cceSLu Baolu * prohibited. Only a single owner may exist for a group.
32431ea2a07aSLu Baolu */
iommu_group_claim_dma_owner(struct iommu_group * group,void * owner)32441ea2a07aSLu Baolu int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
32451ea2a07aSLu Baolu {
32461ea2a07aSLu Baolu int ret = 0;
32471ea2a07aSLu Baolu
324889395cceSLu Baolu if (WARN_ON(!owner))
324989395cceSLu Baolu return -EINVAL;
325089395cceSLu Baolu
32511ea2a07aSLu Baolu mutex_lock(&group->mutex);
32521ea2a07aSLu Baolu if (group->owner_cnt) {
32531ea2a07aSLu Baolu ret = -EPERM;
32541ea2a07aSLu Baolu goto unlock_out;
32551ea2a07aSLu Baolu }
32561ea2a07aSLu Baolu
325789395cceSLu Baolu ret = __iommu_take_dma_ownership(group, owner);
32581ea2a07aSLu Baolu unlock_out:
32591ea2a07aSLu Baolu mutex_unlock(&group->mutex);
32601ea2a07aSLu Baolu
32611ea2a07aSLu Baolu return ret;
32621ea2a07aSLu Baolu }
32631ea2a07aSLu Baolu EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
32641ea2a07aSLu Baolu
32651ea2a07aSLu Baolu /**
326689395cceSLu Baolu * iommu_device_claim_dma_owner() - Set DMA ownership of a device
326789395cceSLu Baolu * @dev: The device.
326889395cceSLu Baolu * @owner: Caller specified pointer. Used for exclusive ownership.
32691ea2a07aSLu Baolu *
327089395cceSLu Baolu * Claim the DMA ownership of a device. Multiple devices in the same group may
327189395cceSLu Baolu * concurrently claim ownership if they present the same owner value. Returns 0
327289395cceSLu Baolu * on success and error code on failure
32731ea2a07aSLu Baolu */
iommu_device_claim_dma_owner(struct device * dev,void * owner)327489395cceSLu Baolu int iommu_device_claim_dma_owner(struct device *dev, void *owner)
327589395cceSLu Baolu {
3276a6a9a5daSMiaoqian Lin struct iommu_group *group;
327789395cceSLu Baolu int ret = 0;
327889395cceSLu Baolu
327989395cceSLu Baolu if (WARN_ON(!owner))
328089395cceSLu Baolu return -EINVAL;
328189395cceSLu Baolu
3282a6a9a5daSMiaoqian Lin group = iommu_group_get(dev);
3283a6a9a5daSMiaoqian Lin if (!group)
3284a6a9a5daSMiaoqian Lin return -ENODEV;
3285a6a9a5daSMiaoqian Lin
328689395cceSLu Baolu mutex_lock(&group->mutex);
328789395cceSLu Baolu if (group->owner_cnt) {
328889395cceSLu Baolu if (group->owner != owner) {
328989395cceSLu Baolu ret = -EPERM;
329089395cceSLu Baolu goto unlock_out;
329189395cceSLu Baolu }
329289395cceSLu Baolu group->owner_cnt++;
329389395cceSLu Baolu goto unlock_out;
329489395cceSLu Baolu }
329589395cceSLu Baolu
329689395cceSLu Baolu ret = __iommu_take_dma_ownership(group, owner);
329789395cceSLu Baolu unlock_out:
329889395cceSLu Baolu mutex_unlock(&group->mutex);
329989395cceSLu Baolu iommu_group_put(group);
330089395cceSLu Baolu
330189395cceSLu Baolu return ret;
330289395cceSLu Baolu }
330389395cceSLu Baolu EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
330489395cceSLu Baolu
__iommu_release_dma_ownership(struct iommu_group * group)330589395cceSLu Baolu static void __iommu_release_dma_ownership(struct iommu_group *group)
33061ea2a07aSLu Baolu {
330716603704SLu Baolu if (WARN_ON(!group->owner_cnt || !group->owner ||
330816603704SLu Baolu !xa_empty(&group->pasid_array)))
330989395cceSLu Baolu return;
33101ea2a07aSLu Baolu
33111ea2a07aSLu Baolu group->owner_cnt = 0;
33121ea2a07aSLu Baolu group->owner = NULL;
3313dcf40ed3SJason Gunthorpe __iommu_group_set_domain_nofail(group, group->default_domain);
331489395cceSLu Baolu }
33150286300eSJason Gunthorpe via iommu
331689395cceSLu Baolu /**
331789395cceSLu Baolu * iommu_group_release_dma_owner() - Release DMA ownership of a group
33186b7867b5SZhu Wang * @group: The group
331989395cceSLu Baolu *
332089395cceSLu Baolu * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
332189395cceSLu Baolu */
iommu_group_release_dma_owner(struct iommu_group * group)332289395cceSLu Baolu void iommu_group_release_dma_owner(struct iommu_group *group)
332389395cceSLu Baolu {
332489395cceSLu Baolu mutex_lock(&group->mutex);
332589395cceSLu Baolu __iommu_release_dma_ownership(group);
33261ea2a07aSLu Baolu mutex_unlock(&group->mutex);
33271ea2a07aSLu Baolu }
33281ea2a07aSLu Baolu EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
33291ea2a07aSLu Baolu
33301ea2a07aSLu Baolu /**
333189395cceSLu Baolu * iommu_device_release_dma_owner() - Release DMA ownership of a device
33326b7867b5SZhu Wang * @dev: The device.
333389395cceSLu Baolu *
333489395cceSLu Baolu * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
333589395cceSLu Baolu */
iommu_device_release_dma_owner(struct device * dev)333689395cceSLu Baolu void iommu_device_release_dma_owner(struct device *dev)
333789395cceSLu Baolu {
333889395cceSLu Baolu struct iommu_group *group = iommu_group_get(dev);
333989395cceSLu Baolu
334089395cceSLu Baolu mutex_lock(&group->mutex);
334189395cceSLu Baolu if (group->owner_cnt > 1)
334289395cceSLu Baolu group->owner_cnt--;
334389395cceSLu Baolu else
334489395cceSLu Baolu __iommu_release_dma_ownership(group);
334589395cceSLu Baolu mutex_unlock(&group->mutex);
334689395cceSLu Baolu iommu_group_put(group);
334789395cceSLu Baolu }
334889395cceSLu Baolu EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
334989395cceSLu Baolu
335089395cceSLu Baolu /**
33511ea2a07aSLu Baolu * iommu_group_dma_owner_claimed() - Query group dma ownership status
33521ea2a07aSLu Baolu * @group: The group.
33531ea2a07aSLu Baolu *
33541ea2a07aSLu Baolu * This provides status query on a given group. It is racy and only for
33551ea2a07aSLu Baolu * non-binding status reporting.
33561ea2a07aSLu Baolu */
iommu_group_dma_owner_claimed(struct iommu_group * group)33571ea2a07aSLu Baolu bool iommu_group_dma_owner_claimed(struct iommu_group *group)
33581ea2a07aSLu Baolu {
33591ea2a07aSLu Baolu unsigned int user;
33601ea2a07aSLu Baolu
33611ea2a07aSLu Baolu mutex_lock(&group->mutex);
33621ea2a07aSLu Baolu user = group->owner_cnt;
33631ea2a07aSLu Baolu mutex_unlock(&group->mutex);
33641ea2a07aSLu Baolu
33651ea2a07aSLu Baolu return user;
33661ea2a07aSLu Baolu }
33671ea2a07aSLu Baolu EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
336816603704SLu Baolu
__iommu_set_group_pasid(struct iommu_domain * domain,struct iommu_group * group,ioasid_t pasid)336916603704SLu Baolu static int __iommu_set_group_pasid(struct iommu_domain *domain,
337016603704SLu Baolu struct iommu_group *group, ioasid_t pasid)
337116603704SLu Baolu {
3372*6b4f6939SYi Liu struct group_device *device, *last_gdev;
3373*6b4f6939SYi Liu int ret;
337416603704SLu Baolu
33753006b15bSJason Gunthorpe for_each_group_device(group, device) {
337616603704SLu Baolu ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
337716603704SLu Baolu if (ret)
3378*6b4f6939SYi Liu goto err_revert;
337916603704SLu Baolu }
338016603704SLu Baolu
3381*6b4f6939SYi Liu return 0;
3382*6b4f6939SYi Liu
3383*6b4f6939SYi Liu err_revert:
3384*6b4f6939SYi Liu last_gdev = device;
3385*6b4f6939SYi Liu for_each_group_device(group, device) {
3386*6b4f6939SYi Liu const struct iommu_ops *ops = dev_iommu_ops(device->dev);
3387*6b4f6939SYi Liu
3388*6b4f6939SYi Liu if (device == last_gdev)
3389*6b4f6939SYi Liu break;
3390*6b4f6939SYi Liu ops->remove_dev_pasid(device->dev, pasid);
3391*6b4f6939SYi Liu }
339216603704SLu Baolu return ret;
339316603704SLu Baolu }
339416603704SLu Baolu
__iommu_remove_group_pasid(struct iommu_group * group,ioasid_t pasid)339516603704SLu Baolu static void __iommu_remove_group_pasid(struct iommu_group *group,
339616603704SLu Baolu ioasid_t pasid)
339716603704SLu Baolu {
339816603704SLu Baolu struct group_device *device;
339916603704SLu Baolu const struct iommu_ops *ops;
340016603704SLu Baolu
34013006b15bSJason Gunthorpe for_each_group_device(group, device) {
340216603704SLu Baolu ops = dev_iommu_ops(device->dev);
340316603704SLu Baolu ops->remove_dev_pasid(device->dev, pasid);
340416603704SLu Baolu }
340516603704SLu Baolu }
340616603704SLu Baolu
340716603704SLu Baolu /*
340816603704SLu Baolu * iommu_attach_device_pasid() - Attach a domain to pasid of device
340916603704SLu Baolu * @domain: the iommu domain.
341016603704SLu Baolu * @dev: the attached device.
341116603704SLu Baolu * @pasid: the pasid of the device.
341216603704SLu Baolu *
341316603704SLu Baolu * Return: 0 on success, or an error.
341416603704SLu Baolu */
iommu_attach_device_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t pasid)341516603704SLu Baolu int iommu_attach_device_pasid(struct iommu_domain *domain,
341616603704SLu Baolu struct device *dev, ioasid_t pasid)
341716603704SLu Baolu {
341816603704SLu Baolu struct iommu_group *group;
341916603704SLu Baolu void *curr;
342016603704SLu Baolu int ret;
342116603704SLu Baolu
342216603704SLu Baolu if (!domain->ops->set_dev_pasid)
342316603704SLu Baolu return -EOPNOTSUPP;
342416603704SLu Baolu
342516603704SLu Baolu group = iommu_group_get(dev);
342616603704SLu Baolu if (!group)
342716603704SLu Baolu return -ENODEV;
342816603704SLu Baolu
342916603704SLu Baolu mutex_lock(&group->mutex);
343016603704SLu Baolu curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
343116603704SLu Baolu if (curr) {
343216603704SLu Baolu ret = xa_err(curr) ? : -EBUSY;
343316603704SLu Baolu goto out_unlock;
343416603704SLu Baolu }
343516603704SLu Baolu
343616603704SLu Baolu ret = __iommu_set_group_pasid(domain, group, pasid);
3437*6b4f6939SYi Liu if (ret)
343816603704SLu Baolu xa_erase(&group->pasid_array, pasid);
343916603704SLu Baolu out_unlock:
344016603704SLu Baolu mutex_unlock(&group->mutex);
344116603704SLu Baolu iommu_group_put(group);
344216603704SLu Baolu
344316603704SLu Baolu return ret;
344416603704SLu Baolu }
344516603704SLu Baolu EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
344616603704SLu Baolu
344716603704SLu Baolu /*
344816603704SLu Baolu * iommu_detach_device_pasid() - Detach the domain from pasid of device
344916603704SLu Baolu * @domain: the iommu domain.
345016603704SLu Baolu * @dev: the attached device.
345116603704SLu Baolu * @pasid: the pasid of the device.
345216603704SLu Baolu *
345316603704SLu Baolu * The @domain must have been attached to @pasid of the @dev with
345416603704SLu Baolu * iommu_attach_device_pasid().
345516603704SLu Baolu */
iommu_detach_device_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t pasid)345616603704SLu Baolu void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
345716603704SLu Baolu ioasid_t pasid)
345816603704SLu Baolu {
345916603704SLu Baolu struct iommu_group *group = iommu_group_get(dev);
346016603704SLu Baolu
346116603704SLu Baolu mutex_lock(&group->mutex);
346216603704SLu Baolu __iommu_remove_group_pasid(group, pasid);
346316603704SLu Baolu WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
346416603704SLu Baolu mutex_unlock(&group->mutex);
346516603704SLu Baolu
346616603704SLu Baolu iommu_group_put(group);
346716603704SLu Baolu }
346816603704SLu Baolu EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
346916603704SLu Baolu
347016603704SLu Baolu /*
347116603704SLu Baolu * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
347216603704SLu Baolu * @dev: the queried device
347316603704SLu Baolu * @pasid: the pasid of the device
347416603704SLu Baolu * @type: matched domain type, 0 for any match
347516603704SLu Baolu *
347616603704SLu Baolu * This is a variant of iommu_get_domain_for_dev(). It returns the existing
347716603704SLu Baolu * domain attached to pasid of a device. Callers must hold a lock around this
347816603704SLu Baolu * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
347916603704SLu Baolu * type is being manipulated. This API does not internally resolve races with
348016603704SLu Baolu * attach/detach.
348116603704SLu Baolu *
348216603704SLu Baolu * Return: attached domain on success, NULL otherwise.
348316603704SLu Baolu */
iommu_get_domain_for_dev_pasid(struct device * dev,ioasid_t pasid,unsigned int type)348416603704SLu Baolu struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
348516603704SLu Baolu ioasid_t pasid,
348616603704SLu Baolu unsigned int type)
348716603704SLu Baolu {
348816603704SLu Baolu struct iommu_domain *domain;
348916603704SLu Baolu struct iommu_group *group;
349016603704SLu Baolu
349116603704SLu Baolu group = iommu_group_get(dev);
349216603704SLu Baolu if (!group)
349316603704SLu Baolu return NULL;
349416603704SLu Baolu
349516603704SLu Baolu xa_lock(&group->pasid_array);
349616603704SLu Baolu domain = xa_load(&group->pasid_array, pasid);
349716603704SLu Baolu if (type && domain && domain->type != type)
349816603704SLu Baolu domain = ERR_PTR(-EBUSY);
349916603704SLu Baolu xa_unlock(&group->pasid_array);
350016603704SLu Baolu iommu_group_put(group);
350116603704SLu Baolu
350216603704SLu Baolu return domain;
350316603704SLu Baolu }
350416603704SLu Baolu EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
350513646796SLu Baolu
iommu_sva_domain_alloc(struct device * dev,struct mm_struct * mm)350613646796SLu Baolu struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
350713646796SLu Baolu struct mm_struct *mm)
350813646796SLu Baolu {
350913646796SLu Baolu const struct iommu_ops *ops = dev_iommu_ops(dev);
351013646796SLu Baolu struct iommu_domain *domain;
351113646796SLu Baolu
351213646796SLu Baolu domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
351313646796SLu Baolu if (!domain)
351413646796SLu Baolu return NULL;
351513646796SLu Baolu
351613646796SLu Baolu domain->type = IOMMU_DOMAIN_SVA;
351713646796SLu Baolu mmgrab(mm);
351813646796SLu Baolu domain->mm = mm;
35198cc93159SLu Baolu domain->iopf_handler = iommu_sva_handle_iopf;
35208cc93159SLu Baolu domain->fault_data = mm;
352113646796SLu Baolu
352213646796SLu Baolu return domain;
352313646796SLu Baolu }
35242dcebc7dSJacob Pan
iommu_alloc_global_pasid(struct device * dev)35252dcebc7dSJacob Pan ioasid_t iommu_alloc_global_pasid(struct device *dev)
35262dcebc7dSJacob Pan {
35272dcebc7dSJacob Pan int ret;
35282dcebc7dSJacob Pan
35292dcebc7dSJacob Pan /* max_pasids == 0 means that the device does not support PASID */
35302dcebc7dSJacob Pan if (!dev->iommu->max_pasids)
35312dcebc7dSJacob Pan return IOMMU_PASID_INVALID;
35322dcebc7dSJacob Pan
35332dcebc7dSJacob Pan /*
35342dcebc7dSJacob Pan * max_pasids is set up by vendor driver based on number of PASID bits
35352dcebc7dSJacob Pan * supported but the IDA allocation is inclusive.
35362dcebc7dSJacob Pan */
35372dcebc7dSJacob Pan ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
35382dcebc7dSJacob Pan dev->iommu->max_pasids - 1, GFP_KERNEL);
35392dcebc7dSJacob Pan return ret < 0 ? IOMMU_PASID_INVALID : ret;
35402dcebc7dSJacob Pan }
35412dcebc7dSJacob Pan EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
35422dcebc7dSJacob Pan
iommu_free_global_pasid(ioasid_t pasid)35432dcebc7dSJacob Pan void iommu_free_global_pasid(ioasid_t pasid)
35442dcebc7dSJacob Pan {
35452dcebc7dSJacob Pan if (WARN_ON(pasid == IOMMU_PASID_INVALID))
35462dcebc7dSJacob Pan return;
35472dcebc7dSJacob Pan
35482dcebc7dSJacob Pan ida_free(&iommu_global_pasid_ida, pasid);
35492dcebc7dSJacob Pan }
35502dcebc7dSJacob Pan EXPORT_SYMBOL_GPL(iommu_free_global_pasid);
3551