blob: 4738bc0242402274aecc35e7bb2fce99f0610701 [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>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040010#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010011#include <linux/bug.h>
12#include <linux/types.h>
Paul Gortmakerc1af7b42018-12-01 14:19:09 -050013#include <linux/init.h>
14#include <linux/export.h>
Andrew Morton60db4022009-05-06 16:03:07 -070015#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010016#include <linux/errno.h>
17#include <linux/iommu.h>
Alex Williamsond72e31c2012-05-30 14:18:53 -060018#include <linux/idr.h>
19#include <linux/notifier.h>
20#include <linux/err.h>
Alex Williamson104a1c12014-07-03 09:51:18 -060021#include <linux/pci.h>
Alex Williamsonf096c062014-09-19 10:03:06 -060022#include <linux/bitops.h>
Robin Murphy57f98d22016-09-13 10:54:14 +010023#include <linux/property.h>
Nipun Guptaeab03e22018-09-10 19:19:18 +053024#include <linux/fsl/mc.h>
Will Deacon25f003d2019-12-19 12:03:41 +000025#include <linux/module.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060026#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010027
Alex Williamsond72e31c2012-05-30 14:18:53 -060028static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020029static DEFINE_IDA(iommu_group_ida);
Joerg Roedel22bb1822019-08-19 15:22:54 +020030
31static unsigned int iommu_def_domain_type __read_mostly;
Zhen Lei68a6efe2018-09-20 17:10:23 +010032static bool iommu_dma_strict __read_mostly = true;
Joerg Roedelfaf14982019-08-19 15:22:46 +020033static u32 iommu_cmd_line __read_mostly;
Alex Williamsond72e31c2012-05-30 14:18:53 -060034
35struct iommu_group {
36 struct kobject kobj;
37 struct kobject *devices_kobj;
38 struct list_head devices;
39 struct mutex mutex;
40 struct blocking_notifier_head notifier;
41 void *iommu_data;
42 void (*iommu_data_release)(void *iommu_data);
43 char *name;
44 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020045 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020046 struct iommu_domain *domain;
Joerg Roedel41df6dc2020-04-29 15:36:47 +020047 struct list_head entry;
Alex Williamsond72e31c2012-05-30 14:18:53 -060048};
49
Joerg Roedelc09e22d2017-02-01 12:19:46 +010050struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060051 struct list_head list;
52 struct device *dev;
53 char *name;
54};
55
56struct iommu_group_attribute {
57 struct attribute attr;
58 ssize_t (*show)(struct iommu_group *group, char *buf);
59 ssize_t (*store)(struct iommu_group *group,
60 const char *buf, size_t count);
61};
62
Eric Augerbc7d12b92017-01-19 20:57:52 +000063static const char * const iommu_group_resv_type_string[] = {
Eric Augeradfd3732019-06-03 08:53:35 +020064 [IOMMU_RESV_DIRECT] = "direct",
65 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
66 [IOMMU_RESV_RESERVED] = "reserved",
67 [IOMMU_RESV_MSI] = "msi",
68 [IOMMU_RESV_SW_MSI] = "msi",
Eric Augerbc7d12b92017-01-19 20:57:52 +000069};
70
Joerg Roedelfaf14982019-08-19 15:22:46 +020071#define IOMMU_CMD_LINE_DMA_API BIT(0)
72
73static void iommu_set_cmd_line_dma_api(void)
74{
75 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
76}
77
Joerg Roedel22bb1822019-08-19 15:22:54 +020078static bool iommu_cmd_line_dma_api(void)
Joerg Roedelfaf14982019-08-19 15:22:46 +020079{
80 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
81}
82
Joerg Roedel79659190e2020-05-25 15:01:22 +020083static int iommu_alloc_default_domain(struct iommu_group *group,
84 struct device *dev);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020085static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
86 unsigned type);
87static int __iommu_attach_device(struct iommu_domain *domain,
88 struct device *dev);
89static int __iommu_attach_group(struct iommu_domain *domain,
90 struct iommu_group *group);
91static void __iommu_detach_group(struct iommu_domain *domain,
92 struct iommu_group *group);
Joerg Roedelce574c22020-04-29 15:36:50 +020093static int iommu_create_device_direct_mappings(struct iommu_group *group,
94 struct device *dev);
Joerg Roedel1b032ec2020-04-29 15:37:12 +020095static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +080096static ssize_t iommu_group_store_type(struct iommu_group *group,
97 const char *buf, size_t count);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020098
Alex Williamsond72e31c2012-05-30 14:18:53 -060099#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
100struct iommu_group_attribute iommu_group_attr_##_name = \
101 __ATTR(_name, _mode, _show, _store)
102
103#define to_iommu_group_attr(_attr) \
104 container_of(_attr, struct iommu_group_attribute, attr)
105#define to_iommu_group(_kobj) \
106 container_of(_kobj, struct iommu_group, kobj)
107
Joerg Roedelb0119e82017-02-01 13:23:08 +0100108static LIST_HEAD(iommu_device_list);
109static DEFINE_SPINLOCK(iommu_device_lock);
110
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200111/*
112 * Use a function instead of an array here because the domain-type is a
113 * bit-field, so an array would waste memory.
114 */
115static const char *iommu_domain_type_str(unsigned int t)
116{
117 switch (t) {
118 case IOMMU_DOMAIN_BLOCKED:
119 return "Blocked";
120 case IOMMU_DOMAIN_IDENTITY:
121 return "Passthrough";
122 case IOMMU_DOMAIN_UNMANAGED:
123 return "Unmanaged";
124 case IOMMU_DOMAIN_DMA:
125 return "Translated";
126 default:
127 return "Unknown";
128 }
129}
130
131static int __init iommu_subsys_init(void)
132{
Joerg Roedel22bb1822019-08-19 15:22:54 +0200133 bool cmd_line = iommu_cmd_line_dma_api();
134
135 if (!cmd_line) {
136 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
137 iommu_set_default_passthrough(false);
138 else
139 iommu_set_default_translated(false);
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200140
Joerg Roedel2896ba42019-09-03 15:15:44 +0200141 if (iommu_default_passthrough() && mem_encrypt_active()) {
142 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200143 iommu_set_default_translated(false);
144 }
Joerg Roedel22bb1822019-08-19 15:22:54 +0200145 }
146
147 pr_info("Default domain type: %s %s\n",
148 iommu_domain_type_str(iommu_def_domain_type),
149 cmd_line ? "(set via kernel command line)" : "");
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200150
151 return 0;
152}
153subsys_initcall(iommu_subsys_init);
154
Joerg Roedelb0119e82017-02-01 13:23:08 +0100155int iommu_device_register(struct iommu_device *iommu)
156{
157 spin_lock(&iommu_device_lock);
158 list_add_tail(&iommu->list, &iommu_device_list);
159 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100160 return 0;
161}
Will Deacona7ba5c32019-12-19 12:03:37 +0000162EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100163
164void iommu_device_unregister(struct iommu_device *iommu)
165{
166 spin_lock(&iommu_device_lock);
167 list_del(&iommu->list);
168 spin_unlock(&iommu_device_lock);
169}
Will Deacona7ba5c32019-12-19 12:03:37 +0000170EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100171
Joerg Roedel045a7042020-03-26 16:08:30 +0100172static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100173{
Joerg Roedel045a7042020-03-26 16:08:30 +0100174 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100175
176 if (param)
177 return param;
178
179 param = kzalloc(sizeof(*param), GFP_KERNEL);
180 if (!param)
181 return NULL;
182
183 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100184 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100185 return param;
186}
187
Joerg Roedel045a7042020-03-26 16:08:30 +0100188static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100189{
Kevin Hao5375e872020-04-02 22:37:49 +0800190 iommu_fwspec_free(dev);
Joerg Roedel045a7042020-03-26 16:08:30 +0100191 kfree(dev->iommu);
192 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100193}
194
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200195static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100196{
197 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200198 struct iommu_device *iommu_dev;
199 struct iommu_group *group;
Jacob Pan0c830e62019-06-03 15:57:48 +0100200 int ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100201
Jacob Pan0c830e62019-06-03 15:57:48 +0100202 if (!ops)
Thierry Redingf38338c2020-05-11 18:10:00 +0200203 return -ENODEV;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100204
Joerg Roedel045a7042020-03-26 16:08:30 +0100205 if (!dev_iommu_get(dev))
Jacob Pan0c830e62019-06-03 15:57:48 +0100206 return -ENOMEM;
207
Will Deacon25f003d2019-12-19 12:03:41 +0000208 if (!try_module_get(ops->owner)) {
209 ret = -EINVAL;
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200210 goto err_free;
Will Deacon25f003d2019-12-19 12:03:41 +0000211 }
212
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200213 iommu_dev = ops->probe_device(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200214 if (IS_ERR(iommu_dev)) {
215 ret = PTR_ERR(iommu_dev);
216 goto out_module_put;
217 }
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200218
219 dev->iommu->iommu_dev = iommu_dev;
220
221 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200222 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200223 ret = PTR_ERR(group);
224 goto out_release;
225 }
226 iommu_group_put(group);
227
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200228 if (group_list && !group->default_domain && list_empty(&group->entry))
229 list_add_tail(&group->entry, group_list);
230
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200231 iommu_device_link(iommu_dev, dev);
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100232
Will Deacon25f003d2019-12-19 12:03:41 +0000233 return 0;
234
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200235out_release:
236 ops->release_device(dev);
237
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200238out_module_put:
Will Deacon25f003d2019-12-19 12:03:41 +0000239 module_put(ops->owner);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200240
241err_free:
Joerg Roedel045a7042020-03-26 16:08:30 +0100242 dev_iommu_free(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200243
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100244 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100245}
246
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200247int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200248{
249 const struct iommu_ops *ops = dev->bus->iommu_ops;
250 struct iommu_group *group;
251 int ret;
252
253 ret = __iommu_probe_device(dev, NULL);
254 if (ret)
255 goto err_out;
256
Joerg Roedel79659190e2020-05-25 15:01:22 +0200257 group = iommu_group_get(dev);
Yang Yingliang058236e2020-11-26 21:38:25 +0800258 if (!group) {
259 ret = -ENODEV;
Joerg Roedel79659190e2020-05-25 15:01:22 +0200260 goto err_release;
Yang Yingliang058236e2020-11-26 21:38:25 +0800261 }
Joerg Roedel79659190e2020-05-25 15:01:22 +0200262
Joerg Roedelcf193882020-04-29 15:36:48 +0200263 /*
264 * Try to allocate a default domain - needs support from the
265 * IOMMU driver. There are still some drivers which don't
266 * support default domains, so the return value is not yet
267 * checked.
268 */
Joerg Roedel79659190e2020-05-25 15:01:22 +0200269 iommu_alloc_default_domain(group, dev);
Joerg Roedelcf193882020-04-29 15:36:48 +0200270
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000271 if (group->default_domain) {
Joerg Roedelcf193882020-04-29 15:36:48 +0200272 ret = __iommu_attach_device(group->default_domain, dev);
Shameer Kolothum77c38c82020-11-19 16:58:46 +0000273 if (ret) {
274 iommu_group_put(group);
275 goto err_release;
276 }
277 }
Joerg Roedelcf193882020-04-29 15:36:48 +0200278
Joerg Roedelce574c22020-04-29 15:36:50 +0200279 iommu_create_device_direct_mappings(group, dev);
280
Joerg Roedelcf193882020-04-29 15:36:48 +0200281 iommu_group_put(group);
282
Joerg Roedelcf193882020-04-29 15:36:48 +0200283 if (ops->probe_finalize)
284 ops->probe_finalize(dev);
285
286 return 0;
287
288err_release:
289 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200290
Joerg Roedelcf193882020-04-29 15:36:48 +0200291err_out:
292 return ret;
293
294}
295
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100296void iommu_release_device(struct device *dev)
297{
298 const struct iommu_ops *ops = dev->bus->iommu_ops;
299
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200300 if (!dev->iommu)
301 return;
Jacob Pan0c830e62019-06-03 15:57:48 +0100302
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200303 iommu_device_unlink(dev->iommu->iommu_dev, dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200304
305 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100306
Qian Cai9ac85452020-07-03 20:10:03 -0400307 iommu_group_remove_device(dev);
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200308 module_put(ops->owner);
309 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100310}
311
Will Deaconfccb4e32017-01-05 18:38:26 +0000312static int __init iommu_set_def_domain_type(char *str)
313{
314 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300315 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000316
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300317 ret = kstrtobool(str, &pt);
318 if (ret)
319 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000320
Joerg Roedeladab0b02019-08-19 15:22:48 +0200321 if (pt)
322 iommu_set_default_passthrough(true);
323 else
324 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200325
Will Deaconfccb4e32017-01-05 18:38:26 +0000326 return 0;
327}
328early_param("iommu.passthrough", iommu_set_def_domain_type);
329
Zhen Lei68a6efe2018-09-20 17:10:23 +0100330static int __init iommu_dma_setup(char *str)
331{
332 return kstrtobool(str, &iommu_dma_strict);
333}
334early_param("iommu.strict", iommu_dma_setup);
335
Alex Williamsond72e31c2012-05-30 14:18:53 -0600336static ssize_t iommu_group_attr_show(struct kobject *kobj,
337 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400338{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600339 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
340 struct iommu_group *group = to_iommu_group(kobj);
341 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400342
Alex Williamsond72e31c2012-05-30 14:18:53 -0600343 if (attr->show)
344 ret = attr->show(group, buf);
345 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400346}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600347
348static ssize_t iommu_group_attr_store(struct kobject *kobj,
349 struct attribute *__attr,
350 const char *buf, size_t count)
351{
352 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
353 struct iommu_group *group = to_iommu_group(kobj);
354 ssize_t ret = -EIO;
355
356 if (attr->store)
357 ret = attr->store(group, buf, count);
358 return ret;
359}
360
361static const struct sysfs_ops iommu_group_sysfs_ops = {
362 .show = iommu_group_attr_show,
363 .store = iommu_group_attr_store,
364};
365
366static int iommu_group_create_file(struct iommu_group *group,
367 struct iommu_group_attribute *attr)
368{
369 return sysfs_create_file(&group->kobj, &attr->attr);
370}
371
372static void iommu_group_remove_file(struct iommu_group *group,
373 struct iommu_group_attribute *attr)
374{
375 sysfs_remove_file(&group->kobj, &attr->attr);
376}
377
378static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
379{
380 return sprintf(buf, "%s\n", group->name);
381}
382
Eric Auger6c65fb32017-01-19 20:57:51 +0000383/**
384 * iommu_insert_resv_region - Insert a new region in the
385 * list of reserved regions.
386 * @new: new region to insert
387 * @regions: list of regions
388 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200389 * Elements are sorted by start address and overlapping segments
390 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000391 */
Wei Yongjun1b0b2a82020-07-13 22:25:42 +0800392static int iommu_insert_resv_region(struct iommu_resv_region *new,
393 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000394{
Eric Auger4dbd2582019-08-21 14:09:40 +0200395 struct iommu_resv_region *iter, *tmp, *nr, *top;
396 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000397
Eric Auger4dbd2582019-08-21 14:09:40 +0200398 nr = iommu_alloc_resv_region(new->start, new->length,
399 new->prot, new->type);
400 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000401 return -ENOMEM;
402
Eric Auger4dbd2582019-08-21 14:09:40 +0200403 /* First add the new element based on start address sorting */
404 list_for_each_entry(iter, regions, list) {
405 if (nr->start < iter->start ||
406 (nr->start == iter->start && nr->type <= iter->type))
407 break;
408 }
409 list_add_tail(&nr->list, &iter->list);
410
411 /* Merge overlapping segments of type nr->type in @regions, if any */
412 list_for_each_entry_safe(iter, tmp, regions, list) {
413 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
414
Eric Auger4c80ba32019-11-26 18:54:13 +0100415 /* no merge needed on elements of different types than @new */
416 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200417 list_move_tail(&iter->list, &stack);
418 continue;
419 }
420
421 /* look for the last stack element of same type as @iter */
422 list_for_each_entry_reverse(top, &stack, list)
423 if (top->type == iter->type)
424 goto check_overlap;
425
426 list_move_tail(&iter->list, &stack);
427 continue;
428
429check_overlap:
430 top_end = top->start + top->length - 1;
431
432 if (iter->start > top_end + 1) {
433 list_move_tail(&iter->list, &stack);
434 } else {
435 top->length = max(top_end, iter_end) - top->start + 1;
436 list_del(&iter->list);
437 kfree(iter);
438 }
439 }
440 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000441 return 0;
442}
443
444static int
445iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
446 struct list_head *group_resv_regions)
447{
448 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100449 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000450
451 list_for_each_entry(entry, dev_resv_regions, list) {
452 ret = iommu_insert_resv_region(entry, group_resv_regions);
453 if (ret)
454 break;
455 }
456 return ret;
457}
458
459int iommu_get_group_resv_regions(struct iommu_group *group,
460 struct list_head *head)
461{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100462 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000463 int ret = 0;
464
465 mutex_lock(&group->mutex);
466 list_for_each_entry(device, &group->devices, list) {
467 struct list_head dev_resv_regions;
468
469 INIT_LIST_HEAD(&dev_resv_regions);
470 iommu_get_resv_regions(device->dev, &dev_resv_regions);
471 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
472 iommu_put_resv_regions(device->dev, &dev_resv_regions);
473 if (ret)
474 break;
475 }
476 mutex_unlock(&group->mutex);
477 return ret;
478}
479EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
480
Eric Augerbc7d12b92017-01-19 20:57:52 +0000481static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
482 char *buf)
483{
484 struct iommu_resv_region *region, *next;
485 struct list_head group_resv_regions;
486 char *str = buf;
487
488 INIT_LIST_HEAD(&group_resv_regions);
489 iommu_get_group_resv_regions(group, &group_resv_regions);
490
491 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
492 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
493 (long long int)region->start,
494 (long long int)(region->start +
495 region->length - 1),
496 iommu_group_resv_type_string[region->type]);
497 kfree(region);
498 }
499
500 return (str - buf);
501}
502
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700503static ssize_t iommu_group_show_type(struct iommu_group *group,
504 char *buf)
505{
506 char *type = "unknown\n";
507
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800508 mutex_lock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700509 if (group->default_domain) {
510 switch (group->default_domain->type) {
511 case IOMMU_DOMAIN_BLOCKED:
512 type = "blocked\n";
513 break;
514 case IOMMU_DOMAIN_IDENTITY:
515 type = "identity\n";
516 break;
517 case IOMMU_DOMAIN_UNMANAGED:
518 type = "unmanaged\n";
519 break;
520 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800521 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700522 break;
523 }
524 }
Sai Praneeth Prakhya0b8a96a2020-11-24 21:06:03 +0800525 mutex_unlock(&group->mutex);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700526 strcpy(buf, type);
527
528 return strlen(type);
529}
530
Alex Williamsond72e31c2012-05-30 14:18:53 -0600531static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
532
Eric Augerbc7d12b92017-01-19 20:57:52 +0000533static IOMMU_GROUP_ATTR(reserved_regions, 0444,
534 iommu_group_show_resv_regions, NULL);
535
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +0800536static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
537 iommu_group_store_type);
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700538
Alex Williamsond72e31c2012-05-30 14:18:53 -0600539static void iommu_group_release(struct kobject *kobj)
540{
541 struct iommu_group *group = to_iommu_group(kobj);
542
Joerg Roedel269aa802015-05-28 18:41:25 +0200543 pr_debug("Releasing group %d\n", group->id);
544
Alex Williamsond72e31c2012-05-30 14:18:53 -0600545 if (group->iommu_data_release)
546 group->iommu_data_release(group->iommu_data);
547
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200548 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600549
Joerg Roedel53723dc2015-05-28 18:41:29 +0200550 if (group->default_domain)
551 iommu_domain_free(group->default_domain);
552
Alex Williamsond72e31c2012-05-30 14:18:53 -0600553 kfree(group->name);
554 kfree(group);
555}
556
557static struct kobj_type iommu_group_ktype = {
558 .sysfs_ops = &iommu_group_sysfs_ops,
559 .release = iommu_group_release,
560};
561
562/**
563 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600564 *
565 * This function is called by an iommu driver to allocate a new iommu
566 * group. The iommu group represents the minimum granularity of the iommu.
567 * Upon successful return, the caller holds a reference to the supplied
568 * group in order to hold the group until devices are added. Use
569 * iommu_group_put() to release this extra reference count, allowing the
570 * group to be automatically reclaimed once it has no devices or external
571 * references.
572 */
573struct iommu_group *iommu_group_alloc(void)
574{
575 struct iommu_group *group;
576 int ret;
577
578 group = kzalloc(sizeof(*group), GFP_KERNEL);
579 if (!group)
580 return ERR_PTR(-ENOMEM);
581
582 group->kobj.kset = iommu_group_kset;
583 mutex_init(&group->mutex);
584 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200585 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600586 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
587
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200588 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
589 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600590 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200591 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600592 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200593 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600594
595 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
596 NULL, "%d", group->id);
597 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200598 ida_simple_remove(&iommu_group_ida, group->id);
Qiushi Wu7cc31612020-05-27 16:00:19 -0500599 kobject_put(&group->kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600600 return ERR_PTR(ret);
601 }
602
603 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
604 if (!group->devices_kobj) {
605 kobject_put(&group->kobj); /* triggers .release & free */
606 return ERR_PTR(-ENOMEM);
607 }
608
609 /*
610 * The devices_kobj holds a reference on the group kobject, so
611 * as long as that exists so will the group. We can therefore
612 * use the devices_kobj for reference counting.
613 */
614 kobject_put(&group->kobj);
615
Eric Augerbc7d12b92017-01-19 20:57:52 +0000616 ret = iommu_group_create_file(group,
617 &iommu_group_attr_reserved_regions);
618 if (ret)
619 return ERR_PTR(ret);
620
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700621 ret = iommu_group_create_file(group, &iommu_group_attr_type);
622 if (ret)
623 return ERR_PTR(ret);
624
Joerg Roedel269aa802015-05-28 18:41:25 +0200625 pr_debug("Allocated group %d\n", group->id);
626
Alex Williamsond72e31c2012-05-30 14:18:53 -0600627 return group;
628}
629EXPORT_SYMBOL_GPL(iommu_group_alloc);
630
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100631struct iommu_group *iommu_group_get_by_id(int id)
632{
633 struct kobject *group_kobj;
634 struct iommu_group *group;
635 const char *name;
636
637 if (!iommu_group_kset)
638 return NULL;
639
640 name = kasprintf(GFP_KERNEL, "%d", id);
641 if (!name)
642 return NULL;
643
644 group_kobj = kset_find_obj(iommu_group_kset, name);
645 kfree(name);
646
647 if (!group_kobj)
648 return NULL;
649
650 group = container_of(group_kobj, struct iommu_group, kobj);
651 BUG_ON(group->id != id);
652
653 kobject_get(group->devices_kobj);
654 kobject_put(&group->kobj);
655
656 return group;
657}
658EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
659
Alex Williamsond72e31c2012-05-30 14:18:53 -0600660/**
661 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
662 * @group: the group
663 *
664 * iommu drivers can store data in the group for use when doing iommu
665 * operations. This function provides a way to retrieve it. Caller
666 * should hold a group reference.
667 */
668void *iommu_group_get_iommudata(struct iommu_group *group)
669{
670 return group->iommu_data;
671}
672EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
673
674/**
675 * iommu_group_set_iommudata - set iommu_data for a group
676 * @group: the group
677 * @iommu_data: new data
678 * @release: release function for iommu_data
679 *
680 * iommu drivers can store data in the group for use when doing iommu
681 * operations. This function provides a way to set the data after
682 * the group has been allocated. Caller should hold a group reference.
683 */
684void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
685 void (*release)(void *iommu_data))
686{
687 group->iommu_data = iommu_data;
688 group->iommu_data_release = release;
689}
690EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
691
692/**
693 * iommu_group_set_name - set name for a group
694 * @group: the group
695 * @name: name
696 *
697 * Allow iommu driver to set a name for a group. When set it will
698 * appear in a name attribute file under the group in sysfs.
699 */
700int iommu_group_set_name(struct iommu_group *group, const char *name)
701{
702 int ret;
703
704 if (group->name) {
705 iommu_group_remove_file(group, &iommu_group_attr_name);
706 kfree(group->name);
707 group->name = NULL;
708 if (!name)
709 return 0;
710 }
711
712 group->name = kstrdup(name, GFP_KERNEL);
713 if (!group->name)
714 return -ENOMEM;
715
716 ret = iommu_group_create_file(group, &iommu_group_attr_name);
717 if (ret) {
718 kfree(group->name);
719 group->name = NULL;
720 return ret;
721 }
722
723 return 0;
724}
725EXPORT_SYMBOL_GPL(iommu_group_set_name);
726
Joerg Roedelce574c22020-04-29 15:36:50 +0200727static int iommu_create_device_direct_mappings(struct iommu_group *group,
728 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200729{
730 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000731 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200732 struct list_head mappings;
733 unsigned long pg_size;
734 int ret = 0;
735
736 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
737 return 0;
738
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100739 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200740
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100741 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200742 INIT_LIST_HEAD(&mappings);
743
Eric Augere5b52342017-01-19 20:57:47 +0000744 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200745
746 /* We need to consider overlapping regions for different devices */
747 list_for_each_entry(entry, &mappings, list) {
748 dma_addr_t start, end, addr;
Yong Wu093b32a2020-12-07 17:35:53 +0800749 size_t map_size = 0;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200750
Eric Augere5b52342017-01-19 20:57:47 +0000751 if (domain->ops->apply_resv_region)
752 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200753
Joerg Roedelbeed2822015-05-28 18:41:34 +0200754 start = ALIGN(entry->start, pg_size);
755 end = ALIGN(entry->start + entry->length, pg_size);
756
Eric Augeradfd3732019-06-03 08:53:35 +0200757 if (entry->type != IOMMU_RESV_DIRECT &&
758 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000759 continue;
760
Yong Wu093b32a2020-12-07 17:35:53 +0800761 for (addr = start; addr <= end; addr += pg_size) {
Joerg Roedelbeed2822015-05-28 18:41:34 +0200762 phys_addr_t phys_addr;
763
Yong Wu093b32a2020-12-07 17:35:53 +0800764 if (addr == end)
765 goto map_end;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200766
Yong Wu093b32a2020-12-07 17:35:53 +0800767 phys_addr = iommu_iova_to_phys(domain, addr);
768 if (!phys_addr) {
769 map_size += pg_size;
770 continue;
771 }
772
773map_end:
774 if (map_size) {
775 ret = iommu_map(domain, addr - map_size,
776 addr - map_size, map_size,
777 entry->prot);
778 if (ret)
779 goto out;
780 map_size = 0;
781 }
Joerg Roedelbeed2822015-05-28 18:41:34 +0200782 }
783
784 }
785
Tom Murphyaae4c8e2020-08-17 22:00:49 +0100786 iommu_flush_iotlb_all(domain);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200787
Joerg Roedelbeed2822015-05-28 18:41:34 +0200788out:
Eric Augere5b52342017-01-19 20:57:47 +0000789 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200790
791 return ret;
792}
793
Joerg Roedelbd421262020-05-19 15:03:40 +0200794static bool iommu_is_attach_deferred(struct iommu_domain *domain,
795 struct device *dev)
796{
797 if (domain->ops->is_attach_deferred)
798 return domain->ops->is_attach_deferred(domain, dev);
799
800 return false;
801}
802
Alex Williamsond72e31c2012-05-30 14:18:53 -0600803/**
804 * iommu_group_add_device - add a device to an iommu group
805 * @group: the group into which to add the device (reference should be held)
806 * @dev: the device
807 *
808 * This function is called by an iommu driver to add a device into a
809 * group. Adding a device increments the group reference count.
810 */
811int iommu_group_add_device(struct iommu_group *group, struct device *dev)
812{
813 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100814 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600815
816 device = kzalloc(sizeof(*device), GFP_KERNEL);
817 if (!device)
818 return -ENOMEM;
819
820 device->dev = dev;
821
822 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000823 if (ret)
824 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600825
826 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
827rename:
828 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000829 ret = -ENOMEM;
830 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600831 }
832
833 ret = sysfs_create_link_nowarn(group->devices_kobj,
834 &dev->kobj, device->name);
835 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600836 if (ret == -EEXIST && i >= 0) {
837 /*
838 * Account for the slim chance of collision
839 * and append an instance to the name.
840 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000841 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600842 device->name = kasprintf(GFP_KERNEL, "%s.%d",
843 kobject_name(&dev->kobj), i++);
844 goto rename;
845 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000846 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600847 }
848
849 kobject_get(group->devices_kobj);
850
851 dev->iommu_group = group;
852
853 mutex_lock(&group->mutex);
854 list_add_tail(&device->list, &group->devices);
Joerg Roedelbd421262020-05-19 15:03:40 +0200855 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
Robin Murphy797a8b42017-01-16 12:58:07 +0000856 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600857 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000858 if (ret)
859 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600860
861 /* Notify any listeners about change to group. */
862 blocking_notifier_call_chain(&group->notifier,
863 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600864
865 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200866
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600867 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200868
Alex Williamsond72e31c2012-05-30 14:18:53 -0600869 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000870
871err_put_group:
872 mutex_lock(&group->mutex);
873 list_del(&device->list);
874 mutex_unlock(&group->mutex);
875 dev->iommu_group = NULL;
876 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700877 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000878err_free_name:
879 kfree(device->name);
880err_remove_link:
881 sysfs_remove_link(&dev->kobj, "iommu_group");
882err_free_device:
883 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600884 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000885 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600886}
887EXPORT_SYMBOL_GPL(iommu_group_add_device);
888
889/**
890 * iommu_group_remove_device - remove a device from it's current group
891 * @dev: device to be removed
892 *
893 * This function is called by an iommu driver to remove the device from
894 * it's current group. This decrements the iommu group reference count.
895 */
896void iommu_group_remove_device(struct device *dev)
897{
898 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100899 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600900
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600901 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200902
Alex Williamsond72e31c2012-05-30 14:18:53 -0600903 /* Pre-notify listeners that a device is being removed. */
904 blocking_notifier_call_chain(&group->notifier,
905 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
906
907 mutex_lock(&group->mutex);
908 list_for_each_entry(tmp_device, &group->devices, list) {
909 if (tmp_device->dev == dev) {
910 device = tmp_device;
911 list_del(&device->list);
912 break;
913 }
914 }
915 mutex_unlock(&group->mutex);
916
917 if (!device)
918 return;
919
920 sysfs_remove_link(group->devices_kobj, device->name);
921 sysfs_remove_link(&dev->kobj, "iommu_group");
922
Shuah Khan2e757082013-08-15 11:59:25 -0600923 trace_remove_device_from_group(group->id, dev);
924
Alex Williamsond72e31c2012-05-30 14:18:53 -0600925 kfree(device->name);
926 kfree(device);
927 dev->iommu_group = NULL;
928 kobject_put(group->devices_kobj);
929}
930EXPORT_SYMBOL_GPL(iommu_group_remove_device);
931
Joerg Roedel426a2732015-05-28 18:41:30 +0200932static int iommu_group_device_count(struct iommu_group *group)
933{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100934 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200935 int ret = 0;
936
937 list_for_each_entry(entry, &group->devices, list)
938 ret++;
939
940 return ret;
941}
942
Alex Williamsond72e31c2012-05-30 14:18:53 -0600943/**
944 * iommu_group_for_each_dev - iterate over each device in the group
945 * @group: the group
946 * @data: caller opaque data to be passed to callback function
947 * @fn: caller supplied callback function
948 *
949 * This function is called by group users to iterate over group devices.
950 * Callers should hold a reference count to the group during callback.
951 * The group->mutex is held across callbacks, which will block calls to
952 * iommu_group_add/remove_device.
953 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200954static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
955 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600956{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100957 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600958 int ret = 0;
959
Alex Williamsond72e31c2012-05-30 14:18:53 -0600960 list_for_each_entry(device, &group->devices, list) {
961 ret = fn(device->dev, data);
962 if (ret)
963 break;
964 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200965 return ret;
966}
967
968
969int iommu_group_for_each_dev(struct iommu_group *group, void *data,
970 int (*fn)(struct device *, void *))
971{
972 int ret;
973
974 mutex_lock(&group->mutex);
975 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600976 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200977
Alex Williamsond72e31c2012-05-30 14:18:53 -0600978 return ret;
979}
980EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
981
982/**
983 * iommu_group_get - Return the group for a device and increment reference
984 * @dev: get the group that this device belongs to
985 *
986 * This function is called by iommu drivers and users to get the group
987 * for the specified device. If found, the group is returned and the group
988 * reference in incremented, else NULL.
989 */
990struct iommu_group *iommu_group_get(struct device *dev)
991{
992 struct iommu_group *group = dev->iommu_group;
993
994 if (group)
995 kobject_get(group->devices_kobj);
996
997 return group;
998}
999EXPORT_SYMBOL_GPL(iommu_group_get);
1000
1001/**
Robin Murphy13f59a72016-11-11 17:59:21 +00001002 * iommu_group_ref_get - Increment reference on a group
1003 * @group: the group to use, must not be NULL
1004 *
1005 * This function is called by iommu drivers to take additional references on an
1006 * existing group. Returns the given group for convenience.
1007 */
1008struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1009{
1010 kobject_get(group->devices_kobj);
1011 return group;
1012}
Will Deacona7ba5c32019-12-19 12:03:37 +00001013EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +00001014
1015/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001016 * iommu_group_put - Decrement group reference
1017 * @group: the group to use
1018 *
1019 * This function is called by iommu drivers and users to release the
1020 * iommu group. Once the reference count is zero, the group is released.
1021 */
1022void iommu_group_put(struct iommu_group *group)
1023{
1024 if (group)
1025 kobject_put(group->devices_kobj);
1026}
1027EXPORT_SYMBOL_GPL(iommu_group_put);
1028
1029/**
1030 * iommu_group_register_notifier - Register a notifier for group changes
1031 * @group: the group to watch
1032 * @nb: notifier block to signal
1033 *
1034 * This function allows iommu group users to track changes in a group.
1035 * See include/linux/iommu.h for actions sent via this notifier. Caller
1036 * should hold a reference to the group throughout notifier registration.
1037 */
1038int iommu_group_register_notifier(struct iommu_group *group,
1039 struct notifier_block *nb)
1040{
1041 return blocking_notifier_chain_register(&group->notifier, nb);
1042}
1043EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1044
1045/**
1046 * iommu_group_unregister_notifier - Unregister a notifier
1047 * @group: the group to watch
1048 * @nb: notifier block to signal
1049 *
1050 * Unregister a previously registered group notifier block.
1051 */
1052int iommu_group_unregister_notifier(struct iommu_group *group,
1053 struct notifier_block *nb)
1054{
1055 return blocking_notifier_chain_unregister(&group->notifier, nb);
1056}
1057EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1058
1059/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001060 * iommu_register_device_fault_handler() - Register a device fault handler
1061 * @dev: the device
1062 * @handler: the fault handler
1063 * @data: private data passed as argument to the handler
1064 *
1065 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001066 * fault event and data as argument. The handler should return 0 on success. If
1067 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1068 * complete the fault by calling iommu_page_response() with one of the following
1069 * response code:
1070 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1071 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1072 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1073 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001074 *
1075 * Return 0 if the fault handler was installed successfully, or an error.
1076 */
1077int iommu_register_device_fault_handler(struct device *dev,
1078 iommu_dev_fault_handler_t handler,
1079 void *data)
1080{
Joerg Roedel045a7042020-03-26 16:08:30 +01001081 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001082 int ret = 0;
1083
1084 if (!param)
1085 return -EINVAL;
1086
1087 mutex_lock(&param->lock);
1088 /* Only allow one fault handler registered for each device */
1089 if (param->fault_param) {
1090 ret = -EBUSY;
1091 goto done_unlock;
1092 }
1093
1094 get_device(dev);
1095 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1096 if (!param->fault_param) {
1097 put_device(dev);
1098 ret = -ENOMEM;
1099 goto done_unlock;
1100 }
1101 param->fault_param->handler = handler;
1102 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001103 mutex_init(&param->fault_param->lock);
1104 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001105
1106done_unlock:
1107 mutex_unlock(&param->lock);
1108
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1112
1113/**
1114 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1115 * @dev: the device
1116 *
1117 * Remove the device fault handler installed with
1118 * iommu_register_device_fault_handler().
1119 *
1120 * Return 0 on success, or an error.
1121 */
1122int iommu_unregister_device_fault_handler(struct device *dev)
1123{
Joerg Roedel045a7042020-03-26 16:08:30 +01001124 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001125 int ret = 0;
1126
1127 if (!param)
1128 return -EINVAL;
1129
1130 mutex_lock(&param->lock);
1131
1132 if (!param->fault_param)
1133 goto unlock;
1134
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001135 /* we cannot unregister handler if there are pending faults */
1136 if (!list_empty(&param->fault_param->faults)) {
1137 ret = -EBUSY;
1138 goto unlock;
1139 }
1140
Jacob Pan0c830e62019-06-03 15:57:48 +01001141 kfree(param->fault_param);
1142 param->fault_param = NULL;
1143 put_device(dev);
1144unlock:
1145 mutex_unlock(&param->lock);
1146
1147 return ret;
1148}
1149EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1150
1151/**
1152 * iommu_report_device_fault() - Report fault event to device driver
1153 * @dev: the device
1154 * @evt: fault event data
1155 *
1156 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001157 * handler. When this function fails and the fault is recoverable, it is the
1158 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001159 *
1160 * Return 0 on success, or an error.
1161 */
1162int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1163{
Joerg Roedel045a7042020-03-26 16:08:30 +01001164 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001165 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001166 struct iommu_fault_param *fparam;
1167 int ret = 0;
1168
1169 if (!param || !evt)
1170 return -EINVAL;
1171
1172 /* we only report device fault if there is a handler registered */
1173 mutex_lock(&param->lock);
1174 fparam = param->fault_param;
1175 if (!fparam || !fparam->handler) {
1176 ret = -EINVAL;
1177 goto done_unlock;
1178 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001179
1180 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1181 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1182 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1183 GFP_KERNEL);
1184 if (!evt_pending) {
1185 ret = -ENOMEM;
1186 goto done_unlock;
1187 }
1188 mutex_lock(&fparam->lock);
1189 list_add_tail(&evt_pending->list, &fparam->faults);
1190 mutex_unlock(&fparam->lock);
1191 }
1192
Jacob Pan0c830e62019-06-03 15:57:48 +01001193 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001194 if (ret && evt_pending) {
1195 mutex_lock(&fparam->lock);
1196 list_del(&evt_pending->list);
1197 mutex_unlock(&fparam->lock);
1198 kfree(evt_pending);
1199 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001200done_unlock:
1201 mutex_unlock(&param->lock);
1202 return ret;
1203}
1204EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1205
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001206int iommu_page_response(struct device *dev,
1207 struct iommu_page_response *msg)
1208{
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001209 bool needs_pasid;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001210 int ret = -EINVAL;
1211 struct iommu_fault_event *evt;
1212 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001213 struct dev_iommu *param = dev->iommu;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001214 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001215 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1216
1217 if (!domain || !domain->ops->page_response)
1218 return -ENODEV;
1219
1220 if (!param || !param->fault_param)
1221 return -EINVAL;
1222
1223 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1224 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1225 return -EINVAL;
1226
1227 /* Only send response if there is a fault report pending */
1228 mutex_lock(&param->fault_param->lock);
1229 if (list_empty(&param->fault_param->faults)) {
1230 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1231 goto done_unlock;
1232 }
1233 /*
1234 * Check if we have a matching page request pending to respond,
1235 * otherwise return -EINVAL
1236 */
1237 list_for_each_entry(evt, &param->fault_param->faults, list) {
1238 prm = &evt->fault.prm;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001239 if (prm->grpid != msg->grpid)
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001240 continue;
1241
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001242 /*
1243 * If the PASID is required, the corresponding request is
1244 * matched using the group ID, the PASID valid bit and the PASID
1245 * value. Otherwise only the group ID matches request and
1246 * response.
1247 */
1248 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1249 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1250 continue;
1251
1252 if (!needs_pasid && has_pasid) {
1253 /* No big deal, just clear it. */
1254 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1255 msg->pasid = 0;
1256 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001257
1258 ret = domain->ops->page_response(dev, evt, msg);
1259 list_del(&evt->list);
1260 kfree(evt);
1261 break;
1262 }
1263
1264done_unlock:
1265 mutex_unlock(&param->fault_param->lock);
1266 return ret;
1267}
1268EXPORT_SYMBOL_GPL(iommu_page_response);
1269
Jacob Pan0c830e62019-06-03 15:57:48 +01001270/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001271 * iommu_group_id - Return ID for a group
1272 * @group: the group to ID
1273 *
1274 * Return the unique ID for the group matching the sysfs group number.
1275 */
1276int iommu_group_id(struct iommu_group *group)
1277{
1278 return group->id;
1279}
1280EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001281
Alex Williamsonf096c062014-09-19 10:03:06 -06001282static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1283 unsigned long *devfns);
1284
Alex Williamson104a1c12014-07-03 09:51:18 -06001285/*
1286 * To consider a PCI device isolated, we require ACS to support Source
1287 * Validation, Request Redirection, Completer Redirection, and Upstream
1288 * Forwarding. This effectively means that devices cannot spoof their
1289 * requester ID, requests and completions cannot be redirected, and all
1290 * transactions are forwarded upstream, even as it passes through a
1291 * bridge where the target device is downstream.
1292 */
1293#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1294
Alex Williamsonf096c062014-09-19 10:03:06 -06001295/*
1296 * For multifunction devices which are not isolated from each other, find
1297 * all the other non-isolated functions and look for existing groups. For
1298 * each function, we also need to look for aliases to or from other devices
1299 * that may already have a group.
1300 */
1301static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1302 unsigned long *devfns)
1303{
1304 struct pci_dev *tmp = NULL;
1305 struct iommu_group *group;
1306
1307 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1308 return NULL;
1309
1310 for_each_pci_dev(tmp) {
1311 if (tmp == pdev || tmp->bus != pdev->bus ||
1312 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1313 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1314 continue;
1315
1316 group = get_pci_alias_group(tmp, devfns);
1317 if (group) {
1318 pci_dev_put(tmp);
1319 return group;
1320 }
1321 }
1322
1323 return NULL;
1324}
1325
1326/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001327 * Look for aliases to or from the given device for existing groups. DMA
1328 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001329 * space is quite small (especially since we're really only looking at pcie
1330 * device, and therefore only expect multiple slots on the root complex or
1331 * downstream switch ports). It's conceivable though that a pair of
1332 * multifunction devices could have aliases between them that would cause a
1333 * loop. To prevent this, we use a bitmap to track where we've been.
1334 */
1335static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1336 unsigned long *devfns)
1337{
1338 struct pci_dev *tmp = NULL;
1339 struct iommu_group *group;
1340
1341 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1342 return NULL;
1343
1344 group = iommu_group_get(&pdev->dev);
1345 if (group)
1346 return group;
1347
1348 for_each_pci_dev(tmp) {
1349 if (tmp == pdev || tmp->bus != pdev->bus)
1350 continue;
1351
1352 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001353 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001354 group = get_pci_alias_group(tmp, devfns);
1355 if (group) {
1356 pci_dev_put(tmp);
1357 return group;
1358 }
1359
1360 group = get_pci_function_alias_group(tmp, devfns);
1361 if (group) {
1362 pci_dev_put(tmp);
1363 return group;
1364 }
1365 }
1366 }
1367
1368 return NULL;
1369}
1370
Alex Williamson104a1c12014-07-03 09:51:18 -06001371struct group_for_pci_data {
1372 struct pci_dev *pdev;
1373 struct iommu_group *group;
1374};
1375
1376/*
1377 * DMA alias iterator callback, return the last seen device. Stop and return
1378 * the IOMMU group if we find one along the way.
1379 */
1380static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1381{
1382 struct group_for_pci_data *data = opaque;
1383
1384 data->pdev = pdev;
1385 data->group = iommu_group_get(&pdev->dev);
1386
1387 return data->group != NULL;
1388}
1389
1390/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001391 * Generic device_group call-back function. It just allocates one
1392 * iommu-group per device.
1393 */
1394struct iommu_group *generic_device_group(struct device *dev)
1395{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001396 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001397}
Will Deacona7ba5c32019-12-19 12:03:37 +00001398EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001399
1400/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001401 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1402 * to find or create an IOMMU group for a device.
1403 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001404struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001405{
Joerg Roedel5e622922015-10-21 23:51:37 +02001406 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001407 struct group_for_pci_data data;
1408 struct pci_bus *bus;
1409 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001410 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001411
Joerg Roedel5e622922015-10-21 23:51:37 +02001412 if (WARN_ON(!dev_is_pci(dev)))
1413 return ERR_PTR(-EINVAL);
1414
Alex Williamson104a1c12014-07-03 09:51:18 -06001415 /*
1416 * Find the upstream DMA alias for the device. A device must not
1417 * be aliased due to topology in order to have its own IOMMU group.
1418 * If we find an alias along the way that already belongs to a
1419 * group, use it.
1420 */
1421 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1422 return data.group;
1423
1424 pdev = data.pdev;
1425
1426 /*
1427 * Continue upstream from the point of minimum IOMMU granularity
1428 * due to aliases to the point where devices are protected from
1429 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1430 * group, use it.
1431 */
1432 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1433 if (!bus->self)
1434 continue;
1435
1436 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1437 break;
1438
1439 pdev = bus->self;
1440
1441 group = iommu_group_get(&pdev->dev);
1442 if (group)
1443 return group;
1444 }
1445
1446 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001447 * Look for existing groups on device aliases. If we alias another
1448 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001449 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001450 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1451 if (group)
1452 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001453
1454 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001455 * Look for existing groups on non-isolated functions on the same
1456 * slot and aliases of those funcions, if any. No need to clear
1457 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001458 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001459 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1460 if (group)
1461 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001462
1463 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001464 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001465}
Will Deacona7ba5c32019-12-19 12:03:37 +00001466EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001467
Nipun Guptaeab03e22018-09-10 19:19:18 +05301468/* Get the IOMMU group for device on fsl-mc bus */
1469struct iommu_group *fsl_mc_device_group(struct device *dev)
1470{
1471 struct device *cont_dev = fsl_mc_cont_dev(dev);
1472 struct iommu_group *group;
1473
1474 group = iommu_group_get(cont_dev);
1475 if (!group)
1476 group = iommu_group_alloc();
1477 return group;
1478}
Will Deacona7ba5c32019-12-19 12:03:37 +00001479EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301480
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001481static int iommu_get_def_domain_type(struct device *dev)
1482{
1483 const struct iommu_ops *ops = dev->bus->iommu_ops;
Lu Baolu28b41e22020-11-24 21:06:01 +08001484
1485 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
1486 return IOMMU_DOMAIN_DMA;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001487
1488 if (ops->def_domain_type)
Lu Baolu28b41e22020-11-24 21:06:01 +08001489 return ops->def_domain_type(dev);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001490
Lu Baolu28b41e22020-11-24 21:06:01 +08001491 return 0;
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001492}
1493
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001494static int iommu_group_alloc_default_domain(struct bus_type *bus,
1495 struct iommu_group *group,
1496 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001497{
1498 struct iommu_domain *dom;
1499
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001500 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001501 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001502 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1503 if (dom)
1504 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1505 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001506 }
1507
1508 if (!dom)
1509 return -ENOMEM;
1510
1511 group->default_domain = dom;
1512 if (!group->domain)
1513 group->domain = dom;
1514
1515 if (!iommu_dma_strict) {
1516 int attr = 1;
1517 iommu_domain_set_attr(dom,
1518 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1519 &attr);
1520 }
1521
1522 return 0;
1523}
1524
Joerg Roedel79659190e2020-05-25 15:01:22 +02001525static int iommu_alloc_default_domain(struct iommu_group *group,
1526 struct device *dev)
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001527{
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001528 unsigned int type;
1529
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001530 if (group->default_domain)
1531 return 0;
1532
Lu Baolu28b41e22020-11-24 21:06:01 +08001533 type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001534
1535 return iommu_group_alloc_default_domain(dev->bus, group, type);
1536}
1537
Alex Williamson104a1c12014-07-03 09:51:18 -06001538/**
1539 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1540 * @dev: target device
1541 *
1542 * This function is intended to be called by IOMMU drivers and extended to
1543 * support common, bus-defined algorithms when determining or creating the
1544 * IOMMU group for a device. On success, the caller will hold a reference
1545 * to the returned IOMMU group, which will already include the provided
1546 * device. The reference should be released with iommu_group_put().
1547 */
Joerg Roedel1b032ec2020-04-29 15:37:12 +02001548static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001549{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001550 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001551 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001552 int ret;
1553
1554 group = iommu_group_get(dev);
1555 if (group)
1556 return group;
1557
Robin Murphy05f803002017-07-21 13:12:38 +01001558 if (!ops)
1559 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001560
Robin Murphy05f803002017-07-21 13:12:38 +01001561 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001562 if (WARN_ON_ONCE(group == NULL))
1563 return ERR_PTR(-EINVAL);
1564
Alex Williamson104a1c12014-07-03 09:51:18 -06001565 if (IS_ERR(group))
1566 return group;
1567
1568 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001569 if (ret)
1570 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001571
1572 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001573
1574out_put_group:
1575 iommu_group_put(group);
1576
1577 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001578}
1579
Joerg Roedel6827ca82015-05-28 18:41:35 +02001580struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1581{
1582 return group->default_domain;
1583}
1584
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001585static int probe_iommu_group(struct device *dev, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001586{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001587 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001588 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001589 int ret;
Joerg Roedel38667f12015-06-29 10:16:08 +02001590
Joerg Roedel5012c392020-04-29 15:36:51 +02001591 /* Device is probed already if in a group */
1592 group = iommu_group_get(dev);
1593 if (group) {
1594 iommu_group_put(group);
1595 return 0;
1596 }
1597
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001598 ret = __iommu_probe_device(dev, group_list);
Joerg Roedel38667f12015-06-29 10:16:08 +02001599 if (ret == -ENODEV)
1600 ret = 0;
1601
1602 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001603}
1604
Joerg Roedel8da30142015-05-28 18:41:27 +02001605static int remove_iommu_group(struct device *dev, void *data)
1606{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001607 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001608
1609 return 0;
1610}
1611
Alex Williamsond72e31c2012-05-30 14:18:53 -06001612static int iommu_bus_notifier(struct notifier_block *nb,
1613 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001614{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001615 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001616 struct device *dev = data;
1617 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001618
Alex Williamsond72e31c2012-05-30 14:18:53 -06001619 /*
1620 * ADD/DEL call into iommu driver ops if provided, which may
1621 * result in ADD/DEL notifiers to group->notifier
1622 */
1623 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001624 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001625
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001626 ret = iommu_probe_device(dev);
1627 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001628 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001629 iommu_release_device(dev);
1630 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001631 }
Alex Williamson14604322011-10-21 15:56:05 -04001632
Alex Williamsond72e31c2012-05-30 14:18:53 -06001633 /*
1634 * Remaining BUS_NOTIFYs get filtered and republished to the
1635 * group, if anyone is listening
1636 */
1637 group = iommu_group_get(dev);
1638 if (!group)
1639 return 0;
1640
1641 switch (action) {
1642 case BUS_NOTIFY_BIND_DRIVER:
1643 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1644 break;
1645 case BUS_NOTIFY_BOUND_DRIVER:
1646 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1647 break;
1648 case BUS_NOTIFY_UNBIND_DRIVER:
1649 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1650 break;
1651 case BUS_NOTIFY_UNBOUND_DRIVER:
1652 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1653 break;
1654 }
1655
1656 if (group_action)
1657 blocking_notifier_call_chain(&group->notifier,
1658 group_action, dev);
1659
1660 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001661 return 0;
1662}
1663
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001664struct __group_domain_type {
1665 struct device *dev;
1666 unsigned int type;
1667};
1668
1669static int probe_get_default_domain_type(struct device *dev, void *data)
1670{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001671 struct __group_domain_type *gtype = data;
Lu Baolu28b41e22020-11-24 21:06:01 +08001672 unsigned int type = iommu_get_def_domain_type(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001673
1674 if (type) {
1675 if (gtype->type && gtype->type != type) {
1676 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1677 iommu_domain_type_str(type),
1678 dev_name(gtype->dev),
1679 iommu_domain_type_str(gtype->type));
1680 gtype->type = 0;
1681 }
1682
1683 if (!gtype->dev) {
1684 gtype->dev = dev;
1685 gtype->type = type;
1686 }
1687 }
1688
1689 return 0;
1690}
1691
1692static void probe_alloc_default_domain(struct bus_type *bus,
1693 struct iommu_group *group)
1694{
1695 struct __group_domain_type gtype;
1696
1697 memset(&gtype, 0, sizeof(gtype));
1698
1699 /* Ask for default domain requirements of all devices in the group */
1700 __iommu_group_for_each_dev(group, &gtype,
1701 probe_get_default_domain_type);
1702
1703 if (!gtype.type)
1704 gtype.type = iommu_def_domain_type;
1705
1706 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001707
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001708}
1709
1710static int iommu_group_do_dma_attach(struct device *dev, void *data)
1711{
1712 struct iommu_domain *domain = data;
Joerg Roedel431275a2020-06-04 11:19:44 +02001713 int ret = 0;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001714
Joerg Roedel431275a2020-06-04 11:19:44 +02001715 if (!iommu_is_attach_deferred(domain, dev))
1716 ret = __iommu_attach_device(domain, dev);
1717
1718 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001719}
1720
1721static int __iommu_group_dma_attach(struct iommu_group *group)
1722{
1723 return __iommu_group_for_each_dev(group, group->default_domain,
1724 iommu_group_do_dma_attach);
1725}
1726
Joerg Roedel70b81702020-05-19 15:28:24 +02001727static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1728{
1729 struct iommu_domain *domain = data;
1730
1731 if (domain->ops->probe_finalize)
1732 domain->ops->probe_finalize(dev);
1733
1734 return 0;
1735}
1736
1737static void __iommu_group_dma_finalize(struct iommu_group *group)
1738{
1739 __iommu_group_for_each_dev(group, group->default_domain,
1740 iommu_group_do_probe_finalize);
1741}
Joerg Roedelcc69fc42020-06-02 10:32:04 +02001742
Joerg Roedelce574c22020-04-29 15:36:50 +02001743static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1744{
1745 struct iommu_group *group = data;
1746
1747 iommu_create_device_direct_mappings(group, dev);
1748
1749 return 0;
1750}
1751
1752static int iommu_group_create_direct_mappings(struct iommu_group *group)
1753{
1754 return __iommu_group_for_each_dev(group, group,
1755 iommu_do_create_direct_mappings);
1756}
1757
Joerg Roedel5012c392020-04-29 15:36:51 +02001758int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001759{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001760 struct iommu_group *group, *next;
1761 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001762 int ret;
1763
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001764 /*
1765 * This code-path does not allocate the default domain when
1766 * creating the iommu group, so do it after the groups are
1767 * created.
1768 */
1769 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1770 if (ret)
1771 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001772
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001773 list_for_each_entry_safe(group, next, &group_list, entry) {
1774 /* Remove item from the list */
1775 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001776
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001777 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001778
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001779 /* Try to allocate default domain */
1780 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001781
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001782 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001783 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001784 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001785 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001786
1787 iommu_group_create_direct_mappings(group);
1788
1789 ret = __iommu_group_dma_attach(group);
1790
1791 mutex_unlock(&group->mutex);
1792
1793 if (ret)
1794 break;
Joerg Roedel70b81702020-05-19 15:28:24 +02001795
1796 __iommu_group_dma_finalize(group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001797 }
1798
1799 return ret;
1800}
1801
Mark Salterfb3e3062014-09-21 13:58:24 -04001802static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001803{
Mark Salterfb3e3062014-09-21 13:58:24 -04001804 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001805 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001806
Mark Salterfb3e3062014-09-21 13:58:24 -04001807 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1808 if (!nb)
1809 return -ENOMEM;
1810
1811 nb->notifier_call = iommu_bus_notifier;
1812
1813 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001814 if (err)
1815 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001816
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001817 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001818 if (err)
1819 goto out_err;
1820
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001821
1822 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001823
1824out_err:
1825 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001826 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001827 bus_unregister_notifier(bus, nb);
1828
1829out_free:
1830 kfree(nb);
1831
1832 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001833}
1834
Joerg Roedelff217762011-08-26 16:48:26 +02001835/**
1836 * bus_set_iommu - set iommu-callbacks for the bus
1837 * @bus: bus.
1838 * @ops: the callbacks provided by the iommu-driver
1839 *
1840 * This function is called by an iommu driver to set the iommu methods
1841 * used for a particular bus. Drivers for devices on that bus can use
1842 * the iommu-api after these ops are registered.
1843 * This special function is needed because IOMMUs are usually devices on
1844 * the bus itself, so the iommu drivers are not initialized when the bus
1845 * is set up. With this function the iommu-driver can set the iommu-ops
1846 * afterwards.
1847 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001848int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001849{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001850 int err;
1851
Will Deacon4312cf72019-12-19 12:03:43 +00001852 if (ops == NULL) {
1853 bus->iommu_ops = NULL;
1854 return 0;
1855 }
1856
Joerg Roedelff217762011-08-26 16:48:26 +02001857 if (bus->iommu_ops != NULL)
1858 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001859
Joerg Roedelff217762011-08-26 16:48:26 +02001860 bus->iommu_ops = ops;
1861
1862 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001863 err = iommu_bus_init(bus, ops);
1864 if (err)
1865 bus->iommu_ops = NULL;
1866
1867 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001868}
Joerg Roedelff217762011-08-26 16:48:26 +02001869EXPORT_SYMBOL_GPL(bus_set_iommu);
1870
Joerg Roedela1b60c12011-09-06 18:46:34 +02001871bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001872{
Joerg Roedel94441c32011-09-06 18:58:54 +02001873 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001874}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001875EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001876
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001877bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1878{
1879 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1880 return false;
1881
1882 return bus->iommu_ops->capable(cap);
1883}
1884EXPORT_SYMBOL_GPL(iommu_capable);
1885
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001886/**
1887 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1888 * @domain: iommu domain
1889 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001890 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001891 *
1892 * This function should be used by IOMMU users which want to be notified
1893 * whenever an IOMMU fault happens.
1894 *
1895 * The fault handler itself should return 0 on success, and an appropriate
1896 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001897 */
1898void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001899 iommu_fault_handler_t handler,
1900 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001901{
1902 BUG_ON(!domain);
1903
1904 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001905 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001906}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001907EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001908
Joerg Roedel53723dc2015-05-28 18:41:29 +02001909static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1910 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001911{
1912 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001913
Joerg Roedel94441c32011-09-06 18:58:54 +02001914 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001915 return NULL;
1916
Joerg Roedel53723dc2015-05-28 18:41:29 +02001917 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001918 if (!domain)
1919 return NULL;
1920
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001921 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001922 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001923 /* Assume all sizes by default; the driver may override this later */
1924 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001925
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001926 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001927}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001928
Joerg Roedel53723dc2015-05-28 18:41:29 +02001929struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1930{
1931 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001932}
1933EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1934
1935void iommu_domain_free(struct iommu_domain *domain)
1936{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001937 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001938}
1939EXPORT_SYMBOL_GPL(iommu_domain_free);
1940
Joerg Roedel426a2732015-05-28 18:41:30 +02001941static int __iommu_attach_device(struct iommu_domain *domain,
1942 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001943{
Shuah Khanb54db772013-08-15 11:59:26 -06001944 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001945
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001946 if (unlikely(domain->ops->attach_dev == NULL))
1947 return -ENODEV;
1948
Shuah Khanb54db772013-08-15 11:59:26 -06001949 ret = domain->ops->attach_dev(domain, dev);
1950 if (!ret)
1951 trace_attach_device_to_domain(dev);
1952 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001953}
Joerg Roedel426a2732015-05-28 18:41:30 +02001954
1955int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1956{
1957 struct iommu_group *group;
1958 int ret;
1959
1960 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001961 if (!group)
1962 return -ENODEV;
1963
Joerg Roedel426a2732015-05-28 18:41:30 +02001964 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001965 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001966 * change while we are attaching
1967 */
1968 mutex_lock(&group->mutex);
1969 ret = -EINVAL;
1970 if (iommu_group_device_count(group) != 1)
1971 goto out_unlock;
1972
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001973 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001974
1975out_unlock:
1976 mutex_unlock(&group->mutex);
1977 iommu_group_put(group);
1978
1979 return ret;
1980}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001981EXPORT_SYMBOL_GPL(iommu_attach_device);
1982
Lianbo Jiang3ab65722021-01-26 19:53:37 +08001983int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
1984{
1985 const struct iommu_ops *ops = domain->ops;
1986
1987 if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
1988 return __iommu_attach_device(domain, dev);
1989
1990 return 0;
1991}
1992
Jacob Pand9057382020-09-25 09:32:46 -07001993/*
1994 * Check flags and other user provided data for valid combinations. We also
1995 * make sure no reserved fields or unused flags are set. This is to ensure
1996 * not breaking userspace in the future when these fields or flags are used.
1997 */
1998static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)
Yi L Liu4c7c1712019-10-02 12:42:40 -07001999{
Jacob Pand9057382020-09-25 09:32:46 -07002000 u32 mask;
2001 int i;
2002
2003 if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)
2004 return -EINVAL;
2005
2006 mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;
2007 if (info->cache & ~mask)
2008 return -EINVAL;
2009
2010 if (info->granularity >= IOMMU_INV_GRANU_NR)
2011 return -EINVAL;
2012
2013 switch (info->granularity) {
2014 case IOMMU_INV_GRANU_ADDR:
2015 if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)
2016 return -EINVAL;
2017
2018 mask = IOMMU_INV_ADDR_FLAGS_PASID |
2019 IOMMU_INV_ADDR_FLAGS_ARCHID |
2020 IOMMU_INV_ADDR_FLAGS_LEAF;
2021
2022 if (info->granu.addr_info.flags & ~mask)
2023 return -EINVAL;
2024 break;
2025 case IOMMU_INV_GRANU_PASID:
2026 mask = IOMMU_INV_PASID_FLAGS_PASID |
2027 IOMMU_INV_PASID_FLAGS_ARCHID;
2028 if (info->granu.pasid_info.flags & ~mask)
2029 return -EINVAL;
2030
2031 break;
2032 case IOMMU_INV_GRANU_DOMAIN:
2033 if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)
2034 return -EINVAL;
2035 break;
2036 default:
2037 return -EINVAL;
2038 }
2039
2040 /* Check reserved padding fields */
2041 for (i = 0; i < sizeof(info->padding); i++) {
2042 if (info->padding[i])
2043 return -EINVAL;
2044 }
2045
2046 return 0;
2047}
2048
2049int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,
2050 void __user *uinfo)
2051{
2052 struct iommu_cache_invalidate_info inv_info = { 0 };
2053 u32 minsz;
2054 int ret;
2055
Yi L Liu4c7c1712019-10-02 12:42:40 -07002056 if (unlikely(!domain->ops->cache_invalidate))
2057 return -ENODEV;
2058
Jacob Pand9057382020-09-25 09:32:46 -07002059 /*
2060 * No new spaces can be added before the variable sized union, the
2061 * minimum size is the offset to the union.
2062 */
2063 minsz = offsetof(struct iommu_cache_invalidate_info, granu);
Yi L Liu4c7c1712019-10-02 12:42:40 -07002064
Jacob Pand9057382020-09-25 09:32:46 -07002065 /* Copy minsz from user to get flags and argsz */
2066 if (copy_from_user(&inv_info, uinfo, minsz))
2067 return -EFAULT;
2068
2069 /* Fields before the variable size union are mandatory */
2070 if (inv_info.argsz < minsz)
2071 return -EINVAL;
2072
2073 /* PASID and address granu require additional info beyond minsz */
2074 if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&
2075 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))
2076 return -EINVAL;
2077
2078 if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&
2079 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))
2080 return -EINVAL;
2081
2082 /*
2083 * User might be using a newer UAPI header which has a larger data
2084 * size, we shall support the existing flags within the current
2085 * size. Copy the remaining user data _after_ minsz but not more
2086 * than the current kernel supported size.
2087 */
2088 if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,
2089 min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))
2090 return -EFAULT;
2091
2092 /* Now the argsz is validated, check the content */
2093 ret = iommu_check_cache_invl_data(&inv_info);
2094 if (ret)
2095 return ret;
2096
2097 return domain->ops->cache_invalidate(domain, dev, &inv_info);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002098}
Jacob Pan23cc3492020-09-25 09:32:45 -07002099EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002100
Jacob Pand9057382020-09-25 09:32:46 -07002101static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)
Jacob Pan808be0a2019-10-02 12:42:43 -07002102{
Dan Carpenter4dd6ce42020-11-03 13:16:23 +03002103 u64 mask;
Jacob Pand9057382020-09-25 09:32:46 -07002104 int i;
2105
2106 if (data->version != IOMMU_GPASID_BIND_VERSION_1)
2107 return -EINVAL;
2108
2109 /* Check the range of supported formats */
2110 if (data->format >= IOMMU_PASID_FORMAT_LAST)
2111 return -EINVAL;
2112
2113 /* Check all flags */
2114 mask = IOMMU_SVA_GPASID_VAL;
2115 if (data->flags & ~mask)
2116 return -EINVAL;
2117
2118 /* Check reserved padding fields */
2119 for (i = 0; i < sizeof(data->padding); i++) {
2120 if (data->padding[i])
2121 return -EINVAL;
2122 }
2123
2124 return 0;
2125}
2126
2127static int iommu_sva_prepare_bind_data(void __user *udata,
2128 struct iommu_gpasid_bind_data *data)
2129{
2130 u32 minsz;
2131
2132 /*
2133 * No new spaces can be added before the variable sized union, the
2134 * minimum size is the offset to the union.
2135 */
2136 minsz = offsetof(struct iommu_gpasid_bind_data, vendor);
2137
2138 /* Copy minsz from user to get flags and argsz */
2139 if (copy_from_user(data, udata, minsz))
2140 return -EFAULT;
2141
2142 /* Fields before the variable size union are mandatory */
2143 if (data->argsz < minsz)
2144 return -EINVAL;
2145 /*
2146 * User might be using a newer UAPI header, we shall let IOMMU vendor
2147 * driver decide on what size it needs. Since the guest PASID bind data
2148 * can be vendor specific, larger argsz could be the result of extension
2149 * for one vendor but it should not affect another vendor.
2150 * Copy the remaining user data _after_ minsz
2151 */
2152 if (copy_from_user((void *)data + minsz, udata + minsz,
2153 min_t(u32, data->argsz, sizeof(*data)) - minsz))
2154 return -EFAULT;
2155
2156 return iommu_check_bind_data(data);
2157}
2158
2159int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
2160 void __user *udata)
2161{
2162 struct iommu_gpasid_bind_data data = { 0 };
2163 int ret;
2164
Jacob Pan808be0a2019-10-02 12:42:43 -07002165 if (unlikely(!domain->ops->sva_bind_gpasid))
2166 return -ENODEV;
2167
Jacob Pand9057382020-09-25 09:32:46 -07002168 ret = iommu_sva_prepare_bind_data(udata, &data);
2169 if (ret)
2170 return ret;
2171
2172 return domain->ops->sva_bind_gpasid(domain, dev, &data);
Jacob Pan808be0a2019-10-02 12:42:43 -07002173}
Jacob Pan23cc3492020-09-25 09:32:45 -07002174EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
Jacob Pan808be0a2019-10-02 12:42:43 -07002175
2176int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2177 ioasid_t pasid)
2178{
2179 if (unlikely(!domain->ops->sva_unbind_gpasid))
2180 return -ENODEV;
2181
2182 return domain->ops->sva_unbind_gpasid(dev, pasid);
2183}
2184EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
2185
Jacob Pand9057382020-09-25 09:32:46 -07002186int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2187 void __user *udata)
2188{
2189 struct iommu_gpasid_bind_data data = { 0 };
2190 int ret;
2191
2192 if (unlikely(!domain->ops->sva_bind_gpasid))
2193 return -ENODEV;
2194
2195 ret = iommu_sva_prepare_bind_data(udata, &data);
2196 if (ret)
2197 return ret;
2198
2199 return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
2200}
Jacob Pan23cc3492020-09-25 09:32:45 -07002201EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002202
2203static void __iommu_detach_device(struct iommu_domain *domain,
2204 struct device *dev)
2205{
Joerg Roedelbd421262020-05-19 15:03:40 +02002206 if (iommu_is_attach_deferred(domain, dev))
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002207 return;
2208
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002209 if (unlikely(domain->ops->detach_dev == NULL))
2210 return;
2211
2212 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06002213 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002214}
Joerg Roedel426a2732015-05-28 18:41:30 +02002215
2216void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2217{
2218 struct iommu_group *group;
2219
2220 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07002221 if (!group)
2222 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02002223
2224 mutex_lock(&group->mutex);
2225 if (iommu_group_device_count(group) != 1) {
2226 WARN_ON(1);
2227 goto out_unlock;
2228 }
2229
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002230 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002231
2232out_unlock:
2233 mutex_unlock(&group->mutex);
2234 iommu_group_put(group);
2235}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002236EXPORT_SYMBOL_GPL(iommu_detach_device);
2237
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002238struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2239{
2240 struct iommu_domain *domain;
2241 struct iommu_group *group;
2242
2243 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002244 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002245 return NULL;
2246
2247 domain = group->domain;
2248
2249 iommu_group_put(group);
2250
2251 return domain;
2252}
2253EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2254
Alex Williamsond72e31c2012-05-30 14:18:53 -06002255/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002256 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2257 * guarantees that the group and its default domain are valid and correct.
2258 */
2259struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2260{
2261 return dev->iommu_group->default_domain;
2262}
2263
2264/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002265 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002266 * the IOMMU API works on domains and devices. Bridge that gap by
2267 * iterating over the devices in a group. Ideally we'd have a single
2268 * device which represents the requestor ID of the group, but we also
2269 * allow IOMMU drivers to create policy defined minimum sets, where
2270 * the physical hardware may be able to distiguish members, but we
2271 * wish to group them at a higher level (ex. untrusted multi-function
2272 * PCI devices). Thus we attach each device.
2273 */
2274static int iommu_group_do_attach_device(struct device *dev, void *data)
2275{
2276 struct iommu_domain *domain = data;
2277
Joerg Roedel426a2732015-05-28 18:41:30 +02002278 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002279}
2280
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002281static int __iommu_attach_group(struct iommu_domain *domain,
2282 struct iommu_group *group)
2283{
2284 int ret;
2285
2286 if (group->default_domain && group->domain != group->default_domain)
2287 return -EBUSY;
2288
2289 ret = __iommu_group_for_each_dev(group, domain,
2290 iommu_group_do_attach_device);
2291 if (ret == 0)
2292 group->domain = domain;
2293
2294 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002295}
2296
2297int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2298{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002299 int ret;
2300
2301 mutex_lock(&group->mutex);
2302 ret = __iommu_attach_group(domain, group);
2303 mutex_unlock(&group->mutex);
2304
2305 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002306}
2307EXPORT_SYMBOL_GPL(iommu_attach_group);
2308
2309static int iommu_group_do_detach_device(struct device *dev, void *data)
2310{
2311 struct iommu_domain *domain = data;
2312
Joerg Roedel426a2732015-05-28 18:41:30 +02002313 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002314
2315 return 0;
2316}
2317
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002318static void __iommu_detach_group(struct iommu_domain *domain,
2319 struct iommu_group *group)
2320{
2321 int ret;
2322
2323 if (!group->default_domain) {
2324 __iommu_group_for_each_dev(group, domain,
2325 iommu_group_do_detach_device);
2326 group->domain = NULL;
2327 return;
2328 }
2329
2330 if (group->domain == group->default_domain)
2331 return;
2332
2333 /* Detach by re-attaching to the default domain */
2334 ret = __iommu_group_for_each_dev(group, group->default_domain,
2335 iommu_group_do_attach_device);
2336 if (ret != 0)
2337 WARN_ON(1);
2338 else
2339 group->domain = group->default_domain;
2340}
2341
Alex Williamsond72e31c2012-05-30 14:18:53 -06002342void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2343{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002344 mutex_lock(&group->mutex);
2345 __iommu_detach_group(domain, group);
2346 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002347}
2348EXPORT_SYMBOL_GPL(iommu_detach_group);
2349
Varun Sethibb5547a2013-03-29 01:23:58 +05302350phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002351{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002352 if (unlikely(domain->ops->iova_to_phys == NULL))
2353 return 0;
2354
2355 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002356}
2357EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002358
Alex Williamsonbd139692013-06-17 19:57:34 -06002359static size_t iommu_pgsize(struct iommu_domain *domain,
2360 unsigned long addr_merge, size_t size)
2361{
2362 unsigned int pgsize_idx;
2363 size_t pgsize;
2364
2365 /* Max page size that still fits into 'size' */
2366 pgsize_idx = __fls(size);
2367
2368 /* need to consider alignment requirements ? */
2369 if (likely(addr_merge)) {
2370 /* Max page size allowed by address */
2371 unsigned int align_pgsize_idx = __ffs(addr_merge);
2372 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2373 }
2374
2375 /* build a mask of acceptable page sizes */
2376 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2377
2378 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002379 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06002380
2381 /* make sure we're still sane */
2382 BUG_ON(!pgsize);
2383
2384 /* pick the biggest page */
2385 pgsize_idx = __fls(pgsize);
2386 pgsize = 1UL << pgsize_idx;
2387
2388 return pgsize;
2389}
2390
Wei Yongjun1b0b2a82020-07-13 22:25:42 +08002391static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2392 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002393{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002394 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002395 unsigned long orig_iova = iova;
2396 unsigned int min_pagesz;
2397 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002398 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002399 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002400
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002401 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002402 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002403 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002404
Joerg Roedela10315e2015-03-26 13:43:06 +01002405 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2406 return -EINVAL;
2407
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002408 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002409 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002410
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002411 /*
2412 * both the virtual address and the physical one, as well as
2413 * the size of the mapping, must be aligned (at least) to the
2414 * size of the smallest page supported by the hardware
2415 */
2416 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002417 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002418 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002419 return -EINVAL;
2420 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002421
Fabio Estevamabedb042013-08-22 10:25:42 -03002422 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002423
2424 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002425 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002426
Fabio Estevamabedb042013-08-22 10:25:42 -03002427 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002428 iova, &paddr, pgsize);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002429 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002430
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002431 if (ret)
2432 break;
2433
2434 iova += pgsize;
2435 paddr += pgsize;
2436 size -= pgsize;
2437 }
2438
2439 /* unroll mapping in case something went wrong */
2440 if (ret)
2441 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002442 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002443 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002444
2445 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002446}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002447
Yong Wud8c1df02021-01-07 20:29:03 +08002448static int _iommu_map(struct iommu_domain *domain, unsigned long iova,
2449 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2450{
2451 const struct iommu_ops *ops = domain->ops;
2452 int ret;
2453
Douglas Andersonb8437a32021-02-01 17:06:23 -08002454 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
Yong Wud8c1df02021-01-07 20:29:03 +08002455 if (ret == 0 && ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002456 ops->iotlb_sync_map(domain, iova, size);
Yong Wud8c1df02021-01-07 20:29:03 +08002457
2458 return ret;
2459}
2460
Tom Murphy781ca2d2019-09-08 09:56:38 -07002461int iommu_map(struct iommu_domain *domain, unsigned long iova,
2462 phys_addr_t paddr, size_t size, int prot)
2463{
2464 might_sleep();
Yong Wud8c1df02021-01-07 20:29:03 +08002465 return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002466}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002467EXPORT_SYMBOL_GPL(iommu_map);
2468
Tom Murphy781ca2d2019-09-08 09:56:38 -07002469int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2470 phys_addr_t paddr, size_t size, int prot)
2471{
Yong Wud8c1df02021-01-07 20:29:03 +08002472 return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002473}
2474EXPORT_SYMBOL_GPL(iommu_map_atomic);
2475
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002476static size_t __iommu_unmap(struct iommu_domain *domain,
2477 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002478 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002479{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002480 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002481 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002482 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002483 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002484
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002485 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002486 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002487 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002488
Joerg Roedela10315e2015-03-26 13:43:06 +01002489 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002490 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002491
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002492 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002493 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002494
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002495 /*
2496 * The virtual address, as well as the size of the mapping, must be
2497 * aligned (at least) to the size of the smallest page supported
2498 * by the hardware
2499 */
2500 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002501 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2502 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002503 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002504 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002505
Joe Perches6197ca82013-06-23 12:29:04 -07002506 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002507
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002508 /*
2509 * Keep iterating until we either unmap 'size' bytes (or more)
2510 * or we hit an area that isn't mapped.
2511 */
2512 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002513 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002514
Will Deacon56f8af52019-07-02 16:44:06 +01002515 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002516 if (!unmapped_page)
2517 break;
2518
Joe Perches6197ca82013-06-23 12:29:04 -07002519 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2520 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002521
2522 iova += unmapped_page;
2523 unmapped += unmapped_page;
2524 }
2525
Shuah Khandb8614d2015-01-16 20:53:17 -07002526 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002527 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002528}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002529
2530size_t iommu_unmap(struct iommu_domain *domain,
2531 unsigned long iova, size_t size)
2532{
Will Deacona7d20dc2019-07-02 16:43:48 +01002533 struct iommu_iotlb_gather iotlb_gather;
2534 size_t ret;
2535
2536 iommu_iotlb_gather_init(&iotlb_gather);
2537 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
Tom Murphyaae4c8e2020-08-17 22:00:49 +01002538 iommu_iotlb_sync(domain, &iotlb_gather);
Will Deacona7d20dc2019-07-02 16:43:48 +01002539
2540 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002541}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002542EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002543
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002544size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002545 unsigned long iova, size_t size,
2546 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002547{
Will Deacona7d20dc2019-07-02 16:43:48 +01002548 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002549}
2550EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2551
Baolin Wang99302642020-06-12 11:39:54 +08002552static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2553 struct scatterlist *sg, unsigned int nents, int prot,
2554 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002555{
Yong Wud8c1df02021-01-07 20:29:03 +08002556 const struct iommu_ops *ops = domain->ops;
Robin Murphy5d95f402018-10-11 16:56:42 +01002557 size_t len = 0, mapped = 0;
2558 phys_addr_t start;
2559 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002560 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002561
Robin Murphy5d95f402018-10-11 16:56:42 +01002562 while (i <= nents) {
2563 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002564
Robin Murphy5d95f402018-10-11 16:56:42 +01002565 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002566 ret = __iommu_map(domain, iova + mapped, start,
2567 len, prot, gfp);
2568
Robin Murphy5d95f402018-10-11 16:56:42 +01002569 if (ret)
2570 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002571
Robin Murphy5d95f402018-10-11 16:56:42 +01002572 mapped += len;
2573 len = 0;
2574 }
Robin Murphy18f23402014-11-25 17:50:55 +00002575
Robin Murphy5d95f402018-10-11 16:56:42 +01002576 if (len) {
2577 len += sg->length;
2578 } else {
2579 len = sg->length;
2580 start = s_phys;
2581 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002582
Robin Murphy5d95f402018-10-11 16:56:42 +01002583 if (++i < nents)
2584 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002585 }
2586
Yong Wud8c1df02021-01-07 20:29:03 +08002587 if (ops->iotlb_sync_map)
Yong Wu2ebbd252021-01-07 20:29:04 +08002588 ops->iotlb_sync_map(domain, iova, mapped);
Olav Haugan315786e2014-10-25 09:55:16 -07002589 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002590
2591out_err:
2592 /* undo mappings already done */
2593 iommu_unmap(domain, iova, mapped);
2594
2595 return 0;
2596
Olav Haugan315786e2014-10-25 09:55:16 -07002597}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002598
2599size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2600 struct scatterlist *sg, unsigned int nents, int prot)
2601{
2602 might_sleep();
2603 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2604}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002605EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002606
Tom Murphy781ca2d2019-09-08 09:56:38 -07002607size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2608 struct scatterlist *sg, unsigned int nents, int prot)
2609{
2610 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2611}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002612
Joerg Roedel207c6e32017-04-26 15:39:28 +02002613/**
2614 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2615 * @domain: the iommu domain where the fault has happened
2616 * @dev: the device where the fault has happened
2617 * @iova: the faulting address
2618 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2619 *
2620 * This function should be called by the low-level IOMMU implementations
2621 * whenever IOMMU faults happen, to allow high-level users, that are
2622 * interested in such events, to know about them.
2623 *
2624 * This event may be useful for several possible use cases:
2625 * - mere logging of the event
2626 * - dynamic TLB/PTE loading
2627 * - if restarting of the faulting device is required
2628 *
2629 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2630 * PTE/TLB loading will one day be supported, implementations will be able
2631 * to tell whether it succeeded or not according to this return value).
2632 *
2633 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2634 * (though fault handlers can also return -ENOSYS, in case they want to
2635 * elicit the default behavior of the IOMMU drivers).
2636 */
2637int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2638 unsigned long iova, int flags)
2639{
2640 int ret = -ENOSYS;
2641
2642 /*
2643 * if upper layers showed interest and installed a fault handler,
2644 * invoke it.
2645 */
2646 if (domain->handler)
2647 ret = domain->handler(domain, dev, iova, flags,
2648 domain->handler_token);
2649
2650 trace_io_page_fault(dev, iova, flags);
2651 return ret;
2652}
2653EXPORT_SYMBOL_GPL(report_iommu_fault);
2654
Alex Williamsond72e31c2012-05-30 14:18:53 -06002655static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002656{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002657 iommu_group_kset = kset_create_and_add("iommu_groups",
2658 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002659 BUG_ON(!iommu_group_kset);
2660
Gary R Hookbad614b2018-06-12 16:41:21 -05002661 iommu_debugfs_setup();
2662
Alex Williamsond72e31c2012-05-30 14:18:53 -06002663 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002664}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002665core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002666
2667int iommu_domain_get_attr(struct iommu_domain *domain,
2668 enum iommu_attr attr, void *data)
2669{
Christoph Hellwigbc9a05e2021-04-01 17:52:51 +02002670 if (!domain->ops->domain_get_attr)
2671 return -EINVAL;
2672 return domain->ops->domain_get_attr(domain, attr, data);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002673}
2674EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2675
2676int iommu_domain_set_attr(struct iommu_domain *domain,
2677 enum iommu_attr attr, void *data)
2678{
Joerg Roedel69356712013-02-04 14:00:01 +01002679 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002680
Joerg Roedel69356712013-02-04 14:00:01 +01002681 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01002682 default:
2683 if (domain->ops->domain_set_attr == NULL)
2684 return -EINVAL;
2685
2686 ret = domain->ops->domain_set_attr(domain, attr, data);
2687 }
2688
2689 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002690}
2691EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02002692
Christoph Hellwig7e147542021-04-01 17:52:52 +02002693int iommu_enable_nesting(struct iommu_domain *domain)
2694{
2695 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2696 return -EINVAL;
2697 if (!domain->ops->enable_nesting)
2698 return -EINVAL;
2699 return domain->ops->enable_nesting(domain);
2700}
2701EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2702
Eric Augere5b52342017-01-19 20:57:47 +00002703void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002704{
2705 const struct iommu_ops *ops = dev->bus->iommu_ops;
2706
Eric Augere5b52342017-01-19 20:57:47 +00002707 if (ops && ops->get_resv_regions)
2708 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002709}
2710
Eric Augere5b52342017-01-19 20:57:47 +00002711void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002712{
2713 const struct iommu_ops *ops = dev->bus->iommu_ops;
2714
Eric Augere5b52342017-01-19 20:57:47 +00002715 if (ops && ops->put_resv_regions)
2716 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002717}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002718
Thierry Redingf9f69712019-12-18 14:42:01 +01002719/**
2720 * generic_iommu_put_resv_regions - Reserved region driver helper
2721 * @dev: device for which to free reserved regions
2722 * @list: reserved region list for device
2723 *
2724 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2725 * for simple reservations. Memory allocated for each reserved region will be
2726 * freed. If an IOMMU driver allocates additional resources per region, it is
2727 * going to have to implement a custom callback.
2728 */
2729void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2730{
2731 struct iommu_resv_region *entry, *next;
2732
2733 list_for_each_entry_safe(entry, next, list, list)
2734 kfree(entry);
2735}
2736EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2737
Eric Auger2b20cbb2017-01-19 20:57:49 +00002738struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002739 size_t length, int prot,
2740 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002741{
2742 struct iommu_resv_region *region;
2743
2744 region = kzalloc(sizeof(*region), GFP_KERNEL);
2745 if (!region)
2746 return NULL;
2747
2748 INIT_LIST_HEAD(&region->list);
2749 region->start = start;
2750 region->length = length;
2751 region->prot = prot;
2752 region->type = type;
2753 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002754}
Will Deacona7ba5c32019-12-19 12:03:37 +00002755EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002756
Joerg Roedel8a699612019-08-19 15:22:47 +02002757void iommu_set_default_passthrough(bool cmd_line)
2758{
2759 if (cmd_line)
2760 iommu_set_cmd_line_dma_api();
2761
2762 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2763}
2764
2765void iommu_set_default_translated(bool cmd_line)
2766{
2767 if (cmd_line)
2768 iommu_set_cmd_line_dma_api();
2769
2770 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2771}
2772
2773bool iommu_default_passthrough(void)
2774{
2775 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2776}
2777EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2778
Joerg Roedel534766d2017-01-31 16:58:42 +01002779const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002780{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002781 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002782 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002783
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002784 spin_lock(&iommu_device_lock);
2785 list_for_each_entry(iommu, &iommu_device_list, list)
2786 if (iommu->fwnode == fwnode) {
2787 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002788 break;
2789 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002790 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002791 return ops;
2792}
2793
Robin Murphy57f98d22016-09-13 10:54:14 +01002794int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2795 const struct iommu_ops *ops)
2796{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002797 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002798
2799 if (fwspec)
2800 return ops == fwspec->ops ? 0 : -EINVAL;
2801
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002802 if (!dev_iommu_get(dev))
2803 return -ENOMEM;
2804
Robin Murphy098accf2020-02-13 14:00:21 +00002805 /* Preallocate for the overwhelmingly common case of 1 ID */
2806 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002807 if (!fwspec)
2808 return -ENOMEM;
2809
2810 of_node_get(to_of_node(iommu_fwnode));
2811 fwspec->iommu_fwnode = iommu_fwnode;
2812 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002813 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002814 return 0;
2815}
2816EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2817
2818void iommu_fwspec_free(struct device *dev)
2819{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002820 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002821
2822 if (fwspec) {
2823 fwnode_handle_put(fwspec->iommu_fwnode);
2824 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002825 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002826 }
2827}
2828EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2829
2830int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2831{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002832 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002833 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002834
2835 if (!fwspec)
2836 return -EINVAL;
2837
Robin Murphy098accf2020-02-13 14:00:21 +00002838 new_num = fwspec->num_ids + num_ids;
2839 if (new_num > 1) {
2840 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2841 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002842 if (!fwspec)
2843 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002844
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002845 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002846 }
2847
2848 for (i = 0; i < num_ids; i++)
2849 fwspec->ids[fwspec->num_ids + i] = ids[i];
2850
Robin Murphy098accf2020-02-13 14:00:21 +00002851 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002852 return 0;
2853}
2854EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002855
2856/*
2857 * Per device IOMMU features.
2858 */
Lu Baolua3a19592019-03-25 09:30:28 +08002859int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2860{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002861 if (dev->iommu && dev->iommu->iommu_dev) {
2862 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002863
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002864 if (ops->dev_enable_feat)
2865 return ops->dev_enable_feat(dev, feat);
2866 }
Lu Baolua3a19592019-03-25 09:30:28 +08002867
2868 return -ENODEV;
2869}
2870EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2871
2872/*
2873 * The device drivers should do the necessary cleanups before calling this.
2874 * For example, before disabling the aux-domain feature, the device driver
2875 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2876 */
2877int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2878{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002879 if (dev->iommu && dev->iommu->iommu_dev) {
2880 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002881
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002882 if (ops->dev_disable_feat)
2883 return ops->dev_disable_feat(dev, feat);
2884 }
Lu Baolua3a19592019-03-25 09:30:28 +08002885
2886 return -EBUSY;
2887}
2888EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2889
2890bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2891{
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002892 if (dev->iommu && dev->iommu->iommu_dev) {
2893 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
Lu Baolua3a19592019-03-25 09:30:28 +08002894
Shameer Kolothumb9abb192021-03-03 17:36:11 +00002895 if (ops->dev_feat_enabled)
2896 return ops->dev_feat_enabled(dev, feat);
2897 }
Lu Baolua3a19592019-03-25 09:30:28 +08002898
2899 return false;
2900}
2901EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2902
2903/*
2904 * Aux-domain specific attach/detach.
2905 *
2906 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2907 * true. Also, as long as domains are attached to a device through this
2908 * interface, any tries to call iommu_attach_device() should fail
2909 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2910 * This should make us safe against a device being attached to a guest as a
2911 * whole while there are still pasid users on it (aux and sva).
2912 */
2913int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2914{
2915 int ret = -ENODEV;
2916
2917 if (domain->ops->aux_attach_dev)
2918 ret = domain->ops->aux_attach_dev(domain, dev);
2919
2920 if (!ret)
2921 trace_attach_device_to_domain(dev);
2922
2923 return ret;
2924}
2925EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2926
2927void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2928{
2929 if (domain->ops->aux_detach_dev) {
2930 domain->ops->aux_detach_dev(domain, dev);
2931 trace_detach_device_from_domain(dev);
2932 }
2933}
2934EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2935
2936int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2937{
2938 int ret = -ENODEV;
2939
2940 if (domain->ops->aux_get_pasid)
2941 ret = domain->ops->aux_get_pasid(domain, dev);
2942
2943 return ret;
2944}
2945EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002946
2947/**
2948 * iommu_sva_bind_device() - Bind a process address space to a device
2949 * @dev: the device
2950 * @mm: the mm to bind, caller must hold a reference to it
2951 *
2952 * Create a bond between device and address space, allowing the device to access
2953 * the mm using the returned PASID. If a bond already exists between @device and
2954 * @mm, it is returned and an additional reference is taken. Caller must call
2955 * iommu_sva_unbind_device() to release each reference.
2956 *
2957 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2958 * initialize the required SVA features.
2959 *
2960 * On error, returns an ERR_PTR value.
2961 */
2962struct iommu_sva *
2963iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2964{
2965 struct iommu_group *group;
2966 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2967 const struct iommu_ops *ops = dev->bus->iommu_ops;
2968
2969 if (!ops || !ops->sva_bind)
2970 return ERR_PTR(-ENODEV);
2971
2972 group = iommu_group_get(dev);
2973 if (!group)
2974 return ERR_PTR(-ENODEV);
2975
2976 /* Ensure device count and domain don't change while we're binding */
2977 mutex_lock(&group->mutex);
2978
2979 /*
2980 * To keep things simple, SVA currently doesn't support IOMMU groups
2981 * with more than one device. Existing SVA-capable systems are not
2982 * affected by the problems that required IOMMU groups (lack of ACS
2983 * isolation, device ID aliasing and other hardware issues).
2984 */
2985 if (iommu_group_device_count(group) != 1)
2986 goto out_unlock;
2987
2988 handle = ops->sva_bind(dev, mm, drvdata);
2989
2990out_unlock:
2991 mutex_unlock(&group->mutex);
2992 iommu_group_put(group);
2993
2994 return handle;
2995}
2996EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2997
2998/**
2999 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
3000 * @handle: the handle returned by iommu_sva_bind_device()
3001 *
3002 * Put reference to a bond between device and address space. The device should
3003 * not be issuing any more transaction for this PASID. All outstanding page
3004 * requests for this PASID must have been flushed to the IOMMU.
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003005 */
3006void iommu_sva_unbind_device(struct iommu_sva *handle)
3007{
3008 struct iommu_group *group;
3009 struct device *dev = handle->dev;
3010 const struct iommu_ops *ops = dev->bus->iommu_ops;
3011
3012 if (!ops || !ops->sva_unbind)
3013 return;
3014
3015 group = iommu_group_get(dev);
3016 if (!group)
3017 return;
3018
3019 mutex_lock(&group->mutex);
3020 ops->sva_unbind(handle);
3021 mutex_unlock(&group->mutex);
3022
3023 iommu_group_put(group);
3024}
3025EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
3026
Fenghua Yuc7b6bac2020-09-15 09:30:05 -07003027u32 iommu_sva_get_pasid(struct iommu_sva *handle)
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003028{
3029 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
3030
3031 if (!ops || !ops->sva_get_pasid)
3032 return IOMMU_PASID_INVALID;
3033
3034 return ops->sva_get_pasid(handle);
3035}
3036EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
Sai Praneeth Prakhya08a27c12020-11-24 21:06:02 +08003037
3038/*
3039 * Changes the default domain of an iommu group that has *only* one device
3040 *
3041 * @group: The group for which the default domain should be changed
3042 * @prev_dev: The device in the group (this is used to make sure that the device
3043 * hasn't changed after the caller has called this function)
3044 * @type: The type of the new default domain that gets associated with the group
3045 *
3046 * Returns 0 on success and error code on failure
3047 *
3048 * Note:
3049 * 1. Presently, this function is called only when user requests to change the
3050 * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
3051 * Please take a closer look if intended to use for other purposes.
3052 */
3053static int iommu_change_dev_def_domain(struct iommu_group *group,
3054 struct device *prev_dev, int type)
3055{
3056 struct iommu_domain *prev_dom;
3057 struct group_device *grp_dev;
3058 int ret, dev_def_dom;
3059 struct device *dev;
3060
3061 if (!group)
3062 return -EINVAL;
3063
3064 mutex_lock(&group->mutex);
3065
3066 if (group->default_domain != group->domain) {
3067 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n");
3068 ret = -EBUSY;
3069 goto out;
3070 }
3071
3072 /*
3073 * iommu group wasn't locked while acquiring device lock in
3074 * iommu_group_store_type(). So, make sure that the device count hasn't
3075 * changed while acquiring device lock.
3076 *
3077 * Changing default domain of an iommu group with two or more devices
3078 * isn't supported because there could be a potential deadlock. Consider
3079 * the following scenario. T1 is trying to acquire device locks of all
3080 * the devices in the group and before it could acquire all of them,
3081 * there could be another thread T2 (from different sub-system and use
3082 * case) that has already acquired some of the device locks and might be
3083 * waiting for T1 to release other device locks.
3084 */
3085 if (iommu_group_device_count(group) != 1) {
3086 dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n");
3087 ret = -EINVAL;
3088 goto out;
3089 }
3090
3091 /* Since group has only one device */
3092 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3093 dev = grp_dev->dev;
3094
3095 if (prev_dev != dev) {
3096 dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n");
3097 ret = -EBUSY;
3098 goto out;
3099 }
3100
3101 prev_dom = group->default_domain;
3102 if (!prev_dom) {
3103 ret = -EINVAL;
3104 goto out;
3105 }
3106
3107 dev_def_dom = iommu_get_def_domain_type(dev);
3108 if (!type) {
3109 /*
3110 * If the user hasn't requested any specific type of domain and
3111 * if the device supports both the domains, then default to the
3112 * domain the device was booted with
3113 */
3114 type = dev_def_dom ? : iommu_def_domain_type;
3115 } else if (dev_def_dom && type != dev_def_dom) {
3116 dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n",
3117 iommu_domain_type_str(type));
3118 ret = -EINVAL;
3119 goto out;
3120 }
3121
3122 /*
3123 * Switch to a new domain only if the requested domain type is different
3124 * from the existing default domain type
3125 */
3126 if (prev_dom->type == type) {
3127 ret = 0;
3128 goto out;
3129 }
3130
3131 /* Sets group->default_domain to the newly allocated domain */
3132 ret = iommu_group_alloc_default_domain(dev->bus, group, type);
3133 if (ret)
3134 goto out;
3135
3136 ret = iommu_create_device_direct_mappings(group, dev);
3137 if (ret)
3138 goto free_new_domain;
3139
3140 ret = __iommu_attach_device(group->default_domain, dev);
3141 if (ret)
3142 goto free_new_domain;
3143
3144 group->domain = group->default_domain;
3145
3146 /*
3147 * Release the mutex here because ops->probe_finalize() call-back of
3148 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3149 * in-turn might call back into IOMMU core code, where it tries to take
3150 * group->mutex, resulting in a deadlock.
3151 */
3152 mutex_unlock(&group->mutex);
3153
3154 /* Make sure dma_ops is appropriatley set */
3155 iommu_group_do_probe_finalize(dev, group->default_domain);
3156 iommu_domain_free(prev_dom);
3157 return 0;
3158
3159free_new_domain:
3160 iommu_domain_free(group->default_domain);
3161 group->default_domain = prev_dom;
3162 group->domain = prev_dom;
3163
3164out:
3165 mutex_unlock(&group->mutex);
3166
3167 return ret;
3168}
3169
3170/*
3171 * Changing the default domain through sysfs requires the users to ubind the
3172 * drivers from the devices in the iommu group. Return failure if this doesn't
3173 * meet.
3174 *
3175 * We need to consider the race between this and the device release path.
3176 * device_lock(dev) is used here to guarantee that the device release path
3177 * will not be entered at the same time.
3178 */
3179static ssize_t iommu_group_store_type(struct iommu_group *group,
3180 const char *buf, size_t count)
3181{
3182 struct group_device *grp_dev;
3183 struct device *dev;
3184 int ret, req_type;
3185
3186 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3187 return -EACCES;
3188
3189 if (WARN_ON(!group))
3190 return -EINVAL;
3191
3192 if (sysfs_streq(buf, "identity"))
3193 req_type = IOMMU_DOMAIN_IDENTITY;
3194 else if (sysfs_streq(buf, "DMA"))
3195 req_type = IOMMU_DOMAIN_DMA;
3196 else if (sysfs_streq(buf, "auto"))
3197 req_type = 0;
3198 else
3199 return -EINVAL;
3200
3201 /*
3202 * Lock/Unlock the group mutex here before device lock to
3203 * 1. Make sure that the iommu group has only one device (this is a
3204 * prerequisite for step 2)
3205 * 2. Get struct *dev which is needed to lock device
3206 */
3207 mutex_lock(&group->mutex);
3208 if (iommu_group_device_count(group) != 1) {
3209 mutex_unlock(&group->mutex);
3210 pr_err_ratelimited("Cannot change default domain: Group has more than one device\n");
3211 return -EINVAL;
3212 }
3213
3214 /* Since group has only one device */
3215 grp_dev = list_first_entry(&group->devices, struct group_device, list);
3216 dev = grp_dev->dev;
3217 get_device(dev);
3218
3219 /*
3220 * Don't hold the group mutex because taking group mutex first and then
3221 * the device lock could potentially cause a deadlock as below. Assume
3222 * two threads T1 and T2. T1 is trying to change default domain of an
3223 * iommu group and T2 is trying to hot unplug a device or release [1] VF
3224 * of a PCIe device which is in the same iommu group. T1 takes group
3225 * mutex and before it could take device lock assume T2 has taken device
3226 * lock and is yet to take group mutex. Now, both the threads will be
3227 * waiting for the other thread to release lock. Below, lock order was
3228 * suggested.
3229 * device_lock(dev);
3230 * mutex_lock(&group->mutex);
3231 * iommu_change_dev_def_domain();
3232 * mutex_unlock(&group->mutex);
3233 * device_unlock(dev);
3234 *
3235 * [1] Typical device release path
3236 * device_lock() from device/driver core code
3237 * -> bus_notifier()
3238 * -> iommu_bus_notifier()
3239 * -> iommu_release_device()
3240 * -> ops->release_device() vendor driver calls back iommu core code
3241 * -> mutex_lock() from iommu core code
3242 */
3243 mutex_unlock(&group->mutex);
3244
3245 /* Check if the device in the group still has a driver bound to it */
3246 device_lock(dev);
3247 if (device_is_bound(dev)) {
3248 pr_err_ratelimited("Device is still bound to driver\n");
3249 ret = -EBUSY;
3250 goto out;
3251 }
3252
3253 ret = iommu_change_dev_def_domain(group, dev, req_type);
3254 ret = ret ?: count;
3255
3256out:
3257 device_unlock(dev);
3258 put_device(dev);
3259
3260 return ret;
3261}