blob: f653a70389ef7465e932833f6c45417c23824216 [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;
Zhen Lei712d8f22021-07-12 19:12:17 +080034static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_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
John Garryd8577d22021-07-12 19:12:16 +0800147 pr_info("DMA domain TLB invalidation policy: %s mode %s\n",
148 iommu_dma_strict ? "strict" : "lazy",
149 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
150 "(set via kernel command line)" : "");
151
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200152 return 0;
153}
154subsys_initcall(iommu_subsys_init);
155
Robin Murphy2d471b22021-04-01 14:56:26 +0100156/**
157 * iommu_device_register() - Register an IOMMU hardware instance
158 * @iommu: IOMMU handle for the instance
159 * @ops: IOMMU ops to associate with the instance
160 * @hwdev: (optional) actual instance device, used for fwnode lookup
161 *
162 * Return: 0 on success, or an error.
163 */
164int iommu_device_register(struct iommu_device *iommu,
165 const struct iommu_ops *ops, struct device *hwdev)
Joerg Roedelb0119e82017-02-01 13:23:08 +0100166{
Robin Murphy2d471b22021-04-01 14:56:26 +0100167 /* We need to be able to take module references appropriately */
168 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
169 return -EINVAL;
170
171 iommu->ops = ops;
172 if (hwdev)
173 iommu->fwnode = hwdev->fwnode;
174
Joerg Roedelb0119e82017-02-01 13:23:08 +0100175 spin_lock(&iommu_device_lock);
176 list_add_tail(&iommu->list, &iommu_device_list);
177 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100178 return 0;
179}
Will Deacona7ba5c32019-12-19 12:03:37 +0000180EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100181
182void iommu_device_unregister(struct iommu_device *iommu)
183{
184 spin_lock(&iommu_device_lock);
185 list_del(&iommu->list);
186 spin_unlock(&iommu_device_lock);
187}
Will Deacona7ba5c32019-12-19 12:03:37 +0000188EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100189
Joerg Roedel045a7042020-03-26 16:08:30 +0100190static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100191{
Joerg Roedel045a7042020-03-26 16:08:30 +0100192 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100193
194 if (param)
195 return param;
196
197 param = kzalloc(sizeof(*param), GFP_KERNEL);
198 if (!param)
199 return NULL;
200
201 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100202 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100203 return param;
204}
205
Joerg Roedel045a7042020-03-26 16:08:30 +0100206static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100207{
Kevin Hao5375e872020-04-02 22:37:49 +0800208 iommu_fwspec_free(dev);
Joerg Roedel045a7042020-03-26 16:08:30 +0100209 kfree(dev->iommu);
210 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100211}
212
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200213static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100214{
215 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200216 struct iommu_device *iommu_dev;
217 struct iommu_group *group;
Jacob Pan0c830e62019-06-03 15:57:48 +0100218 int ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100219
Jacob Pan0c830e62019-06-03 15:57:48 +0100220 if (!ops)
Thierry Redingf38338c2020-05-11 18:10:00 +0200221 return -ENODEV;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100222
Joerg Roedel045a7042020-03-26 16:08:30 +0100223 if (!dev_iommu_get(dev))
Jacob Pan0c830e62019-06-03 15:57:48 +0100224 return -ENOMEM;
225
Will Deacon25f003d2019-12-19 12:03:41 +0000226 if (!try_module_get(ops->owner)) {
227 ret = -EINVAL;
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200228 goto err_free;
Will Deacon25f003d2019-12-19 12:03:41 +0000229 }
230
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200231 iommu_dev = ops->probe_device(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200232 if (IS_ERR(iommu_dev)) {
233 ret = PTR_ERR(iommu_dev);
234 goto out_module_put;
235 }
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200236
237 dev->iommu->iommu_dev = iommu_dev;
238
239 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200240 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200241 ret = PTR_ERR(group);
242 goto out_release;
243 }
244 iommu_group_put(group);
245
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200246 if (group_list && !group->default_domain && list_empty(&group->entry))
247 list_add_tail(&group->entry, group_list);
248
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200249 iommu_device_link(iommu_dev, dev);
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100250
Will Deacon25f003d2019-12-19 12:03:41 +0000251 return 0;
252
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200253out_release:
254 ops->release_device(dev);
255
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200256out_module_put:
Will Deacon25f003d2019-12-19 12:03:41 +0000257 module_put(ops->owner);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200258
259err_free:
Joerg Roedel045a7042020-03-26 16:08:30 +0100260 dev_iommu_free(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200261
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100262 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100263}
264
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200265int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200266{
267 const struct iommu_ops *ops = dev->bus->iommu_ops;
268 struct iommu_group *group;
269 int ret;
270
271 ret = __iommu_probe_device(dev, NULL);
272 if (ret)
273 goto err_out;
274
Joerg Roedel79659190e2020-05-25 15:01:22 +0200275 group = iommu_group_get(dev);
Yang Yingliang058236e2020-11-26 21:38:25 +0800276 if (!group) {
277 ret = -ENODEV;
Joerg Roedel79659190e2020-05-25 15:01:22 +0200278 goto err_release;
Yang Yingliang058236e2020-11-26 21:38:25 +0800279 }
Joerg Roedel79659190e2020-05-25 15:01:22 +0200280
Joerg Roedelcf193882020-04-29 15:36:48 +0200281 /*
282 * Try to allocate a default domain - needs support from the
283 * IOMMU driver. There are still some drivers which don't
284 * support default domains, so the return value is not yet
285 * checked.
286 */
Joerg Roedel79659190e2020-05-25 15:01:22 +0200287 iommu_alloc_default_domain(group, dev);
Joerg Roedelcf193882020-04-29 15:36:48 +0200288
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000289 if (group->default_domain) {
Joerg Roedelcf193882020-04-29 15:36:48 +0200290 ret = __iommu_attach_device(group->default_domain, dev);
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000291 if (ret) {
292 iommu_group_put(group);
293 goto err_release;
294 }
295 }
Joerg Roedelcf193882020-04-29 15:36:48 +0200296
Joerg Roedelce574c22020-04-29 15:36:50 +0200297 iommu_create_device_direct_mappings(group, dev);
298
Joerg Roedelcf193882020-04-29 15:36:48 +0200299 iommu_group_put(group);
300
Joerg Roedelcf193882020-04-29 15:36:48 +0200301 if (ops->probe_finalize)
302 ops->probe_finalize(dev);
303
304 return 0;
305
306err_release:
307 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200308
Joerg Roedelcf193882020-04-29 15:36:48 +0200309err_out:
310 return ret;
311
312}
313
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100314void iommu_release_device(struct device *dev)
315{
316 const struct iommu_ops *ops = dev->bus->iommu_ops;
317
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200318 if (!dev->iommu)
319 return;
Jacob Pan0c830e62019-06-03 15:57:48 +0100320
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200321 iommu_device_unlink(dev->iommu->iommu_dev, dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200322
323 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100324
Qian Cai9ac85452020-07-03 20:10:03 -0400325 iommu_group_remove_device(dev);
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200326 module_put(ops->owner);
327 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100328}
329
Will Deaconfccb4e32017-01-05 18:38:26 +0000330static int __init iommu_set_def_domain_type(char *str)
331{
332 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300333 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000334
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300335 ret = kstrtobool(str, &pt);
336 if (ret)
337 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000338
Joerg Roedeladab0b02019-08-19 15:22:48 +0200339 if (pt)
340 iommu_set_default_passthrough(true);
341 else
342 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200343
Will Deaconfccb4e32017-01-05 18:38:26 +0000344 return 0;
345}
346early_param("iommu.passthrough", iommu_set_def_domain_type);
347
Zhen Lei68a6efe2018-09-20 17:10:23 +0100348static int __init iommu_dma_setup(char *str)
349{
Robin Murphya250c232021-04-01 17:52:54 +0200350 int ret = kstrtobool(str, &iommu_dma_strict);
351
352 if (!ret)
353 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
354 return ret;
Zhen Lei68a6efe2018-09-20 17:10:23 +0100355}
356early_param("iommu.strict", iommu_dma_setup);
357
John Garry308723e2021-07-12 19:12:20 +0800358void iommu_set_dma_strict(void)
Robin Murphya250c232021-04-01 17:52:54 +0200359{
John Garry308723e2021-07-12 19:12:20 +0800360 iommu_dma_strict = true;
Robin Murphyc2089162021-08-11 13:21:34 +0100361 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
362 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Robin Murphya250c232021-04-01 17:52:54 +0200363}
364
Alex Williamsond72e31c2012-05-30 14:18:53 -0600365static ssize_t iommu_group_attr_show(struct kobject *kobj,
366 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400367{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600368 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
369 struct iommu_group *group = to_iommu_group(kobj);
370 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400371
Alex Williamsond72e31c2012-05-30 14:18:53 -0600372 if (attr->show)
373 ret = attr->show(group, buf);
374 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400375}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600376
377static ssize_t iommu_group_attr_store(struct kobject *kobj,
378 struct attribute *__attr,
379 const char *buf, size_t count)
380{
381 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
382 struct iommu_group *group = to_iommu_group(kobj);
383 ssize_t ret = -EIO;
384
385 if (attr->store)
386 ret = attr->store(group, buf, count);
387 return ret;
388}
389
390static const struct sysfs_ops iommu_group_sysfs_ops = {
391 .show = iommu_group_attr_show,
392 .store = iommu_group_attr_store,
393};
394
395static int iommu_group_create_file(struct iommu_group *group,
396 struct iommu_group_attribute *attr)
397{
398 return sysfs_create_file(&group->kobj, &attr->attr);
399}
400
401static void iommu_group_remove_file(struct iommu_group *group,
402 struct iommu_group_attribute *attr)
403{
404 sysfs_remove_file(&group->kobj, &attr->attr);
405}
406
407static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
408{
409 return sprintf(buf, "%s\n", group->name);
410}
411
Eric Auger6c65fb32017-01-19 20:57:51 +0000412/**
413 * iommu_insert_resv_region - Insert a new region in the
414 * list of reserved regions.
415 * @new: new region to insert
416 * @regions: list of regions
417 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200418 * Elements are sorted by start address and overlapping segments
419 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000420 */
Wei Yongjun1b0b2a82020-07-13 22:25:42 +0800421static int iommu_insert_resv_region(struct iommu_resv_region *new,
422 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000423{
Eric Auger4dbd2582019-08-21 14:09:40 +0200424 struct iommu_resv_region *iter, *tmp, *nr, *top;
425 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000426
Eric Auger4dbd2582019-08-21 14:09:40 +0200427 nr = iommu_alloc_resv_region(new->start, new->length,
428 new->prot, new->type);
429 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000430 return -ENOMEM;
431
Eric Auger4dbd2582019-08-21 14:09:40 +0200432 /* First add the new element based on start address sorting */
433 list_for_each_entry(iter, regions, list) {
434 if (nr->start < iter->start ||
435 (nr->start == iter->start && nr->type <= iter->type))
436 break;
437 }
438 list_add_tail(&nr->list, &iter->list);
439
440 /* Merge overlapping segments of type nr->type in @regions, if any */
441 list_for_each_entry_safe(iter, tmp, regions, list) {
442 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
443
Eric Auger4c80ba32019-11-26 18:54:13 +0100444 /* no merge needed on elements of different types than @new */
445 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200446 list_move_tail(&iter->list, &stack);
447 continue;
448 }
449
450 /* look for the last stack element of same type as @iter */
451 list_for_each_entry_reverse(top, &stack, list)
452 if (top->type == iter->type)
453 goto check_overlap;
454
455 list_move_tail(&iter->list, &stack);
456 continue;
457
458check_overlap:
459 top_end = top->start + top->length - 1;
460
461 if (iter->start > top_end + 1) {
462 list_move_tail(&iter->list, &stack);
463 } else {
464 top->length = max(top_end, iter_end) - top->start + 1;
465 list_del(&iter->list);
466 kfree(iter);
467 }
468 }
469 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000470 return 0;
471}
472
473static int
474iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
475 struct list_head *group_resv_regions)
476{
477 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100478 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000479
480 list_for_each_entry(entry, dev_resv_regions, list) {
481 ret = iommu_insert_resv_region(entry, group_resv_regions);
482 if (ret)
483 break;
484 }
485 return ret;
486}
487
488int iommu_get_group_resv_regions(struct iommu_group *group,
489 struct list_head *head)
490{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100491 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000492 int ret = 0;
493
494 mutex_lock(&group->mutex);
495 list_for_each_entry(device, &group->devices, list) {
496 struct list_head dev_resv_regions;
497
498 INIT_LIST_HEAD(&dev_resv_regions);
499 iommu_get_resv_regions(device->dev, &dev_resv_regions);
500 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
501 iommu_put_resv_regions(device->dev, &dev_resv_regions);
502 if (ret)
503 break;
504 }
505 mutex_unlock(&group->mutex);
506 return ret;
507}
508EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
509
Eric Augerbc7d12b92017-01-19 20:57:52 +0000510static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
511 char *buf)
512{
513 struct iommu_resv_region *region, *next;
514 struct list_head group_resv_regions;
515 char *str = buf;
516
517 INIT_LIST_HEAD(&group_resv_regions);
518 iommu_get_group_resv_regions(group, &group_resv_regions);
519
520 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
521 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
522 (long long int)region->start,
523 (long long int)(region->start +
524 region->length - 1),
525 iommu_group_resv_type_string[region->type]);
526 kfree(region);
527 }
528
529 return (str - buf);
530}
531
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700532static ssize_t iommu_group_show_type(struct iommu_group *group,
533 char *buf)
534{
535 char *type = "unknown\n";
536
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800537 mutex_lock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700538 if (group->default_domain) {
539 switch (group->default_domain->type) {
540 case IOMMU_DOMAIN_BLOCKED:
541 type = "blocked\n";
542 break;
543 case IOMMU_DOMAIN_IDENTITY:
544 type = "identity\n";
545 break;
546 case IOMMU_DOMAIN_UNMANAGED:
547 type = "unmanaged\n";
548 break;
549 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800550 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700551 break;
Robin Murphybf3aed462021-08-11 13:21:30 +0100552 case IOMMU_DOMAIN_DMA_FQ:
553 type = "DMA-FQ\n";
554 break;
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700555 }
556 }
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800557 mutex_unlock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700558 strcpy(buf, type);
559
560 return strlen(type);
561}
562
Alex Williamsond72e31c2012-05-30 14:18:53 -0600563static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
564
Eric Augerbc7d12b92017-01-19 20:57:52 +0000565static IOMMU_GROUP_ATTR(reserved_regions, 0444,
566 iommu_group_show_resv_regions, NULL);
567
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +0800568static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
569 iommu_group_store_type);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700570
Alex Williamsond72e31c2012-05-30 14:18:53 -0600571static void iommu_group_release(struct kobject *kobj)
572{
573 struct iommu_group *group = to_iommu_group(kobj);
574
Joerg Roedel269aa802015-05-28 18:41:25 +0200575 pr_debug("Releasing group %d\n", group->id);
576
Alex Williamsond72e31c2012-05-30 14:18:53 -0600577 if (group->iommu_data_release)
578 group->iommu_data_release(group->iommu_data);
579
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200580 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600581
Joerg Roedel53723dc2015-05-28 18:41:29 +0200582 if (group->default_domain)
583 iommu_domain_free(group->default_domain);
584
Alex Williamsond72e31c2012-05-30 14:18:53 -0600585 kfree(group->name);
586 kfree(group);
587}
588
589static struct kobj_type iommu_group_ktype = {
590 .sysfs_ops = &iommu_group_sysfs_ops,
591 .release = iommu_group_release,
592};
593
594/**
595 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600596 *
597 * This function is called by an iommu driver to allocate a new iommu
598 * group. The iommu group represents the minimum granularity of the iommu.
599 * Upon successful return, the caller holds a reference to the supplied
600 * group in order to hold the group until devices are added. Use
601 * iommu_group_put() to release this extra reference count, allowing the
602 * group to be automatically reclaimed once it has no devices or external
603 * references.
604 */
605struct iommu_group *iommu_group_alloc(void)
606{
607 struct iommu_group *group;
608 int ret;
609
610 group = kzalloc(sizeof(*group), GFP_KERNEL);
611 if (!group)
612 return ERR_PTR(-ENOMEM);
613
614 group->kobj.kset = iommu_group_kset;
615 mutex_init(&group->mutex);
616 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200617 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600618 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
619
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200620 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
621 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600622 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200623 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600624 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200625 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600626
627 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
628 NULL, "%d", group->id);
629 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200630 ida_simple_remove(&iommu_group_ida, group->id);
Qiushi Wu7cc31612020-05-27 16:00:19 -0500631 kobject_put(&group->kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600632 return ERR_PTR(ret);
633 }
634
635 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
636 if (!group->devices_kobj) {
637 kobject_put(&group->kobj); /* triggers .release & free */
638 return ERR_PTR(-ENOMEM);
639 }
640
641 /*
642 * The devices_kobj holds a reference on the group kobject, so
643 * as long as that exists so will the group. We can therefore
644 * use the devices_kobj for reference counting.
645 */
646 kobject_put(&group->kobj);
647
Eric Augerbc7d12b92017-01-19 20:57:52 +0000648 ret = iommu_group_create_file(group,
649 &iommu_group_attr_reserved_regions);
650 if (ret)
651 return ERR_PTR(ret);
652
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700653 ret = iommu_group_create_file(group, &iommu_group_attr_type);
654 if (ret)
655 return ERR_PTR(ret);
656
Joerg Roedel269aa802015-05-28 18:41:25 +0200657 pr_debug("Allocated group %d\n", group->id);
658
Alex Williamsond72e31c2012-05-30 14:18:53 -0600659 return group;
660}
661EXPORT_SYMBOL_GPL(iommu_group_alloc);
662
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100663struct iommu_group *iommu_group_get_by_id(int id)
664{
665 struct kobject *group_kobj;
666 struct iommu_group *group;
667 const char *name;
668
669 if (!iommu_group_kset)
670 return NULL;
671
672 name = kasprintf(GFP_KERNEL, "%d", id);
673 if (!name)
674 return NULL;
675
676 group_kobj = kset_find_obj(iommu_group_kset, name);
677 kfree(name);
678
679 if (!group_kobj)
680 return NULL;
681
682 group = container_of(group_kobj, struct iommu_group, kobj);
683 BUG_ON(group->id != id);
684
685 kobject_get(group->devices_kobj);
686 kobject_put(&group->kobj);
687
688 return group;
689}
690EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
691
Alex Williamsond72e31c2012-05-30 14:18:53 -0600692/**
693 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
694 * @group: the group
695 *
696 * iommu drivers can store data in the group for use when doing iommu
697 * operations. This function provides a way to retrieve it. Caller
698 * should hold a group reference.
699 */
700void *iommu_group_get_iommudata(struct iommu_group *group)
701{
702 return group->iommu_data;
703}
704EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
705
706/**
707 * iommu_group_set_iommudata - set iommu_data for a group
708 * @group: the group
709 * @iommu_data: new data
710 * @release: release function for iommu_data
711 *
712 * iommu drivers can store data in the group for use when doing iommu
713 * operations. This function provides a way to set the data after
714 * the group has been allocated. Caller should hold a group reference.
715 */
716void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
717 void (*release)(void *iommu_data))
718{
719 group->iommu_data = iommu_data;
720 group->iommu_data_release = release;
721}
722EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
723
724/**
725 * iommu_group_set_name - set name for a group
726 * @group: the group
727 * @name: name
728 *
729 * Allow iommu driver to set a name for a group. When set it will
730 * appear in a name attribute file under the group in sysfs.
731 */
732int iommu_group_set_name(struct iommu_group *group, const char *name)
733{
734 int ret;
735
736 if (group->name) {
737 iommu_group_remove_file(group, &iommu_group_attr_name);
738 kfree(group->name);
739 group->name = NULL;
740 if (!name)
741 return 0;
742 }
743
744 group->name = kstrdup(name, GFP_KERNEL);
745 if (!group->name)
746 return -ENOMEM;
747
748 ret = iommu_group_create_file(group, &iommu_group_attr_name);
749 if (ret) {
750 kfree(group->name);
751 group->name = NULL;
752 return ret;
753 }
754
755 return 0;
756}
757EXPORT_SYMBOL_GPL(iommu_group_set_name);
758
Joerg Roedelce574c22020-04-29 15:36:50 +0200759static int iommu_create_device_direct_mappings(struct iommu_group *group,
760 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200761{
762 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000763 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200764 struct list_head mappings;
765 unsigned long pg_size;
766 int ret = 0;
767
Robin Murphybf3aed462021-08-11 13:21:30 +0100768 if (!domain || !iommu_is_dma_domain(domain))
Joerg Roedelbeed2822015-05-28 18:41:34 +0200769 return 0;
770
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100771 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200772
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100773 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200774 INIT_LIST_HEAD(&mappings);
775
Eric Augere5b52342017-01-19 20:57:47 +0000776 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200777
778 /* We need to consider overlapping regions for different devices */
779 list_for_each_entry(entry, &mappings, list) {
780 dma_addr_t start, end, addr;
Yong Wu093b32a2020-12-07 17:35:53 +0800781 size_t map_size = 0;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200782
Eric Augere5b52342017-01-19 20:57:47 +0000783 if (domain->ops->apply_resv_region)
784 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200785
Joerg Roedelbeed2822015-05-28 18:41:34 +0200786 start = ALIGN(entry->start, pg_size);
787 end = ALIGN(entry->start + entry->length, pg_size);
788
Eric Augeradfd3732019-06-03 08:53:35 +0200789 if (entry->type != IOMMU_RESV_DIRECT &&
790 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000791 continue;
792
Yong Wu093b32a2020-12-07 17:35:53 +0800793 for (addr = start; addr <= end; addr += pg_size) {
Joerg Roedelbeed2822015-05-28 18:41:34 +0200794 phys_addr_t phys_addr;
795
Yong Wu093b32a2020-12-07 17:35:53 +0800796 if (addr == end)
797 goto map_end;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200798
Yong Wu093b32a2020-12-07 17:35:53 +0800799 phys_addr = iommu_iova_to_phys(domain, addr);
800 if (!phys_addr) {
801 map_size += pg_size;
802 continue;
803 }
804
805map_end:
806 if (map_size) {
807 ret = iommu_map(domain, addr - map_size,
808 addr - map_size, map_size,
809 entry->prot);
810 if (ret)
811 goto out;
812 map_size = 0;
813 }
Joerg Roedelbeed2822015-05-28 18:41:34 +0200814 }
815
816 }
817
Tom Murphyaae4c8e2020-08-17 22:00:49 +0100818 iommu_flush_iotlb_all(domain);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200819
Joerg Roedelbeed2822015-05-28 18:41:34 +0200820out:
Eric Augere5b52342017-01-19 20:57:47 +0000821 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200822
823 return ret;
824}
825
Joerg Roedelbd421262020-05-19 15:03:40 +0200826static bool iommu_is_attach_deferred(struct iommu_domain *domain,
827 struct device *dev)
828{
829 if (domain->ops->is_attach_deferred)
830 return domain->ops->is_attach_deferred(domain, dev);
831
832 return false;
833}
834
Alex Williamsond72e31c2012-05-30 14:18:53 -0600835/**
836 * iommu_group_add_device - add a device to an iommu group
837 * @group: the group into which to add the device (reference should be held)
838 * @dev: the device
839 *
840 * This function is called by an iommu driver to add a device into a
841 * group. Adding a device increments the group reference count.
842 */
843int iommu_group_add_device(struct iommu_group *group, struct device *dev)
844{
845 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100846 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600847
848 device = kzalloc(sizeof(*device), GFP_KERNEL);
849 if (!device)
850 return -ENOMEM;
851
852 device->dev = dev;
853
854 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000855 if (ret)
856 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600857
858 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
859rename:
860 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000861 ret = -ENOMEM;
862 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600863 }
864
865 ret = sysfs_create_link_nowarn(group->devices_kobj,
866 &dev->kobj, device->name);
867 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600868 if (ret == -EEXIST && i >= 0) {
869 /*
870 * Account for the slim chance of collision
871 * and append an instance to the name.
872 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000873 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600874 device->name = kasprintf(GFP_KERNEL, "%s.%d",
875 kobject_name(&dev->kobj), i++);
876 goto rename;
877 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000878 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600879 }
880
881 kobject_get(group->devices_kobj);
882
883 dev->iommu_group = group;
884
885 mutex_lock(&group->mutex);
886 list_add_tail(&device->list, &group->devices);
Joerg Roedelbd421262020-05-19 15:03:40 +0200887 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
Robin Murphy797a8b42017-01-16 12:58:07 +0000888 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600889 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000890 if (ret)
891 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600892
893 /* Notify any listeners about change to group. */
894 blocking_notifier_call_chain(&group->notifier,
895 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600896
897 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200898
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600899 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200900
Alex Williamsond72e31c2012-05-30 14:18:53 -0600901 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000902
903err_put_group:
904 mutex_lock(&group->mutex);
905 list_del(&device->list);
906 mutex_unlock(&group->mutex);
907 dev->iommu_group = NULL;
908 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700909 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000910err_free_name:
911 kfree(device->name);
912err_remove_link:
913 sysfs_remove_link(&dev->kobj, "iommu_group");
914err_free_device:
915 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600916 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000917 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600918}
919EXPORT_SYMBOL_GPL(iommu_group_add_device);
920
921/**
922 * iommu_group_remove_device - remove a device from it's current group
923 * @dev: device to be removed
924 *
925 * This function is called by an iommu driver to remove the device from
926 * it's current group. This decrements the iommu group reference count.
927 */
928void iommu_group_remove_device(struct device *dev)
929{
930 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100931 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600932
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600933 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200934
Alex Williamsond72e31c2012-05-30 14:18:53 -0600935 /* Pre-notify listeners that a device is being removed. */
936 blocking_notifier_call_chain(&group->notifier,
937 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
938
939 mutex_lock(&group->mutex);
940 list_for_each_entry(tmp_device, &group->devices, list) {
941 if (tmp_device->dev == dev) {
942 device = tmp_device;
943 list_del(&device->list);
944 break;
945 }
946 }
947 mutex_unlock(&group->mutex);
948
949 if (!device)
950 return;
951
952 sysfs_remove_link(group->devices_kobj, device->name);
953 sysfs_remove_link(&dev->kobj, "iommu_group");
954
Shuah Khan2e757082013-08-15 11:59:25 -0600955 trace_remove_device_from_group(group->id, dev);
956
Alex Williamsond72e31c2012-05-30 14:18:53 -0600957 kfree(device->name);
958 kfree(device);
959 dev->iommu_group = NULL;
960 kobject_put(group->devices_kobj);
961}
962EXPORT_SYMBOL_GPL(iommu_group_remove_device);
963
Joerg Roedel426a2732015-05-28 18:41:30 +0200964static int iommu_group_device_count(struct iommu_group *group)
965{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100966 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200967 int ret = 0;
968
969 list_for_each_entry(entry, &group->devices, list)
970 ret++;
971
972 return ret;
973}
974
Alex Williamsond72e31c2012-05-30 14:18:53 -0600975/**
976 * iommu_group_for_each_dev - iterate over each device in the group
977 * @group: the group
978 * @data: caller opaque data to be passed to callback function
979 * @fn: caller supplied callback function
980 *
981 * This function is called by group users to iterate over group devices.
982 * Callers should hold a reference count to the group during callback.
983 * The group->mutex is held across callbacks, which will block calls to
984 * iommu_group_add/remove_device.
985 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200986static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
987 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600988{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100989 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600990 int ret = 0;
991
Alex Williamsond72e31c2012-05-30 14:18:53 -0600992 list_for_each_entry(device, &group->devices, list) {
993 ret = fn(device->dev, data);
994 if (ret)
995 break;
996 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200997 return ret;
998}
999
1000
1001int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1002 int (*fn)(struct device *, void *))
1003{
1004 int ret;
1005
1006 mutex_lock(&group->mutex);
1007 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001008 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001009
Alex Williamsond72e31c2012-05-30 14:18:53 -06001010 return ret;
1011}
1012EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1013
1014/**
1015 * iommu_group_get - Return the group for a device and increment reference
1016 * @dev: get the group that this device belongs to
1017 *
1018 * This function is called by iommu drivers and users to get the group
1019 * for the specified device. If found, the group is returned and the group
1020 * reference in incremented, else NULL.
1021 */
1022struct iommu_group *iommu_group_get(struct device *dev)
1023{
1024 struct iommu_group *group = dev->iommu_group;
1025
1026 if (group)
1027 kobject_get(group->devices_kobj);
1028
1029 return group;
1030}
1031EXPORT_SYMBOL_GPL(iommu_group_get);
1032
1033/**
Robin Murphy13f59a72016-11-11 17:59:21 +00001034 * iommu_group_ref_get - Increment reference on a group
1035 * @group: the group to use, must not be NULL
1036 *
1037 * This function is called by iommu drivers to take additional references on an
1038 * existing group. Returns the given group for convenience.
1039 */
1040struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1041{
1042 kobject_get(group->devices_kobj);
1043 return group;
1044}
Will Deacona7ba5c32019-12-19 12:03:37 +00001045EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +00001046
1047/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001048 * iommu_group_put - Decrement group reference
1049 * @group: the group to use
1050 *
1051 * This function is called by iommu drivers and users to release the
1052 * iommu group. Once the reference count is zero, the group is released.
1053 */
1054void iommu_group_put(struct iommu_group *group)
1055{
1056 if (group)
1057 kobject_put(group->devices_kobj);
1058}
1059EXPORT_SYMBOL_GPL(iommu_group_put);
1060
1061/**
1062 * iommu_group_register_notifier - Register a notifier for group changes
1063 * @group: the group to watch
1064 * @nb: notifier block to signal
1065 *
1066 * This function allows iommu group users to track changes in a group.
1067 * See include/linux/iommu.h for actions sent via this notifier. Caller
1068 * should hold a reference to the group throughout notifier registration.
1069 */
1070int iommu_group_register_notifier(struct iommu_group *group,
1071 struct notifier_block *nb)
1072{
1073 return blocking_notifier_chain_register(&group->notifier, nb);
1074}
1075EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1076
1077/**
1078 * iommu_group_unregister_notifier - Unregister a notifier
1079 * @group: the group to watch
1080 * @nb: notifier block to signal
1081 *
1082 * Unregister a previously registered group notifier block.
1083 */
1084int iommu_group_unregister_notifier(struct iommu_group *group,
1085 struct notifier_block *nb)
1086{
1087 return blocking_notifier_chain_unregister(&group->notifier, nb);
1088}
1089EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1090
1091/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001092 * iommu_register_device_fault_handler() - Register a device fault handler
1093 * @dev: the device
1094 * @handler: the fault handler
1095 * @data: private data passed as argument to the handler
1096 *
1097 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001098 * fault event and data as argument. The handler should return 0 on success. If
1099 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1100 * complete the fault by calling iommu_page_response() with one of the following
1101 * response code:
1102 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1103 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1104 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1105 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001106 *
1107 * Return 0 if the fault handler was installed successfully, or an error.
1108 */
1109int iommu_register_device_fault_handler(struct device *dev,
1110 iommu_dev_fault_handler_t handler,
1111 void *data)
1112{
Joerg Roedel045a7042020-03-26 16:08:30 +01001113 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001114 int ret = 0;
1115
1116 if (!param)
1117 return -EINVAL;
1118
1119 mutex_lock(&param->lock);
1120 /* Only allow one fault handler registered for each device */
1121 if (param->fault_param) {
1122 ret = -EBUSY;
1123 goto done_unlock;
1124 }
1125
1126 get_device(dev);
1127 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1128 if (!param->fault_param) {
1129 put_device(dev);
1130 ret = -ENOMEM;
1131 goto done_unlock;
1132 }
1133 param->fault_param->handler = handler;
1134 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001135 mutex_init(&param->fault_param->lock);
1136 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001137
1138done_unlock:
1139 mutex_unlock(&param->lock);
1140
1141 return ret;
1142}
1143EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1144
1145/**
1146 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1147 * @dev: the device
1148 *
1149 * Remove the device fault handler installed with
1150 * iommu_register_device_fault_handler().
1151 *
1152 * Return 0 on success, or an error.
1153 */
1154int iommu_unregister_device_fault_handler(struct device *dev)
1155{
Joerg Roedel045a7042020-03-26 16:08:30 +01001156 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001157 int ret = 0;
1158
1159 if (!param)
1160 return -EINVAL;
1161
1162 mutex_lock(&param->lock);
1163
1164 if (!param->fault_param)
1165 goto unlock;
1166
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001167 /* we cannot unregister handler if there are pending faults */
1168 if (!list_empty(&param->fault_param->faults)) {
1169 ret = -EBUSY;
1170 goto unlock;
1171 }
1172
Jacob Pan0c830e62019-06-03 15:57:48 +01001173 kfree(param->fault_param);
1174 param->fault_param = NULL;
1175 put_device(dev);
1176unlock:
1177 mutex_unlock(&param->lock);
1178
1179 return ret;
1180}
1181EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1182
1183/**
1184 * iommu_report_device_fault() - Report fault event to device driver
1185 * @dev: the device
1186 * @evt: fault event data
1187 *
1188 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001189 * handler. When this function fails and the fault is recoverable, it is the
1190 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001191 *
1192 * Return 0 on success, or an error.
1193 */
1194int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1195{
Joerg Roedel045a7042020-03-26 16:08:30 +01001196 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001197 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001198 struct iommu_fault_param *fparam;
1199 int ret = 0;
1200
1201 if (!param || !evt)
1202 return -EINVAL;
1203
1204 /* we only report device fault if there is a handler registered */
1205 mutex_lock(&param->lock);
1206 fparam = param->fault_param;
1207 if (!fparam || !fparam->handler) {
1208 ret = -EINVAL;
1209 goto done_unlock;
1210 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001211
1212 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1213 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1214 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1215 GFP_KERNEL);
1216 if (!evt_pending) {
1217 ret = -ENOMEM;
1218 goto done_unlock;
1219 }
1220 mutex_lock(&fparam->lock);
1221 list_add_tail(&evt_pending->list, &fparam->faults);
1222 mutex_unlock(&fparam->lock);
1223 }
1224
Jacob Pan0c830e62019-06-03 15:57:48 +01001225 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001226 if (ret && evt_pending) {
1227 mutex_lock(&fparam->lock);
1228 list_del(&evt_pending->list);
1229 mutex_unlock(&fparam->lock);
1230 kfree(evt_pending);
1231 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001232done_unlock:
1233 mutex_unlock(&param->lock);
1234 return ret;
1235}
1236EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1237
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001238int iommu_page_response(struct device *dev,
1239 struct iommu_page_response *msg)
1240{
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001241 bool needs_pasid;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001242 int ret = -EINVAL;
1243 struct iommu_fault_event *evt;
1244 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001245 struct dev_iommu *param = dev->iommu;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001246 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001247 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1248
1249 if (!domain || !domain->ops->page_response)
1250 return -ENODEV;
1251
1252 if (!param || !param->fault_param)
1253 return -EINVAL;
1254
1255 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1256 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1257 return -EINVAL;
1258
1259 /* Only send response if there is a fault report pending */
1260 mutex_lock(&param->fault_param->lock);
1261 if (list_empty(&param->fault_param->faults)) {
1262 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1263 goto done_unlock;
1264 }
1265 /*
1266 * Check if we have a matching page request pending to respond,
1267 * otherwise return -EINVAL
1268 */
1269 list_for_each_entry(evt, &param->fault_param->faults, list) {
1270 prm = &evt->fault.prm;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001271 if (prm->grpid != msg->grpid)
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001272 continue;
1273
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001274 /*
1275 * If the PASID is required, the corresponding request is
1276 * matched using the group ID, the PASID valid bit and the PASID
1277 * value. Otherwise only the group ID matches request and
1278 * response.
1279 */
1280 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1281 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1282 continue;
1283
1284 if (!needs_pasid && has_pasid) {
1285 /* No big deal, just clear it. */
1286 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1287 msg->pasid = 0;
1288 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001289
1290 ret = domain->ops->page_response(dev, evt, msg);
1291 list_del(&evt->list);
1292 kfree(evt);
1293 break;
1294 }
1295
1296done_unlock:
1297 mutex_unlock(&param->fault_param->lock);
1298 return ret;
1299}
1300EXPORT_SYMBOL_GPL(iommu_page_response);
1301
Jacob Pan0c830e62019-06-03 15:57:48 +01001302/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001303 * iommu_group_id - Return ID for a group
1304 * @group: the group to ID
1305 *
1306 * Return the unique ID for the group matching the sysfs group number.
1307 */
1308int iommu_group_id(struct iommu_group *group)
1309{
1310 return group->id;
1311}
1312EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001313
Alex Williamsonf096c062014-09-19 10:03:06 -06001314static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1315 unsigned long *devfns);
1316
Alex Williamson104a1c12014-07-03 09:51:18 -06001317/*
1318 * To consider a PCI device isolated, we require ACS to support Source
1319 * Validation, Request Redirection, Completer Redirection, and Upstream
1320 * Forwarding. This effectively means that devices cannot spoof their
1321 * requester ID, requests and completions cannot be redirected, and all
1322 * transactions are forwarded upstream, even as it passes through a
1323 * bridge where the target device is downstream.
1324 */
1325#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1326
Alex Williamsonf096c062014-09-19 10:03:06 -06001327/*
1328 * For multifunction devices which are not isolated from each other, find
1329 * all the other non-isolated functions and look for existing groups. For
1330 * each function, we also need to look for aliases to or from other devices
1331 * that may already have a group.
1332 */
1333static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1334 unsigned long *devfns)
1335{
1336 struct pci_dev *tmp = NULL;
1337 struct iommu_group *group;
1338
1339 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1340 return NULL;
1341
1342 for_each_pci_dev(tmp) {
1343 if (tmp == pdev || tmp->bus != pdev->bus ||
1344 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1345 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1346 continue;
1347
1348 group = get_pci_alias_group(tmp, devfns);
1349 if (group) {
1350 pci_dev_put(tmp);
1351 return group;
1352 }
1353 }
1354
1355 return NULL;
1356}
1357
1358/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001359 * Look for aliases to or from the given device for existing groups. DMA
1360 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001361 * space is quite small (especially since we're really only looking at pcie
1362 * device, and therefore only expect multiple slots on the root complex or
1363 * downstream switch ports). It's conceivable though that a pair of
1364 * multifunction devices could have aliases between them that would cause a
1365 * loop. To prevent this, we use a bitmap to track where we've been.
1366 */
1367static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1368 unsigned long *devfns)
1369{
1370 struct pci_dev *tmp = NULL;
1371 struct iommu_group *group;
1372
1373 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1374 return NULL;
1375
1376 group = iommu_group_get(&pdev->dev);
1377 if (group)
1378 return group;
1379
1380 for_each_pci_dev(tmp) {
1381 if (tmp == pdev || tmp->bus != pdev->bus)
1382 continue;
1383
1384 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001385 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001386 group = get_pci_alias_group(tmp, devfns);
1387 if (group) {
1388 pci_dev_put(tmp);
1389 return group;
1390 }
1391
1392 group = get_pci_function_alias_group(tmp, devfns);
1393 if (group) {
1394 pci_dev_put(tmp);
1395 return group;
1396 }
1397 }
1398 }
1399
1400 return NULL;
1401}
1402
Alex Williamson104a1c12014-07-03 09:51:18 -06001403struct group_for_pci_data {
1404 struct pci_dev *pdev;
1405 struct iommu_group *group;
1406};
1407
1408/*
1409 * DMA alias iterator callback, return the last seen device. Stop and return
1410 * the IOMMU group if we find one along the way.
1411 */
1412static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1413{
1414 struct group_for_pci_data *data = opaque;
1415
1416 data->pdev = pdev;
1417 data->group = iommu_group_get(&pdev->dev);
1418
1419 return data->group != NULL;
1420}
1421
1422/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001423 * Generic device_group call-back function. It just allocates one
1424 * iommu-group per device.
1425 */
1426struct iommu_group *generic_device_group(struct device *dev)
1427{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001428 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001429}
Will Deacona7ba5c32019-12-19 12:03:37 +00001430EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001431
1432/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001433 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1434 * to find or create an IOMMU group for a device.
1435 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001436struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001437{
Joerg Roedel5e622922015-10-21 23:51:37 +02001438 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001439 struct group_for_pci_data data;
1440 struct pci_bus *bus;
1441 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001442 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001443
Joerg Roedel5e622922015-10-21 23:51:37 +02001444 if (WARN_ON(!dev_is_pci(dev)))
1445 return ERR_PTR(-EINVAL);
1446
Alex Williamson104a1c12014-07-03 09:51:18 -06001447 /*
1448 * Find the upstream DMA alias for the device. A device must not
1449 * be aliased due to topology in order to have its own IOMMU group.
1450 * If we find an alias along the way that already belongs to a
1451 * group, use it.
1452 */
1453 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1454 return data.group;
1455
1456 pdev = data.pdev;
1457
1458 /*
1459 * Continue upstream from the point of minimum IOMMU granularity
1460 * due to aliases to the point where devices are protected from
1461 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1462 * group, use it.
1463 */
1464 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1465 if (!bus->self)
1466 continue;
1467
1468 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1469 break;
1470
1471 pdev = bus->self;
1472
1473 group = iommu_group_get(&pdev->dev);
1474 if (group)
1475 return group;
1476 }
1477
1478 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001479 * Look for existing groups on device aliases. If we alias another
1480 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001481 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001482 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1483 if (group)
1484 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001485
1486 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001487 * Look for existing groups on non-isolated functions on the same
1488 * slot and aliases of those funcions, if any. No need to clear
1489 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001490 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001491 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1492 if (group)
1493 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001494
1495 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001496 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001497}
Will Deacona7ba5c32019-12-19 12:03:37 +00001498EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001499
Nipun Guptaeab03e22018-09-10 19:19:18 +05301500/* Get the IOMMU group for device on fsl-mc bus */
1501struct iommu_group *fsl_mc_device_group(struct device *dev)
1502{
1503 struct device *cont_dev = fsl_mc_cont_dev(dev);
1504 struct iommu_group *group;
1505
1506 group = iommu_group_get(cont_dev);
1507 if (!group)
1508 group = iommu_group_alloc();
1509 return group;
1510}
Will Deacona7ba5c32019-12-19 12:03:37 +00001511EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301512
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001513static int iommu_get_def_domain_type(struct device *dev)
1514{
1515 const struct iommu_ops *ops = dev->bus->iommu_ops;
Lu Baolu28b41e22020-11-24 21:06:01 +08001516
1517 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
1518 return IOMMU_DOMAIN_DMA;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001519
1520 if (ops->def_domain_type)
Lu Baolu28b41e22020-11-24 21:06:01 +08001521 return ops->def_domain_type(dev);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001522
Lu Baolu28b41e22020-11-24 21:06:01 +08001523 return 0;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001524}
1525
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001526static int iommu_group_alloc_default_domain(struct bus_type *bus,
1527 struct iommu_group *group,
1528 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001529{
1530 struct iommu_domain *dom;
1531
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001532 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001533 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001534 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1535 if (dom)
1536 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1537 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001538 }
1539
1540 if (!dom)
1541 return -ENOMEM;
1542
1543 group->default_domain = dom;
1544 if (!group->domain)
1545 group->domain = dom;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001546 return 0;
1547}
1548
Joerg Roedel79659190e2020-05-25 15:01:22 +02001549static int iommu_alloc_default_domain(struct iommu_group *group,
1550 struct device *dev)
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001551{
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001552 unsigned int type;
1553
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001554 if (group->default_domain)
1555 return 0;
1556
Lu Baolu28b41e22020-11-24 21:06:01 +08001557 type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001558
1559 return iommu_group_alloc_default_domain(dev->bus, group, type);
1560}
1561
Alex Williamson104a1c12014-07-03 09:51:18 -06001562/**
1563 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1564 * @dev: target device
1565 *
1566 * This function is intended to be called by IOMMU drivers and extended to
1567 * support common, bus-defined algorithms when determining or creating the
1568 * IOMMU group for a device. On success, the caller will hold a reference
1569 * to the returned IOMMU group, which will already include the provided
1570 * device. The reference should be released with iommu_group_put().
1571 */
Joerg Roedel1b032ec2020-04-29 15:37:12 +02001572static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001573{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001574 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001575 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001576 int ret;
1577
1578 group = iommu_group_get(dev);
1579 if (group)
1580 return group;
1581
Robin Murphy05f803002017-07-21 13:12:38 +01001582 if (!ops)
1583 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001584
Robin Murphy05f803002017-07-21 13:12:38 +01001585 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001586 if (WARN_ON_ONCE(group == NULL))
1587 return ERR_PTR(-EINVAL);
1588
Alex Williamson104a1c12014-07-03 09:51:18 -06001589 if (IS_ERR(group))
1590 return group;
1591
1592 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001593 if (ret)
1594 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001595
1596 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001597
1598out_put_group:
1599 iommu_group_put(group);
1600
1601 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001602}
1603
Joerg Roedel6827ca82015-05-28 18:41:35 +02001604struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1605{
1606 return group->default_domain;
1607}
1608
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001609static int probe_iommu_group(struct device *dev, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001610{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001611 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001612 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001613 int ret;
Joerg Roedel38667f12015-06-29 10:16:08 +02001614
Joerg Roedel5012c392020-04-29 15:36:51 +02001615 /* Device is probed already if in a group */
1616 group = iommu_group_get(dev);
1617 if (group) {
1618 iommu_group_put(group);
1619 return 0;
1620 }
1621
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001622 ret = __iommu_probe_device(dev, group_list);
Joerg Roedel38667f12015-06-29 10:16:08 +02001623 if (ret == -ENODEV)
1624 ret = 0;
1625
1626 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001627}
1628
Joerg Roedel8da30142015-05-28 18:41:27 +02001629static int remove_iommu_group(struct device *dev, void *data)
1630{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001631 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001632
1633 return 0;
1634}
1635
Alex Williamsond72e31c2012-05-30 14:18:53 -06001636static int iommu_bus_notifier(struct notifier_block *nb,
1637 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001638{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001639 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001640 struct device *dev = data;
1641 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001642
Alex Williamsond72e31c2012-05-30 14:18:53 -06001643 /*
1644 * ADD/DEL call into iommu driver ops if provided, which may
1645 * result in ADD/DEL notifiers to group->notifier
1646 */
1647 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001648 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001649
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001650 ret = iommu_probe_device(dev);
1651 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001652 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001653 iommu_release_device(dev);
1654 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001655 }
Alex Williamson14604322011-10-21 15:56:05 -04001656
Alex Williamsond72e31c2012-05-30 14:18:53 -06001657 /*
1658 * Remaining BUS_NOTIFYs get filtered and republished to the
1659 * group, if anyone is listening
1660 */
1661 group = iommu_group_get(dev);
1662 if (!group)
1663 return 0;
1664
1665 switch (action) {
1666 case BUS_NOTIFY_BIND_DRIVER:
1667 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1668 break;
1669 case BUS_NOTIFY_BOUND_DRIVER:
1670 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1671 break;
1672 case BUS_NOTIFY_UNBIND_DRIVER:
1673 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1674 break;
1675 case BUS_NOTIFY_UNBOUND_DRIVER:
1676 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1677 break;
1678 }
1679
1680 if (group_action)
1681 blocking_notifier_call_chain(&group->notifier,
1682 group_action, dev);
1683
1684 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001685 return 0;
1686}
1687
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001688struct __group_domain_type {
1689 struct device *dev;
1690 unsigned int type;
1691};
1692
1693static int probe_get_default_domain_type(struct device *dev, void *data)
1694{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001695 struct __group_domain_type *gtype = data;
Lu Baolu28b41e22020-11-24 21:06:01 +08001696 unsigned int type = iommu_get_def_domain_type(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001697
1698 if (type) {
1699 if (gtype->type && gtype->type != type) {
1700 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1701 iommu_domain_type_str(type),
1702 dev_name(gtype->dev),
1703 iommu_domain_type_str(gtype->type));
1704 gtype->type = 0;
1705 }
1706
1707 if (!gtype->dev) {
1708 gtype->dev = dev;
1709 gtype->type = type;
1710 }
1711 }
1712
1713 return 0;
1714}
1715
1716static void probe_alloc_default_domain(struct bus_type *bus,
1717 struct iommu_group *group)
1718{
1719 struct __group_domain_type gtype;
1720
1721 memset(&gtype, 0, sizeof(gtype));
1722
1723 /* Ask for default domain requirements of all devices in the group */
1724 __iommu_group_for_each_dev(group, &gtype,
1725 probe_get_default_domain_type);
1726
1727 if (!gtype.type)
1728 gtype.type = iommu_def_domain_type;
1729
1730 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001731
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001732}
1733
1734static int iommu_group_do_dma_attach(struct device *dev, void *data)
1735{
1736 struct iommu_domain *domain = data;
Joerg Roedel431275a2020-06-04 11:19:44 +02001737 int ret = 0;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001738
Joerg Roedel431275a2020-06-04 11:19:44 +02001739 if (!iommu_is_attach_deferred(domain, dev))
1740 ret = __iommu_attach_device(domain, dev);
1741
1742 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001743}
1744
1745static int __iommu_group_dma_attach(struct iommu_group *group)
1746{
1747 return __iommu_group_for_each_dev(group, group->default_domain,
1748 iommu_group_do_dma_attach);
1749}
1750
Joerg Roedel70b81702020-05-19 15:28:24 +02001751static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1752{
1753 struct iommu_domain *domain = data;
1754
1755 if (domain->ops->probe_finalize)
1756 domain->ops->probe_finalize(dev);
1757
1758 return 0;
1759}
1760
1761static void __iommu_group_dma_finalize(struct iommu_group *group)
1762{
1763 __iommu_group_for_each_dev(group, group->default_domain,
1764 iommu_group_do_probe_finalize);
1765}
Joerg Roedelcc69fc42020-06-02 10:32:04 +02001766
Joerg Roedelce574c22020-04-29 15:36:50 +02001767static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1768{
1769 struct iommu_group *group = data;
1770
1771 iommu_create_device_direct_mappings(group, dev);
1772
1773 return 0;
1774}
1775
1776static int iommu_group_create_direct_mappings(struct iommu_group *group)
1777{
1778 return __iommu_group_for_each_dev(group, group,
1779 iommu_do_create_direct_mappings);
1780}
1781
Joerg Roedel5012c392020-04-29 15:36:51 +02001782int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001783{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001784 struct iommu_group *group, *next;
1785 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001786 int ret;
1787
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001788 /*
1789 * This code-path does not allocate the default domain when
1790 * creating the iommu group, so do it after the groups are
1791 * created.
1792 */
1793 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1794 if (ret)
1795 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001796
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001797 list_for_each_entry_safe(group, next, &group_list, entry) {
1798 /* Remove item from the list */
1799 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001800
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001801 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001802
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001803 /* Try to allocate default domain */
1804 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001805
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001806 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001807 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001808 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001809 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001810
1811 iommu_group_create_direct_mappings(group);
1812
1813 ret = __iommu_group_dma_attach(group);
1814
1815 mutex_unlock(&group->mutex);
1816
1817 if (ret)
1818 break;
Joerg Roedel70b81702020-05-19 15:28:24 +02001819
1820 __iommu_group_dma_finalize(group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001821 }
1822
1823 return ret;
1824}
1825
Mark Salterfb3e3062014-09-21 13:58:24 -04001826static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001827{
Mark Salterfb3e3062014-09-21 13:58:24 -04001828 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001829 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001830
Mark Salterfb3e3062014-09-21 13:58:24 -04001831 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1832 if (!nb)
1833 return -ENOMEM;
1834
1835 nb->notifier_call = iommu_bus_notifier;
1836
1837 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001838 if (err)
1839 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001840
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001841 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001842 if (err)
1843 goto out_err;
1844
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001845
1846 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001847
1848out_err:
1849 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001850 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001851 bus_unregister_notifier(bus, nb);
1852
1853out_free:
1854 kfree(nb);
1855
1856 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001857}
1858
Joerg Roedelff217762011-08-26 16:48:26 +02001859/**
1860 * bus_set_iommu - set iommu-callbacks for the bus
1861 * @bus: bus.
1862 * @ops: the callbacks provided by the iommu-driver
1863 *
1864 * This function is called by an iommu driver to set the iommu methods
1865 * used for a particular bus. Drivers for devices on that bus can use
1866 * the iommu-api after these ops are registered.
1867 * This special function is needed because IOMMUs are usually devices on
1868 * the bus itself, so the iommu drivers are not initialized when the bus
1869 * is set up. With this function the iommu-driver can set the iommu-ops
1870 * afterwards.
1871 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001872int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001873{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001874 int err;
1875
Will Deacon4312cf72019-12-19 12:03:43 +00001876 if (ops == NULL) {
1877 bus->iommu_ops = NULL;
1878 return 0;
1879 }
1880
Joerg Roedelff217762011-08-26 16:48:26 +02001881 if (bus->iommu_ops != NULL)
1882 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001883
Joerg Roedelff217762011-08-26 16:48:26 +02001884 bus->iommu_ops = ops;
1885
1886 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001887 err = iommu_bus_init(bus, ops);
1888 if (err)
1889 bus->iommu_ops = NULL;
1890
1891 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001892}
Joerg Roedelff217762011-08-26 16:48:26 +02001893EXPORT_SYMBOL_GPL(bus_set_iommu);
1894
Joerg Roedela1b60c12011-09-06 18:46:34 +02001895bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001896{
Joerg Roedel94441c32011-09-06 18:58:54 +02001897 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001898}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001899EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001900
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001901bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1902{
1903 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1904 return false;
1905
1906 return bus->iommu_ops->capable(cap);
1907}
1908EXPORT_SYMBOL_GPL(iommu_capable);
1909
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001910/**
1911 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1912 * @domain: iommu domain
1913 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001914 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001915 *
1916 * This function should be used by IOMMU users which want to be notified
1917 * whenever an IOMMU fault happens.
1918 *
1919 * The fault handler itself should return 0 on success, and an appropriate
1920 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001921 */
1922void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001923 iommu_fault_handler_t handler,
1924 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001925{
1926 BUG_ON(!domain);
1927
1928 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001929 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001930}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001931EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001932
Joerg Roedel53723dc2015-05-28 18:41:29 +02001933static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1934 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001935{
1936 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001937
Joerg Roedel94441c32011-09-06 18:58:54 +02001938 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001939 return NULL;
1940
Joerg Roedel53723dc2015-05-28 18:41:29 +02001941 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001942 if (!domain)
1943 return NULL;
1944
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001945 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001946 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001947 /* Assume all sizes by default; the driver may override this later */
1948 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001949
Robin Murphy46983fc2021-08-11 13:21:15 +01001950 /* Temporarily avoid -EEXIST while drivers still get their own cookies */
Robin Murphybf3aed462021-08-11 13:21:30 +01001951 if (iommu_is_dma_domain(domain) && !domain->iova_cookie && iommu_get_dma_cookie(domain)) {
Robin Murphy46983fc2021-08-11 13:21:15 +01001952 iommu_domain_free(domain);
1953 domain = NULL;
1954 }
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001955 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001956}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001957
Joerg Roedel53723dc2015-05-28 18:41:29 +02001958struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1959{
1960 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001961}
1962EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1963
1964void iommu_domain_free(struct iommu_domain *domain)
1965{
Robin Murphy46983fc2021-08-11 13:21:15 +01001966 iommu_put_dma_cookie(domain);
Joerg Roedel89be34a2015-03-26 13:43:19 +01001967 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001968}
1969EXPORT_SYMBOL_GPL(iommu_domain_free);
1970
Joerg Roedel426a2732015-05-28 18:41:30 +02001971static int __iommu_attach_device(struct iommu_domain *domain,
1972 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001973{
Shuah Khanb54db772013-08-15 11:59:26 -06001974 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001975
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001976 if (unlikely(domain->ops->attach_dev == NULL))
1977 return -ENODEV;
1978
Shuah Khanb54db772013-08-15 11:59:26 -06001979 ret = domain->ops->attach_dev(domain, dev);
1980 if (!ret)
1981 trace_attach_device_to_domain(dev);
1982 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001983}
Joerg Roedel426a2732015-05-28 18:41:30 +02001984
1985int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1986{
1987 struct iommu_group *group;
1988 int ret;
1989
1990 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001991 if (!group)
1992 return -ENODEV;
1993
Joerg Roedel426a2732015-05-28 18:41:30 +02001994 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001995 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001996 * change while we are attaching
1997 */
1998 mutex_lock(&group->mutex);
1999 ret = -EINVAL;
2000 if (iommu_group_device_count(group) != 1)
2001 goto out_unlock;
2002
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002003 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002004
2005out_unlock:
2006 mutex_unlock(&group->mutex);
2007 iommu_group_put(group);
2008
2009 return ret;
2010}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002011EXPORT_SYMBOL_GPL(iommu_attach_device);
2012
Lianbo Jiang3ab65722021-01-26 19:53:37 +08002013int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2014{
2015 const struct iommu_ops *ops = domain->ops;
2016
2017 if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
2018 return __iommu_attach_device(domain, dev);
2019
2020 return 0;
2021}
2022
Jacob Pand9057382020-09-25 09:32:46 -07002023/*
2024 * Check flags and other user provided data for valid combinations. We also
2025 * make sure no reserved fields or unused flags are set. This is to ensure
2026 * not breaking userspace in the future when these fields or flags are used.
2027 */
2028static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)
Yi L Liu4c7c1712019-10-02 12:42:40 -07002029{
Jacob Pand9057382020-09-25 09:32:46 -07002030 u32 mask;
2031 int i;
2032
2033 if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)
2034 return -EINVAL;
2035
2036 mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;
2037 if (info->cache & ~mask)
2038 return -EINVAL;
2039
2040 if (info->granularity >= IOMMU_INV_GRANU_NR)
2041 return -EINVAL;
2042
2043 switch (info->granularity) {
2044 case IOMMU_INV_GRANU_ADDR:
2045 if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)
2046 return -EINVAL;
2047
2048 mask = IOMMU_INV_ADDR_FLAGS_PASID |
2049 IOMMU_INV_ADDR_FLAGS_ARCHID |
2050 IOMMU_INV_ADDR_FLAGS_LEAF;
2051
2052 if (info->granu.addr_info.flags & ~mask)
2053 return -EINVAL;
2054 break;
2055 case IOMMU_INV_GRANU_PASID:
2056 mask = IOMMU_INV_PASID_FLAGS_PASID |
2057 IOMMU_INV_PASID_FLAGS_ARCHID;
2058 if (info->granu.pasid_info.flags & ~mask)
2059 return -EINVAL;
2060
2061 break;
2062 case IOMMU_INV_GRANU_DOMAIN:
2063 if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)
2064 return -EINVAL;
2065 break;
2066 default:
2067 return -EINVAL;
2068 }
2069
2070 /* Check reserved padding fields */
2071 for (i = 0; i < sizeof(info->padding); i++) {
2072 if (info->padding[i])
2073 return -EINVAL;
2074 }
2075
2076 return 0;
2077}
2078
2079int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,
2080 void __user *uinfo)
2081{
2082 struct iommu_cache_invalidate_info inv_info = { 0 };
2083 u32 minsz;
2084 int ret;
2085
Yi L Liu4c7c1712019-10-02 12:42:40 -07002086 if (unlikely(!domain->ops->cache_invalidate))
2087 return -ENODEV;
2088
Jacob Pand9057382020-09-25 09:32:46 -07002089 /*
2090 * No new spaces can be added before the variable sized union, the
2091 * minimum size is the offset to the union.
2092 */
2093 minsz = offsetof(struct iommu_cache_invalidate_info, granu);
Yi L Liu4c7c1712019-10-02 12:42:40 -07002094
Jacob Pand9057382020-09-25 09:32:46 -07002095 /* Copy minsz from user to get flags and argsz */
2096 if (copy_from_user(&inv_info, uinfo, minsz))
2097 return -EFAULT;
2098
2099 /* Fields before the variable size union are mandatory */
2100 if (inv_info.argsz < minsz)
2101 return -EINVAL;
2102
2103 /* PASID and address granu require additional info beyond minsz */
2104 if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&
2105 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))
2106 return -EINVAL;
2107
2108 if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&
2109 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))
2110 return -EINVAL;
2111
2112 /*
2113 * User might be using a newer UAPI header which has a larger data
2114 * size, we shall support the existing flags within the current
2115 * size. Copy the remaining user data _after_ minsz but not more
2116 * than the current kernel supported size.
2117 */
2118 if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,
2119 min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))
2120 return -EFAULT;
2121
2122 /* Now the argsz is validated, check the content */
2123 ret = iommu_check_cache_invl_data(&inv_info);
2124 if (ret)
2125 return ret;
2126
2127 return domain->ops->cache_invalidate(domain, dev, &inv_info);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002128}
Jacob Pan23cc3492020-09-25 09:32:45 -07002129EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002130
Jacob Pand9057382020-09-25 09:32:46 -07002131static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)
Jacob Pan808be0a2019-10-02 12:42:43 -07002132{
Dan Carpenter4dd6ce42020-11-03 13:16:23 +03002133 u64 mask;
Jacob Pand9057382020-09-25 09:32:46 -07002134 int i;
2135
2136 if (data->version != IOMMU_GPASID_BIND_VERSION_1)
2137 return -EINVAL;
2138
2139 /* Check the range of supported formats */
2140 if (data->format >= IOMMU_PASID_FORMAT_LAST)
2141 return -EINVAL;
2142
2143 /* Check all flags */
2144 mask = IOMMU_SVA_GPASID_VAL;
2145 if (data->flags & ~mask)
2146 return -EINVAL;
2147
2148 /* Check reserved padding fields */
2149 for (i = 0; i < sizeof(data->padding); i++) {
2150 if (data->padding[i])
2151 return -EINVAL;
2152 }
2153
2154 return 0;
2155}
2156
2157static int iommu_sva_prepare_bind_data(void __user *udata,
2158 struct iommu_gpasid_bind_data *data)
2159{
2160 u32 minsz;
2161
2162 /*
2163 * No new spaces can be added before the variable sized union, the
2164 * minimum size is the offset to the union.
2165 */
2166 minsz = offsetof(struct iommu_gpasid_bind_data, vendor);
2167
2168 /* Copy minsz from user to get flags and argsz */
2169 if (copy_from_user(data, udata, minsz))
2170 return -EFAULT;
2171
2172 /* Fields before the variable size union are mandatory */
2173 if (data->argsz < minsz)
2174 return -EINVAL;
2175 /*
2176 * User might be using a newer UAPI header, we shall let IOMMU vendor
2177 * driver decide on what size it needs. Since the guest PASID bind data
2178 * can be vendor specific, larger argsz could be the result of extension
2179 * for one vendor but it should not affect another vendor.
2180 * Copy the remaining user data _after_ minsz
2181 */
2182 if (copy_from_user((void *)data + minsz, udata + minsz,
2183 min_t(u32, data->argsz, sizeof(*data)) - minsz))
2184 return -EFAULT;
2185
2186 return iommu_check_bind_data(data);
2187}
2188
2189int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
2190 void __user *udata)
2191{
2192 struct iommu_gpasid_bind_data data = { 0 };
2193 int ret;
2194
Jacob Pan808be0a2019-10-02 12:42:43 -07002195 if (unlikely(!domain->ops->sva_bind_gpasid))
2196 return -ENODEV;
2197
Jacob Pand9057382020-09-25 09:32:46 -07002198 ret = iommu_sva_prepare_bind_data(udata, &data);
2199 if (ret)
2200 return ret;
2201
2202 return domain->ops->sva_bind_gpasid(domain, dev, &data);
Jacob Pan808be0a2019-10-02 12:42:43 -07002203}
Jacob Pan23cc3492020-09-25 09:32:45 -07002204EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
Jacob Pan808be0a2019-10-02 12:42:43 -07002205
2206int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2207 ioasid_t pasid)
2208{
2209 if (unlikely(!domain->ops->sva_unbind_gpasid))
2210 return -ENODEV;
2211
2212 return domain->ops->sva_unbind_gpasid(dev, pasid);
2213}
2214EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
2215
Jacob Pand9057382020-09-25 09:32:46 -07002216int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2217 void __user *udata)
2218{
2219 struct iommu_gpasid_bind_data data = { 0 };
2220 int ret;
2221
2222 if (unlikely(!domain->ops->sva_bind_gpasid))
2223 return -ENODEV;
2224
2225 ret = iommu_sva_prepare_bind_data(udata, &data);
2226 if (ret)
2227 return ret;
2228
2229 return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
2230}
Jacob Pan23cc3492020-09-25 09:32:45 -07002231EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002232
2233static void __iommu_detach_device(struct iommu_domain *domain,
2234 struct device *dev)
2235{
Joerg Roedelbd421262020-05-19 15:03:40 +02002236 if (iommu_is_attach_deferred(domain, dev))
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002237 return;
2238
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002239 if (unlikely(domain->ops->detach_dev == NULL))
2240 return;
2241
2242 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06002243 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002244}
Joerg Roedel426a2732015-05-28 18:41:30 +02002245
2246void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2247{
2248 struct iommu_group *group;
2249
2250 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07002251 if (!group)
2252 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02002253
2254 mutex_lock(&group->mutex);
2255 if (iommu_group_device_count(group) != 1) {
2256 WARN_ON(1);
2257 goto out_unlock;
2258 }
2259
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002260 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002261
2262out_unlock:
2263 mutex_unlock(&group->mutex);
2264 iommu_group_put(group);
2265}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002266EXPORT_SYMBOL_GPL(iommu_detach_device);
2267
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002268struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2269{
2270 struct iommu_domain *domain;
2271 struct iommu_group *group;
2272
2273 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002274 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002275 return NULL;
2276
2277 domain = group->domain;
2278
2279 iommu_group_put(group);
2280
2281 return domain;
2282}
2283EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2284
Alex Williamsond72e31c2012-05-30 14:18:53 -06002285/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002286 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2287 * guarantees that the group and its default domain are valid and correct.
2288 */
2289struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2290{
2291 return dev->iommu_group->default_domain;
2292}
2293
2294/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002295 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002296 * the IOMMU API works on domains and devices. Bridge that gap by
2297 * iterating over the devices in a group. Ideally we'd have a single
2298 * device which represents the requestor ID of the group, but we also
2299 * allow IOMMU drivers to create policy defined minimum sets, where
2300 * the physical hardware may be able to distiguish members, but we
2301 * wish to group them at a higher level (ex. untrusted multi-function
2302 * PCI devices). Thus we attach each device.
2303 */
2304static int iommu_group_do_attach_device(struct device *dev, void *data)
2305{
2306 struct iommu_domain *domain = data;
2307
Joerg Roedel426a2732015-05-28 18:41:30 +02002308 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002309}
2310
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002311static int __iommu_attach_group(struct iommu_domain *domain,
2312 struct iommu_group *group)
2313{
2314 int ret;
2315
2316 if (group->default_domain && group->domain != group->default_domain)
2317 return -EBUSY;
2318
2319 ret = __iommu_group_for_each_dev(group, domain,
2320 iommu_group_do_attach_device);
2321 if (ret == 0)
2322 group->domain = domain;
2323
2324 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002325}
2326
2327int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2328{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002329 int ret;
2330
2331 mutex_lock(&group->mutex);
2332 ret = __iommu_attach_group(domain, group);
2333 mutex_unlock(&group->mutex);
2334
2335 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002336}
2337EXPORT_SYMBOL_GPL(iommu_attach_group);
2338
2339static int iommu_group_do_detach_device(struct device *dev, void *data)
2340{
2341 struct iommu_domain *domain = data;
2342
Joerg Roedel426a2732015-05-28 18:41:30 +02002343 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002344
2345 return 0;
2346}
2347
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002348static void __iommu_detach_group(struct iommu_domain *domain,
2349 struct iommu_group *group)
2350{
2351 int ret;
2352
2353 if (!group->default_domain) {
2354 __iommu_group_for_each_dev(group, domain,
2355 iommu_group_do_detach_device);
2356 group->domain = NULL;
2357 return;
2358 }
2359
2360 if (group->domain == group->default_domain)
2361 return;
2362
2363 /* Detach by re-attaching to the default domain */
2364 ret = __iommu_group_for_each_dev(group, group->default_domain,
2365 iommu_group_do_attach_device);
2366 if (ret != 0)
2367 WARN_ON(1);
2368 else
2369 group->domain = group->default_domain;
2370}
2371
Alex Williamsond72e31c2012-05-30 14:18:53 -06002372void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2373{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002374 mutex_lock(&group->mutex);
2375 __iommu_detach_group(domain, group);
2376 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002377}
2378EXPORT_SYMBOL_GPL(iommu_detach_group);
2379
Varun Sethibb5547a2013-03-29 01:23:58 +05302380phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002381{
Robin Murphy13b6eb62021-07-15 14:04:24 +01002382 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2383 return iova;
2384
2385 if (domain->type == IOMMU_DOMAIN_BLOCKED)
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002386 return 0;
2387
2388 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002389}
2390EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002391
Will Deacon89d5b962021-06-16 06:38:47 -07002392static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
Will Deaconb1d99dc2021-06-16 06:38:48 -07002393 phys_addr_t paddr, size_t size, size_t *count)
Alex Williamsonbd139692013-06-17 19:57:34 -06002394{
Will Deaconb1d99dc2021-06-16 06:38:48 -07002395 unsigned int pgsize_idx, pgsize_idx_next;
Will Deacone7d6fff2021-06-16 06:38:46 -07002396 unsigned long pgsizes;
Will Deaconb1d99dc2021-06-16 06:38:48 -07002397 size_t offset, pgsize, pgsize_next;
Will Deacon89d5b962021-06-16 06:38:47 -07002398 unsigned long addr_merge = paddr | iova;
Alex Williamsonbd139692013-06-17 19:57:34 -06002399
Will Deacone7d6fff2021-06-16 06:38:46 -07002400 /* Page sizes supported by the hardware and small enough for @size */
2401 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
Alex Williamsonbd139692013-06-17 19:57:34 -06002402
Will Deacone7d6fff2021-06-16 06:38:46 -07002403 /* Constrain the page sizes further based on the maximum alignment */
2404 if (likely(addr_merge))
2405 pgsizes &= GENMASK(__ffs(addr_merge), 0);
Alex Williamsonbd139692013-06-17 19:57:34 -06002406
Will Deacone7d6fff2021-06-16 06:38:46 -07002407 /* Make sure we have at least one suitable page size */
2408 BUG_ON(!pgsizes);
Alex Williamsonbd139692013-06-17 19:57:34 -06002409
Will Deacone7d6fff2021-06-16 06:38:46 -07002410 /* Pick the biggest page size remaining */
2411 pgsize_idx = __fls(pgsizes);
2412 pgsize = BIT(pgsize_idx);
Will Deaconb1d99dc2021-06-16 06:38:48 -07002413 if (!count)
2414 return pgsize;
Alex Williamsonbd139692013-06-17 19:57:34 -06002415
Will Deaconb1d99dc2021-06-16 06:38:48 -07002416 /* Find the next biggest support page size, if it exists */
2417 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2418 if (!pgsizes)
2419 goto out_set_count;
2420
2421 pgsize_idx_next = __ffs(pgsizes);
2422 pgsize_next = BIT(pgsize_idx_next);
2423
2424 /*
2425 * There's no point trying a bigger page size unless the virtual
2426 * and physical addresses are similarly offset within the larger page.
2427 */
2428 if ((iova ^ paddr) & (pgsize_next - 1))
2429 goto out_set_count;
2430
2431 /* Calculate the offset to the next page size alignment boundary */
2432 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2433
2434 /*
2435 * If size is big enough to accommodate the larger page, reduce
2436 * the number of smaller pages.
2437 */
2438 if (offset + pgsize_next <= size)
2439 size = offset;
2440
2441out_set_count:
2442 *count = size >> pgsize_idx;
Alex Williamsonbd139692013-06-17 19:57:34 -06002443 return pgsize;
2444}
2445
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002446static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova,
2447 phys_addr_t paddr, size_t size, int prot,
2448 gfp_t gfp, size_t *mapped)
2449{
2450 const struct iommu_ops *ops = domain->ops;
2451 size_t pgsize, count;
2452 int ret;
2453
2454 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2455
2456 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2457 iova, &paddr, pgsize, count);
2458
2459 if (ops->map_pages) {
2460 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2461 gfp, mapped);
2462 } else {
2463 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
2464 *mapped = ret ? 0 : pgsize;
2465 }
2466
2467 return ret;
2468}
2469
Wei Yongjun1b0b2a82020-07-13 22:25:42 +08002470static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2471 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002472{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002473 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002474 unsigned long orig_iova = iova;
2475 unsigned int min_pagesz;
2476 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002477 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002478 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002479
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002480 if (unlikely(!(ops->map || ops->map_pages) ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002481 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002482 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002483
Joerg Roedela10315e2015-03-26 13:43:06 +01002484 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2485 return -EINVAL;
2486
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002487 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002488 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002489
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002490 /*
2491 * both the virtual address and the physical one, as well as
2492 * the size of the mapping, must be aligned (at least) to the
2493 * size of the smallest page supported by the hardware
2494 */
2495 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002496 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002497 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002498 return -EINVAL;
2499 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002500
Fabio Estevamabedb042013-08-22 10:25:42 -03002501 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002502
2503 while (size) {
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002504 size_t mapped = 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002505
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002506 ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp,
2507 &mapped);
2508 /*
2509 * Some pages may have been mapped, even if an error occurred,
2510 * so we should account for those so they can be unmapped.
2511 */
2512 size -= mapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002513
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002514 if (ret)
2515 break;
2516
Isaac J. Manjarres647c5772021-06-16 06:38:49 -07002517 iova += mapped;
2518 paddr += mapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002519 }
2520
2521 /* unroll mapping in case something went wrong */
2522 if (ret)
2523 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002524 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002525 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002526
2527 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002528}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002529
Yong Wud8c1df02021-01-07 20:29:03 +08002530static int _iommu_map(struct iommu_domain *domain, unsigned long iova,
2531 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2532{
2533 const struct iommu_ops *ops = domain->ops;
2534 int ret;
2535
Douglas Andersonb8437a32021-02-01 17:06:23 -08002536 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
Yong Wud8c1df02021-01-07 20:29:03 +08002537 if (ret == 0 && ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002538 ops->iotlb_sync_map(domain, iova, size);
Yong Wud8c1df02021-01-07 20:29:03 +08002539
2540 return ret;
2541}
2542
Tom Murphy781ca2d2019-09-08 09:56:38 -07002543int iommu_map(struct iommu_domain *domain, unsigned long iova,
2544 phys_addr_t paddr, size_t size, int prot)
2545{
2546 might_sleep();
Yong Wud8c1df02021-01-07 20:29:03 +08002547 return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002548}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002549EXPORT_SYMBOL_GPL(iommu_map);
2550
Tom Murphy781ca2d2019-09-08 09:56:38 -07002551int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2552 phys_addr_t paddr, size_t size, int prot)
2553{
Yong Wud8c1df02021-01-07 20:29:03 +08002554 return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002555}
2556EXPORT_SYMBOL_GPL(iommu_map_atomic);
2557
Will Deaconb1d99dc2021-06-16 06:38:48 -07002558static size_t __iommu_unmap_pages(struct iommu_domain *domain,
2559 unsigned long iova, size_t size,
2560 struct iommu_iotlb_gather *iotlb_gather)
2561{
2562 const struct iommu_ops *ops = domain->ops;
2563 size_t pgsize, count;
2564
2565 pgsize = iommu_pgsize(domain, iova, iova, size, &count);
2566 return ops->unmap_pages ?
2567 ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) :
2568 ops->unmap(domain, iova, pgsize, iotlb_gather);
2569}
2570
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002571static size_t __iommu_unmap(struct iommu_domain *domain,
2572 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002573 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002574{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002575 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002576 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002577 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002578 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002579
Will Deaconb1d99dc2021-06-16 06:38:48 -07002580 if (unlikely(!(ops->unmap || ops->unmap_pages) ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002581 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002582 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002583
Joerg Roedela10315e2015-03-26 13:43:06 +01002584 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002585 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002586
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002587 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002588 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002589
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002590 /*
2591 * The virtual address, as well as the size of the mapping, must be
2592 * aligned (at least) to the size of the smallest page supported
2593 * by the hardware
2594 */
2595 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002596 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2597 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002598 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002599 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002600
Joe Perches6197ca82013-06-23 12:29:04 -07002601 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002602
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002603 /*
2604 * Keep iterating until we either unmap 'size' bytes (or more)
2605 * or we hit an area that isn't mapped.
2606 */
2607 while (unmapped < size) {
Will Deaconb1d99dc2021-06-16 06:38:48 -07002608 unmapped_page = __iommu_unmap_pages(domain, iova,
2609 size - unmapped,
2610 iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002611 if (!unmapped_page)
2612 break;
2613
Joe Perches6197ca82013-06-23 12:29:04 -07002614 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2615 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002616
2617 iova += unmapped_page;
2618 unmapped += unmapped_page;
2619 }
2620
Shuah Khandb8614d2015-01-16 20:53:17 -07002621 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002622 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002623}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002624
2625size_t iommu_unmap(struct iommu_domain *domain,
2626 unsigned long iova, size_t size)
2627{
Will Deacona7d20dc2019-07-02 16:43:48 +01002628 struct iommu_iotlb_gather iotlb_gather;
2629 size_t ret;
2630
2631 iommu_iotlb_gather_init(&iotlb_gather);
2632 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
Tom Murphyaae4c8e2020-08-17 22:00:49 +01002633 iommu_iotlb_sync(domain, &iotlb_gather);
Will Deacona7d20dc2019-07-02 16:43:48 +01002634
2635 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002636}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002637EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002638
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002639size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002640 unsigned long iova, size_t size,
2641 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002642{
Will Deacona7d20dc2019-07-02 16:43:48 +01002643 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002644}
2645EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2646
Baolin Wang99302642020-06-12 11:39:54 +08002647static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2648 struct scatterlist *sg, unsigned int nents, int prot,
2649 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002650{
Yong Wud8c1df02021-01-07 20:29:03 +08002651 const struct iommu_ops *ops = domain->ops;
Robin Murphy5d95f402018-10-11 16:56:42 +01002652 size_t len = 0, mapped = 0;
2653 phys_addr_t start;
2654 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002655 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002656
Robin Murphy5d95f402018-10-11 16:56:42 +01002657 while (i <= nents) {
2658 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002659
Robin Murphy5d95f402018-10-11 16:56:42 +01002660 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002661 ret = __iommu_map(domain, iova + mapped, start,
2662 len, prot, gfp);
2663
Robin Murphy5d95f402018-10-11 16:56:42 +01002664 if (ret)
2665 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002666
Robin Murphy5d95f402018-10-11 16:56:42 +01002667 mapped += len;
2668 len = 0;
2669 }
Robin Murphy18f23402014-11-25 17:50:55 +00002670
Robin Murphy5d95f402018-10-11 16:56:42 +01002671 if (len) {
2672 len += sg->length;
2673 } else {
2674 len = sg->length;
2675 start = s_phys;
2676 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002677
Robin Murphy5d95f402018-10-11 16:56:42 +01002678 if (++i < nents)
2679 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002680 }
2681
Yong Wud8c1df02021-01-07 20:29:03 +08002682 if (ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002683 ops->iotlb_sync_map(domain, iova, mapped);
Olav Haugan315786e2014-10-25 09:55:16 -07002684 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002685
2686out_err:
2687 /* undo mappings already done */
2688 iommu_unmap(domain, iova, mapped);
2689
2690 return 0;
2691
Olav Haugan315786e2014-10-25 09:55:16 -07002692}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002693
2694size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2695 struct scatterlist *sg, unsigned int nents, int prot)
2696{
2697 might_sleep();
2698 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2699}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002700EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002701
Tom Murphy781ca2d2019-09-08 09:56:38 -07002702size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2703 struct scatterlist *sg, unsigned int nents, int prot)
2704{
2705 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2706}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002707
Joerg Roedel207c6e32017-04-26 15:39:28 +02002708/**
2709 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2710 * @domain: the iommu domain where the fault has happened
2711 * @dev: the device where the fault has happened
2712 * @iova: the faulting address
2713 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2714 *
2715 * This function should be called by the low-level IOMMU implementations
2716 * whenever IOMMU faults happen, to allow high-level users, that are
2717 * interested in such events, to know about them.
2718 *
2719 * This event may be useful for several possible use cases:
2720 * - mere logging of the event
2721 * - dynamic TLB/PTE loading
2722 * - if restarting of the faulting device is required
2723 *
2724 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2725 * PTE/TLB loading will one day be supported, implementations will be able
2726 * to tell whether it succeeded or not according to this return value).
2727 *
2728 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2729 * (though fault handlers can also return -ENOSYS, in case they want to
2730 * elicit the default behavior of the IOMMU drivers).
2731 */
2732int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2733 unsigned long iova, int flags)
2734{
2735 int ret = -ENOSYS;
2736
2737 /*
2738 * if upper layers showed interest and installed a fault handler,
2739 * invoke it.
2740 */
2741 if (domain->handler)
2742 ret = domain->handler(domain, dev, iova, flags,
2743 domain->handler_token);
2744
2745 trace_io_page_fault(dev, iova, flags);
2746 return ret;
2747}
2748EXPORT_SYMBOL_GPL(report_iommu_fault);
2749
Alex Williamsond72e31c2012-05-30 14:18:53 -06002750static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002751{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002752 iommu_group_kset = kset_create_and_add("iommu_groups",
2753 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002754 BUG_ON(!iommu_group_kset);
2755
Gary R Hookbad614b2018-06-12 16:41:21 -05002756 iommu_debugfs_setup();
2757
Alex Williamsond72e31c2012-05-30 14:18:53 -06002758 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002759}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002760core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002761
Christoph Hellwig7e147542021-04-01 17:52:52 +02002762int iommu_enable_nesting(struct iommu_domain *domain)
2763{
2764 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2765 return -EINVAL;
2766 if (!domain->ops->enable_nesting)
2767 return -EINVAL;
2768 return domain->ops->enable_nesting(domain);
2769}
2770EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2771
Christoph Hellwig4fc52b82021-04-01 17:52:55 +02002772int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2773 unsigned long quirk)
2774{
2775 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2776 return -EINVAL;
2777 if (!domain->ops->set_pgtable_quirks)
2778 return -EINVAL;
2779 return domain->ops->set_pgtable_quirks(domain, quirk);
2780}
2781EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2782
Eric Augere5b52342017-01-19 20:57:47 +00002783void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002784{
2785 const struct iommu_ops *ops = dev->bus->iommu_ops;
2786
Eric Augere5b52342017-01-19 20:57:47 +00002787 if (ops && ops->get_resv_regions)
2788 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002789}
2790
Eric Augere5b52342017-01-19 20:57:47 +00002791void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002792{
2793 const struct iommu_ops *ops = dev->bus->iommu_ops;
2794
Eric Augere5b52342017-01-19 20:57:47 +00002795 if (ops && ops->put_resv_regions)
2796 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002797}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002798
Thierry Redingf9f69712019-12-18 14:42:01 +01002799/**
2800 * generic_iommu_put_resv_regions - Reserved region driver helper
2801 * @dev: device for which to free reserved regions
2802 * @list: reserved region list for device
2803 *
2804 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2805 * for simple reservations. Memory allocated for each reserved region will be
2806 * freed. If an IOMMU driver allocates additional resources per region, it is
2807 * going to have to implement a custom callback.
2808 */
2809void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2810{
2811 struct iommu_resv_region *entry, *next;
2812
2813 list_for_each_entry_safe(entry, next, list, list)
2814 kfree(entry);
2815}
2816EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2817
Eric Auger2b20cbb2017-01-19 20:57:49 +00002818struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002819 size_t length, int prot,
2820 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002821{
2822 struct iommu_resv_region *region;
2823
2824 region = kzalloc(sizeof(*region), GFP_KERNEL);
2825 if (!region)
2826 return NULL;
2827
2828 INIT_LIST_HEAD(&region->list);
2829 region->start = start;
2830 region->length = length;
2831 region->prot = prot;
2832 region->type = type;
2833 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002834}
Will Deacona7ba5c32019-12-19 12:03:37 +00002835EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002836
Joerg Roedel8a699612019-08-19 15:22:47 +02002837void iommu_set_default_passthrough(bool cmd_line)
2838{
2839 if (cmd_line)
Christoph Hellwig31897132021-04-01 17:52:53 +02002840 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
Joerg Roedel8a699612019-08-19 15:22:47 +02002841 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2842}
2843
2844void iommu_set_default_translated(bool cmd_line)
2845{
2846 if (cmd_line)
Christoph Hellwig31897132021-04-01 17:52:53 +02002847 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
Joerg Roedel8a699612019-08-19 15:22:47 +02002848 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2849}
2850
2851bool iommu_default_passthrough(void)
2852{
2853 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2854}
2855EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2856
Joerg Roedel534766d2017-01-31 16:58:42 +01002857const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002858{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002859 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002860 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002861
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002862 spin_lock(&iommu_device_lock);
2863 list_for_each_entry(iommu, &iommu_device_list, list)
2864 if (iommu->fwnode == fwnode) {
2865 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002866 break;
2867 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002868 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002869 return ops;
2870}
2871
Robin Murphy57f98d22016-09-13 10:54:14 +01002872int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2873 const struct iommu_ops *ops)
2874{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002875 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002876
2877 if (fwspec)
2878 return ops == fwspec->ops ? 0 : -EINVAL;
2879
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002880 if (!dev_iommu_get(dev))
2881 return -ENOMEM;
2882
Robin Murphy098accf2020-02-13 14:00:21 +00002883 /* Preallocate for the overwhelmingly common case of 1 ID */
2884 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002885 if (!fwspec)
2886 return -ENOMEM;
2887
2888 of_node_get(to_of_node(iommu_fwnode));
2889 fwspec->iommu_fwnode = iommu_fwnode;
2890 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002891 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002892 return 0;
2893}
2894EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2895
2896void iommu_fwspec_free(struct device *dev)
2897{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002898 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002899
2900 if (fwspec) {
2901 fwnode_handle_put(fwspec->iommu_fwnode);
2902 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002903 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002904 }
2905}
2906EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2907
2908int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2909{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002910 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002911 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002912
2913 if (!fwspec)
2914 return -EINVAL;
2915
Robin Murphy098accf2020-02-13 14:00:21 +00002916 new_num = fwspec->num_ids + num_ids;
2917 if (new_num > 1) {
2918 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2919 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002920 if (!fwspec)
2921 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002922
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002923 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002924 }
2925
2926 for (i = 0; i < num_ids; i++)
2927 fwspec->ids[fwspec->num_ids + i] = ids[i];
2928
Robin Murphy098accf2020-02-13 14:00:21 +00002929 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002930 return 0;
2931}
2932EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002933
2934/*
2935 * Per device IOMMU features.
2936 */
Lu Baolua3a19592019-03-25 09:30:28 +08002937int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2938{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002939 if (dev->iommu && dev->iommu->iommu_dev) {
2940 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002941
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002942 if (ops->dev_enable_feat)
2943 return ops->dev_enable_feat(dev, feat);
2944 }
Lu Baolua3a19592019-03-25 09:30:28 +08002945
2946 return -ENODEV;
2947}
2948EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2949
2950/*
2951 * The device drivers should do the necessary cleanups before calling this.
2952 * For example, before disabling the aux-domain feature, the device driver
2953 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2954 */
2955int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2956{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002957 if (dev->iommu && dev->iommu->iommu_dev) {
2958 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002959
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002960 if (ops->dev_disable_feat)
2961 return ops->dev_disable_feat(dev, feat);
2962 }
Lu Baolua3a19592019-03-25 09:30:28 +08002963
2964 return -EBUSY;
2965}
2966EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2967
2968bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2969{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002970 if (dev->iommu && dev->iommu->iommu_dev) {
2971 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002972
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002973 if (ops->dev_feat_enabled)
2974 return ops->dev_feat_enabled(dev, feat);
2975 }
Lu Baolua3a19592019-03-25 09:30:28 +08002976
2977 return false;
2978}
2979EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2980
2981/*
2982 * Aux-domain specific attach/detach.
2983 *
2984 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2985 * true. Also, as long as domains are attached to a device through this
2986 * interface, any tries to call iommu_attach_device() should fail
2987 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2988 * This should make us safe against a device being attached to a guest as a
2989 * whole while there are still pasid users on it (aux and sva).
2990 */
2991int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2992{
2993 int ret = -ENODEV;
2994
2995 if (domain->ops->aux_attach_dev)
2996 ret = domain->ops->aux_attach_dev(domain, dev);
2997
2998 if (!ret)
2999 trace_attach_device_to_domain(dev);
3000
3001 return ret;
3002}
3003EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
3004
3005void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
3006{
3007 if (domain->ops->aux_detach_dev) {
3008 domain->ops->aux_detach_dev(domain, dev);
3009 trace_detach_device_from_domain(dev);
3010 }
3011}
3012EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
3013
3014int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
3015{
3016 int ret = -ENODEV;
3017
3018 if (domain->ops->aux_get_pasid)
3019 ret = domain->ops->aux_get_pasid(domain, dev);
3020
3021 return ret;
3022}
3023EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003024
3025/**
3026 * iommu_sva_bind_device() - Bind a process address space to a device
3027 * @dev: the device
3028 * @mm: the mm to bind, caller must hold a reference to it
3029 *
3030 * Create a bond between device and address space, allowing the device to access
3031 * the mm using the returned PASID. If a bond already exists between @device and
3032 * @mm, it is returned and an additional reference is taken. Caller must call
3033 * iommu_sva_unbind_device() to release each reference.
3034 *
3035 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
3036 * initialize the required SVA features.
3037 *
3038 * On error, returns an ERR_PTR value.
3039 */
3040struct iommu_sva *
3041iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
3042{
3043 struct iommu_group *group;
3044 struct iommu_sva *handle = ERR_PTR(-EINVAL);
3045 const struct iommu_ops *ops = dev->bus->iommu_ops;
3046
3047 if (!ops || !ops->sva_bind)
3048 return ERR_PTR(-ENODEV);
3049
3050 group = iommu_group_get(dev);
3051 if (!group)
3052 return ERR_PTR(-ENODEV);
3053
3054 /* Ensure device count and domain don't change while we're binding */
3055 mutex_lock(&group->mutex);
3056
3057 /*
3058 * To keep things simple, SVA currently doesn't support IOMMU groups
3059 * with more than one device. Existing SVA-capable systems are not
3060 * affected by the problems that required IOMMU groups (lack of ACS
3061 * isolation, device ID aliasing and other hardware issues).
3062 */
3063 if (iommu_group_device_count(group) != 1)
3064 goto out_unlock;
3065
3066 handle = ops->sva_bind(dev, mm, drvdata);
3067
3068out_unlock:
3069 mutex_unlock(&group->mutex);
3070 iommu_group_put(group);
3071
3072 return handle;
3073}
3074EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
3075
3076/**
3077 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
3078 * @handle: the handle returned by iommu_sva_bind_device()
3079 *
3080 * Put reference to a bond between device and address space. The device should
3081 * not be issuing any more transaction for this PASID. All outstanding page
3082 * requests for this PASID must have been flushed to the IOMMU.
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003083 */
3084void iommu_sva_unbind_device(struct iommu_sva *handle)
3085{
3086 struct iommu_group *group;
3087 struct device *dev = handle->dev;
3088 const struct iommu_ops *ops = dev->bus->iommu_ops;
3089
3090 if (!ops || !ops->sva_unbind)
3091 return;
3092
3093 group = iommu_group_get(dev);
3094 if (!group)
3095 return;
3096
3097 mutex_lock(&group->mutex);
3098 ops->sva_unbind(handle);
3099 mutex_unlock(&group->mutex);
3100
3101 iommu_group_put(group);
3102}
3103EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
3104
Fenghua Yuc7b6bac2020-09-15 09:30:05 -07003105u32 iommu_sva_get_pasid(struct iommu_sva *handle)
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003106{
3107 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
3108
3109 if (!ops || !ops->sva_get_pasid)
3110 return IOMMU_PASID_INVALID;
3111
3112 return ops->sva_get_pasid(handle);
3113}
3114EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003115
3116/*
3117 * Changes the default domain of an iommu group that has *only* one device
3118 *
3119 * @group: The group for which the default domain should be changed
3120 * @prev_dev: The device in the group (this is used to make sure that the device
3121 * hasn't changed after the caller has called this function)
3122 * @type: The type of the new default domain that gets associated with the group
3123 *
3124 * Returns 0 on success and error code on failure
3125 *
3126 * Note:
3127 * 1. Presently, this function is called only when user requests to change the
3128 * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
3129 * Please take a closer look if intended to use for other purposes.
3130 */
3131static int iommu_change_dev_def_domain(struct iommu_group *group,
3132 struct device *prev_dev, int type)
3133{
3134 struct iommu_domain *prev_dom;
3135 struct group_device *grp_dev;
3136 int ret, dev_def_dom;
3137 struct device *dev;
3138
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003139 mutex_lock(&group->mutex);
3140
3141 if (group->default_domain != group->domain) {
3142 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n");
3143 ret = -EBUSY;
3144 goto out;
3145 }
3146
3147 /*
3148 * iommu group wasn't locked while acquiring device lock in
3149 * iommu_group_store_type(). So, make sure that the device count hasn't
3150 * changed while acquiring device lock.
3151 *
3152 * Changing default domain of an iommu group with two or more devices
3153 * isn't supported because there could be a potential deadlock. Consider
3154 * the following scenario. T1 is trying to acquire device locks of all
3155 * the devices in the group and before it could acquire all of them,
3156 * there could be another thread T2 (from different sub-system and use
3157 * case) that has already acquired some of the device locks and might be
3158 * waiting for T1 to release other device locks.
3159 */
3160 if (iommu_group_device_count(group) != 1) {
3161 dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n");
3162 ret = -EINVAL;
3163 goto out;
3164 }
3165
3166 /* Since group has only one device */
3167 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3168 dev = grp_dev->dev;
3169
3170 if (prev_dev != dev) {
3171 dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n");
3172 ret = -EBUSY;
3173 goto out;
3174 }
3175
3176 prev_dom = group->default_domain;
3177 if (!prev_dom) {
3178 ret = -EINVAL;
3179 goto out;
3180 }
3181
3182 dev_def_dom = iommu_get_def_domain_type(dev);
3183 if (!type) {
3184 /*
3185 * If the user hasn't requested any specific type of domain and
3186 * if the device supports both the domains, then default to the
3187 * domain the device was booted with
3188 */
3189 type = dev_def_dom ? : iommu_def_domain_type;
3190 } else if (dev_def_dom && type != dev_def_dom) {
3191 dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
3192 iommu_domain_type_str(type));
3193 ret = -EINVAL;
3194 goto out;
3195 }
3196
3197 /*
3198 * Switch to a new domain only if the requested domain type is different
3199 * from the existing default domain type
3200 */
3201 if (prev_dom->type == type) {
3202 ret = 0;
3203 goto out;
3204 }
3205
3206 /* Sets group->default_domain to the newly allocated domain */
3207 ret = iommu_group_alloc_default_domain(dev->bus, group, type);
3208 if (ret)
3209 goto out;
3210
3211 ret = iommu_create_device_direct_mappings(group, dev);
3212 if (ret)
3213 goto free_new_domain;
3214
3215 ret = __iommu_attach_device(group->default_domain, dev);
3216 if (ret)
3217 goto free_new_domain;
3218
3219 group->domain = group->default_domain;
3220
3221 /*
3222 * Release the mutex here because ops->probe_finalize() call-back of
3223 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3224 * in-turn might call back into IOMMU core code, where it tries to take
3225 * group->mutex, resulting in a deadlock.
3226 */
3227 mutex_unlock(&group->mutex);
3228
3229 /* Make sure dma_ops is appropriatley set */
3230 iommu_group_do_probe_finalize(dev, group->default_domain);
3231 iommu_domain_free(prev_dom);
3232 return 0;
3233
3234free_new_domain:
3235 iommu_domain_free(group->default_domain);
3236 group->default_domain = prev_dom;
3237 group->domain = prev_dom;
3238
3239out:
3240 mutex_unlock(&group->mutex);
3241
3242 return ret;
3243}
3244
3245/*
3246 * Changing the default domain through sysfs requires the users to ubind the
3247 * drivers from the devices in the iommu group. Return failure if this doesn't
3248 * meet.
3249 *
3250 * We need to consider the race between this and the device release path.
3251 * device_lock(dev) is used here to guarantee that the device release path
3252 * will not be entered at the same time.
3253 */
3254static ssize_t iommu_group_store_type(struct iommu_group *group,
3255 const char *buf, size_t count)
3256{
3257 struct group_device *grp_dev;
3258 struct device *dev;
3259 int ret, req_type;
3260
3261 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3262 return -EACCES;
3263
3264 if (WARN_ON(!group))
3265 return -EINVAL;
3266
3267 if (sysfs_streq(buf, "identity"))
3268 req_type = IOMMU_DOMAIN_IDENTITY;
3269 else if (sysfs_streq(buf, "DMA"))
3270 req_type = IOMMU_DOMAIN_DMA;
Robin Murphy26225be2021-08-11 13:21:35 +01003271 else if (sysfs_streq(buf, "DMA-FQ"))
3272 req_type = IOMMU_DOMAIN_DMA_FQ;
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003273 else if (sysfs_streq(buf, "auto"))
3274 req_type = 0;
3275 else
3276 return -EINVAL;
3277
3278 /*
3279 * Lock/Unlock the group mutex here before device lock to
3280 * 1. Make sure that the iommu group has only one device (this is a
3281 * prerequisite for step 2)
3282 * 2. Get struct *dev which is needed to lock device
3283 */
3284 mutex_lock(&group->mutex);
3285 if (iommu_group_device_count(group) != 1) {
3286 mutex_unlock(&group->mutex);
3287 pr_err_ratelimited("Cannot change default domain: Group has more than one device\n");
3288 return -EINVAL;
3289 }
3290
3291 /* Since group has only one device */
3292 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3293 dev = grp_dev->dev;
3294 get_device(dev);
3295
3296 /*
3297 * Don't hold the group mutex because taking group mutex first and then
3298 * the device lock could potentially cause a deadlock as below. Assume
3299 * two threads T1 and T2. T1 is trying to change default domain of an
3300 * iommu group and T2 is trying to hot unplug a device or release [1] VF
3301 * of a PCIe device which is in the same iommu group. T1 takes group
3302 * mutex and before it could take device lock assume T2 has taken device
3303 * lock and is yet to take group mutex. Now, both the threads will be
3304 * waiting for the other thread to release lock. Below, lock order was
3305 * suggested.
3306 * device_lock(dev);
3307 * mutex_lock(&group->mutex);
3308 * iommu_change_dev_def_domain();
3309 * mutex_unlock(&group->mutex);
3310 * device_unlock(dev);
3311 *
3312 * [1] Typical device release path
3313 * device_lock() from device/driver core code
3314 * -> bus_notifier()
3315 * -> iommu_bus_notifier()
3316 * -> iommu_release_device()
3317 * -> ops->release_device() vendor driver calls back iommu core code
3318 * -> mutex_lock() from iommu core code
3319 */
3320 mutex_unlock(&group->mutex);
3321
3322 /* Check if the device in the group still has a driver bound to it */
3323 device_lock(dev);
3324 if (device_is_bound(dev)) {
3325 pr_err_ratelimited("Device is still bound to driver\n");
3326 ret = -EBUSY;
3327 goto out;
3328 }
3329
3330 ret = iommu_change_dev_def_domain(group, dev, req_type);
3331 ret = ret ?: count;
3332
3333out:
3334 device_unlock(dev);
3335 put_device(dev);
3336
3337 return ret;
3338}