1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 */
6
7#define pr_fmt(fmt)    "iommu: " fmt
8
9#include <linux/amba/bus.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/bits.h>
13#include <linux/bug.h>
14#include <linux/types.h>
15#include <linux/init.h>
16#include <linux/export.h>
17#include <linux/slab.h>
18#include <linux/errno.h>
19#include <linux/host1x_context_bus.h>
20#include <linux/iommu.h>
21#include <linux/idr.h>
22#include <linux/err.h>
23#include <linux/pci.h>
24#include <linux/pci-ats.h>
25#include <linux/bitops.h>
26#include <linux/platform_device.h>
27#include <linux/property.h>
28#include <linux/fsl/mc.h>
29#include <linux/module.h>
30#include <linux/cc_platform.h>
31#include <linux/cdx/cdx_bus.h>
32#include <trace/events/iommu.h>
33#include <linux/sched/mm.h>
34#include <linux/msi.h>
35
36#include "dma-iommu.h"
37#include "iommu-priv.h"
38
39static struct kset *iommu_group_kset;
40static DEFINE_IDA(iommu_group_ida);
41static DEFINE_IDA(iommu_global_pasid_ida);
42
43static unsigned int iommu_def_domain_type __read_mostly;
44static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
45static u32 iommu_cmd_line __read_mostly;
46
47struct iommu_group {
48	struct kobject kobj;
49	struct kobject *devices_kobj;
50	struct list_head devices;
51	struct xarray pasid_array;
52	struct mutex mutex;
53	void *iommu_data;
54	void (*iommu_data_release)(void *iommu_data);
55	char *name;
56	int id;
57	struct iommu_domain *default_domain;
58	struct iommu_domain *blocking_domain;
59	struct iommu_domain *domain;
60	struct list_head entry;
61	unsigned int owner_cnt;
62	void *owner;
63};
64
65struct group_device {
66	struct list_head list;
67	struct device *dev;
68	char *name;
69};
70
71/* Iterate over each struct group_device in a struct iommu_group */
72#define for_each_group_device(group, pos) \
73	list_for_each_entry(pos, &(group)->devices, list)
74
75struct iommu_group_attribute {
76	struct attribute attr;
77	ssize_t (*show)(struct iommu_group *group, char *buf);
78	ssize_t (*store)(struct iommu_group *group,
79			 const char *buf, size_t count);
80};
81
82static const char * const iommu_group_resv_type_string[] = {
83	[IOMMU_RESV_DIRECT]			= "direct",
84	[IOMMU_RESV_DIRECT_RELAXABLE]		= "direct-relaxable",
85	[IOMMU_RESV_RESERVED]			= "reserved",
86	[IOMMU_RESV_MSI]			= "msi",
87	[IOMMU_RESV_SW_MSI]			= "msi",
88};
89
90#define IOMMU_CMD_LINE_DMA_API		BIT(0)
91#define IOMMU_CMD_LINE_STRICT		BIT(1)
92
93static int iommu_bus_notifier(struct notifier_block *nb,
94			      unsigned long action, void *data);
95static void iommu_release_device(struct device *dev);
96static struct iommu_domain *
97__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type);
98static int __iommu_attach_device(struct iommu_domain *domain,
99				 struct device *dev);
100static int __iommu_attach_group(struct iommu_domain *domain,
101				struct iommu_group *group);
102
103enum {
104	IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
105};
106
107static int __iommu_device_set_domain(struct iommu_group *group,
108				     struct device *dev,
109				     struct iommu_domain *new_domain,
110				     unsigned int flags);
111static int __iommu_group_set_domain_internal(struct iommu_group *group,
112					     struct iommu_domain *new_domain,
113					     unsigned int flags);
114static int __iommu_group_set_domain(struct iommu_group *group,
115				    struct iommu_domain *new_domain)
116{
117	return __iommu_group_set_domain_internal(group, new_domain, 0);
118}
119static void __iommu_group_set_domain_nofail(struct iommu_group *group,
120					    struct iommu_domain *new_domain)
121{
122	WARN_ON(__iommu_group_set_domain_internal(
123		group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
124}
125
126static int iommu_setup_default_domain(struct iommu_group *group,
127				      int target_type);
128static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
129					       struct device *dev);
130static ssize_t iommu_group_store_type(struct iommu_group *group,
131				      const char *buf, size_t count);
132static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
133						     struct device *dev);
134static void __iommu_group_free_device(struct iommu_group *group,
135				      struct group_device *grp_dev);
136
137#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store)		\
138struct iommu_group_attribute iommu_group_attr_##_name =		\
139	__ATTR(_name, _mode, _show, _store)
140
141#define to_iommu_group_attr(_attr)	\
142	container_of(_attr, struct iommu_group_attribute, attr)
143#define to_iommu_group(_kobj)		\
144	container_of(_kobj, struct iommu_group, kobj)
145
146static LIST_HEAD(iommu_device_list);
147static DEFINE_SPINLOCK(iommu_device_lock);
148
149static const struct bus_type * const iommu_buses[] = {
150	&platform_bus_type,
151#ifdef CONFIG_PCI
152	&pci_bus_type,
153#endif
154#ifdef CONFIG_ARM_AMBA
155	&amba_bustype,
156#endif
157#ifdef CONFIG_FSL_MC_BUS
158	&fsl_mc_bus_type,
159#endif
160#ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
161	&host1x_context_device_bus_type,
162#endif
163#ifdef CONFIG_CDX_BUS
164	&cdx_bus_type,
165#endif
166};
167
168/*
169 * Use a function instead of an array here because the domain-type is a
170 * bit-field, so an array would waste memory.
171 */
172static const char *iommu_domain_type_str(unsigned int t)
173{
174	switch (t) {
175	case IOMMU_DOMAIN_BLOCKED:
176		return "Blocked";
177	case IOMMU_DOMAIN_IDENTITY:
178		return "Passthrough";
179	case IOMMU_DOMAIN_UNMANAGED:
180		return "Unmanaged";
181	case IOMMU_DOMAIN_DMA:
182	case IOMMU_DOMAIN_DMA_FQ:
183		return "Translated";
184	case IOMMU_DOMAIN_PLATFORM:
185		return "Platform";
186	default:
187		return "Unknown";
188	}
189}
190
191static int __init iommu_subsys_init(void)
192{
193	struct notifier_block *nb;
194
195	if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
196		if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
197			iommu_set_default_passthrough(false);
198		else
199			iommu_set_default_translated(false);
200
201		if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
202			pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
203			iommu_set_default_translated(false);
204		}
205	}
206
207	if (!iommu_default_passthrough() && !iommu_dma_strict)
208		iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
209
210	pr_info("Default domain type: %s%s\n",
211		iommu_domain_type_str(iommu_def_domain_type),
212		(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
213			" (set via kernel command line)" : "");
214
215	if (!iommu_default_passthrough())
216		pr_info("DMA domain TLB invalidation policy: %s mode%s\n",
217			iommu_dma_strict ? "strict" : "lazy",
218			(iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
219				" (set via kernel command line)" : "");
220
221	nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL);
222	if (!nb)
223		return -ENOMEM;
224
225	for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
226		nb[i].notifier_call = iommu_bus_notifier;
227		bus_register_notifier(iommu_buses[i], &nb[i]);
228	}
229
230	return 0;
231}
232subsys_initcall(iommu_subsys_init);
233
234static int remove_iommu_group(struct device *dev, void *data)
235{
236	if (dev->iommu && dev->iommu->iommu_dev == data)
237		iommu_release_device(dev);
238
239	return 0;
240}
241
242/**
243 * iommu_device_register() - Register an IOMMU hardware instance
244 * @iommu: IOMMU handle for the instance
245 * @ops:   IOMMU ops to associate with the instance
246 * @hwdev: (optional) actual instance device, used for fwnode lookup
247 *
248 * Return: 0 on success, or an error.
249 */
250int iommu_device_register(struct iommu_device *iommu,
251			  const struct iommu_ops *ops, struct device *hwdev)
252{
253	int err = 0;
254
255	/* We need to be able to take module references appropriately */
256	if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
257		return -EINVAL;
258
259	iommu->ops = ops;
260	if (hwdev)
261		iommu->fwnode = dev_fwnode(hwdev);
262
263	spin_lock(&iommu_device_lock);
264	list_add_tail(&iommu->list, &iommu_device_list);
265	spin_unlock(&iommu_device_lock);
266
267	for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++)
268		err = bus_iommu_probe(iommu_buses[i]);
269	if (err)
270		iommu_device_unregister(iommu);
271	return err;
272}
273EXPORT_SYMBOL_GPL(iommu_device_register);
274
275void iommu_device_unregister(struct iommu_device *iommu)
276{
277	for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
278		bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group);
279
280	spin_lock(&iommu_device_lock);
281	list_del(&iommu->list);
282	spin_unlock(&iommu_device_lock);
283
284	/* Pairs with the alloc in generic_single_device_group() */
285	iommu_group_put(iommu->singleton_group);
286	iommu->singleton_group = NULL;
287}
288EXPORT_SYMBOL_GPL(iommu_device_unregister);
289
290#if IS_ENABLED(CONFIG_IOMMUFD_TEST)
291void iommu_device_unregister_bus(struct iommu_device *iommu,
292				 const struct bus_type *bus,
293				 struct notifier_block *nb)
294{
295	bus_unregister_notifier(bus, nb);
296	iommu_device_unregister(iommu);
297}
298EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
299
300/*
301 * Register an iommu driver against a single bus. This is only used by iommufd
302 * selftest to create a mock iommu driver. The caller must provide
303 * some memory to hold a notifier_block.
304 */
305int iommu_device_register_bus(struct iommu_device *iommu,
306			      const struct iommu_ops *ops,
307			      const struct bus_type *bus,
308			      struct notifier_block *nb)
309{
310	int err;
311
312	iommu->ops = ops;
313	nb->notifier_call = iommu_bus_notifier;
314	err = bus_register_notifier(bus, nb);
315	if (err)
316		return err;
317
318	spin_lock(&iommu_device_lock);
319	list_add_tail(&iommu->list, &iommu_device_list);
320	spin_unlock(&iommu_device_lock);
321
322	err = bus_iommu_probe(bus);
323	if (err) {
324		iommu_device_unregister_bus(iommu, bus, nb);
325		return err;
326	}
327	return 0;
328}
329EXPORT_SYMBOL_GPL(iommu_device_register_bus);
330#endif
331
332static struct dev_iommu *dev_iommu_get(struct device *dev)
333{
334	struct dev_iommu *param = dev->iommu;
335
336	lockdep_assert_held(&iommu_probe_device_lock);
337
338	if (param)
339		return param;
340
341	param = kzalloc(sizeof(*param), GFP_KERNEL);
342	if (!param)
343		return NULL;
344
345	mutex_init(&param->lock);
346	dev->iommu = param;
347	return param;
348}
349
350static void dev_iommu_free(struct device *dev)
351{
352	struct dev_iommu *param = dev->iommu;
353
354	dev->iommu = NULL;
355	if (param->fwspec) {
356		fwnode_handle_put(param->fwspec->iommu_fwnode);
357		kfree(param->fwspec);
358	}
359	kfree(param);
360}
361
362/*
363 * Internal equivalent of device_iommu_mapped() for when we care that a device
364 * actually has API ops, and don't want false positives from VFIO-only groups.
365 */
366static bool dev_has_iommu(struct device *dev)
367{
368	return dev->iommu && dev->iommu->iommu_dev;
369}
370
371static u32 dev_iommu_get_max_pasids(struct device *dev)
372{
373	u32 max_pasids = 0, bits = 0;
374	int ret;
375
376	if (dev_is_pci(dev)) {
377		ret = pci_max_pasids(to_pci_dev(dev));
378		if (ret > 0)
379			max_pasids = ret;
380	} else {
381		ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
382		if (!ret)
383			max_pasids = 1UL << bits;
384	}
385
386	return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
387}
388
389void dev_iommu_priv_set(struct device *dev, void *priv)
390{
391	/* FSL_PAMU does something weird */
392	if (!IS_ENABLED(CONFIG_FSL_PAMU))
393		lockdep_assert_held(&iommu_probe_device_lock);
394	dev->iommu->priv = priv;
395}
396EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
397
398/*
399 * Init the dev->iommu and dev->iommu_group in the struct device and get the
400 * driver probed
401 */
402static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
403{
404	struct iommu_device *iommu_dev;
405	struct iommu_group *group;
406	int ret;
407
408	if (!dev_iommu_get(dev))
409		return -ENOMEM;
410
411	if (!try_module_get(ops->owner)) {
412		ret = -EINVAL;
413		goto err_free;
414	}
415
416	iommu_dev = ops->probe_device(dev);
417	if (IS_ERR(iommu_dev)) {
418		ret = PTR_ERR(iommu_dev);
419		goto err_module_put;
420	}
421	dev->iommu->iommu_dev = iommu_dev;
422
423	ret = iommu_device_link(iommu_dev, dev);
424	if (ret)
425		goto err_release;
426
427	group = ops->device_group(dev);
428	if (WARN_ON_ONCE(group == NULL))
429		group = ERR_PTR(-EINVAL);
430	if (IS_ERR(group)) {
431		ret = PTR_ERR(group);
432		goto err_unlink;
433	}
434	dev->iommu_group = group;
435
436	dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
437	if (ops->is_attach_deferred)
438		dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
439	return 0;
440
441err_unlink:
442	iommu_device_unlink(iommu_dev, dev);
443err_release:
444	if (ops->release_device)
445		ops->release_device(dev);
446err_module_put:
447	module_put(ops->owner);
448err_free:
449	dev->iommu->iommu_dev = NULL;
450	dev_iommu_free(dev);
451	return ret;
452}
453
454static void iommu_deinit_device(struct device *dev)
455{
456	struct iommu_group *group = dev->iommu_group;
457	const struct iommu_ops *ops = dev_iommu_ops(dev);
458
459	lockdep_assert_held(&group->mutex);
460
461	iommu_device_unlink(dev->iommu->iommu_dev, dev);
462
463	/*
464	 * release_device() must stop using any attached domain on the device.
465	 * If there are still other devices in the group, they are not affected
466	 * by this callback.
467	 *
468	 * If the iommu driver provides release_domain, the core code ensures
469	 * that domain is attached prior to calling release_device. Drivers can
470	 * use this to enforce a translation on the idle iommu. Typically, the
471	 * global static blocked_domain is a good choice.
472	 *
473	 * Otherwise, the iommu driver must set the device to either an identity
474	 * or a blocking translation in release_device() and stop using any
475	 * domain pointer, as it is going to be freed.
476	 *
477	 * Regardless, if a delayed attach never occurred, then the release
478	 * should still avoid touching any hardware configuration either.
479	 */
480	if (!dev->iommu->attach_deferred && ops->release_domain)
481		ops->release_domain->ops->attach_dev(ops->release_domain, dev);
482
483	if (ops->release_device)
484		ops->release_device(dev);
485
486	/*
487	 * If this is the last driver to use the group then we must free the
488	 * domains before we do the module_put().
489	 */
490	if (list_empty(&group->devices)) {
491		if (group->default_domain) {
492			iommu_domain_free(group->default_domain);
493			group->default_domain = NULL;
494		}
495		if (group->blocking_domain) {
496			iommu_domain_free(group->blocking_domain);
497			group->blocking_domain = NULL;
498		}
499		group->domain = NULL;
500	}
501
502	/* Caller must put iommu_group */
503	dev->iommu_group = NULL;
504	module_put(ops->owner);
505	dev_iommu_free(dev);
506}
507
508DEFINE_MUTEX(iommu_probe_device_lock);
509
510static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
511{
512	const struct iommu_ops *ops;
513	struct iommu_fwspec *fwspec;
514	struct iommu_group *group;
515	struct group_device *gdev;
516	int ret;
517
518	/*
519	 * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU
520	 * instances with non-NULL fwnodes, and client devices should have been
521	 * identified with a fwspec by this point. Otherwise, we can currently
522	 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
523	 * be present, and that any of their registered instances has suitable
524	 * ops for probing, and thus cheekily co-opt the same mechanism.
525	 */
526	fwspec = dev_iommu_fwspec_get(dev);
527	if (fwspec && fwspec->ops)
528		ops = fwspec->ops;
529	else
530		ops = iommu_ops_from_fwnode(NULL);
531
532	if (!ops)
533		return -ENODEV;
534	/*
535	 * Serialise to avoid races between IOMMU drivers registering in
536	 * parallel and/or the "replay" calls from ACPI/OF code via client
537	 * driver probe. Once the latter have been cleaned up we should
538	 * probably be able to use device_lock() here to minimise the scope,
539	 * but for now enforcing a simple global ordering is fine.
540	 */
541	lockdep_assert_held(&iommu_probe_device_lock);
542
543	/* Device is probed already if in a group */
544	if (dev->iommu_group)
545		return 0;
546
547	ret = iommu_init_device(dev, ops);
548	if (ret)
549		return ret;
550
551	group = dev->iommu_group;
552	gdev = iommu_group_alloc_device(group, dev);
553	mutex_lock(&group->mutex);
554	if (IS_ERR(gdev)) {
555		ret = PTR_ERR(gdev);
556		goto err_put_group;
557	}
558
559	/*
560	 * The gdev must be in the list before calling
561	 * iommu_setup_default_domain()
562	 */
563	list_add_tail(&gdev->list, &group->devices);
564	WARN_ON(group->default_domain && !group->domain);
565	if (group->default_domain)
566		iommu_create_device_direct_mappings(group->default_domain, dev);
567	if (group->domain) {
568		ret = __iommu_device_set_domain(group, dev, group->domain, 0);
569		if (ret)
570			goto err_remove_gdev;
571	} else if (!group->default_domain && !group_list) {
572		ret = iommu_setup_default_domain(group, 0);
573		if (ret)
574			goto err_remove_gdev;
575	} else if (!group->default_domain) {
576		/*
577		 * With a group_list argument we defer the default_domain setup
578		 * to the caller by providing a de-duplicated list of groups
579		 * that need further setup.
580		 */
581		if (list_empty(&group->entry))
582			list_add_tail(&group->entry, group_list);
583	}
584	mutex_unlock(&group->mutex);
585
586	if (dev_is_pci(dev))
587		iommu_dma_set_pci_32bit_workaround(dev);
588
589	return 0;
590
591err_remove_gdev:
592	list_del(&gdev->list);
593	__iommu_group_free_device(group, gdev);
594err_put_group:
595	iommu_deinit_device(dev);
596	mutex_unlock(&group->mutex);
597	iommu_group_put(group);
598
599	return ret;
600}
601
602int iommu_probe_device(struct device *dev)
603{
604	const struct iommu_ops *ops;
605	int ret;
606
607	mutex_lock(&iommu_probe_device_lock);
608	ret = __iommu_probe_device(dev, NULL);
609	mutex_unlock(&iommu_probe_device_lock);
610	if (ret)
611		return ret;
612
613	ops = dev_iommu_ops(dev);
614	if (ops->probe_finalize)
615		ops->probe_finalize(dev);
616
617	return 0;
618}
619
620static void __iommu_group_free_device(struct iommu_group *group,
621				      struct group_device *grp_dev)
622{
623	struct device *dev = grp_dev->dev;
624
625	sysfs_remove_link(group->devices_kobj, grp_dev->name);
626	sysfs_remove_link(&dev->kobj, "iommu_group");
627
628	trace_remove_device_from_group(group->id, dev);
629
630	/*
631	 * If the group has become empty then ownership must have been
632	 * released, and the current domain must be set back to NULL or
633	 * the default domain.
634	 */
635	if (list_empty(&group->devices))
636		WARN_ON(group->owner_cnt ||
637			group->domain != group->default_domain);
638
639	kfree(grp_dev->name);
640	kfree(grp_dev);
641}
642
643/* Remove the iommu_group from the struct device. */
644static void __iommu_group_remove_device(struct device *dev)
645{
646	struct iommu_group *group = dev->iommu_group;
647	struct group_device *device;
648
649	mutex_lock(&group->mutex);
650	for_each_group_device(group, device) {
651		if (device->dev != dev)
652			continue;
653
654		list_del(&device->list);
655		__iommu_group_free_device(group, device);
656		if (dev_has_iommu(dev))
657			iommu_deinit_device(dev);
658		else
659			dev->iommu_group = NULL;
660		break;
661	}
662	mutex_unlock(&group->mutex);
663
664	/*
665	 * Pairs with the get in iommu_init_device() or
666	 * iommu_group_add_device()
667	 */
668	iommu_group_put(group);
669}
670
671static void iommu_release_device(struct device *dev)
672{
673	struct iommu_group *group = dev->iommu_group;
674
675	if (group)
676		__iommu_group_remove_device(dev);
677
678	/* Free any fwspec if no iommu_driver was ever attached */
679	if (dev->iommu)
680		dev_iommu_free(dev);
681}
682
683static int __init iommu_set_def_domain_type(char *str)
684{
685	bool pt;
686	int ret;
687
688	ret = kstrtobool(str, &pt);
689	if (ret)
690		return ret;
691
692	if (pt)
693		iommu_set_default_passthrough(true);
694	else
695		iommu_set_default_translated(true);
696
697	return 0;
698}
699early_param("iommu.passthrough", iommu_set_def_domain_type);
700
701static int __init iommu_dma_setup(char *str)
702{
703	int ret = kstrtobool(str, &iommu_dma_strict);
704
705	if (!ret)
706		iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
707	return ret;
708}
709early_param("iommu.strict", iommu_dma_setup);
710
711void iommu_set_dma_strict(void)
712{
713	iommu_dma_strict = true;
714	if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
715		iommu_def_domain_type = IOMMU_DOMAIN_DMA;
716}
717
718static ssize_t iommu_group_attr_show(struct kobject *kobj,
719				     struct attribute *__attr, char *buf)
720{
721	struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
722	struct iommu_group *group = to_iommu_group(kobj);
723	ssize_t ret = -EIO;
724
725	if (attr->show)
726		ret = attr->show(group, buf);
727	return ret;
728}
729
730static ssize_t iommu_group_attr_store(struct kobject *kobj,
731				      struct attribute *__attr,
732				      const char *buf, size_t count)
733{
734	struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
735	struct iommu_group *group = to_iommu_group(kobj);
736	ssize_t ret = -EIO;
737
738	if (attr->store)
739		ret = attr->store(group, buf, count);
740	return ret;
741}
742
743static const struct sysfs_ops iommu_group_sysfs_ops = {
744	.show = iommu_group_attr_show,
745	.store = iommu_group_attr_store,
746};
747
748static int iommu_group_create_file(struct iommu_group *group,
749				   struct iommu_group_attribute *attr)
750{
751	return sysfs_create_file(&group->kobj, &attr->attr);
752}
753
754static void iommu_group_remove_file(struct iommu_group *group,
755				    struct iommu_group_attribute *attr)
756{
757	sysfs_remove_file(&group->kobj, &attr->attr);
758}
759
760static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
761{
762	return sysfs_emit(buf, "%s\n", group->name);
763}
764
765/**
766 * iommu_insert_resv_region - Insert a new region in the
767 * list of reserved regions.
768 * @new: new region to insert
769 * @regions: list of regions
770 *
771 * Elements are sorted by start address and overlapping segments
772 * of the same type are merged.
773 */
774static int iommu_insert_resv_region(struct iommu_resv_region *new,
775				    struct list_head *regions)
776{
777	struct iommu_resv_region *iter, *tmp, *nr, *top;
778	LIST_HEAD(stack);
779
780	nr = iommu_alloc_resv_region(new->start, new->length,
781				     new->prot, new->type, GFP_KERNEL);
782	if (!nr)
783		return -ENOMEM;
784
785	/* First add the new element based on start address sorting */
786	list_for_each_entry(iter, regions, list) {
787		if (nr->start < iter->start ||
788		    (nr->start == iter->start && nr->type <= iter->type))
789			break;
790	}
791	list_add_tail(&nr->list, &iter->list);
792
793	/* Merge overlapping segments of type nr->type in @regions, if any */
794	list_for_each_entry_safe(iter, tmp, regions, list) {
795		phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
796
797		/* no merge needed on elements of different types than @new */
798		if (iter->type != new->type) {
799			list_move_tail(&iter->list, &stack);
800			continue;
801		}
802
803		/* look for the last stack element of same type as @iter */
804		list_for_each_entry_reverse(top, &stack, list)
805			if (top->type == iter->type)
806				goto check_overlap;
807
808		list_move_tail(&iter->list, &stack);
809		continue;
810
811check_overlap:
812		top_end = top->start + top->length - 1;
813
814		if (iter->start > top_end + 1) {
815			list_move_tail(&iter->list, &stack);
816		} else {
817			top->length = max(top_end, iter_end) - top->start + 1;
818			list_del(&iter->list);
819			kfree(iter);
820		}
821	}
822	list_splice(&stack, regions);
823	return 0;
824}
825
826static int
827iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
828				 struct list_head *group_resv_regions)
829{
830	struct iommu_resv_region *entry;
831	int ret = 0;
832
833	list_for_each_entry(entry, dev_resv_regions, list) {
834		ret = iommu_insert_resv_region(entry, group_resv_regions);
835		if (ret)
836			break;
837	}
838	return ret;
839}
840
841int iommu_get_group_resv_regions(struct iommu_group *group,
842				 struct list_head *head)
843{
844	struct group_device *device;
845	int ret = 0;
846
847	mutex_lock(&group->mutex);
848	for_each_group_device(group, device) {
849		struct list_head dev_resv_regions;
850
851		/*
852		 * Non-API groups still expose reserved_regions in sysfs,
853		 * so filter out calls that get here that way.
854		 */
855		if (!dev_has_iommu(device->dev))
856			break;
857
858		INIT_LIST_HEAD(&dev_resv_regions);
859		iommu_get_resv_regions(device->dev, &dev_resv_regions);
860		ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
861		iommu_put_resv_regions(device->dev, &dev_resv_regions);
862		if (ret)
863			break;
864	}
865	mutex_unlock(&group->mutex);
866	return ret;
867}
868EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
869
870static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
871					     char *buf)
872{
873	struct iommu_resv_region *region, *next;
874	struct list_head group_resv_regions;
875	int offset = 0;
876
877	INIT_LIST_HEAD(&group_resv_regions);
878	iommu_get_group_resv_regions(group, &group_resv_regions);
879
880	list_for_each_entry_safe(region, next, &group_resv_regions, list) {
881		offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
882					(long long)region->start,
883					(long long)(region->start +
884						    region->length - 1),
885					iommu_group_resv_type_string[region->type]);
886		kfree(region);
887	}
888
889	return offset;
890}
891
892static ssize_t iommu_group_show_type(struct iommu_group *group,
893				     char *buf)
894{
895	char *type = "unknown";
896
897	mutex_lock(&group->mutex);
898	if (group->default_domain) {
899		switch (group->default_domain->type) {
900		case IOMMU_DOMAIN_BLOCKED:
901			type = "blocked";
902			break;
903		case IOMMU_DOMAIN_IDENTITY:
904			type = "identity";
905			break;
906		case IOMMU_DOMAIN_UNMANAGED:
907			type = "unmanaged";
908			break;
909		case IOMMU_DOMAIN_DMA:
910			type = "DMA";
911			break;
912		case IOMMU_DOMAIN_DMA_FQ:
913			type = "DMA-FQ";
914			break;
915		}
916	}
917	mutex_unlock(&group->mutex);
918
919	return sysfs_emit(buf, "%s\n", type);
920}
921
922static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
923
924static IOMMU_GROUP_ATTR(reserved_regions, 0444,
925			iommu_group_show_resv_regions, NULL);
926
927static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
928			iommu_group_store_type);
929
930static void iommu_group_release(struct kobject *kobj)
931{
932	struct iommu_group *group = to_iommu_group(kobj);
933
934	pr_debug("Releasing group %d\n", group->id);
935
936	if (group->iommu_data_release)
937		group->iommu_data_release(group->iommu_data);
938
939	ida_free(&iommu_group_ida, group->id);
940
941	/* Domains are free'd by iommu_deinit_device() */
942	WARN_ON(group->default_domain);
943	WARN_ON(group->blocking_domain);
944
945	kfree(group->name);
946	kfree(group);
947}
948
949static const struct kobj_type iommu_group_ktype = {
950	.sysfs_ops = &iommu_group_sysfs_ops,
951	.release = iommu_group_release,
952};
953
954/**
955 * iommu_group_alloc - Allocate a new group
956 *
957 * This function is called by an iommu driver to allocate a new iommu
958 * group.  The iommu group represents the minimum granularity of the iommu.
959 * Upon successful return, the caller holds a reference to the supplied
960 * group in order to hold the group until devices are added.  Use
961 * iommu_group_put() to release this extra reference count, allowing the
962 * group to be automatically reclaimed once it has no devices or external
963 * references.
964 */
965struct iommu_group *iommu_group_alloc(void)
966{
967	struct iommu_group *group;
968	int ret;
969
970	group = kzalloc(sizeof(*group), GFP_KERNEL);
971	if (!group)
972		return ERR_PTR(-ENOMEM);
973
974	group->kobj.kset = iommu_group_kset;
975	mutex_init(&group->mutex);
976	INIT_LIST_HEAD(&group->devices);
977	INIT_LIST_HEAD(&group->entry);
978	xa_init(&group->pasid_array);
979
980	ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
981	if (ret < 0) {
982		kfree(group);
983		return ERR_PTR(ret);
984	}
985	group->id = ret;
986
987	ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
988				   NULL, "%d", group->id);
989	if (ret) {
990		kobject_put(&group->kobj);
991		return ERR_PTR(ret);
992	}
993
994	group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
995	if (!group->devices_kobj) {
996		kobject_put(&group->kobj); /* triggers .release & free */
997		return ERR_PTR(-ENOMEM);
998	}
999
1000	/*
1001	 * The devices_kobj holds a reference on the group kobject, so
1002	 * as long as that exists so will the group.  We can therefore
1003	 * use the devices_kobj for reference counting.
1004	 */
1005	kobject_put(&group->kobj);
1006
1007	ret = iommu_group_create_file(group,
1008				      &iommu_group_attr_reserved_regions);
1009	if (ret) {
1010		kobject_put(group->devices_kobj);
1011		return ERR_PTR(ret);
1012	}
1013
1014	ret = iommu_group_create_file(group, &iommu_group_attr_type);
1015	if (ret) {
1016		kobject_put(group->devices_kobj);
1017		return ERR_PTR(ret);
1018	}
1019
1020	pr_debug("Allocated group %d\n", group->id);
1021
1022	return group;
1023}
1024EXPORT_SYMBOL_GPL(iommu_group_alloc);
1025
1026/**
1027 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
1028 * @group: the group
1029 *
1030 * iommu drivers can store data in the group for use when doing iommu
1031 * operations.  This function provides a way to retrieve it.  Caller
1032 * should hold a group reference.
1033 */
1034void *iommu_group_get_iommudata(struct iommu_group *group)
1035{
1036	return group->iommu_data;
1037}
1038EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
1039
1040/**
1041 * iommu_group_set_iommudata - set iommu_data for a group
1042 * @group: the group
1043 * @iommu_data: new data
1044 * @release: release function for iommu_data
1045 *
1046 * iommu drivers can store data in the group for use when doing iommu
1047 * operations.  This function provides a way to set the data after
1048 * the group has been allocated.  Caller should hold a group reference.
1049 */
1050void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
1051			       void (*release)(void *iommu_data))
1052{
1053	group->iommu_data = iommu_data;
1054	group->iommu_data_release = release;
1055}
1056EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1057
1058/**
1059 * iommu_group_set_name - set name for a group
1060 * @group: the group
1061 * @name: name
1062 *
1063 * Allow iommu driver to set a name for a group.  When set it will
1064 * appear in a name attribute file under the group in sysfs.
1065 */
1066int iommu_group_set_name(struct iommu_group *group, const char *name)
1067{
1068	int ret;
1069
1070	if (group->name) {
1071		iommu_group_remove_file(group, &iommu_group_attr_name);
1072		kfree(group->name);
1073		group->name = NULL;
1074		if (!name)
1075			return 0;
1076	}
1077
1078	group->name = kstrdup(name, GFP_KERNEL);
1079	if (!group->name)
1080		return -ENOMEM;
1081
1082	ret = iommu_group_create_file(group, &iommu_group_attr_name);
1083	if (ret) {
1084		kfree(group->name);
1085		group->name = NULL;
1086		return ret;
1087	}
1088
1089	return 0;
1090}
1091EXPORT_SYMBOL_GPL(iommu_group_set_name);
1092
1093static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1094					       struct device *dev)
1095{
1096	struct iommu_resv_region *entry;
1097	struct list_head mappings;
1098	unsigned long pg_size;
1099	int ret = 0;
1100
1101	pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
1102	INIT_LIST_HEAD(&mappings);
1103
1104	if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
1105		return -EINVAL;
1106
1107	iommu_get_resv_regions(dev, &mappings);
1108
1109	/* We need to consider overlapping regions for different devices */
1110	list_for_each_entry(entry, &mappings, list) {
1111		dma_addr_t start, end, addr;
1112		size_t map_size = 0;
1113
1114		if (entry->type == IOMMU_RESV_DIRECT)
1115			dev->iommu->require_direct = 1;
1116
1117		if ((entry->type != IOMMU_RESV_DIRECT &&
1118		     entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1119		    !iommu_is_dma_domain(domain))
1120			continue;
1121
1122		start = ALIGN(entry->start, pg_size);
1123		end   = ALIGN(entry->start + entry->length, pg_size);
1124
1125		for (addr = start; addr <= end; addr += pg_size) {
1126			phys_addr_t phys_addr;
1127
1128			if (addr == end)
1129				goto map_end;
1130
1131			phys_addr = iommu_iova_to_phys(domain, addr);
1132			if (!phys_addr) {
1133				map_size += pg_size;
1134				continue;
1135			}
1136
1137map_end:
1138			if (map_size) {
1139				ret = iommu_map(domain, addr - map_size,
1140						addr - map_size, map_size,
1141						entry->prot, GFP_KERNEL);
1142				if (ret)
1143					goto out;
1144				map_size = 0;
1145			}
1146		}
1147
1148	}
1149
1150	if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
1151		iommu_flush_iotlb_all(domain);
1152
1153out:
1154	iommu_put_resv_regions(dev, &mappings);
1155
1156	return ret;
1157}
1158
1159/* This is undone by __iommu_group_free_device() */
1160static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
1161						     struct device *dev)
1162{
1163	int ret, i = 0;
1164	struct group_device *device;
1165
1166	device = kzalloc(sizeof(*device), GFP_KERNEL);
1167	if (!device)
1168		return ERR_PTR(-ENOMEM);
1169
1170	device->dev = dev;
1171
1172	ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
1173	if (ret)
1174		goto err_free_device;
1175
1176	device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
1177rename:
1178	if (!device->name) {
1179		ret = -ENOMEM;
1180		goto err_remove_link;
1181	}
1182
1183	ret = sysfs_create_link_nowarn(group->devices_kobj,
1184				       &dev->kobj, device->name);
1185	if (ret) {
1186		if (ret == -EEXIST && i >= 0) {
1187			/*
1188			 * Account for the slim chance of collision
1189			 * and append an instance to the name.
1190			 */
1191			kfree(device->name);
1192			device->name = kasprintf(GFP_KERNEL, "%s.%d",
1193						 kobject_name(&dev->kobj), i++);
1194			goto rename;
1195		}
1196		goto err_free_name;
1197	}
1198
1199	trace_add_device_to_group(group->id, dev);
1200
1201	dev_info(dev, "Adding to iommu group %d\n", group->id);
1202
1203	return device;
1204
1205err_free_name:
1206	kfree(device->name);
1207err_remove_link:
1208	sysfs_remove_link(&dev->kobj, "iommu_group");
1209err_free_device:
1210	kfree(device);
1211	dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
1212	return ERR_PTR(ret);
1213}
1214
1215/**
1216 * iommu_group_add_device - add a device to an iommu group
1217 * @group: the group into which to add the device (reference should be held)
1218 * @dev: the device
1219 *
1220 * This function is called by an iommu driver to add a device into a
1221 * group.  Adding a device increments the group reference count.
1222 */
1223int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1224{
1225	struct group_device *gdev;
1226
1227	gdev = iommu_group_alloc_device(group, dev);
1228	if (IS_ERR(gdev))
1229		return PTR_ERR(gdev);
1230
1231	iommu_group_ref_get(group);
1232	dev->iommu_group = group;
1233
1234	mutex_lock(&group->mutex);
1235	list_add_tail(&gdev->list, &group->devices);
1236	mutex_unlock(&group->mutex);
1237	return 0;
1238}
1239EXPORT_SYMBOL_GPL(iommu_group_add_device);
1240
1241/**
1242 * iommu_group_remove_device - remove a device from it's current group
1243 * @dev: device to be removed
1244 *
1245 * This function is called by an iommu driver to remove the device from
1246 * it's current group.  This decrements the iommu group reference count.
1247 */
1248void iommu_group_remove_device(struct device *dev)
1249{
1250	struct iommu_group *group = dev->iommu_group;
1251
1252	if (!group)
1253		return;
1254
1255	dev_info(dev, "Removing from iommu group %d\n", group->id);
1256
1257	__iommu_group_remove_device(dev);
1258}
1259EXPORT_SYMBOL_GPL(iommu_group_remove_device);
1260
1261#if IS_ENABLED(CONFIG_LOCKDEP) && IS_ENABLED(CONFIG_IOMMU_API)
1262/**
1263 * iommu_group_mutex_assert - Check device group mutex lock
1264 * @dev: the device that has group param set
1265 *
1266 * This function is called by an iommu driver to check whether it holds
1267 * group mutex lock for the given device or not.
1268 *
1269 * Note that this function must be called after device group param is set.
1270 */
1271void iommu_group_mutex_assert(struct device *dev)
1272{
1273	struct iommu_group *group = dev->iommu_group;
1274
1275	lockdep_assert_held(&group->mutex);
1276}
1277EXPORT_SYMBOL_GPL(iommu_group_mutex_assert);
1278#endif
1279
1280static struct device *iommu_group_first_dev(struct iommu_group *group)
1281{
1282	lockdep_assert_held(&group->mutex);
1283	return list_first_entry(&group->devices, struct group_device, list)->dev;
1284}
1285
1286/**
1287 * iommu_group_for_each_dev - iterate over each device in the group
1288 * @group: the group
1289 * @data: caller opaque data to be passed to callback function
1290 * @fn: caller supplied callback function
1291 *
1292 * This function is called by group users to iterate over group devices.
1293 * Callers should hold a reference count to the group during callback.
1294 * The group->mutex is held across callbacks, which will block calls to
1295 * iommu_group_add/remove_device.
1296 */
1297int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1298			     int (*fn)(struct device *, void *))
1299{
1300	struct group_device *device;
1301	int ret = 0;
1302
1303	mutex_lock(&group->mutex);
1304	for_each_group_device(group, device) {
1305		ret = fn(device->dev, data);
1306		if (ret)
1307			break;
1308	}
1309	mutex_unlock(&group->mutex);
1310
1311	return ret;
1312}
1313EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1314
1315/**
1316 * iommu_group_get - Return the group for a device and increment reference
1317 * @dev: get the group that this device belongs to
1318 *
1319 * This function is called by iommu drivers and users to get the group
1320 * for the specified device.  If found, the group is returned and the group
1321 * reference in incremented, else NULL.
1322 */
1323struct iommu_group *iommu_group_get(struct device *dev)
1324{
1325	struct iommu_group *group = dev->iommu_group;
1326
1327	if (group)
1328		kobject_get(group->devices_kobj);
1329
1330	return group;
1331}
1332EXPORT_SYMBOL_GPL(iommu_group_get);
1333
1334/**
1335 * iommu_group_ref_get - Increment reference on a group
1336 * @group: the group to use, must not be NULL
1337 *
1338 * This function is called by iommu drivers to take additional references on an
1339 * existing group.  Returns the given group for convenience.
1340 */
1341struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1342{
1343	kobject_get(group->devices_kobj);
1344	return group;
1345}
1346EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1347
1348/**
1349 * iommu_group_put - Decrement group reference
1350 * @group: the group to use
1351 *
1352 * This function is called by iommu drivers and users to release the
1353 * iommu group.  Once the reference count is zero, the group is released.
1354 */
1355void iommu_group_put(struct iommu_group *group)
1356{
1357	if (group)
1358		kobject_put(group->devices_kobj);
1359}
1360EXPORT_SYMBOL_GPL(iommu_group_put);
1361
1362/**
1363 * iommu_group_id - Return ID for a group
1364 * @group: the group to ID
1365 *
1366 * Return the unique ID for the group matching the sysfs group number.
1367 */
1368int iommu_group_id(struct iommu_group *group)
1369{
1370	return group->id;
1371}
1372EXPORT_SYMBOL_GPL(iommu_group_id);
1373
1374static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1375					       unsigned long *devfns);
1376
1377/*
1378 * To consider a PCI device isolated, we require ACS to support Source
1379 * Validation, Request Redirection, Completer Redirection, and Upstream
1380 * Forwarding.  This effectively means that devices cannot spoof their
1381 * requester ID, requests and completions cannot be redirected, and all
1382 * transactions are forwarded upstream, even as it passes through a
1383 * bridge where the target device is downstream.
1384 */
1385#define REQ_ACS_FLAGS   (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1386
1387/*
1388 * For multifunction devices which are not isolated from each other, find
1389 * all the other non-isolated functions and look for existing groups.  For
1390 * each function, we also need to look for aliases to or from other devices
1391 * that may already have a group.
1392 */
1393static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1394							unsigned long *devfns)
1395{
1396	struct pci_dev *tmp = NULL;
1397	struct iommu_group *group;
1398
1399	if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1400		return NULL;
1401
1402	for_each_pci_dev(tmp) {
1403		if (tmp == pdev || tmp->bus != pdev->bus ||
1404		    PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1405		    pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1406			continue;
1407
1408		group = get_pci_alias_group(tmp, devfns);
1409		if (group) {
1410			pci_dev_put(tmp);
1411			return group;
1412		}
1413	}
1414
1415	return NULL;
1416}
1417
1418/*
1419 * Look for aliases to or from the given device for existing groups. DMA
1420 * aliases are only supported on the same bus, therefore the search
1421 * space is quite small (especially since we're really only looking at pcie
1422 * device, and therefore only expect multiple slots on the root complex or
1423 * downstream switch ports).  It's conceivable though that a pair of
1424 * multifunction devices could have aliases between them that would cause a
1425 * loop.  To prevent this, we use a bitmap to track where we've been.
1426 */
1427static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1428					       unsigned long *devfns)
1429{
1430	struct pci_dev *tmp = NULL;
1431	struct iommu_group *group;
1432
1433	if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1434		return NULL;
1435
1436	group = iommu_group_get(&pdev->dev);
1437	if (group)
1438		return group;
1439
1440	for_each_pci_dev(tmp) {
1441		if (tmp == pdev || tmp->bus != pdev->bus)
1442			continue;
1443
1444		/* We alias them or they alias us */
1445		if (pci_devs_are_dma_aliases(pdev, tmp)) {
1446			group = get_pci_alias_group(tmp, devfns);
1447			if (group) {
1448				pci_dev_put(tmp);
1449				return group;
1450			}
1451
1452			group = get_pci_function_alias_group(tmp, devfns);
1453			if (group) {
1454				pci_dev_put(tmp);
1455				return group;
1456			}
1457		}
1458	}
1459
1460	return NULL;
1461}
1462
1463struct group_for_pci_data {
1464	struct pci_dev *pdev;
1465	struct iommu_group *group;
1466};
1467
1468/*
1469 * DMA alias iterator callback, return the last seen device.  Stop and return
1470 * the IOMMU group if we find one along the way.
1471 */
1472static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1473{
1474	struct group_for_pci_data *data = opaque;
1475
1476	data->pdev = pdev;
1477	data->group = iommu_group_get(&pdev->dev);
1478
1479	return data->group != NULL;
1480}
1481
1482/*
1483 * Generic device_group call-back function. It just allocates one
1484 * iommu-group per device.
1485 */
1486struct iommu_group *generic_device_group(struct device *dev)
1487{
1488	return iommu_group_alloc();
1489}
1490EXPORT_SYMBOL_GPL(generic_device_group);
1491
1492/*
1493 * Generic device_group call-back function. It just allocates one
1494 * iommu-group per iommu driver instance shared by every device
1495 * probed by that iommu driver.
1496 */
1497struct iommu_group *generic_single_device_group(struct device *dev)
1498{
1499	struct iommu_device *iommu = dev->iommu->iommu_dev;
1500
1501	if (!iommu->singleton_group) {
1502		struct iommu_group *group;
1503
1504		group = iommu_group_alloc();
1505		if (IS_ERR(group))
1506			return group;
1507		iommu->singleton_group = group;
1508	}
1509	return iommu_group_ref_get(iommu->singleton_group);
1510}
1511EXPORT_SYMBOL_GPL(generic_single_device_group);
1512
1513/*
1514 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1515 * to find or create an IOMMU group for a device.
1516 */
1517struct iommu_group *pci_device_group(struct device *dev)
1518{
1519	struct pci_dev *pdev = to_pci_dev(dev);
1520	struct group_for_pci_data data;
1521	struct pci_bus *bus;
1522	struct iommu_group *group = NULL;
1523	u64 devfns[4] = { 0 };
1524
1525	if (WARN_ON(!dev_is_pci(dev)))
1526		return ERR_PTR(-EINVAL);
1527
1528	/*
1529	 * Find the upstream DMA alias for the device.  A device must not
1530	 * be aliased due to topology in order to have its own IOMMU group.
1531	 * If we find an alias along the way that already belongs to a
1532	 * group, use it.
1533	 */
1534	if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1535		return data.group;
1536
1537	pdev = data.pdev;
1538
1539	/*
1540	 * Continue upstream from the point of minimum IOMMU granularity
1541	 * due to aliases to the point where devices are protected from
1542	 * peer-to-peer DMA by PCI ACS.  Again, if we find an existing
1543	 * group, use it.
1544	 */
1545	for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1546		if (!bus->self)
1547			continue;
1548
1549		if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1550			break;
1551
1552		pdev = bus->self;
1553
1554		group = iommu_group_get(&pdev->dev);
1555		if (group)
1556			return group;
1557	}
1558
1559	/*
1560	 * Look for existing groups on device aliases.  If we alias another
1561	 * device or another device aliases us, use the same group.
1562	 */
1563	group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1564	if (group)
1565		return group;
1566
1567	/*
1568	 * Look for existing groups on non-isolated functions on the same
1569	 * slot and aliases of those funcions, if any.  No need to clear
1570	 * the search bitmap, the tested devfns are still valid.
1571	 */
1572	group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1573	if (group)
1574		return group;
1575
1576	/* No shared group found, allocate new */
1577	return iommu_group_alloc();
1578}
1579EXPORT_SYMBOL_GPL(pci_device_group);
1580
1581/* Get the IOMMU group for device on fsl-mc bus */
1582struct iommu_group *fsl_mc_device_group(struct device *dev)
1583{
1584	struct device *cont_dev = fsl_mc_cont_dev(dev);
1585	struct iommu_group *group;
1586
1587	group = iommu_group_get(cont_dev);
1588	if (!group)
1589		group = iommu_group_alloc();
1590	return group;
1591}
1592EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1593
1594static struct iommu_domain *
1595__iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1596{
1597	if (group->default_domain && group->default_domain->type == req_type)
1598		return group->default_domain;
1599	return __iommu_group_domain_alloc(group, req_type);
1600}
1601
1602/*
1603 * req_type of 0 means "auto" which means to select a domain based on
1604 * iommu_def_domain_type or what the driver actually supports.
1605 */
1606static struct iommu_domain *
1607iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1608{
1609	const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group));
1610	struct iommu_domain *dom;
1611
1612	lockdep_assert_held(&group->mutex);
1613
1614	/*
1615	 * Allow legacy drivers to specify the domain that will be the default
1616	 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
1617	 * domain. Do not use in new drivers.
1618	 */
1619	if (ops->default_domain) {
1620		if (req_type != ops->default_domain->type)
1621			return ERR_PTR(-EINVAL);
1622		return ops->default_domain;
1623	}
1624
1625	if (req_type)
1626		return __iommu_group_alloc_default_domain(group, req_type);
1627
1628	/* The driver gave no guidance on what type to use, try the default */
1629	dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
1630	if (!IS_ERR(dom))
1631		return dom;
1632
1633	/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1634	if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1635		return ERR_PTR(-EINVAL);
1636	dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
1637	if (IS_ERR(dom))
1638		return dom;
1639
1640	pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1641		iommu_def_domain_type, group->name);
1642	return dom;
1643}
1644
1645struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1646{
1647	return group->default_domain;
1648}
1649
1650static int probe_iommu_group(struct device *dev, void *data)
1651{
1652	struct list_head *group_list = data;
1653	int ret;
1654
1655	mutex_lock(&iommu_probe_device_lock);
1656	ret = __iommu_probe_device(dev, group_list);
1657	mutex_unlock(&iommu_probe_device_lock);
1658	if (ret == -ENODEV)
1659		ret = 0;
1660
1661	return ret;
1662}
1663
1664static int iommu_bus_notifier(struct notifier_block *nb,
1665			      unsigned long action, void *data)
1666{
1667	struct device *dev = data;
1668
1669	if (action == BUS_NOTIFY_ADD_DEVICE) {
1670		int ret;
1671
1672		ret = iommu_probe_device(dev);
1673		return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1674	} else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1675		iommu_release_device(dev);
1676		return NOTIFY_OK;
1677	}
1678
1679	return 0;
1680}
1681
1682/*
1683 * Combine the driver's chosen def_domain_type across all the devices in a
1684 * group. Drivers must give a consistent result.
1685 */
1686static int iommu_get_def_domain_type(struct iommu_group *group,
1687				     struct device *dev, int cur_type)
1688{
1689	const struct iommu_ops *ops = dev_iommu_ops(dev);
1690	int type;
1691
1692	if (ops->default_domain) {
1693		/*
1694		 * Drivers that declare a global static default_domain will
1695		 * always choose that.
1696		 */
1697		type = ops->default_domain->type;
1698	} else {
1699		if (ops->def_domain_type)
1700			type = ops->def_domain_type(dev);
1701		else
1702			return cur_type;
1703	}
1704	if (!type || cur_type == type)
1705		return cur_type;
1706	if (!cur_type)
1707		return type;
1708
1709	dev_err_ratelimited(
1710		dev,
1711		"IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",
1712		iommu_domain_type_str(cur_type), iommu_domain_type_str(type),
1713		group->id);
1714
1715	/*
1716	 * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY
1717	 * takes precedence.
1718	 */
1719	if (type == IOMMU_DOMAIN_IDENTITY)
1720		return type;
1721	return cur_type;
1722}
1723
1724/*
1725 * A target_type of 0 will select the best domain type. 0 can be returned in
1726 * this case meaning the global default should be used.
1727 */
1728static int iommu_get_default_domain_type(struct iommu_group *group,
1729					 int target_type)
1730{
1731	struct device *untrusted = NULL;
1732	struct group_device *gdev;
1733	int driver_type = 0;
1734
1735	lockdep_assert_held(&group->mutex);
1736
1737	/*
1738	 * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an
1739	 * identity_domain and it will automatically become their default
1740	 * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.
1741	 * Override the selection to IDENTITY.
1742	 */
1743	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
1744		static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&
1745				IS_ENABLED(CONFIG_IOMMU_DMA)));
1746		driver_type = IOMMU_DOMAIN_IDENTITY;
1747	}
1748
1749	for_each_group_device(group, gdev) {
1750		driver_type = iommu_get_def_domain_type(group, gdev->dev,
1751							driver_type);
1752
1753		if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
1754			/*
1755			 * No ARM32 using systems will set untrusted, it cannot
1756			 * work.
1757			 */
1758			if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
1759				return -1;
1760			untrusted = gdev->dev;
1761		}
1762	}
1763
1764	/*
1765	 * If the common dma ops are not selected in kconfig then we cannot use
1766	 * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been
1767	 * selected.
1768	 */
1769	if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {
1770		if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))
1771			return -1;
1772		if (!driver_type)
1773			driver_type = IOMMU_DOMAIN_IDENTITY;
1774	}
1775
1776	if (untrusted) {
1777		if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
1778			dev_err_ratelimited(
1779				untrusted,
1780				"Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
1781				group->id, iommu_domain_type_str(driver_type));
1782			return -1;
1783		}
1784		driver_type = IOMMU_DOMAIN_DMA;
1785	}
1786
1787	if (target_type) {
1788		if (driver_type && target_type != driver_type)
1789			return -1;
1790		return target_type;
1791	}
1792	return driver_type;
1793}
1794
1795static void iommu_group_do_probe_finalize(struct device *dev)
1796{
1797	const struct iommu_ops *ops = dev_iommu_ops(dev);
1798
1799	if (ops->probe_finalize)
1800		ops->probe_finalize(dev);
1801}
1802
1803int bus_iommu_probe(const struct bus_type *bus)
1804{
1805	struct iommu_group *group, *next;
1806	LIST_HEAD(group_list);
1807	int ret;
1808
1809	ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1810	if (ret)
1811		return ret;
1812
1813	list_for_each_entry_safe(group, next, &group_list, entry) {
1814		struct group_device *gdev;
1815
1816		mutex_lock(&group->mutex);
1817
1818		/* Remove item from the list */
1819		list_del_init(&group->entry);
1820
1821		/*
1822		 * We go to the trouble of deferred default domain creation so
1823		 * that the cross-group default domain type and the setup of the
1824		 * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
1825		 */
1826		ret = iommu_setup_default_domain(group, 0);
1827		if (ret) {
1828			mutex_unlock(&group->mutex);
1829			return ret;
1830		}
1831		mutex_unlock(&group->mutex);
1832
1833		/*
1834		 * FIXME: Mis-locked because the ops->probe_finalize() call-back
1835		 * of some IOMMU drivers calls arm_iommu_attach_device() which
1836		 * in-turn might call back into IOMMU core code, where it tries
1837		 * to take group->mutex, resulting in a deadlock.
1838		 */
1839		for_each_group_device(group, gdev)
1840			iommu_group_do_probe_finalize(gdev->dev);
1841	}
1842
1843	return 0;
1844}
1845
1846/**
1847 * iommu_present() - make platform-specific assumptions about an IOMMU
1848 * @bus: bus to check
1849 *
1850 * Do not use this function. You want device_iommu_mapped() instead.
1851 *
1852 * Return: true if some IOMMU is present and aware of devices on the given bus;
1853 * in general it may not be the only IOMMU, and it may not have anything to do
1854 * with whatever device you are ultimately interested in.
1855 */
1856bool iommu_present(const struct bus_type *bus)
1857{
1858	bool ret = false;
1859
1860	for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
1861		if (iommu_buses[i] == bus) {
1862			spin_lock(&iommu_device_lock);
1863			ret = !list_empty(&iommu_device_list);
1864			spin_unlock(&iommu_device_lock);
1865		}
1866	}
1867	return ret;
1868}
1869EXPORT_SYMBOL_GPL(iommu_present);
1870
1871/**
1872 * device_iommu_capable() - check for a general IOMMU capability
1873 * @dev: device to which the capability would be relevant, if available
1874 * @cap: IOMMU capability
1875 *
1876 * Return: true if an IOMMU is present and supports the given capability
1877 * for the given device, otherwise false.
1878 */
1879bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
1880{
1881	const struct iommu_ops *ops;
1882
1883	if (!dev_has_iommu(dev))
1884		return false;
1885
1886	ops = dev_iommu_ops(dev);
1887	if (!ops->capable)
1888		return false;
1889
1890	return ops->capable(dev, cap);
1891}
1892EXPORT_SYMBOL_GPL(device_iommu_capable);
1893
1894/**
1895 * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
1896 *       for a group
1897 * @group: Group to query
1898 *
1899 * IOMMU groups should not have differing values of
1900 * msi_device_has_isolated_msi() for devices in a group. However nothing
1901 * directly prevents this, so ensure mistakes don't result in isolation failures
1902 * by checking that all the devices are the same.
1903 */
1904bool iommu_group_has_isolated_msi(struct iommu_group *group)
1905{
1906	struct group_device *group_dev;
1907	bool ret = true;
1908
1909	mutex_lock(&group->mutex);
1910	for_each_group_device(group, group_dev)
1911		ret &= msi_device_has_isolated_msi(group_dev->dev);
1912	mutex_unlock(&group->mutex);
1913	return ret;
1914}
1915EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
1916
1917/**
1918 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1919 * @domain: iommu domain
1920 * @handler: fault handler
1921 * @token: user data, will be passed back to the fault handler
1922 *
1923 * This function should be used by IOMMU users which want to be notified
1924 * whenever an IOMMU fault happens.
1925 *
1926 * The fault handler itself should return 0 on success, and an appropriate
1927 * error code otherwise.
1928 */
1929void iommu_set_fault_handler(struct iommu_domain *domain,
1930					iommu_fault_handler_t handler,
1931					void *token)
1932{
1933	BUG_ON(!domain);
1934
1935	domain->handler = handler;
1936	domain->handler_token = token;
1937}
1938EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1939
1940static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
1941						 struct device *dev,
1942						 unsigned int type)
1943{
1944	struct iommu_domain *domain;
1945	unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
1946
1947	if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)
1948		return ops->identity_domain;
1949	else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)
1950		return ops->blocked_domain;
1951	else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)
1952		domain = ops->domain_alloc_paging(dev);
1953	else if (ops->domain_alloc)
1954		domain = ops->domain_alloc(alloc_type);
1955	else
1956		return ERR_PTR(-EOPNOTSUPP);
1957
1958	/*
1959	 * Many domain_alloc ops now return ERR_PTR, make things easier for the
1960	 * driver by accepting ERR_PTR from all domain_alloc ops instead of
1961	 * having two rules.
1962	 */
1963	if (IS_ERR(domain))
1964		return domain;
1965	if (!domain)
1966		return ERR_PTR(-ENOMEM);
1967
1968	domain->type = type;
1969	domain->owner = ops;
1970	/*
1971	 * If not already set, assume all sizes by default; the driver
1972	 * may override this later
1973	 */
1974	if (!domain->pgsize_bitmap)
1975		domain->pgsize_bitmap = ops->pgsize_bitmap;
1976
1977	if (!domain->ops)
1978		domain->ops = ops->default_domain_ops;
1979
1980	if (iommu_is_dma_domain(domain)) {
1981		int rc;
1982
1983		rc = iommu_get_dma_cookie(domain);
1984		if (rc) {
1985			iommu_domain_free(domain);
1986			return ERR_PTR(rc);
1987		}
1988	}
1989	return domain;
1990}
1991
1992static struct iommu_domain *
1993__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
1994{
1995	struct device *dev = iommu_group_first_dev(group);
1996
1997	return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);
1998}
1999
2000static int __iommu_domain_alloc_dev(struct device *dev, void *data)
2001{
2002	const struct iommu_ops **ops = data;
2003
2004	if (!dev_has_iommu(dev))
2005		return 0;
2006
2007	if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),
2008		      "Multiple IOMMU drivers present for bus %s, which the public IOMMU API can't fully support yet. You will still need to disable one or more for this to work, sorry!\n",
2009		      dev_bus_name(dev)))
2010		return -EBUSY;
2011
2012	*ops = dev_iommu_ops(dev);
2013	return 0;
2014}
2015
2016struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
2017{
2018	const struct iommu_ops *ops = NULL;
2019	int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);
2020	struct iommu_domain *domain;
2021
2022	if (err || !ops)
2023		return NULL;
2024
2025	domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);
2026	if (IS_ERR(domain))
2027		return NULL;
2028	return domain;
2029}
2030EXPORT_SYMBOL_GPL(iommu_domain_alloc);
2031
2032void iommu_domain_free(struct iommu_domain *domain)
2033{
2034	if (domain->type == IOMMU_DOMAIN_SVA)
2035		mmdrop(domain->mm);
2036	iommu_put_dma_cookie(domain);
2037	if (domain->ops->free)
2038		domain->ops->free(domain);
2039}
2040EXPORT_SYMBOL_GPL(iommu_domain_free);
2041
2042/*
2043 * Put the group's domain back to the appropriate core-owned domain - either the
2044 * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
2045 */
2046static void __iommu_group_set_core_domain(struct iommu_group *group)
2047{
2048	struct iommu_domain *new_domain;
2049
2050	if (group->owner)
2051		new_domain = group->blocking_domain;
2052	else
2053		new_domain = group->default_domain;
2054
2055	__iommu_group_set_domain_nofail(group, new_domain);
2056}
2057
2058static int __iommu_attach_device(struct iommu_domain *domain,
2059				 struct device *dev)
2060{
2061	int ret;
2062
2063	if (unlikely(domain->ops->attach_dev == NULL))
2064		return -ENODEV;
2065
2066	ret = domain->ops->attach_dev(domain, dev);
2067	if (ret)
2068		return ret;
2069	dev->iommu->attach_deferred = 0;
2070	trace_attach_device_to_domain(dev);
2071	return 0;
2072}
2073
2074/**
2075 * iommu_attach_device - Attach an IOMMU domain to a device
2076 * @domain: IOMMU domain to attach
2077 * @dev: Device that will be attached
2078 *
2079 * Returns 0 on success and error code on failure
2080 *
2081 * Note that EINVAL can be treated as a soft failure, indicating
2082 * that certain configuration of the domain is incompatible with
2083 * the device. In this case attaching a different domain to the
2084 * device may succeed.
2085 */
2086int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
2087{
2088	/* Caller must be a probed driver on dev */
2089	struct iommu_group *group = dev->iommu_group;
2090	int ret;
2091
2092	if (!group)
2093		return -ENODEV;
2094
2095	/*
2096	 * Lock the group to make sure the device-count doesn't
2097	 * change while we are attaching
2098	 */
2099	mutex_lock(&group->mutex);
2100	ret = -EINVAL;
2101	if (list_count_nodes(&group->devices) != 1)
2102		goto out_unlock;
2103
2104	ret = __iommu_attach_group(domain, group);
2105
2106out_unlock:
2107	mutex_unlock(&group->mutex);
2108	return ret;
2109}
2110EXPORT_SYMBOL_GPL(iommu_attach_device);
2111
2112int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2113{
2114	if (dev->iommu && dev->iommu->attach_deferred)
2115		return __iommu_attach_device(domain, dev);
2116
2117	return 0;
2118}
2119
2120void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2121{
2122	/* Caller must be a probed driver on dev */
2123	struct iommu_group *group = dev->iommu_group;
2124
2125	if (!group)
2126		return;
2127
2128	mutex_lock(&group->mutex);
2129	if (WARN_ON(domain != group->domain) ||
2130	    WARN_ON(list_count_nodes(&group->devices) != 1))
2131		goto out_unlock;
2132	__iommu_group_set_core_domain(group);
2133
2134out_unlock:
2135	mutex_unlock(&group->mutex);
2136}
2137EXPORT_SYMBOL_GPL(iommu_detach_device);
2138
2139struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2140{
2141	/* Caller must be a probed driver on dev */
2142	struct iommu_group *group = dev->iommu_group;
2143
2144	if (!group)
2145		return NULL;
2146
2147	return group->domain;
2148}
2149EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2150
2151/*
2152 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2153 * guarantees that the group and its default domain are valid and correct.
2154 */
2155struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2156{
2157	return dev->iommu_group->default_domain;
2158}
2159
2160static int __iommu_attach_group(struct iommu_domain *domain,
2161				struct iommu_group *group)
2162{
2163	struct device *dev;
2164
2165	if (group->domain && group->domain != group->default_domain &&
2166	    group->domain != group->blocking_domain)
2167		return -EBUSY;
2168
2169	dev = iommu_group_first_dev(group);
2170	if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
2171		return -EINVAL;
2172
2173	return __iommu_group_set_domain(group, domain);
2174}
2175
2176/**
2177 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
2178 * @domain: IOMMU domain to attach
2179 * @group: IOMMU group that will be attached
2180 *
2181 * Returns 0 on success and error code on failure
2182 *
2183 * Note that EINVAL can be treated as a soft failure, indicating
2184 * that certain configuration of the domain is incompatible with
2185 * the group. In this case attaching a different domain to the
2186 * group may succeed.
2187 */
2188int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2189{
2190	int ret;
2191
2192	mutex_lock(&group->mutex);
2193	ret = __iommu_attach_group(domain, group);
2194	mutex_unlock(&group->mutex);
2195
2196	return ret;
2197}
2198EXPORT_SYMBOL_GPL(iommu_attach_group);
2199
2200/**
2201 * iommu_group_replace_domain - replace the domain that a group is attached to
2202 * @new_domain: new IOMMU domain to replace with
2203 * @group: IOMMU group that will be attached to the new domain
2204 *
2205 * This API allows the group to switch domains without being forced to go to
2206 * the blocking domain in-between.
2207 *
2208 * If the currently attached domain is a core domain (e.g. a default_domain),
2209 * it will act just like the iommu_attach_group().
2210 */
2211int iommu_group_replace_domain(struct iommu_group *group,
2212			       struct iommu_domain *new_domain)
2213{
2214	int ret;
2215
2216	if (!new_domain)
2217		return -EINVAL;
2218
2219	mutex_lock(&group->mutex);
2220	ret = __iommu_group_set_domain(group, new_domain);
2221	mutex_unlock(&group->mutex);
2222	return ret;
2223}
2224EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
2225
2226static int __iommu_device_set_domain(struct iommu_group *group,
2227				     struct device *dev,
2228				     struct iommu_domain *new_domain,
2229				     unsigned int flags)
2230{
2231	int ret;
2232
2233	/*
2234	 * If the device requires IOMMU_RESV_DIRECT then we cannot allow
2235	 * the blocking domain to be attached as it does not contain the
2236	 * required 1:1 mapping. This test effectively excludes the device
2237	 * being used with iommu_group_claim_dma_owner() which will block
2238	 * vfio and iommufd as well.
2239	 */
2240	if (dev->iommu->require_direct &&
2241	    (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2242	     new_domain == group->blocking_domain)) {
2243		dev_warn(dev,
2244			 "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");
2245		return -EINVAL;
2246	}
2247
2248	if (dev->iommu->attach_deferred) {
2249		if (new_domain == group->default_domain)
2250			return 0;
2251		dev->iommu->attach_deferred = 0;
2252	}
2253
2254	ret = __iommu_attach_device(new_domain, dev);
2255	if (ret) {
2256		/*
2257		 * If we have a blocking domain then try to attach that in hopes
2258		 * of avoiding a UAF. Modern drivers should implement blocking
2259		 * domains as global statics that cannot fail.
2260		 */
2261		if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2262		    group->blocking_domain &&
2263		    group->blocking_domain != new_domain)
2264			__iommu_attach_device(group->blocking_domain, dev);
2265		return ret;
2266	}
2267	return 0;
2268}
2269
2270/*
2271 * If 0 is returned the group's domain is new_domain. If an error is returned
2272 * then the group's domain will be set back to the existing domain unless
2273 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2274 * domains is left inconsistent. This is a driver bug to fail attach with a
2275 * previously good domain. We try to avoid a kernel UAF because of this.
2276 *
2277 * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2278 * API works on domains and devices.  Bridge that gap by iterating over the
2279 * devices in a group.  Ideally we'd have a single device which represents the
2280 * requestor ID of the group, but we also allow IOMMU drivers to create policy
2281 * defined minimum sets, where the physical hardware may be able to distiguish
2282 * members, but we wish to group them at a higher level (ex. untrusted
2283 * multi-function PCI devices).  Thus we attach each device.
2284 */
2285static int __iommu_group_set_domain_internal(struct iommu_group *group,
2286					     struct iommu_domain *new_domain,
2287					     unsigned int flags)
2288{
2289	struct group_device *last_gdev;
2290	struct group_device *gdev;
2291	int result;
2292	int ret;
2293
2294	lockdep_assert_held(&group->mutex);
2295
2296	if (group->domain == new_domain)
2297		return 0;
2298
2299	if (WARN_ON(!new_domain))
2300		return -EINVAL;
2301
2302	/*
2303	 * Changing the domain is done by calling attach_dev() on the new
2304	 * domain. This switch does not have to be atomic and DMA can be
2305	 * discarded during the transition. DMA must only be able to access
2306	 * either new_domain or group->domain, never something else.
2307	 */
2308	result = 0;
2309	for_each_group_device(group, gdev) {
2310		ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2311						flags);
2312		if (ret) {
2313			result = ret;
2314			/*
2315			 * Keep trying the other devices in the group. If a
2316			 * driver fails attach to an otherwise good domain, and
2317			 * does not support blocking domains, it should at least
2318			 * drop its reference on the current domain so we don't
2319			 * UAF.
2320			 */
2321			if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2322				continue;
2323			goto err_revert;
2324		}
2325	}
2326	group->domain = new_domain;
2327	return result;
2328
2329err_revert:
2330	/*
2331	 * This is called in error unwind paths. A well behaved driver should
2332	 * always allow us to attach to a domain that was already attached.
2333	 */
2334	last_gdev = gdev;
2335	for_each_group_device(group, gdev) {
2336		/*
2337		 * A NULL domain can happen only for first probe, in which case
2338		 * we leave group->domain as NULL and let release clean
2339		 * everything up.
2340		 */
2341		if (group->domain)
2342			WARN_ON(__iommu_device_set_domain(
2343				group, gdev->dev, group->domain,
2344				IOMMU_SET_DOMAIN_MUST_SUCCEED));
2345		if (gdev == last_gdev)
2346			break;
2347	}
2348	return ret;
2349}
2350
2351void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2352{
2353	mutex_lock(&group->mutex);
2354	__iommu_group_set_core_domain(group);
2355	mutex_unlock(&group->mutex);
2356}
2357EXPORT_SYMBOL_GPL(iommu_detach_group);
2358
2359phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2360{
2361	if (domain->type == IOMMU_DOMAIN_IDENTITY)
2362		return iova;
2363
2364	if (domain->type == IOMMU_DOMAIN_BLOCKED)
2365		return 0;
2366
2367	return domain->ops->iova_to_phys(domain, iova);
2368}
2369EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2370
2371static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2372			   phys_addr_t paddr, size_t size, size_t *count)
2373{
2374	unsigned int pgsize_idx, pgsize_idx_next;
2375	unsigned long pgsizes;
2376	size_t offset, pgsize, pgsize_next;
2377	unsigned long addr_merge = paddr | iova;
2378
2379	/* Page sizes supported by the hardware and small enough for @size */
2380	pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2381
2382	/* Constrain the page sizes further based on the maximum alignment */
2383	if (likely(addr_merge))
2384		pgsizes &= GENMASK(__ffs(addr_merge), 0);
2385
2386	/* Make sure we have at least one suitable page size */
2387	BUG_ON(!pgsizes);
2388
2389	/* Pick the biggest page size remaining */
2390	pgsize_idx = __fls(pgsizes);
2391	pgsize = BIT(pgsize_idx);
2392	if (!count)
2393		return pgsize;
2394
2395	/* Find the next biggest support page size, if it exists */
2396	pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2397	if (!pgsizes)
2398		goto out_set_count;
2399
2400	pgsize_idx_next = __ffs(pgsizes);
2401	pgsize_next = BIT(pgsize_idx_next);
2402
2403	/*
2404	 * There's no point trying a bigger page size unless the virtual
2405	 * and physical addresses are similarly offset within the larger page.
2406	 */
2407	if ((iova ^ paddr) & (pgsize_next - 1))
2408		goto out_set_count;
2409
2410	/* Calculate the offset to the next page size alignment boundary */
2411	offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2412
2413	/*
2414	 * If size is big enough to accommodate the larger page, reduce
2415	 * the number of smaller pages.
2416	 */
2417	if (offset + pgsize_next <= size)
2418		size = offset;
2419
2420out_set_count:
2421	*count = size >> pgsize_idx;
2422	return pgsize;
2423}
2424
2425static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2426		       phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2427{
2428	const struct iommu_domain_ops *ops = domain->ops;
2429	unsigned long orig_iova = iova;
2430	unsigned int min_pagesz;
2431	size_t orig_size = size;
2432	phys_addr_t orig_paddr = paddr;
2433	int ret = 0;
2434
2435	if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2436		return -EINVAL;
2437
2438	if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))
2439		return -ENODEV;
2440
2441	/* find out the minimum page size supported */
2442	min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2443
2444	/*
2445	 * both the virtual address and the physical one, as well as
2446	 * the size of the mapping, must be aligned (at least) to the
2447	 * size of the smallest page supported by the hardware
2448	 */
2449	if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2450		pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2451		       iova, &paddr, size, min_pagesz);
2452		return -EINVAL;
2453	}
2454
2455	pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2456
2457	while (size) {
2458		size_t pgsize, count, mapped = 0;
2459
2460		pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2461
2462		pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2463			 iova, &paddr, pgsize, count);
2464		ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2465				     gfp, &mapped);
2466		/*
2467		 * Some pages may have been mapped, even if an error occurred,
2468		 * so we should account for those so they can be unmapped.
2469		 */
2470		size -= mapped;
2471
2472		if (ret)
2473			break;
2474
2475		iova += mapped;
2476		paddr += mapped;
2477	}
2478
2479	/* unroll mapping in case something went wrong */
2480	if (ret)
2481		iommu_unmap(domain, orig_iova, orig_size - size);
2482	else
2483		trace_map(orig_iova, orig_paddr, orig_size);
2484
2485	return ret;
2486}
2487
2488int iommu_map(struct iommu_domain *domain, unsigned long iova,
2489	      phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2490{
2491	const struct iommu_domain_ops *ops = domain->ops;
2492	int ret;
2493
2494	might_sleep_if(gfpflags_allow_blocking(gfp));
2495
2496	/* Discourage passing strange GFP flags */
2497	if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2498				__GFP_HIGHMEM)))
2499		return -EINVAL;
2500
2501	ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2502	if (ret == 0 && ops->iotlb_sync_map) {
2503		ret = ops->iotlb_sync_map(domain, iova, size);
2504		if (ret)
2505			goto out_err;
2506	}
2507
2508	return ret;
2509
2510out_err:
2511	/* undo mappings already done */
2512	iommu_unmap(domain, iova, size);
2513
2514	return ret;
2515}
2516EXPORT_SYMBOL_GPL(iommu_map);
2517
2518static size_t __iommu_unmap(struct iommu_domain *domain,
2519			    unsigned long iova, size_t size,
2520			    struct iommu_iotlb_gather *iotlb_gather)
2521{
2522	const struct iommu_domain_ops *ops = domain->ops;
2523	size_t unmapped_page, unmapped = 0;
2524	unsigned long orig_iova = iova;
2525	unsigned int min_pagesz;
2526
2527	if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2528		return 0;
2529
2530	if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))
2531		return 0;
2532
2533	/* find out the minimum page size supported */
2534	min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2535
2536	/*
2537	 * The virtual address, as well as the size of the mapping, must be
2538	 * aligned (at least) to the size of the smallest page supported
2539	 * by the hardware
2540	 */
2541	if (!IS_ALIGNED(iova | size, min_pagesz)) {
2542		pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2543		       iova, size, min_pagesz);
2544		return 0;
2545	}
2546
2547	pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2548
2549	/*
2550	 * Keep iterating until we either unmap 'size' bytes (or more)
2551	 * or we hit an area that isn't mapped.
2552	 */
2553	while (unmapped < size) {
2554		size_t pgsize, count;
2555
2556		pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);
2557		unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);
2558		if (!unmapped_page)
2559			break;
2560
2561		pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2562			 iova, unmapped_page);
2563
2564		iova += unmapped_page;
2565		unmapped += unmapped_page;
2566	}
2567
2568	trace_unmap(orig_iova, size, unmapped);
2569	return unmapped;
2570}
2571
2572size_t iommu_unmap(struct iommu_domain *domain,
2573		   unsigned long iova, size_t size)
2574{
2575	struct iommu_iotlb_gather iotlb_gather;
2576	size_t ret;
2577
2578	iommu_iotlb_gather_init(&iotlb_gather);
2579	ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2580	iommu_iotlb_sync(domain, &iotlb_gather);
2581
2582	return ret;
2583}
2584EXPORT_SYMBOL_GPL(iommu_unmap);
2585
2586size_t iommu_unmap_fast(struct iommu_domain *domain,
2587			unsigned long iova, size_t size,
2588			struct iommu_iotlb_gather *iotlb_gather)
2589{
2590	return __iommu_unmap(domain, iova, size, iotlb_gather);
2591}
2592EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2593
2594ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2595		     struct scatterlist *sg, unsigned int nents, int prot,
2596		     gfp_t gfp)
2597{
2598	const struct iommu_domain_ops *ops = domain->ops;
2599	size_t len = 0, mapped = 0;
2600	phys_addr_t start;
2601	unsigned int i = 0;
2602	int ret;
2603
2604	might_sleep_if(gfpflags_allow_blocking(gfp));
2605
2606	/* Discourage passing strange GFP flags */
2607	if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2608				__GFP_HIGHMEM)))
2609		return -EINVAL;
2610
2611	while (i <= nents) {
2612		phys_addr_t s_phys = sg_phys(sg);
2613
2614		if (len && s_phys != start + len) {
2615			ret = __iommu_map(domain, iova + mapped, start,
2616					len, prot, gfp);
2617
2618			if (ret)
2619				goto out_err;
2620
2621			mapped += len;
2622			len = 0;
2623		}
2624
2625		if (sg_dma_is_bus_address(sg))
2626			goto next;
2627
2628		if (len) {
2629			len += sg->length;
2630		} else {
2631			len = sg->length;
2632			start = s_phys;
2633		}
2634
2635next:
2636		if (++i < nents)
2637			sg = sg_next(sg);
2638	}
2639
2640	if (ops->iotlb_sync_map) {
2641		ret = ops->iotlb_sync_map(domain, iova, mapped);
2642		if (ret)
2643			goto out_err;
2644	}
2645	return mapped;
2646
2647out_err:
2648	/* undo mappings already done */
2649	iommu_unmap(domain, iova, mapped);
2650
2651	return ret;
2652}
2653EXPORT_SYMBOL_GPL(iommu_map_sg);
2654
2655/**
2656 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2657 * @domain: the iommu domain where the fault has happened
2658 * @dev: the device where the fault has happened
2659 * @iova: the faulting address
2660 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2661 *
2662 * This function should be called by the low-level IOMMU implementations
2663 * whenever IOMMU faults happen, to allow high-level users, that are
2664 * interested in such events, to know about them.
2665 *
2666 * This event may be useful for several possible use cases:
2667 * - mere logging of the event
2668 * - dynamic TLB/PTE loading
2669 * - if restarting of the faulting device is required
2670 *
2671 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2672 * PTE/TLB loading will one day be supported, implementations will be able
2673 * to tell whether it succeeded or not according to this return value).
2674 *
2675 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2676 * (though fault handlers can also return -ENOSYS, in case they want to
2677 * elicit the default behavior of the IOMMU drivers).
2678 */
2679int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2680		       unsigned long iova, int flags)
2681{
2682	int ret = -ENOSYS;
2683
2684	/*
2685	 * if upper layers showed interest and installed a fault handler,
2686	 * invoke it.
2687	 */
2688	if (domain->handler)
2689		ret = domain->handler(domain, dev, iova, flags,
2690						domain->handler_token);
2691
2692	trace_io_page_fault(dev, iova, flags);
2693	return ret;
2694}
2695EXPORT_SYMBOL_GPL(report_iommu_fault);
2696
2697static int __init iommu_init(void)
2698{
2699	iommu_group_kset = kset_create_and_add("iommu_groups",
2700					       NULL, kernel_kobj);
2701	BUG_ON(!iommu_group_kset);
2702
2703	iommu_debugfs_setup();
2704
2705	return 0;
2706}
2707core_initcall(iommu_init);
2708
2709int iommu_enable_nesting(struct iommu_domain *domain)
2710{
2711	if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2712		return -EINVAL;
2713	if (!domain->ops->enable_nesting)
2714		return -EINVAL;
2715	return domain->ops->enable_nesting(domain);
2716}
2717EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2718
2719int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2720		unsigned long quirk)
2721{
2722	if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2723		return -EINVAL;
2724	if (!domain->ops->set_pgtable_quirks)
2725		return -EINVAL;
2726	return domain->ops->set_pgtable_quirks(domain, quirk);
2727}
2728EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2729
2730/**
2731 * iommu_get_resv_regions - get reserved regions
2732 * @dev: device for which to get reserved regions
2733 * @list: reserved region list for device
2734 *
2735 * This returns a list of reserved IOVA regions specific to this device.
2736 * A domain user should not map IOVA in these ranges.
2737 */
2738void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2739{
2740	const struct iommu_ops *ops = dev_iommu_ops(dev);
2741
2742	if (ops->get_resv_regions)
2743		ops->get_resv_regions(dev, list);
2744}
2745EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
2746
2747/**
2748 * iommu_put_resv_regions - release reserved regions
2749 * @dev: device for which to free reserved regions
2750 * @list: reserved region list for device
2751 *
2752 * This releases a reserved region list acquired by iommu_get_resv_regions().
2753 */
2754void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2755{
2756	struct iommu_resv_region *entry, *next;
2757
2758	list_for_each_entry_safe(entry, next, list, list) {
2759		if (entry->free)
2760			entry->free(dev, entry);
2761		else
2762			kfree(entry);
2763	}
2764}
2765EXPORT_SYMBOL(iommu_put_resv_regions);
2766
2767struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2768						  size_t length, int prot,
2769						  enum iommu_resv_type type,
2770						  gfp_t gfp)
2771{
2772	struct iommu_resv_region *region;
2773
2774	region = kzalloc(sizeof(*region), gfp);
2775	if (!region)
2776		return NULL;
2777
2778	INIT_LIST_HEAD(&region->list);
2779	region->start = start;
2780	region->length = length;
2781	region->prot = prot;
2782	region->type = type;
2783	return region;
2784}
2785EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2786
2787void iommu_set_default_passthrough(bool cmd_line)
2788{
2789	if (cmd_line)
2790		iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2791	iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2792}
2793
2794void iommu_set_default_translated(bool cmd_line)
2795{
2796	if (cmd_line)
2797		iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2798	iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2799}
2800
2801bool iommu_default_passthrough(void)
2802{
2803	return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2804}
2805EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2806
2807const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode)
2808{
2809	const struct iommu_ops *ops = NULL;
2810	struct iommu_device *iommu;
2811
2812	spin_lock(&iommu_device_lock);
2813	list_for_each_entry(iommu, &iommu_device_list, list)
2814		if (iommu->fwnode == fwnode) {
2815			ops = iommu->ops;
2816			break;
2817		}
2818	spin_unlock(&iommu_device_lock);
2819	return ops;
2820}
2821
2822int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2823		      const struct iommu_ops *ops)
2824{
2825	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2826
2827	if (fwspec)
2828		return ops == fwspec->ops ? 0 : -EINVAL;
2829
2830	if (!dev_iommu_get(dev))
2831		return -ENOMEM;
2832
2833	/* Preallocate for the overwhelmingly common case of 1 ID */
2834	fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2835	if (!fwspec)
2836		return -ENOMEM;
2837
2838	of_node_get(to_of_node(iommu_fwnode));
2839	fwspec->iommu_fwnode = iommu_fwnode;
2840	fwspec->ops = ops;
2841	dev_iommu_fwspec_set(dev, fwspec);
2842	return 0;
2843}
2844EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2845
2846void iommu_fwspec_free(struct device *dev)
2847{
2848	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2849
2850	if (fwspec) {
2851		fwnode_handle_put(fwspec->iommu_fwnode);
2852		kfree(fwspec);
2853		dev_iommu_fwspec_set(dev, NULL);
2854	}
2855}
2856EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2857
2858int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids)
2859{
2860	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2861	int i, new_num;
2862
2863	if (!fwspec)
2864		return -EINVAL;
2865
2866	new_num = fwspec->num_ids + num_ids;
2867	if (new_num > 1) {
2868		fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2869				  GFP_KERNEL);
2870		if (!fwspec)
2871			return -ENOMEM;
2872
2873		dev_iommu_fwspec_set(dev, fwspec);
2874	}
2875
2876	for (i = 0; i < num_ids; i++)
2877		fwspec->ids[fwspec->num_ids + i] = ids[i];
2878
2879	fwspec->num_ids = new_num;
2880	return 0;
2881}
2882EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2883
2884/*
2885 * Per device IOMMU features.
2886 */
2887int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2888{
2889	if (dev_has_iommu(dev)) {
2890		const struct iommu_ops *ops = dev_iommu_ops(dev);
2891
2892		if (ops->dev_enable_feat)
2893			return ops->dev_enable_feat(dev, feat);
2894	}
2895
2896	return -ENODEV;
2897}
2898EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2899
2900/*
2901 * The device drivers should do the necessary cleanups before calling this.
2902 */
2903int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2904{
2905	if (dev_has_iommu(dev)) {
2906		const struct iommu_ops *ops = dev_iommu_ops(dev);
2907
2908		if (ops->dev_disable_feat)
2909			return ops->dev_disable_feat(dev, feat);
2910	}
2911
2912	return -EBUSY;
2913}
2914EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2915
2916/**
2917 * iommu_setup_default_domain - Set the default_domain for the group
2918 * @group: Group to change
2919 * @target_type: Domain type to set as the default_domain
2920 *
2921 * Allocate a default domain and set it as the current domain on the group. If
2922 * the group already has a default domain it will be changed to the target_type.
2923 * When target_type is 0 the default domain is selected based on driver and
2924 * system preferences.
2925 */
2926static int iommu_setup_default_domain(struct iommu_group *group,
2927				      int target_type)
2928{
2929	struct iommu_domain *old_dom = group->default_domain;
2930	struct group_device *gdev;
2931	struct iommu_domain *dom;
2932	bool direct_failed;
2933	int req_type;
2934	int ret;
2935
2936	lockdep_assert_held(&group->mutex);
2937
2938	req_type = iommu_get_default_domain_type(group, target_type);
2939	if (req_type < 0)
2940		return -EINVAL;
2941
2942	dom = iommu_group_alloc_default_domain(group, req_type);
2943	if (IS_ERR(dom))
2944		return PTR_ERR(dom);
2945
2946	if (group->default_domain == dom)
2947		return 0;
2948
2949	/*
2950	 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
2951	 * mapped before their device is attached, in order to guarantee
2952	 * continuity with any FW activity
2953	 */
2954	direct_failed = false;
2955	for_each_group_device(group, gdev) {
2956		if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
2957			direct_failed = true;
2958			dev_warn_once(
2959				gdev->dev->iommu->iommu_dev->dev,
2960				"IOMMU driver was not able to establish FW requested direct mapping.");
2961		}
2962	}
2963
2964	/* We must set default_domain early for __iommu_device_set_domain */
2965	group->default_domain = dom;
2966	if (!group->domain) {
2967		/*
2968		 * Drivers are not allowed to fail the first domain attach.
2969		 * The only way to recover from this is to fail attaching the
2970		 * iommu driver and call ops->release_device. Put the domain
2971		 * in group->default_domain so it is freed after.
2972		 */
2973		ret = __iommu_group_set_domain_internal(
2974			group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
2975		if (WARN_ON(ret))
2976			goto out_free_old;
2977	} else {
2978		ret = __iommu_group_set_domain(group, dom);
2979		if (ret)
2980			goto err_restore_def_domain;
2981	}
2982
2983	/*
2984	 * Drivers are supposed to allow mappings to be installed in a domain
2985	 * before device attachment, but some don't. Hack around this defect by
2986	 * trying again after attaching. If this happens it means the device
2987	 * will not continuously have the IOMMU_RESV_DIRECT map.
2988	 */
2989	if (direct_failed) {
2990		for_each_group_device(group, gdev) {
2991			ret = iommu_create_device_direct_mappings(dom, gdev->dev);
2992			if (ret)
2993				goto err_restore_domain;
2994		}
2995	}
2996
2997out_free_old:
2998	if (old_dom)
2999		iommu_domain_free(old_dom);
3000	return ret;
3001
3002err_restore_domain:
3003	if (old_dom)
3004		__iommu_group_set_domain_internal(
3005			group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3006err_restore_def_domain:
3007	if (old_dom) {
3008		iommu_domain_free(dom);
3009		group->default_domain = old_dom;
3010	}
3011	return ret;
3012}
3013
3014/*
3015 * Changing the default domain through sysfs requires the users to unbind the
3016 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3017 * transition. Return failure if this isn't met.
3018 *
3019 * We need to consider the race between this and the device release path.
3020 * group->mutex is used here to guarantee that the device release path
3021 * will not be entered at the same time.
3022 */
3023static ssize_t iommu_group_store_type(struct iommu_group *group,
3024				      const char *buf, size_t count)
3025{
3026	struct group_device *gdev;
3027	int ret, req_type;
3028
3029	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3030		return -EACCES;
3031
3032	if (WARN_ON(!group) || !group->default_domain)
3033		return -EINVAL;
3034
3035	if (sysfs_streq(buf, "identity"))
3036		req_type = IOMMU_DOMAIN_IDENTITY;
3037	else if (sysfs_streq(buf, "DMA"))
3038		req_type = IOMMU_DOMAIN_DMA;
3039	else if (sysfs_streq(buf, "DMA-FQ"))
3040		req_type = IOMMU_DOMAIN_DMA_FQ;
3041	else if (sysfs_streq(buf, "auto"))
3042		req_type = 0;
3043	else
3044		return -EINVAL;
3045
3046	mutex_lock(&group->mutex);
3047	/* We can bring up a flush queue without tearing down the domain. */
3048	if (req_type == IOMMU_DOMAIN_DMA_FQ &&
3049	    group->default_domain->type == IOMMU_DOMAIN_DMA) {
3050		ret = iommu_dma_init_fq(group->default_domain);
3051		if (ret)
3052			goto out_unlock;
3053
3054		group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
3055		ret = count;
3056		goto out_unlock;
3057	}
3058
3059	/* Otherwise, ensure that device exists and no driver is bound. */
3060	if (list_empty(&group->devices) || group->owner_cnt) {
3061		ret = -EPERM;
3062		goto out_unlock;
3063	}
3064
3065	ret = iommu_setup_default_domain(group, req_type);
3066	if (ret)
3067		goto out_unlock;
3068
3069	/*
3070	 * Release the mutex here because ops->probe_finalize() call-back of
3071	 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3072	 * in-turn might call back into IOMMU core code, where it tries to take
3073	 * group->mutex, resulting in a deadlock.
3074	 */
3075	mutex_unlock(&group->mutex);
3076
3077	/* Make sure dma_ops is appropriatley set */
3078	for_each_group_device(group, gdev)
3079		iommu_group_do_probe_finalize(gdev->dev);
3080	return count;
3081
3082out_unlock:
3083	mutex_unlock(&group->mutex);
3084	return ret ?: count;
3085}
3086
3087/**
3088 * iommu_device_use_default_domain() - Device driver wants to handle device
3089 *                                     DMA through the kernel DMA API.
3090 * @dev: The device.
3091 *
3092 * The device driver about to bind @dev wants to do DMA through the kernel
3093 * DMA API. Return 0 if it is allowed, otherwise an error.
3094 */
3095int iommu_device_use_default_domain(struct device *dev)
3096{
3097	/* Caller is the driver core during the pre-probe path */
3098	struct iommu_group *group = dev->iommu_group;
3099	int ret = 0;
3100
3101	if (!group)
3102		return 0;
3103
3104	mutex_lock(&group->mutex);
3105	if (group->owner_cnt) {
3106		if (group->domain != group->default_domain || group->owner ||
3107		    !xa_empty(&group->pasid_array)) {
3108			ret = -EBUSY;
3109			goto unlock_out;
3110		}
3111	}
3112
3113	group->owner_cnt++;
3114
3115unlock_out:
3116	mutex_unlock(&group->mutex);
3117	return ret;
3118}
3119
3120/**
3121 * iommu_device_unuse_default_domain() - Device driver stops handling device
3122 *                                       DMA through the kernel DMA API.
3123 * @dev: The device.
3124 *
3125 * The device driver doesn't want to do DMA through kernel DMA API anymore.
3126 * It must be called after iommu_device_use_default_domain().
3127 */
3128void iommu_device_unuse_default_domain(struct device *dev)
3129{
3130	/* Caller is the driver core during the post-probe path */
3131	struct iommu_group *group = dev->iommu_group;
3132
3133	if (!group)
3134		return;
3135
3136	mutex_lock(&group->mutex);
3137	if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
3138		group->owner_cnt--;
3139
3140	mutex_unlock(&group->mutex);
3141}
3142
3143static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
3144{
3145	struct iommu_domain *domain;
3146
3147	if (group->blocking_domain)
3148		return 0;
3149
3150	domain = __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
3151	if (IS_ERR(domain)) {
3152		/*
3153		 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
3154		 * create an empty domain instead.
3155		 */
3156		domain = __iommu_group_domain_alloc(group,
3157						    IOMMU_DOMAIN_UNMANAGED);
3158		if (IS_ERR(domain))
3159			return PTR_ERR(domain);
3160	}
3161	group->blocking_domain = domain;
3162	return 0;
3163}
3164
3165static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
3166{
3167	int ret;
3168
3169	if ((group->domain && group->domain != group->default_domain) ||
3170	    !xa_empty(&group->pasid_array))
3171		return -EBUSY;
3172
3173	ret = __iommu_group_alloc_blocking_domain(group);
3174	if (ret)
3175		return ret;
3176	ret = __iommu_group_set_domain(group, group->blocking_domain);
3177	if (ret)
3178		return ret;
3179
3180	group->owner = owner;
3181	group->owner_cnt++;
3182	return 0;
3183}
3184
3185/**
3186 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3187 * @group: The group.
3188 * @owner: Caller specified pointer. Used for exclusive ownership.
3189 *
3190 * This is to support backward compatibility for vfio which manages the dma
3191 * ownership in iommu_group level. New invocations on this interface should be
3192 * prohibited. Only a single owner may exist for a group.
3193 */
3194int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
3195{
3196	int ret = 0;
3197
3198	if (WARN_ON(!owner))
3199		return -EINVAL;
3200
3201	mutex_lock(&group->mutex);
3202	if (group->owner_cnt) {
3203		ret = -EPERM;
3204		goto unlock_out;
3205	}
3206
3207	ret = __iommu_take_dma_ownership(group, owner);
3208unlock_out:
3209	mutex_unlock(&group->mutex);
3210
3211	return ret;
3212}
3213EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
3214
3215/**
3216 * iommu_device_claim_dma_owner() - Set DMA ownership of a device
3217 * @dev: The device.
3218 * @owner: Caller specified pointer. Used for exclusive ownership.
3219 *
3220 * Claim the DMA ownership of a device. Multiple devices in the same group may
3221 * concurrently claim ownership if they present the same owner value. Returns 0
3222 * on success and error code on failure
3223 */
3224int iommu_device_claim_dma_owner(struct device *dev, void *owner)
3225{
3226	/* Caller must be a probed driver on dev */
3227	struct iommu_group *group = dev->iommu_group;
3228	int ret = 0;
3229
3230	if (WARN_ON(!owner))
3231		return -EINVAL;
3232
3233	if (!group)
3234		return -ENODEV;
3235
3236	mutex_lock(&group->mutex);
3237	if (group->owner_cnt) {
3238		if (group->owner != owner) {
3239			ret = -EPERM;
3240			goto unlock_out;
3241		}
3242		group->owner_cnt++;
3243		goto unlock_out;
3244	}
3245
3246	ret = __iommu_take_dma_ownership(group, owner);
3247unlock_out:
3248	mutex_unlock(&group->mutex);
3249	return ret;
3250}
3251EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
3252
3253static void __iommu_release_dma_ownership(struct iommu_group *group)
3254{
3255	if (WARN_ON(!group->owner_cnt || !group->owner ||
3256		    !xa_empty(&group->pasid_array)))
3257		return;
3258
3259	group->owner_cnt = 0;
3260	group->owner = NULL;
3261	__iommu_group_set_domain_nofail(group, group->default_domain);
3262}
3263
3264/**
3265 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3266 * @group: The group
3267 *
3268 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
3269 */
3270void iommu_group_release_dma_owner(struct iommu_group *group)
3271{
3272	mutex_lock(&group->mutex);
3273	__iommu_release_dma_ownership(group);
3274	mutex_unlock(&group->mutex);
3275}
3276EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
3277
3278/**
3279 * iommu_device_release_dma_owner() - Release DMA ownership of a device
3280 * @dev: The device.
3281 *
3282 * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
3283 */
3284void iommu_device_release_dma_owner(struct device *dev)
3285{
3286	/* Caller must be a probed driver on dev */
3287	struct iommu_group *group = dev->iommu_group;
3288
3289	mutex_lock(&group->mutex);
3290	if (group->owner_cnt > 1)
3291		group->owner_cnt--;
3292	else
3293		__iommu_release_dma_ownership(group);
3294	mutex_unlock(&group->mutex);
3295}
3296EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
3297
3298/**
3299 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3300 * @group: The group.
3301 *
3302 * This provides status query on a given group. It is racy and only for
3303 * non-binding status reporting.
3304 */
3305bool iommu_group_dma_owner_claimed(struct iommu_group *group)
3306{
3307	unsigned int user;
3308
3309	mutex_lock(&group->mutex);
3310	user = group->owner_cnt;
3311	mutex_unlock(&group->mutex);
3312
3313	return user;
3314}
3315EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
3316
3317static int __iommu_set_group_pasid(struct iommu_domain *domain,
3318				   struct iommu_group *group, ioasid_t pasid)
3319{
3320	struct group_device *device;
3321	int ret = 0;
3322
3323	for_each_group_device(group, device) {
3324		ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
3325		if (ret)
3326			break;
3327	}
3328
3329	return ret;
3330}
3331
3332static void __iommu_remove_group_pasid(struct iommu_group *group,
3333				       ioasid_t pasid)
3334{
3335	struct group_device *device;
3336	const struct iommu_ops *ops;
3337
3338	for_each_group_device(group, device) {
3339		ops = dev_iommu_ops(device->dev);
3340		ops->remove_dev_pasid(device->dev, pasid);
3341	}
3342}
3343
3344/*
3345 * iommu_attach_device_pasid() - Attach a domain to pasid of device
3346 * @domain: the iommu domain.
3347 * @dev: the attached device.
3348 * @pasid: the pasid of the device.
3349 *
3350 * Return: 0 on success, or an error.
3351 */
3352int iommu_attach_device_pasid(struct iommu_domain *domain,
3353			      struct device *dev, ioasid_t pasid)
3354{
3355	/* Caller must be a probed driver on dev */
3356	struct iommu_group *group = dev->iommu_group;
3357	struct group_device *device;
3358	void *curr;
3359	int ret;
3360
3361	if (!domain->ops->set_dev_pasid)
3362		return -EOPNOTSUPP;
3363
3364	if (!group)
3365		return -ENODEV;
3366
3367	if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner ||
3368	    pasid == IOMMU_NO_PASID)
3369		return -EINVAL;
3370
3371	mutex_lock(&group->mutex);
3372	for_each_group_device(group, device) {
3373		if (pasid >= device->dev->iommu->max_pasids) {
3374			ret = -EINVAL;
3375			goto out_unlock;
3376		}
3377	}
3378
3379	curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
3380	if (curr) {
3381		ret = xa_err(curr) ? : -EBUSY;
3382		goto out_unlock;
3383	}
3384
3385	ret = __iommu_set_group_pasid(domain, group, pasid);
3386	if (ret) {
3387		__iommu_remove_group_pasid(group, pasid);
3388		xa_erase(&group->pasid_array, pasid);
3389	}
3390out_unlock:
3391	mutex_unlock(&group->mutex);
3392	return ret;
3393}
3394EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
3395
3396/*
3397 * iommu_detach_device_pasid() - Detach the domain from pasid of device
3398 * @domain: the iommu domain.
3399 * @dev: the attached device.
3400 * @pasid: the pasid of the device.
3401 *
3402 * The @domain must have been attached to @pasid of the @dev with
3403 * iommu_attach_device_pasid().
3404 */
3405void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
3406			       ioasid_t pasid)
3407{
3408	/* Caller must be a probed driver on dev */
3409	struct iommu_group *group = dev->iommu_group;
3410
3411	mutex_lock(&group->mutex);
3412	__iommu_remove_group_pasid(group, pasid);
3413	WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
3414	mutex_unlock(&group->mutex);
3415}
3416EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
3417
3418/*
3419 * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
3420 * @dev: the queried device
3421 * @pasid: the pasid of the device
3422 * @type: matched domain type, 0 for any match
3423 *
3424 * This is a variant of iommu_get_domain_for_dev(). It returns the existing
3425 * domain attached to pasid of a device. Callers must hold a lock around this
3426 * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
3427 * type is being manipulated. This API does not internally resolve races with
3428 * attach/detach.
3429 *
3430 * Return: attached domain on success, NULL otherwise.
3431 */
3432struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
3433						    ioasid_t pasid,
3434						    unsigned int type)
3435{
3436	/* Caller must be a probed driver on dev */
3437	struct iommu_group *group = dev->iommu_group;
3438	struct iommu_domain *domain;
3439
3440	if (!group)
3441		return NULL;
3442
3443	xa_lock(&group->pasid_array);
3444	domain = xa_load(&group->pasid_array, pasid);
3445	if (type && domain && domain->type != type)
3446		domain = ERR_PTR(-EBUSY);
3447	xa_unlock(&group->pasid_array);
3448
3449	return domain;
3450}
3451EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
3452
3453ioasid_t iommu_alloc_global_pasid(struct device *dev)
3454{
3455	int ret;
3456
3457	/* max_pasids == 0 means that the device does not support PASID */
3458	if (!dev->iommu->max_pasids)
3459		return IOMMU_PASID_INVALID;
3460
3461	/*
3462	 * max_pasids is set up by vendor driver based on number of PASID bits
3463	 * supported but the IDA allocation is inclusive.
3464	 */
3465	ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
3466			      dev->iommu->max_pasids - 1, GFP_KERNEL);
3467	return ret < 0 ? IOMMU_PASID_INVALID : ret;
3468}
3469EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
3470
3471void iommu_free_global_pasid(ioasid_t pasid)
3472{
3473	if (WARN_ON(pasid == IOMMU_PASID_INVALID))
3474		return;
3475
3476	ida_free(&iommu_global_pasid_ida, pasid);
3477}
3478EXPORT_SYMBOL_GPL(iommu_free_global_pasid);
3479