blob: 0e1f791873fa9ca13740e2b2283f79a85c0cae00 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002/*
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01004 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelfc2100e2008-11-26 17:21:24 +01005 */
6
Joerg Roedel92e70662015-05-28 18:41:24 +02007#define pr_fmt(fmt) "iommu: " fmt
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02008
Joerg Roedel905d66c2011-09-06 16:03:26 +02009#include <linux/device.h>
Robin Murphy46983fc2021-08-11 13:21:15 +010010#include <linux/dma-iommu.h>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040011#include <linux/kernel.h>
Will Deacone7d6fff2021-06-16 06:38:46 -070012#include <linux/bits.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010013#include <linux/bug.h>
14#include <linux/types.h>
Paul Gortmakerc1af7b42018-12-01 14:19:09 -050015#include <linux/init.h>
16#include <linux/export.h>
Andrew Morton60db4022009-05-06 16:03:07 -070017#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010018#include <linux/errno.h>
19#include <linux/iommu.h>
Alex Williamsond72e31c2012-05-30 14:18:53 -060020#include <linux/idr.h>
21#include <linux/notifier.h>
22#include <linux/err.h>
Alex Williamson104a1c12014-07-03 09:51:18 -060023#include <linux/pci.h>
Alex Williamsonf096c062014-09-19 10:03:06 -060024#include <linux/bitops.h>
Robin Murphy57f98d22016-09-13 10:54:14 +010025#include <linux/property.h>
Nipun Guptaeab03e22018-09-10 19:19:18 +053026#include <linux/fsl/mc.h>
Will Deacon25f003d2019-12-19 12:03:41 +000027#include <linux/module.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060028#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010029
Alex Williamsond72e31c2012-05-30 14:18:53 -060030static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020031static DEFINE_IDA(iommu_group_ida);
Joerg Roedel22bb1822019-08-19 15:22:54 +020032
33static unsigned int iommu_def_domain_type __read_mostly;
Robin Murphye96763e2021-08-11 13:21:37 +010034static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
Joerg Roedelfaf14982019-08-19 15:22:46 +020035static u32 iommu_cmd_line __read_mostly;
Alex Williamsond72e31c2012-05-30 14:18:53 -060036
37struct iommu_group {
38 struct kobject kobj;
39 struct kobject *devices_kobj;
40 struct list_head devices;
41 struct mutex mutex;
42 struct blocking_notifier_head notifier;
43 void *iommu_data;
44 void (*iommu_data_release)(void *iommu_data);
45 char *name;
46 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020047 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020048 struct iommu_domain *domain;
Joerg Roedel41df6dc2020-04-29 15:36:47 +020049 struct list_head entry;
Alex Williamsond72e31c2012-05-30 14:18:53 -060050};
51
Joerg Roedelc09e22d2017-02-01 12:19:46 +010052struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060053 struct list_head list;
54 struct device *dev;
55 char *name;
56};
57
58struct iommu_group_attribute {
59 struct attribute attr;
60 ssize_t (*show)(struct iommu_group *group, char *buf);
61 ssize_t (*store)(struct iommu_group *group,
62 const char *buf, size_t count);
63};
64
Eric Augerbc7d12b92017-01-19 20:57:52 +000065static const char * const iommu_group_resv_type_string[] = {
Eric Augeradfd3732019-06-03 08:53:35 +020066 [IOMMU_RESV_DIRECT] = "direct",
67 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
68 [IOMMU_RESV_RESERVED] = "reserved",
69 [IOMMU_RESV_MSI] = "msi",
70 [IOMMU_RESV_SW_MSI] = "msi",
Eric Augerbc7d12b92017-01-19 20:57:52 +000071};
72
Joerg Roedelfaf14982019-08-19 15:22:46 +020073#define IOMMU_CMD_LINE_DMA_API BIT(0)
Robin Murphya250c232021-04-01 17:52:54 +020074#define IOMMU_CMD_LINE_STRICT BIT(1)
Joerg Roedelfaf14982019-08-19 15:22:46 +020075
Joerg Roedel79659190e2020-05-25 15:01:22 +020076static int iommu_alloc_default_domain(struct iommu_group *group,
77 struct device *dev);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020078static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
79 unsigned type);
80static int __iommu_attach_device(struct iommu_domain *domain,
81 struct device *dev);
82static int __iommu_attach_group(struct iommu_domain *domain,
83 struct iommu_group *group);
84static void __iommu_detach_group(struct iommu_domain *domain,
85 struct iommu_group *group);
Joerg Roedelce574c22020-04-29 15:36:50 +020086static int iommu_create_device_direct_mappings(struct iommu_group *group,
87 struct device *dev);
Joerg Roedel1b032ec2020-04-29 15:37:12 +020088static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +080089static ssize_t iommu_group_store_type(struct iommu_group *group,
90 const char *buf, size_t count);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020091
Alex Williamsond72e31c2012-05-30 14:18:53 -060092#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
93struct iommu_group_attribute iommu_group_attr_##_name = \
94 __ATTR(_name, _mode, _show, _store)
95
96#define to_iommu_group_attr(_attr) \
97 container_of(_attr, struct iommu_group_attribute, attr)
98#define to_iommu_group(_kobj) \
99 container_of(_kobj, struct iommu_group, kobj)
100
Joerg Roedelb0119e82017-02-01 13:23:08 +0100101static LIST_HEAD(iommu_device_list);
102static DEFINE_SPINLOCK(iommu_device_lock);
103
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200104/*
105 * Use a function instead of an array here because the domain-type is a
106 * bit-field, so an array would waste memory.
107 */
108static const char *iommu_domain_type_str(unsigned int t)
109{
110 switch (t) {
111 case IOMMU_DOMAIN_BLOCKED:
112 return "Blocked";
113 case IOMMU_DOMAIN_IDENTITY:
114 return "Passthrough";
115 case IOMMU_DOMAIN_UNMANAGED:
116 return "Unmanaged";
117 case IOMMU_DOMAIN_DMA:
Robin Murphybf3aed462021-08-11 13:21:30 +0100118 case IOMMU_DOMAIN_DMA_FQ:
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200119 return "Translated";
120 default:
121 return "Unknown";
122 }
123}
124
125static int __init iommu_subsys_init(void)
126{
Christoph Hellwig31897132021-04-01 17:52:53 +0200127 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
Joerg Roedel22bb1822019-08-19 15:22:54 +0200128 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
129 iommu_set_default_passthrough(false);
130 else
131 iommu_set_default_translated(false);
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200132
Joerg Roedel2896ba42019-09-03 15:15:44 +0200133 if (iommu_default_passthrough() && mem_encrypt_active()) {
134 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200135 iommu_set_default_translated(false);
136 }
Joerg Roedel22bb1822019-08-19 15:22:54 +0200137 }
138
Robin Murphyc2089162021-08-11 13:21:34 +0100139 if (!iommu_default_passthrough() && !iommu_dma_strict)
140 iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
141
Joerg Roedel22bb1822019-08-19 15:22:54 +0200142 pr_info("Default domain type: %s %s\n",
143 iommu_domain_type_str(iommu_def_domain_type),
Christoph Hellwig31897132021-04-01 17:52:53 +0200144 (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
145 "(set via kernel command line)" : "");
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200146
Robin Murphy7cf8a632021-08-11 13:21:36 +0100147 if (!iommu_default_passthrough())
148 pr_info("DMA domain TLB invalidation policy: %s mode %s\n",
149 iommu_dma_strict ? "strict" : "lazy",
150 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
151 "(set via kernel command line)" : "");
John Garryd8577d22021-07-12 19:12:16 +0800152
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200153 return 0;
154}
155subsys_initcall(iommu_subsys_init);
156
Robin Murphy2d471b22021-04-01 14:56:26 +0100157/**
158 * iommu_device_register() - Register an IOMMU hardware instance
159 * @iommu: IOMMU handle for the instance
160 * @ops: IOMMU ops to associate with the instance
161 * @hwdev: (optional) actual instance device, used for fwnode lookup
162 *
163 * Return: 0 on success, or an error.
164 */
165int iommu_device_register(struct iommu_device *iommu,
166 const struct iommu_ops *ops, struct device *hwdev)
Joerg Roedelb0119e82017-02-01 13:23:08 +0100167{
Robin Murphy2d471b22021-04-01 14:56:26 +0100168 /* We need to be able to take module references appropriately */
169 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
170 return -EINVAL;
171
172 iommu->ops = ops;
173 if (hwdev)
174 iommu->fwnode = hwdev->fwnode;
175
Joerg Roedelb0119e82017-02-01 13:23:08 +0100176 spin_lock(&iommu_device_lock);
177 list_add_tail(&iommu->list, &iommu_device_list);
178 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100179 return 0;
180}
Will Deacona7ba5c32019-12-19 12:03:37 +0000181EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100182
183void iommu_device_unregister(struct iommu_device *iommu)
184{
185 spin_lock(&iommu_device_lock);
186 list_del(&iommu->list);
187 spin_unlock(&iommu_device_lock);
188}
Will Deacona7ba5c32019-12-19 12:03:37 +0000189EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100190
Joerg Roedel045a7042020-03-26 16:08:30 +0100191static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100192{
Joerg Roedel045a7042020-03-26 16:08:30 +0100193 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100194
195 if (param)
196 return param;
197
198 param = kzalloc(sizeof(*param), GFP_KERNEL);
199 if (!param)
200 return NULL;
201
202 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100203 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100204 return param;
205}
206
Joerg Roedel045a7042020-03-26 16:08:30 +0100207static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100208{
Kevin Hao5375e872020-04-02 22:37:49 +0800209 iommu_fwspec_free(dev);
Joerg Roedel045a7042020-03-26 16:08:30 +0100210 kfree(dev->iommu);
211 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100212}
213
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200214static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100215{
216 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200217 struct iommu_device *iommu_dev;
218 struct iommu_group *group;
Jacob Pan0c830e62019-06-03 15:57:48 +0100219 int ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100220
Jacob Pan0c830e62019-06-03 15:57:48 +0100221 if (!ops)
Thierry Redingf38338c2020-05-11 18:10:00 +0200222 return -ENODEV;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100223
Joerg Roedel045a7042020-03-26 16:08:30 +0100224 if (!dev_iommu_get(dev))
Jacob Pan0c830e62019-06-03 15:57:48 +0100225 return -ENOMEM;
226
Will Deacon25f003d2019-12-19 12:03:41 +0000227 if (!try_module_get(ops->owner)) {
228 ret = -EINVAL;
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200229 goto err_free;
Will Deacon25f003d2019-12-19 12:03:41 +0000230 }
231
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200232 iommu_dev = ops->probe_device(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200233 if (IS_ERR(iommu_dev)) {
234 ret = PTR_ERR(iommu_dev);
235 goto out_module_put;
236 }
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200237
238 dev->iommu->iommu_dev = iommu_dev;
239
240 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200241 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200242 ret = PTR_ERR(group);
243 goto out_release;
244 }
245 iommu_group_put(group);
246
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200247 if (group_list && !group->default_domain && list_empty(&group->entry))
248 list_add_tail(&group->entry, group_list);
249
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200250 iommu_device_link(iommu_dev, dev);
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100251
Will Deacon25f003d2019-12-19 12:03:41 +0000252 return 0;
253
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200254out_release:
255 ops->release_device(dev);
256
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200257out_module_put:
Will Deacon25f003d2019-12-19 12:03:41 +0000258 module_put(ops->owner);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200259
260err_free:
Joerg Roedel045a7042020-03-26 16:08:30 +0100261 dev_iommu_free(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200262
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100263 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100264}
265
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200266int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200267{
268 const struct iommu_ops *ops = dev->bus->iommu_ops;
269 struct iommu_group *group;
270 int ret;
271
272 ret = __iommu_probe_device(dev, NULL);
273 if (ret)
274 goto err_out;
275
Joerg Roedel79659190e2020-05-25 15:01:22 +0200276 group = iommu_group_get(dev);
Yang Yingliang058236e2020-11-26 21:38:25 +0800277 if (!group) {
278 ret = -ENODEV;
Joerg Roedel79659190e2020-05-25 15:01:22 +0200279 goto err_release;
Yang Yingliang058236e2020-11-26 21:38:25 +0800280 }
Joerg Roedel79659190e2020-05-25 15:01:22 +0200281
Joerg Roedelcf193882020-04-29 15:36:48 +0200282 /*
283 * Try to allocate a default domain - needs support from the
284 * IOMMU driver. There are still some drivers which don't
285 * support default domains, so the return value is not yet
286 * checked.
287 */
Joerg Roedel79659190e2020-05-25 15:01:22 +0200288 iommu_alloc_default_domain(group, dev);
Joerg Roedelcf193882020-04-29 15:36:48 +0200289
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000290 if (group->default_domain) {
Joerg Roedelcf193882020-04-29 15:36:48 +0200291 ret = __iommu_attach_device(group->default_domain, dev);
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000292 if (ret) {
293 iommu_group_put(group);
294 goto err_release;
295 }
296 }
Joerg Roedelcf193882020-04-29 15:36:48 +0200297
Joerg Roedelce574c22020-04-29 15:36:50 +0200298 iommu_create_device_direct_mappings(group, dev);
299
Joerg Roedelcf193882020-04-29 15:36:48 +0200300 iommu_group_put(group);
301
Joerg Roedelcf193882020-04-29 15:36:48 +0200302 if (ops->probe_finalize)
303 ops->probe_finalize(dev);
304
305 return 0;
306
307err_release:
308 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200309
Joerg Roedelcf193882020-04-29 15:36:48 +0200310err_out:
311 return ret;
312
313}
314
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100315void iommu_release_device(struct device *dev)
316{
317 const struct iommu_ops *ops = dev->bus->iommu_ops;
318
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200319 if (!dev->iommu)
320 return;
Jacob Pan0c830e62019-06-03 15:57:48 +0100321
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200322 iommu_device_unlink(dev->iommu->iommu_dev, dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200323
324 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100325
Qian Cai9ac85452020-07-03 20:10:03 -0400326 iommu_group_remove_device(dev);
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200327 module_put(ops->owner);
328 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100329}
330
Will Deaconfccb4e32017-01-05 18:38:26 +0000331static int __init iommu_set_def_domain_type(char *str)
332{
333 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300334 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000335
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300336 ret = kstrtobool(str, &pt);
337 if (ret)
338 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000339
Joerg Roedeladab0b02019-08-19 15:22:48 +0200340 if (pt)
341 iommu_set_default_passthrough(true);
342 else
343 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200344
Will Deaconfccb4e32017-01-05 18:38:26 +0000345 return 0;
346}
347early_param("iommu.passthrough", iommu_set_def_domain_type);
348
Zhen Lei68a6efe2018-09-20 17:10:23 +0100349static int __init iommu_dma_setup(char *str)
350{
Robin Murphya250c232021-04-01 17:52:54 +0200351 int ret = kstrtobool(str, &iommu_dma_strict);
352
353 if (!ret)
354 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
355 return ret;
Zhen Lei68a6efe2018-09-20 17:10:23 +0100356}
357early_param("iommu.strict", iommu_dma_setup);
358
John Garry308723e2021-07-12 19:12:20 +0800359void iommu_set_dma_strict(void)
Robin Murphya250c232021-04-01 17:52:54 +0200360{
John Garry308723e2021-07-12 19:12:20 +0800361 iommu_dma_strict = true;
Robin Murphyc2089162021-08-11 13:21:34 +0100362 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
363 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Robin Murphya250c232021-04-01 17:52:54 +0200364}
365
Alex Williamsond72e31c2012-05-30 14:18:53 -0600366static ssize_t iommu_group_attr_show(struct kobject *kobj,
367 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400368{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600369 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
370 struct iommu_group *group = to_iommu_group(kobj);
371 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400372
Alex Williamsond72e31c2012-05-30 14:18:53 -0600373 if (attr->show)
374 ret = attr->show(group, buf);
375 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400376}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600377
378static ssize_t iommu_group_attr_store(struct kobject *kobj,
379 struct attribute *__attr,
380 const char *buf, size_t count)
381{
382 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
383 struct iommu_group *group = to_iommu_group(kobj);
384 ssize_t ret = -EIO;
385
386 if (attr->store)
387 ret = attr->store(group, buf, count);
388 return ret;
389}
390
391static const struct sysfs_ops iommu_group_sysfs_ops = {
392 .show = iommu_group_attr_show,
393 .store = iommu_group_attr_store,
394};
395
396static int iommu_group_create_file(struct iommu_group *group,
397 struct iommu_group_attribute *attr)
398{
399 return sysfs_create_file(&group->kobj, &attr->attr);
400}
401
402static void iommu_group_remove_file(struct iommu_group *group,
403 struct iommu_group_attribute *attr)
404{
405 sysfs_remove_file(&group->kobj, &attr->attr);
406}
407
408static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
409{
410 return sprintf(buf, "%s\n", group->name);
411}
412
Eric Auger6c65fb32017-01-19 20:57:51 +0000413/**
414 * iommu_insert_resv_region - Insert a new region in the
415 * list of reserved regions.
416 * @new: new region to insert
417 * @regions: list of regions
418 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200419 * Elements are sorted by start address and overlapping segments
420 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000421 */
Wei Yongjun1b0b2a82020-07-13 22:25:42 +0800422static int iommu_insert_resv_region(struct iommu_resv_region *new,
423 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000424{
Eric Auger4dbd2582019-08-21 14:09:40 +0200425 struct iommu_resv_region *iter, *tmp, *nr, *top;
426 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000427
Eric Auger4dbd2582019-08-21 14:09:40 +0200428 nr = iommu_alloc_resv_region(new->start, new->length,
429 new->prot, new->type);
430 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000431 return -ENOMEM;
432
Eric Auger4dbd2582019-08-21 14:09:40 +0200433 /* First add the new element based on start address sorting */
434 list_for_each_entry(iter, regions, list) {
435 if (nr->start < iter->start ||
436 (nr->start == iter->start && nr->type <= iter->type))
437 break;
438 }
439 list_add_tail(&nr->list, &iter->list);
440
441 /* Merge overlapping segments of type nr->type in @regions, if any */
442 list_for_each_entry_safe(iter, tmp, regions, list) {
443 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
444
Eric Auger4c80ba32019-11-26 18:54:13 +0100445 /* no merge needed on elements of different types than @new */
446 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200447 list_move_tail(&iter->list, &stack);
448 continue;
449 }
450
451 /* look for the last stack element of same type as @iter */
452 list_for_each_entry_reverse(top, &stack, list)
453 if (top->type == iter->type)
454 goto check_overlap;
455
456 list_move_tail(&iter->list, &stack);
457 continue;
458
459check_overlap:
460 top_end = top->start + top->length - 1;
461
462 if (iter->start > top_end + 1) {
463 list_move_tail(&iter->list, &stack);
464 } else {
465 top->length = max(top_end, iter_end) - top->start + 1;
466 list_del(&iter->list);
467 kfree(iter);
468 }
469 }
470 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000471 return 0;
472}
473
474static int
475iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
476 struct list_head *group_resv_regions)
477{
478 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100479 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000480
481 list_for_each_entry(entry, dev_resv_regions, list) {
482 ret = iommu_insert_resv_region(entry, group_resv_regions);
483 if (ret)
484 break;
485 }
486 return ret;
487}
488
489int iommu_get_group_resv_regions(struct iommu_group *group,
490 struct list_head *head)
491{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100492 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000493 int ret = 0;
494
495 mutex_lock(&group->mutex);
496 list_for_each_entry(device, &group->devices, list) {
497 struct list_head dev_resv_regions;
498
499 INIT_LIST_HEAD(&dev_resv_regions);
500 iommu_get_resv_regions(device->dev, &dev_resv_regions);
501 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
502 iommu_put_resv_regions(device->dev, &dev_resv_regions);
503 if (ret)
504 break;
505 }
506 mutex_unlock(&group->mutex);
507 return ret;
508}
509EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
510
Eric Augerbc7d12b92017-01-19 20:57:52 +0000511static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
512 char *buf)
513{
514 struct iommu_resv_region *region, *next;
515 struct list_head group_resv_regions;
516 char *str = buf;
517
518 INIT_LIST_HEAD(&group_resv_regions);
519 iommu_get_group_resv_regions(group, &group_resv_regions);
520
521 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
522 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
523 (long long int)region->start,
524 (long long int)(region->start +
525 region->length - 1),
526 iommu_group_resv_type_string[region->type]);
527 kfree(region);
528 }
529
530 return (str - buf);
531}
532
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700533static ssize_t iommu_group_show_type(struct iommu_group *group,
534 char *buf)
535{
536 char *type = "unknown\n";
537
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800538 mutex_lock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700539 if (group->default_domain) {
540 switch (group->default_domain->type) {
541 case IOMMU_DOMAIN_BLOCKED:
542 type = "blocked\n";
543 break;
544 case IOMMU_DOMAIN_IDENTITY:
545 type = "identity\n";
546 break;
547 case IOMMU_DOMAIN_UNMANAGED:
548 type = "unmanaged\n";
549 break;
550 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800551 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700552 break;
Robin Murphybf3aed462021-08-11 13:21:30 +0100553 case IOMMU_DOMAIN_DMA_FQ:
554 type = "DMA-FQ\n";
555 break;
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700556 }
557 }
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800558 mutex_unlock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700559 strcpy(buf, type);
560
561 return strlen(type);
562}
563
Alex Williamsond72e31c2012-05-30 14:18:53 -0600564static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
565
Eric Augerbc7d12b92017-01-19 20:57:52 +0000566static IOMMU_GROUP_ATTR(reserved_regions, 0444,
567 iommu_group_show_resv_regions, NULL);
568
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +0800569static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
570 iommu_group_store_type);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700571
Alex Williamsond72e31c2012-05-30 14:18:53 -0600572static void iommu_group_release(struct kobject *kobj)
573{
574 struct iommu_group *group = to_iommu_group(kobj);
575
Joerg Roedel269aa802015-05-28 18:41:25 +0200576 pr_debug("Releasing group %d\n", group->id);
577
Alex Williamsond72e31c2012-05-30 14:18:53 -0600578 if (group->iommu_data_release)
579 group->iommu_data_release(group->iommu_data);
580
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200581 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600582
Joerg Roedel53723dc2015-05-28 18:41:29 +0200583 if (group->default_domain)
584 iommu_domain_free(group->default_domain);
585
Alex Williamsond72e31c2012-05-30 14:18:53 -0600586 kfree(group->name);
587 kfree(group);
588}
589
590static struct kobj_type iommu_group_ktype = {
591 .sysfs_ops = &iommu_group_sysfs_ops,
592 .release = iommu_group_release,
593};
594
595/**
596 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600597 *
598 * This function is called by an iommu driver to allocate a new iommu
599 * group. The iommu group represents the minimum granularity of the iommu.
600 * Upon successful return, the caller holds a reference to the supplied
601 * group in order to hold the group until devices are added. Use
602 * iommu_group_put() to release this extra reference count, allowing the
603 * group to be automatically reclaimed once it has no devices or external
604 * references.
605 */
606struct iommu_group *iommu_group_alloc(void)
607{
608 struct iommu_group *group;
609 int ret;
610
611 group = kzalloc(sizeof(*group), GFP_KERNEL);
612 if (!group)
613 return ERR_PTR(-ENOMEM);
614
615 group->kobj.kset = iommu_group_kset;
616 mutex_init(&group->mutex);
617 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200618 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600619 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
620
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200621 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
622 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600623 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200624 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600625 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200626 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600627
628 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
629 NULL, "%d", group->id);
630 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200631 ida_simple_remove(&iommu_group_ida, group->id);
Qiushi Wu7cc31612020-05-27 16:00:19 -0500632 kobject_put(&group->kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600633 return ERR_PTR(ret);
634 }
635
636 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
637 if (!group->devices_kobj) {
638 kobject_put(&group->kobj); /* triggers .release & free */
639 return ERR_PTR(-ENOMEM);
640 }
641
642 /*
643 * The devices_kobj holds a reference on the group kobject, so
644 * as long as that exists so will the group. We can therefore
645 * use the devices_kobj for reference counting.
646 */
647 kobject_put(&group->kobj);
648
Eric Augerbc7d12b92017-01-19 20:57:52 +0000649 ret = iommu_group_create_file(group,
650 &iommu_group_attr_reserved_regions);
651 if (ret)
652 return ERR_PTR(ret);
653
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700654 ret = iommu_group_create_file(group, &iommu_group_attr_type);
655 if (ret)
656 return ERR_PTR(ret);
657
Joerg Roedel269aa802015-05-28 18:41:25 +0200658 pr_debug("Allocated group %d\n", group->id);
659
Alex Williamsond72e31c2012-05-30 14:18:53 -0600660 return group;
661}
662EXPORT_SYMBOL_GPL(iommu_group_alloc);
663
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100664struct iommu_group *iommu_group_get_by_id(int id)
665{
666 struct kobject *group_kobj;
667 struct iommu_group *group;
668 const char *name;
669
670 if (!iommu_group_kset)
671 return NULL;
672
673 name = kasprintf(GFP_KERNEL, "%d", id);
674 if (!name)
675 return NULL;
676
677 group_kobj = kset_find_obj(iommu_group_kset, name);
678 kfree(name);
679
680 if (!group_kobj)
681 return NULL;
682
683 group = container_of(group_kobj, struct iommu_group, kobj);
684 BUG_ON(group->id != id);
685
686 kobject_get(group->devices_kobj);
687 kobject_put(&group->kobj);
688
689 return group;
690}
691EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
692
Alex Williamsond72e31c2012-05-30 14:18:53 -0600693/**
694 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
695 * @group: the group
696 *
697 * iommu drivers can store data in the group for use when doing iommu
698 * operations. This function provides a way to retrieve it. Caller
699 * should hold a group reference.
700 */
701void *iommu_group_get_iommudata(struct iommu_group *group)
702{
703 return group->iommu_data;
704}
705EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
706
707/**
708 * iommu_group_set_iommudata - set iommu_data for a group
709 * @group: the group
710 * @iommu_data: new data
711 * @release: release function for iommu_data
712 *
713 * iommu drivers can store data in the group for use when doing iommu
714 * operations. This function provides a way to set the data after
715 * the group has been allocated. Caller should hold a group reference.
716 */
717void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
718 void (*release)(void *iommu_data))
719{
720 group->iommu_data = iommu_data;
721 group->iommu_data_release = release;
722}
723EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
724
725/**
726 * iommu_group_set_name - set name for a group
727 * @group: the group
728 * @name: name
729 *
730 * Allow iommu driver to set a name for a group. When set it will
731 * appear in a name attribute file under the group in sysfs.
732 */
733int iommu_group_set_name(struct iommu_group *group, const char *name)
734{
735 int ret;
736
737 if (group->name) {
738 iommu_group_remove_file(group, &iommu_group_attr_name);
739 kfree(group->name);
740 group->name = NULL;
741 if (!name)
742 return 0;
743 }
744
745 group->name = kstrdup(name, GFP_KERNEL);
746 if (!group->name)
747 return -ENOMEM;
748
749 ret = iommu_group_create_file(group, &iommu_group_attr_name);
750 if (ret) {
751 kfree(group->name);
752 group->name = NULL;
753 return ret;
754 }
755
756 return 0;
757}
758EXPORT_SYMBOL_GPL(iommu_group_set_name);
759
Joerg Roedelce574c22020-04-29 15:36:50 +0200760static int iommu_create_device_direct_mappings(struct iommu_group *group,
761 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200762{
763 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000764 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200765 struct list_head mappings;
766 unsigned long pg_size;
767 int ret = 0;
768
Robin Murphybf3aed462021-08-11 13:21:30 +0100769 if (!domain || !iommu_is_dma_domain(domain))
Joerg Roedelbeed2822015-05-28 18:41:34 +0200770 return 0;
771
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100772 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200773
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100774 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200775 INIT_LIST_HEAD(&mappings);
776
Eric Augere5b52342017-01-19 20:57:47 +0000777 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200778
779 /* We need to consider overlapping regions for different devices */
780 list_for_each_entry(entry, &mappings, list) {
781 dma_addr_t start, end, addr;
Yong Wu093b32a2020-12-07 17:35:53 +0800782 size_t map_size = 0;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200783
Eric Augere5b52342017-01-19 20:57:47 +0000784 if (domain->ops->apply_resv_region)
785 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200786
Joerg Roedelbeed2822015-05-28 18:41:34 +0200787 start = ALIGN(entry->start, pg_size);
788 end = ALIGN(entry->start + entry->length, pg_size);
789
Eric Augeradfd3732019-06-03 08:53:35 +0200790 if (entry->type != IOMMU_RESV_DIRECT &&
791 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000792 continue;
793
Yong Wu093b32a2020-12-07 17:35:53 +0800794 for (addr = start; addr <= end; addr += pg_size) {
Joerg Roedelbeed2822015-05-28 18:41:34 +0200795 phys_addr_t phys_addr;
796
Yong Wu093b32a2020-12-07 17:35:53 +0800797 if (addr == end)
798 goto map_end;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200799
Yong Wu093b32a2020-12-07 17:35:53 +0800800 phys_addr = iommu_iova_to_phys(domain, addr);
801 if (!phys_addr) {
802 map_size += pg_size;
803 continue;
804 }
805
806map_end:
807 if (map_size) {
808 ret = iommu_map(domain, addr - map_size,
809 addr - map_size, map_size,
810 entry->prot);
811 if (ret)
812 goto out;
813 map_size = 0;
814 }
Joerg Roedelbeed2822015-05-28 18:41:34 +0200815 }
816
817 }
818
Tom Murphyaae4c8e2020-08-17 22:00:49 +0100819 iommu_flush_iotlb_all(domain);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200820
Joerg Roedelbeed2822015-05-28 18:41:34 +0200821out:
Eric Augere5b52342017-01-19 20:57:47 +0000822 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200823
824 return ret;
825}
826
Joerg Roedelbd421262020-05-19 15:03:40 +0200827static bool iommu_is_attach_deferred(struct iommu_domain *domain,
828 struct device *dev)
829{
830 if (domain->ops->is_attach_deferred)
831 return domain->ops->is_attach_deferred(domain, dev);
832
833 return false;
834}
835
Alex Williamsond72e31c2012-05-30 14:18:53 -0600836/**
837 * iommu_group_add_device - add a device to an iommu group
838 * @group: the group into which to add the device (reference should be held)
839 * @dev: the device
840 *
841 * This function is called by an iommu driver to add a device into a
842 * group. Adding a device increments the group reference count.
843 */
844int iommu_group_add_device(struct iommu_group *group, struct device *dev)
845{
846 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100847 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600848
849 device = kzalloc(sizeof(*device), GFP_KERNEL);
850 if (!device)
851 return -ENOMEM;
852
853 device->dev = dev;
854
855 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000856 if (ret)
857 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600858
859 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
860rename:
861 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000862 ret = -ENOMEM;
863 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600864 }
865
866 ret = sysfs_create_link_nowarn(group->devices_kobj,
867 &dev->kobj, device->name);
868 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600869 if (ret == -EEXIST && i >= 0) {
870 /*
871 * Account for the slim chance of collision
872 * and append an instance to the name.
873 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000874 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600875 device->name = kasprintf(GFP_KERNEL, "%s.%d",
876 kobject_name(&dev->kobj), i++);
877 goto rename;
878 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000879 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600880 }
881
882 kobject_get(group->devices_kobj);
883
884 dev->iommu_group = group;
885
886 mutex_lock(&group->mutex);
887 list_add_tail(&device->list, &group->devices);
Joerg Roedelbd421262020-05-19 15:03:40 +0200888 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
Robin Murphy797a8b42017-01-16 12:58:07 +0000889 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600890 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000891 if (ret)
892 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600893
894 /* Notify any listeners about change to group. */
895 blocking_notifier_call_chain(&group->notifier,
896 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600897
898 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200899
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600900 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200901
Alex Williamsond72e31c2012-05-30 14:18:53 -0600902 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000903
904err_put_group:
905 mutex_lock(&group->mutex);
906 list_del(&device->list);
907 mutex_unlock(&group->mutex);
908 dev->iommu_group = NULL;
909 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700910 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000911err_free_name:
912 kfree(device->name);
913err_remove_link:
914 sysfs_remove_link(&dev->kobj, "iommu_group");
915err_free_device:
916 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600917 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000918 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600919}
920EXPORT_SYMBOL_GPL(iommu_group_add_device);
921
922/**
923 * iommu_group_remove_device - remove a device from it's current group
924 * @dev: device to be removed
925 *
926 * This function is called by an iommu driver to remove the device from
927 * it's current group. This decrements the iommu group reference count.
928 */
929void iommu_group_remove_device(struct device *dev)
930{
931 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100932 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600933
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600934 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200935
Alex Williamsond72e31c2012-05-30 14:18:53 -0600936 /* Pre-notify listeners that a device is being removed. */
937 blocking_notifier_call_chain(&group->notifier,
938 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
939
940 mutex_lock(&group->mutex);
941 list_for_each_entry(tmp_device, &group->devices, list) {
942 if (tmp_device->dev == dev) {
943 device = tmp_device;
944 list_del(&device->list);
945 break;
946 }
947 }
948 mutex_unlock(&group->mutex);
949
950 if (!device)
951 return;
952
953 sysfs_remove_link(group->devices_kobj, device->name);
954 sysfs_remove_link(&dev->kobj, "iommu_group");
955
Shuah Khan2e757082013-08-15 11:59:25 -0600956 trace_remove_device_from_group(group->id, dev);
957
Alex Williamsond72e31c2012-05-30 14:18:53 -0600958 kfree(device->name);
959 kfree(device);
960 dev->iommu_group = NULL;
961 kobject_put(group->devices_kobj);
962}
963EXPORT_SYMBOL_GPL(iommu_group_remove_device);
964
Joerg Roedel426a2732015-05-28 18:41:30 +0200965static int iommu_group_device_count(struct iommu_group *group)
966{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100967 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200968 int ret = 0;
969
970 list_for_each_entry(entry, &group->devices, list)
971 ret++;
972
973 return ret;
974}
975
Alex Williamsond72e31c2012-05-30 14:18:53 -0600976/**
977 * iommu_group_for_each_dev - iterate over each device in the group
978 * @group: the group
979 * @data: caller opaque data to be passed to callback function
980 * @fn: caller supplied callback function
981 *
982 * This function is called by group users to iterate over group devices.
983 * Callers should hold a reference count to the group during callback.
984 * The group->mutex is held across callbacks, which will block calls to
985 * iommu_group_add/remove_device.
986 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200987static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
988 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600989{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100990 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600991 int ret = 0;
992
Alex Williamsond72e31c2012-05-30 14:18:53 -0600993 list_for_each_entry(device, &group->devices, list) {
994 ret = fn(device->dev, data);
995 if (ret)
996 break;
997 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200998 return ret;
999}
1000
1001
1002int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1003 int (*fn)(struct device *, void *))
1004{
1005 int ret;
1006
1007 mutex_lock(&group->mutex);
1008 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001009 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001010
Alex Williamsond72e31c2012-05-30 14:18:53 -06001011 return ret;
1012}
1013EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1014
1015/**
1016 * iommu_group_get - Return the group for a device and increment reference
1017 * @dev: get the group that this device belongs to
1018 *
1019 * This function is called by iommu drivers and users to get the group
1020 * for the specified device. If found, the group is returned and the group
1021 * reference in incremented, else NULL.
1022 */
1023struct iommu_group *iommu_group_get(struct device *dev)
1024{
1025 struct iommu_group *group = dev->iommu_group;
1026
1027 if (group)
1028 kobject_get(group->devices_kobj);
1029
1030 return group;
1031}
1032EXPORT_SYMBOL_GPL(iommu_group_get);
1033
1034/**
Robin Murphy13f59a72016-11-11 17:59:21 +00001035 * iommu_group_ref_get - Increment reference on a group
1036 * @group: the group to use, must not be NULL
1037 *
1038 * This function is called by iommu drivers to take additional references on an
1039 * existing group. Returns the given group for convenience.
1040 */
1041struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1042{
1043 kobject_get(group->devices_kobj);
1044 return group;
1045}
Will Deacona7ba5c32019-12-19 12:03:37 +00001046EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +00001047
1048/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001049 * iommu_group_put - Decrement group reference
1050 * @group: the group to use
1051 *
1052 * This function is called by iommu drivers and users to release the
1053 * iommu group. Once the reference count is zero, the group is released.
1054 */
1055void iommu_group_put(struct iommu_group *group)
1056{
1057 if (group)
1058 kobject_put(group->devices_kobj);
1059}
1060EXPORT_SYMBOL_GPL(iommu_group_put);
1061
1062/**
1063 * iommu_group_register_notifier - Register a notifier for group changes
1064 * @group: the group to watch
1065 * @nb: notifier block to signal
1066 *
1067 * This function allows iommu group users to track changes in a group.
1068 * See include/linux/iommu.h for actions sent via this notifier. Caller
1069 * should hold a reference to the group throughout notifier registration.
1070 */
1071int iommu_group_register_notifier(struct iommu_group *group,
1072 struct notifier_block *nb)
1073{
1074 return blocking_notifier_chain_register(&group->notifier, nb);
1075}
1076EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1077
1078/**
1079 * iommu_group_unregister_notifier - Unregister a notifier
1080 * @group: the group to watch
1081 * @nb: notifier block to signal
1082 *
1083 * Unregister a previously registered group notifier block.
1084 */
1085int iommu_group_unregister_notifier(struct iommu_group *group,
1086 struct notifier_block *nb)
1087{
1088 return blocking_notifier_chain_unregister(&group->notifier, nb);
1089}
1090EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1091
1092/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001093 * iommu_register_device_fault_handler() - Register a device fault handler
1094 * @dev: the device
1095 * @handler: the fault handler
1096 * @data: private data passed as argument to the handler
1097 *
1098 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001099 * fault event and data as argument. The handler should return 0 on success. If
1100 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1101 * complete the fault by calling iommu_page_response() with one of the following
1102 * response code:
1103 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1104 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1105 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1106 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001107 *
1108 * Return 0 if the fault handler was installed successfully, or an error.
1109 */
1110int iommu_register_device_fault_handler(struct device *dev,
1111 iommu_dev_fault_handler_t handler,
1112 void *data)
1113{
Joerg Roedel045a7042020-03-26 16:08:30 +01001114 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001115 int ret = 0;
1116
1117 if (!param)
1118 return -EINVAL;
1119
1120 mutex_lock(&param->lock);
1121 /* Only allow one fault handler registered for each device */
1122 if (param->fault_param) {
1123 ret = -EBUSY;
1124 goto done_unlock;
1125 }
1126
1127 get_device(dev);
1128 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1129 if (!param->fault_param) {
1130 put_device(dev);
1131 ret = -ENOMEM;
1132 goto done_unlock;
1133 }
1134 param->fault_param->handler = handler;
1135 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001136 mutex_init(&param->fault_param->lock);
1137 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001138
1139done_unlock:
1140 mutex_unlock(&param->lock);
1141
1142 return ret;
1143}
1144EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1145
1146/**
1147 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1148 * @dev: the device
1149 *
1150 * Remove the device fault handler installed with
1151 * iommu_register_device_fault_handler().
1152 *
1153 * Return 0 on success, or an error.
1154 */
1155int iommu_unregister_device_fault_handler(struct device *dev)
1156{
Joerg Roedel045a7042020-03-26 16:08:30 +01001157 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001158 int ret = 0;
1159
1160 if (!param)
1161 return -EINVAL;
1162
1163 mutex_lock(&param->lock);
1164
1165 if (!param->fault_param)
1166 goto unlock;
1167
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001168 /* we cannot unregister handler if there are pending faults */
1169 if (!list_empty(&param->fault_param->faults)) {
1170 ret = -EBUSY;
1171 goto unlock;
1172 }
1173
Jacob Pan0c830e62019-06-03 15:57:48 +01001174 kfree(param->fault_param);
1175 param->fault_param = NULL;
1176 put_device(dev);
1177unlock:
1178 mutex_unlock(&param->lock);
1179
1180 return ret;
1181}
1182EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1183
1184/**
1185 * iommu_report_device_fault() - Report fault event to device driver
1186 * @dev: the device
1187 * @evt: fault event data
1188 *
1189 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001190 * handler. When this function fails and the fault is recoverable, it is the
1191 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001192 *
1193 * Return 0 on success, or an error.
1194 */
1195int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1196{
Joerg Roedel045a7042020-03-26 16:08:30 +01001197 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001198 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001199 struct iommu_fault_param *fparam;
1200 int ret = 0;
1201
1202 if (!param || !evt)
1203 return -EINVAL;
1204
1205 /* we only report device fault if there is a handler registered */
1206 mutex_lock(&param->lock);
1207 fparam = param->fault_param;
1208 if (!fparam || !fparam->handler) {
1209 ret = -EINVAL;
1210 goto done_unlock;
1211 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001212
1213 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1214 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1215 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1216 GFP_KERNEL);
1217 if (!evt_pending) {
1218 ret = -ENOMEM;
1219 goto done_unlock;
1220 }
1221 mutex_lock(&fparam->lock);
1222 list_add_tail(&evt_pending->list, &fparam->faults);
1223 mutex_unlock(&fparam->lock);
1224 }
1225
Jacob Pan0c830e62019-06-03 15:57:48 +01001226 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001227 if (ret && evt_pending) {
1228 mutex_lock(&fparam->lock);
1229 list_del(&evt_pending->list);
1230 mutex_unlock(&fparam->lock);
1231 kfree(evt_pending);
1232 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001233done_unlock:
1234 mutex_unlock(&param->lock);
1235 return ret;
1236}
1237EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1238
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001239int iommu_page_response(struct device *dev,
1240 struct iommu_page_response *msg)
1241{
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001242 bool needs_pasid;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001243 int ret = -EINVAL;
1244 struct iommu_fault_event *evt;
1245 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001246 struct dev_iommu *param = dev->iommu;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001247 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001248 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1249
1250 if (!domain || !domain->ops->page_response)
1251 return -ENODEV;
1252
1253 if (!param || !param->fault_param)
1254 return -EINVAL;
1255
1256 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1257 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1258 return -EINVAL;
1259
1260 /* Only send response if there is a fault report pending */
1261 mutex_lock(&param->fault_param->lock);
1262 if (list_empty(&param->fault_param->faults)) {
1263 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1264 goto done_unlock;
1265 }
1266 /*
1267 * Check if we have a matching page request pending to respond,
1268 * otherwise return -EINVAL
1269 */
1270 list_for_each_entry(evt, &param->fault_param->faults, list) {
1271 prm = &evt->fault.prm;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001272 if (prm->grpid != msg->grpid)
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001273 continue;
1274
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001275 /*
1276 * If the PASID is required, the corresponding request is
1277 * matched using the group ID, the PASID valid bit and the PASID
1278 * value. Otherwise only the group ID matches request and
1279 * response.
1280 */
1281 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1282 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1283 continue;
1284
1285 if (!needs_pasid && has_pasid) {
1286 /* No big deal, just clear it. */
1287 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1288 msg->pasid = 0;
1289 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001290
1291 ret = domain->ops->page_response(dev, evt, msg);
1292 list_del(&evt->list);
1293 kfree(evt);
1294 break;
1295 }
1296
1297done_unlock:
1298 mutex_unlock(&param->fault_param->lock);
1299 return ret;
1300}
1301EXPORT_SYMBOL_GPL(iommu_page_response);
1302
Jacob Pan0c830e62019-06-03 15:57:48 +01001303/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001304 * iommu_group_id - Return ID for a group
1305 * @group: the group to ID
1306 *
1307 * Return the unique ID for the group matching the sysfs group number.
1308 */
1309int iommu_group_id(struct iommu_group *group)
1310{
1311 return group->id;
1312}
1313EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001314
Alex Williamsonf096c062014-09-19 10:03:06 -06001315static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1316 unsigned long *devfns);
1317
Alex Williamson104a1c12014-07-03 09:51:18 -06001318/*
1319 * To consider a PCI device isolated, we require ACS to support Source
1320 * Validation, Request Redirection, Completer Redirection, and Upstream
1321 * Forwarding. This effectively means that devices cannot spoof their
1322 * requester ID, requests and completions cannot be redirected, and all
1323 * transactions are forwarded upstream, even as it passes through a
1324 * bridge where the target device is downstream.
1325 */
1326#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1327
Alex Williamsonf096c062014-09-19 10:03:06 -06001328/*
1329 * For multifunction devices which are not isolated from each other, find
1330 * all the other non-isolated functions and look for existing groups. For
1331 * each function, we also need to look for aliases to or from other devices
1332 * that may already have a group.
1333 */
1334static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1335 unsigned long *devfns)
1336{
1337 struct pci_dev *tmp = NULL;
1338 struct iommu_group *group;
1339
1340 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1341 return NULL;
1342
1343 for_each_pci_dev(tmp) {
1344 if (tmp == pdev || tmp->bus != pdev->bus ||
1345 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1346 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1347 continue;
1348
1349 group = get_pci_alias_group(tmp, devfns);
1350 if (group) {
1351 pci_dev_put(tmp);
1352 return group;
1353 }
1354 }
1355
1356 return NULL;
1357}
1358
1359/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001360 * Look for aliases to or from the given device for existing groups. DMA
1361 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001362 * space is quite small (especially since we're really only looking at pcie
1363 * device, and therefore only expect multiple slots on the root complex or
1364 * downstream switch ports). It's conceivable though that a pair of
1365 * multifunction devices could have aliases between them that would cause a
1366 * loop. To prevent this, we use a bitmap to track where we've been.
1367 */
1368static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1369 unsigned long *devfns)
1370{
1371 struct pci_dev *tmp = NULL;
1372 struct iommu_group *group;
1373
1374 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1375 return NULL;
1376
1377 group = iommu_group_get(&pdev->dev);
1378 if (group)
1379 return group;
1380
1381 for_each_pci_dev(tmp) {
1382 if (tmp == pdev || tmp->bus != pdev->bus)
1383 continue;
1384
1385 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001386 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001387 group = get_pci_alias_group(tmp, devfns);
1388 if (group) {
1389 pci_dev_put(tmp);
1390 return group;
1391 }
1392
1393 group = get_pci_function_alias_group(tmp, devfns);
1394 if (group) {
1395 pci_dev_put(tmp);
1396 return group;
1397 }
1398 }
1399 }
1400
1401 return NULL;
1402}
1403
Alex Williamson104a1c12014-07-03 09:51:18 -06001404struct group_for_pci_data {
1405 struct pci_dev *pdev;
1406 struct iommu_group *group;
1407};
1408
1409/*
1410 * DMA alias iterator callback, return the last seen device. Stop and return
1411 * the IOMMU group if we find one along the way.
1412 */
1413static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1414{
1415 struct group_for_pci_data *data = opaque;
1416
1417 data->pdev = pdev;
1418 data->group = iommu_group_get(&pdev->dev);
1419
1420 return data->group != NULL;
1421}
1422
1423/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001424 * Generic device_group call-back function. It just allocates one
1425 * iommu-group per device.
1426 */
1427struct iommu_group *generic_device_group(struct device *dev)
1428{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001429 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001430}
Will Deacona7ba5c32019-12-19 12:03:37 +00001431EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001432
1433/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001434 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1435 * to find or create an IOMMU group for a device.
1436 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001437struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001438{
Joerg Roedel5e622922015-10-21 23:51:37 +02001439 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001440 struct group_for_pci_data data;
1441 struct pci_bus *bus;
1442 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001443 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001444
Joerg Roedel5e622922015-10-21 23:51:37 +02001445 if (WARN_ON(!dev_is_pci(dev)))
1446 return ERR_PTR(-EINVAL);
1447
Alex Williamson104a1c12014-07-03 09:51:18 -06001448 /*
1449 * Find the upstream DMA alias for the device. A device must not
1450 * be aliased due to topology in order to have its own IOMMU group.
1451 * If we find an alias along the way that already belongs to a
1452 * group, use it.
1453 */
1454 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1455 return data.group;
1456
1457 pdev = data.pdev;
1458
1459 /*
1460 * Continue upstream from the point of minimum IOMMU granularity
1461 * due to aliases to the point where devices are protected from
1462 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1463 * group, use it.
1464 */
1465 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1466 if (!bus->self)
1467 continue;
1468
1469 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1470 break;
1471
1472 pdev = bus->self;
1473
1474 group = iommu_group_get(&pdev->dev);
1475 if (group)
1476 return group;
1477 }
1478
1479 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001480 * Look for existing groups on device aliases. If we alias another
1481 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001482 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001483 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1484 if (group)
1485 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001486
1487 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001488 * Look for existing groups on non-isolated functions on the same
1489 * slot and aliases of those funcions, if any. No need to clear
1490 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001491 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001492 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1493 if (group)
1494 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001495
1496 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001497 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001498}
Will Deacona7ba5c32019-12-19 12:03:37 +00001499EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001500
Nipun Guptaeab03e22018-09-10 19:19:18 +05301501/* Get the IOMMU group for device on fsl-mc bus */
1502struct iommu_group *fsl_mc_device_group(struct device *dev)
1503{
1504 struct device *cont_dev = fsl_mc_cont_dev(dev);
1505 struct iommu_group *group;
1506
1507 group = iommu_group_get(cont_dev);
1508 if (!group)
1509 group = iommu_group_alloc();
1510 return group;
1511}
Will Deacona7ba5c32019-12-19 12:03:37 +00001512EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301513
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001514static int iommu_get_def_domain_type(struct device *dev)
1515{
1516 const struct iommu_ops *ops = dev->bus->iommu_ops;
Lu Baolu28b41e22020-11-24 21:06:01 +08001517
1518 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
1519 return IOMMU_DOMAIN_DMA;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001520
1521 if (ops->def_domain_type)
Lu Baolu28b41e22020-11-24 21:06:01 +08001522 return ops->def_domain_type(dev);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001523
Lu Baolu28b41e22020-11-24 21:06:01 +08001524 return 0;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001525}
1526
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001527static int iommu_group_alloc_default_domain(struct bus_type *bus,
1528 struct iommu_group *group,
1529 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001530{
1531 struct iommu_domain *dom;
1532
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001533 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001534 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001535 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1536 if (dom)
1537 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1538 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001539 }
1540
1541 if (!dom)
1542 return -ENOMEM;
1543
1544 group->default_domain = dom;
1545 if (!group->domain)
1546 group->domain = dom;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001547 return 0;
1548}
1549
Joerg Roedel79659190e2020-05-25 15:01:22 +02001550static int iommu_alloc_default_domain(struct iommu_group *group,
1551 struct device *dev)
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001552{
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001553 unsigned int type;
1554
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001555 if (group->default_domain)
1556 return 0;
1557
Lu Baolu28b41e22020-11-24 21:06:01 +08001558 type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001559
1560 return iommu_group_alloc_default_domain(dev->bus, group, type);
1561}
1562
Alex Williamson104a1c12014-07-03 09:51:18 -06001563/**
1564 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1565 * @dev: target device
1566 *
1567 * This function is intended to be called by IOMMU drivers and extended to
1568 * support common, bus-defined algorithms when determining or creating the
1569 * IOMMU group for a device. On success, the caller will hold a reference
1570 * to the returned IOMMU group, which will already include the provided
1571 * device. The reference should be released with iommu_group_put().
1572 */
Joerg Roedel1b032ec2020-04-29 15:37:12 +02001573static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001574{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001575 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001576 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001577 int ret;
1578
1579 group = iommu_group_get(dev);
1580 if (group)
1581 return group;
1582
Robin Murphy05f803002017-07-21 13:12:38 +01001583 if (!ops)
1584 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001585
Robin Murphy05f803002017-07-21 13:12:38 +01001586 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001587 if (WARN_ON_ONCE(group == NULL))
1588 return ERR_PTR(-EINVAL);
1589
Alex Williamson104a1c12014-07-03 09:51:18 -06001590 if (IS_ERR(group))
1591 return group;
1592
1593 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001594 if (ret)
1595 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001596
1597 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001598
1599out_put_group:
1600 iommu_group_put(group);
1601
1602 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001603}
1604
Joerg Roedel6827ca82015-05-28 18:41:35 +02001605struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1606{
1607 return group->default_domain;
1608}
1609
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001610static int probe_iommu_group(struct device *dev, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001611{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001612 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001613 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001614 int ret;
Joerg Roedel38667f12015-06-29 10:16:08 +02001615
Joerg Roedel5012c392020-04-29 15:36:51 +02001616 /* Device is probed already if in a group */
1617 group = iommu_group_get(dev);
1618 if (group) {
1619 iommu_group_put(group);
1620 return 0;
1621 }
1622
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001623 ret = __iommu_probe_device(dev, group_list);
Joerg Roedel38667f12015-06-29 10:16:08 +02001624 if (ret == -ENODEV)
1625 ret = 0;
1626
1627 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001628}
1629
Joerg Roedel8da30142015-05-28 18:41:27 +02001630static int remove_iommu_group(struct device *dev, void *data)
1631{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001632 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001633
1634 return 0;
1635}
1636
Alex Williamsond72e31c2012-05-30 14:18:53 -06001637static int iommu_bus_notifier(struct notifier_block *nb,
1638 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001639{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001640 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001641 struct device *dev = data;
1642 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001643
Alex Williamsond72e31c2012-05-30 14:18:53 -06001644 /*
1645 * ADD/DEL call into iommu driver ops if provided, which may
1646 * result in ADD/DEL notifiers to group->notifier
1647 */
1648 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001649 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001650
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001651 ret = iommu_probe_device(dev);
1652 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001653 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001654 iommu_release_device(dev);
1655 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001656 }
Alex Williamson14604322011-10-21 15:56:05 -04001657
Alex Williamsond72e31c2012-05-30 14:18:53 -06001658 /*
1659 * Remaining BUS_NOTIFYs get filtered and republished to the
1660 * group, if anyone is listening
1661 */
1662 group = iommu_group_get(dev);
1663 if (!group)
1664 return 0;
1665
1666 switch (action) {
1667 case BUS_NOTIFY_BIND_DRIVER:
1668 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1669 break;
1670 case BUS_NOTIFY_BOUND_DRIVER:
1671 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1672 break;
1673 case BUS_NOTIFY_UNBIND_DRIVER:
1674 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1675 break;
1676 case BUS_NOTIFY_UNBOUND_DRIVER:
1677 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1678 break;
1679 }
1680
1681 if (group_action)
1682 blocking_notifier_call_chain(&group->notifier,
1683 group_action, dev);
1684
1685 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001686 return 0;
1687}
1688
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001689struct __group_domain_type {
1690 struct device *dev;
1691 unsigned int type;
1692};
1693
1694static int probe_get_default_domain_type(struct device *dev, void *data)
1695{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001696 struct __group_domain_type *gtype = data;
Lu Baolu28b41e22020-11-24 21:06:01 +08001697 unsigned int type = iommu_get_def_domain_type(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001698
1699 if (type) {
1700 if (gtype->type && gtype->type != type) {
1701 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1702 iommu_domain_type_str(type),
1703 dev_name(gtype->dev),
1704 iommu_domain_type_str(gtype->type));
1705 gtype->type = 0;
1706 }
1707
1708 if (!gtype->dev) {
1709 gtype->dev = dev;
1710 gtype->type = type;
1711 }
1712 }
1713
1714 return 0;
1715}
1716
1717static void probe_alloc_default_domain(struct bus_type *bus,
1718 struct iommu_group *group)
1719{
1720 struct __group_domain_type gtype;
1721
1722 memset(&gtype, 0, sizeof(gtype));
1723
1724 /* Ask for default domain requirements of all devices in the group */
1725 __iommu_group_for_each_dev(group, &gtype,
1726 probe_get_default_domain_type);
1727
1728 if (!gtype.type)
1729 gtype.type = iommu_def_domain_type;
1730
1731 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001732
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001733}
1734
1735static int iommu_group_do_dma_attach(struct device *dev, void *data)
1736{
1737 struct iommu_domain *domain = data;
Joerg Roedel431275a2020-06-04 11:19:44 +02001738 int ret = 0;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001739
Joerg Roedel431275a2020-06-04 11:19:44 +02001740 if (!iommu_is_attach_deferred(domain, dev))
1741 ret = __iommu_attach_device(domain, dev);
1742
1743 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001744}
1745
1746static int __iommu_group_dma_attach(struct iommu_group *group)
1747{
1748 return __iommu_group_for_each_dev(group, group->default_domain,
1749 iommu_group_do_dma_attach);
1750}
1751
Joerg Roedel70b81702020-05-19 15:28:24 +02001752static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1753{
1754 struct iommu_domain *domain = data;
1755
1756 if (domain->ops->probe_finalize)
1757 domain->ops->probe_finalize(dev);
1758
1759 return 0;
1760}
1761
1762static void __iommu_group_dma_finalize(struct iommu_group *group)
1763{
1764 __iommu_group_for_each_dev(group, group->default_domain,
1765 iommu_group_do_probe_finalize);
1766}
Joerg Roedelcc69fc42020-06-02 10:32:04 +02001767
Joerg Roedelce574c22020-04-29 15:36:50 +02001768static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1769{
1770 struct iommu_group *group = data;
1771
1772 iommu_create_device_direct_mappings(group, dev);
1773
1774 return 0;
1775}
1776
1777static int iommu_group_create_direct_mappings(struct iommu_group *group)
1778{
1779 return __iommu_group_for_each_dev(group, group,
1780 iommu_do_create_direct_mappings);
1781}
1782
Joerg Roedel5012c392020-04-29 15:36:51 +02001783int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001784{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001785 struct iommu_group *group, *next;
1786 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001787 int ret;
1788
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001789 /*
1790 * This code-path does not allocate the default domain when
1791 * creating the iommu group, so do it after the groups are
1792 * created.
1793 */
1794 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1795 if (ret)
1796 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001797
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001798 list_for_each_entry_safe(group, next, &group_list, entry) {
1799 /* Remove item from the list */
1800 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001801
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001802 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001803
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001804 /* Try to allocate default domain */
1805 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001806
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001807 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001808 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001809 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001810 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001811
1812 iommu_group_create_direct_mappings(group);
1813
1814 ret = __iommu_group_dma_attach(group);
1815
1816 mutex_unlock(&group->mutex);
1817
1818 if (ret)
1819 break;
Joerg Roedel70b81702020-05-19 15:28:24 +02001820
1821 __iommu_group_dma_finalize(group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001822 }
1823
1824 return ret;
1825}
1826
Mark Salterfb3e3062014-09-21 13:58:24 -04001827static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001828{
Mark Salterfb3e3062014-09-21 13:58:24 -04001829 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001830 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001831
Mark Salterfb3e3062014-09-21 13:58:24 -04001832 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1833 if (!nb)
1834 return -ENOMEM;
1835
1836 nb->notifier_call = iommu_bus_notifier;
1837
1838 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001839 if (err)
1840 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001841
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001842 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001843 if (err)
1844 goto out_err;
1845
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001846
1847 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001848
1849out_err:
1850 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001851 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001852 bus_unregister_notifier(bus, nb);
1853
1854out_free:
1855 kfree(nb);
1856
1857 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001858}
1859
Joerg Roedelff217762011-08-26 16:48:26 +02001860/**
1861 * bus_set_iommu - set iommu-callbacks for the bus
1862 * @bus: bus.
1863 * @ops: the callbacks provided by the iommu-driver
1864 *
1865 * This function is called by an iommu driver to set the iommu methods
1866 * used for a particular bus. Drivers for devices on that bus can use
1867 * the iommu-api after these ops are registered.
1868 * This special function is needed because IOMMUs are usually devices on
1869 * the bus itself, so the iommu drivers are not initialized when the bus
1870 * is set up. With this function the iommu-driver can set the iommu-ops
1871 * afterwards.
1872 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001873int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001874{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001875 int err;
1876
Will Deacon4312cf72019-12-19 12:03:43 +00001877 if (ops == NULL) {
1878 bus->iommu_ops = NULL;
1879 return 0;
1880 }
1881
Joerg Roedelff217762011-08-26 16:48:26 +02001882 if (bus->iommu_ops != NULL)
1883 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001884
Joerg Roedelff217762011-08-26 16:48:26 +02001885 bus->iommu_ops = ops;
1886
1887 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001888 err = iommu_bus_init(bus, ops);
1889 if (err)
1890 bus->iommu_ops = NULL;
1891
1892 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001893}
Joerg Roedelff217762011-08-26 16:48:26 +02001894EXPORT_SYMBOL_GPL(bus_set_iommu);
1895
Joerg Roedela1b60c12011-09-06 18:46:34 +02001896bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001897{
Joerg Roedel94441c32011-09-06 18:58:54 +02001898 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001899}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001900EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001901
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001902bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1903{
1904 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1905 return false;
1906
1907 return bus->iommu_ops->capable(cap);
1908}
1909EXPORT_SYMBOL_GPL(iommu_capable);
1910
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001911/**
1912 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1913 * @domain: iommu domain
1914 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001915 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001916 *
1917 * This function should be used by IOMMU users which want to be notified
1918 * whenever an IOMMU fault happens.
1919 *
1920 * The fault handler itself should return 0 on success, and an appropriate
1921 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001922 */
1923void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001924 iommu_fault_handler_t handler,
1925 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001926{
1927 BUG_ON(!domain);
1928
1929 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001930 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001931}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001932EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001933
Joerg Roedel53723dc2015-05-28 18:41:29 +02001934static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1935 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001936{
1937 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001938
Joerg Roedel94441c32011-09-06 18:58:54 +02001939 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001940 return NULL;
1941
Joerg Roedel53723dc2015-05-28 18:41:29 +02001942 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001943 if (!domain)
1944 return NULL;
1945
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001946 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001947 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001948 /* Assume all sizes by default; the driver may override this later */
1949 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001950
Robin Murphy46983fc2021-08-11 13:21:15 +01001951 /* Temporarily avoid -EEXIST while drivers still get their own cookies */
Robin Murphybf3aed462021-08-11 13:21:30 +01001952 if (iommu_is_dma_domain(domain) && !domain->iova_cookie && iommu_get_dma_cookie(domain)) {
Robin Murphy46983fc2021-08-11 13:21:15 +01001953 iommu_domain_free(domain);
1954 domain = NULL;
1955 }
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001956 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001957}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001958
Joerg Roedel53723dc2015-05-28 18:41:29 +02001959struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1960{
1961 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001962}
1963EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1964
1965void iommu_domain_free(struct iommu_domain *domain)
1966{
Robin Murphy46983fc2021-08-11 13:21:15 +01001967 iommu_put_dma_cookie(domain);
Joerg Roedel89be34a2015-03-26 13:43:19 +01001968 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001969}
1970EXPORT_SYMBOL_GPL(iommu_domain_free);
1971
Joerg Roedel426a2732015-05-28 18:41:30 +02001972static int __iommu_attach_device(struct iommu_domain *domain,
1973 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001974{
Shuah Khanb54db772013-08-15 11:59:26 -06001975 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001976
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001977 if (unlikely(domain->ops->attach_dev == NULL))
1978 return -ENODEV;
1979
Shuah Khanb54db772013-08-15 11:59:26 -06001980 ret = domain->ops->attach_dev(domain, dev);
1981 if (!ret)
1982 trace_attach_device_to_domain(dev);
1983 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001984}
Joerg Roedel426a2732015-05-28 18:41:30 +02001985
1986int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1987{
1988 struct iommu_group *group;
1989 int ret;
1990
1991 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001992 if (!group)
1993 return -ENODEV;
1994
Joerg Roedel426a2732015-05-28 18:41:30 +02001995 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001996 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001997 * change while we are attaching
1998 */
1999 mutex_lock(&group->mutex);
2000 ret = -EINVAL;
2001 if (iommu_group_device_count(group) != 1)
2002 goto out_unlock;
2003
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002004 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002005
2006out_unlock:
2007 mutex_unlock(&group->mutex);
2008 iommu_group_put(group);
2009
2010 return ret;
2011}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002012EXPORT_SYMBOL_GPL(iommu_attach_device);
2013
Lianbo Jiang3ab65722021-01-26 19:53:37 +08002014int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2015{
2016 const struct iommu_ops *ops = domain->ops;
2017
2018 if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
2019 return __iommu_attach_device(domain, dev);
2020
2021 return 0;
2022}
2023
Jacob Pand9057382020-09-25 09:32:46 -07002024/*
2025 * Check flags and other user provided data for valid combinations. We also
2026 * make sure no reserved fields or unused flags are set. This is to ensure
2027 * not breaking userspace in the future when these fields or flags are used.
2028 */
2029static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)
Yi L Liu4c7c1712019-10-02 12:42:40 -07002030{
Jacob Pand9057382020-09-25 09:32:46 -07002031 u32 mask;
2032 int i;
2033
2034 if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)
2035 return -EINVAL;
2036
2037 mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;
2038 if (info->cache & ~mask)
2039 return -EINVAL;
2040
2041 if (info->granularity >= IOMMU_INV_GRANU_NR)
2042 return -EINVAL;
2043
2044 switch (info->granularity) {
2045 case IOMMU_INV_GRANU_ADDR:
2046 if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)
2047 return -EINVAL;
2048
2049 mask = IOMMU_INV_ADDR_FLAGS_PASID |
2050 IOMMU_INV_ADDR_FLAGS_ARCHID |
2051 IOMMU_INV_ADDR_FLAGS_LEAF;
2052
2053 if (info->granu.addr_info.flags & ~mask)
2054 return -EINVAL;
2055 break;
2056 case IOMMU_INV_GRANU_PASID:
2057 mask = IOMMU_INV_PASID_FLAGS_PASID |
2058 IOMMU_INV_PASID_FLAGS_ARCHID;
2059 if (info->granu.pasid_info.flags & ~mask)
2060 return -EINVAL;
2061
2062 break;
2063 case IOMMU_INV_GRANU_DOMAIN:
2064 if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)
2065 return -EINVAL;
2066 break;
2067 default:
2068 return -EINVAL;
2069 }
2070
2071 /* Check reserved padding fields */
2072 for (i = 0; i < sizeof(info->padding); i++) {
2073 if (info->padding[i])
2074 return -EINVAL;
2075 }
2076
2077 return 0;
2078}
2079
2080int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,
2081 void __user *uinfo)
2082{
2083 struct iommu_cache_invalidate_info inv_info = { 0 };
2084 u32 minsz;
2085 int ret;
2086
Yi L Liu4c7c1712019-10-02 12:42:40 -07002087 if (unlikely(!domain->ops->cache_invalidate))
2088 return -ENODEV;
2089
Jacob Pand9057382020-09-25 09:32:46 -07002090 /*
2091 * No new spaces can be added before the variable sized union, the
2092 * minimum size is the offset to the union.
2093 */
2094 minsz = offsetof(struct iommu_cache_invalidate_info, granu);
Yi L Liu4c7c1712019-10-02 12:42:40 -07002095
Jacob Pand9057382020-09-25 09:32:46 -07002096 /* Copy minsz from user to get flags and argsz */
2097 if (copy_from_user(&inv_info, uinfo, minsz))
2098 return -EFAULT;
2099
2100 /* Fields before the variable size union are mandatory */
2101 if (inv_info.argsz < minsz)
2102 return -EINVAL;
2103
2104 /* PASID and address granu require additional info beyond minsz */
2105 if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&
2106 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))
2107 return -EINVAL;
2108
2109 if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&
2110 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))
2111 return -EINVAL;
2112
2113 /*
2114 * User might be using a newer UAPI header which has a larger data
2115 * size, we shall support the existing flags within the current
2116 * size. Copy the remaining user data _after_ minsz but not more
2117 * than the current kernel supported size.
2118 */
2119 if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,
2120 min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))
2121 return -EFAULT;
2122
2123 /* Now the argsz is validated, check the content */
2124 ret = iommu_check_cache_invl_data(&inv_info);
2125 if (ret)
2126 return ret;
2127
2128 return domain->ops->cache_invalidate(domain, dev, &inv_info);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002129}
Jacob Pan23cc3492020-09-25 09:32:45 -07002130EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002131
Jacob Pand9057382020-09-25 09:32:46 -07002132static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)
Jacob Pan808be0a2019-10-02 12:42:43 -07002133{
Dan Carpenter4dd6ce42020-11-03 13:16:23 +03002134 u64 mask;
Jacob Pand9057382020-09-25 09:32:46 -07002135 int i;
2136
2137 if (data->version != IOMMU_GPASID_BIND_VERSION_1)
2138 return -EINVAL;
2139
2140 /* Check the range of supported formats */
2141 if (data->format >= IOMMU_PASID_FORMAT_LAST)
2142 return -EINVAL;
2143
2144 /* Check all flags */
2145 mask = IOMMU_SVA_GPASID_VAL;
2146 if (data->flags & ~mask)
2147 return -EINVAL;
2148
2149 /* Check reserved padding fields */
2150 for (i = 0; i < sizeof(data->padding); i++) {
2151 if (data->padding[i])
2152 return -EINVAL;
2153 }
2154
2155 return 0;
2156}
2157
2158static int iommu_sva_prepare_bind_data(void __user *udata,
2159 struct iommu_gpasid_bind_data *data)
2160{
2161 u32 minsz;
2162
2163 /*
2164 * No new spaces can be added before the variable sized union, the
2165 * minimum size is the offset to the union.
2166 */
2167 minsz = offsetof(struct iommu_gpasid_bind_data, vendor);
2168
2169 /* Copy minsz from user to get flags and argsz */
2170 if (copy_from_user(data, udata, minsz))
2171 return -EFAULT;
2172
2173 /* Fields before the variable size union are mandatory */
2174 if (data->argsz < minsz)
2175 return -EINVAL;
2176 /*
2177 * User might be using a newer UAPI header, we shall let IOMMU vendor
2178 * driver decide on what size it needs. Since the guest PASID bind data
2179 * can be vendor specific, larger argsz could be the result of extension
2180 * for one vendor but it should not affect another vendor.
2181 * Copy the remaining user data _after_ minsz
2182 */
2183 if (copy_from_user((void *)data + minsz, udata + minsz,
2184 min_t(u32, data->argsz, sizeof(*data)) - minsz))
2185 return -EFAULT;
2186
2187 return iommu_check_bind_data(data);
2188}
2189
2190int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
2191 void __user *udata)
2192{
2193 struct iommu_gpasid_bind_data data = { 0 };
2194 int ret;
2195
Jacob Pan808be0a2019-10-02 12:42:43 -07002196 if (unlikely(!domain->ops->sva_bind_gpasid))
2197 return -ENODEV;
2198
Jacob Pand9057382020-09-25 09:32:46 -07002199 ret = iommu_sva_prepare_bind_data(udata, &data);
2200 if (ret)
2201 return ret;
2202
2203 return domain->ops->sva_bind_gpasid(domain, dev, &data);
Jacob Pan808be0a2019-10-02 12:42:43 -07002204}
Jacob Pan23cc3492020-09-25 09:32:45 -07002205EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
Jacob Pan808be0a2019-10-02 12:42:43 -07002206
2207int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2208 ioasid_t pasid)
2209{
2210 if (unlikely(!domain->ops->sva_unbind_gpasid))
2211 return -ENODEV;
2212
2213 return domain->ops->sva_unbind_gpasid(dev, pasid);
2214}
2215EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
2216
Jacob Pand9057382020-09-25 09:32:46 -07002217int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2218 void __user *udata)
2219{
2220 struct iommu_gpasid_bind_data data = { 0 };
2221 int ret;
2222
2223 if (unlikely(!domain->ops->sva_bind_gpasid))
2224 return -ENODEV;
2225
2226 ret = iommu_sva_prepare_bind_data(udata, &data);
2227 if (ret)
2228 return ret;
2229
2230 return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
2231}
Jacob Pan23cc3492020-09-25 09:32:45 -07002232EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002233
2234static void __iommu_detach_device(struct iommu_domain *domain,
2235 struct device *dev)
2236{
Joerg Roedelbd421262020-05-19 15:03:40 +02002237 if (iommu_is_attach_deferred(domain, dev))
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002238 return;
2239
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002240 if (unlikely(domain->ops->detach_dev == NULL))
2241 return;
2242
2243 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06002244 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002245}
Joerg Roedel426a2732015-05-28 18:41:30 +02002246
2247void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2248{
2249 struct iommu_group *group;
2250
2251 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07002252 if (!group)
2253 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02002254
2255 mutex_lock(&group->mutex);
2256 if (iommu_group_device_count(group) != 1) {
2257 WARN_ON(1);
2258 goto out_unlock;
2259 }
2260
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002261 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002262
2263out_unlock:
2264 mutex_unlock(&group->mutex);
2265 iommu_group_put(group);
2266}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002267EXPORT_SYMBOL_GPL(iommu_detach_device);
2268
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002269struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2270{
2271 struct iommu_domain *domain;
2272 struct iommu_group *group;
2273
2274 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002275 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002276 return NULL;
2277
2278 domain = group->domain;
2279
2280 iommu_group_put(group);
2281
2282 return domain;
2283}
2284EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2285
Alex Williamsond72e31c2012-05-30 14:18:53 -06002286/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002287 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2288 * guarantees that the group and its default domain are valid and correct.
2289 */
2290struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2291{
2292 return dev->iommu_group->default_domain;
2293}
2294
2295/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002296 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002297 * the IOMMU API works on domains and devices. Bridge that gap by
2298 * iterating over the devices in a group. Ideally we'd have a single
2299 * device which represents the requestor ID of the group, but we also
2300 * allow IOMMU drivers to create policy defined minimum sets, where
2301 * the physical hardware may be able to distiguish members, but we
2302 * wish to group them at a higher level (ex. untrusted multi-function
2303 * PCI devices). Thus we attach each device.
2304 */
2305static int iommu_group_do_attach_device(struct device *dev, void *data)
2306{
2307 struct iommu_domain *domain = data;
2308
Joerg Roedel426a2732015-05-28 18:41:30 +02002309 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002310}
2311
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002312static int __iommu_attach_group(struct iommu_domain *domain,
2313 struct iommu_group *group)
2314{
2315 int ret;
2316
2317 if (group->default_domain && group->domain != group->default_domain)
2318 return -EBUSY;
2319
2320 ret = __iommu_group_for_each_dev(group, domain,
2321 iommu_group_do_attach_device);
2322 if (ret == 0)
2323 group->domain = domain;
2324
2325 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002326}
2327
2328int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2329{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002330 int ret;
2331
2332 mutex_lock(&group->mutex);
2333 ret = __iommu_attach_group(domain, group);
2334 mutex_unlock(&group->mutex);
2335
2336 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002337}
2338EXPORT_SYMBOL_GPL(iommu_attach_group);
2339
2340static int iommu_group_do_detach_device(struct device *dev, void *data)
2341{
2342 struct iommu_domain *domain = data;
2343
Joerg Roedel426a2732015-05-28 18:41:30 +02002344 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002345
2346 return 0;
2347}
2348
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002349static void __iommu_detach_group(struct iommu_domain *domain,
2350 struct iommu_group *group)
2351{
2352 int ret;
2353
2354 if (!group->default_domain) {
2355 __iommu_group_for_each_dev(group, domain,
2356 iommu_group_do_detach_device);
2357 group->domain = NULL;
2358 return;
2359 }
2360
2361 if (group->domain == group->default_domain)
2362 return;
2363
2364 /* Detach by re-attaching to the default domain */
2365 ret = __iommu_group_for_each_dev(group, group->default_domain,
2366 iommu_group_do_attach_device);
2367 if (ret != 0)
2368 WARN_ON(1);
2369 else
2370 group->domain = group->default_domain;
2371}
2372
Alex Williamsond72e31c2012-05-30 14:18:53 -06002373void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2374{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002375 mutex_lock(&group->mutex);
2376 __iommu_detach_group(domain, group);
2377 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002378}
2379EXPORT_SYMBOL_GPL(iommu_detach_group);
2380
Varun Sethibb5547a2013-03-29 01:23:58 +05302381phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002382{
Robin Murphy13b6eb62021-07-15 14:04:24 +01002383 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2384 return iova;
2385
2386 if (domain->type == IOMMU_DOMAIN_BLOCKED)
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002387 return 0;
2388
2389 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002390}
2391EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002392
Will Deacon89d5b962021-06-16 06:38:47 -07002393static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
Will Deaconb1d99dc2021-06-16 06:38:48 -07002394 phys_addr_t paddr, size_t size, size_t *count)
Alex Williamsonbd139692013-06-17 19:57:34 -06002395{
Will Deaconb1d99dc2021-06-16 06:38:48 -07002396 unsigned int pgsize_idx, pgsize_idx_next;
Will Deacone7d6fff2021-06-16 06:38:46 -07002397 unsigned long pgsizes;
Will Deaconb1d99dc2021-06-16 06:38:48 -07002398 size_t offset, pgsize, pgsize_next;
Will Deacon89d5b962021-06-16 06:38:47 -07002399 unsigned long addr_merge = paddr | iova;
Alex Williamsonbd139692013-06-17 19:57:34 -06002400
Will Deacone7d6fff2021-06-16 06:38:46 -07002401 /* Page sizes supported by the hardware and small enough for @size */
2402 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
Alex Williamsonbd139692013-06-17 19:57:34 -06002403
Will Deacone7d6fff2021-06-16 06:38:46 -07002404 /* Constrain the page sizes further based on the maximum alignment */
2405 if (likely(addr_merge))
2406 pgsizes &= GENMASK(__ffs(addr_merge), 0);
Alex Williamsonbd139692013-06-17 19:57:34 -06002407
Will Deacone7d6fff2021-06-16 06:38:46 -07002408 /* Make sure we have at least one suitable page size */
2409 BUG_ON(!pgsizes);
Alex Williamsonbd139692013-06-17 19:57:34 -06002410
Will Deacone7d6fff2021-06-16 06:38:46 -07002411 /* Pick the biggest page size remaining */
2412 pgsize_idx = __fls(pgsizes);
2413 pgsize = BIT(pgsize_idx);
Will Deaconb1d99dc2021-06-16 06:38:48 -07002414 if (!count)
2415 return pgsize;
Alex Williamsonbd139692013-06-17 19:57:34 -06002416
Will Deaconb1d99dc2021-06-16 06:38:48 -07002417 /* Find the next biggest support page size, if it exists */
2418 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2419 if (!pgsizes)
2420 goto out_set_count;
2421
2422 pgsize_idx_next = __ffs(pgsizes);
2423 pgsize_next = BIT(pgsize_idx_next);
2424
2425 /*
2426 * There's no point trying a bigger page size unless the virtual
2427 * and physical addresses are similarly offset within the larger page.
2428 */
2429 if ((iova ^ paddr) & (pgsize_next - 1))
2430 goto out_set_count;
2431
2432 /* Calculate the offset to the next page size alignment boundary */
2433 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2434
2435 /*
2436 * If size is big enough to accommodate the larger page, reduce
2437 * the number of smaller pages.
2438 */
2439 if (offset + pgsize_next <= size)
2440 size = offset;
2441
2442out_set_count:
2443 *count = size >> pgsize_idx;
Alex Williamsonbd139692013-06-17 19:57:34 -06002444 return pgsize;
2445}
2446
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002447static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova,
2448 phys_addr_t paddr, size_t size, int prot,
2449 gfp_t gfp, size_t *mapped)
2450{
2451 const struct iommu_ops *ops = domain->ops;
2452 size_t pgsize, count;
2453 int ret;
2454
2455 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2456
2457 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2458 iova, &paddr, pgsize, count);
2459
2460 if (ops->map_pages) {
2461 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2462 gfp, mapped);
2463 } else {
2464 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2465 *mapped = ret ? 0 : pgsize;
2466 }
2467
2468 return ret;
2469}
2470
Wei Yongjun1b0b2a82020-07-13 22:25:42 +08002471static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2472 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002473{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002474 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002475 unsigned long orig_iova = iova;
2476 unsigned int min_pagesz;
2477 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002478 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002479 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002480
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002481 if (unlikely(!(ops->map || ops->map_pages) ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002482 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002483 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002484
Joerg Roedela10315e2015-03-26 13:43:06 +01002485 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2486 return -EINVAL;
2487
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002488 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002489 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002490
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002491 /*
2492 * both the virtual address and the physical one, as well as
2493 * the size of the mapping, must be aligned (at least) to the
2494 * size of the smallest page supported by the hardware
2495 */
2496 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002497 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002498 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002499 return -EINVAL;
2500 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002501
Fabio Estevamabedb042013-08-22 10:25:42 -03002502 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002503
2504 while (size) {
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002505 size_t mapped = 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002506
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002507 ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp,
2508 &mapped);
2509 /*
2510 * Some pages may have been mapped, even if an error occurred,
2511 * so we should account for those so they can be unmapped.
2512 */
2513 size -= mapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002514
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002515 if (ret)
2516 break;
2517
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002518 iova += mapped;
2519 paddr += mapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002520 }
2521
2522 /* unroll mapping in case something went wrong */
2523 if (ret)
2524 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002525 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002526 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002527
2528 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002529}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002530
Yong Wud8c1df02021-01-07 20:29:03 +08002531static int _iommu_map(struct iommu_domain *domain, unsigned long iova,
2532 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2533{
2534 const struct iommu_ops *ops = domain->ops;
2535 int ret;
2536
Douglas Andersonb8437a32021-02-01 17:06:23 -08002537 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
Yong Wud8c1df02021-01-07 20:29:03 +08002538 if (ret == 0 && ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002539 ops->iotlb_sync_map(domain, iova, size);
Yong Wud8c1df02021-01-07 20:29:03 +08002540
2541 return ret;
2542}
2543
Tom Murphy781ca2d2019-09-08 09:56:38 -07002544int iommu_map(struct iommu_domain *domain, unsigned long iova,
2545 phys_addr_t paddr, size_t size, int prot)
2546{
2547 might_sleep();
Yong Wud8c1df02021-01-07 20:29:03 +08002548 return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002549}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002550EXPORT_SYMBOL_GPL(iommu_map);
2551
Tom Murphy781ca2d2019-09-08 09:56:38 -07002552int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2553 phys_addr_t paddr, size_t size, int prot)
2554{
Yong Wud8c1df02021-01-07 20:29:03 +08002555 return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002556}
2557EXPORT_SYMBOL_GPL(iommu_map_atomic);
2558
Will Deaconb1d99dc2021-06-16 06:38:48 -07002559static size_t __iommu_unmap_pages(struct iommu_domain *domain,
2560 unsigned long iova, size_t size,
2561 struct iommu_iotlb_gather *iotlb_gather)
2562{
2563 const struct iommu_ops *ops = domain->ops;
2564 size_t pgsize, count;
2565
2566 pgsize = iommu_pgsize(domain, iova, iova, size, &count);
2567 return ops->unmap_pages ?
2568 ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) :
2569 ops->unmap(domain, iova, pgsize, iotlb_gather);
2570}
2571
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002572static size_t __iommu_unmap(struct iommu_domain *domain,
2573 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002574 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002575{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002576 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002577 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002578 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002579 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002580
Will Deaconb1d99dc2021-06-16 06:38:48 -07002581 if (unlikely(!(ops->unmap || ops->unmap_pages) ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002582 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002583 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002584
Joerg Roedela10315e2015-03-26 13:43:06 +01002585 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002586 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002587
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002588 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002589 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002590
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002591 /*
2592 * The virtual address, as well as the size of the mapping, must be
2593 * aligned (at least) to the size of the smallest page supported
2594 * by the hardware
2595 */
2596 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002597 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2598 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002599 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002600 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002601
Joe Perches6197ca82013-06-23 12:29:04 -07002602 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002603
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002604 /*
2605 * Keep iterating until we either unmap 'size' bytes (or more)
2606 * or we hit an area that isn't mapped.
2607 */
2608 while (unmapped < size) {
Will Deaconb1d99dc2021-06-16 06:38:48 -07002609 unmapped_page = __iommu_unmap_pages(domain, iova,
2610 size - unmapped,
2611 iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002612 if (!unmapped_page)
2613 break;
2614
Joe Perches6197ca82013-06-23 12:29:04 -07002615 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2616 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002617
2618 iova += unmapped_page;
2619 unmapped += unmapped_page;
2620 }
2621
Shuah Khandb8614d2015-01-16 20:53:17 -07002622 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002623 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002624}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002625
2626size_t iommu_unmap(struct iommu_domain *domain,
2627 unsigned long iova, size_t size)
2628{
Will Deacona7d20dc2019-07-02 16:43:48 +01002629 struct iommu_iotlb_gather iotlb_gather;
2630 size_t ret;
2631
2632 iommu_iotlb_gather_init(&iotlb_gather);
2633 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
Tom Murphyaae4c8e2020-08-17 22:00:49 +01002634 iommu_iotlb_sync(domain, &iotlb_gather);
Will Deacona7d20dc2019-07-02 16:43:48 +01002635
2636 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002637}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002638EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002639
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002640size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002641 unsigned long iova, size_t size,
2642 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002643{
Will Deacona7d20dc2019-07-02 16:43:48 +01002644 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002645}
2646EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2647
Baolin Wang99302642020-06-12 11:39:54 +08002648static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2649 struct scatterlist *sg, unsigned int nents, int prot,
2650 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002651{
Yong Wud8c1df02021-01-07 20:29:03 +08002652 const struct iommu_ops *ops = domain->ops;
Robin Murphy5d95f402018-10-11 16:56:42 +01002653 size_t len = 0, mapped = 0;
2654 phys_addr_t start;
2655 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002656 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002657
Robin Murphy5d95f402018-10-11 16:56:42 +01002658 while (i <= nents) {
2659 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002660
Robin Murphy5d95f402018-10-11 16:56:42 +01002661 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002662 ret = __iommu_map(domain, iova + mapped, start,
2663 len, prot, gfp);
2664
Robin Murphy5d95f402018-10-11 16:56:42 +01002665 if (ret)
2666 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002667
Robin Murphy5d95f402018-10-11 16:56:42 +01002668 mapped += len;
2669 len = 0;
2670 }
Robin Murphy18f23402014-11-25 17:50:55 +00002671
Robin Murphy5d95f402018-10-11 16:56:42 +01002672 if (len) {
2673 len += sg->length;
2674 } else {
2675 len = sg->length;
2676 start = s_phys;
2677 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002678
Robin Murphy5d95f402018-10-11 16:56:42 +01002679 if (++i < nents)
2680 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002681 }
2682
Yong Wud8c1df02021-01-07 20:29:03 +08002683 if (ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002684 ops->iotlb_sync_map(domain, iova, mapped);
Olav Haugan315786e2014-10-25 09:55:16 -07002685 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002686
2687out_err:
2688 /* undo mappings already done */
2689 iommu_unmap(domain, iova, mapped);
2690
2691 return 0;
2692
Olav Haugan315786e2014-10-25 09:55:16 -07002693}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002694
2695size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2696 struct scatterlist *sg, unsigned int nents, int prot)
2697{
2698 might_sleep();
2699 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2700}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002701EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002702
Tom Murphy781ca2d2019-09-08 09:56:38 -07002703size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2704 struct scatterlist *sg, unsigned int nents, int prot)
2705{
2706 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2707}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002708
Joerg Roedel207c6e32017-04-26 15:39:28 +02002709/**
2710 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2711 * @domain: the iommu domain where the fault has happened
2712 * @dev: the device where the fault has happened
2713 * @iova: the faulting address
2714 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2715 *
2716 * This function should be called by the low-level IOMMU implementations
2717 * whenever IOMMU faults happen, to allow high-level users, that are
2718 * interested in such events, to know about them.
2719 *
2720 * This event may be useful for several possible use cases:
2721 * - mere logging of the event
2722 * - dynamic TLB/PTE loading
2723 * - if restarting of the faulting device is required
2724 *
2725 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2726 * PTE/TLB loading will one day be supported, implementations will be able
2727 * to tell whether it succeeded or not according to this return value).
2728 *
2729 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2730 * (though fault handlers can also return -ENOSYS, in case they want to
2731 * elicit the default behavior of the IOMMU drivers).
2732 */
2733int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2734 unsigned long iova, int flags)
2735{
2736 int ret = -ENOSYS;
2737
2738 /*
2739 * if upper layers showed interest and installed a fault handler,
2740 * invoke it.
2741 */
2742 if (domain->handler)
2743 ret = domain->handler(domain, dev, iova, flags,
2744 domain->handler_token);
2745
2746 trace_io_page_fault(dev, iova, flags);
2747 return ret;
2748}
2749EXPORT_SYMBOL_GPL(report_iommu_fault);
2750
Alex Williamsond72e31c2012-05-30 14:18:53 -06002751static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002752{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002753 iommu_group_kset = kset_create_and_add("iommu_groups",
2754 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002755 BUG_ON(!iommu_group_kset);
2756
Gary R Hookbad614b2018-06-12 16:41:21 -05002757 iommu_debugfs_setup();
2758
Alex Williamsond72e31c2012-05-30 14:18:53 -06002759 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002760}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002761core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002762
Christoph Hellwig7e147542021-04-01 17:52:52 +02002763int iommu_enable_nesting(struct iommu_domain *domain)
2764{
2765 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2766 return -EINVAL;
2767 if (!domain->ops->enable_nesting)
2768 return -EINVAL;
2769 return domain->ops->enable_nesting(domain);
2770}
2771EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2772
Christoph Hellwig4fc52b82021-04-01 17:52:55 +02002773int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2774 unsigned long quirk)
2775{
2776 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2777 return -EINVAL;
2778 if (!domain->ops->set_pgtable_quirks)
2779 return -EINVAL;
2780 return domain->ops->set_pgtable_quirks(domain, quirk);
2781}
2782EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2783
Eric Augere5b52342017-01-19 20:57:47 +00002784void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002785{
2786 const struct iommu_ops *ops = dev->bus->iommu_ops;
2787
Eric Augere5b52342017-01-19 20:57:47 +00002788 if (ops && ops->get_resv_regions)
2789 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002790}
2791
Eric Augere5b52342017-01-19 20:57:47 +00002792void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002793{
2794 const struct iommu_ops *ops = dev->bus->iommu_ops;
2795
Eric Augere5b52342017-01-19 20:57:47 +00002796 if (ops && ops->put_resv_regions)
2797 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002798}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002799
Thierry Redingf9f69712019-12-18 14:42:01 +01002800/**
2801 * generic_iommu_put_resv_regions - Reserved region driver helper
2802 * @dev: device for which to free reserved regions
2803 * @list: reserved region list for device
2804 *
2805 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2806 * for simple reservations. Memory allocated for each reserved region will be
2807 * freed. If an IOMMU driver allocates additional resources per region, it is
2808 * going to have to implement a custom callback.
2809 */
2810void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2811{
2812 struct iommu_resv_region *entry, *next;
2813
2814 list_for_each_entry_safe(entry, next, list, list)
2815 kfree(entry);
2816}
2817EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2818
Eric Auger2b20cbb2017-01-19 20:57:49 +00002819struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002820 size_t length, int prot,
2821 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002822{
2823 struct iommu_resv_region *region;
2824
2825 region = kzalloc(sizeof(*region), GFP_KERNEL);
2826 if (!region)
2827 return NULL;
2828
2829 INIT_LIST_HEAD(&region->list);
2830 region->start = start;
2831 region->length = length;
2832 region->prot = prot;
2833 region->type = type;
2834 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002835}
Will Deacona7ba5c32019-12-19 12:03:37 +00002836EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002837
Joerg Roedel8a699612019-08-19 15:22:47 +02002838void iommu_set_default_passthrough(bool cmd_line)
2839{
2840 if (cmd_line)
Christoph Hellwig31897132021-04-01 17:52:53 +02002841 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
Joerg Roedel8a699612019-08-19 15:22:47 +02002842 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2843}
2844
2845void iommu_set_default_translated(bool cmd_line)
2846{
2847 if (cmd_line)
Christoph Hellwig31897132021-04-01 17:52:53 +02002848 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
Joerg Roedel8a699612019-08-19 15:22:47 +02002849 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2850}
2851
2852bool iommu_default_passthrough(void)
2853{
2854 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2855}
2856EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2857
Joerg Roedel534766d2017-01-31 16:58:42 +01002858const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002859{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002860 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002861 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002862
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002863 spin_lock(&iommu_device_lock);
2864 list_for_each_entry(iommu, &iommu_device_list, list)
2865 if (iommu->fwnode == fwnode) {
2866 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002867 break;
2868 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002869 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002870 return ops;
2871}
2872
Robin Murphy57f98d22016-09-13 10:54:14 +01002873int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2874 const struct iommu_ops *ops)
2875{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002876 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002877
2878 if (fwspec)
2879 return ops == fwspec->ops ? 0 : -EINVAL;
2880
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002881 if (!dev_iommu_get(dev))
2882 return -ENOMEM;
2883
Robin Murphy098accf2020-02-13 14:00:21 +00002884 /* Preallocate for the overwhelmingly common case of 1 ID */
2885 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002886 if (!fwspec)
2887 return -ENOMEM;
2888
2889 of_node_get(to_of_node(iommu_fwnode));
2890 fwspec->iommu_fwnode = iommu_fwnode;
2891 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002892 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002893 return 0;
2894}
2895EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2896
2897void iommu_fwspec_free(struct device *dev)
2898{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002899 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002900
2901 if (fwspec) {
2902 fwnode_handle_put(fwspec->iommu_fwnode);
2903 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002904 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002905 }
2906}
2907EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2908
2909int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2910{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002911 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002912 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002913
2914 if (!fwspec)
2915 return -EINVAL;
2916
Robin Murphy098accf2020-02-13 14:00:21 +00002917 new_num = fwspec->num_ids + num_ids;
2918 if (new_num > 1) {
2919 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2920 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002921 if (!fwspec)
2922 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002923
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002924 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002925 }
2926
2927 for (i = 0; i < num_ids; i++)
2928 fwspec->ids[fwspec->num_ids + i] = ids[i];
2929
Robin Murphy098accf2020-02-13 14:00:21 +00002930 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002931 return 0;
2932}
2933EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002934
2935/*
2936 * Per device IOMMU features.
2937 */
Lu Baolua3a19592019-03-25 09:30:28 +08002938int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2939{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002940 if (dev->iommu && dev->iommu->iommu_dev) {
2941 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002942
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002943 if (ops->dev_enable_feat)
2944 return ops->dev_enable_feat(dev, feat);
2945 }
Lu Baolua3a19592019-03-25 09:30:28 +08002946
2947 return -ENODEV;
2948}
2949EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2950
2951/*
2952 * The device drivers should do the necessary cleanups before calling this.
2953 * For example, before disabling the aux-domain feature, the device driver
2954 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2955 */
2956int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2957{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002958 if (dev->iommu && dev->iommu->iommu_dev) {
2959 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002960
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002961 if (ops->dev_disable_feat)
2962 return ops->dev_disable_feat(dev, feat);
2963 }
Lu Baolua3a19592019-03-25 09:30:28 +08002964
2965 return -EBUSY;
2966}
2967EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2968
2969bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2970{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002971 if (dev->iommu && dev->iommu->iommu_dev) {
2972 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002973
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002974 if (ops->dev_feat_enabled)
2975 return ops->dev_feat_enabled(dev, feat);
2976 }
Lu Baolua3a19592019-03-25 09:30:28 +08002977
2978 return false;
2979}
2980EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2981
2982/*
2983 * Aux-domain specific attach/detach.
2984 *
2985 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2986 * true. Also, as long as domains are attached to a device through this
2987 * interface, any tries to call iommu_attach_device() should fail
2988 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2989 * This should make us safe against a device being attached to a guest as a
2990 * whole while there are still pasid users on it (aux and sva).
2991 */
2992int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2993{
2994 int ret = -ENODEV;
2995
2996 if (domain->ops->aux_attach_dev)
2997 ret = domain->ops->aux_attach_dev(domain, dev);
2998
2999 if (!ret)
3000 trace_attach_device_to_domain(dev);
3001
3002 return ret;
3003}
3004EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
3005
3006void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
3007{
3008 if (domain->ops->aux_detach_dev) {
3009 domain->ops->aux_detach_dev(domain, dev);
3010 trace_detach_device_from_domain(dev);
3011 }
3012}
3013EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
3014
3015int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
3016{
3017 int ret = -ENODEV;
3018
3019 if (domain->ops->aux_get_pasid)
3020 ret = domain->ops->aux_get_pasid(domain, dev);
3021
3022 return ret;
3023}
3024EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003025
3026/**
3027 * iommu_sva_bind_device() - Bind a process address space to a device
3028 * @dev: the device
3029 * @mm: the mm to bind, caller must hold a reference to it
3030 *
3031 * Create a bond between device and address space, allowing the device to access
3032 * the mm using the returned PASID. If a bond already exists between @device and
3033 * @mm, it is returned and an additional reference is taken. Caller must call
3034 * iommu_sva_unbind_device() to release each reference.
3035 *
3036 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
3037 * initialize the required SVA features.
3038 *
3039 * On error, returns an ERR_PTR value.
3040 */
3041struct iommu_sva *
3042iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
3043{
3044 struct iommu_group *group;
3045 struct iommu_sva *handle = ERR_PTR(-EINVAL);
3046 const struct iommu_ops *ops = dev->bus->iommu_ops;
3047
3048 if (!ops || !ops->sva_bind)
3049 return ERR_PTR(-ENODEV);
3050
3051 group = iommu_group_get(dev);
3052 if (!group)
3053 return ERR_PTR(-ENODEV);
3054
3055 /* Ensure device count and domain don't change while we're binding */
3056 mutex_lock(&group->mutex);
3057
3058 /*
3059 * To keep things simple, SVA currently doesn't support IOMMU groups
3060 * with more than one device. Existing SVA-capable systems are not
3061 * affected by the problems that required IOMMU groups (lack of ACS
3062 * isolation, device ID aliasing and other hardware issues).
3063 */
3064 if (iommu_group_device_count(group) != 1)
3065 goto out_unlock;
3066
3067 handle = ops->sva_bind(dev, mm, drvdata);
3068
3069out_unlock:
3070 mutex_unlock(&group->mutex);
3071 iommu_group_put(group);
3072
3073 return handle;
3074}
3075EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
3076
3077/**
3078 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
3079 * @handle: the handle returned by iommu_sva_bind_device()
3080 *
3081 * Put reference to a bond between device and address space. The device should
3082 * not be issuing any more transaction for this PASID. All outstanding page
3083 * requests for this PASID must have been flushed to the IOMMU.
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003084 */
3085void iommu_sva_unbind_device(struct iommu_sva *handle)
3086{
3087 struct iommu_group *group;
3088 struct device *dev = handle->dev;
3089 const struct iommu_ops *ops = dev->bus->iommu_ops;
3090
3091 if (!ops || !ops->sva_unbind)
3092 return;
3093
3094 group = iommu_group_get(dev);
3095 if (!group)
3096 return;
3097
3098 mutex_lock(&group->mutex);
3099 ops->sva_unbind(handle);
3100 mutex_unlock(&group->mutex);
3101
3102 iommu_group_put(group);
3103}
3104EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
3105
Fenghua Yuc7b6bac2020-09-15 09:30:05 -07003106u32 iommu_sva_get_pasid(struct iommu_sva *handle)
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003107{
3108 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
3109
3110 if (!ops || !ops->sva_get_pasid)
3111 return IOMMU_PASID_INVALID;
3112
3113 return ops->sva_get_pasid(handle);
3114}
3115EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003116
3117/*
3118 * Changes the default domain of an iommu group that has *only* one device
3119 *
3120 * @group: The group for which the default domain should be changed
3121 * @prev_dev: The device in the group (this is used to make sure that the device
3122 * hasn't changed after the caller has called this function)
3123 * @type: The type of the new default domain that gets associated with the group
3124 *
3125 * Returns 0 on success and error code on failure
3126 *
3127 * Note:
3128 * 1. Presently, this function is called only when user requests to change the
3129 * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
3130 * Please take a closer look if intended to use for other purposes.
3131 */
3132static int iommu_change_dev_def_domain(struct iommu_group *group,
3133 struct device *prev_dev, int type)
3134{
3135 struct iommu_domain *prev_dom;
3136 struct group_device *grp_dev;
3137 int ret, dev_def_dom;
3138 struct device *dev;
3139
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003140 mutex_lock(&group->mutex);
3141
3142 if (group->default_domain != group->domain) {
3143 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n");
3144 ret = -EBUSY;
3145 goto out;
3146 }
3147
3148 /*
3149 * iommu group wasn't locked while acquiring device lock in
3150 * iommu_group_store_type(). So, make sure that the device count hasn't
3151 * changed while acquiring device lock.
3152 *
3153 * Changing default domain of an iommu group with two or more devices
3154 * isn't supported because there could be a potential deadlock. Consider
3155 * the following scenario. T1 is trying to acquire device locks of all
3156 * the devices in the group and before it could acquire all of them,
3157 * there could be another thread T2 (from different sub-system and use
3158 * case) that has already acquired some of the device locks and might be
3159 * waiting for T1 to release other device locks.
3160 */
3161 if (iommu_group_device_count(group) != 1) {
3162 dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n");
3163 ret = -EINVAL;
3164 goto out;
3165 }
3166
3167 /* Since group has only one device */
3168 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3169 dev = grp_dev->dev;
3170
3171 if (prev_dev != dev) {
3172 dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n");
3173 ret = -EBUSY;
3174 goto out;
3175 }
3176
3177 prev_dom = group->default_domain;
3178 if (!prev_dom) {
3179 ret = -EINVAL;
3180 goto out;
3181 }
3182
3183 dev_def_dom = iommu_get_def_domain_type(dev);
3184 if (!type) {
3185 /*
3186 * If the user hasn't requested any specific type of domain and
3187 * if the device supports both the domains, then default to the
3188 * domain the device was booted with
3189 */
3190 type = dev_def_dom ? : iommu_def_domain_type;
3191 } else if (dev_def_dom && type != dev_def_dom) {
3192 dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
3193 iommu_domain_type_str(type));
3194 ret = -EINVAL;
3195 goto out;
3196 }
3197
3198 /*
3199 * Switch to a new domain only if the requested domain type is different
3200 * from the existing default domain type
3201 */
3202 if (prev_dom->type == type) {
3203 ret = 0;
3204 goto out;
3205 }
3206
3207 /* Sets group->default_domain to the newly allocated domain */
3208 ret = iommu_group_alloc_default_domain(dev->bus, group, type);
3209 if (ret)
3210 goto out;
3211
3212 ret = iommu_create_device_direct_mappings(group, dev);
3213 if (ret)
3214 goto free_new_domain;
3215
3216 ret = __iommu_attach_device(group->default_domain, dev);
3217 if (ret)
3218 goto free_new_domain;
3219
3220 group->domain = group->default_domain;
3221
3222 /*
3223 * Release the mutex here because ops->probe_finalize() call-back of
3224 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3225 * in-turn might call back into IOMMU core code, where it tries to take
3226 * group->mutex, resulting in a deadlock.
3227 */
3228 mutex_unlock(&group->mutex);
3229
3230 /* Make sure dma_ops is appropriatley set */
3231 iommu_group_do_probe_finalize(dev, group->default_domain);
3232 iommu_domain_free(prev_dom);
3233 return 0;
3234
3235free_new_domain:
3236 iommu_domain_free(group->default_domain);
3237 group->default_domain = prev_dom;
3238 group->domain = prev_dom;
3239
3240out:
3241 mutex_unlock(&group->mutex);
3242
3243 return ret;
3244}
3245
3246/*
3247 * Changing the default domain through sysfs requires the users to ubind the
3248 * drivers from the devices in the iommu group. Return failure if this doesn't
3249 * meet.
3250 *
3251 * We need to consider the race between this and the device release path.
3252 * device_lock(dev) is used here to guarantee that the device release path
3253 * will not be entered at the same time.
3254 */
3255static ssize_t iommu_group_store_type(struct iommu_group *group,
3256 const char *buf, size_t count)
3257{
3258 struct group_device *grp_dev;
3259 struct device *dev;
3260 int ret, req_type;
3261
3262 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3263 return -EACCES;
3264
3265 if (WARN_ON(!group))
3266 return -EINVAL;
3267
3268 if (sysfs_streq(buf, "identity"))
3269 req_type = IOMMU_DOMAIN_IDENTITY;
3270 else if (sysfs_streq(buf, "DMA"))
3271 req_type = IOMMU_DOMAIN_DMA;
Robin Murphy26225be2021-08-11 13:21:35 +01003272 else if (sysfs_streq(buf, "DMA-FQ"))
3273 req_type = IOMMU_DOMAIN_DMA_FQ;
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003274 else if (sysfs_streq(buf, "auto"))
3275 req_type = 0;
3276 else
3277 return -EINVAL;
3278
3279 /*
3280 * Lock/Unlock the group mutex here before device lock to
3281 * 1. Make sure that the iommu group has only one device (this is a
3282 * prerequisite for step 2)
3283 * 2. Get struct *dev which is needed to lock device
3284 */
3285 mutex_lock(&group->mutex);
3286 if (iommu_group_device_count(group) != 1) {
3287 mutex_unlock(&group->mutex);
3288 pr_err_ratelimited("Cannot change default domain: Group has more than one device\n");
3289 return -EINVAL;
3290 }
3291
3292 /* Since group has only one device */
3293 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3294 dev = grp_dev->dev;
3295 get_device(dev);
3296
3297 /*
3298 * Don't hold the group mutex because taking group mutex first and then
3299 * the device lock could potentially cause a deadlock as below. Assume
3300 * two threads T1 and T2. T1 is trying to change default domain of an
3301 * iommu group and T2 is trying to hot unplug a device or release [1] VF
3302 * of a PCIe device which is in the same iommu group. T1 takes group
3303 * mutex and before it could take device lock assume T2 has taken device
3304 * lock and is yet to take group mutex. Now, both the threads will be
3305 * waiting for the other thread to release lock. Below, lock order was
3306 * suggested.
3307 * device_lock(dev);
3308 * mutex_lock(&group->mutex);
3309 * iommu_change_dev_def_domain();
3310 * mutex_unlock(&group->mutex);
3311 * device_unlock(dev);
3312 *
3313 * [1] Typical device release path
3314 * device_lock() from device/driver core code
3315 * -> bus_notifier()
3316 * -> iommu_bus_notifier()
3317 * -> iommu_release_device()
3318 * -> ops->release_device() vendor driver calls back iommu core code
3319 * -> mutex_lock() from iommu core code
3320 */
3321 mutex_unlock(&group->mutex);
3322
3323 /* Check if the device in the group still has a driver bound to it */
3324 device_lock(dev);
3325 if (device_is_bound(dev)) {
3326 pr_err_ratelimited("Device is still bound to driver\n");
3327 ret = -EBUSY;
3328 goto out;
3329 }
3330
3331 ret = iommu_change_dev_def_domain(group, dev, req_type);
3332 ret = ret ?: count;
3333
3334out:
3335 device_unlock(dev);
3336 put_device(dev);
3337
3338 return ret;
3339}