blob: 6d847027d35eafb5edcb0175b7764f59696309dd [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);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020096
Alex Williamsond72e31c2012-05-30 14:18:53 -060097#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
98struct iommu_group_attribute iommu_group_attr_##_name = \
99 __ATTR(_name, _mode, _show, _store)
100
101#define to_iommu_group_attr(_attr) \
102 container_of(_attr, struct iommu_group_attribute, attr)
103#define to_iommu_group(_kobj) \
104 container_of(_kobj, struct iommu_group, kobj)
105
Joerg Roedelb0119e82017-02-01 13:23:08 +0100106static LIST_HEAD(iommu_device_list);
107static DEFINE_SPINLOCK(iommu_device_lock);
108
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200109/*
110 * Use a function instead of an array here because the domain-type is a
111 * bit-field, so an array would waste memory.
112 */
113static const char *iommu_domain_type_str(unsigned int t)
114{
115 switch (t) {
116 case IOMMU_DOMAIN_BLOCKED:
117 return "Blocked";
118 case IOMMU_DOMAIN_IDENTITY:
119 return "Passthrough";
120 case IOMMU_DOMAIN_UNMANAGED:
121 return "Unmanaged";
122 case IOMMU_DOMAIN_DMA:
123 return "Translated";
124 default:
125 return "Unknown";
126 }
127}
128
129static int __init iommu_subsys_init(void)
130{
Joerg Roedel22bb1822019-08-19 15:22:54 +0200131 bool cmd_line = iommu_cmd_line_dma_api();
132
133 if (!cmd_line) {
134 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
135 iommu_set_default_passthrough(false);
136 else
137 iommu_set_default_translated(false);
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200138
Joerg Roedel2896ba42019-09-03 15:15:44 +0200139 if (iommu_default_passthrough() && mem_encrypt_active()) {
140 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200141 iommu_set_default_translated(false);
142 }
Joerg Roedel22bb1822019-08-19 15:22:54 +0200143 }
144
145 pr_info("Default domain type: %s %s\n",
146 iommu_domain_type_str(iommu_def_domain_type),
147 cmd_line ? "(set via kernel command line)" : "");
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200148
149 return 0;
150}
151subsys_initcall(iommu_subsys_init);
152
Joerg Roedelb0119e82017-02-01 13:23:08 +0100153int iommu_device_register(struct iommu_device *iommu)
154{
155 spin_lock(&iommu_device_lock);
156 list_add_tail(&iommu->list, &iommu_device_list);
157 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100158 return 0;
159}
Will Deacona7ba5c32019-12-19 12:03:37 +0000160EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100161
162void iommu_device_unregister(struct iommu_device *iommu)
163{
164 spin_lock(&iommu_device_lock);
165 list_del(&iommu->list);
166 spin_unlock(&iommu_device_lock);
167}
Will Deacona7ba5c32019-12-19 12:03:37 +0000168EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100169
Joerg Roedel045a7042020-03-26 16:08:30 +0100170static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100171{
Joerg Roedel045a7042020-03-26 16:08:30 +0100172 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100173
174 if (param)
175 return param;
176
177 param = kzalloc(sizeof(*param), GFP_KERNEL);
178 if (!param)
179 return NULL;
180
181 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100182 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100183 return param;
184}
185
Joerg Roedel045a7042020-03-26 16:08:30 +0100186static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100187{
Kevin Hao5375e872020-04-02 22:37:49 +0800188 iommu_fwspec_free(dev);
Joerg Roedel045a7042020-03-26 16:08:30 +0100189 kfree(dev->iommu);
190 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100191}
192
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200193static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100194{
195 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200196 struct iommu_device *iommu_dev;
197 struct iommu_group *group;
Jacob Pan0c830e62019-06-03 15:57:48 +0100198 int ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100199
Jacob Pan0c830e62019-06-03 15:57:48 +0100200 if (!ops)
Thierry Redingf38338c2020-05-11 18:10:00 +0200201 return -ENODEV;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100202
Joerg Roedel045a7042020-03-26 16:08:30 +0100203 if (!dev_iommu_get(dev))
Jacob Pan0c830e62019-06-03 15:57:48 +0100204 return -ENOMEM;
205
Will Deacon25f003d2019-12-19 12:03:41 +0000206 if (!try_module_get(ops->owner)) {
207 ret = -EINVAL;
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200208 goto err_free;
Will Deacon25f003d2019-12-19 12:03:41 +0000209 }
210
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200211 iommu_dev = ops->probe_device(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200212 if (IS_ERR(iommu_dev)) {
213 ret = PTR_ERR(iommu_dev);
214 goto out_module_put;
215 }
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200216
217 dev->iommu->iommu_dev = iommu_dev;
218
219 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200220 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200221 ret = PTR_ERR(group);
222 goto out_release;
223 }
224 iommu_group_put(group);
225
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200226 if (group_list && !group->default_domain && list_empty(&group->entry))
227 list_add_tail(&group->entry, group_list);
228
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200229 iommu_device_link(iommu_dev, dev);
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100230
Will Deacon25f003d2019-12-19 12:03:41 +0000231 return 0;
232
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200233out_release:
234 ops->release_device(dev);
235
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200236out_module_put:
Will Deacon25f003d2019-12-19 12:03:41 +0000237 module_put(ops->owner);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200238
239err_free:
Joerg Roedel045a7042020-03-26 16:08:30 +0100240 dev_iommu_free(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200241
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100242 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100243}
244
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200245int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200246{
247 const struct iommu_ops *ops = dev->bus->iommu_ops;
248 struct iommu_group *group;
249 int ret;
250
251 ret = __iommu_probe_device(dev, NULL);
252 if (ret)
253 goto err_out;
254
Joerg Roedel79659190e2020-05-25 15:01:22 +0200255 group = iommu_group_get(dev);
256 if (!group)
257 goto err_release;
258
Joerg Roedelcf193882020-04-29 15:36:48 +0200259 /*
260 * Try to allocate a default domain - needs support from the
261 * IOMMU driver. There are still some drivers which don't
262 * support default domains, so the return value is not yet
263 * checked.
264 */
Joerg Roedel79659190e2020-05-25 15:01:22 +0200265 iommu_alloc_default_domain(group, dev);
Joerg Roedelcf193882020-04-29 15:36:48 +0200266
267 if (group->default_domain)
268 ret = __iommu_attach_device(group->default_domain, dev);
269
Joerg Roedelce574c22020-04-29 15:36:50 +0200270 iommu_create_device_direct_mappings(group, dev);
271
Joerg Roedelcf193882020-04-29 15:36:48 +0200272 iommu_group_put(group);
273
274 if (ret)
275 goto err_release;
276
277 if (ops->probe_finalize)
278 ops->probe_finalize(dev);
279
280 return 0;
281
282err_release:
283 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200284
Joerg Roedelcf193882020-04-29 15:36:48 +0200285err_out:
286 return ret;
287
288}
289
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100290void iommu_release_device(struct device *dev)
291{
292 const struct iommu_ops *ops = dev->bus->iommu_ops;
293
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200294 if (!dev->iommu)
295 return;
Jacob Pan0c830e62019-06-03 15:57:48 +0100296
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200297 iommu_device_unlink(dev->iommu->iommu_dev, dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200298
299 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100300
Qian Cai9ac85452020-07-03 20:10:03 -0400301 iommu_group_remove_device(dev);
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200302 module_put(ops->owner);
303 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100304}
305
Will Deaconfccb4e32017-01-05 18:38:26 +0000306static int __init iommu_set_def_domain_type(char *str)
307{
308 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300309 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000310
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300311 ret = kstrtobool(str, &pt);
312 if (ret)
313 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000314
Joerg Roedeladab0b02019-08-19 15:22:48 +0200315 if (pt)
316 iommu_set_default_passthrough(true);
317 else
318 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200319
Will Deaconfccb4e32017-01-05 18:38:26 +0000320 return 0;
321}
322early_param("iommu.passthrough", iommu_set_def_domain_type);
323
Zhen Lei68a6efe2018-09-20 17:10:23 +0100324static int __init iommu_dma_setup(char *str)
325{
326 return kstrtobool(str, &iommu_dma_strict);
327}
328early_param("iommu.strict", iommu_dma_setup);
329
Alex Williamsond72e31c2012-05-30 14:18:53 -0600330static ssize_t iommu_group_attr_show(struct kobject *kobj,
331 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400332{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600333 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
334 struct iommu_group *group = to_iommu_group(kobj);
335 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400336
Alex Williamsond72e31c2012-05-30 14:18:53 -0600337 if (attr->show)
338 ret = attr->show(group, buf);
339 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400340}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600341
342static ssize_t iommu_group_attr_store(struct kobject *kobj,
343 struct attribute *__attr,
344 const char *buf, size_t count)
345{
346 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
347 struct iommu_group *group = to_iommu_group(kobj);
348 ssize_t ret = -EIO;
349
350 if (attr->store)
351 ret = attr->store(group, buf, count);
352 return ret;
353}
354
355static const struct sysfs_ops iommu_group_sysfs_ops = {
356 .show = iommu_group_attr_show,
357 .store = iommu_group_attr_store,
358};
359
360static int iommu_group_create_file(struct iommu_group *group,
361 struct iommu_group_attribute *attr)
362{
363 return sysfs_create_file(&group->kobj, &attr->attr);
364}
365
366static void iommu_group_remove_file(struct iommu_group *group,
367 struct iommu_group_attribute *attr)
368{
369 sysfs_remove_file(&group->kobj, &attr->attr);
370}
371
372static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
373{
374 return sprintf(buf, "%s\n", group->name);
375}
376
Eric Auger6c65fb32017-01-19 20:57:51 +0000377/**
378 * iommu_insert_resv_region - Insert a new region in the
379 * list of reserved regions.
380 * @new: new region to insert
381 * @regions: list of regions
382 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200383 * Elements are sorted by start address and overlapping segments
384 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000385 */
Wei Yongjun1b0b2a82020-07-13 22:25:42 +0800386static int iommu_insert_resv_region(struct iommu_resv_region *new,
387 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000388{
Eric Auger4dbd2582019-08-21 14:09:40 +0200389 struct iommu_resv_region *iter, *tmp, *nr, *top;
390 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000391
Eric Auger4dbd2582019-08-21 14:09:40 +0200392 nr = iommu_alloc_resv_region(new->start, new->length,
393 new->prot, new->type);
394 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000395 return -ENOMEM;
396
Eric Auger4dbd2582019-08-21 14:09:40 +0200397 /* First add the new element based on start address sorting */
398 list_for_each_entry(iter, regions, list) {
399 if (nr->start < iter->start ||
400 (nr->start == iter->start && nr->type <= iter->type))
401 break;
402 }
403 list_add_tail(&nr->list, &iter->list);
404
405 /* Merge overlapping segments of type nr->type in @regions, if any */
406 list_for_each_entry_safe(iter, tmp, regions, list) {
407 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
408
Eric Auger4c80ba32019-11-26 18:54:13 +0100409 /* no merge needed on elements of different types than @new */
410 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200411 list_move_tail(&iter->list, &stack);
412 continue;
413 }
414
415 /* look for the last stack element of same type as @iter */
416 list_for_each_entry_reverse(top, &stack, list)
417 if (top->type == iter->type)
418 goto check_overlap;
419
420 list_move_tail(&iter->list, &stack);
421 continue;
422
423check_overlap:
424 top_end = top->start + top->length - 1;
425
426 if (iter->start > top_end + 1) {
427 list_move_tail(&iter->list, &stack);
428 } else {
429 top->length = max(top_end, iter_end) - top->start + 1;
430 list_del(&iter->list);
431 kfree(iter);
432 }
433 }
434 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000435 return 0;
436}
437
438static int
439iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
440 struct list_head *group_resv_regions)
441{
442 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100443 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000444
445 list_for_each_entry(entry, dev_resv_regions, list) {
446 ret = iommu_insert_resv_region(entry, group_resv_regions);
447 if (ret)
448 break;
449 }
450 return ret;
451}
452
453int iommu_get_group_resv_regions(struct iommu_group *group,
454 struct list_head *head)
455{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100456 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000457 int ret = 0;
458
459 mutex_lock(&group->mutex);
460 list_for_each_entry(device, &group->devices, list) {
461 struct list_head dev_resv_regions;
462
463 INIT_LIST_HEAD(&dev_resv_regions);
464 iommu_get_resv_regions(device->dev, &dev_resv_regions);
465 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
466 iommu_put_resv_regions(device->dev, &dev_resv_regions);
467 if (ret)
468 break;
469 }
470 mutex_unlock(&group->mutex);
471 return ret;
472}
473EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
474
Eric Augerbc7d12b92017-01-19 20:57:52 +0000475static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
476 char *buf)
477{
478 struct iommu_resv_region *region, *next;
479 struct list_head group_resv_regions;
480 char *str = buf;
481
482 INIT_LIST_HEAD(&group_resv_regions);
483 iommu_get_group_resv_regions(group, &group_resv_regions);
484
485 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
486 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
487 (long long int)region->start,
488 (long long int)(region->start +
489 region->length - 1),
490 iommu_group_resv_type_string[region->type]);
491 kfree(region);
492 }
493
494 return (str - buf);
495}
496
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700497static ssize_t iommu_group_show_type(struct iommu_group *group,
498 char *buf)
499{
500 char *type = "unknown\n";
501
502 if (group->default_domain) {
503 switch (group->default_domain->type) {
504 case IOMMU_DOMAIN_BLOCKED:
505 type = "blocked\n";
506 break;
507 case IOMMU_DOMAIN_IDENTITY:
508 type = "identity\n";
509 break;
510 case IOMMU_DOMAIN_UNMANAGED:
511 type = "unmanaged\n";
512 break;
513 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800514 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700515 break;
516 }
517 }
518 strcpy(buf, type);
519
520 return strlen(type);
521}
522
Alex Williamsond72e31c2012-05-30 14:18:53 -0600523static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
524
Eric Augerbc7d12b92017-01-19 20:57:52 +0000525static IOMMU_GROUP_ATTR(reserved_regions, 0444,
526 iommu_group_show_resv_regions, NULL);
527
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700528static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
529
Alex Williamsond72e31c2012-05-30 14:18:53 -0600530static void iommu_group_release(struct kobject *kobj)
531{
532 struct iommu_group *group = to_iommu_group(kobj);
533
Joerg Roedel269aa802015-05-28 18:41:25 +0200534 pr_debug("Releasing group %d\n", group->id);
535
Alex Williamsond72e31c2012-05-30 14:18:53 -0600536 if (group->iommu_data_release)
537 group->iommu_data_release(group->iommu_data);
538
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200539 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600540
Joerg Roedel53723dc2015-05-28 18:41:29 +0200541 if (group->default_domain)
542 iommu_domain_free(group->default_domain);
543
Alex Williamsond72e31c2012-05-30 14:18:53 -0600544 kfree(group->name);
545 kfree(group);
546}
547
548static struct kobj_type iommu_group_ktype = {
549 .sysfs_ops = &iommu_group_sysfs_ops,
550 .release = iommu_group_release,
551};
552
553/**
554 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600555 *
556 * This function is called by an iommu driver to allocate a new iommu
557 * group. The iommu group represents the minimum granularity of the iommu.
558 * Upon successful return, the caller holds a reference to the supplied
559 * group in order to hold the group until devices are added. Use
560 * iommu_group_put() to release this extra reference count, allowing the
561 * group to be automatically reclaimed once it has no devices or external
562 * references.
563 */
564struct iommu_group *iommu_group_alloc(void)
565{
566 struct iommu_group *group;
567 int ret;
568
569 group = kzalloc(sizeof(*group), GFP_KERNEL);
570 if (!group)
571 return ERR_PTR(-ENOMEM);
572
573 group->kobj.kset = iommu_group_kset;
574 mutex_init(&group->mutex);
575 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200576 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600577 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
578
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200579 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
580 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600581 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200582 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600583 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200584 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600585
586 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
587 NULL, "%d", group->id);
588 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200589 ida_simple_remove(&iommu_group_ida, group->id);
Qiushi Wu7cc31612020-05-27 16:00:19 -0500590 kobject_put(&group->kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600591 return ERR_PTR(ret);
592 }
593
594 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
595 if (!group->devices_kobj) {
596 kobject_put(&group->kobj); /* triggers .release & free */
597 return ERR_PTR(-ENOMEM);
598 }
599
600 /*
601 * The devices_kobj holds a reference on the group kobject, so
602 * as long as that exists so will the group. We can therefore
603 * use the devices_kobj for reference counting.
604 */
605 kobject_put(&group->kobj);
606
Eric Augerbc7d12b92017-01-19 20:57:52 +0000607 ret = iommu_group_create_file(group,
608 &iommu_group_attr_reserved_regions);
609 if (ret)
610 return ERR_PTR(ret);
611
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700612 ret = iommu_group_create_file(group, &iommu_group_attr_type);
613 if (ret)
614 return ERR_PTR(ret);
615
Joerg Roedel269aa802015-05-28 18:41:25 +0200616 pr_debug("Allocated group %d\n", group->id);
617
Alex Williamsond72e31c2012-05-30 14:18:53 -0600618 return group;
619}
620EXPORT_SYMBOL_GPL(iommu_group_alloc);
621
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100622struct iommu_group *iommu_group_get_by_id(int id)
623{
624 struct kobject *group_kobj;
625 struct iommu_group *group;
626 const char *name;
627
628 if (!iommu_group_kset)
629 return NULL;
630
631 name = kasprintf(GFP_KERNEL, "%d", id);
632 if (!name)
633 return NULL;
634
635 group_kobj = kset_find_obj(iommu_group_kset, name);
636 kfree(name);
637
638 if (!group_kobj)
639 return NULL;
640
641 group = container_of(group_kobj, struct iommu_group, kobj);
642 BUG_ON(group->id != id);
643
644 kobject_get(group->devices_kobj);
645 kobject_put(&group->kobj);
646
647 return group;
648}
649EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
650
Alex Williamsond72e31c2012-05-30 14:18:53 -0600651/**
652 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
653 * @group: the group
654 *
655 * iommu drivers can store data in the group for use when doing iommu
656 * operations. This function provides a way to retrieve it. Caller
657 * should hold a group reference.
658 */
659void *iommu_group_get_iommudata(struct iommu_group *group)
660{
661 return group->iommu_data;
662}
663EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
664
665/**
666 * iommu_group_set_iommudata - set iommu_data for a group
667 * @group: the group
668 * @iommu_data: new data
669 * @release: release function for iommu_data
670 *
671 * iommu drivers can store data in the group for use when doing iommu
672 * operations. This function provides a way to set the data after
673 * the group has been allocated. Caller should hold a group reference.
674 */
675void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
676 void (*release)(void *iommu_data))
677{
678 group->iommu_data = iommu_data;
679 group->iommu_data_release = release;
680}
681EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
682
683/**
684 * iommu_group_set_name - set name for a group
685 * @group: the group
686 * @name: name
687 *
688 * Allow iommu driver to set a name for a group. When set it will
689 * appear in a name attribute file under the group in sysfs.
690 */
691int iommu_group_set_name(struct iommu_group *group, const char *name)
692{
693 int ret;
694
695 if (group->name) {
696 iommu_group_remove_file(group, &iommu_group_attr_name);
697 kfree(group->name);
698 group->name = NULL;
699 if (!name)
700 return 0;
701 }
702
703 group->name = kstrdup(name, GFP_KERNEL);
704 if (!group->name)
705 return -ENOMEM;
706
707 ret = iommu_group_create_file(group, &iommu_group_attr_name);
708 if (ret) {
709 kfree(group->name);
710 group->name = NULL;
711 return ret;
712 }
713
714 return 0;
715}
716EXPORT_SYMBOL_GPL(iommu_group_set_name);
717
Joerg Roedelce574c22020-04-29 15:36:50 +0200718static int iommu_create_device_direct_mappings(struct iommu_group *group,
719 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200720{
721 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000722 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200723 struct list_head mappings;
724 unsigned long pg_size;
725 int ret = 0;
726
727 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
728 return 0;
729
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100730 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200731
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100732 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200733 INIT_LIST_HEAD(&mappings);
734
Eric Augere5b52342017-01-19 20:57:47 +0000735 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200736
737 /* We need to consider overlapping regions for different devices */
738 list_for_each_entry(entry, &mappings, list) {
739 dma_addr_t start, end, addr;
740
Eric Augere5b52342017-01-19 20:57:47 +0000741 if (domain->ops->apply_resv_region)
742 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200743
Joerg Roedelbeed2822015-05-28 18:41:34 +0200744 start = ALIGN(entry->start, pg_size);
745 end = ALIGN(entry->start + entry->length, pg_size);
746
Eric Augeradfd3732019-06-03 08:53:35 +0200747 if (entry->type != IOMMU_RESV_DIRECT &&
748 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000749 continue;
750
Joerg Roedelbeed2822015-05-28 18:41:34 +0200751 for (addr = start; addr < end; addr += pg_size) {
752 phys_addr_t phys_addr;
753
754 phys_addr = iommu_iova_to_phys(domain, addr);
755 if (phys_addr)
756 continue;
757
758 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
759 if (ret)
760 goto out;
761 }
762
763 }
764
Tom Murphyaae4c8e2020-08-17 22:00:49 +0100765 iommu_flush_iotlb_all(domain);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200766
Joerg Roedelbeed2822015-05-28 18:41:34 +0200767out:
Eric Augere5b52342017-01-19 20:57:47 +0000768 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200769
770 return ret;
771}
772
Joerg Roedelbd421262020-05-19 15:03:40 +0200773static bool iommu_is_attach_deferred(struct iommu_domain *domain,
774 struct device *dev)
775{
776 if (domain->ops->is_attach_deferred)
777 return domain->ops->is_attach_deferred(domain, dev);
778
779 return false;
780}
781
Alex Williamsond72e31c2012-05-30 14:18:53 -0600782/**
783 * iommu_group_add_device - add a device to an iommu group
784 * @group: the group into which to add the device (reference should be held)
785 * @dev: the device
786 *
787 * This function is called by an iommu driver to add a device into a
788 * group. Adding a device increments the group reference count.
789 */
790int iommu_group_add_device(struct iommu_group *group, struct device *dev)
791{
792 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100793 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600794
795 device = kzalloc(sizeof(*device), GFP_KERNEL);
796 if (!device)
797 return -ENOMEM;
798
799 device->dev = dev;
800
801 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000802 if (ret)
803 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600804
805 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
806rename:
807 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000808 ret = -ENOMEM;
809 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600810 }
811
812 ret = sysfs_create_link_nowarn(group->devices_kobj,
813 &dev->kobj, device->name);
814 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600815 if (ret == -EEXIST && i >= 0) {
816 /*
817 * Account for the slim chance of collision
818 * and append an instance to the name.
819 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000820 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600821 device->name = kasprintf(GFP_KERNEL, "%s.%d",
822 kobject_name(&dev->kobj), i++);
823 goto rename;
824 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000825 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600826 }
827
828 kobject_get(group->devices_kobj);
829
830 dev->iommu_group = group;
831
832 mutex_lock(&group->mutex);
833 list_add_tail(&device->list, &group->devices);
Joerg Roedelbd421262020-05-19 15:03:40 +0200834 if (group->domain && !iommu_is_attach_deferred(group->domain, dev))
Robin Murphy797a8b42017-01-16 12:58:07 +0000835 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600836 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000837 if (ret)
838 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600839
840 /* Notify any listeners about change to group. */
841 blocking_notifier_call_chain(&group->notifier,
842 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600843
844 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200845
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600846 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200847
Alex Williamsond72e31c2012-05-30 14:18:53 -0600848 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000849
850err_put_group:
851 mutex_lock(&group->mutex);
852 list_del(&device->list);
853 mutex_unlock(&group->mutex);
854 dev->iommu_group = NULL;
855 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700856 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000857err_free_name:
858 kfree(device->name);
859err_remove_link:
860 sysfs_remove_link(&dev->kobj, "iommu_group");
861err_free_device:
862 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600863 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000864 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600865}
866EXPORT_SYMBOL_GPL(iommu_group_add_device);
867
868/**
869 * iommu_group_remove_device - remove a device from it's current group
870 * @dev: device to be removed
871 *
872 * This function is called by an iommu driver to remove the device from
873 * it's current group. This decrements the iommu group reference count.
874 */
875void iommu_group_remove_device(struct device *dev)
876{
877 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100878 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600879
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600880 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200881
Alex Williamsond72e31c2012-05-30 14:18:53 -0600882 /* Pre-notify listeners that a device is being removed. */
883 blocking_notifier_call_chain(&group->notifier,
884 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
885
886 mutex_lock(&group->mutex);
887 list_for_each_entry(tmp_device, &group->devices, list) {
888 if (tmp_device->dev == dev) {
889 device = tmp_device;
890 list_del(&device->list);
891 break;
892 }
893 }
894 mutex_unlock(&group->mutex);
895
896 if (!device)
897 return;
898
899 sysfs_remove_link(group->devices_kobj, device->name);
900 sysfs_remove_link(&dev->kobj, "iommu_group");
901
Shuah Khan2e757082013-08-15 11:59:25 -0600902 trace_remove_device_from_group(group->id, dev);
903
Alex Williamsond72e31c2012-05-30 14:18:53 -0600904 kfree(device->name);
905 kfree(device);
906 dev->iommu_group = NULL;
907 kobject_put(group->devices_kobj);
908}
909EXPORT_SYMBOL_GPL(iommu_group_remove_device);
910
Joerg Roedel426a2732015-05-28 18:41:30 +0200911static int iommu_group_device_count(struct iommu_group *group)
912{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100913 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200914 int ret = 0;
915
916 list_for_each_entry(entry, &group->devices, list)
917 ret++;
918
919 return ret;
920}
921
Alex Williamsond72e31c2012-05-30 14:18:53 -0600922/**
923 * iommu_group_for_each_dev - iterate over each device in the group
924 * @group: the group
925 * @data: caller opaque data to be passed to callback function
926 * @fn: caller supplied callback function
927 *
928 * This function is called by group users to iterate over group devices.
929 * Callers should hold a reference count to the group during callback.
930 * The group->mutex is held across callbacks, which will block calls to
931 * iommu_group_add/remove_device.
932 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200933static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
934 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600935{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100936 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600937 int ret = 0;
938
Alex Williamsond72e31c2012-05-30 14:18:53 -0600939 list_for_each_entry(device, &group->devices, list) {
940 ret = fn(device->dev, data);
941 if (ret)
942 break;
943 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200944 return ret;
945}
946
947
948int iommu_group_for_each_dev(struct iommu_group *group, void *data,
949 int (*fn)(struct device *, void *))
950{
951 int ret;
952
953 mutex_lock(&group->mutex);
954 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600955 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200956
Alex Williamsond72e31c2012-05-30 14:18:53 -0600957 return ret;
958}
959EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
960
961/**
962 * iommu_group_get - Return the group for a device and increment reference
963 * @dev: get the group that this device belongs to
964 *
965 * This function is called by iommu drivers and users to get the group
966 * for the specified device. If found, the group is returned and the group
967 * reference in incremented, else NULL.
968 */
969struct iommu_group *iommu_group_get(struct device *dev)
970{
971 struct iommu_group *group = dev->iommu_group;
972
973 if (group)
974 kobject_get(group->devices_kobj);
975
976 return group;
977}
978EXPORT_SYMBOL_GPL(iommu_group_get);
979
980/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000981 * iommu_group_ref_get - Increment reference on a group
982 * @group: the group to use, must not be NULL
983 *
984 * This function is called by iommu drivers to take additional references on an
985 * existing group. Returns the given group for convenience.
986 */
987struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
988{
989 kobject_get(group->devices_kobj);
990 return group;
991}
Will Deacona7ba5c32019-12-19 12:03:37 +0000992EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +0000993
994/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600995 * iommu_group_put - Decrement group reference
996 * @group: the group to use
997 *
998 * This function is called by iommu drivers and users to release the
999 * iommu group. Once the reference count is zero, the group is released.
1000 */
1001void iommu_group_put(struct iommu_group *group)
1002{
1003 if (group)
1004 kobject_put(group->devices_kobj);
1005}
1006EXPORT_SYMBOL_GPL(iommu_group_put);
1007
1008/**
1009 * iommu_group_register_notifier - Register a notifier for group changes
1010 * @group: the group to watch
1011 * @nb: notifier block to signal
1012 *
1013 * This function allows iommu group users to track changes in a group.
1014 * See include/linux/iommu.h for actions sent via this notifier. Caller
1015 * should hold a reference to the group throughout notifier registration.
1016 */
1017int iommu_group_register_notifier(struct iommu_group *group,
1018 struct notifier_block *nb)
1019{
1020 return blocking_notifier_chain_register(&group->notifier, nb);
1021}
1022EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1023
1024/**
1025 * iommu_group_unregister_notifier - Unregister a notifier
1026 * @group: the group to watch
1027 * @nb: notifier block to signal
1028 *
1029 * Unregister a previously registered group notifier block.
1030 */
1031int iommu_group_unregister_notifier(struct iommu_group *group,
1032 struct notifier_block *nb)
1033{
1034 return blocking_notifier_chain_unregister(&group->notifier, nb);
1035}
1036EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1037
1038/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001039 * iommu_register_device_fault_handler() - Register a device fault handler
1040 * @dev: the device
1041 * @handler: the fault handler
1042 * @data: private data passed as argument to the handler
1043 *
1044 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001045 * fault event and data as argument. The handler should return 0 on success. If
1046 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1047 * complete the fault by calling iommu_page_response() with one of the following
1048 * response code:
1049 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1050 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1051 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1052 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001053 *
1054 * Return 0 if the fault handler was installed successfully, or an error.
1055 */
1056int iommu_register_device_fault_handler(struct device *dev,
1057 iommu_dev_fault_handler_t handler,
1058 void *data)
1059{
Joerg Roedel045a7042020-03-26 16:08:30 +01001060 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001061 int ret = 0;
1062
1063 if (!param)
1064 return -EINVAL;
1065
1066 mutex_lock(&param->lock);
1067 /* Only allow one fault handler registered for each device */
1068 if (param->fault_param) {
1069 ret = -EBUSY;
1070 goto done_unlock;
1071 }
1072
1073 get_device(dev);
1074 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1075 if (!param->fault_param) {
1076 put_device(dev);
1077 ret = -ENOMEM;
1078 goto done_unlock;
1079 }
1080 param->fault_param->handler = handler;
1081 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001082 mutex_init(&param->fault_param->lock);
1083 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001084
1085done_unlock:
1086 mutex_unlock(&param->lock);
1087
1088 return ret;
1089}
1090EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1091
1092/**
1093 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1094 * @dev: the device
1095 *
1096 * Remove the device fault handler installed with
1097 * iommu_register_device_fault_handler().
1098 *
1099 * Return 0 on success, or an error.
1100 */
1101int iommu_unregister_device_fault_handler(struct device *dev)
1102{
Joerg Roedel045a7042020-03-26 16:08:30 +01001103 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001104 int ret = 0;
1105
1106 if (!param)
1107 return -EINVAL;
1108
1109 mutex_lock(&param->lock);
1110
1111 if (!param->fault_param)
1112 goto unlock;
1113
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001114 /* we cannot unregister handler if there are pending faults */
1115 if (!list_empty(&param->fault_param->faults)) {
1116 ret = -EBUSY;
1117 goto unlock;
1118 }
1119
Jacob Pan0c830e62019-06-03 15:57:48 +01001120 kfree(param->fault_param);
1121 param->fault_param = NULL;
1122 put_device(dev);
1123unlock:
1124 mutex_unlock(&param->lock);
1125
1126 return ret;
1127}
1128EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1129
1130/**
1131 * iommu_report_device_fault() - Report fault event to device driver
1132 * @dev: the device
1133 * @evt: fault event data
1134 *
1135 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001136 * handler. When this function fails and the fault is recoverable, it is the
1137 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001138 *
1139 * Return 0 on success, or an error.
1140 */
1141int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1142{
Joerg Roedel045a7042020-03-26 16:08:30 +01001143 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001144 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001145 struct iommu_fault_param *fparam;
1146 int ret = 0;
1147
1148 if (!param || !evt)
1149 return -EINVAL;
1150
1151 /* we only report device fault if there is a handler registered */
1152 mutex_lock(&param->lock);
1153 fparam = param->fault_param;
1154 if (!fparam || !fparam->handler) {
1155 ret = -EINVAL;
1156 goto done_unlock;
1157 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001158
1159 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1160 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1161 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1162 GFP_KERNEL);
1163 if (!evt_pending) {
1164 ret = -ENOMEM;
1165 goto done_unlock;
1166 }
1167 mutex_lock(&fparam->lock);
1168 list_add_tail(&evt_pending->list, &fparam->faults);
1169 mutex_unlock(&fparam->lock);
1170 }
1171
Jacob Pan0c830e62019-06-03 15:57:48 +01001172 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001173 if (ret && evt_pending) {
1174 mutex_lock(&fparam->lock);
1175 list_del(&evt_pending->list);
1176 mutex_unlock(&fparam->lock);
1177 kfree(evt_pending);
1178 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001179done_unlock:
1180 mutex_unlock(&param->lock);
1181 return ret;
1182}
1183EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1184
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001185int iommu_page_response(struct device *dev,
1186 struct iommu_page_response *msg)
1187{
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001188 bool needs_pasid;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001189 int ret = -EINVAL;
1190 struct iommu_fault_event *evt;
1191 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001192 struct dev_iommu *param = dev->iommu;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001193 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001194 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1195
1196 if (!domain || !domain->ops->page_response)
1197 return -ENODEV;
1198
1199 if (!param || !param->fault_param)
1200 return -EINVAL;
1201
1202 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1203 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1204 return -EINVAL;
1205
1206 /* Only send response if there is a fault report pending */
1207 mutex_lock(&param->fault_param->lock);
1208 if (list_empty(&param->fault_param->faults)) {
1209 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1210 goto done_unlock;
1211 }
1212 /*
1213 * Check if we have a matching page request pending to respond,
1214 * otherwise return -EINVAL
1215 */
1216 list_for_each_entry(evt, &param->fault_param->faults, list) {
1217 prm = &evt->fault.prm;
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001218 if (prm->grpid != msg->grpid)
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001219 continue;
1220
Jean-Philippe Brucker970471912020-06-16 16:47:14 +02001221 /*
1222 * If the PASID is required, the corresponding request is
1223 * matched using the group ID, the PASID valid bit and the PASID
1224 * value. Otherwise only the group ID matches request and
1225 * response.
1226 */
1227 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1228 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1229 continue;
1230
1231 if (!needs_pasid && has_pasid) {
1232 /* No big deal, just clear it. */
1233 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1234 msg->pasid = 0;
1235 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001236
1237 ret = domain->ops->page_response(dev, evt, msg);
1238 list_del(&evt->list);
1239 kfree(evt);
1240 break;
1241 }
1242
1243done_unlock:
1244 mutex_unlock(&param->fault_param->lock);
1245 return ret;
1246}
1247EXPORT_SYMBOL_GPL(iommu_page_response);
1248
Jacob Pan0c830e62019-06-03 15:57:48 +01001249/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001250 * iommu_group_id - Return ID for a group
1251 * @group: the group to ID
1252 *
1253 * Return the unique ID for the group matching the sysfs group number.
1254 */
1255int iommu_group_id(struct iommu_group *group)
1256{
1257 return group->id;
1258}
1259EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001260
Alex Williamsonf096c062014-09-19 10:03:06 -06001261static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1262 unsigned long *devfns);
1263
Alex Williamson104a1c12014-07-03 09:51:18 -06001264/*
1265 * To consider a PCI device isolated, we require ACS to support Source
1266 * Validation, Request Redirection, Completer Redirection, and Upstream
1267 * Forwarding. This effectively means that devices cannot spoof their
1268 * requester ID, requests and completions cannot be redirected, and all
1269 * transactions are forwarded upstream, even as it passes through a
1270 * bridge where the target device is downstream.
1271 */
1272#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1273
Alex Williamsonf096c062014-09-19 10:03:06 -06001274/*
1275 * For multifunction devices which are not isolated from each other, find
1276 * all the other non-isolated functions and look for existing groups. For
1277 * each function, we also need to look for aliases to or from other devices
1278 * that may already have a group.
1279 */
1280static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1281 unsigned long *devfns)
1282{
1283 struct pci_dev *tmp = NULL;
1284 struct iommu_group *group;
1285
1286 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1287 return NULL;
1288
1289 for_each_pci_dev(tmp) {
1290 if (tmp == pdev || tmp->bus != pdev->bus ||
1291 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1292 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1293 continue;
1294
1295 group = get_pci_alias_group(tmp, devfns);
1296 if (group) {
1297 pci_dev_put(tmp);
1298 return group;
1299 }
1300 }
1301
1302 return NULL;
1303}
1304
1305/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001306 * Look for aliases to or from the given device for existing groups. DMA
1307 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001308 * space is quite small (especially since we're really only looking at pcie
1309 * device, and therefore only expect multiple slots on the root complex or
1310 * downstream switch ports). It's conceivable though that a pair of
1311 * multifunction devices could have aliases between them that would cause a
1312 * loop. To prevent this, we use a bitmap to track where we've been.
1313 */
1314static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1315 unsigned long *devfns)
1316{
1317 struct pci_dev *tmp = NULL;
1318 struct iommu_group *group;
1319
1320 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1321 return NULL;
1322
1323 group = iommu_group_get(&pdev->dev);
1324 if (group)
1325 return group;
1326
1327 for_each_pci_dev(tmp) {
1328 if (tmp == pdev || tmp->bus != pdev->bus)
1329 continue;
1330
1331 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001332 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001333 group = get_pci_alias_group(tmp, devfns);
1334 if (group) {
1335 pci_dev_put(tmp);
1336 return group;
1337 }
1338
1339 group = get_pci_function_alias_group(tmp, devfns);
1340 if (group) {
1341 pci_dev_put(tmp);
1342 return group;
1343 }
1344 }
1345 }
1346
1347 return NULL;
1348}
1349
Alex Williamson104a1c12014-07-03 09:51:18 -06001350struct group_for_pci_data {
1351 struct pci_dev *pdev;
1352 struct iommu_group *group;
1353};
1354
1355/*
1356 * DMA alias iterator callback, return the last seen device. Stop and return
1357 * the IOMMU group if we find one along the way.
1358 */
1359static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1360{
1361 struct group_for_pci_data *data = opaque;
1362
1363 data->pdev = pdev;
1364 data->group = iommu_group_get(&pdev->dev);
1365
1366 return data->group != NULL;
1367}
1368
1369/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001370 * Generic device_group call-back function. It just allocates one
1371 * iommu-group per device.
1372 */
1373struct iommu_group *generic_device_group(struct device *dev)
1374{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001375 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001376}
Will Deacona7ba5c32019-12-19 12:03:37 +00001377EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001378
1379/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001380 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1381 * to find or create an IOMMU group for a device.
1382 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001383struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001384{
Joerg Roedel5e622922015-10-21 23:51:37 +02001385 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001386 struct group_for_pci_data data;
1387 struct pci_bus *bus;
1388 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001389 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001390
Joerg Roedel5e622922015-10-21 23:51:37 +02001391 if (WARN_ON(!dev_is_pci(dev)))
1392 return ERR_PTR(-EINVAL);
1393
Alex Williamson104a1c12014-07-03 09:51:18 -06001394 /*
1395 * Find the upstream DMA alias for the device. A device must not
1396 * be aliased due to topology in order to have its own IOMMU group.
1397 * If we find an alias along the way that already belongs to a
1398 * group, use it.
1399 */
1400 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1401 return data.group;
1402
1403 pdev = data.pdev;
1404
1405 /*
1406 * Continue upstream from the point of minimum IOMMU granularity
1407 * due to aliases to the point where devices are protected from
1408 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1409 * group, use it.
1410 */
1411 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1412 if (!bus->self)
1413 continue;
1414
1415 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1416 break;
1417
1418 pdev = bus->self;
1419
1420 group = iommu_group_get(&pdev->dev);
1421 if (group)
1422 return group;
1423 }
1424
1425 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001426 * Look for existing groups on device aliases. If we alias another
1427 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001428 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001429 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1430 if (group)
1431 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001432
1433 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001434 * Look for existing groups on non-isolated functions on the same
1435 * slot and aliases of those funcions, if any. No need to clear
1436 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001437 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001438 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1439 if (group)
1440 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001441
1442 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001443 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001444}
Will Deacona7ba5c32019-12-19 12:03:37 +00001445EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001446
Nipun Guptaeab03e22018-09-10 19:19:18 +05301447/* Get the IOMMU group for device on fsl-mc bus */
1448struct iommu_group *fsl_mc_device_group(struct device *dev)
1449{
1450 struct device *cont_dev = fsl_mc_cont_dev(dev);
1451 struct iommu_group *group;
1452
1453 group = iommu_group_get(cont_dev);
1454 if (!group)
1455 group = iommu_group_alloc();
1456 return group;
1457}
Will Deacona7ba5c32019-12-19 12:03:37 +00001458EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301459
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001460static int iommu_get_def_domain_type(struct device *dev)
1461{
1462 const struct iommu_ops *ops = dev->bus->iommu_ops;
1463 unsigned int type = 0;
1464
1465 if (ops->def_domain_type)
1466 type = ops->def_domain_type(dev);
1467
1468 return (type == 0) ? iommu_def_domain_type : type;
1469}
1470
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001471static int iommu_group_alloc_default_domain(struct bus_type *bus,
1472 struct iommu_group *group,
1473 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001474{
1475 struct iommu_domain *dom;
1476
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001477 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001478 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001479 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1480 if (dom)
1481 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1482 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001483 }
1484
1485 if (!dom)
1486 return -ENOMEM;
1487
1488 group->default_domain = dom;
1489 if (!group->domain)
1490 group->domain = dom;
1491
1492 if (!iommu_dma_strict) {
1493 int attr = 1;
1494 iommu_domain_set_attr(dom,
1495 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1496 &attr);
1497 }
1498
1499 return 0;
1500}
1501
Joerg Roedel79659190e2020-05-25 15:01:22 +02001502static int iommu_alloc_default_domain(struct iommu_group *group,
1503 struct device *dev)
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001504{
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001505 unsigned int type;
1506
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001507 if (group->default_domain)
1508 return 0;
1509
1510 type = iommu_get_def_domain_type(dev);
1511
1512 return iommu_group_alloc_default_domain(dev->bus, group, type);
1513}
1514
Alex Williamson104a1c12014-07-03 09:51:18 -06001515/**
1516 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1517 * @dev: target device
1518 *
1519 * This function is intended to be called by IOMMU drivers and extended to
1520 * support common, bus-defined algorithms when determining or creating the
1521 * IOMMU group for a device. On success, the caller will hold a reference
1522 * to the returned IOMMU group, which will already include the provided
1523 * device. The reference should be released with iommu_group_put().
1524 */
Joerg Roedel1b032ec2020-04-29 15:37:12 +02001525static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001526{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001527 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001528 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001529 int ret;
1530
1531 group = iommu_group_get(dev);
1532 if (group)
1533 return group;
1534
Robin Murphy05f803002017-07-21 13:12:38 +01001535 if (!ops)
1536 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001537
Robin Murphy05f803002017-07-21 13:12:38 +01001538 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001539 if (WARN_ON_ONCE(group == NULL))
1540 return ERR_PTR(-EINVAL);
1541
Alex Williamson104a1c12014-07-03 09:51:18 -06001542 if (IS_ERR(group))
1543 return group;
1544
1545 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001546 if (ret)
1547 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001548
1549 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001550
1551out_put_group:
1552 iommu_group_put(group);
1553
1554 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001555}
1556
Joerg Roedel6827ca82015-05-28 18:41:35 +02001557struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1558{
1559 return group->default_domain;
1560}
1561
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001562static int probe_iommu_group(struct device *dev, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001563{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001564 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001565 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001566 int ret;
Joerg Roedel38667f12015-06-29 10:16:08 +02001567
Joerg Roedel5012c392020-04-29 15:36:51 +02001568 /* Device is probed already if in a group */
1569 group = iommu_group_get(dev);
1570 if (group) {
1571 iommu_group_put(group);
1572 return 0;
1573 }
1574
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001575 ret = __iommu_probe_device(dev, group_list);
Joerg Roedel38667f12015-06-29 10:16:08 +02001576 if (ret == -ENODEV)
1577 ret = 0;
1578
1579 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001580}
1581
Joerg Roedel8da30142015-05-28 18:41:27 +02001582static int remove_iommu_group(struct device *dev, void *data)
1583{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001584 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001585
1586 return 0;
1587}
1588
Alex Williamsond72e31c2012-05-30 14:18:53 -06001589static int iommu_bus_notifier(struct notifier_block *nb,
1590 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001591{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001592 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001593 struct device *dev = data;
1594 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001595
Alex Williamsond72e31c2012-05-30 14:18:53 -06001596 /*
1597 * ADD/DEL call into iommu driver ops if provided, which may
1598 * result in ADD/DEL notifiers to group->notifier
1599 */
1600 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001601 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001602
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001603 ret = iommu_probe_device(dev);
1604 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001605 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001606 iommu_release_device(dev);
1607 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001608 }
Alex Williamson14604322011-10-21 15:56:05 -04001609
Alex Williamsond72e31c2012-05-30 14:18:53 -06001610 /*
1611 * Remaining BUS_NOTIFYs get filtered and republished to the
1612 * group, if anyone is listening
1613 */
1614 group = iommu_group_get(dev);
1615 if (!group)
1616 return 0;
1617
1618 switch (action) {
1619 case BUS_NOTIFY_BIND_DRIVER:
1620 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1621 break;
1622 case BUS_NOTIFY_BOUND_DRIVER:
1623 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1624 break;
1625 case BUS_NOTIFY_UNBIND_DRIVER:
1626 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1627 break;
1628 case BUS_NOTIFY_UNBOUND_DRIVER:
1629 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1630 break;
1631 }
1632
1633 if (group_action)
1634 blocking_notifier_call_chain(&group->notifier,
1635 group_action, dev);
1636
1637 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001638 return 0;
1639}
1640
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001641struct __group_domain_type {
1642 struct device *dev;
1643 unsigned int type;
1644};
1645
1646static int probe_get_default_domain_type(struct device *dev, void *data)
1647{
1648 const struct iommu_ops *ops = dev->bus->iommu_ops;
1649 struct __group_domain_type *gtype = data;
1650 unsigned int type = 0;
1651
1652 if (ops->def_domain_type)
1653 type = ops->def_domain_type(dev);
1654
1655 if (type) {
1656 if (gtype->type && gtype->type != type) {
1657 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1658 iommu_domain_type_str(type),
1659 dev_name(gtype->dev),
1660 iommu_domain_type_str(gtype->type));
1661 gtype->type = 0;
1662 }
1663
1664 if (!gtype->dev) {
1665 gtype->dev = dev;
1666 gtype->type = type;
1667 }
1668 }
1669
1670 return 0;
1671}
1672
1673static void probe_alloc_default_domain(struct bus_type *bus,
1674 struct iommu_group *group)
1675{
1676 struct __group_domain_type gtype;
1677
1678 memset(&gtype, 0, sizeof(gtype));
1679
1680 /* Ask for default domain requirements of all devices in the group */
1681 __iommu_group_for_each_dev(group, &gtype,
1682 probe_get_default_domain_type);
1683
1684 if (!gtype.type)
1685 gtype.type = iommu_def_domain_type;
1686
1687 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001688
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001689}
1690
1691static int iommu_group_do_dma_attach(struct device *dev, void *data)
1692{
1693 struct iommu_domain *domain = data;
Joerg Roedel431275a2020-06-04 11:19:44 +02001694 int ret = 0;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001695
Joerg Roedel431275a2020-06-04 11:19:44 +02001696 if (!iommu_is_attach_deferred(domain, dev))
1697 ret = __iommu_attach_device(domain, dev);
1698
1699 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001700}
1701
1702static int __iommu_group_dma_attach(struct iommu_group *group)
1703{
1704 return __iommu_group_for_each_dev(group, group->default_domain,
1705 iommu_group_do_dma_attach);
1706}
1707
Joerg Roedel70b81702020-05-19 15:28:24 +02001708static int iommu_group_do_probe_finalize(struct device *dev, void *data)
1709{
1710 struct iommu_domain *domain = data;
1711
1712 if (domain->ops->probe_finalize)
1713 domain->ops->probe_finalize(dev);
1714
1715 return 0;
1716}
1717
1718static void __iommu_group_dma_finalize(struct iommu_group *group)
1719{
1720 __iommu_group_for_each_dev(group, group->default_domain,
1721 iommu_group_do_probe_finalize);
1722}
Joerg Roedelcc69fc42020-06-02 10:32:04 +02001723
Joerg Roedelce574c22020-04-29 15:36:50 +02001724static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1725{
1726 struct iommu_group *group = data;
1727
1728 iommu_create_device_direct_mappings(group, dev);
1729
1730 return 0;
1731}
1732
1733static int iommu_group_create_direct_mappings(struct iommu_group *group)
1734{
1735 return __iommu_group_for_each_dev(group, group,
1736 iommu_do_create_direct_mappings);
1737}
1738
Joerg Roedel5012c392020-04-29 15:36:51 +02001739int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001740{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001741 struct iommu_group *group, *next;
1742 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001743 int ret;
1744
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001745 /*
1746 * This code-path does not allocate the default domain when
1747 * creating the iommu group, so do it after the groups are
1748 * created.
1749 */
1750 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1751 if (ret)
1752 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001753
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001754 list_for_each_entry_safe(group, next, &group_list, entry) {
1755 /* Remove item from the list */
1756 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001757
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001758 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001759
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001760 /* Try to allocate default domain */
1761 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001762
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001763 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001764 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001765 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001766 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001767
1768 iommu_group_create_direct_mappings(group);
1769
1770 ret = __iommu_group_dma_attach(group);
1771
1772 mutex_unlock(&group->mutex);
1773
1774 if (ret)
1775 break;
Joerg Roedel70b81702020-05-19 15:28:24 +02001776
1777 __iommu_group_dma_finalize(group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001778 }
1779
1780 return ret;
1781}
1782
Mark Salterfb3e3062014-09-21 13:58:24 -04001783static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001784{
Mark Salterfb3e3062014-09-21 13:58:24 -04001785 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001786 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001787
Mark Salterfb3e3062014-09-21 13:58:24 -04001788 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1789 if (!nb)
1790 return -ENOMEM;
1791
1792 nb->notifier_call = iommu_bus_notifier;
1793
1794 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001795 if (err)
1796 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001797
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001798 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001799 if (err)
1800 goto out_err;
1801
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001802
1803 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001804
1805out_err:
1806 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001807 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001808 bus_unregister_notifier(bus, nb);
1809
1810out_free:
1811 kfree(nb);
1812
1813 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001814}
1815
Joerg Roedelff217762011-08-26 16:48:26 +02001816/**
1817 * bus_set_iommu - set iommu-callbacks for the bus
1818 * @bus: bus.
1819 * @ops: the callbacks provided by the iommu-driver
1820 *
1821 * This function is called by an iommu driver to set the iommu methods
1822 * used for a particular bus. Drivers for devices on that bus can use
1823 * the iommu-api after these ops are registered.
1824 * This special function is needed because IOMMUs are usually devices on
1825 * the bus itself, so the iommu drivers are not initialized when the bus
1826 * is set up. With this function the iommu-driver can set the iommu-ops
1827 * afterwards.
1828 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001829int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001830{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001831 int err;
1832
Will Deacon4312cf72019-12-19 12:03:43 +00001833 if (ops == NULL) {
1834 bus->iommu_ops = NULL;
1835 return 0;
1836 }
1837
Joerg Roedelff217762011-08-26 16:48:26 +02001838 if (bus->iommu_ops != NULL)
1839 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001840
Joerg Roedelff217762011-08-26 16:48:26 +02001841 bus->iommu_ops = ops;
1842
1843 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001844 err = iommu_bus_init(bus, ops);
1845 if (err)
1846 bus->iommu_ops = NULL;
1847
1848 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001849}
Joerg Roedelff217762011-08-26 16:48:26 +02001850EXPORT_SYMBOL_GPL(bus_set_iommu);
1851
Joerg Roedela1b60c12011-09-06 18:46:34 +02001852bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001853{
Joerg Roedel94441c32011-09-06 18:58:54 +02001854 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001855}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001856EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001857
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001858bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1859{
1860 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1861 return false;
1862
1863 return bus->iommu_ops->capable(cap);
1864}
1865EXPORT_SYMBOL_GPL(iommu_capable);
1866
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001867/**
1868 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1869 * @domain: iommu domain
1870 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001871 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001872 *
1873 * This function should be used by IOMMU users which want to be notified
1874 * whenever an IOMMU fault happens.
1875 *
1876 * The fault handler itself should return 0 on success, and an appropriate
1877 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001878 */
1879void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001880 iommu_fault_handler_t handler,
1881 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001882{
1883 BUG_ON(!domain);
1884
1885 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001886 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001887}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001888EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001889
Joerg Roedel53723dc2015-05-28 18:41:29 +02001890static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1891 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001892{
1893 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001894
Joerg Roedel94441c32011-09-06 18:58:54 +02001895 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001896 return NULL;
1897
Joerg Roedel53723dc2015-05-28 18:41:29 +02001898 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001899 if (!domain)
1900 return NULL;
1901
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001902 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001903 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001904 /* Assume all sizes by default; the driver may override this later */
1905 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001906
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001907 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001908}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001909
Joerg Roedel53723dc2015-05-28 18:41:29 +02001910struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1911{
1912 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001913}
1914EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1915
1916void iommu_domain_free(struct iommu_domain *domain)
1917{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001918 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001919}
1920EXPORT_SYMBOL_GPL(iommu_domain_free);
1921
Joerg Roedel426a2732015-05-28 18:41:30 +02001922static int __iommu_attach_device(struct iommu_domain *domain,
1923 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001924{
Shuah Khanb54db772013-08-15 11:59:26 -06001925 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001926
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001927 if (unlikely(domain->ops->attach_dev == NULL))
1928 return -ENODEV;
1929
Shuah Khanb54db772013-08-15 11:59:26 -06001930 ret = domain->ops->attach_dev(domain, dev);
1931 if (!ret)
1932 trace_attach_device_to_domain(dev);
1933 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001934}
Joerg Roedel426a2732015-05-28 18:41:30 +02001935
1936int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1937{
1938 struct iommu_group *group;
1939 int ret;
1940
1941 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001942 if (!group)
1943 return -ENODEV;
1944
Joerg Roedel426a2732015-05-28 18:41:30 +02001945 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001946 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001947 * change while we are attaching
1948 */
1949 mutex_lock(&group->mutex);
1950 ret = -EINVAL;
1951 if (iommu_group_device_count(group) != 1)
1952 goto out_unlock;
1953
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001954 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001955
1956out_unlock:
1957 mutex_unlock(&group->mutex);
1958 iommu_group_put(group);
1959
1960 return ret;
1961}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001962EXPORT_SYMBOL_GPL(iommu_attach_device);
1963
Jacob Pand9057382020-09-25 09:32:46 -07001964/*
1965 * Check flags and other user provided data for valid combinations. We also
1966 * make sure no reserved fields or unused flags are set. This is to ensure
1967 * not breaking userspace in the future when these fields or flags are used.
1968 */
1969static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info)
Yi L Liu4c7c1712019-10-02 12:42:40 -07001970{
Jacob Pand9057382020-09-25 09:32:46 -07001971 u32 mask;
1972 int i;
1973
1974 if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1)
1975 return -EINVAL;
1976
1977 mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1;
1978 if (info->cache & ~mask)
1979 return -EINVAL;
1980
1981 if (info->granularity >= IOMMU_INV_GRANU_NR)
1982 return -EINVAL;
1983
1984 switch (info->granularity) {
1985 case IOMMU_INV_GRANU_ADDR:
1986 if (info->cache & IOMMU_CACHE_INV_TYPE_PASID)
1987 return -EINVAL;
1988
1989 mask = IOMMU_INV_ADDR_FLAGS_PASID |
1990 IOMMU_INV_ADDR_FLAGS_ARCHID |
1991 IOMMU_INV_ADDR_FLAGS_LEAF;
1992
1993 if (info->granu.addr_info.flags & ~mask)
1994 return -EINVAL;
1995 break;
1996 case IOMMU_INV_GRANU_PASID:
1997 mask = IOMMU_INV_PASID_FLAGS_PASID |
1998 IOMMU_INV_PASID_FLAGS_ARCHID;
1999 if (info->granu.pasid_info.flags & ~mask)
2000 return -EINVAL;
2001
2002 break;
2003 case IOMMU_INV_GRANU_DOMAIN:
2004 if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB)
2005 return -EINVAL;
2006 break;
2007 default:
2008 return -EINVAL;
2009 }
2010
2011 /* Check reserved padding fields */
2012 for (i = 0; i < sizeof(info->padding); i++) {
2013 if (info->padding[i])
2014 return -EINVAL;
2015 }
2016
2017 return 0;
2018}
2019
2020int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev,
2021 void __user *uinfo)
2022{
2023 struct iommu_cache_invalidate_info inv_info = { 0 };
2024 u32 minsz;
2025 int ret;
2026
Yi L Liu4c7c1712019-10-02 12:42:40 -07002027 if (unlikely(!domain->ops->cache_invalidate))
2028 return -ENODEV;
2029
Jacob Pand9057382020-09-25 09:32:46 -07002030 /*
2031 * No new spaces can be added before the variable sized union, the
2032 * minimum size is the offset to the union.
2033 */
2034 minsz = offsetof(struct iommu_cache_invalidate_info, granu);
2035
2036 /* Copy minsz from user to get flags and argsz */
2037 if (copy_from_user(&inv_info, uinfo, minsz))
2038 return -EFAULT;
2039
2040 /* Fields before the variable size union are mandatory */
2041 if (inv_info.argsz < minsz)
2042 return -EINVAL;
2043
2044 /* PASID and address granu require additional info beyond minsz */
2045 if (inv_info.granularity == IOMMU_INV_GRANU_PASID &&
2046 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info))
2047 return -EINVAL;
2048
2049 if (inv_info.granularity == IOMMU_INV_GRANU_ADDR &&
2050 inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info))
2051 return -EINVAL;
2052
2053 /*
2054 * User might be using a newer UAPI header which has a larger data
2055 * size, we shall support the existing flags within the current
2056 * size. Copy the remaining user data _after_ minsz but not more
2057 * than the current kernel supported size.
2058 */
2059 if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz,
2060 min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz))
2061 return -EFAULT;
2062
2063 /* Now the argsz is validated, check the content */
2064 ret = iommu_check_cache_invl_data(&inv_info);
2065 if (ret)
2066 return ret;
2067
2068 return domain->ops->cache_invalidate(domain, dev, &inv_info);
Yi L Liu4c7c1712019-10-02 12:42:40 -07002069}
Jacob Pan23cc3492020-09-25 09:32:45 -07002070EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate);
Yi L Liu4c7c1712019-10-02 12:42:40 -07002071
Jacob Pand9057382020-09-25 09:32:46 -07002072static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data)
Jacob Pan808be0a2019-10-02 12:42:43 -07002073{
Jacob Pand9057382020-09-25 09:32:46 -07002074 u32 mask;
2075 int i;
2076
2077 if (data->version != IOMMU_GPASID_BIND_VERSION_1)
2078 return -EINVAL;
2079
2080 /* Check the range of supported formats */
2081 if (data->format >= IOMMU_PASID_FORMAT_LAST)
2082 return -EINVAL;
2083
2084 /* Check all flags */
2085 mask = IOMMU_SVA_GPASID_VAL;
2086 if (data->flags & ~mask)
2087 return -EINVAL;
2088
2089 /* Check reserved padding fields */
2090 for (i = 0; i < sizeof(data->padding); i++) {
2091 if (data->padding[i])
2092 return -EINVAL;
2093 }
2094
2095 return 0;
2096}
2097
2098static int iommu_sva_prepare_bind_data(void __user *udata,
2099 struct iommu_gpasid_bind_data *data)
2100{
2101 u32 minsz;
2102
2103 /*
2104 * No new spaces can be added before the variable sized union, the
2105 * minimum size is the offset to the union.
2106 */
2107 minsz = offsetof(struct iommu_gpasid_bind_data, vendor);
2108
2109 /* Copy minsz from user to get flags and argsz */
2110 if (copy_from_user(data, udata, minsz))
2111 return -EFAULT;
2112
2113 /* Fields before the variable size union are mandatory */
2114 if (data->argsz < minsz)
2115 return -EINVAL;
2116 /*
2117 * User might be using a newer UAPI header, we shall let IOMMU vendor
2118 * driver decide on what size it needs. Since the guest PASID bind data
2119 * can be vendor specific, larger argsz could be the result of extension
2120 * for one vendor but it should not affect another vendor.
2121 * Copy the remaining user data _after_ minsz
2122 */
2123 if (copy_from_user((void *)data + minsz, udata + minsz,
2124 min_t(u32, data->argsz, sizeof(*data)) - minsz))
2125 return -EFAULT;
2126
2127 return iommu_check_bind_data(data);
2128}
2129
2130int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev,
2131 void __user *udata)
2132{
2133 struct iommu_gpasid_bind_data data = { 0 };
2134 int ret;
2135
Jacob Pan808be0a2019-10-02 12:42:43 -07002136 if (unlikely(!domain->ops->sva_bind_gpasid))
2137 return -ENODEV;
2138
Jacob Pand9057382020-09-25 09:32:46 -07002139 ret = iommu_sva_prepare_bind_data(udata, &data);
2140 if (ret)
2141 return ret;
2142
2143 return domain->ops->sva_bind_gpasid(domain, dev, &data);
Jacob Pan808be0a2019-10-02 12:42:43 -07002144}
Jacob Pan23cc3492020-09-25 09:32:45 -07002145EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid);
Jacob Pan808be0a2019-10-02 12:42:43 -07002146
Jacob Pand9057382020-09-25 09:32:46 -07002147int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2148 ioasid_t pasid)
Jacob Pan808be0a2019-10-02 12:42:43 -07002149{
2150 if (unlikely(!domain->ops->sva_unbind_gpasid))
2151 return -ENODEV;
2152
2153 return domain->ops->sva_unbind_gpasid(dev, pasid);
2154}
Jacob Pand9057382020-09-25 09:32:46 -07002155EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
2156
2157int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
2158 void __user *udata)
2159{
2160 struct iommu_gpasid_bind_data data = { 0 };
2161 int ret;
2162
2163 if (unlikely(!domain->ops->sva_bind_gpasid))
2164 return -ENODEV;
2165
2166 ret = iommu_sva_prepare_bind_data(udata, &data);
2167 if (ret)
2168 return ret;
2169
2170 return iommu_sva_unbind_gpasid(domain, dev, data.hpasid);
2171}
Jacob Pan23cc3492020-09-25 09:32:45 -07002172EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid);
Jacob Pan808be0a2019-10-02 12:42:43 -07002173
Joerg Roedel426a2732015-05-28 18:41:30 +02002174static void __iommu_detach_device(struct iommu_domain *domain,
2175 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002176{
Joerg Roedelbd421262020-05-19 15:03:40 +02002177 if (iommu_is_attach_deferred(domain, dev))
Baoquan Hee01d1912017-08-09 16:33:40 +08002178 return;
2179
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002180 if (unlikely(domain->ops->detach_dev == NULL))
2181 return;
2182
2183 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06002184 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002185}
Joerg Roedel426a2732015-05-28 18:41:30 +02002186
2187void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2188{
2189 struct iommu_group *group;
2190
2191 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07002192 if (!group)
2193 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02002194
2195 mutex_lock(&group->mutex);
2196 if (iommu_group_device_count(group) != 1) {
2197 WARN_ON(1);
2198 goto out_unlock;
2199 }
2200
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002201 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002202
2203out_unlock:
2204 mutex_unlock(&group->mutex);
2205 iommu_group_put(group);
2206}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002207EXPORT_SYMBOL_GPL(iommu_detach_device);
2208
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002209struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2210{
2211 struct iommu_domain *domain;
2212 struct iommu_group *group;
2213
2214 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002215 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002216 return NULL;
2217
2218 domain = group->domain;
2219
2220 iommu_group_put(group);
2221
2222 return domain;
2223}
2224EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2225
Alex Williamsond72e31c2012-05-30 14:18:53 -06002226/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002227 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2228 * guarantees that the group and its default domain are valid and correct.
2229 */
2230struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2231{
2232 return dev->iommu_group->default_domain;
2233}
2234
2235/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002236 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002237 * the IOMMU API works on domains and devices. Bridge that gap by
2238 * iterating over the devices in a group. Ideally we'd have a single
2239 * device which represents the requestor ID of the group, but we also
2240 * allow IOMMU drivers to create policy defined minimum sets, where
2241 * the physical hardware may be able to distiguish members, but we
2242 * wish to group them at a higher level (ex. untrusted multi-function
2243 * PCI devices). Thus we attach each device.
2244 */
2245static int iommu_group_do_attach_device(struct device *dev, void *data)
2246{
2247 struct iommu_domain *domain = data;
2248
Joerg Roedel426a2732015-05-28 18:41:30 +02002249 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002250}
2251
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002252static int __iommu_attach_group(struct iommu_domain *domain,
2253 struct iommu_group *group)
2254{
2255 int ret;
2256
2257 if (group->default_domain && group->domain != group->default_domain)
2258 return -EBUSY;
2259
2260 ret = __iommu_group_for_each_dev(group, domain,
2261 iommu_group_do_attach_device);
2262 if (ret == 0)
2263 group->domain = domain;
2264
2265 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002266}
2267
2268int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2269{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002270 int ret;
2271
2272 mutex_lock(&group->mutex);
2273 ret = __iommu_attach_group(domain, group);
2274 mutex_unlock(&group->mutex);
2275
2276 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002277}
2278EXPORT_SYMBOL_GPL(iommu_attach_group);
2279
2280static int iommu_group_do_detach_device(struct device *dev, void *data)
2281{
2282 struct iommu_domain *domain = data;
2283
Joerg Roedel426a2732015-05-28 18:41:30 +02002284 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002285
2286 return 0;
2287}
2288
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002289static void __iommu_detach_group(struct iommu_domain *domain,
2290 struct iommu_group *group)
2291{
2292 int ret;
2293
2294 if (!group->default_domain) {
2295 __iommu_group_for_each_dev(group, domain,
2296 iommu_group_do_detach_device);
2297 group->domain = NULL;
2298 return;
2299 }
2300
2301 if (group->domain == group->default_domain)
2302 return;
2303
2304 /* Detach by re-attaching to the default domain */
2305 ret = __iommu_group_for_each_dev(group, group->default_domain,
2306 iommu_group_do_attach_device);
2307 if (ret != 0)
2308 WARN_ON(1);
2309 else
2310 group->domain = group->default_domain;
2311}
2312
Alex Williamsond72e31c2012-05-30 14:18:53 -06002313void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2314{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002315 mutex_lock(&group->mutex);
2316 __iommu_detach_group(domain, group);
2317 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002318}
2319EXPORT_SYMBOL_GPL(iommu_detach_group);
2320
Varun Sethibb5547a2013-03-29 01:23:58 +05302321phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002322{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002323 if (unlikely(domain->ops->iova_to_phys == NULL))
2324 return 0;
2325
2326 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002327}
2328EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002329
Alex Williamsonbd139692013-06-17 19:57:34 -06002330static size_t iommu_pgsize(struct iommu_domain *domain,
2331 unsigned long addr_merge, size_t size)
2332{
2333 unsigned int pgsize_idx;
2334 size_t pgsize;
2335
2336 /* Max page size that still fits into 'size' */
2337 pgsize_idx = __fls(size);
2338
2339 /* need to consider alignment requirements ? */
2340 if (likely(addr_merge)) {
2341 /* Max page size allowed by address */
2342 unsigned int align_pgsize_idx = __ffs(addr_merge);
2343 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2344 }
2345
2346 /* build a mask of acceptable page sizes */
2347 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2348
2349 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002350 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06002351
2352 /* make sure we're still sane */
2353 BUG_ON(!pgsize);
2354
2355 /* pick the biggest page */
2356 pgsize_idx = __fls(pgsize);
2357 pgsize = 1UL << pgsize_idx;
2358
2359 return pgsize;
2360}
2361
Wei Yongjun1b0b2a82020-07-13 22:25:42 +08002362static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2363 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002364{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002365 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002366 unsigned long orig_iova = iova;
2367 unsigned int min_pagesz;
2368 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002369 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002370 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002371
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002372 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002373 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002374 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002375
Joerg Roedela10315e2015-03-26 13:43:06 +01002376 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2377 return -EINVAL;
2378
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002379 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002380 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002381
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002382 /*
2383 * both the virtual address and the physical one, as well as
2384 * the size of the mapping, must be aligned (at least) to the
2385 * size of the smallest page supported by the hardware
2386 */
2387 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002388 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002389 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002390 return -EINVAL;
2391 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002392
Fabio Estevamabedb042013-08-22 10:25:42 -03002393 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002394
2395 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002396 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002397
Fabio Estevamabedb042013-08-22 10:25:42 -03002398 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002399 iova, &paddr, pgsize);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002400 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002401
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002402 if (ret)
2403 break;
2404
2405 iova += pgsize;
2406 paddr += pgsize;
2407 size -= pgsize;
2408 }
2409
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002410 if (ops->iotlb_sync_map)
2411 ops->iotlb_sync_map(domain);
2412
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002413 /* unroll mapping in case something went wrong */
2414 if (ret)
2415 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002416 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002417 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002418
2419 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002420}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002421
2422int iommu_map(struct iommu_domain *domain, unsigned long iova,
2423 phys_addr_t paddr, size_t size, int prot)
2424{
2425 might_sleep();
2426 return __iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2427}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002428EXPORT_SYMBOL_GPL(iommu_map);
2429
Tom Murphy781ca2d2019-09-08 09:56:38 -07002430int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2431 phys_addr_t paddr, size_t size, int prot)
2432{
2433 return __iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2434}
2435EXPORT_SYMBOL_GPL(iommu_map_atomic);
2436
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002437static size_t __iommu_unmap(struct iommu_domain *domain,
2438 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002439 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002440{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002441 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002442 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002443 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002444 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002445
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002446 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002447 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002448 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002449
Joerg Roedela10315e2015-03-26 13:43:06 +01002450 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002451 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002452
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002453 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002454 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002455
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002456 /*
2457 * The virtual address, as well as the size of the mapping, must be
2458 * aligned (at least) to the size of the smallest page supported
2459 * by the hardware
2460 */
2461 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002462 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2463 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002464 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002465 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002466
Joe Perches6197ca82013-06-23 12:29:04 -07002467 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002468
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002469 /*
2470 * Keep iterating until we either unmap 'size' bytes (or more)
2471 * or we hit an area that isn't mapped.
2472 */
2473 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002474 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002475
Will Deacon56f8af52019-07-02 16:44:06 +01002476 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002477 if (!unmapped_page)
2478 break;
2479
Joe Perches6197ca82013-06-23 12:29:04 -07002480 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2481 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002482
2483 iova += unmapped_page;
2484 unmapped += unmapped_page;
2485 }
2486
Shuah Khandb8614d2015-01-16 20:53:17 -07002487 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002488 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002489}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002490
2491size_t iommu_unmap(struct iommu_domain *domain,
2492 unsigned long iova, size_t size)
2493{
Will Deacona7d20dc2019-07-02 16:43:48 +01002494 struct iommu_iotlb_gather iotlb_gather;
2495 size_t ret;
2496
2497 iommu_iotlb_gather_init(&iotlb_gather);
2498 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
Tom Murphyaae4c8e2020-08-17 22:00:49 +01002499 iommu_iotlb_sync(domain, &iotlb_gather);
Will Deacona7d20dc2019-07-02 16:43:48 +01002500
2501 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002502}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002503EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002504
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002505size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002506 unsigned long iova, size_t size,
2507 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002508{
Will Deacona7d20dc2019-07-02 16:43:48 +01002509 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002510}
2511EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2512
Baolin Wang99302642020-06-12 11:39:54 +08002513static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2514 struct scatterlist *sg, unsigned int nents, int prot,
2515 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002516{
Robin Murphy5d95f402018-10-11 16:56:42 +01002517 size_t len = 0, mapped = 0;
2518 phys_addr_t start;
2519 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002520 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002521
Robin Murphy5d95f402018-10-11 16:56:42 +01002522 while (i <= nents) {
2523 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002524
Robin Murphy5d95f402018-10-11 16:56:42 +01002525 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002526 ret = __iommu_map(domain, iova + mapped, start,
2527 len, prot, gfp);
2528
Robin Murphy5d95f402018-10-11 16:56:42 +01002529 if (ret)
2530 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002531
Robin Murphy5d95f402018-10-11 16:56:42 +01002532 mapped += len;
2533 len = 0;
2534 }
Robin Murphy18f23402014-11-25 17:50:55 +00002535
Robin Murphy5d95f402018-10-11 16:56:42 +01002536 if (len) {
2537 len += sg->length;
2538 } else {
2539 len = sg->length;
2540 start = s_phys;
2541 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002542
Robin Murphy5d95f402018-10-11 16:56:42 +01002543 if (++i < nents)
2544 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002545 }
2546
2547 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002548
2549out_err:
2550 /* undo mappings already done */
2551 iommu_unmap(domain, iova, mapped);
2552
2553 return 0;
2554
Olav Haugan315786e2014-10-25 09:55:16 -07002555}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002556
2557size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2558 struct scatterlist *sg, unsigned int nents, int prot)
2559{
2560 might_sleep();
2561 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2562}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002563EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002564
Tom Murphy781ca2d2019-09-08 09:56:38 -07002565size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2566 struct scatterlist *sg, unsigned int nents, int prot)
2567{
2568 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2569}
2570EXPORT_SYMBOL_GPL(iommu_map_sg_atomic);
2571
Joerg Roedeld7787d52013-01-29 14:26:20 +01002572int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05302573 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01002574{
2575 if (unlikely(domain->ops->domain_window_enable == NULL))
2576 return -ENODEV;
2577
Varun Sethi80f97f02013-03-29 01:24:00 +05302578 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2579 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002580}
2581EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2582
2583void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2584{
2585 if (unlikely(domain->ops->domain_window_disable == NULL))
2586 return;
2587
2588 return domain->ops->domain_window_disable(domain, wnd_nr);
2589}
2590EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2591
Joerg Roedel207c6e32017-04-26 15:39:28 +02002592/**
2593 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2594 * @domain: the iommu domain where the fault has happened
2595 * @dev: the device where the fault has happened
2596 * @iova: the faulting address
2597 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2598 *
2599 * This function should be called by the low-level IOMMU implementations
2600 * whenever IOMMU faults happen, to allow high-level users, that are
2601 * interested in such events, to know about them.
2602 *
2603 * This event may be useful for several possible use cases:
2604 * - mere logging of the event
2605 * - dynamic TLB/PTE loading
2606 * - if restarting of the faulting device is required
2607 *
2608 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2609 * PTE/TLB loading will one day be supported, implementations will be able
2610 * to tell whether it succeeded or not according to this return value).
2611 *
2612 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2613 * (though fault handlers can also return -ENOSYS, in case they want to
2614 * elicit the default behavior of the IOMMU drivers).
2615 */
2616int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2617 unsigned long iova, int flags)
2618{
2619 int ret = -ENOSYS;
2620
2621 /*
2622 * if upper layers showed interest and installed a fault handler,
2623 * invoke it.
2624 */
2625 if (domain->handler)
2626 ret = domain->handler(domain, dev, iova, flags,
2627 domain->handler_token);
2628
2629 trace_io_page_fault(dev, iova, flags);
2630 return ret;
2631}
2632EXPORT_SYMBOL_GPL(report_iommu_fault);
2633
Alex Williamsond72e31c2012-05-30 14:18:53 -06002634static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002635{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002636 iommu_group_kset = kset_create_and_add("iommu_groups",
2637 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002638 BUG_ON(!iommu_group_kset);
2639
Gary R Hookbad614b2018-06-12 16:41:21 -05002640 iommu_debugfs_setup();
2641
Alex Williamsond72e31c2012-05-30 14:18:53 -06002642 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002643}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002644core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002645
2646int iommu_domain_get_attr(struct iommu_domain *domain,
2647 enum iommu_attr attr, void *data)
2648{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002649 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002650 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002651 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002652
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002653 switch (attr) {
2654 case DOMAIN_ATTR_GEOMETRY:
2655 geometry = data;
2656 *geometry = domain->geometry;
2657
2658 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002659 case DOMAIN_ATTR_PAGING:
2660 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002661 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01002662 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002663 default:
2664 if (!domain->ops->domain_get_attr)
2665 return -EINVAL;
2666
2667 ret = domain->ops->domain_get_attr(domain, attr, data);
2668 }
2669
2670 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002671}
2672EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2673
2674int iommu_domain_set_attr(struct iommu_domain *domain,
2675 enum iommu_attr attr, void *data)
2676{
Joerg Roedel69356712013-02-04 14:00:01 +01002677 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002678
Joerg Roedel69356712013-02-04 14:00:01 +01002679 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01002680 default:
2681 if (domain->ops->domain_set_attr == NULL)
2682 return -EINVAL;
2683
2684 ret = domain->ops->domain_set_attr(domain, attr, data);
2685 }
2686
2687 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002688}
2689EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02002690
Eric Augere5b52342017-01-19 20:57:47 +00002691void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002692{
2693 const struct iommu_ops *ops = dev->bus->iommu_ops;
2694
Eric Augere5b52342017-01-19 20:57:47 +00002695 if (ops && ops->get_resv_regions)
2696 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002697}
2698
Eric Augere5b52342017-01-19 20:57:47 +00002699void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002700{
2701 const struct iommu_ops *ops = dev->bus->iommu_ops;
2702
Eric Augere5b52342017-01-19 20:57:47 +00002703 if (ops && ops->put_resv_regions)
2704 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002705}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002706
Thierry Redingf9f69712019-12-18 14:42:01 +01002707/**
2708 * generic_iommu_put_resv_regions - Reserved region driver helper
2709 * @dev: device for which to free reserved regions
2710 * @list: reserved region list for device
2711 *
2712 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2713 * for simple reservations. Memory allocated for each reserved region will be
2714 * freed. If an IOMMU driver allocates additional resources per region, it is
2715 * going to have to implement a custom callback.
2716 */
2717void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2718{
2719 struct iommu_resv_region *entry, *next;
2720
2721 list_for_each_entry_safe(entry, next, list, list)
2722 kfree(entry);
2723}
2724EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2725
Eric Auger2b20cbb2017-01-19 20:57:49 +00002726struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002727 size_t length, int prot,
2728 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002729{
2730 struct iommu_resv_region *region;
2731
2732 region = kzalloc(sizeof(*region), GFP_KERNEL);
2733 if (!region)
2734 return NULL;
2735
2736 INIT_LIST_HEAD(&region->list);
2737 region->start = start;
2738 region->length = length;
2739 region->prot = prot;
2740 region->type = type;
2741 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002742}
Will Deacona7ba5c32019-12-19 12:03:37 +00002743EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002744
Joerg Roedel8a699612019-08-19 15:22:47 +02002745void iommu_set_default_passthrough(bool cmd_line)
2746{
2747 if (cmd_line)
2748 iommu_set_cmd_line_dma_api();
2749
2750 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2751}
2752
2753void iommu_set_default_translated(bool cmd_line)
2754{
2755 if (cmd_line)
2756 iommu_set_cmd_line_dma_api();
2757
2758 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2759}
2760
2761bool iommu_default_passthrough(void)
2762{
2763 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2764}
2765EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2766
Joerg Roedel534766d2017-01-31 16:58:42 +01002767const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002768{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002769 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002770 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002771
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002772 spin_lock(&iommu_device_lock);
2773 list_for_each_entry(iommu, &iommu_device_list, list)
2774 if (iommu->fwnode == fwnode) {
2775 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002776 break;
2777 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002778 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002779 return ops;
2780}
2781
Robin Murphy57f98d22016-09-13 10:54:14 +01002782int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2783 const struct iommu_ops *ops)
2784{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002785 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002786
2787 if (fwspec)
2788 return ops == fwspec->ops ? 0 : -EINVAL;
2789
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002790 if (!dev_iommu_get(dev))
2791 return -ENOMEM;
2792
Robin Murphy098accf2020-02-13 14:00:21 +00002793 /* Preallocate for the overwhelmingly common case of 1 ID */
2794 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002795 if (!fwspec)
2796 return -ENOMEM;
2797
2798 of_node_get(to_of_node(iommu_fwnode));
2799 fwspec->iommu_fwnode = iommu_fwnode;
2800 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002801 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002802 return 0;
2803}
2804EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2805
2806void iommu_fwspec_free(struct device *dev)
2807{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002808 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002809
2810 if (fwspec) {
2811 fwnode_handle_put(fwspec->iommu_fwnode);
2812 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002813 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002814 }
2815}
2816EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2817
2818int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2819{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002820 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002821 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002822
2823 if (!fwspec)
2824 return -EINVAL;
2825
Robin Murphy098accf2020-02-13 14:00:21 +00002826 new_num = fwspec->num_ids + num_ids;
2827 if (new_num > 1) {
2828 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2829 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002830 if (!fwspec)
2831 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002832
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002833 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002834 }
2835
2836 for (i = 0; i < num_ids; i++)
2837 fwspec->ids[fwspec->num_ids + i] = ids[i];
2838
Robin Murphy098accf2020-02-13 14:00:21 +00002839 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002843
2844/*
2845 * Per device IOMMU features.
2846 */
2847bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2848{
2849 const struct iommu_ops *ops = dev->bus->iommu_ops;
2850
2851 if (ops && ops->dev_has_feat)
2852 return ops->dev_has_feat(dev, feat);
2853
2854 return false;
2855}
2856EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2857
2858int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2859{
2860 const struct iommu_ops *ops = dev->bus->iommu_ops;
2861
2862 if (ops && ops->dev_enable_feat)
2863 return ops->dev_enable_feat(dev, feat);
2864
2865 return -ENODEV;
2866}
2867EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2868
2869/*
2870 * The device drivers should do the necessary cleanups before calling this.
2871 * For example, before disabling the aux-domain feature, the device driver
2872 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2873 */
2874int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2875{
2876 const struct iommu_ops *ops = dev->bus->iommu_ops;
2877
2878 if (ops && ops->dev_disable_feat)
2879 return ops->dev_disable_feat(dev, feat);
2880
2881 return -EBUSY;
2882}
2883EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2884
2885bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2886{
2887 const struct iommu_ops *ops = dev->bus->iommu_ops;
2888
2889 if (ops && ops->dev_feat_enabled)
2890 return ops->dev_feat_enabled(dev, feat);
2891
2892 return false;
2893}
2894EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2895
2896/*
2897 * Aux-domain specific attach/detach.
2898 *
2899 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2900 * true. Also, as long as domains are attached to a device through this
2901 * interface, any tries to call iommu_attach_device() should fail
2902 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2903 * This should make us safe against a device being attached to a guest as a
2904 * whole while there are still pasid users on it (aux and sva).
2905 */
2906int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2907{
2908 int ret = -ENODEV;
2909
2910 if (domain->ops->aux_attach_dev)
2911 ret = domain->ops->aux_attach_dev(domain, dev);
2912
2913 if (!ret)
2914 trace_attach_device_to_domain(dev);
2915
2916 return ret;
2917}
2918EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2919
2920void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2921{
2922 if (domain->ops->aux_detach_dev) {
2923 domain->ops->aux_detach_dev(domain, dev);
2924 trace_detach_device_from_domain(dev);
2925 }
2926}
2927EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2928
2929int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2930{
2931 int ret = -ENODEV;
2932
2933 if (domain->ops->aux_get_pasid)
2934 ret = domain->ops->aux_get_pasid(domain, dev);
2935
2936 return ret;
2937}
2938EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002939
2940/**
2941 * iommu_sva_bind_device() - Bind a process address space to a device
2942 * @dev: the device
2943 * @mm: the mm to bind, caller must hold a reference to it
2944 *
2945 * Create a bond between device and address space, allowing the device to access
2946 * the mm using the returned PASID. If a bond already exists between @device and
2947 * @mm, it is returned and an additional reference is taken. Caller must call
2948 * iommu_sva_unbind_device() to release each reference.
2949 *
2950 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2951 * initialize the required SVA features.
2952 *
2953 * On error, returns an ERR_PTR value.
2954 */
2955struct iommu_sva *
2956iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2957{
2958 struct iommu_group *group;
2959 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2960 const struct iommu_ops *ops = dev->bus->iommu_ops;
2961
2962 if (!ops || !ops->sva_bind)
2963 return ERR_PTR(-ENODEV);
2964
2965 group = iommu_group_get(dev);
2966 if (!group)
2967 return ERR_PTR(-ENODEV);
2968
2969 /* Ensure device count and domain don't change while we're binding */
2970 mutex_lock(&group->mutex);
2971
2972 /*
2973 * To keep things simple, SVA currently doesn't support IOMMU groups
2974 * with more than one device. Existing SVA-capable systems are not
2975 * affected by the problems that required IOMMU groups (lack of ACS
2976 * isolation, device ID aliasing and other hardware issues).
2977 */
2978 if (iommu_group_device_count(group) != 1)
2979 goto out_unlock;
2980
2981 handle = ops->sva_bind(dev, mm, drvdata);
2982
2983out_unlock:
2984 mutex_unlock(&group->mutex);
2985 iommu_group_put(group);
2986
2987 return handle;
2988}
2989EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2990
2991/**
2992 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2993 * @handle: the handle returned by iommu_sva_bind_device()
2994 *
2995 * Put reference to a bond between device and address space. The device should
2996 * not be issuing any more transaction for this PASID. All outstanding page
2997 * requests for this PASID must have been flushed to the IOMMU.
2998 *
2999 * Returns 0 on success, or an error value
3000 */
3001void iommu_sva_unbind_device(struct iommu_sva *handle)
3002{
3003 struct iommu_group *group;
3004 struct device *dev = handle->dev;
3005 const struct iommu_ops *ops = dev->bus->iommu_ops;
3006
3007 if (!ops || !ops->sva_unbind)
3008 return;
3009
3010 group = iommu_group_get(dev);
3011 if (!group)
3012 return;
3013
3014 mutex_lock(&group->mutex);
3015 ops->sva_unbind(handle);
3016 mutex_unlock(&group->mutex);
3017
3018 iommu_group_put(group);
3019}
3020EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
3021
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01003022int iommu_sva_get_pasid(struct iommu_sva *handle)
3023{
3024 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
3025
3026 if (!ops || !ops->sva_get_pasid)
3027 return IOMMU_PASID_INVALID;
3028
3029 return ops->sva_get_pasid(handle);
3030}
3031EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);