blob: a9e5618cde8027e342bbae88e24b766c78b4d377 [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 Roedel6e1aa202020-04-29 15:36:46 +020083static int iommu_alloc_default_domain(struct device *dev);
84static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
85 unsigned type);
86static int __iommu_attach_device(struct iommu_domain *domain,
87 struct device *dev);
88static int __iommu_attach_group(struct iommu_domain *domain,
89 struct iommu_group *group);
90static void __iommu_detach_group(struct iommu_domain *domain,
91 struct iommu_group *group);
Joerg Roedelce574c22020-04-29 15:36:50 +020092static int iommu_create_device_direct_mappings(struct iommu_group *group,
93 struct device *dev);
Joerg Roedel1b032ec2020-04-29 15:37:12 +020094static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
Joerg Roedel6e1aa202020-04-29 15:36:46 +020095
Alex Williamsond72e31c2012-05-30 14:18:53 -060096#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
97struct iommu_group_attribute iommu_group_attr_##_name = \
98 __ATTR(_name, _mode, _show, _store)
99
100#define to_iommu_group_attr(_attr) \
101 container_of(_attr, struct iommu_group_attribute, attr)
102#define to_iommu_group(_kobj) \
103 container_of(_kobj, struct iommu_group, kobj)
104
Joerg Roedelb0119e82017-02-01 13:23:08 +0100105static LIST_HEAD(iommu_device_list);
106static DEFINE_SPINLOCK(iommu_device_lock);
107
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200108/*
109 * Use a function instead of an array here because the domain-type is a
110 * bit-field, so an array would waste memory.
111 */
112static const char *iommu_domain_type_str(unsigned int t)
113{
114 switch (t) {
115 case IOMMU_DOMAIN_BLOCKED:
116 return "Blocked";
117 case IOMMU_DOMAIN_IDENTITY:
118 return "Passthrough";
119 case IOMMU_DOMAIN_UNMANAGED:
120 return "Unmanaged";
121 case IOMMU_DOMAIN_DMA:
122 return "Translated";
123 default:
124 return "Unknown";
125 }
126}
127
128static int __init iommu_subsys_init(void)
129{
Joerg Roedel22bb1822019-08-19 15:22:54 +0200130 bool cmd_line = iommu_cmd_line_dma_api();
131
132 if (!cmd_line) {
133 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
134 iommu_set_default_passthrough(false);
135 else
136 iommu_set_default_translated(false);
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200137
Joerg Roedel2896ba42019-09-03 15:15:44 +0200138 if (iommu_default_passthrough() && mem_encrypt_active()) {
139 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200140 iommu_set_default_translated(false);
141 }
Joerg Roedel22bb1822019-08-19 15:22:54 +0200142 }
143
144 pr_info("Default domain type: %s %s\n",
145 iommu_domain_type_str(iommu_def_domain_type),
146 cmd_line ? "(set via kernel command line)" : "");
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200147
148 return 0;
149}
150subsys_initcall(iommu_subsys_init);
151
Joerg Roedelb0119e82017-02-01 13:23:08 +0100152int iommu_device_register(struct iommu_device *iommu)
153{
154 spin_lock(&iommu_device_lock);
155 list_add_tail(&iommu->list, &iommu_device_list);
156 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100157 return 0;
158}
Will Deacona7ba5c32019-12-19 12:03:37 +0000159EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100160
161void iommu_device_unregister(struct iommu_device *iommu)
162{
163 spin_lock(&iommu_device_lock);
164 list_del(&iommu->list);
165 spin_unlock(&iommu_device_lock);
166}
Will Deacona7ba5c32019-12-19 12:03:37 +0000167EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100168
Joerg Roedel045a7042020-03-26 16:08:30 +0100169static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100170{
Joerg Roedel045a7042020-03-26 16:08:30 +0100171 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100172
173 if (param)
174 return param;
175
176 param = kzalloc(sizeof(*param), GFP_KERNEL);
177 if (!param)
178 return NULL;
179
180 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100181 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100182 return param;
183}
184
Joerg Roedel045a7042020-03-26 16:08:30 +0100185static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100186{
Joerg Roedel045a7042020-03-26 16:08:30 +0100187 kfree(dev->iommu);
188 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100189}
190
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200191static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200192{
193 const struct iommu_ops *ops = dev->bus->iommu_ops;
194 struct iommu_device *iommu_dev;
195 struct iommu_group *group;
196 int ret;
197
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200198 if (!dev_iommu_get(dev))
199 return -ENOMEM;
200
201 if (!try_module_get(ops->owner)) {
202 ret = -EINVAL;
203 goto err_free;
204 }
205
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200206 iommu_dev = ops->probe_device(dev);
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200207 if (IS_ERR(iommu_dev)) {
208 ret = PTR_ERR(iommu_dev);
209 goto out_module_put;
210 }
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200211
212 dev->iommu->iommu_dev = iommu_dev;
213
214 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200215 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200216 ret = PTR_ERR(group);
217 goto out_release;
218 }
219 iommu_group_put(group);
220
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200221 if (group_list && !group->default_domain && list_empty(&group->entry))
222 list_add_tail(&group->entry, group_list);
223
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200224 iommu_device_link(iommu_dev, dev);
225
226 return 0;
227
228out_release:
229 ops->release_device(dev);
230
Joerg Roedel4e8906f2020-04-29 15:37:11 +0200231out_module_put:
232 module_put(ops->owner);
233
234err_free:
235 dev_iommu_free(dev);
236
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200237 return ret;
238}
239
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200240int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200241{
242 const struct iommu_ops *ops = dev->bus->iommu_ops;
243 struct iommu_group *group;
244 int ret;
245
246 ret = __iommu_probe_device(dev, NULL);
247 if (ret)
248 goto err_out;
249
250 /*
251 * Try to allocate a default domain - needs support from the
252 * IOMMU driver. There are still some drivers which don't
253 * support default domains, so the return value is not yet
254 * checked.
255 */
256 iommu_alloc_default_domain(dev);
257
258 group = iommu_group_get(dev);
259 if (!group)
260 goto err_release;
261
262 if (group->default_domain)
263 ret = __iommu_attach_device(group->default_domain, dev);
264
Joerg Roedelce574c22020-04-29 15:36:50 +0200265 iommu_create_device_direct_mappings(group, dev);
266
Joerg Roedelcf193882020-04-29 15:36:48 +0200267 iommu_group_put(group);
268
269 if (ret)
270 goto err_release;
271
272 if (ops->probe_finalize)
273 ops->probe_finalize(dev);
274
275 return 0;
276
277err_release:
278 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200279
Joerg Roedelcf193882020-04-29 15:36:48 +0200280err_out:
281 return ret;
282
283}
284
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100285void iommu_release_device(struct device *dev)
286{
287 const struct iommu_ops *ops = dev->bus->iommu_ops;
288
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200289 if (!dev->iommu)
290 return;
291
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200292 iommu_device_unlink(dev->iommu->iommu_dev, dev);
293 iommu_group_remove_device(dev);
294
295 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100296
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200297 module_put(ops->owner);
298 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100299}
300
Will Deaconfccb4e32017-01-05 18:38:26 +0000301static int __init iommu_set_def_domain_type(char *str)
302{
303 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300304 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000305
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300306 ret = kstrtobool(str, &pt);
307 if (ret)
308 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000309
Joerg Roedeladab0b02019-08-19 15:22:48 +0200310 if (pt)
311 iommu_set_default_passthrough(true);
312 else
313 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200314
Will Deaconfccb4e32017-01-05 18:38:26 +0000315 return 0;
316}
317early_param("iommu.passthrough", iommu_set_def_domain_type);
318
Zhen Lei68a6efe2018-09-20 17:10:23 +0100319static int __init iommu_dma_setup(char *str)
320{
321 return kstrtobool(str, &iommu_dma_strict);
322}
323early_param("iommu.strict", iommu_dma_setup);
324
Alex Williamsond72e31c2012-05-30 14:18:53 -0600325static ssize_t iommu_group_attr_show(struct kobject *kobj,
326 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400327{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600328 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
329 struct iommu_group *group = to_iommu_group(kobj);
330 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400331
Alex Williamsond72e31c2012-05-30 14:18:53 -0600332 if (attr->show)
333 ret = attr->show(group, buf);
334 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400335}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600336
337static ssize_t iommu_group_attr_store(struct kobject *kobj,
338 struct attribute *__attr,
339 const char *buf, size_t count)
340{
341 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
342 struct iommu_group *group = to_iommu_group(kobj);
343 ssize_t ret = -EIO;
344
345 if (attr->store)
346 ret = attr->store(group, buf, count);
347 return ret;
348}
349
350static const struct sysfs_ops iommu_group_sysfs_ops = {
351 .show = iommu_group_attr_show,
352 .store = iommu_group_attr_store,
353};
354
355static int iommu_group_create_file(struct iommu_group *group,
356 struct iommu_group_attribute *attr)
357{
358 return sysfs_create_file(&group->kobj, &attr->attr);
359}
360
361static void iommu_group_remove_file(struct iommu_group *group,
362 struct iommu_group_attribute *attr)
363{
364 sysfs_remove_file(&group->kobj, &attr->attr);
365}
366
367static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
368{
369 return sprintf(buf, "%s\n", group->name);
370}
371
Eric Auger6c65fb32017-01-19 20:57:51 +0000372/**
373 * iommu_insert_resv_region - Insert a new region in the
374 * list of reserved regions.
375 * @new: new region to insert
376 * @regions: list of regions
377 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200378 * Elements are sorted by start address and overlapping segments
379 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000380 */
Eric Auger4dbd2582019-08-21 14:09:40 +0200381int iommu_insert_resv_region(struct iommu_resv_region *new,
382 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000383{
Eric Auger4dbd2582019-08-21 14:09:40 +0200384 struct iommu_resv_region *iter, *tmp, *nr, *top;
385 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000386
Eric Auger4dbd2582019-08-21 14:09:40 +0200387 nr = iommu_alloc_resv_region(new->start, new->length,
388 new->prot, new->type);
389 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000390 return -ENOMEM;
391
Eric Auger4dbd2582019-08-21 14:09:40 +0200392 /* First add the new element based on start address sorting */
393 list_for_each_entry(iter, regions, list) {
394 if (nr->start < iter->start ||
395 (nr->start == iter->start && nr->type <= iter->type))
396 break;
397 }
398 list_add_tail(&nr->list, &iter->list);
399
400 /* Merge overlapping segments of type nr->type in @regions, if any */
401 list_for_each_entry_safe(iter, tmp, regions, list) {
402 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
403
Eric Auger4c80ba32019-11-26 18:54:13 +0100404 /* no merge needed on elements of different types than @new */
405 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200406 list_move_tail(&iter->list, &stack);
407 continue;
408 }
409
410 /* look for the last stack element of same type as @iter */
411 list_for_each_entry_reverse(top, &stack, list)
412 if (top->type == iter->type)
413 goto check_overlap;
414
415 list_move_tail(&iter->list, &stack);
416 continue;
417
418check_overlap:
419 top_end = top->start + top->length - 1;
420
421 if (iter->start > top_end + 1) {
422 list_move_tail(&iter->list, &stack);
423 } else {
424 top->length = max(top_end, iter_end) - top->start + 1;
425 list_del(&iter->list);
426 kfree(iter);
427 }
428 }
429 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000430 return 0;
431}
432
433static int
434iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
435 struct list_head *group_resv_regions)
436{
437 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100438 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000439
440 list_for_each_entry(entry, dev_resv_regions, list) {
441 ret = iommu_insert_resv_region(entry, group_resv_regions);
442 if (ret)
443 break;
444 }
445 return ret;
446}
447
448int iommu_get_group_resv_regions(struct iommu_group *group,
449 struct list_head *head)
450{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100451 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000452 int ret = 0;
453
454 mutex_lock(&group->mutex);
455 list_for_each_entry(device, &group->devices, list) {
456 struct list_head dev_resv_regions;
457
458 INIT_LIST_HEAD(&dev_resv_regions);
459 iommu_get_resv_regions(device->dev, &dev_resv_regions);
460 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
461 iommu_put_resv_regions(device->dev, &dev_resv_regions);
462 if (ret)
463 break;
464 }
465 mutex_unlock(&group->mutex);
466 return ret;
467}
468EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
469
Eric Augerbc7d12b92017-01-19 20:57:52 +0000470static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
471 char *buf)
472{
473 struct iommu_resv_region *region, *next;
474 struct list_head group_resv_regions;
475 char *str = buf;
476
477 INIT_LIST_HEAD(&group_resv_regions);
478 iommu_get_group_resv_regions(group, &group_resv_regions);
479
480 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
481 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
482 (long long int)region->start,
483 (long long int)(region->start +
484 region->length - 1),
485 iommu_group_resv_type_string[region->type]);
486 kfree(region);
487 }
488
489 return (str - buf);
490}
491
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700492static ssize_t iommu_group_show_type(struct iommu_group *group,
493 char *buf)
494{
495 char *type = "unknown\n";
496
497 if (group->default_domain) {
498 switch (group->default_domain->type) {
499 case IOMMU_DOMAIN_BLOCKED:
500 type = "blocked\n";
501 break;
502 case IOMMU_DOMAIN_IDENTITY:
503 type = "identity\n";
504 break;
505 case IOMMU_DOMAIN_UNMANAGED:
506 type = "unmanaged\n";
507 break;
508 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800509 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700510 break;
511 }
512 }
513 strcpy(buf, type);
514
515 return strlen(type);
516}
517
Alex Williamsond72e31c2012-05-30 14:18:53 -0600518static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
519
Eric Augerbc7d12b92017-01-19 20:57:52 +0000520static IOMMU_GROUP_ATTR(reserved_regions, 0444,
521 iommu_group_show_resv_regions, NULL);
522
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700523static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
524
Alex Williamsond72e31c2012-05-30 14:18:53 -0600525static void iommu_group_release(struct kobject *kobj)
526{
527 struct iommu_group *group = to_iommu_group(kobj);
528
Joerg Roedel269aa802015-05-28 18:41:25 +0200529 pr_debug("Releasing group %d\n", group->id);
530
Alex Williamsond72e31c2012-05-30 14:18:53 -0600531 if (group->iommu_data_release)
532 group->iommu_data_release(group->iommu_data);
533
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200534 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600535
Joerg Roedel53723dc2015-05-28 18:41:29 +0200536 if (group->default_domain)
537 iommu_domain_free(group->default_domain);
538
Alex Williamsond72e31c2012-05-30 14:18:53 -0600539 kfree(group->name);
540 kfree(group);
541}
542
543static struct kobj_type iommu_group_ktype = {
544 .sysfs_ops = &iommu_group_sysfs_ops,
545 .release = iommu_group_release,
546};
547
548/**
549 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600550 *
551 * This function is called by an iommu driver to allocate a new iommu
552 * group. The iommu group represents the minimum granularity of the iommu.
553 * Upon successful return, the caller holds a reference to the supplied
554 * group in order to hold the group until devices are added. Use
555 * iommu_group_put() to release this extra reference count, allowing the
556 * group to be automatically reclaimed once it has no devices or external
557 * references.
558 */
559struct iommu_group *iommu_group_alloc(void)
560{
561 struct iommu_group *group;
562 int ret;
563
564 group = kzalloc(sizeof(*group), GFP_KERNEL);
565 if (!group)
566 return ERR_PTR(-ENOMEM);
567
568 group->kobj.kset = iommu_group_kset;
569 mutex_init(&group->mutex);
570 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200571 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600572 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
573
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200574 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
575 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600576 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200577 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600578 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200579 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600580
581 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
582 NULL, "%d", group->id);
583 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200584 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600585 kfree(group);
586 return ERR_PTR(ret);
587 }
588
589 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
590 if (!group->devices_kobj) {
591 kobject_put(&group->kobj); /* triggers .release & free */
592 return ERR_PTR(-ENOMEM);
593 }
594
595 /*
596 * The devices_kobj holds a reference on the group kobject, so
597 * as long as that exists so will the group. We can therefore
598 * use the devices_kobj for reference counting.
599 */
600 kobject_put(&group->kobj);
601
Eric Augerbc7d12b92017-01-19 20:57:52 +0000602 ret = iommu_group_create_file(group,
603 &iommu_group_attr_reserved_regions);
604 if (ret)
605 return ERR_PTR(ret);
606
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700607 ret = iommu_group_create_file(group, &iommu_group_attr_type);
608 if (ret)
609 return ERR_PTR(ret);
610
Joerg Roedel269aa802015-05-28 18:41:25 +0200611 pr_debug("Allocated group %d\n", group->id);
612
Alex Williamsond72e31c2012-05-30 14:18:53 -0600613 return group;
614}
615EXPORT_SYMBOL_GPL(iommu_group_alloc);
616
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100617struct iommu_group *iommu_group_get_by_id(int id)
618{
619 struct kobject *group_kobj;
620 struct iommu_group *group;
621 const char *name;
622
623 if (!iommu_group_kset)
624 return NULL;
625
626 name = kasprintf(GFP_KERNEL, "%d", id);
627 if (!name)
628 return NULL;
629
630 group_kobj = kset_find_obj(iommu_group_kset, name);
631 kfree(name);
632
633 if (!group_kobj)
634 return NULL;
635
636 group = container_of(group_kobj, struct iommu_group, kobj);
637 BUG_ON(group->id != id);
638
639 kobject_get(group->devices_kobj);
640 kobject_put(&group->kobj);
641
642 return group;
643}
644EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
645
Alex Williamsond72e31c2012-05-30 14:18:53 -0600646/**
647 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
648 * @group: the group
649 *
650 * iommu drivers can store data in the group for use when doing iommu
651 * operations. This function provides a way to retrieve it. Caller
652 * should hold a group reference.
653 */
654void *iommu_group_get_iommudata(struct iommu_group *group)
655{
656 return group->iommu_data;
657}
658EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
659
660/**
661 * iommu_group_set_iommudata - set iommu_data for a group
662 * @group: the group
663 * @iommu_data: new data
664 * @release: release function for iommu_data
665 *
666 * iommu drivers can store data in the group for use when doing iommu
667 * operations. This function provides a way to set the data after
668 * the group has been allocated. Caller should hold a group reference.
669 */
670void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
671 void (*release)(void *iommu_data))
672{
673 group->iommu_data = iommu_data;
674 group->iommu_data_release = release;
675}
676EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
677
678/**
679 * iommu_group_set_name - set name for a group
680 * @group: the group
681 * @name: name
682 *
683 * Allow iommu driver to set a name for a group. When set it will
684 * appear in a name attribute file under the group in sysfs.
685 */
686int iommu_group_set_name(struct iommu_group *group, const char *name)
687{
688 int ret;
689
690 if (group->name) {
691 iommu_group_remove_file(group, &iommu_group_attr_name);
692 kfree(group->name);
693 group->name = NULL;
694 if (!name)
695 return 0;
696 }
697
698 group->name = kstrdup(name, GFP_KERNEL);
699 if (!group->name)
700 return -ENOMEM;
701
702 ret = iommu_group_create_file(group, &iommu_group_attr_name);
703 if (ret) {
704 kfree(group->name);
705 group->name = NULL;
706 return ret;
707 }
708
709 return 0;
710}
711EXPORT_SYMBOL_GPL(iommu_group_set_name);
712
Joerg Roedelce574c22020-04-29 15:36:50 +0200713static int iommu_create_device_direct_mappings(struct iommu_group *group,
714 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200715{
716 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000717 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200718 struct list_head mappings;
719 unsigned long pg_size;
720 int ret = 0;
721
722 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
723 return 0;
724
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100725 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200726
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100727 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200728 INIT_LIST_HEAD(&mappings);
729
Eric Augere5b52342017-01-19 20:57:47 +0000730 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200731
732 /* We need to consider overlapping regions for different devices */
733 list_for_each_entry(entry, &mappings, list) {
734 dma_addr_t start, end, addr;
735
Eric Augere5b52342017-01-19 20:57:47 +0000736 if (domain->ops->apply_resv_region)
737 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200738
Joerg Roedelbeed2822015-05-28 18:41:34 +0200739 start = ALIGN(entry->start, pg_size);
740 end = ALIGN(entry->start + entry->length, pg_size);
741
Eric Augeradfd3732019-06-03 08:53:35 +0200742 if (entry->type != IOMMU_RESV_DIRECT &&
743 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000744 continue;
745
Joerg Roedelbeed2822015-05-28 18:41:34 +0200746 for (addr = start; addr < end; addr += pg_size) {
747 phys_addr_t phys_addr;
748
749 phys_addr = iommu_iova_to_phys(domain, addr);
750 if (phys_addr)
751 continue;
752
753 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
754 if (ret)
755 goto out;
756 }
757
758 }
759
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200760 iommu_flush_tlb_all(domain);
761
Joerg Roedelbeed2822015-05-28 18:41:34 +0200762out:
Eric Augere5b52342017-01-19 20:57:47 +0000763 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200764
765 return ret;
766}
767
Alex Williamsond72e31c2012-05-30 14:18:53 -0600768/**
769 * iommu_group_add_device - add a device to an iommu group
770 * @group: the group into which to add the device (reference should be held)
771 * @dev: the device
772 *
773 * This function is called by an iommu driver to add a device into a
774 * group. Adding a device increments the group reference count.
775 */
776int iommu_group_add_device(struct iommu_group *group, struct device *dev)
777{
778 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100779 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600780
781 device = kzalloc(sizeof(*device), GFP_KERNEL);
782 if (!device)
783 return -ENOMEM;
784
785 device->dev = dev;
786
787 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000788 if (ret)
789 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600790
791 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
792rename:
793 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000794 ret = -ENOMEM;
795 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600796 }
797
798 ret = sysfs_create_link_nowarn(group->devices_kobj,
799 &dev->kobj, device->name);
800 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600801 if (ret == -EEXIST && i >= 0) {
802 /*
803 * Account for the slim chance of collision
804 * and append an instance to the name.
805 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000806 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600807 device->name = kasprintf(GFP_KERNEL, "%s.%d",
808 kobject_name(&dev->kobj), i++);
809 goto rename;
810 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000811 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600812 }
813
814 kobject_get(group->devices_kobj);
815
816 dev->iommu_group = group;
817
818 mutex_lock(&group->mutex);
819 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200820 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000821 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600822 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000823 if (ret)
824 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600825
826 /* Notify any listeners about change to group. */
827 blocking_notifier_call_chain(&group->notifier,
828 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600829
830 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200831
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600832 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200833
Alex Williamsond72e31c2012-05-30 14:18:53 -0600834 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000835
836err_put_group:
837 mutex_lock(&group->mutex);
838 list_del(&device->list);
839 mutex_unlock(&group->mutex);
840 dev->iommu_group = NULL;
841 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700842 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000843err_free_name:
844 kfree(device->name);
845err_remove_link:
846 sysfs_remove_link(&dev->kobj, "iommu_group");
847err_free_device:
848 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600849 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000850 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600851}
852EXPORT_SYMBOL_GPL(iommu_group_add_device);
853
854/**
855 * iommu_group_remove_device - remove a device from it's current group
856 * @dev: device to be removed
857 *
858 * This function is called by an iommu driver to remove the device from
859 * it's current group. This decrements the iommu group reference count.
860 */
861void iommu_group_remove_device(struct device *dev)
862{
863 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100864 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600865
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600866 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200867
Alex Williamsond72e31c2012-05-30 14:18:53 -0600868 /* Pre-notify listeners that a device is being removed. */
869 blocking_notifier_call_chain(&group->notifier,
870 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
871
872 mutex_lock(&group->mutex);
873 list_for_each_entry(tmp_device, &group->devices, list) {
874 if (tmp_device->dev == dev) {
875 device = tmp_device;
876 list_del(&device->list);
877 break;
878 }
879 }
880 mutex_unlock(&group->mutex);
881
882 if (!device)
883 return;
884
885 sysfs_remove_link(group->devices_kobj, device->name);
886 sysfs_remove_link(&dev->kobj, "iommu_group");
887
Shuah Khan2e757082013-08-15 11:59:25 -0600888 trace_remove_device_from_group(group->id, dev);
889
Alex Williamsond72e31c2012-05-30 14:18:53 -0600890 kfree(device->name);
891 kfree(device);
892 dev->iommu_group = NULL;
893 kobject_put(group->devices_kobj);
894}
895EXPORT_SYMBOL_GPL(iommu_group_remove_device);
896
Joerg Roedel426a2732015-05-28 18:41:30 +0200897static int iommu_group_device_count(struct iommu_group *group)
898{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100899 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200900 int ret = 0;
901
902 list_for_each_entry(entry, &group->devices, list)
903 ret++;
904
905 return ret;
906}
907
Alex Williamsond72e31c2012-05-30 14:18:53 -0600908/**
909 * iommu_group_for_each_dev - iterate over each device in the group
910 * @group: the group
911 * @data: caller opaque data to be passed to callback function
912 * @fn: caller supplied callback function
913 *
914 * This function is called by group users to iterate over group devices.
915 * Callers should hold a reference count to the group during callback.
916 * The group->mutex is held across callbacks, which will block calls to
917 * iommu_group_add/remove_device.
918 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200919static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
920 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600921{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100922 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600923 int ret = 0;
924
Alex Williamsond72e31c2012-05-30 14:18:53 -0600925 list_for_each_entry(device, &group->devices, list) {
926 ret = fn(device->dev, data);
927 if (ret)
928 break;
929 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200930 return ret;
931}
932
933
934int iommu_group_for_each_dev(struct iommu_group *group, void *data,
935 int (*fn)(struct device *, void *))
936{
937 int ret;
938
939 mutex_lock(&group->mutex);
940 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600941 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200942
Alex Williamsond72e31c2012-05-30 14:18:53 -0600943 return ret;
944}
945EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
946
947/**
948 * iommu_group_get - Return the group for a device and increment reference
949 * @dev: get the group that this device belongs to
950 *
951 * This function is called by iommu drivers and users to get the group
952 * for the specified device. If found, the group is returned and the group
953 * reference in incremented, else NULL.
954 */
955struct iommu_group *iommu_group_get(struct device *dev)
956{
957 struct iommu_group *group = dev->iommu_group;
958
959 if (group)
960 kobject_get(group->devices_kobj);
961
962 return group;
963}
964EXPORT_SYMBOL_GPL(iommu_group_get);
965
966/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000967 * iommu_group_ref_get - Increment reference on a group
968 * @group: the group to use, must not be NULL
969 *
970 * This function is called by iommu drivers to take additional references on an
971 * existing group. Returns the given group for convenience.
972 */
973struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
974{
975 kobject_get(group->devices_kobj);
976 return group;
977}
Will Deacona7ba5c32019-12-19 12:03:37 +0000978EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +0000979
980/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600981 * iommu_group_put - Decrement group reference
982 * @group: the group to use
983 *
984 * This function is called by iommu drivers and users to release the
985 * iommu group. Once the reference count is zero, the group is released.
986 */
987void iommu_group_put(struct iommu_group *group)
988{
989 if (group)
990 kobject_put(group->devices_kobj);
991}
992EXPORT_SYMBOL_GPL(iommu_group_put);
993
994/**
995 * iommu_group_register_notifier - Register a notifier for group changes
996 * @group: the group to watch
997 * @nb: notifier block to signal
998 *
999 * This function allows iommu group users to track changes in a group.
1000 * See include/linux/iommu.h for actions sent via this notifier. Caller
1001 * should hold a reference to the group throughout notifier registration.
1002 */
1003int iommu_group_register_notifier(struct iommu_group *group,
1004 struct notifier_block *nb)
1005{
1006 return blocking_notifier_chain_register(&group->notifier, nb);
1007}
1008EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1009
1010/**
1011 * iommu_group_unregister_notifier - Unregister a notifier
1012 * @group: the group to watch
1013 * @nb: notifier block to signal
1014 *
1015 * Unregister a previously registered group notifier block.
1016 */
1017int iommu_group_unregister_notifier(struct iommu_group *group,
1018 struct notifier_block *nb)
1019{
1020 return blocking_notifier_chain_unregister(&group->notifier, nb);
1021}
1022EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1023
1024/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001025 * iommu_register_device_fault_handler() - Register a device fault handler
1026 * @dev: the device
1027 * @handler: the fault handler
1028 * @data: private data passed as argument to the handler
1029 *
1030 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001031 * fault event and data as argument. The handler should return 0 on success. If
1032 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1033 * complete the fault by calling iommu_page_response() with one of the following
1034 * response code:
1035 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1036 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1037 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1038 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001039 *
1040 * Return 0 if the fault handler was installed successfully, or an error.
1041 */
1042int iommu_register_device_fault_handler(struct device *dev,
1043 iommu_dev_fault_handler_t handler,
1044 void *data)
1045{
Joerg Roedel045a7042020-03-26 16:08:30 +01001046 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001047 int ret = 0;
1048
1049 if (!param)
1050 return -EINVAL;
1051
1052 mutex_lock(&param->lock);
1053 /* Only allow one fault handler registered for each device */
1054 if (param->fault_param) {
1055 ret = -EBUSY;
1056 goto done_unlock;
1057 }
1058
1059 get_device(dev);
1060 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1061 if (!param->fault_param) {
1062 put_device(dev);
1063 ret = -ENOMEM;
1064 goto done_unlock;
1065 }
1066 param->fault_param->handler = handler;
1067 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001068 mutex_init(&param->fault_param->lock);
1069 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001070
1071done_unlock:
1072 mutex_unlock(&param->lock);
1073
1074 return ret;
1075}
1076EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1077
1078/**
1079 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1080 * @dev: the device
1081 *
1082 * Remove the device fault handler installed with
1083 * iommu_register_device_fault_handler().
1084 *
1085 * Return 0 on success, or an error.
1086 */
1087int iommu_unregister_device_fault_handler(struct device *dev)
1088{
Joerg Roedel045a7042020-03-26 16:08:30 +01001089 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001090 int ret = 0;
1091
1092 if (!param)
1093 return -EINVAL;
1094
1095 mutex_lock(&param->lock);
1096
1097 if (!param->fault_param)
1098 goto unlock;
1099
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001100 /* we cannot unregister handler if there are pending faults */
1101 if (!list_empty(&param->fault_param->faults)) {
1102 ret = -EBUSY;
1103 goto unlock;
1104 }
1105
Jacob Pan0c830e62019-06-03 15:57:48 +01001106 kfree(param->fault_param);
1107 param->fault_param = NULL;
1108 put_device(dev);
1109unlock:
1110 mutex_unlock(&param->lock);
1111
1112 return ret;
1113}
1114EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1115
1116/**
1117 * iommu_report_device_fault() - Report fault event to device driver
1118 * @dev: the device
1119 * @evt: fault event data
1120 *
1121 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001122 * handler. When this function fails and the fault is recoverable, it is the
1123 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001124 *
1125 * Return 0 on success, or an error.
1126 */
1127int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1128{
Joerg Roedel045a7042020-03-26 16:08:30 +01001129 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001130 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001131 struct iommu_fault_param *fparam;
1132 int ret = 0;
1133
1134 if (!param || !evt)
1135 return -EINVAL;
1136
1137 /* we only report device fault if there is a handler registered */
1138 mutex_lock(&param->lock);
1139 fparam = param->fault_param;
1140 if (!fparam || !fparam->handler) {
1141 ret = -EINVAL;
1142 goto done_unlock;
1143 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001144
1145 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1146 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1147 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1148 GFP_KERNEL);
1149 if (!evt_pending) {
1150 ret = -ENOMEM;
1151 goto done_unlock;
1152 }
1153 mutex_lock(&fparam->lock);
1154 list_add_tail(&evt_pending->list, &fparam->faults);
1155 mutex_unlock(&fparam->lock);
1156 }
1157
Jacob Pan0c830e62019-06-03 15:57:48 +01001158 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001159 if (ret && evt_pending) {
1160 mutex_lock(&fparam->lock);
1161 list_del(&evt_pending->list);
1162 mutex_unlock(&fparam->lock);
1163 kfree(evt_pending);
1164 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001165done_unlock:
1166 mutex_unlock(&param->lock);
1167 return ret;
1168}
1169EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1170
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001171int iommu_page_response(struct device *dev,
1172 struct iommu_page_response *msg)
1173{
1174 bool pasid_valid;
1175 int ret = -EINVAL;
1176 struct iommu_fault_event *evt;
1177 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001178 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001179 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1180
1181 if (!domain || !domain->ops->page_response)
1182 return -ENODEV;
1183
1184 if (!param || !param->fault_param)
1185 return -EINVAL;
1186
1187 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1188 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1189 return -EINVAL;
1190
1191 /* Only send response if there is a fault report pending */
1192 mutex_lock(&param->fault_param->lock);
1193 if (list_empty(&param->fault_param->faults)) {
1194 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1195 goto done_unlock;
1196 }
1197 /*
1198 * Check if we have a matching page request pending to respond,
1199 * otherwise return -EINVAL
1200 */
1201 list_for_each_entry(evt, &param->fault_param->faults, list) {
1202 prm = &evt->fault.prm;
1203 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1204
1205 if ((pasid_valid && prm->pasid != msg->pasid) ||
1206 prm->grpid != msg->grpid)
1207 continue;
1208
1209 /* Sanitize the reply */
1210 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1211
1212 ret = domain->ops->page_response(dev, evt, msg);
1213 list_del(&evt->list);
1214 kfree(evt);
1215 break;
1216 }
1217
1218done_unlock:
1219 mutex_unlock(&param->fault_param->lock);
1220 return ret;
1221}
1222EXPORT_SYMBOL_GPL(iommu_page_response);
1223
Jacob Pan0c830e62019-06-03 15:57:48 +01001224/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001225 * iommu_group_id - Return ID for a group
1226 * @group: the group to ID
1227 *
1228 * Return the unique ID for the group matching the sysfs group number.
1229 */
1230int iommu_group_id(struct iommu_group *group)
1231{
1232 return group->id;
1233}
1234EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001235
Alex Williamsonf096c062014-09-19 10:03:06 -06001236static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1237 unsigned long *devfns);
1238
Alex Williamson104a1c12014-07-03 09:51:18 -06001239/*
1240 * To consider a PCI device isolated, we require ACS to support Source
1241 * Validation, Request Redirection, Completer Redirection, and Upstream
1242 * Forwarding. This effectively means that devices cannot spoof their
1243 * requester ID, requests and completions cannot be redirected, and all
1244 * transactions are forwarded upstream, even as it passes through a
1245 * bridge where the target device is downstream.
1246 */
1247#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1248
Alex Williamsonf096c062014-09-19 10:03:06 -06001249/*
1250 * For multifunction devices which are not isolated from each other, find
1251 * all the other non-isolated functions and look for existing groups. For
1252 * each function, we also need to look for aliases to or from other devices
1253 * that may already have a group.
1254 */
1255static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1256 unsigned long *devfns)
1257{
1258 struct pci_dev *tmp = NULL;
1259 struct iommu_group *group;
1260
1261 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1262 return NULL;
1263
1264 for_each_pci_dev(tmp) {
1265 if (tmp == pdev || tmp->bus != pdev->bus ||
1266 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1267 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1268 continue;
1269
1270 group = get_pci_alias_group(tmp, devfns);
1271 if (group) {
1272 pci_dev_put(tmp);
1273 return group;
1274 }
1275 }
1276
1277 return NULL;
1278}
1279
1280/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001281 * Look for aliases to or from the given device for existing groups. DMA
1282 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001283 * space is quite small (especially since we're really only looking at pcie
1284 * device, and therefore only expect multiple slots on the root complex or
1285 * downstream switch ports). It's conceivable though that a pair of
1286 * multifunction devices could have aliases between them that would cause a
1287 * loop. To prevent this, we use a bitmap to track where we've been.
1288 */
1289static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1290 unsigned long *devfns)
1291{
1292 struct pci_dev *tmp = NULL;
1293 struct iommu_group *group;
1294
1295 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1296 return NULL;
1297
1298 group = iommu_group_get(&pdev->dev);
1299 if (group)
1300 return group;
1301
1302 for_each_pci_dev(tmp) {
1303 if (tmp == pdev || tmp->bus != pdev->bus)
1304 continue;
1305
1306 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001307 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001308 group = get_pci_alias_group(tmp, devfns);
1309 if (group) {
1310 pci_dev_put(tmp);
1311 return group;
1312 }
1313
1314 group = get_pci_function_alias_group(tmp, devfns);
1315 if (group) {
1316 pci_dev_put(tmp);
1317 return group;
1318 }
1319 }
1320 }
1321
1322 return NULL;
1323}
1324
Alex Williamson104a1c12014-07-03 09:51:18 -06001325struct group_for_pci_data {
1326 struct pci_dev *pdev;
1327 struct iommu_group *group;
1328};
1329
1330/*
1331 * DMA alias iterator callback, return the last seen device. Stop and return
1332 * the IOMMU group if we find one along the way.
1333 */
1334static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1335{
1336 struct group_for_pci_data *data = opaque;
1337
1338 data->pdev = pdev;
1339 data->group = iommu_group_get(&pdev->dev);
1340
1341 return data->group != NULL;
1342}
1343
1344/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001345 * Generic device_group call-back function. It just allocates one
1346 * iommu-group per device.
1347 */
1348struct iommu_group *generic_device_group(struct device *dev)
1349{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001350 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001351}
Will Deacona7ba5c32019-12-19 12:03:37 +00001352EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001353
1354/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001355 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1356 * to find or create an IOMMU group for a device.
1357 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001358struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001359{
Joerg Roedel5e622922015-10-21 23:51:37 +02001360 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001361 struct group_for_pci_data data;
1362 struct pci_bus *bus;
1363 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001364 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001365
Joerg Roedel5e622922015-10-21 23:51:37 +02001366 if (WARN_ON(!dev_is_pci(dev)))
1367 return ERR_PTR(-EINVAL);
1368
Alex Williamson104a1c12014-07-03 09:51:18 -06001369 /*
1370 * Find the upstream DMA alias for the device. A device must not
1371 * be aliased due to topology in order to have its own IOMMU group.
1372 * If we find an alias along the way that already belongs to a
1373 * group, use it.
1374 */
1375 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1376 return data.group;
1377
1378 pdev = data.pdev;
1379
1380 /*
1381 * Continue upstream from the point of minimum IOMMU granularity
1382 * due to aliases to the point where devices are protected from
1383 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1384 * group, use it.
1385 */
1386 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1387 if (!bus->self)
1388 continue;
1389
1390 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1391 break;
1392
1393 pdev = bus->self;
1394
1395 group = iommu_group_get(&pdev->dev);
1396 if (group)
1397 return group;
1398 }
1399
1400 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001401 * Look for existing groups on device aliases. If we alias another
1402 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001403 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001404 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1405 if (group)
1406 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001407
1408 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001409 * Look for existing groups on non-isolated functions on the same
1410 * slot and aliases of those funcions, if any. No need to clear
1411 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001412 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001413 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1414 if (group)
1415 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001416
1417 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001418 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001419}
Will Deacona7ba5c32019-12-19 12:03:37 +00001420EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001421
Nipun Guptaeab03e22018-09-10 19:19:18 +05301422/* Get the IOMMU group for device on fsl-mc bus */
1423struct iommu_group *fsl_mc_device_group(struct device *dev)
1424{
1425 struct device *cont_dev = fsl_mc_cont_dev(dev);
1426 struct iommu_group *group;
1427
1428 group = iommu_group_get(cont_dev);
1429 if (!group)
1430 group = iommu_group_alloc();
1431 return group;
1432}
Will Deacona7ba5c32019-12-19 12:03:37 +00001433EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301434
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001435static int iommu_get_def_domain_type(struct device *dev)
1436{
1437 const struct iommu_ops *ops = dev->bus->iommu_ops;
1438 unsigned int type = 0;
1439
1440 if (ops->def_domain_type)
1441 type = ops->def_domain_type(dev);
1442
1443 return (type == 0) ? iommu_def_domain_type : type;
1444}
1445
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001446static int iommu_group_alloc_default_domain(struct bus_type *bus,
1447 struct iommu_group *group,
1448 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001449{
1450 struct iommu_domain *dom;
1451
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001452 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001453 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001454 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1455 if (dom)
1456 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1457 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001458 }
1459
1460 if (!dom)
1461 return -ENOMEM;
1462
1463 group->default_domain = dom;
1464 if (!group->domain)
1465 group->domain = dom;
1466
1467 if (!iommu_dma_strict) {
1468 int attr = 1;
1469 iommu_domain_set_attr(dom,
1470 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1471 &attr);
1472 }
1473
1474 return 0;
1475}
1476
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001477static int iommu_alloc_default_domain(struct device *dev)
1478{
1479 struct iommu_group *group;
1480 unsigned int type;
1481
1482 group = iommu_group_get(dev);
1483 if (!group)
1484 return -ENODEV;
1485
1486 if (group->default_domain)
1487 return 0;
1488
1489 type = iommu_get_def_domain_type(dev);
1490
1491 return iommu_group_alloc_default_domain(dev->bus, group, type);
1492}
1493
Alex Williamson104a1c12014-07-03 09:51:18 -06001494/**
1495 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1496 * @dev: target device
1497 *
1498 * This function is intended to be called by IOMMU drivers and extended to
1499 * support common, bus-defined algorithms when determining or creating the
1500 * IOMMU group for a device. On success, the caller will hold a reference
1501 * to the returned IOMMU group, which will already include the provided
1502 * device. The reference should be released with iommu_group_put().
1503 */
Joerg Roedel1b032ec2020-04-29 15:37:12 +02001504static struct iommu_group *iommu_group_get_for_dev(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001505{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001506 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001507 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001508 int ret;
1509
1510 group = iommu_group_get(dev);
1511 if (group)
1512 return group;
1513
Robin Murphy05f803002017-07-21 13:12:38 +01001514 if (!ops)
1515 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001516
Robin Murphy05f803002017-07-21 13:12:38 +01001517 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001518 if (WARN_ON_ONCE(group == NULL))
1519 return ERR_PTR(-EINVAL);
1520
Alex Williamson104a1c12014-07-03 09:51:18 -06001521 if (IS_ERR(group))
1522 return group;
1523
1524 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001525 if (ret)
1526 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001527
1528 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001529
1530out_put_group:
1531 iommu_group_put(group);
1532
1533 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001534}
1535
Joerg Roedel6827ca82015-05-28 18:41:35 +02001536struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1537{
1538 return group->default_domain;
1539}
1540
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001541static int probe_iommu_group(struct device *dev, void *data)
1542{
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001543 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001544 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001545 int ret;
1546
Joerg Roedel5012c392020-04-29 15:36:51 +02001547 /* Device is probed already if in a group */
1548 group = iommu_group_get(dev);
1549 if (group) {
1550 iommu_group_put(group);
1551 return 0;
1552 }
1553
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001554 ret = __iommu_probe_device(dev, group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001555 if (ret == -ENODEV)
1556 ret = 0;
1557
1558 return ret;
1559}
1560
Joerg Roedel8da30142015-05-28 18:41:27 +02001561static int remove_iommu_group(struct device *dev, void *data)
1562{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001563 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001564
1565 return 0;
1566}
1567
Alex Williamsond72e31c2012-05-30 14:18:53 -06001568static int iommu_bus_notifier(struct notifier_block *nb,
1569 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001570{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001571 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001572 struct device *dev = data;
1573 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001574
Alex Williamsond72e31c2012-05-30 14:18:53 -06001575 /*
1576 * ADD/DEL call into iommu driver ops if provided, which may
1577 * result in ADD/DEL notifiers to group->notifier
1578 */
1579 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001580 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001581
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001582 ret = iommu_probe_device(dev);
1583 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001584 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001585 iommu_release_device(dev);
1586 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001587 }
Alex Williamson14604322011-10-21 15:56:05 -04001588
Alex Williamsond72e31c2012-05-30 14:18:53 -06001589 /*
1590 * Remaining BUS_NOTIFYs get filtered and republished to the
1591 * group, if anyone is listening
1592 */
1593 group = iommu_group_get(dev);
1594 if (!group)
1595 return 0;
1596
1597 switch (action) {
1598 case BUS_NOTIFY_BIND_DRIVER:
1599 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1600 break;
1601 case BUS_NOTIFY_BOUND_DRIVER:
1602 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1603 break;
1604 case BUS_NOTIFY_UNBIND_DRIVER:
1605 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1606 break;
1607 case BUS_NOTIFY_UNBOUND_DRIVER:
1608 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1609 break;
1610 }
1611
1612 if (group_action)
1613 blocking_notifier_call_chain(&group->notifier,
1614 group_action, dev);
1615
1616 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001617 return 0;
1618}
1619
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001620struct __group_domain_type {
1621 struct device *dev;
1622 unsigned int type;
1623};
1624
1625static int probe_get_default_domain_type(struct device *dev, void *data)
1626{
1627 const struct iommu_ops *ops = dev->bus->iommu_ops;
1628 struct __group_domain_type *gtype = data;
1629 unsigned int type = 0;
1630
1631 if (ops->def_domain_type)
1632 type = ops->def_domain_type(dev);
1633
1634 if (type) {
1635 if (gtype->type && gtype->type != type) {
1636 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1637 iommu_domain_type_str(type),
1638 dev_name(gtype->dev),
1639 iommu_domain_type_str(gtype->type));
1640 gtype->type = 0;
1641 }
1642
1643 if (!gtype->dev) {
1644 gtype->dev = dev;
1645 gtype->type = type;
1646 }
1647 }
1648
1649 return 0;
1650}
1651
1652static void probe_alloc_default_domain(struct bus_type *bus,
1653 struct iommu_group *group)
1654{
1655 struct __group_domain_type gtype;
1656
1657 memset(&gtype, 0, sizeof(gtype));
1658
1659 /* Ask for default domain requirements of all devices in the group */
1660 __iommu_group_for_each_dev(group, &gtype,
1661 probe_get_default_domain_type);
1662
1663 if (!gtype.type)
1664 gtype.type = iommu_def_domain_type;
1665
1666 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001667
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001668}
1669
1670static int iommu_group_do_dma_attach(struct device *dev, void *data)
1671{
1672 struct iommu_domain *domain = data;
1673 const struct iommu_ops *ops;
1674 int ret;
1675
1676 ret = __iommu_attach_device(domain, dev);
1677
1678 ops = domain->ops;
1679
1680 if (ret == 0 && ops->probe_finalize)
1681 ops->probe_finalize(dev);
1682
1683 return ret;
1684}
1685
1686static int __iommu_group_dma_attach(struct iommu_group *group)
1687{
1688 return __iommu_group_for_each_dev(group, group->default_domain,
1689 iommu_group_do_dma_attach);
1690}
1691
Joerg Roedelce574c22020-04-29 15:36:50 +02001692static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1693{
1694 struct iommu_group *group = data;
1695
1696 iommu_create_device_direct_mappings(group, dev);
1697
1698 return 0;
1699}
1700
1701static int iommu_group_create_direct_mappings(struct iommu_group *group)
1702{
1703 return __iommu_group_for_each_dev(group, group,
1704 iommu_do_create_direct_mappings);
1705}
1706
Joerg Roedel5012c392020-04-29 15:36:51 +02001707int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001708{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001709 struct iommu_group *group, *next;
1710 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001711 int ret;
1712
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001713 /*
1714 * This code-path does not allocate the default domain when
1715 * creating the iommu group, so do it after the groups are
1716 * created.
1717 */
1718 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1719 if (ret)
1720 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001721
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001722 list_for_each_entry_safe(group, next, &group_list, entry) {
1723 /* Remove item from the list */
1724 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001725
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001726 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001727
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001728 /* Try to allocate default domain */
1729 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001730
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001731 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001732 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001733 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001734 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001735
1736 iommu_group_create_direct_mappings(group);
1737
1738 ret = __iommu_group_dma_attach(group);
1739
1740 mutex_unlock(&group->mutex);
1741
1742 if (ret)
1743 break;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001744 }
1745
1746 return ret;
1747}
1748
Mark Salterfb3e3062014-09-21 13:58:24 -04001749static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001750{
Mark Salterfb3e3062014-09-21 13:58:24 -04001751 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001752 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001753
Mark Salterfb3e3062014-09-21 13:58:24 -04001754 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1755 if (!nb)
1756 return -ENOMEM;
1757
1758 nb->notifier_call = iommu_bus_notifier;
1759
1760 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001761 if (err)
1762 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001763
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001764 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001765 if (err)
1766 goto out_err;
1767
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001768
1769 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001770
1771out_err:
1772 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001773 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001774 bus_unregister_notifier(bus, nb);
1775
1776out_free:
1777 kfree(nb);
1778
1779 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001780}
1781
Joerg Roedelff217762011-08-26 16:48:26 +02001782/**
1783 * bus_set_iommu - set iommu-callbacks for the bus
1784 * @bus: bus.
1785 * @ops: the callbacks provided by the iommu-driver
1786 *
1787 * This function is called by an iommu driver to set the iommu methods
1788 * used for a particular bus. Drivers for devices on that bus can use
1789 * the iommu-api after these ops are registered.
1790 * This special function is needed because IOMMUs are usually devices on
1791 * the bus itself, so the iommu drivers are not initialized when the bus
1792 * is set up. With this function the iommu-driver can set the iommu-ops
1793 * afterwards.
1794 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001795int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001796{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001797 int err;
1798
Will Deacon4312cf72019-12-19 12:03:43 +00001799 if (ops == NULL) {
1800 bus->iommu_ops = NULL;
1801 return 0;
1802 }
1803
Joerg Roedelff217762011-08-26 16:48:26 +02001804 if (bus->iommu_ops != NULL)
1805 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001806
Joerg Roedelff217762011-08-26 16:48:26 +02001807 bus->iommu_ops = ops;
1808
1809 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001810 err = iommu_bus_init(bus, ops);
1811 if (err)
1812 bus->iommu_ops = NULL;
1813
1814 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001815}
Joerg Roedelff217762011-08-26 16:48:26 +02001816EXPORT_SYMBOL_GPL(bus_set_iommu);
1817
Joerg Roedela1b60c12011-09-06 18:46:34 +02001818bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001819{
Joerg Roedel94441c32011-09-06 18:58:54 +02001820 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001821}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001822EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001823
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001824bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1825{
1826 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1827 return false;
1828
1829 return bus->iommu_ops->capable(cap);
1830}
1831EXPORT_SYMBOL_GPL(iommu_capable);
1832
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001833/**
1834 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1835 * @domain: iommu domain
1836 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001837 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001838 *
1839 * This function should be used by IOMMU users which want to be notified
1840 * whenever an IOMMU fault happens.
1841 *
1842 * The fault handler itself should return 0 on success, and an appropriate
1843 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001844 */
1845void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001846 iommu_fault_handler_t handler,
1847 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001848{
1849 BUG_ON(!domain);
1850
1851 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001852 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001853}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001854EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001855
Joerg Roedel53723dc2015-05-28 18:41:29 +02001856static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1857 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001858{
1859 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001860
Joerg Roedel94441c32011-09-06 18:58:54 +02001861 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001862 return NULL;
1863
Joerg Roedel53723dc2015-05-28 18:41:29 +02001864 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001865 if (!domain)
1866 return NULL;
1867
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001868 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001869 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001870 /* Assume all sizes by default; the driver may override this later */
1871 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001872
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001873 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001874}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001875
Joerg Roedel53723dc2015-05-28 18:41:29 +02001876struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1877{
1878 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001879}
1880EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1881
1882void iommu_domain_free(struct iommu_domain *domain)
1883{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001884 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001885}
1886EXPORT_SYMBOL_GPL(iommu_domain_free);
1887
Joerg Roedel426a2732015-05-28 18:41:30 +02001888static int __iommu_attach_device(struct iommu_domain *domain,
1889 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001890{
Shuah Khanb54db772013-08-15 11:59:26 -06001891 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001892 if ((domain->ops->is_attach_deferred != NULL) &&
1893 domain->ops->is_attach_deferred(domain, dev))
1894 return 0;
1895
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001896 if (unlikely(domain->ops->attach_dev == NULL))
1897 return -ENODEV;
1898
Shuah Khanb54db772013-08-15 11:59:26 -06001899 ret = domain->ops->attach_dev(domain, dev);
1900 if (!ret)
1901 trace_attach_device_to_domain(dev);
1902 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001903}
Joerg Roedel426a2732015-05-28 18:41:30 +02001904
1905int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1906{
1907 struct iommu_group *group;
1908 int ret;
1909
1910 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001911 if (!group)
1912 return -ENODEV;
1913
Joerg Roedel426a2732015-05-28 18:41:30 +02001914 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001915 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001916 * change while we are attaching
1917 */
1918 mutex_lock(&group->mutex);
1919 ret = -EINVAL;
1920 if (iommu_group_device_count(group) != 1)
1921 goto out_unlock;
1922
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001923 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001924
1925out_unlock:
1926 mutex_unlock(&group->mutex);
1927 iommu_group_put(group);
1928
1929 return ret;
1930}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001931EXPORT_SYMBOL_GPL(iommu_attach_device);
1932
Yi L Liu4c7c1712019-10-02 12:42:40 -07001933int iommu_cache_invalidate(struct iommu_domain *domain, struct device *dev,
1934 struct iommu_cache_invalidate_info *inv_info)
1935{
1936 if (unlikely(!domain->ops->cache_invalidate))
1937 return -ENODEV;
1938
1939 return domain->ops->cache_invalidate(domain, dev, inv_info);
1940}
1941EXPORT_SYMBOL_GPL(iommu_cache_invalidate);
1942
Jacob Pan808be0a2019-10-02 12:42:43 -07001943int iommu_sva_bind_gpasid(struct iommu_domain *domain,
1944 struct device *dev, struct iommu_gpasid_bind_data *data)
1945{
1946 if (unlikely(!domain->ops->sva_bind_gpasid))
1947 return -ENODEV;
1948
1949 return domain->ops->sva_bind_gpasid(domain, dev, data);
1950}
1951EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid);
1952
1953int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
1954 ioasid_t pasid)
1955{
1956 if (unlikely(!domain->ops->sva_unbind_gpasid))
1957 return -ENODEV;
1958
1959 return domain->ops->sva_unbind_gpasid(dev, pasid);
1960}
1961EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
1962
Joerg Roedel426a2732015-05-28 18:41:30 +02001963static void __iommu_detach_device(struct iommu_domain *domain,
1964 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001965{
Baoquan Hee01d1912017-08-09 16:33:40 +08001966 if ((domain->ops->is_attach_deferred != NULL) &&
1967 domain->ops->is_attach_deferred(domain, dev))
1968 return;
1969
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001970 if (unlikely(domain->ops->detach_dev == NULL))
1971 return;
1972
1973 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001974 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001975}
Joerg Roedel426a2732015-05-28 18:41:30 +02001976
1977void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1978{
1979 struct iommu_group *group;
1980
1981 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001982 if (!group)
1983 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001984
1985 mutex_lock(&group->mutex);
1986 if (iommu_group_device_count(group) != 1) {
1987 WARN_ON(1);
1988 goto out_unlock;
1989 }
1990
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001991 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001992
1993out_unlock:
1994 mutex_unlock(&group->mutex);
1995 iommu_group_put(group);
1996}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001997EXPORT_SYMBOL_GPL(iommu_detach_device);
1998
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001999struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2000{
2001 struct iommu_domain *domain;
2002 struct iommu_group *group;
2003
2004 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002005 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002006 return NULL;
2007
2008 domain = group->domain;
2009
2010 iommu_group_put(group);
2011
2012 return domain;
2013}
2014EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2015
Alex Williamsond72e31c2012-05-30 14:18:53 -06002016/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002017 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2018 * guarantees that the group and its default domain are valid and correct.
2019 */
2020struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2021{
2022 return dev->iommu_group->default_domain;
2023}
2024
2025/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002026 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002027 * the IOMMU API works on domains and devices. Bridge that gap by
2028 * iterating over the devices in a group. Ideally we'd have a single
2029 * device which represents the requestor ID of the group, but we also
2030 * allow IOMMU drivers to create policy defined minimum sets, where
2031 * the physical hardware may be able to distiguish members, but we
2032 * wish to group them at a higher level (ex. untrusted multi-function
2033 * PCI devices). Thus we attach each device.
2034 */
2035static int iommu_group_do_attach_device(struct device *dev, void *data)
2036{
2037 struct iommu_domain *domain = data;
2038
Joerg Roedel426a2732015-05-28 18:41:30 +02002039 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002040}
2041
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002042static int __iommu_attach_group(struct iommu_domain *domain,
2043 struct iommu_group *group)
2044{
2045 int ret;
2046
2047 if (group->default_domain && group->domain != group->default_domain)
2048 return -EBUSY;
2049
2050 ret = __iommu_group_for_each_dev(group, domain,
2051 iommu_group_do_attach_device);
2052 if (ret == 0)
2053 group->domain = domain;
2054
2055 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002056}
2057
2058int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2059{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002060 int ret;
2061
2062 mutex_lock(&group->mutex);
2063 ret = __iommu_attach_group(domain, group);
2064 mutex_unlock(&group->mutex);
2065
2066 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002067}
2068EXPORT_SYMBOL_GPL(iommu_attach_group);
2069
2070static int iommu_group_do_detach_device(struct device *dev, void *data)
2071{
2072 struct iommu_domain *domain = data;
2073
Joerg Roedel426a2732015-05-28 18:41:30 +02002074 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002075
2076 return 0;
2077}
2078
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002079static void __iommu_detach_group(struct iommu_domain *domain,
2080 struct iommu_group *group)
2081{
2082 int ret;
2083
2084 if (!group->default_domain) {
2085 __iommu_group_for_each_dev(group, domain,
2086 iommu_group_do_detach_device);
2087 group->domain = NULL;
2088 return;
2089 }
2090
2091 if (group->domain == group->default_domain)
2092 return;
2093
2094 /* Detach by re-attaching to the default domain */
2095 ret = __iommu_group_for_each_dev(group, group->default_domain,
2096 iommu_group_do_attach_device);
2097 if (ret != 0)
2098 WARN_ON(1);
2099 else
2100 group->domain = group->default_domain;
2101}
2102
Alex Williamsond72e31c2012-05-30 14:18:53 -06002103void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2104{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002105 mutex_lock(&group->mutex);
2106 __iommu_detach_group(domain, group);
2107 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002108}
2109EXPORT_SYMBOL_GPL(iommu_detach_group);
2110
Varun Sethibb5547a2013-03-29 01:23:58 +05302111phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002112{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002113 if (unlikely(domain->ops->iova_to_phys == NULL))
2114 return 0;
2115
2116 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002117}
2118EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002119
Alex Williamsonbd139692013-06-17 19:57:34 -06002120static size_t iommu_pgsize(struct iommu_domain *domain,
2121 unsigned long addr_merge, size_t size)
2122{
2123 unsigned int pgsize_idx;
2124 size_t pgsize;
2125
2126 /* Max page size that still fits into 'size' */
2127 pgsize_idx = __fls(size);
2128
2129 /* need to consider alignment requirements ? */
2130 if (likely(addr_merge)) {
2131 /* Max page size allowed by address */
2132 unsigned int align_pgsize_idx = __ffs(addr_merge);
2133 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2134 }
2135
2136 /* build a mask of acceptable page sizes */
2137 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2138
2139 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002140 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06002141
2142 /* make sure we're still sane */
2143 BUG_ON(!pgsize);
2144
2145 /* pick the biggest page */
2146 pgsize_idx = __fls(pgsize);
2147 pgsize = 1UL << pgsize_idx;
2148
2149 return pgsize;
2150}
2151
Tom Murphy781ca2d2019-09-08 09:56:38 -07002152int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2153 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002154{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002155 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002156 unsigned long orig_iova = iova;
2157 unsigned int min_pagesz;
2158 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002159 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002160 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002161
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002162 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002163 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002164 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002165
Joerg Roedela10315e2015-03-26 13:43:06 +01002166 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2167 return -EINVAL;
2168
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002169 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002170 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002171
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002172 /*
2173 * both the virtual address and the physical one, as well as
2174 * the size of the mapping, must be aligned (at least) to the
2175 * size of the smallest page supported by the hardware
2176 */
2177 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002178 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002179 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002180 return -EINVAL;
2181 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002182
Fabio Estevamabedb042013-08-22 10:25:42 -03002183 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002184
2185 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002186 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002187
Fabio Estevamabedb042013-08-22 10:25:42 -03002188 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002189 iova, &paddr, pgsize);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002190 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002191
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002192 if (ret)
2193 break;
2194
2195 iova += pgsize;
2196 paddr += pgsize;
2197 size -= pgsize;
2198 }
2199
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002200 if (ops->iotlb_sync_map)
2201 ops->iotlb_sync_map(domain);
2202
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002203 /* unroll mapping in case something went wrong */
2204 if (ret)
2205 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002206 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002207 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002208
2209 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002210}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002211
2212int iommu_map(struct iommu_domain *domain, unsigned long iova,
2213 phys_addr_t paddr, size_t size, int prot)
2214{
2215 might_sleep();
2216 return __iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2217}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002218EXPORT_SYMBOL_GPL(iommu_map);
2219
Tom Murphy781ca2d2019-09-08 09:56:38 -07002220int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2221 phys_addr_t paddr, size_t size, int prot)
2222{
2223 return __iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2224}
2225EXPORT_SYMBOL_GPL(iommu_map_atomic);
2226
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002227static size_t __iommu_unmap(struct iommu_domain *domain,
2228 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002229 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002230{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002231 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002232 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002233 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002234 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002235
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002236 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002237 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002238 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002239
Joerg Roedela10315e2015-03-26 13:43:06 +01002240 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002241 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002242
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002243 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002244 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002245
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002246 /*
2247 * The virtual address, as well as the size of the mapping, must be
2248 * aligned (at least) to the size of the smallest page supported
2249 * by the hardware
2250 */
2251 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002252 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2253 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002254 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002255 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002256
Joe Perches6197ca82013-06-23 12:29:04 -07002257 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002258
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002259 /*
2260 * Keep iterating until we either unmap 'size' bytes (or more)
2261 * or we hit an area that isn't mapped.
2262 */
2263 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002264 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002265
Will Deacon56f8af52019-07-02 16:44:06 +01002266 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002267 if (!unmapped_page)
2268 break;
2269
Joe Perches6197ca82013-06-23 12:29:04 -07002270 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2271 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002272
2273 iova += unmapped_page;
2274 unmapped += unmapped_page;
2275 }
2276
Shuah Khandb8614d2015-01-16 20:53:17 -07002277 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002278 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002279}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002280
2281size_t iommu_unmap(struct iommu_domain *domain,
2282 unsigned long iova, size_t size)
2283{
Will Deacona7d20dc2019-07-02 16:43:48 +01002284 struct iommu_iotlb_gather iotlb_gather;
2285 size_t ret;
2286
2287 iommu_iotlb_gather_init(&iotlb_gather);
2288 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2289 iommu_tlb_sync(domain, &iotlb_gather);
2290
2291 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002292}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002293EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002294
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002295size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002296 unsigned long iova, size_t size,
2297 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002298{
Will Deacona7d20dc2019-07-02 16:43:48 +01002299 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002300}
2301EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2302
Tom Murphy781ca2d2019-09-08 09:56:38 -07002303size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2304 struct scatterlist *sg, unsigned int nents, int prot,
2305 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002306{
Robin Murphy5d95f402018-10-11 16:56:42 +01002307 size_t len = 0, mapped = 0;
2308 phys_addr_t start;
2309 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002310 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002311
Robin Murphy5d95f402018-10-11 16:56:42 +01002312 while (i <= nents) {
2313 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002314
Robin Murphy5d95f402018-10-11 16:56:42 +01002315 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002316 ret = __iommu_map(domain, iova + mapped, start,
2317 len, prot, gfp);
2318
Robin Murphy5d95f402018-10-11 16:56:42 +01002319 if (ret)
2320 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002321
Robin Murphy5d95f402018-10-11 16:56:42 +01002322 mapped += len;
2323 len = 0;
2324 }
Robin Murphy18f23402014-11-25 17:50:55 +00002325
Robin Murphy5d95f402018-10-11 16:56:42 +01002326 if (len) {
2327 len += sg->length;
2328 } else {
2329 len = sg->length;
2330 start = s_phys;
2331 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002332
Robin Murphy5d95f402018-10-11 16:56:42 +01002333 if (++i < nents)
2334 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002335 }
2336
2337 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002338
2339out_err:
2340 /* undo mappings already done */
2341 iommu_unmap(domain, iova, mapped);
2342
2343 return 0;
2344
Olav Haugan315786e2014-10-25 09:55:16 -07002345}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002346
2347size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2348 struct scatterlist *sg, unsigned int nents, int prot)
2349{
2350 might_sleep();
2351 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2352}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002353EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002354
Tom Murphy781ca2d2019-09-08 09:56:38 -07002355size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2356 struct scatterlist *sg, unsigned int nents, int prot)
2357{
2358 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2359}
2360EXPORT_SYMBOL_GPL(iommu_map_sg_atomic);
2361
Joerg Roedeld7787d52013-01-29 14:26:20 +01002362int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05302363 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01002364{
2365 if (unlikely(domain->ops->domain_window_enable == NULL))
2366 return -ENODEV;
2367
Varun Sethi80f97f02013-03-29 01:24:00 +05302368 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2369 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002370}
2371EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2372
2373void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2374{
2375 if (unlikely(domain->ops->domain_window_disable == NULL))
2376 return;
2377
2378 return domain->ops->domain_window_disable(domain, wnd_nr);
2379}
2380EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2381
Joerg Roedel207c6e32017-04-26 15:39:28 +02002382/**
2383 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2384 * @domain: the iommu domain where the fault has happened
2385 * @dev: the device where the fault has happened
2386 * @iova: the faulting address
2387 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2388 *
2389 * This function should be called by the low-level IOMMU implementations
2390 * whenever IOMMU faults happen, to allow high-level users, that are
2391 * interested in such events, to know about them.
2392 *
2393 * This event may be useful for several possible use cases:
2394 * - mere logging of the event
2395 * - dynamic TLB/PTE loading
2396 * - if restarting of the faulting device is required
2397 *
2398 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2399 * PTE/TLB loading will one day be supported, implementations will be able
2400 * to tell whether it succeeded or not according to this return value).
2401 *
2402 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2403 * (though fault handlers can also return -ENOSYS, in case they want to
2404 * elicit the default behavior of the IOMMU drivers).
2405 */
2406int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2407 unsigned long iova, int flags)
2408{
2409 int ret = -ENOSYS;
2410
2411 /*
2412 * if upper layers showed interest and installed a fault handler,
2413 * invoke it.
2414 */
2415 if (domain->handler)
2416 ret = domain->handler(domain, dev, iova, flags,
2417 domain->handler_token);
2418
2419 trace_io_page_fault(dev, iova, flags);
2420 return ret;
2421}
2422EXPORT_SYMBOL_GPL(report_iommu_fault);
2423
Alex Williamsond72e31c2012-05-30 14:18:53 -06002424static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002425{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002426 iommu_group_kset = kset_create_and_add("iommu_groups",
2427 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002428 BUG_ON(!iommu_group_kset);
2429
Gary R Hookbad614b2018-06-12 16:41:21 -05002430 iommu_debugfs_setup();
2431
Alex Williamsond72e31c2012-05-30 14:18:53 -06002432 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002433}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002434core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002435
2436int iommu_domain_get_attr(struct iommu_domain *domain,
2437 enum iommu_attr attr, void *data)
2438{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002439 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002440 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002441 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002442
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002443 switch (attr) {
2444 case DOMAIN_ATTR_GEOMETRY:
2445 geometry = data;
2446 *geometry = domain->geometry;
2447
2448 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002449 case DOMAIN_ATTR_PAGING:
2450 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002451 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01002452 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002453 default:
2454 if (!domain->ops->domain_get_attr)
2455 return -EINVAL;
2456
2457 ret = domain->ops->domain_get_attr(domain, attr, data);
2458 }
2459
2460 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002461}
2462EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2463
2464int iommu_domain_set_attr(struct iommu_domain *domain,
2465 enum iommu_attr attr, void *data)
2466{
Joerg Roedel69356712013-02-04 14:00:01 +01002467 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002468
Joerg Roedel69356712013-02-04 14:00:01 +01002469 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01002470 default:
2471 if (domain->ops->domain_set_attr == NULL)
2472 return -EINVAL;
2473
2474 ret = domain->ops->domain_set_attr(domain, attr, data);
2475 }
2476
2477 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002478}
2479EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02002480
Eric Augere5b52342017-01-19 20:57:47 +00002481void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002482{
2483 const struct iommu_ops *ops = dev->bus->iommu_ops;
2484
Eric Augere5b52342017-01-19 20:57:47 +00002485 if (ops && ops->get_resv_regions)
2486 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002487}
2488
Eric Augere5b52342017-01-19 20:57:47 +00002489void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002490{
2491 const struct iommu_ops *ops = dev->bus->iommu_ops;
2492
Eric Augere5b52342017-01-19 20:57:47 +00002493 if (ops && ops->put_resv_regions)
2494 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002495}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002496
Thierry Redingf9f69712019-12-18 14:42:01 +01002497/**
2498 * generic_iommu_put_resv_regions - Reserved region driver helper
2499 * @dev: device for which to free reserved regions
2500 * @list: reserved region list for device
2501 *
2502 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2503 * for simple reservations. Memory allocated for each reserved region will be
2504 * freed. If an IOMMU driver allocates additional resources per region, it is
2505 * going to have to implement a custom callback.
2506 */
2507void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2508{
2509 struct iommu_resv_region *entry, *next;
2510
2511 list_for_each_entry_safe(entry, next, list, list)
2512 kfree(entry);
2513}
2514EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2515
Eric Auger2b20cbb2017-01-19 20:57:49 +00002516struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002517 size_t length, int prot,
2518 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002519{
2520 struct iommu_resv_region *region;
2521
2522 region = kzalloc(sizeof(*region), GFP_KERNEL);
2523 if (!region)
2524 return NULL;
2525
2526 INIT_LIST_HEAD(&region->list);
2527 region->start = start;
2528 region->length = length;
2529 region->prot = prot;
2530 region->type = type;
2531 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002532}
Will Deacona7ba5c32019-12-19 12:03:37 +00002533EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002534
Lu Baolu7423e012019-05-25 13:41:22 +08002535static int
2536request_default_domain_for_dev(struct device *dev, unsigned long type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002537{
Lu Baolu7423e012019-05-25 13:41:22 +08002538 struct iommu_domain *domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002539 struct iommu_group *group;
2540 int ret;
2541
2542 /* Device must already be in a group before calling this function */
Lu Baolu57274ea2019-05-21 15:27:35 +08002543 group = iommu_group_get(dev);
2544 if (!group)
2545 return -EINVAL;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002546
2547 mutex_lock(&group->mutex);
2548
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002549 ret = 0;
Lu Baolu7423e012019-05-25 13:41:22 +08002550 if (group->default_domain && group->default_domain->type == type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002551 goto out;
2552
2553 /* Don't change mappings of existing devices */
2554 ret = -EBUSY;
2555 if (iommu_group_device_count(group) != 1)
2556 goto out;
2557
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002558 ret = -ENOMEM;
Lu Baolu7423e012019-05-25 13:41:22 +08002559 domain = __iommu_domain_alloc(dev->bus, type);
2560 if (!domain)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002561 goto out;
2562
2563 /* Attach the device to the domain */
Lu Baolu7423e012019-05-25 13:41:22 +08002564 ret = __iommu_attach_group(domain, group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002565 if (ret) {
Lu Baolu7423e012019-05-25 13:41:22 +08002566 iommu_domain_free(domain);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002567 goto out;
2568 }
2569
Tom Murphyd127bc92019-08-26 05:48:21 +01002570 /* Make the domain the default for this group */
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002571 if (group->default_domain)
2572 iommu_domain_free(group->default_domain);
Lu Baolu7423e012019-05-25 13:41:22 +08002573 group->default_domain = domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002574
Joerg Roedelce574c22020-04-29 15:36:50 +02002575 iommu_create_device_direct_mappings(group, dev);
Jerry Snitselaard3602112019-12-10 11:56:06 -07002576
Lu Baolu7423e012019-05-25 13:41:22 +08002577 dev_info(dev, "Using iommu %s mapping\n",
2578 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002579
2580 ret = 0;
2581out:
2582 mutex_unlock(&group->mutex);
2583 iommu_group_put(group);
2584
2585 return ret;
2586}
Robin Murphy57f98d22016-09-13 10:54:14 +01002587
Lu Baolu7423e012019-05-25 13:41:22 +08002588/* Request that a device is direct mapped by the IOMMU */
2589int iommu_request_dm_for_dev(struct device *dev)
2590{
2591 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2592}
2593
2594/* Request that a device can't be direct mapped by the IOMMU */
2595int iommu_request_dma_domain_for_dev(struct device *dev)
2596{
2597 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2598}
2599
Joerg Roedel8a699612019-08-19 15:22:47 +02002600void iommu_set_default_passthrough(bool cmd_line)
2601{
2602 if (cmd_line)
2603 iommu_set_cmd_line_dma_api();
2604
2605 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2606}
2607
2608void iommu_set_default_translated(bool cmd_line)
2609{
2610 if (cmd_line)
2611 iommu_set_cmd_line_dma_api();
2612
2613 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2614}
2615
2616bool iommu_default_passthrough(void)
2617{
2618 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2619}
2620EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2621
Joerg Roedel534766d2017-01-31 16:58:42 +01002622const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002623{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002624 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002625 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002626
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002627 spin_lock(&iommu_device_lock);
2628 list_for_each_entry(iommu, &iommu_device_list, list)
2629 if (iommu->fwnode == fwnode) {
2630 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002631 break;
2632 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002633 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002634 return ops;
2635}
2636
Robin Murphy57f98d22016-09-13 10:54:14 +01002637int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2638 const struct iommu_ops *ops)
2639{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002640 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002641
2642 if (fwspec)
2643 return ops == fwspec->ops ? 0 : -EINVAL;
2644
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002645 if (!dev_iommu_get(dev))
2646 return -ENOMEM;
2647
Robin Murphy098accf2020-02-13 14:00:21 +00002648 /* Preallocate for the overwhelmingly common case of 1 ID */
2649 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002650 if (!fwspec)
2651 return -ENOMEM;
2652
2653 of_node_get(to_of_node(iommu_fwnode));
2654 fwspec->iommu_fwnode = iommu_fwnode;
2655 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002656 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002657 return 0;
2658}
2659EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2660
2661void iommu_fwspec_free(struct device *dev)
2662{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002663 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002664
2665 if (fwspec) {
2666 fwnode_handle_put(fwspec->iommu_fwnode);
2667 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002668 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002669 }
2670}
2671EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2672
2673int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2674{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002675 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002676 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002677
2678 if (!fwspec)
2679 return -EINVAL;
2680
Robin Murphy098accf2020-02-13 14:00:21 +00002681 new_num = fwspec->num_ids + num_ids;
2682 if (new_num > 1) {
2683 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2684 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002685 if (!fwspec)
2686 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002687
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002688 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002689 }
2690
2691 for (i = 0; i < num_ids; i++)
2692 fwspec->ids[fwspec->num_ids + i] = ids[i];
2693
Robin Murphy098accf2020-02-13 14:00:21 +00002694 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002695 return 0;
2696}
2697EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002698
2699/*
2700 * Per device IOMMU features.
2701 */
2702bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2703{
2704 const struct iommu_ops *ops = dev->bus->iommu_ops;
2705
2706 if (ops && ops->dev_has_feat)
2707 return ops->dev_has_feat(dev, feat);
2708
2709 return false;
2710}
2711EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2712
2713int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2714{
2715 const struct iommu_ops *ops = dev->bus->iommu_ops;
2716
2717 if (ops && ops->dev_enable_feat)
2718 return ops->dev_enable_feat(dev, feat);
2719
2720 return -ENODEV;
2721}
2722EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2723
2724/*
2725 * The device drivers should do the necessary cleanups before calling this.
2726 * For example, before disabling the aux-domain feature, the device driver
2727 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2728 */
2729int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2730{
2731 const struct iommu_ops *ops = dev->bus->iommu_ops;
2732
2733 if (ops && ops->dev_disable_feat)
2734 return ops->dev_disable_feat(dev, feat);
2735
2736 return -EBUSY;
2737}
2738EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2739
2740bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2741{
2742 const struct iommu_ops *ops = dev->bus->iommu_ops;
2743
2744 if (ops && ops->dev_feat_enabled)
2745 return ops->dev_feat_enabled(dev, feat);
2746
2747 return false;
2748}
2749EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2750
2751/*
2752 * Aux-domain specific attach/detach.
2753 *
2754 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2755 * true. Also, as long as domains are attached to a device through this
2756 * interface, any tries to call iommu_attach_device() should fail
2757 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2758 * This should make us safe against a device being attached to a guest as a
2759 * whole while there are still pasid users on it (aux and sva).
2760 */
2761int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2762{
2763 int ret = -ENODEV;
2764
2765 if (domain->ops->aux_attach_dev)
2766 ret = domain->ops->aux_attach_dev(domain, dev);
2767
2768 if (!ret)
2769 trace_attach_device_to_domain(dev);
2770
2771 return ret;
2772}
2773EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2774
2775void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2776{
2777 if (domain->ops->aux_detach_dev) {
2778 domain->ops->aux_detach_dev(domain, dev);
2779 trace_detach_device_from_domain(dev);
2780 }
2781}
2782EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2783
2784int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2785{
2786 int ret = -ENODEV;
2787
2788 if (domain->ops->aux_get_pasid)
2789 ret = domain->ops->aux_get_pasid(domain, dev);
2790
2791 return ret;
2792}
2793EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002794
2795/**
2796 * iommu_sva_bind_device() - Bind a process address space to a device
2797 * @dev: the device
2798 * @mm: the mm to bind, caller must hold a reference to it
2799 *
2800 * Create a bond between device and address space, allowing the device to access
2801 * the mm using the returned PASID. If a bond already exists between @device and
2802 * @mm, it is returned and an additional reference is taken. Caller must call
2803 * iommu_sva_unbind_device() to release each reference.
2804 *
2805 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2806 * initialize the required SVA features.
2807 *
2808 * On error, returns an ERR_PTR value.
2809 */
2810struct iommu_sva *
2811iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2812{
2813 struct iommu_group *group;
2814 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2815 const struct iommu_ops *ops = dev->bus->iommu_ops;
2816
2817 if (!ops || !ops->sva_bind)
2818 return ERR_PTR(-ENODEV);
2819
2820 group = iommu_group_get(dev);
2821 if (!group)
2822 return ERR_PTR(-ENODEV);
2823
2824 /* Ensure device count and domain don't change while we're binding */
2825 mutex_lock(&group->mutex);
2826
2827 /*
2828 * To keep things simple, SVA currently doesn't support IOMMU groups
2829 * with more than one device. Existing SVA-capable systems are not
2830 * affected by the problems that required IOMMU groups (lack of ACS
2831 * isolation, device ID aliasing and other hardware issues).
2832 */
2833 if (iommu_group_device_count(group) != 1)
2834 goto out_unlock;
2835
2836 handle = ops->sva_bind(dev, mm, drvdata);
2837
2838out_unlock:
2839 mutex_unlock(&group->mutex);
2840 iommu_group_put(group);
2841
2842 return handle;
2843}
2844EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2845
2846/**
2847 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2848 * @handle: the handle returned by iommu_sva_bind_device()
2849 *
2850 * Put reference to a bond between device and address space. The device should
2851 * not be issuing any more transaction for this PASID. All outstanding page
2852 * requests for this PASID must have been flushed to the IOMMU.
2853 *
2854 * Returns 0 on success, or an error value
2855 */
2856void iommu_sva_unbind_device(struct iommu_sva *handle)
2857{
2858 struct iommu_group *group;
2859 struct device *dev = handle->dev;
2860 const struct iommu_ops *ops = dev->bus->iommu_ops;
2861
2862 if (!ops || !ops->sva_unbind)
2863 return;
2864
2865 group = iommu_group_get(dev);
2866 if (!group)
2867 return;
2868
2869 mutex_lock(&group->mutex);
2870 ops->sva_unbind(handle);
2871 mutex_unlock(&group->mutex);
2872
2873 iommu_group_put(group);
2874}
2875EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2876
2877int iommu_sva_set_ops(struct iommu_sva *handle,
2878 const struct iommu_sva_ops *sva_ops)
2879{
2880 if (handle->ops && handle->ops != sva_ops)
2881 return -EEXIST;
2882
2883 handle->ops = sva_ops;
2884 return 0;
2885}
2886EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2887
2888int iommu_sva_get_pasid(struct iommu_sva *handle)
2889{
2890 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2891
2892 if (!ops || !ops->sva_get_pasid)
2893 return IOMMU_PASID_INVALID;
2894
2895 return ops->sva_get_pasid(handle);
2896}
2897EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);