blob: 7f99e5ae432c689ce0a0710796edaaa8bcd5c5ca [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 Roedel6e1aa202020-04-29 15:36:46 +020094
Alex Williamsond72e31c2012-05-30 14:18:53 -060095#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
96struct iommu_group_attribute iommu_group_attr_##_name = \
97 __ATTR(_name, _mode, _show, _store)
98
99#define to_iommu_group_attr(_attr) \
100 container_of(_attr, struct iommu_group_attribute, attr)
101#define to_iommu_group(_kobj) \
102 container_of(_kobj, struct iommu_group, kobj)
103
Joerg Roedelb0119e82017-02-01 13:23:08 +0100104static LIST_HEAD(iommu_device_list);
105static DEFINE_SPINLOCK(iommu_device_lock);
106
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200107/*
108 * Use a function instead of an array here because the domain-type is a
109 * bit-field, so an array would waste memory.
110 */
111static const char *iommu_domain_type_str(unsigned int t)
112{
113 switch (t) {
114 case IOMMU_DOMAIN_BLOCKED:
115 return "Blocked";
116 case IOMMU_DOMAIN_IDENTITY:
117 return "Passthrough";
118 case IOMMU_DOMAIN_UNMANAGED:
119 return "Unmanaged";
120 case IOMMU_DOMAIN_DMA:
121 return "Translated";
122 default:
123 return "Unknown";
124 }
125}
126
127static int __init iommu_subsys_init(void)
128{
Joerg Roedel22bb1822019-08-19 15:22:54 +0200129 bool cmd_line = iommu_cmd_line_dma_api();
130
131 if (!cmd_line) {
132 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
133 iommu_set_default_passthrough(false);
134 else
135 iommu_set_default_translated(false);
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200136
Joerg Roedel2896ba42019-09-03 15:15:44 +0200137 if (iommu_default_passthrough() && mem_encrypt_active()) {
138 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
Joerg Roedel2cc13bb2019-08-19 15:22:55 +0200139 iommu_set_default_translated(false);
140 }
Joerg Roedel22bb1822019-08-19 15:22:54 +0200141 }
142
143 pr_info("Default domain type: %s %s\n",
144 iommu_domain_type_str(iommu_def_domain_type),
145 cmd_line ? "(set via kernel command line)" : "");
Joerg Roedel5fa9e7c2019-08-19 15:22:53 +0200146
147 return 0;
148}
149subsys_initcall(iommu_subsys_init);
150
Joerg Roedelb0119e82017-02-01 13:23:08 +0100151int iommu_device_register(struct iommu_device *iommu)
152{
153 spin_lock(&iommu_device_lock);
154 list_add_tail(&iommu->list, &iommu_device_list);
155 spin_unlock(&iommu_device_lock);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100156 return 0;
157}
Will Deacona7ba5c32019-12-19 12:03:37 +0000158EXPORT_SYMBOL_GPL(iommu_device_register);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100159
160void iommu_device_unregister(struct iommu_device *iommu)
161{
162 spin_lock(&iommu_device_lock);
163 list_del(&iommu->list);
164 spin_unlock(&iommu_device_lock);
165}
Will Deacona7ba5c32019-12-19 12:03:37 +0000166EXPORT_SYMBOL_GPL(iommu_device_unregister);
Joerg Roedelb0119e82017-02-01 13:23:08 +0100167
Joerg Roedel045a7042020-03-26 16:08:30 +0100168static struct dev_iommu *dev_iommu_get(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100169{
Joerg Roedel045a7042020-03-26 16:08:30 +0100170 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +0100171
172 if (param)
173 return param;
174
175 param = kzalloc(sizeof(*param), GFP_KERNEL);
176 if (!param)
177 return NULL;
178
179 mutex_init(&param->lock);
Joerg Roedel045a7042020-03-26 16:08:30 +0100180 dev->iommu = param;
Jacob Pan0c830e62019-06-03 15:57:48 +0100181 return param;
182}
183
Joerg Roedel045a7042020-03-26 16:08:30 +0100184static void dev_iommu_free(struct device *dev)
Jacob Pan0c830e62019-06-03 15:57:48 +0100185{
Joerg Roedel045a7042020-03-26 16:08:30 +0100186 kfree(dev->iommu);
187 dev->iommu = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100188}
189
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200190static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200191{
192 const struct iommu_ops *ops = dev->bus->iommu_ops;
193 struct iommu_device *iommu_dev;
194 struct iommu_group *group;
195 int ret;
196
197 iommu_dev = ops->probe_device(dev);
198 if (IS_ERR(iommu_dev))
199 return PTR_ERR(iommu_dev);
200
201 dev->iommu->iommu_dev = iommu_dev;
202
203 group = iommu_group_get_for_dev(dev);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +0200204 if (IS_ERR(group)) {
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200205 ret = PTR_ERR(group);
206 goto out_release;
207 }
208 iommu_group_put(group);
209
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200210 if (group_list && !group->default_domain && list_empty(&group->entry))
211 list_add_tail(&group->entry, group_list);
212
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200213 iommu_device_link(iommu_dev, dev);
214
215 return 0;
216
217out_release:
218 ops->release_device(dev);
219
220 return ret;
221}
222
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200223int iommu_probe_device(struct device *dev)
Joerg Roedelcf193882020-04-29 15:36:48 +0200224{
225 const struct iommu_ops *ops = dev->bus->iommu_ops;
226 struct iommu_group *group;
227 int ret;
228
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200229 if (!dev_iommu_get(dev))
230 return -ENOMEM;
231
232 if (!try_module_get(ops->owner)) {
233 ret = -EINVAL;
234 goto err_out;
235 }
236
Joerg Roedelcf193882020-04-29 15:36:48 +0200237 ret = __iommu_probe_device(dev, NULL);
238 if (ret)
239 goto err_out;
240
241 /*
242 * Try to allocate a default domain - needs support from the
243 * IOMMU driver. There are still some drivers which don't
244 * support default domains, so the return value is not yet
245 * checked.
246 */
247 iommu_alloc_default_domain(dev);
248
249 group = iommu_group_get(dev);
250 if (!group)
251 goto err_release;
252
253 if (group->default_domain)
254 ret = __iommu_attach_device(group->default_domain, dev);
255
Joerg Roedelce574c22020-04-29 15:36:50 +0200256 iommu_create_device_direct_mappings(group, dev);
257
Joerg Roedelcf193882020-04-29 15:36:48 +0200258 iommu_group_put(group);
259
260 if (ret)
261 goto err_release;
262
263 if (ops->probe_finalize)
264 ops->probe_finalize(dev);
265
266 return 0;
267
268err_release:
269 iommu_release_device(dev);
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200270
Joerg Roedelcf193882020-04-29 15:36:48 +0200271err_out:
272 return ret;
273
274}
275
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100276void iommu_release_device(struct device *dev)
277{
278 const struct iommu_ops *ops = dev->bus->iommu_ops;
279
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200280 if (!dev->iommu)
281 return;
282
Joerg Roedel3eeeb452020-04-29 15:37:10 +0200283 iommu_device_unlink(dev->iommu->iommu_dev, dev);
284 iommu_group_remove_device(dev);
285
286 ops->release_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100287
Joerg Roedela6a4c7e2020-04-29 15:36:45 +0200288 module_put(ops->owner);
289 dev_iommu_free(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100290}
291
Will Deaconfccb4e32017-01-05 18:38:26 +0000292static int __init iommu_set_def_domain_type(char *str)
293{
294 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300295 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000296
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300297 ret = kstrtobool(str, &pt);
298 if (ret)
299 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000300
Joerg Roedeladab0b02019-08-19 15:22:48 +0200301 if (pt)
302 iommu_set_default_passthrough(true);
303 else
304 iommu_set_default_translated(true);
Joerg Roedelfaf14982019-08-19 15:22:46 +0200305
Will Deaconfccb4e32017-01-05 18:38:26 +0000306 return 0;
307}
308early_param("iommu.passthrough", iommu_set_def_domain_type);
309
Zhen Lei68a6efe2018-09-20 17:10:23 +0100310static int __init iommu_dma_setup(char *str)
311{
312 return kstrtobool(str, &iommu_dma_strict);
313}
314early_param("iommu.strict", iommu_dma_setup);
315
Alex Williamsond72e31c2012-05-30 14:18:53 -0600316static ssize_t iommu_group_attr_show(struct kobject *kobj,
317 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400318{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600319 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
320 struct iommu_group *group = to_iommu_group(kobj);
321 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400322
Alex Williamsond72e31c2012-05-30 14:18:53 -0600323 if (attr->show)
324 ret = attr->show(group, buf);
325 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400326}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600327
328static ssize_t iommu_group_attr_store(struct kobject *kobj,
329 struct attribute *__attr,
330 const char *buf, size_t count)
331{
332 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
333 struct iommu_group *group = to_iommu_group(kobj);
334 ssize_t ret = -EIO;
335
336 if (attr->store)
337 ret = attr->store(group, buf, count);
338 return ret;
339}
340
341static const struct sysfs_ops iommu_group_sysfs_ops = {
342 .show = iommu_group_attr_show,
343 .store = iommu_group_attr_store,
344};
345
346static int iommu_group_create_file(struct iommu_group *group,
347 struct iommu_group_attribute *attr)
348{
349 return sysfs_create_file(&group->kobj, &attr->attr);
350}
351
352static void iommu_group_remove_file(struct iommu_group *group,
353 struct iommu_group_attribute *attr)
354{
355 sysfs_remove_file(&group->kobj, &attr->attr);
356}
357
358static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
359{
360 return sprintf(buf, "%s\n", group->name);
361}
362
Eric Auger6c65fb32017-01-19 20:57:51 +0000363/**
364 * iommu_insert_resv_region - Insert a new region in the
365 * list of reserved regions.
366 * @new: new region to insert
367 * @regions: list of regions
368 *
Eric Auger4dbd2582019-08-21 14:09:40 +0200369 * Elements are sorted by start address and overlapping segments
370 * of the same type are merged.
Eric Auger6c65fb32017-01-19 20:57:51 +0000371 */
Eric Auger4dbd2582019-08-21 14:09:40 +0200372int iommu_insert_resv_region(struct iommu_resv_region *new,
373 struct list_head *regions)
Eric Auger6c65fb32017-01-19 20:57:51 +0000374{
Eric Auger4dbd2582019-08-21 14:09:40 +0200375 struct iommu_resv_region *iter, *tmp, *nr, *top;
376 LIST_HEAD(stack);
Eric Auger6c65fb32017-01-19 20:57:51 +0000377
Eric Auger4dbd2582019-08-21 14:09:40 +0200378 nr = iommu_alloc_resv_region(new->start, new->length,
379 new->prot, new->type);
380 if (!nr)
Eric Auger6c65fb32017-01-19 20:57:51 +0000381 return -ENOMEM;
382
Eric Auger4dbd2582019-08-21 14:09:40 +0200383 /* First add the new element based on start address sorting */
384 list_for_each_entry(iter, regions, list) {
385 if (nr->start < iter->start ||
386 (nr->start == iter->start && nr->type <= iter->type))
387 break;
388 }
389 list_add_tail(&nr->list, &iter->list);
390
391 /* Merge overlapping segments of type nr->type in @regions, if any */
392 list_for_each_entry_safe(iter, tmp, regions, list) {
393 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
394
Eric Auger4c80ba32019-11-26 18:54:13 +0100395 /* no merge needed on elements of different types than @new */
396 if (iter->type != new->type) {
Eric Auger4dbd2582019-08-21 14:09:40 +0200397 list_move_tail(&iter->list, &stack);
398 continue;
399 }
400
401 /* look for the last stack element of same type as @iter */
402 list_for_each_entry_reverse(top, &stack, list)
403 if (top->type == iter->type)
404 goto check_overlap;
405
406 list_move_tail(&iter->list, &stack);
407 continue;
408
409check_overlap:
410 top_end = top->start + top->length - 1;
411
412 if (iter->start > top_end + 1) {
413 list_move_tail(&iter->list, &stack);
414 } else {
415 top->length = max(top_end, iter_end) - top->start + 1;
416 list_del(&iter->list);
417 kfree(iter);
418 }
419 }
420 list_splice(&stack, regions);
Eric Auger6c65fb32017-01-19 20:57:51 +0000421 return 0;
422}
423
424static int
425iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
426 struct list_head *group_resv_regions)
427{
428 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100429 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000430
431 list_for_each_entry(entry, dev_resv_regions, list) {
432 ret = iommu_insert_resv_region(entry, group_resv_regions);
433 if (ret)
434 break;
435 }
436 return ret;
437}
438
439int iommu_get_group_resv_regions(struct iommu_group *group,
440 struct list_head *head)
441{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100442 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000443 int ret = 0;
444
445 mutex_lock(&group->mutex);
446 list_for_each_entry(device, &group->devices, list) {
447 struct list_head dev_resv_regions;
448
449 INIT_LIST_HEAD(&dev_resv_regions);
450 iommu_get_resv_regions(device->dev, &dev_resv_regions);
451 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
452 iommu_put_resv_regions(device->dev, &dev_resv_regions);
453 if (ret)
454 break;
455 }
456 mutex_unlock(&group->mutex);
457 return ret;
458}
459EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
460
Eric Augerbc7d12b92017-01-19 20:57:52 +0000461static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
462 char *buf)
463{
464 struct iommu_resv_region *region, *next;
465 struct list_head group_resv_regions;
466 char *str = buf;
467
468 INIT_LIST_HEAD(&group_resv_regions);
469 iommu_get_group_resv_regions(group, &group_resv_regions);
470
471 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
472 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
473 (long long int)region->start,
474 (long long int)(region->start +
475 region->length - 1),
476 iommu_group_resv_type_string[region->type]);
477 kfree(region);
478 }
479
480 return (str - buf);
481}
482
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700483static ssize_t iommu_group_show_type(struct iommu_group *group,
484 char *buf)
485{
486 char *type = "unknown\n";
487
488 if (group->default_domain) {
489 switch (group->default_domain->type) {
490 case IOMMU_DOMAIN_BLOCKED:
491 type = "blocked\n";
492 break;
493 case IOMMU_DOMAIN_IDENTITY:
494 type = "identity\n";
495 break;
496 case IOMMU_DOMAIN_UNMANAGED:
497 type = "unmanaged\n";
498 break;
499 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800500 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700501 break;
502 }
503 }
504 strcpy(buf, type);
505
506 return strlen(type);
507}
508
Alex Williamsond72e31c2012-05-30 14:18:53 -0600509static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
510
Eric Augerbc7d12b92017-01-19 20:57:52 +0000511static IOMMU_GROUP_ATTR(reserved_regions, 0444,
512 iommu_group_show_resv_regions, NULL);
513
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700514static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
515
Alex Williamsond72e31c2012-05-30 14:18:53 -0600516static void iommu_group_release(struct kobject *kobj)
517{
518 struct iommu_group *group = to_iommu_group(kobj);
519
Joerg Roedel269aa802015-05-28 18:41:25 +0200520 pr_debug("Releasing group %d\n", group->id);
521
Alex Williamsond72e31c2012-05-30 14:18:53 -0600522 if (group->iommu_data_release)
523 group->iommu_data_release(group->iommu_data);
524
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200525 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600526
Joerg Roedel53723dc2015-05-28 18:41:29 +0200527 if (group->default_domain)
528 iommu_domain_free(group->default_domain);
529
Alex Williamsond72e31c2012-05-30 14:18:53 -0600530 kfree(group->name);
531 kfree(group);
532}
533
534static struct kobj_type iommu_group_ktype = {
535 .sysfs_ops = &iommu_group_sysfs_ops,
536 .release = iommu_group_release,
537};
538
539/**
540 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600541 *
542 * This function is called by an iommu driver to allocate a new iommu
543 * group. The iommu group represents the minimum granularity of the iommu.
544 * Upon successful return, the caller holds a reference to the supplied
545 * group in order to hold the group until devices are added. Use
546 * iommu_group_put() to release this extra reference count, allowing the
547 * group to be automatically reclaimed once it has no devices or external
548 * references.
549 */
550struct iommu_group *iommu_group_alloc(void)
551{
552 struct iommu_group *group;
553 int ret;
554
555 group = kzalloc(sizeof(*group), GFP_KERNEL);
556 if (!group)
557 return ERR_PTR(-ENOMEM);
558
559 group->kobj.kset = iommu_group_kset;
560 mutex_init(&group->mutex);
561 INIT_LIST_HEAD(&group->devices);
Joerg Roedel41df6dc2020-04-29 15:36:47 +0200562 INIT_LIST_HEAD(&group->entry);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600563 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
564
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200565 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
566 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600567 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200568 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600569 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200570 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600571
572 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
573 NULL, "%d", group->id);
574 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200575 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600576 kfree(group);
577 return ERR_PTR(ret);
578 }
579
580 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
581 if (!group->devices_kobj) {
582 kobject_put(&group->kobj); /* triggers .release & free */
583 return ERR_PTR(-ENOMEM);
584 }
585
586 /*
587 * The devices_kobj holds a reference on the group kobject, so
588 * as long as that exists so will the group. We can therefore
589 * use the devices_kobj for reference counting.
590 */
591 kobject_put(&group->kobj);
592
Eric Augerbc7d12b92017-01-19 20:57:52 +0000593 ret = iommu_group_create_file(group,
594 &iommu_group_attr_reserved_regions);
595 if (ret)
596 return ERR_PTR(ret);
597
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700598 ret = iommu_group_create_file(group, &iommu_group_attr_type);
599 if (ret)
600 return ERR_PTR(ret);
601
Joerg Roedel269aa802015-05-28 18:41:25 +0200602 pr_debug("Allocated group %d\n", group->id);
603
Alex Williamsond72e31c2012-05-30 14:18:53 -0600604 return group;
605}
606EXPORT_SYMBOL_GPL(iommu_group_alloc);
607
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100608struct iommu_group *iommu_group_get_by_id(int id)
609{
610 struct kobject *group_kobj;
611 struct iommu_group *group;
612 const char *name;
613
614 if (!iommu_group_kset)
615 return NULL;
616
617 name = kasprintf(GFP_KERNEL, "%d", id);
618 if (!name)
619 return NULL;
620
621 group_kobj = kset_find_obj(iommu_group_kset, name);
622 kfree(name);
623
624 if (!group_kobj)
625 return NULL;
626
627 group = container_of(group_kobj, struct iommu_group, kobj);
628 BUG_ON(group->id != id);
629
630 kobject_get(group->devices_kobj);
631 kobject_put(&group->kobj);
632
633 return group;
634}
635EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
636
Alex Williamsond72e31c2012-05-30 14:18:53 -0600637/**
638 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
639 * @group: the group
640 *
641 * iommu drivers can store data in the group for use when doing iommu
642 * operations. This function provides a way to retrieve it. Caller
643 * should hold a group reference.
644 */
645void *iommu_group_get_iommudata(struct iommu_group *group)
646{
647 return group->iommu_data;
648}
649EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
650
651/**
652 * iommu_group_set_iommudata - set iommu_data for a group
653 * @group: the group
654 * @iommu_data: new data
655 * @release: release function for iommu_data
656 *
657 * iommu drivers can store data in the group for use when doing iommu
658 * operations. This function provides a way to set the data after
659 * the group has been allocated. Caller should hold a group reference.
660 */
661void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
662 void (*release)(void *iommu_data))
663{
664 group->iommu_data = iommu_data;
665 group->iommu_data_release = release;
666}
667EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
668
669/**
670 * iommu_group_set_name - set name for a group
671 * @group: the group
672 * @name: name
673 *
674 * Allow iommu driver to set a name for a group. When set it will
675 * appear in a name attribute file under the group in sysfs.
676 */
677int iommu_group_set_name(struct iommu_group *group, const char *name)
678{
679 int ret;
680
681 if (group->name) {
682 iommu_group_remove_file(group, &iommu_group_attr_name);
683 kfree(group->name);
684 group->name = NULL;
685 if (!name)
686 return 0;
687 }
688
689 group->name = kstrdup(name, GFP_KERNEL);
690 if (!group->name)
691 return -ENOMEM;
692
693 ret = iommu_group_create_file(group, &iommu_group_attr_name);
694 if (ret) {
695 kfree(group->name);
696 group->name = NULL;
697 return ret;
698 }
699
700 return 0;
701}
702EXPORT_SYMBOL_GPL(iommu_group_set_name);
703
Joerg Roedelce574c22020-04-29 15:36:50 +0200704static int iommu_create_device_direct_mappings(struct iommu_group *group,
705 struct device *dev)
Joerg Roedelbeed2822015-05-28 18:41:34 +0200706{
707 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000708 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200709 struct list_head mappings;
710 unsigned long pg_size;
711 int ret = 0;
712
713 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
714 return 0;
715
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100716 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200717
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100718 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200719 INIT_LIST_HEAD(&mappings);
720
Eric Augere5b52342017-01-19 20:57:47 +0000721 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200722
723 /* We need to consider overlapping regions for different devices */
724 list_for_each_entry(entry, &mappings, list) {
725 dma_addr_t start, end, addr;
726
Eric Augere5b52342017-01-19 20:57:47 +0000727 if (domain->ops->apply_resv_region)
728 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200729
Joerg Roedelbeed2822015-05-28 18:41:34 +0200730 start = ALIGN(entry->start, pg_size);
731 end = ALIGN(entry->start + entry->length, pg_size);
732
Eric Augeradfd3732019-06-03 08:53:35 +0200733 if (entry->type != IOMMU_RESV_DIRECT &&
734 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000735 continue;
736
Joerg Roedelbeed2822015-05-28 18:41:34 +0200737 for (addr = start; addr < end; addr += pg_size) {
738 phys_addr_t phys_addr;
739
740 phys_addr = iommu_iova_to_phys(domain, addr);
741 if (phys_addr)
742 continue;
743
744 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
745 if (ret)
746 goto out;
747 }
748
749 }
750
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200751 iommu_flush_tlb_all(domain);
752
Joerg Roedelbeed2822015-05-28 18:41:34 +0200753out:
Eric Augere5b52342017-01-19 20:57:47 +0000754 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200755
756 return ret;
757}
758
Alex Williamsond72e31c2012-05-30 14:18:53 -0600759/**
760 * iommu_group_add_device - add a device to an iommu group
761 * @group: the group into which to add the device (reference should be held)
762 * @dev: the device
763 *
764 * This function is called by an iommu driver to add a device into a
765 * group. Adding a device increments the group reference count.
766 */
767int iommu_group_add_device(struct iommu_group *group, struct device *dev)
768{
769 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100770 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600771
772 device = kzalloc(sizeof(*device), GFP_KERNEL);
773 if (!device)
774 return -ENOMEM;
775
776 device->dev = dev;
777
778 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000779 if (ret)
780 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600781
782 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
783rename:
784 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000785 ret = -ENOMEM;
786 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600787 }
788
789 ret = sysfs_create_link_nowarn(group->devices_kobj,
790 &dev->kobj, device->name);
791 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600792 if (ret == -EEXIST && i >= 0) {
793 /*
794 * Account for the slim chance of collision
795 * and append an instance to the name.
796 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000797 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600798 device->name = kasprintf(GFP_KERNEL, "%s.%d",
799 kobject_name(&dev->kobj), i++);
800 goto rename;
801 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000802 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600803 }
804
805 kobject_get(group->devices_kobj);
806
807 dev->iommu_group = group;
808
809 mutex_lock(&group->mutex);
810 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200811 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000812 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600813 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000814 if (ret)
815 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600816
817 /* Notify any listeners about change to group. */
818 blocking_notifier_call_chain(&group->notifier,
819 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600820
821 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200822
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600823 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200824
Alex Williamsond72e31c2012-05-30 14:18:53 -0600825 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000826
827err_put_group:
828 mutex_lock(&group->mutex);
829 list_del(&device->list);
830 mutex_unlock(&group->mutex);
831 dev->iommu_group = NULL;
832 kobject_put(group->devices_kobj);
Jon Derrick7d4e6cc2019-12-31 13:24:19 -0700833 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphy797a8b42017-01-16 12:58:07 +0000834err_free_name:
835 kfree(device->name);
836err_remove_link:
837 sysfs_remove_link(&dev->kobj, "iommu_group");
838err_free_device:
839 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600840 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000841 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600842}
843EXPORT_SYMBOL_GPL(iommu_group_add_device);
844
845/**
846 * iommu_group_remove_device - remove a device from it's current group
847 * @dev: device to be removed
848 *
849 * This function is called by an iommu driver to remove the device from
850 * it's current group. This decrements the iommu group reference count.
851 */
852void iommu_group_remove_device(struct device *dev)
853{
854 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100855 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600856
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600857 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200858
Alex Williamsond72e31c2012-05-30 14:18:53 -0600859 /* Pre-notify listeners that a device is being removed. */
860 blocking_notifier_call_chain(&group->notifier,
861 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
862
863 mutex_lock(&group->mutex);
864 list_for_each_entry(tmp_device, &group->devices, list) {
865 if (tmp_device->dev == dev) {
866 device = tmp_device;
867 list_del(&device->list);
868 break;
869 }
870 }
871 mutex_unlock(&group->mutex);
872
873 if (!device)
874 return;
875
876 sysfs_remove_link(group->devices_kobj, device->name);
877 sysfs_remove_link(&dev->kobj, "iommu_group");
878
Shuah Khan2e757082013-08-15 11:59:25 -0600879 trace_remove_device_from_group(group->id, dev);
880
Alex Williamsond72e31c2012-05-30 14:18:53 -0600881 kfree(device->name);
882 kfree(device);
883 dev->iommu_group = NULL;
884 kobject_put(group->devices_kobj);
885}
886EXPORT_SYMBOL_GPL(iommu_group_remove_device);
887
Joerg Roedel426a2732015-05-28 18:41:30 +0200888static int iommu_group_device_count(struct iommu_group *group)
889{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100890 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200891 int ret = 0;
892
893 list_for_each_entry(entry, &group->devices, list)
894 ret++;
895
896 return ret;
897}
898
Alex Williamsond72e31c2012-05-30 14:18:53 -0600899/**
900 * iommu_group_for_each_dev - iterate over each device in the group
901 * @group: the group
902 * @data: caller opaque data to be passed to callback function
903 * @fn: caller supplied callback function
904 *
905 * This function is called by group users to iterate over group devices.
906 * Callers should hold a reference count to the group during callback.
907 * The group->mutex is held across callbacks, which will block calls to
908 * iommu_group_add/remove_device.
909 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200910static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
911 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600912{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100913 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600914 int ret = 0;
915
Alex Williamsond72e31c2012-05-30 14:18:53 -0600916 list_for_each_entry(device, &group->devices, list) {
917 ret = fn(device->dev, data);
918 if (ret)
919 break;
920 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200921 return ret;
922}
923
924
925int iommu_group_for_each_dev(struct iommu_group *group, void *data,
926 int (*fn)(struct device *, void *))
927{
928 int ret;
929
930 mutex_lock(&group->mutex);
931 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600932 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200933
Alex Williamsond72e31c2012-05-30 14:18:53 -0600934 return ret;
935}
936EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
937
938/**
939 * iommu_group_get - Return the group for a device and increment reference
940 * @dev: get the group that this device belongs to
941 *
942 * This function is called by iommu drivers and users to get the group
943 * for the specified device. If found, the group is returned and the group
944 * reference in incremented, else NULL.
945 */
946struct iommu_group *iommu_group_get(struct device *dev)
947{
948 struct iommu_group *group = dev->iommu_group;
949
950 if (group)
951 kobject_get(group->devices_kobj);
952
953 return group;
954}
955EXPORT_SYMBOL_GPL(iommu_group_get);
956
957/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000958 * iommu_group_ref_get - Increment reference on a group
959 * @group: the group to use, must not be NULL
960 *
961 * This function is called by iommu drivers to take additional references on an
962 * existing group. Returns the given group for convenience.
963 */
964struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
965{
966 kobject_get(group->devices_kobj);
967 return group;
968}
Will Deacona7ba5c32019-12-19 12:03:37 +0000969EXPORT_SYMBOL_GPL(iommu_group_ref_get);
Robin Murphy13f59a72016-11-11 17:59:21 +0000970
971/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600972 * iommu_group_put - Decrement group reference
973 * @group: the group to use
974 *
975 * This function is called by iommu drivers and users to release the
976 * iommu group. Once the reference count is zero, the group is released.
977 */
978void iommu_group_put(struct iommu_group *group)
979{
980 if (group)
981 kobject_put(group->devices_kobj);
982}
983EXPORT_SYMBOL_GPL(iommu_group_put);
984
985/**
986 * iommu_group_register_notifier - Register a notifier for group changes
987 * @group: the group to watch
988 * @nb: notifier block to signal
989 *
990 * This function allows iommu group users to track changes in a group.
991 * See include/linux/iommu.h for actions sent via this notifier. Caller
992 * should hold a reference to the group throughout notifier registration.
993 */
994int iommu_group_register_notifier(struct iommu_group *group,
995 struct notifier_block *nb)
996{
997 return blocking_notifier_chain_register(&group->notifier, nb);
998}
999EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
1000
1001/**
1002 * iommu_group_unregister_notifier - Unregister a notifier
1003 * @group: the group to watch
1004 * @nb: notifier block to signal
1005 *
1006 * Unregister a previously registered group notifier block.
1007 */
1008int iommu_group_unregister_notifier(struct iommu_group *group,
1009 struct notifier_block *nb)
1010{
1011 return blocking_notifier_chain_unregister(&group->notifier, nb);
1012}
1013EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
1014
1015/**
Jacob Pan0c830e62019-06-03 15:57:48 +01001016 * iommu_register_device_fault_handler() - Register a device fault handler
1017 * @dev: the device
1018 * @handler: the fault handler
1019 * @data: private data passed as argument to the handler
1020 *
1021 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001022 * fault event and data as argument. The handler should return 0 on success. If
1023 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1024 * complete the fault by calling iommu_page_response() with one of the following
1025 * response code:
1026 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1027 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1028 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1029 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +01001030 *
1031 * Return 0 if the fault handler was installed successfully, or an error.
1032 */
1033int iommu_register_device_fault_handler(struct device *dev,
1034 iommu_dev_fault_handler_t handler,
1035 void *data)
1036{
Joerg Roedel045a7042020-03-26 16:08:30 +01001037 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001038 int ret = 0;
1039
1040 if (!param)
1041 return -EINVAL;
1042
1043 mutex_lock(&param->lock);
1044 /* Only allow one fault handler registered for each device */
1045 if (param->fault_param) {
1046 ret = -EBUSY;
1047 goto done_unlock;
1048 }
1049
1050 get_device(dev);
1051 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1052 if (!param->fault_param) {
1053 put_device(dev);
1054 ret = -ENOMEM;
1055 goto done_unlock;
1056 }
1057 param->fault_param->handler = handler;
1058 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001059 mutex_init(&param->fault_param->lock);
1060 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +01001061
1062done_unlock:
1063 mutex_unlock(&param->lock);
1064
1065 return ret;
1066}
1067EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1068
1069/**
1070 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1071 * @dev: the device
1072 *
1073 * Remove the device fault handler installed with
1074 * iommu_register_device_fault_handler().
1075 *
1076 * Return 0 on success, or an error.
1077 */
1078int iommu_unregister_device_fault_handler(struct device *dev)
1079{
Joerg Roedel045a7042020-03-26 16:08:30 +01001080 struct dev_iommu *param = dev->iommu;
Jacob Pan0c830e62019-06-03 15:57:48 +01001081 int ret = 0;
1082
1083 if (!param)
1084 return -EINVAL;
1085
1086 mutex_lock(&param->lock);
1087
1088 if (!param->fault_param)
1089 goto unlock;
1090
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001091 /* we cannot unregister handler if there are pending faults */
1092 if (!list_empty(&param->fault_param->faults)) {
1093 ret = -EBUSY;
1094 goto unlock;
1095 }
1096
Jacob Pan0c830e62019-06-03 15:57:48 +01001097 kfree(param->fault_param);
1098 param->fault_param = NULL;
1099 put_device(dev);
1100unlock:
1101 mutex_unlock(&param->lock);
1102
1103 return ret;
1104}
1105EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1106
1107/**
1108 * iommu_report_device_fault() - Report fault event to device driver
1109 * @dev: the device
1110 * @evt: fault event data
1111 *
1112 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001113 * handler. When this function fails and the fault is recoverable, it is the
1114 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +01001115 *
1116 * Return 0 on success, or an error.
1117 */
1118int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1119{
Joerg Roedel045a7042020-03-26 16:08:30 +01001120 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001121 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +01001122 struct iommu_fault_param *fparam;
1123 int ret = 0;
1124
1125 if (!param || !evt)
1126 return -EINVAL;
1127
1128 /* we only report device fault if there is a handler registered */
1129 mutex_lock(&param->lock);
1130 fparam = param->fault_param;
1131 if (!fparam || !fparam->handler) {
1132 ret = -EINVAL;
1133 goto done_unlock;
1134 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001135
1136 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1137 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1138 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1139 GFP_KERNEL);
1140 if (!evt_pending) {
1141 ret = -ENOMEM;
1142 goto done_unlock;
1143 }
1144 mutex_lock(&fparam->lock);
1145 list_add_tail(&evt_pending->list, &fparam->faults);
1146 mutex_unlock(&fparam->lock);
1147 }
1148
Jacob Pan0c830e62019-06-03 15:57:48 +01001149 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001150 if (ret && evt_pending) {
1151 mutex_lock(&fparam->lock);
1152 list_del(&evt_pending->list);
1153 mutex_unlock(&fparam->lock);
1154 kfree(evt_pending);
1155 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001156done_unlock:
1157 mutex_unlock(&param->lock);
1158 return ret;
1159}
1160EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1161
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001162int iommu_page_response(struct device *dev,
1163 struct iommu_page_response *msg)
1164{
1165 bool pasid_valid;
1166 int ret = -EINVAL;
1167 struct iommu_fault_event *evt;
1168 struct iommu_fault_page_request *prm;
Joerg Roedel045a7042020-03-26 16:08:30 +01001169 struct dev_iommu *param = dev->iommu;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001170 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1171
1172 if (!domain || !domain->ops->page_response)
1173 return -ENODEV;
1174
1175 if (!param || !param->fault_param)
1176 return -EINVAL;
1177
1178 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1179 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1180 return -EINVAL;
1181
1182 /* Only send response if there is a fault report pending */
1183 mutex_lock(&param->fault_param->lock);
1184 if (list_empty(&param->fault_param->faults)) {
1185 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1186 goto done_unlock;
1187 }
1188 /*
1189 * Check if we have a matching page request pending to respond,
1190 * otherwise return -EINVAL
1191 */
1192 list_for_each_entry(evt, &param->fault_param->faults, list) {
1193 prm = &evt->fault.prm;
1194 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1195
1196 if ((pasid_valid && prm->pasid != msg->pasid) ||
1197 prm->grpid != msg->grpid)
1198 continue;
1199
1200 /* Sanitize the reply */
1201 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1202
1203 ret = domain->ops->page_response(dev, evt, msg);
1204 list_del(&evt->list);
1205 kfree(evt);
1206 break;
1207 }
1208
1209done_unlock:
1210 mutex_unlock(&param->fault_param->lock);
1211 return ret;
1212}
1213EXPORT_SYMBOL_GPL(iommu_page_response);
1214
Jacob Pan0c830e62019-06-03 15:57:48 +01001215/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001216 * iommu_group_id - Return ID for a group
1217 * @group: the group to ID
1218 *
1219 * Return the unique ID for the group matching the sysfs group number.
1220 */
1221int iommu_group_id(struct iommu_group *group)
1222{
1223 return group->id;
1224}
1225EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001226
Alex Williamsonf096c062014-09-19 10:03:06 -06001227static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1228 unsigned long *devfns);
1229
Alex Williamson104a1c12014-07-03 09:51:18 -06001230/*
1231 * To consider a PCI device isolated, we require ACS to support Source
1232 * Validation, Request Redirection, Completer Redirection, and Upstream
1233 * Forwarding. This effectively means that devices cannot spoof their
1234 * requester ID, requests and completions cannot be redirected, and all
1235 * transactions are forwarded upstream, even as it passes through a
1236 * bridge where the target device is downstream.
1237 */
1238#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1239
Alex Williamsonf096c062014-09-19 10:03:06 -06001240/*
1241 * For multifunction devices which are not isolated from each other, find
1242 * all the other non-isolated functions and look for existing groups. For
1243 * each function, we also need to look for aliases to or from other devices
1244 * that may already have a group.
1245 */
1246static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1247 unsigned long *devfns)
1248{
1249 struct pci_dev *tmp = NULL;
1250 struct iommu_group *group;
1251
1252 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1253 return NULL;
1254
1255 for_each_pci_dev(tmp) {
1256 if (tmp == pdev || tmp->bus != pdev->bus ||
1257 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1258 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1259 continue;
1260
1261 group = get_pci_alias_group(tmp, devfns);
1262 if (group) {
1263 pci_dev_put(tmp);
1264 return group;
1265 }
1266 }
1267
1268 return NULL;
1269}
1270
1271/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001272 * Look for aliases to or from the given device for existing groups. DMA
1273 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001274 * space is quite small (especially since we're really only looking at pcie
1275 * device, and therefore only expect multiple slots on the root complex or
1276 * downstream switch ports). It's conceivable though that a pair of
1277 * multifunction devices could have aliases between them that would cause a
1278 * loop. To prevent this, we use a bitmap to track where we've been.
1279 */
1280static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1281 unsigned long *devfns)
1282{
1283 struct pci_dev *tmp = NULL;
1284 struct iommu_group *group;
1285
1286 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1287 return NULL;
1288
1289 group = iommu_group_get(&pdev->dev);
1290 if (group)
1291 return group;
1292
1293 for_each_pci_dev(tmp) {
1294 if (tmp == pdev || tmp->bus != pdev->bus)
1295 continue;
1296
1297 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001298 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001299 group = get_pci_alias_group(tmp, devfns);
1300 if (group) {
1301 pci_dev_put(tmp);
1302 return group;
1303 }
1304
1305 group = get_pci_function_alias_group(tmp, devfns);
1306 if (group) {
1307 pci_dev_put(tmp);
1308 return group;
1309 }
1310 }
1311 }
1312
1313 return NULL;
1314}
1315
Alex Williamson104a1c12014-07-03 09:51:18 -06001316struct group_for_pci_data {
1317 struct pci_dev *pdev;
1318 struct iommu_group *group;
1319};
1320
1321/*
1322 * DMA alias iterator callback, return the last seen device. Stop and return
1323 * the IOMMU group if we find one along the way.
1324 */
1325static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1326{
1327 struct group_for_pci_data *data = opaque;
1328
1329 data->pdev = pdev;
1330 data->group = iommu_group_get(&pdev->dev);
1331
1332 return data->group != NULL;
1333}
1334
1335/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001336 * Generic device_group call-back function. It just allocates one
1337 * iommu-group per device.
1338 */
1339struct iommu_group *generic_device_group(struct device *dev)
1340{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001341 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001342}
Will Deacona7ba5c32019-12-19 12:03:37 +00001343EXPORT_SYMBOL_GPL(generic_device_group);
Joerg Roedel6eab5562015-10-21 23:51:38 +02001344
1345/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001346 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1347 * to find or create an IOMMU group for a device.
1348 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001349struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001350{
Joerg Roedel5e622922015-10-21 23:51:37 +02001351 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001352 struct group_for_pci_data data;
1353 struct pci_bus *bus;
1354 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001355 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001356
Joerg Roedel5e622922015-10-21 23:51:37 +02001357 if (WARN_ON(!dev_is_pci(dev)))
1358 return ERR_PTR(-EINVAL);
1359
Alex Williamson104a1c12014-07-03 09:51:18 -06001360 /*
1361 * Find the upstream DMA alias for the device. A device must not
1362 * be aliased due to topology in order to have its own IOMMU group.
1363 * If we find an alias along the way that already belongs to a
1364 * group, use it.
1365 */
1366 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1367 return data.group;
1368
1369 pdev = data.pdev;
1370
1371 /*
1372 * Continue upstream from the point of minimum IOMMU granularity
1373 * due to aliases to the point where devices are protected from
1374 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1375 * group, use it.
1376 */
1377 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1378 if (!bus->self)
1379 continue;
1380
1381 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1382 break;
1383
1384 pdev = bus->self;
1385
1386 group = iommu_group_get(&pdev->dev);
1387 if (group)
1388 return group;
1389 }
1390
1391 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001392 * Look for existing groups on device aliases. If we alias another
1393 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001394 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001395 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1396 if (group)
1397 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001398
1399 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001400 * Look for existing groups on non-isolated functions on the same
1401 * slot and aliases of those funcions, if any. No need to clear
1402 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001403 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001404 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1405 if (group)
1406 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001407
1408 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001409 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001410}
Will Deacona7ba5c32019-12-19 12:03:37 +00001411EXPORT_SYMBOL_GPL(pci_device_group);
Alex Williamson104a1c12014-07-03 09:51:18 -06001412
Nipun Guptaeab03e22018-09-10 19:19:18 +05301413/* Get the IOMMU group for device on fsl-mc bus */
1414struct iommu_group *fsl_mc_device_group(struct device *dev)
1415{
1416 struct device *cont_dev = fsl_mc_cont_dev(dev);
1417 struct iommu_group *group;
1418
1419 group = iommu_group_get(cont_dev);
1420 if (!group)
1421 group = iommu_group_alloc();
1422 return group;
1423}
Will Deacona7ba5c32019-12-19 12:03:37 +00001424EXPORT_SYMBOL_GPL(fsl_mc_device_group);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301425
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001426static int iommu_get_def_domain_type(struct device *dev)
1427{
1428 const struct iommu_ops *ops = dev->bus->iommu_ops;
1429 unsigned int type = 0;
1430
1431 if (ops->def_domain_type)
1432 type = ops->def_domain_type(dev);
1433
1434 return (type == 0) ? iommu_def_domain_type : type;
1435}
1436
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001437static int iommu_group_alloc_default_domain(struct bus_type *bus,
1438 struct iommu_group *group,
1439 unsigned int type)
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001440{
1441 struct iommu_domain *dom;
1442
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001443 dom = __iommu_domain_alloc(bus, type);
Sai Praneeth Prakhya4cbf3852020-04-29 15:36:40 +02001444 if (!dom && type != IOMMU_DOMAIN_DMA) {
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001445 dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1446 if (dom)
1447 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1448 type, group->name);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001449 }
1450
1451 if (!dom)
1452 return -ENOMEM;
1453
1454 group->default_domain = dom;
1455 if (!group->domain)
1456 group->domain = dom;
1457
1458 if (!iommu_dma_strict) {
1459 int attr = 1;
1460 iommu_domain_set_attr(dom,
1461 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1462 &attr);
1463 }
1464
1465 return 0;
1466}
1467
Joerg Roedel6e1aa202020-04-29 15:36:46 +02001468static int iommu_alloc_default_domain(struct device *dev)
1469{
1470 struct iommu_group *group;
1471 unsigned int type;
1472
1473 group = iommu_group_get(dev);
1474 if (!group)
1475 return -ENODEV;
1476
1477 if (group->default_domain)
1478 return 0;
1479
1480 type = iommu_get_def_domain_type(dev);
1481
1482 return iommu_group_alloc_default_domain(dev->bus, group, type);
1483}
1484
Alex Williamson104a1c12014-07-03 09:51:18 -06001485/**
1486 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1487 * @dev: target device
1488 *
1489 * This function is intended to be called by IOMMU drivers and extended to
1490 * support common, bus-defined algorithms when determining or creating the
1491 * IOMMU group for a device. On success, the caller will hold a reference
1492 * to the returned IOMMU group, which will already include the provided
1493 * device. The reference should be released with iommu_group_put().
1494 */
1495struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1496{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001497 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001498 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001499 int ret;
1500
1501 group = iommu_group_get(dev);
1502 if (group)
1503 return group;
1504
Robin Murphy05f803002017-07-21 13:12:38 +01001505 if (!ops)
1506 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001507
Robin Murphy05f803002017-07-21 13:12:38 +01001508 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001509 if (WARN_ON_ONCE(group == NULL))
1510 return ERR_PTR(-EINVAL);
1511
Alex Williamson104a1c12014-07-03 09:51:18 -06001512 if (IS_ERR(group))
1513 return group;
1514
1515 ret = iommu_group_add_device(group, dev);
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001516 if (ret)
1517 goto out_put_group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001518
1519 return group;
Joerg Roedelff2a08b2020-04-29 15:36:39 +02001520
1521out_put_group:
1522 iommu_group_put(group);
1523
1524 return ERR_PTR(ret);
Alex Williamson104a1c12014-07-03 09:51:18 -06001525}
Will Deacona7ba5c32019-12-19 12:03:37 +00001526EXPORT_SYMBOL(iommu_group_get_for_dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001527
Joerg Roedel6827ca82015-05-28 18:41:35 +02001528struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1529{
1530 return group->default_domain;
1531}
1532
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001533static int probe_iommu_group(struct device *dev, void *data)
1534{
1535 const struct iommu_ops *ops = dev->bus->iommu_ops;
1536 struct list_head *group_list = data;
Joerg Roedel5012c392020-04-29 15:36:51 +02001537 struct iommu_group *group;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001538 int ret;
1539
1540 if (!dev_iommu_get(dev))
1541 return -ENOMEM;
1542
Joerg Roedel5012c392020-04-29 15:36:51 +02001543 /* Device is probed already if in a group */
1544 group = iommu_group_get(dev);
1545 if (group) {
1546 iommu_group_put(group);
1547 return 0;
1548 }
1549
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001550 if (!try_module_get(ops->owner)) {
1551 ret = -EINVAL;
1552 goto err_free_dev_iommu;
1553 }
1554
1555 ret = __iommu_probe_device(dev, group_list);
1556 if (ret)
1557 goto err_module_put;
1558
1559 return 0;
1560
1561err_module_put:
1562 module_put(ops->owner);
1563err_free_dev_iommu:
1564 dev_iommu_free(dev);
1565
1566 if (ret == -ENODEV)
1567 ret = 0;
1568
1569 return ret;
1570}
1571
Joerg Roedel8da30142015-05-28 18:41:27 +02001572static int remove_iommu_group(struct device *dev, void *data)
1573{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001574 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001575
1576 return 0;
1577}
1578
Alex Williamsond72e31c2012-05-30 14:18:53 -06001579static int iommu_bus_notifier(struct notifier_block *nb,
1580 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001581{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001582 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001583 struct device *dev = data;
1584 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001585
Alex Williamsond72e31c2012-05-30 14:18:53 -06001586 /*
1587 * ADD/DEL call into iommu driver ops if provided, which may
1588 * result in ADD/DEL notifiers to group->notifier
1589 */
1590 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001591 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001592
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001593 ret = iommu_probe_device(dev);
1594 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001595 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001596 iommu_release_device(dev);
1597 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001598 }
Alex Williamson14604322011-10-21 15:56:05 -04001599
Alex Williamsond72e31c2012-05-30 14:18:53 -06001600 /*
1601 * Remaining BUS_NOTIFYs get filtered and republished to the
1602 * group, if anyone is listening
1603 */
1604 group = iommu_group_get(dev);
1605 if (!group)
1606 return 0;
1607
1608 switch (action) {
1609 case BUS_NOTIFY_BIND_DRIVER:
1610 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1611 break;
1612 case BUS_NOTIFY_BOUND_DRIVER:
1613 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1614 break;
1615 case BUS_NOTIFY_UNBIND_DRIVER:
1616 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1617 break;
1618 case BUS_NOTIFY_UNBOUND_DRIVER:
1619 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1620 break;
1621 }
1622
1623 if (group_action)
1624 blocking_notifier_call_chain(&group->notifier,
1625 group_action, dev);
1626
1627 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001628 return 0;
1629}
1630
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001631struct __group_domain_type {
1632 struct device *dev;
1633 unsigned int type;
1634};
1635
1636static int probe_get_default_domain_type(struct device *dev, void *data)
1637{
1638 const struct iommu_ops *ops = dev->bus->iommu_ops;
1639 struct __group_domain_type *gtype = data;
1640 unsigned int type = 0;
1641
1642 if (ops->def_domain_type)
1643 type = ops->def_domain_type(dev);
1644
1645 if (type) {
1646 if (gtype->type && gtype->type != type) {
1647 dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1648 iommu_domain_type_str(type),
1649 dev_name(gtype->dev),
1650 iommu_domain_type_str(gtype->type));
1651 gtype->type = 0;
1652 }
1653
1654 if (!gtype->dev) {
1655 gtype->dev = dev;
1656 gtype->type = type;
1657 }
1658 }
1659
1660 return 0;
1661}
1662
1663static void probe_alloc_default_domain(struct bus_type *bus,
1664 struct iommu_group *group)
1665{
1666 struct __group_domain_type gtype;
1667
1668 memset(&gtype, 0, sizeof(gtype));
1669
1670 /* Ask for default domain requirements of all devices in the group */
1671 __iommu_group_for_each_dev(group, &gtype,
1672 probe_get_default_domain_type);
1673
1674 if (!gtype.type)
1675 gtype.type = iommu_def_domain_type;
1676
1677 iommu_group_alloc_default_domain(bus, group, gtype.type);
Joerg Roedelce574c22020-04-29 15:36:50 +02001678
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001679}
1680
1681static int iommu_group_do_dma_attach(struct device *dev, void *data)
1682{
1683 struct iommu_domain *domain = data;
1684 const struct iommu_ops *ops;
1685 int ret;
1686
1687 ret = __iommu_attach_device(domain, dev);
1688
1689 ops = domain->ops;
1690
1691 if (ret == 0 && ops->probe_finalize)
1692 ops->probe_finalize(dev);
1693
1694 return ret;
1695}
1696
1697static int __iommu_group_dma_attach(struct iommu_group *group)
1698{
1699 return __iommu_group_for_each_dev(group, group->default_domain,
1700 iommu_group_do_dma_attach);
1701}
1702
Joerg Roedelce574c22020-04-29 15:36:50 +02001703static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1704{
1705 struct iommu_group *group = data;
1706
1707 iommu_create_device_direct_mappings(group, dev);
1708
1709 return 0;
1710}
1711
1712static int iommu_group_create_direct_mappings(struct iommu_group *group)
1713{
1714 return __iommu_group_for_each_dev(group, group,
1715 iommu_do_create_direct_mappings);
1716}
1717
Joerg Roedel5012c392020-04-29 15:36:51 +02001718int bus_iommu_probe(struct bus_type *bus)
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001719{
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001720 struct iommu_group *group, *next;
1721 LIST_HEAD(group_list);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001722 int ret;
1723
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001724 /*
1725 * This code-path does not allocate the default domain when
1726 * creating the iommu group, so do it after the groups are
1727 * created.
1728 */
1729 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1730 if (ret)
1731 return ret;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001732
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001733 list_for_each_entry_safe(group, next, &group_list, entry) {
1734 /* Remove item from the list */
1735 list_del_init(&group->entry);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001736
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001737 mutex_lock(&group->mutex);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001738
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001739 /* Try to allocate default domain */
1740 probe_alloc_default_domain(bus, group);
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001741
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001742 if (!group->default_domain) {
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001743 mutex_unlock(&group->mutex);
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001744 continue;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001745 }
Joerg Roedel3eeeb452020-04-29 15:37:10 +02001746
1747 iommu_group_create_direct_mappings(group);
1748
1749 ret = __iommu_group_dma_attach(group);
1750
1751 mutex_unlock(&group->mutex);
1752
1753 if (ret)
1754 break;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001755 }
1756
1757 return ret;
1758}
1759
Mark Salterfb3e3062014-09-21 13:58:24 -04001760static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001761{
Mark Salterfb3e3062014-09-21 13:58:24 -04001762 struct notifier_block *nb;
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001763 int err;
Thierry Redingb22f6432014-06-27 09:03:12 +02001764
Mark Salterfb3e3062014-09-21 13:58:24 -04001765 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1766 if (!nb)
1767 return -ENOMEM;
1768
1769 nb->notifier_call = iommu_bus_notifier;
1770
1771 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001772 if (err)
1773 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001774
Joerg Roedeldeac0b3b2020-04-29 15:36:49 +02001775 err = bus_iommu_probe(bus);
Joerg Roedel8da30142015-05-28 18:41:27 +02001776 if (err)
1777 goto out_err;
1778
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001779
1780 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001781
1782out_err:
1783 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001784 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001785 bus_unregister_notifier(bus, nb);
1786
1787out_free:
1788 kfree(nb);
1789
1790 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001791}
1792
Joerg Roedelff217762011-08-26 16:48:26 +02001793/**
1794 * bus_set_iommu - set iommu-callbacks for the bus
1795 * @bus: bus.
1796 * @ops: the callbacks provided by the iommu-driver
1797 *
1798 * This function is called by an iommu driver to set the iommu methods
1799 * used for a particular bus. Drivers for devices on that bus can use
1800 * the iommu-api after these ops are registered.
1801 * This special function is needed because IOMMUs are usually devices on
1802 * the bus itself, so the iommu drivers are not initialized when the bus
1803 * is set up. With this function the iommu-driver can set the iommu-ops
1804 * afterwards.
1805 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001806int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001807{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001808 int err;
1809
Will Deacon4312cf72019-12-19 12:03:43 +00001810 if (ops == NULL) {
1811 bus->iommu_ops = NULL;
1812 return 0;
1813 }
1814
Joerg Roedelff217762011-08-26 16:48:26 +02001815 if (bus->iommu_ops != NULL)
1816 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001817
Joerg Roedelff217762011-08-26 16:48:26 +02001818 bus->iommu_ops = ops;
1819
1820 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001821 err = iommu_bus_init(bus, ops);
1822 if (err)
1823 bus->iommu_ops = NULL;
1824
1825 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001826}
Joerg Roedelff217762011-08-26 16:48:26 +02001827EXPORT_SYMBOL_GPL(bus_set_iommu);
1828
Joerg Roedela1b60c12011-09-06 18:46:34 +02001829bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001830{
Joerg Roedel94441c32011-09-06 18:58:54 +02001831 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001832}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001833EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001834
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001835bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1836{
1837 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1838 return false;
1839
1840 return bus->iommu_ops->capable(cap);
1841}
1842EXPORT_SYMBOL_GPL(iommu_capable);
1843
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001844/**
1845 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1846 * @domain: iommu domain
1847 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001848 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001849 *
1850 * This function should be used by IOMMU users which want to be notified
1851 * whenever an IOMMU fault happens.
1852 *
1853 * The fault handler itself should return 0 on success, and an appropriate
1854 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001855 */
1856void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001857 iommu_fault_handler_t handler,
1858 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001859{
1860 BUG_ON(!domain);
1861
1862 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001863 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001864}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001865EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001866
Joerg Roedel53723dc2015-05-28 18:41:29 +02001867static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1868 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001869{
1870 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001871
Joerg Roedel94441c32011-09-06 18:58:54 +02001872 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001873 return NULL;
1874
Joerg Roedel53723dc2015-05-28 18:41:29 +02001875 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001876 if (!domain)
1877 return NULL;
1878
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001879 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001880 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001881 /* Assume all sizes by default; the driver may override this later */
1882 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001883
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001884 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001885}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001886
Joerg Roedel53723dc2015-05-28 18:41:29 +02001887struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1888{
1889 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001890}
1891EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1892
1893void iommu_domain_free(struct iommu_domain *domain)
1894{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001895 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001896}
1897EXPORT_SYMBOL_GPL(iommu_domain_free);
1898
Joerg Roedel426a2732015-05-28 18:41:30 +02001899static int __iommu_attach_device(struct iommu_domain *domain,
1900 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001901{
Shuah Khanb54db772013-08-15 11:59:26 -06001902 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001903 if ((domain->ops->is_attach_deferred != NULL) &&
1904 domain->ops->is_attach_deferred(domain, dev))
1905 return 0;
1906
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001907 if (unlikely(domain->ops->attach_dev == NULL))
1908 return -ENODEV;
1909
Shuah Khanb54db772013-08-15 11:59:26 -06001910 ret = domain->ops->attach_dev(domain, dev);
1911 if (!ret)
1912 trace_attach_device_to_domain(dev);
1913 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001914}
Joerg Roedel426a2732015-05-28 18:41:30 +02001915
1916int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1917{
1918 struct iommu_group *group;
1919 int ret;
1920
1921 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001922 if (!group)
1923 return -ENODEV;
1924
Joerg Roedel426a2732015-05-28 18:41:30 +02001925 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001926 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001927 * change while we are attaching
1928 */
1929 mutex_lock(&group->mutex);
1930 ret = -EINVAL;
1931 if (iommu_group_device_count(group) != 1)
1932 goto out_unlock;
1933
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001934 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001935
1936out_unlock:
1937 mutex_unlock(&group->mutex);
1938 iommu_group_put(group);
1939
1940 return ret;
1941}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001942EXPORT_SYMBOL_GPL(iommu_attach_device);
1943
Yi L Liu4c7c1712019-10-02 12:42:40 -07001944int iommu_cache_invalidate(struct iommu_domain *domain, struct device *dev,
1945 struct iommu_cache_invalidate_info *inv_info)
1946{
1947 if (unlikely(!domain->ops->cache_invalidate))
1948 return -ENODEV;
1949
1950 return domain->ops->cache_invalidate(domain, dev, inv_info);
1951}
1952EXPORT_SYMBOL_GPL(iommu_cache_invalidate);
1953
Jacob Pan808be0a2019-10-02 12:42:43 -07001954int iommu_sva_bind_gpasid(struct iommu_domain *domain,
1955 struct device *dev, struct iommu_gpasid_bind_data *data)
1956{
1957 if (unlikely(!domain->ops->sva_bind_gpasid))
1958 return -ENODEV;
1959
1960 return domain->ops->sva_bind_gpasid(domain, dev, data);
1961}
1962EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid);
1963
1964int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
1965 ioasid_t pasid)
1966{
1967 if (unlikely(!domain->ops->sva_unbind_gpasid))
1968 return -ENODEV;
1969
1970 return domain->ops->sva_unbind_gpasid(dev, pasid);
1971}
1972EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
1973
Joerg Roedel426a2732015-05-28 18:41:30 +02001974static void __iommu_detach_device(struct iommu_domain *domain,
1975 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001976{
Baoquan Hee01d1912017-08-09 16:33:40 +08001977 if ((domain->ops->is_attach_deferred != NULL) &&
1978 domain->ops->is_attach_deferred(domain, dev))
1979 return;
1980
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001981 if (unlikely(domain->ops->detach_dev == NULL))
1982 return;
1983
1984 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001985 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001986}
Joerg Roedel426a2732015-05-28 18:41:30 +02001987
1988void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1989{
1990 struct iommu_group *group;
1991
1992 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001993 if (!group)
1994 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001995
1996 mutex_lock(&group->mutex);
1997 if (iommu_group_device_count(group) != 1) {
1998 WARN_ON(1);
1999 goto out_unlock;
2000 }
2001
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002002 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02002003
2004out_unlock:
2005 mutex_unlock(&group->mutex);
2006 iommu_group_put(group);
2007}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002008EXPORT_SYMBOL_GPL(iommu_detach_device);
2009
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002010struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2011{
2012 struct iommu_domain *domain;
2013 struct iommu_group *group;
2014
2015 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01002016 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02002017 return NULL;
2018
2019 domain = group->domain;
2020
2021 iommu_group_put(group);
2022
2023 return domain;
2024}
2025EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2026
Alex Williamsond72e31c2012-05-30 14:18:53 -06002027/*
Robin Murphy6af588f2018-09-12 16:24:12 +01002028 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2029 * guarantees that the group and its default domain are valid and correct.
2030 */
2031struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2032{
2033 return dev->iommu_group->default_domain;
2034}
2035
2036/*
Rami Rosen35449ad2018-09-18 17:38:49 +03002037 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06002038 * the IOMMU API works on domains and devices. Bridge that gap by
2039 * iterating over the devices in a group. Ideally we'd have a single
2040 * device which represents the requestor ID of the group, but we also
2041 * allow IOMMU drivers to create policy defined minimum sets, where
2042 * the physical hardware may be able to distiguish members, but we
2043 * wish to group them at a higher level (ex. untrusted multi-function
2044 * PCI devices). Thus we attach each device.
2045 */
2046static int iommu_group_do_attach_device(struct device *dev, void *data)
2047{
2048 struct iommu_domain *domain = data;
2049
Joerg Roedel426a2732015-05-28 18:41:30 +02002050 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002051}
2052
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002053static int __iommu_attach_group(struct iommu_domain *domain,
2054 struct iommu_group *group)
2055{
2056 int ret;
2057
2058 if (group->default_domain && group->domain != group->default_domain)
2059 return -EBUSY;
2060
2061 ret = __iommu_group_for_each_dev(group, domain,
2062 iommu_group_do_attach_device);
2063 if (ret == 0)
2064 group->domain = domain;
2065
2066 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002067}
2068
2069int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2070{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002071 int ret;
2072
2073 mutex_lock(&group->mutex);
2074 ret = __iommu_attach_group(domain, group);
2075 mutex_unlock(&group->mutex);
2076
2077 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06002078}
2079EXPORT_SYMBOL_GPL(iommu_attach_group);
2080
2081static int iommu_group_do_detach_device(struct device *dev, void *data)
2082{
2083 struct iommu_domain *domain = data;
2084
Joerg Roedel426a2732015-05-28 18:41:30 +02002085 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002086
2087 return 0;
2088}
2089
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002090static void __iommu_detach_group(struct iommu_domain *domain,
2091 struct iommu_group *group)
2092{
2093 int ret;
2094
2095 if (!group->default_domain) {
2096 __iommu_group_for_each_dev(group, domain,
2097 iommu_group_do_detach_device);
2098 group->domain = NULL;
2099 return;
2100 }
2101
2102 if (group->domain == group->default_domain)
2103 return;
2104
2105 /* Detach by re-attaching to the default domain */
2106 ret = __iommu_group_for_each_dev(group, group->default_domain,
2107 iommu_group_do_attach_device);
2108 if (ret != 0)
2109 WARN_ON(1);
2110 else
2111 group->domain = group->default_domain;
2112}
2113
Alex Williamsond72e31c2012-05-30 14:18:53 -06002114void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2115{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02002116 mutex_lock(&group->mutex);
2117 __iommu_detach_group(domain, group);
2118 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002119}
2120EXPORT_SYMBOL_GPL(iommu_detach_group);
2121
Varun Sethibb5547a2013-03-29 01:23:58 +05302122phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002123{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002124 if (unlikely(domain->ops->iova_to_phys == NULL))
2125 return 0;
2126
2127 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002128}
2129EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002130
Alex Williamsonbd139692013-06-17 19:57:34 -06002131static size_t iommu_pgsize(struct iommu_domain *domain,
2132 unsigned long addr_merge, size_t size)
2133{
2134 unsigned int pgsize_idx;
2135 size_t pgsize;
2136
2137 /* Max page size that still fits into 'size' */
2138 pgsize_idx = __fls(size);
2139
2140 /* need to consider alignment requirements ? */
2141 if (likely(addr_merge)) {
2142 /* Max page size allowed by address */
2143 unsigned int align_pgsize_idx = __ffs(addr_merge);
2144 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
2145 }
2146
2147 /* build a mask of acceptable page sizes */
2148 pgsize = (1UL << (pgsize_idx + 1)) - 1;
2149
2150 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002151 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06002152
2153 /* make sure we're still sane */
2154 BUG_ON(!pgsize);
2155
2156 /* pick the biggest page */
2157 pgsize_idx = __fls(pgsize);
2158 pgsize = 1UL << pgsize_idx;
2159
2160 return pgsize;
2161}
2162
Tom Murphy781ca2d2019-09-08 09:56:38 -07002163int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2164 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002165{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002166 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002167 unsigned long orig_iova = iova;
2168 unsigned int min_pagesz;
2169 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002170 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002171 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002172
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002173 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002174 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02002175 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002176
Joerg Roedela10315e2015-03-26 13:43:06 +01002177 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2178 return -EINVAL;
2179
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002180 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002181 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002182
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002183 /*
2184 * both the virtual address and the physical one, as well as
2185 * the size of the mapping, must be aligned (at least) to the
2186 * size of the smallest page supported by the hardware
2187 */
2188 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03002189 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002190 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002191 return -EINVAL;
2192 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002193
Fabio Estevamabedb042013-08-22 10:25:42 -03002194 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002195
2196 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002197 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002198
Fabio Estevamabedb042013-08-22 10:25:42 -03002199 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07002200 iova, &paddr, pgsize);
Tom Murphy781ca2d2019-09-08 09:56:38 -07002201 ret = ops->map(domain, iova, paddr, pgsize, prot, gfp);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002202
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002203 if (ret)
2204 break;
2205
2206 iova += pgsize;
2207 paddr += pgsize;
2208 size -= pgsize;
2209 }
2210
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03002211 if (ops->iotlb_sync_map)
2212 ops->iotlb_sync_map(domain);
2213
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002214 /* unroll mapping in case something went wrong */
2215 if (ret)
2216 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06002217 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09002218 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002219
2220 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002221}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002222
2223int iommu_map(struct iommu_domain *domain, unsigned long iova,
2224 phys_addr_t paddr, size_t size, int prot)
2225{
2226 might_sleep();
2227 return __iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL);
2228}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002229EXPORT_SYMBOL_GPL(iommu_map);
2230
Tom Murphy781ca2d2019-09-08 09:56:38 -07002231int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
2232 phys_addr_t paddr, size_t size, int prot)
2233{
2234 return __iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC);
2235}
2236EXPORT_SYMBOL_GPL(iommu_map_atomic);
2237
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002238static size_t __iommu_unmap(struct iommu_domain *domain,
2239 unsigned long iova, size_t size,
Will Deacona7d20dc2019-07-02 16:43:48 +01002240 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002241{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002242 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002243 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07002244 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002245 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002246
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002247 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002248 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002249 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002250
Joerg Roedela10315e2015-03-26 13:43:06 +01002251 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002252 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01002253
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002254 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002255 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002256
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002257 /*
2258 * The virtual address, as well as the size of the mapping, must be
2259 * aligned (at least) to the size of the smallest page supported
2260 * by the hardware
2261 */
2262 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07002263 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2264 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05002265 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002266 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002267
Joe Perches6197ca82013-06-23 12:29:04 -07002268 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002269
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002270 /*
2271 * Keep iterating until we either unmap 'size' bytes (or more)
2272 * or we hit an area that isn't mapped.
2273 */
2274 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06002275 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002276
Will Deacon56f8af52019-07-02 16:44:06 +01002277 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002278 if (!unmapped_page)
2279 break;
2280
Joe Perches6197ca82013-06-23 12:29:04 -07002281 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2282 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002283
2284 iova += unmapped_page;
2285 unmapped += unmapped_page;
2286 }
2287
Shuah Khandb8614d2015-01-16 20:53:17 -07002288 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02002289 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002290}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002291
2292size_t iommu_unmap(struct iommu_domain *domain,
2293 unsigned long iova, size_t size)
2294{
Will Deacona7d20dc2019-07-02 16:43:48 +01002295 struct iommu_iotlb_gather iotlb_gather;
2296 size_t ret;
2297
2298 iommu_iotlb_gather_init(&iotlb_gather);
2299 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2300 iommu_tlb_sync(domain, &iotlb_gather);
2301
2302 return ret;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002303}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01002304EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04002305
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002306size_t iommu_unmap_fast(struct iommu_domain *domain,
Will Deacona7d20dc2019-07-02 16:43:48 +01002307 unsigned long iova, size_t size,
2308 struct iommu_iotlb_gather *iotlb_gather)
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002309{
Will Deacona7d20dc2019-07-02 16:43:48 +01002310 return __iommu_unmap(domain, iova, size, iotlb_gather);
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02002311}
2312EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2313
Tom Murphy781ca2d2019-09-08 09:56:38 -07002314size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2315 struct scatterlist *sg, unsigned int nents, int prot,
2316 gfp_t gfp)
Olav Haugan315786e2014-10-25 09:55:16 -07002317{
Robin Murphy5d95f402018-10-11 16:56:42 +01002318 size_t len = 0, mapped = 0;
2319 phys_addr_t start;
2320 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002321 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07002322
Robin Murphy5d95f402018-10-11 16:56:42 +01002323 while (i <= nents) {
2324 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002325
Robin Murphy5d95f402018-10-11 16:56:42 +01002326 if (len && s_phys != start + len) {
Tom Murphy781ca2d2019-09-08 09:56:38 -07002327 ret = __iommu_map(domain, iova + mapped, start,
2328 len, prot, gfp);
2329
Robin Murphy5d95f402018-10-11 16:56:42 +01002330 if (ret)
2331 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00002332
Robin Murphy5d95f402018-10-11 16:56:42 +01002333 mapped += len;
2334 len = 0;
2335 }
Robin Murphy18f23402014-11-25 17:50:55 +00002336
Robin Murphy5d95f402018-10-11 16:56:42 +01002337 if (len) {
2338 len += sg->length;
2339 } else {
2340 len = sg->length;
2341 start = s_phys;
2342 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01002343
Robin Murphy5d95f402018-10-11 16:56:42 +01002344 if (++i < nents)
2345 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07002346 }
2347
2348 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01002349
2350out_err:
2351 /* undo mappings already done */
2352 iommu_unmap(domain, iova, mapped);
2353
2354 return 0;
2355
Olav Haugan315786e2014-10-25 09:55:16 -07002356}
Tom Murphy781ca2d2019-09-08 09:56:38 -07002357
2358size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2359 struct scatterlist *sg, unsigned int nents, int prot)
2360{
2361 might_sleep();
2362 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL);
2363}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02002364EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002365
Tom Murphy781ca2d2019-09-08 09:56:38 -07002366size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova,
2367 struct scatterlist *sg, unsigned int nents, int prot)
2368{
2369 return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
2370}
2371EXPORT_SYMBOL_GPL(iommu_map_sg_atomic);
2372
Joerg Roedeld7787d52013-01-29 14:26:20 +01002373int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05302374 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01002375{
2376 if (unlikely(domain->ops->domain_window_enable == NULL))
2377 return -ENODEV;
2378
Varun Sethi80f97f02013-03-29 01:24:00 +05302379 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2380 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01002381}
2382EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2383
2384void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2385{
2386 if (unlikely(domain->ops->domain_window_disable == NULL))
2387 return;
2388
2389 return domain->ops->domain_window_disable(domain, wnd_nr);
2390}
2391EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2392
Joerg Roedel207c6e32017-04-26 15:39:28 +02002393/**
2394 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2395 * @domain: the iommu domain where the fault has happened
2396 * @dev: the device where the fault has happened
2397 * @iova: the faulting address
2398 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2399 *
2400 * This function should be called by the low-level IOMMU implementations
2401 * whenever IOMMU faults happen, to allow high-level users, that are
2402 * interested in such events, to know about them.
2403 *
2404 * This event may be useful for several possible use cases:
2405 * - mere logging of the event
2406 * - dynamic TLB/PTE loading
2407 * - if restarting of the faulting device is required
2408 *
2409 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2410 * PTE/TLB loading will one day be supported, implementations will be able
2411 * to tell whether it succeeded or not according to this return value).
2412 *
2413 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2414 * (though fault handlers can also return -ENOSYS, in case they want to
2415 * elicit the default behavior of the IOMMU drivers).
2416 */
2417int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2418 unsigned long iova, int flags)
2419{
2420 int ret = -ENOSYS;
2421
2422 /*
2423 * if upper layers showed interest and installed a fault handler,
2424 * invoke it.
2425 */
2426 if (domain->handler)
2427 ret = domain->handler(domain, dev, iova, flags,
2428 domain->handler_token);
2429
2430 trace_io_page_fault(dev, iova, flags);
2431 return ret;
2432}
2433EXPORT_SYMBOL_GPL(report_iommu_fault);
2434
Alex Williamsond72e31c2012-05-30 14:18:53 -06002435static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002436{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002437 iommu_group_kset = kset_create_and_add("iommu_groups",
2438 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002439 BUG_ON(!iommu_group_kset);
2440
Gary R Hookbad614b2018-06-12 16:41:21 -05002441 iommu_debugfs_setup();
2442
Alex Williamsond72e31c2012-05-30 14:18:53 -06002443 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002444}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002445core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002446
2447int iommu_domain_get_attr(struct iommu_domain *domain,
2448 enum iommu_attr attr, void *data)
2449{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002450 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002451 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002452 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002453
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002454 switch (attr) {
2455 case DOMAIN_ATTR_GEOMETRY:
2456 geometry = data;
2457 *geometry = domain->geometry;
2458
2459 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002460 case DOMAIN_ATTR_PAGING:
2461 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002462 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01002463 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002464 default:
2465 if (!domain->ops->domain_get_attr)
2466 return -EINVAL;
2467
2468 ret = domain->ops->domain_get_attr(domain, attr, data);
2469 }
2470
2471 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002472}
2473EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2474
2475int iommu_domain_set_attr(struct iommu_domain *domain,
2476 enum iommu_attr attr, void *data)
2477{
Joerg Roedel69356712013-02-04 14:00:01 +01002478 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002479
Joerg Roedel69356712013-02-04 14:00:01 +01002480 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01002481 default:
2482 if (domain->ops->domain_set_attr == NULL)
2483 return -EINVAL;
2484
2485 ret = domain->ops->domain_set_attr(domain, attr, data);
2486 }
2487
2488 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002489}
2490EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02002491
Eric Augere5b52342017-01-19 20:57:47 +00002492void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002493{
2494 const struct iommu_ops *ops = dev->bus->iommu_ops;
2495
Eric Augere5b52342017-01-19 20:57:47 +00002496 if (ops && ops->get_resv_regions)
2497 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002498}
2499
Eric Augere5b52342017-01-19 20:57:47 +00002500void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002501{
2502 const struct iommu_ops *ops = dev->bus->iommu_ops;
2503
Eric Augere5b52342017-01-19 20:57:47 +00002504 if (ops && ops->put_resv_regions)
2505 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002506}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002507
Thierry Redingf9f69712019-12-18 14:42:01 +01002508/**
2509 * generic_iommu_put_resv_regions - Reserved region driver helper
2510 * @dev: device for which to free reserved regions
2511 * @list: reserved region list for device
2512 *
2513 * IOMMU drivers can use this to implement their .put_resv_regions() callback
2514 * for simple reservations. Memory allocated for each reserved region will be
2515 * freed. If an IOMMU driver allocates additional resources per region, it is
2516 * going to have to implement a custom callback.
2517 */
2518void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)
2519{
2520 struct iommu_resv_region *entry, *next;
2521
2522 list_for_each_entry_safe(entry, next, list, list)
2523 kfree(entry);
2524}
2525EXPORT_SYMBOL(generic_iommu_put_resv_regions);
2526
Eric Auger2b20cbb2017-01-19 20:57:49 +00002527struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002528 size_t length, int prot,
2529 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002530{
2531 struct iommu_resv_region *region;
2532
2533 region = kzalloc(sizeof(*region), GFP_KERNEL);
2534 if (!region)
2535 return NULL;
2536
2537 INIT_LIST_HEAD(&region->list);
2538 region->start = start;
2539 region->length = length;
2540 region->prot = prot;
2541 region->type = type;
2542 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002543}
Will Deacona7ba5c32019-12-19 12:03:37 +00002544EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002545
Lu Baolu7423e012019-05-25 13:41:22 +08002546static int
2547request_default_domain_for_dev(struct device *dev, unsigned long type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002548{
Lu Baolu7423e012019-05-25 13:41:22 +08002549 struct iommu_domain *domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002550 struct iommu_group *group;
2551 int ret;
2552
2553 /* Device must already be in a group before calling this function */
Lu Baolu57274ea2019-05-21 15:27:35 +08002554 group = iommu_group_get(dev);
2555 if (!group)
2556 return -EINVAL;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002557
2558 mutex_lock(&group->mutex);
2559
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002560 ret = 0;
Lu Baolu7423e012019-05-25 13:41:22 +08002561 if (group->default_domain && group->default_domain->type == type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002562 goto out;
2563
2564 /* Don't change mappings of existing devices */
2565 ret = -EBUSY;
2566 if (iommu_group_device_count(group) != 1)
2567 goto out;
2568
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002569 ret = -ENOMEM;
Lu Baolu7423e012019-05-25 13:41:22 +08002570 domain = __iommu_domain_alloc(dev->bus, type);
2571 if (!domain)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002572 goto out;
2573
2574 /* Attach the device to the domain */
Lu Baolu7423e012019-05-25 13:41:22 +08002575 ret = __iommu_attach_group(domain, group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002576 if (ret) {
Lu Baolu7423e012019-05-25 13:41:22 +08002577 iommu_domain_free(domain);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002578 goto out;
2579 }
2580
Tom Murphyd127bc92019-08-26 05:48:21 +01002581 /* Make the domain the default for this group */
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002582 if (group->default_domain)
2583 iommu_domain_free(group->default_domain);
Lu Baolu7423e012019-05-25 13:41:22 +08002584 group->default_domain = domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002585
Joerg Roedelce574c22020-04-29 15:36:50 +02002586 iommu_create_device_direct_mappings(group, dev);
Jerry Snitselaard3602112019-12-10 11:56:06 -07002587
Lu Baolu7423e012019-05-25 13:41:22 +08002588 dev_info(dev, "Using iommu %s mapping\n",
2589 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002590
2591 ret = 0;
2592out:
2593 mutex_unlock(&group->mutex);
2594 iommu_group_put(group);
2595
2596 return ret;
2597}
Robin Murphy57f98d22016-09-13 10:54:14 +01002598
Lu Baolu7423e012019-05-25 13:41:22 +08002599/* Request that a device is direct mapped by the IOMMU */
2600int iommu_request_dm_for_dev(struct device *dev)
2601{
2602 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2603}
2604
2605/* Request that a device can't be direct mapped by the IOMMU */
2606int iommu_request_dma_domain_for_dev(struct device *dev)
2607{
2608 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2609}
2610
Joerg Roedel8a699612019-08-19 15:22:47 +02002611void iommu_set_default_passthrough(bool cmd_line)
2612{
2613 if (cmd_line)
2614 iommu_set_cmd_line_dma_api();
2615
2616 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2617}
2618
2619void iommu_set_default_translated(bool cmd_line)
2620{
2621 if (cmd_line)
2622 iommu_set_cmd_line_dma_api();
2623
2624 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2625}
2626
2627bool iommu_default_passthrough(void)
2628{
2629 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2630}
2631EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2632
Joerg Roedel534766d2017-01-31 16:58:42 +01002633const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002634{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002635 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002636 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002637
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002638 spin_lock(&iommu_device_lock);
2639 list_for_each_entry(iommu, &iommu_device_list, list)
2640 if (iommu->fwnode == fwnode) {
2641 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002642 break;
2643 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002644 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002645 return ops;
2646}
2647
Robin Murphy57f98d22016-09-13 10:54:14 +01002648int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2649 const struct iommu_ops *ops)
2650{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002651 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002652
2653 if (fwspec)
2654 return ops == fwspec->ops ? 0 : -EINVAL;
2655
Joerg Roedel72acd9d2020-03-26 16:08:31 +01002656 if (!dev_iommu_get(dev))
2657 return -ENOMEM;
2658
Robin Murphy098accf2020-02-13 14:00:21 +00002659 /* Preallocate for the overwhelmingly common case of 1 ID */
2660 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002661 if (!fwspec)
2662 return -ENOMEM;
2663
2664 of_node_get(to_of_node(iommu_fwnode));
2665 fwspec->iommu_fwnode = iommu_fwnode;
2666 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002667 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002668 return 0;
2669}
2670EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2671
2672void iommu_fwspec_free(struct device *dev)
2673{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002674 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002675
2676 if (fwspec) {
2677 fwnode_handle_put(fwspec->iommu_fwnode);
2678 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002679 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002680 }
2681}
2682EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2683
2684int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2685{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002686 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy098accf2020-02-13 14:00:21 +00002687 int i, new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002688
2689 if (!fwspec)
2690 return -EINVAL;
2691
Robin Murphy098accf2020-02-13 14:00:21 +00002692 new_num = fwspec->num_ids + num_ids;
2693 if (new_num > 1) {
2694 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2695 GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002696 if (!fwspec)
2697 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002698
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002699 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002700 }
2701
2702 for (i = 0; i < num_ids; i++)
2703 fwspec->ids[fwspec->num_ids + i] = ids[i];
2704
Robin Murphy098accf2020-02-13 14:00:21 +00002705 fwspec->num_ids = new_num;
Robin Murphy57f98d22016-09-13 10:54:14 +01002706 return 0;
2707}
2708EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002709
2710/*
2711 * Per device IOMMU features.
2712 */
2713bool iommu_dev_has_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_has_feat)
2718 return ops->dev_has_feat(dev, feat);
2719
2720 return false;
2721}
2722EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2723
2724int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2725{
2726 const struct iommu_ops *ops = dev->bus->iommu_ops;
2727
2728 if (ops && ops->dev_enable_feat)
2729 return ops->dev_enable_feat(dev, feat);
2730
2731 return -ENODEV;
2732}
2733EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2734
2735/*
2736 * The device drivers should do the necessary cleanups before calling this.
2737 * For example, before disabling the aux-domain feature, the device driver
2738 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2739 */
2740int iommu_dev_disable_feature(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_disable_feat)
2745 return ops->dev_disable_feat(dev, feat);
2746
2747 return -EBUSY;
2748}
2749EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2750
2751bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2752{
2753 const struct iommu_ops *ops = dev->bus->iommu_ops;
2754
2755 if (ops && ops->dev_feat_enabled)
2756 return ops->dev_feat_enabled(dev, feat);
2757
2758 return false;
2759}
2760EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2761
2762/*
2763 * Aux-domain specific attach/detach.
2764 *
2765 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2766 * true. Also, as long as domains are attached to a device through this
2767 * interface, any tries to call iommu_attach_device() should fail
2768 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2769 * This should make us safe against a device being attached to a guest as a
2770 * whole while there are still pasid users on it (aux and sva).
2771 */
2772int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2773{
2774 int ret = -ENODEV;
2775
2776 if (domain->ops->aux_attach_dev)
2777 ret = domain->ops->aux_attach_dev(domain, dev);
2778
2779 if (!ret)
2780 trace_attach_device_to_domain(dev);
2781
2782 return ret;
2783}
2784EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2785
2786void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2787{
2788 if (domain->ops->aux_detach_dev) {
2789 domain->ops->aux_detach_dev(domain, dev);
2790 trace_detach_device_from_domain(dev);
2791 }
2792}
2793EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2794
2795int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2796{
2797 int ret = -ENODEV;
2798
2799 if (domain->ops->aux_get_pasid)
2800 ret = domain->ops->aux_get_pasid(domain, dev);
2801
2802 return ret;
2803}
2804EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002805
2806/**
2807 * iommu_sva_bind_device() - Bind a process address space to a device
2808 * @dev: the device
2809 * @mm: the mm to bind, caller must hold a reference to it
2810 *
2811 * Create a bond between device and address space, allowing the device to access
2812 * the mm using the returned PASID. If a bond already exists between @device and
2813 * @mm, it is returned and an additional reference is taken. Caller must call
2814 * iommu_sva_unbind_device() to release each reference.
2815 *
2816 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2817 * initialize the required SVA features.
2818 *
2819 * On error, returns an ERR_PTR value.
2820 */
2821struct iommu_sva *
2822iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2823{
2824 struct iommu_group *group;
2825 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2826 const struct iommu_ops *ops = dev->bus->iommu_ops;
2827
2828 if (!ops || !ops->sva_bind)
2829 return ERR_PTR(-ENODEV);
2830
2831 group = iommu_group_get(dev);
2832 if (!group)
2833 return ERR_PTR(-ENODEV);
2834
2835 /* Ensure device count and domain don't change while we're binding */
2836 mutex_lock(&group->mutex);
2837
2838 /*
2839 * To keep things simple, SVA currently doesn't support IOMMU groups
2840 * with more than one device. Existing SVA-capable systems are not
2841 * affected by the problems that required IOMMU groups (lack of ACS
2842 * isolation, device ID aliasing and other hardware issues).
2843 */
2844 if (iommu_group_device_count(group) != 1)
2845 goto out_unlock;
2846
2847 handle = ops->sva_bind(dev, mm, drvdata);
2848
2849out_unlock:
2850 mutex_unlock(&group->mutex);
2851 iommu_group_put(group);
2852
2853 return handle;
2854}
2855EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2856
2857/**
2858 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2859 * @handle: the handle returned by iommu_sva_bind_device()
2860 *
2861 * Put reference to a bond between device and address space. The device should
2862 * not be issuing any more transaction for this PASID. All outstanding page
2863 * requests for this PASID must have been flushed to the IOMMU.
2864 *
2865 * Returns 0 on success, or an error value
2866 */
2867void iommu_sva_unbind_device(struct iommu_sva *handle)
2868{
2869 struct iommu_group *group;
2870 struct device *dev = handle->dev;
2871 const struct iommu_ops *ops = dev->bus->iommu_ops;
2872
2873 if (!ops || !ops->sva_unbind)
2874 return;
2875
2876 group = iommu_group_get(dev);
2877 if (!group)
2878 return;
2879
2880 mutex_lock(&group->mutex);
2881 ops->sva_unbind(handle);
2882 mutex_unlock(&group->mutex);
2883
2884 iommu_group_put(group);
2885}
2886EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2887
2888int iommu_sva_set_ops(struct iommu_sva *handle,
2889 const struct iommu_sva_ops *sva_ops)
2890{
2891 if (handle->ops && handle->ops != sva_ops)
2892 return -EEXIST;
2893
2894 handle->ops = sva_ops;
2895 return 0;
2896}
2897EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2898
2899int iommu_sva_get_pasid(struct iommu_sva *handle)
2900{
2901 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2902
2903 if (!ops || !ops->sva_get_pasid)
2904 return IOMMU_PASID_INVALID;
2905
2906 return ops->sva_get_pasid(handle);
2907}
2908EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);