blob: 0c674d80c37fd5768fa62296f6be72f97c5dd245 [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>
Shuah Khan7f6db172013-08-15 11:59:23 -060025#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010026
Alex Williamsond72e31c2012-05-30 14:18:53 -060027static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020028static DEFINE_IDA(iommu_group_ida);
Olof Johansson58d11312018-07-20 11:02:23 -070029#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
30static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
31#else
Will Deaconfccb4e32017-01-05 18:38:26 +000032static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Olof Johansson58d11312018-07-20 11:02:23 -070033#endif
Zhen Lei68a6efe2018-09-20 17:10:23 +010034static bool iommu_dma_strict __read_mostly = true;
Alex Williamsond72e31c2012-05-30 14:18:53 -060035
36struct iommu_group {
37 struct kobject kobj;
38 struct kobject *devices_kobj;
39 struct list_head devices;
40 struct mutex mutex;
41 struct blocking_notifier_head notifier;
42 void *iommu_data;
43 void (*iommu_data_release)(void *iommu_data);
44 char *name;
45 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020046 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020047 struct iommu_domain *domain;
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
Alex Williamsond72e31c2012-05-30 14:18:53 -060071#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
72struct iommu_group_attribute iommu_group_attr_##_name = \
73 __ATTR(_name, _mode, _show, _store)
74
75#define to_iommu_group_attr(_attr) \
76 container_of(_attr, struct iommu_group_attribute, attr)
77#define to_iommu_group(_kobj) \
78 container_of(_kobj, struct iommu_group, kobj)
79
Joerg Roedelb0119e82017-02-01 13:23:08 +010080static LIST_HEAD(iommu_device_list);
81static DEFINE_SPINLOCK(iommu_device_lock);
82
83int iommu_device_register(struct iommu_device *iommu)
84{
85 spin_lock(&iommu_device_lock);
86 list_add_tail(&iommu->list, &iommu_device_list);
87 spin_unlock(&iommu_device_lock);
88
89 return 0;
90}
91
92void iommu_device_unregister(struct iommu_device *iommu)
93{
94 spin_lock(&iommu_device_lock);
95 list_del(&iommu->list);
96 spin_unlock(&iommu_device_lock);
97}
98
Jacob Pan0c830e62019-06-03 15:57:48 +010099static struct iommu_param *iommu_get_dev_param(struct device *dev)
100{
101 struct iommu_param *param = dev->iommu_param;
102
103 if (param)
104 return param;
105
106 param = kzalloc(sizeof(*param), GFP_KERNEL);
107 if (!param)
108 return NULL;
109
110 mutex_init(&param->lock);
111 dev->iommu_param = param;
112 return param;
113}
114
115static void iommu_free_dev_param(struct device *dev)
116{
117 kfree(dev->iommu_param);
118 dev->iommu_param = NULL;
119}
120
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100121int iommu_probe_device(struct device *dev)
122{
123 const struct iommu_ops *ops = dev->bus->iommu_ops;
Jacob Pan0c830e62019-06-03 15:57:48 +0100124 int ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100125
126 WARN_ON(dev->iommu_group);
Jacob Pan0c830e62019-06-03 15:57:48 +0100127 if (!ops)
128 return -EINVAL;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100129
Jacob Pan0c830e62019-06-03 15:57:48 +0100130 if (!iommu_get_dev_param(dev))
131 return -ENOMEM;
132
133 ret = ops->add_device(dev);
134 if (ret)
135 iommu_free_dev_param(dev);
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100136
137 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100138}
139
140void iommu_release_device(struct device *dev)
141{
142 const struct iommu_ops *ops = dev->bus->iommu_ops;
143
144 if (dev->iommu_group)
145 ops->remove_device(dev);
Jacob Pan0c830e62019-06-03 15:57:48 +0100146
147 iommu_free_dev_param(dev);
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100148}
149
Joerg Roedel53723dc2015-05-28 18:41:29 +0200150static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
151 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200152static int __iommu_attach_device(struct iommu_domain *domain,
153 struct device *dev);
154static int __iommu_attach_group(struct iommu_domain *domain,
155 struct iommu_group *group);
156static void __iommu_detach_group(struct iommu_domain *domain,
157 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +0200158
Will Deaconfccb4e32017-01-05 18:38:26 +0000159static int __init iommu_set_def_domain_type(char *str)
160{
161 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300162 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000163
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300164 ret = kstrtobool(str, &pt);
165 if (ret)
166 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000167
168 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
169 return 0;
170}
171early_param("iommu.passthrough", iommu_set_def_domain_type);
172
Zhen Lei68a6efe2018-09-20 17:10:23 +0100173static int __init iommu_dma_setup(char *str)
174{
175 return kstrtobool(str, &iommu_dma_strict);
176}
177early_param("iommu.strict", iommu_dma_setup);
178
Alex Williamsond72e31c2012-05-30 14:18:53 -0600179static ssize_t iommu_group_attr_show(struct kobject *kobj,
180 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400181{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600182 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
183 struct iommu_group *group = to_iommu_group(kobj);
184 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400185
Alex Williamsond72e31c2012-05-30 14:18:53 -0600186 if (attr->show)
187 ret = attr->show(group, buf);
188 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400189}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600190
191static ssize_t iommu_group_attr_store(struct kobject *kobj,
192 struct attribute *__attr,
193 const char *buf, size_t count)
194{
195 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
196 struct iommu_group *group = to_iommu_group(kobj);
197 ssize_t ret = -EIO;
198
199 if (attr->store)
200 ret = attr->store(group, buf, count);
201 return ret;
202}
203
204static const struct sysfs_ops iommu_group_sysfs_ops = {
205 .show = iommu_group_attr_show,
206 .store = iommu_group_attr_store,
207};
208
209static int iommu_group_create_file(struct iommu_group *group,
210 struct iommu_group_attribute *attr)
211{
212 return sysfs_create_file(&group->kobj, &attr->attr);
213}
214
215static void iommu_group_remove_file(struct iommu_group *group,
216 struct iommu_group_attribute *attr)
217{
218 sysfs_remove_file(&group->kobj, &attr->attr);
219}
220
221static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
222{
223 return sprintf(buf, "%s\n", group->name);
224}
225
Eric Auger6c65fb32017-01-19 20:57:51 +0000226/**
227 * iommu_insert_resv_region - Insert a new region in the
228 * list of reserved regions.
229 * @new: new region to insert
230 * @regions: list of regions
231 *
232 * The new element is sorted by address with respect to the other
233 * regions of the same type. In case it overlaps with another
234 * region of the same type, regions are merged. In case it
235 * overlaps with another region of different type, regions are
236 * not merged.
237 */
238static int iommu_insert_resv_region(struct iommu_resv_region *new,
239 struct list_head *regions)
240{
241 struct iommu_resv_region *region;
242 phys_addr_t start = new->start;
243 phys_addr_t end = new->start + new->length - 1;
244 struct list_head *pos = regions->next;
245
246 while (pos != regions) {
247 struct iommu_resv_region *entry =
248 list_entry(pos, struct iommu_resv_region, list);
249 phys_addr_t a = entry->start;
250 phys_addr_t b = entry->start + entry->length - 1;
251 int type = entry->type;
252
253 if (end < a) {
254 goto insert;
255 } else if (start > b) {
256 pos = pos->next;
257 } else if ((start >= a) && (end <= b)) {
258 if (new->type == type)
Eric Augerad0834d2019-06-03 08:53:30 +0200259 return 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000260 else
261 pos = pos->next;
262 } else {
263 if (new->type == type) {
264 phys_addr_t new_start = min(a, start);
265 phys_addr_t new_end = max(b, end);
Eric Augerad0834d2019-06-03 08:53:30 +0200266 int ret;
Eric Auger6c65fb32017-01-19 20:57:51 +0000267
268 list_del(&entry->list);
269 entry->start = new_start;
270 entry->length = new_end - new_start + 1;
Eric Augerad0834d2019-06-03 08:53:30 +0200271 ret = iommu_insert_resv_region(entry, regions);
272 kfree(entry);
273 return ret;
Eric Auger6c65fb32017-01-19 20:57:51 +0000274 } else {
275 pos = pos->next;
276 }
277 }
278 }
279insert:
280 region = iommu_alloc_resv_region(new->start, new->length,
281 new->prot, new->type);
282 if (!region)
283 return -ENOMEM;
284
285 list_add_tail(&region->list, pos);
Eric Auger6c65fb32017-01-19 20:57:51 +0000286 return 0;
287}
288
289static int
290iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
291 struct list_head *group_resv_regions)
292{
293 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100294 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000295
296 list_for_each_entry(entry, dev_resv_regions, list) {
297 ret = iommu_insert_resv_region(entry, group_resv_regions);
298 if (ret)
299 break;
300 }
301 return ret;
302}
303
304int iommu_get_group_resv_regions(struct iommu_group *group,
305 struct list_head *head)
306{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100307 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000308 int ret = 0;
309
310 mutex_lock(&group->mutex);
311 list_for_each_entry(device, &group->devices, list) {
312 struct list_head dev_resv_regions;
313
314 INIT_LIST_HEAD(&dev_resv_regions);
315 iommu_get_resv_regions(device->dev, &dev_resv_regions);
316 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
317 iommu_put_resv_regions(device->dev, &dev_resv_regions);
318 if (ret)
319 break;
320 }
321 mutex_unlock(&group->mutex);
322 return ret;
323}
324EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
325
Eric Augerbc7d12b92017-01-19 20:57:52 +0000326static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
327 char *buf)
328{
329 struct iommu_resv_region *region, *next;
330 struct list_head group_resv_regions;
331 char *str = buf;
332
333 INIT_LIST_HEAD(&group_resv_regions);
334 iommu_get_group_resv_regions(group, &group_resv_regions);
335
336 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
337 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
338 (long long int)region->start,
339 (long long int)(region->start +
340 region->length - 1),
341 iommu_group_resv_type_string[region->type]);
342 kfree(region);
343 }
344
345 return (str - buf);
346}
347
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700348static ssize_t iommu_group_show_type(struct iommu_group *group,
349 char *buf)
350{
351 char *type = "unknown\n";
352
353 if (group->default_domain) {
354 switch (group->default_domain->type) {
355 case IOMMU_DOMAIN_BLOCKED:
356 type = "blocked\n";
357 break;
358 case IOMMU_DOMAIN_IDENTITY:
359 type = "identity\n";
360 break;
361 case IOMMU_DOMAIN_UNMANAGED:
362 type = "unmanaged\n";
363 break;
364 case IOMMU_DOMAIN_DMA:
Lu Baolu24f307d2019-05-24 14:30:56 +0800365 type = "DMA\n";
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700366 break;
367 }
368 }
369 strcpy(buf, type);
370
371 return strlen(type);
372}
373
Alex Williamsond72e31c2012-05-30 14:18:53 -0600374static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
375
Eric Augerbc7d12b92017-01-19 20:57:52 +0000376static IOMMU_GROUP_ATTR(reserved_regions, 0444,
377 iommu_group_show_resv_regions, NULL);
378
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700379static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
380
Alex Williamsond72e31c2012-05-30 14:18:53 -0600381static void iommu_group_release(struct kobject *kobj)
382{
383 struct iommu_group *group = to_iommu_group(kobj);
384
Joerg Roedel269aa802015-05-28 18:41:25 +0200385 pr_debug("Releasing group %d\n", group->id);
386
Alex Williamsond72e31c2012-05-30 14:18:53 -0600387 if (group->iommu_data_release)
388 group->iommu_data_release(group->iommu_data);
389
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200390 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600391
Joerg Roedel53723dc2015-05-28 18:41:29 +0200392 if (group->default_domain)
393 iommu_domain_free(group->default_domain);
394
Alex Williamsond72e31c2012-05-30 14:18:53 -0600395 kfree(group->name);
396 kfree(group);
397}
398
399static struct kobj_type iommu_group_ktype = {
400 .sysfs_ops = &iommu_group_sysfs_ops,
401 .release = iommu_group_release,
402};
403
404/**
405 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600406 *
407 * This function is called by an iommu driver to allocate a new iommu
408 * group. The iommu group represents the minimum granularity of the iommu.
409 * Upon successful return, the caller holds a reference to the supplied
410 * group in order to hold the group until devices are added. Use
411 * iommu_group_put() to release this extra reference count, allowing the
412 * group to be automatically reclaimed once it has no devices or external
413 * references.
414 */
415struct iommu_group *iommu_group_alloc(void)
416{
417 struct iommu_group *group;
418 int ret;
419
420 group = kzalloc(sizeof(*group), GFP_KERNEL);
421 if (!group)
422 return ERR_PTR(-ENOMEM);
423
424 group->kobj.kset = iommu_group_kset;
425 mutex_init(&group->mutex);
426 INIT_LIST_HEAD(&group->devices);
427 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
428
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200429 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
430 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600431 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200432 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600433 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200434 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600435
436 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
437 NULL, "%d", group->id);
438 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200439 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600440 kfree(group);
441 return ERR_PTR(ret);
442 }
443
444 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
445 if (!group->devices_kobj) {
446 kobject_put(&group->kobj); /* triggers .release & free */
447 return ERR_PTR(-ENOMEM);
448 }
449
450 /*
451 * The devices_kobj holds a reference on the group kobject, so
452 * as long as that exists so will the group. We can therefore
453 * use the devices_kobj for reference counting.
454 */
455 kobject_put(&group->kobj);
456
Eric Augerbc7d12b92017-01-19 20:57:52 +0000457 ret = iommu_group_create_file(group,
458 &iommu_group_attr_reserved_regions);
459 if (ret)
460 return ERR_PTR(ret);
461
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700462 ret = iommu_group_create_file(group, &iommu_group_attr_type);
463 if (ret)
464 return ERR_PTR(ret);
465
Joerg Roedel269aa802015-05-28 18:41:25 +0200466 pr_debug("Allocated group %d\n", group->id);
467
Alex Williamsond72e31c2012-05-30 14:18:53 -0600468 return group;
469}
470EXPORT_SYMBOL_GPL(iommu_group_alloc);
471
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100472struct iommu_group *iommu_group_get_by_id(int id)
473{
474 struct kobject *group_kobj;
475 struct iommu_group *group;
476 const char *name;
477
478 if (!iommu_group_kset)
479 return NULL;
480
481 name = kasprintf(GFP_KERNEL, "%d", id);
482 if (!name)
483 return NULL;
484
485 group_kobj = kset_find_obj(iommu_group_kset, name);
486 kfree(name);
487
488 if (!group_kobj)
489 return NULL;
490
491 group = container_of(group_kobj, struct iommu_group, kobj);
492 BUG_ON(group->id != id);
493
494 kobject_get(group->devices_kobj);
495 kobject_put(&group->kobj);
496
497 return group;
498}
499EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
500
Alex Williamsond72e31c2012-05-30 14:18:53 -0600501/**
502 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
503 * @group: the group
504 *
505 * iommu drivers can store data in the group for use when doing iommu
506 * operations. This function provides a way to retrieve it. Caller
507 * should hold a group reference.
508 */
509void *iommu_group_get_iommudata(struct iommu_group *group)
510{
511 return group->iommu_data;
512}
513EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
514
515/**
516 * iommu_group_set_iommudata - set iommu_data for a group
517 * @group: the group
518 * @iommu_data: new data
519 * @release: release function for iommu_data
520 *
521 * iommu drivers can store data in the group for use when doing iommu
522 * operations. This function provides a way to set the data after
523 * the group has been allocated. Caller should hold a group reference.
524 */
525void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
526 void (*release)(void *iommu_data))
527{
528 group->iommu_data = iommu_data;
529 group->iommu_data_release = release;
530}
531EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
532
533/**
534 * iommu_group_set_name - set name for a group
535 * @group: the group
536 * @name: name
537 *
538 * Allow iommu driver to set a name for a group. When set it will
539 * appear in a name attribute file under the group in sysfs.
540 */
541int iommu_group_set_name(struct iommu_group *group, const char *name)
542{
543 int ret;
544
545 if (group->name) {
546 iommu_group_remove_file(group, &iommu_group_attr_name);
547 kfree(group->name);
548 group->name = NULL;
549 if (!name)
550 return 0;
551 }
552
553 group->name = kstrdup(name, GFP_KERNEL);
554 if (!group->name)
555 return -ENOMEM;
556
557 ret = iommu_group_create_file(group, &iommu_group_attr_name);
558 if (ret) {
559 kfree(group->name);
560 group->name = NULL;
561 return ret;
562 }
563
564 return 0;
565}
566EXPORT_SYMBOL_GPL(iommu_group_set_name);
567
Joerg Roedelbeed2822015-05-28 18:41:34 +0200568static int iommu_group_create_direct_mappings(struct iommu_group *group,
569 struct device *dev)
570{
571 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000572 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200573 struct list_head mappings;
574 unsigned long pg_size;
575 int ret = 0;
576
577 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
578 return 0;
579
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100580 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200581
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100582 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200583 INIT_LIST_HEAD(&mappings);
584
Eric Augere5b52342017-01-19 20:57:47 +0000585 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200586
587 /* We need to consider overlapping regions for different devices */
588 list_for_each_entry(entry, &mappings, list) {
589 dma_addr_t start, end, addr;
590
Eric Augere5b52342017-01-19 20:57:47 +0000591 if (domain->ops->apply_resv_region)
592 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200593
Joerg Roedelbeed2822015-05-28 18:41:34 +0200594 start = ALIGN(entry->start, pg_size);
595 end = ALIGN(entry->start + entry->length, pg_size);
596
Eric Augeradfd3732019-06-03 08:53:35 +0200597 if (entry->type != IOMMU_RESV_DIRECT &&
598 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
Eric Auger544a25d2017-01-19 20:57:50 +0000599 continue;
600
Joerg Roedelbeed2822015-05-28 18:41:34 +0200601 for (addr = start; addr < end; addr += pg_size) {
602 phys_addr_t phys_addr;
603
604 phys_addr = iommu_iova_to_phys(domain, addr);
605 if (phys_addr)
606 continue;
607
608 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
609 if (ret)
610 goto out;
611 }
612
613 }
614
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200615 iommu_flush_tlb_all(domain);
616
Joerg Roedelbeed2822015-05-28 18:41:34 +0200617out:
Eric Augere5b52342017-01-19 20:57:47 +0000618 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200619
620 return ret;
621}
622
Alex Williamsond72e31c2012-05-30 14:18:53 -0600623/**
624 * iommu_group_add_device - add a device to an iommu group
625 * @group: the group into which to add the device (reference should be held)
626 * @dev: the device
627 *
628 * This function is called by an iommu driver to add a device into a
629 * group. Adding a device increments the group reference count.
630 */
631int iommu_group_add_device(struct iommu_group *group, struct device *dev)
632{
633 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100634 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600635
636 device = kzalloc(sizeof(*device), GFP_KERNEL);
637 if (!device)
638 return -ENOMEM;
639
640 device->dev = dev;
641
642 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000643 if (ret)
644 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600645
646 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
647rename:
648 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000649 ret = -ENOMEM;
650 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600651 }
652
653 ret = sysfs_create_link_nowarn(group->devices_kobj,
654 &dev->kobj, device->name);
655 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600656 if (ret == -EEXIST && i >= 0) {
657 /*
658 * Account for the slim chance of collision
659 * and append an instance to the name.
660 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000661 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600662 device->name = kasprintf(GFP_KERNEL, "%s.%d",
663 kobject_name(&dev->kobj), i++);
664 goto rename;
665 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000666 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600667 }
668
669 kobject_get(group->devices_kobj);
670
671 dev->iommu_group = group;
672
Joerg Roedelbeed2822015-05-28 18:41:34 +0200673 iommu_group_create_direct_mappings(group, dev);
674
Alex Williamsond72e31c2012-05-30 14:18:53 -0600675 mutex_lock(&group->mutex);
676 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200677 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000678 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600679 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000680 if (ret)
681 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600682
683 /* Notify any listeners about change to group. */
684 blocking_notifier_call_chain(&group->notifier,
685 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600686
687 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200688
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600689 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200690
Alex Williamsond72e31c2012-05-30 14:18:53 -0600691 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000692
693err_put_group:
694 mutex_lock(&group->mutex);
695 list_del(&device->list);
696 mutex_unlock(&group->mutex);
697 dev->iommu_group = NULL;
698 kobject_put(group->devices_kobj);
699err_free_name:
700 kfree(device->name);
701err_remove_link:
702 sysfs_remove_link(&dev->kobj, "iommu_group");
703err_free_device:
704 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600705 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000706 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600707}
708EXPORT_SYMBOL_GPL(iommu_group_add_device);
709
710/**
711 * iommu_group_remove_device - remove a device from it's current group
712 * @dev: device to be removed
713 *
714 * This function is called by an iommu driver to remove the device from
715 * it's current group. This decrements the iommu group reference count.
716 */
717void iommu_group_remove_device(struct device *dev)
718{
719 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100720 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600721
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600722 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200723
Alex Williamsond72e31c2012-05-30 14:18:53 -0600724 /* Pre-notify listeners that a device is being removed. */
725 blocking_notifier_call_chain(&group->notifier,
726 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
727
728 mutex_lock(&group->mutex);
729 list_for_each_entry(tmp_device, &group->devices, list) {
730 if (tmp_device->dev == dev) {
731 device = tmp_device;
732 list_del(&device->list);
733 break;
734 }
735 }
736 mutex_unlock(&group->mutex);
737
738 if (!device)
739 return;
740
741 sysfs_remove_link(group->devices_kobj, device->name);
742 sysfs_remove_link(&dev->kobj, "iommu_group");
743
Shuah Khan2e757082013-08-15 11:59:25 -0600744 trace_remove_device_from_group(group->id, dev);
745
Alex Williamsond72e31c2012-05-30 14:18:53 -0600746 kfree(device->name);
747 kfree(device);
748 dev->iommu_group = NULL;
749 kobject_put(group->devices_kobj);
750}
751EXPORT_SYMBOL_GPL(iommu_group_remove_device);
752
Joerg Roedel426a2732015-05-28 18:41:30 +0200753static int iommu_group_device_count(struct iommu_group *group)
754{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100755 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200756 int ret = 0;
757
758 list_for_each_entry(entry, &group->devices, list)
759 ret++;
760
761 return ret;
762}
763
Alex Williamsond72e31c2012-05-30 14:18:53 -0600764/**
765 * iommu_group_for_each_dev - iterate over each device in the group
766 * @group: the group
767 * @data: caller opaque data to be passed to callback function
768 * @fn: caller supplied callback function
769 *
770 * This function is called by group users to iterate over group devices.
771 * Callers should hold a reference count to the group during callback.
772 * The group->mutex is held across callbacks, which will block calls to
773 * iommu_group_add/remove_device.
774 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200775static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
776 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600777{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100778 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600779 int ret = 0;
780
Alex Williamsond72e31c2012-05-30 14:18:53 -0600781 list_for_each_entry(device, &group->devices, list) {
782 ret = fn(device->dev, data);
783 if (ret)
784 break;
785 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200786 return ret;
787}
788
789
790int iommu_group_for_each_dev(struct iommu_group *group, void *data,
791 int (*fn)(struct device *, void *))
792{
793 int ret;
794
795 mutex_lock(&group->mutex);
796 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600797 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200798
Alex Williamsond72e31c2012-05-30 14:18:53 -0600799 return ret;
800}
801EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
802
803/**
804 * iommu_group_get - Return the group for a device and increment reference
805 * @dev: get the group that this device belongs to
806 *
807 * This function is called by iommu drivers and users to get the group
808 * for the specified device. If found, the group is returned and the group
809 * reference in incremented, else NULL.
810 */
811struct iommu_group *iommu_group_get(struct device *dev)
812{
813 struct iommu_group *group = dev->iommu_group;
814
815 if (group)
816 kobject_get(group->devices_kobj);
817
818 return group;
819}
820EXPORT_SYMBOL_GPL(iommu_group_get);
821
822/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000823 * iommu_group_ref_get - Increment reference on a group
824 * @group: the group to use, must not be NULL
825 *
826 * This function is called by iommu drivers to take additional references on an
827 * existing group. Returns the given group for convenience.
828 */
829struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
830{
831 kobject_get(group->devices_kobj);
832 return group;
833}
834
835/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600836 * iommu_group_put - Decrement group reference
837 * @group: the group to use
838 *
839 * This function is called by iommu drivers and users to release the
840 * iommu group. Once the reference count is zero, the group is released.
841 */
842void iommu_group_put(struct iommu_group *group)
843{
844 if (group)
845 kobject_put(group->devices_kobj);
846}
847EXPORT_SYMBOL_GPL(iommu_group_put);
848
849/**
850 * iommu_group_register_notifier - Register a notifier for group changes
851 * @group: the group to watch
852 * @nb: notifier block to signal
853 *
854 * This function allows iommu group users to track changes in a group.
855 * See include/linux/iommu.h for actions sent via this notifier. Caller
856 * should hold a reference to the group throughout notifier registration.
857 */
858int iommu_group_register_notifier(struct iommu_group *group,
859 struct notifier_block *nb)
860{
861 return blocking_notifier_chain_register(&group->notifier, nb);
862}
863EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
864
865/**
866 * iommu_group_unregister_notifier - Unregister a notifier
867 * @group: the group to watch
868 * @nb: notifier block to signal
869 *
870 * Unregister a previously registered group notifier block.
871 */
872int iommu_group_unregister_notifier(struct iommu_group *group,
873 struct notifier_block *nb)
874{
875 return blocking_notifier_chain_unregister(&group->notifier, nb);
876}
877EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
878
879/**
Jacob Pan0c830e62019-06-03 15:57:48 +0100880 * iommu_register_device_fault_handler() - Register a device fault handler
881 * @dev: the device
882 * @handler: the fault handler
883 * @data: private data passed as argument to the handler
884 *
885 * When an IOMMU fault event is received, this handler gets called with the
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100886 * fault event and data as argument. The handler should return 0 on success. If
887 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
888 * complete the fault by calling iommu_page_response() with one of the following
889 * response code:
890 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
891 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
892 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
893 * page faults if possible.
Jacob Pan0c830e62019-06-03 15:57:48 +0100894 *
895 * Return 0 if the fault handler was installed successfully, or an error.
896 */
897int iommu_register_device_fault_handler(struct device *dev,
898 iommu_dev_fault_handler_t handler,
899 void *data)
900{
901 struct iommu_param *param = dev->iommu_param;
902 int ret = 0;
903
904 if (!param)
905 return -EINVAL;
906
907 mutex_lock(&param->lock);
908 /* Only allow one fault handler registered for each device */
909 if (param->fault_param) {
910 ret = -EBUSY;
911 goto done_unlock;
912 }
913
914 get_device(dev);
915 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
916 if (!param->fault_param) {
917 put_device(dev);
918 ret = -ENOMEM;
919 goto done_unlock;
920 }
921 param->fault_param->handler = handler;
922 param->fault_param->data = data;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100923 mutex_init(&param->fault_param->lock);
924 INIT_LIST_HEAD(&param->fault_param->faults);
Jacob Pan0c830e62019-06-03 15:57:48 +0100925
926done_unlock:
927 mutex_unlock(&param->lock);
928
929 return ret;
930}
931EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
932
933/**
934 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
935 * @dev: the device
936 *
937 * Remove the device fault handler installed with
938 * iommu_register_device_fault_handler().
939 *
940 * Return 0 on success, or an error.
941 */
942int iommu_unregister_device_fault_handler(struct device *dev)
943{
944 struct iommu_param *param = dev->iommu_param;
945 int ret = 0;
946
947 if (!param)
948 return -EINVAL;
949
950 mutex_lock(&param->lock);
951
952 if (!param->fault_param)
953 goto unlock;
954
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100955 /* we cannot unregister handler if there are pending faults */
956 if (!list_empty(&param->fault_param->faults)) {
957 ret = -EBUSY;
958 goto unlock;
959 }
960
Jacob Pan0c830e62019-06-03 15:57:48 +0100961 kfree(param->fault_param);
962 param->fault_param = NULL;
963 put_device(dev);
964unlock:
965 mutex_unlock(&param->lock);
966
967 return ret;
968}
969EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
970
971/**
972 * iommu_report_device_fault() - Report fault event to device driver
973 * @dev: the device
974 * @evt: fault event data
975 *
976 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100977 * handler. When this function fails and the fault is recoverable, it is the
978 * caller's responsibility to complete the fault.
Jacob Pan0c830e62019-06-03 15:57:48 +0100979 *
980 * Return 0 on success, or an error.
981 */
982int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
983{
984 struct iommu_param *param = dev->iommu_param;
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100985 struct iommu_fault_event *evt_pending = NULL;
Jacob Pan0c830e62019-06-03 15:57:48 +0100986 struct iommu_fault_param *fparam;
987 int ret = 0;
988
989 if (!param || !evt)
990 return -EINVAL;
991
992 /* we only report device fault if there is a handler registered */
993 mutex_lock(&param->lock);
994 fparam = param->fault_param;
995 if (!fparam || !fparam->handler) {
996 ret = -EINVAL;
997 goto done_unlock;
998 }
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +0100999
1000 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1001 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1002 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1003 GFP_KERNEL);
1004 if (!evt_pending) {
1005 ret = -ENOMEM;
1006 goto done_unlock;
1007 }
1008 mutex_lock(&fparam->lock);
1009 list_add_tail(&evt_pending->list, &fparam->faults);
1010 mutex_unlock(&fparam->lock);
1011 }
1012
Jacob Pan0c830e62019-06-03 15:57:48 +01001013 ret = fparam->handler(&evt->fault, fparam->data);
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001014 if (ret && evt_pending) {
1015 mutex_lock(&fparam->lock);
1016 list_del(&evt_pending->list);
1017 mutex_unlock(&fparam->lock);
1018 kfree(evt_pending);
1019 }
Jacob Pan0c830e62019-06-03 15:57:48 +01001020done_unlock:
1021 mutex_unlock(&param->lock);
1022 return ret;
1023}
1024EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1025
Jean-Philippe Bruckerbf3255b2019-06-03 15:57:49 +01001026int iommu_page_response(struct device *dev,
1027 struct iommu_page_response *msg)
1028{
1029 bool pasid_valid;
1030 int ret = -EINVAL;
1031 struct iommu_fault_event *evt;
1032 struct iommu_fault_page_request *prm;
1033 struct iommu_param *param = dev->iommu_param;
1034 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1035
1036 if (!domain || !domain->ops->page_response)
1037 return -ENODEV;
1038
1039 if (!param || !param->fault_param)
1040 return -EINVAL;
1041
1042 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1043 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1044 return -EINVAL;
1045
1046 /* Only send response if there is a fault report pending */
1047 mutex_lock(&param->fault_param->lock);
1048 if (list_empty(&param->fault_param->faults)) {
1049 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1050 goto done_unlock;
1051 }
1052 /*
1053 * Check if we have a matching page request pending to respond,
1054 * otherwise return -EINVAL
1055 */
1056 list_for_each_entry(evt, &param->fault_param->faults, list) {
1057 prm = &evt->fault.prm;
1058 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1059
1060 if ((pasid_valid && prm->pasid != msg->pasid) ||
1061 prm->grpid != msg->grpid)
1062 continue;
1063
1064 /* Sanitize the reply */
1065 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1066
1067 ret = domain->ops->page_response(dev, evt, msg);
1068 list_del(&evt->list);
1069 kfree(evt);
1070 break;
1071 }
1072
1073done_unlock:
1074 mutex_unlock(&param->fault_param->lock);
1075 return ret;
1076}
1077EXPORT_SYMBOL_GPL(iommu_page_response);
1078
Jacob Pan0c830e62019-06-03 15:57:48 +01001079/**
Alex Williamsond72e31c2012-05-30 14:18:53 -06001080 * iommu_group_id - Return ID for a group
1081 * @group: the group to ID
1082 *
1083 * Return the unique ID for the group matching the sysfs group number.
1084 */
1085int iommu_group_id(struct iommu_group *group)
1086{
1087 return group->id;
1088}
1089EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -04001090
Alex Williamsonf096c062014-09-19 10:03:06 -06001091static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1092 unsigned long *devfns);
1093
Alex Williamson104a1c12014-07-03 09:51:18 -06001094/*
1095 * To consider a PCI device isolated, we require ACS to support Source
1096 * Validation, Request Redirection, Completer Redirection, and Upstream
1097 * Forwarding. This effectively means that devices cannot spoof their
1098 * requester ID, requests and completions cannot be redirected, and all
1099 * transactions are forwarded upstream, even as it passes through a
1100 * bridge where the target device is downstream.
1101 */
1102#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1103
Alex Williamsonf096c062014-09-19 10:03:06 -06001104/*
1105 * For multifunction devices which are not isolated from each other, find
1106 * all the other non-isolated functions and look for existing groups. For
1107 * each function, we also need to look for aliases to or from other devices
1108 * that may already have a group.
1109 */
1110static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1111 unsigned long *devfns)
1112{
1113 struct pci_dev *tmp = NULL;
1114 struct iommu_group *group;
1115
1116 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1117 return NULL;
1118
1119 for_each_pci_dev(tmp) {
1120 if (tmp == pdev || tmp->bus != pdev->bus ||
1121 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1122 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1123 continue;
1124
1125 group = get_pci_alias_group(tmp, devfns);
1126 if (group) {
1127 pci_dev_put(tmp);
1128 return group;
1129 }
1130 }
1131
1132 return NULL;
1133}
1134
1135/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001136 * Look for aliases to or from the given device for existing groups. DMA
1137 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -06001138 * space is quite small (especially since we're really only looking at pcie
1139 * device, and therefore only expect multiple slots on the root complex or
1140 * downstream switch ports). It's conceivable though that a pair of
1141 * multifunction devices could have aliases between them that would cause a
1142 * loop. To prevent this, we use a bitmap to track where we've been.
1143 */
1144static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1145 unsigned long *devfns)
1146{
1147 struct pci_dev *tmp = NULL;
1148 struct iommu_group *group;
1149
1150 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1151 return NULL;
1152
1153 group = iommu_group_get(&pdev->dev);
1154 if (group)
1155 return group;
1156
1157 for_each_pci_dev(tmp) {
1158 if (tmp == pdev || tmp->bus != pdev->bus)
1159 continue;
1160
1161 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +01001162 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -06001163 group = get_pci_alias_group(tmp, devfns);
1164 if (group) {
1165 pci_dev_put(tmp);
1166 return group;
1167 }
1168
1169 group = get_pci_function_alias_group(tmp, devfns);
1170 if (group) {
1171 pci_dev_put(tmp);
1172 return group;
1173 }
1174 }
1175 }
1176
1177 return NULL;
1178}
1179
Alex Williamson104a1c12014-07-03 09:51:18 -06001180struct group_for_pci_data {
1181 struct pci_dev *pdev;
1182 struct iommu_group *group;
1183};
1184
1185/*
1186 * DMA alias iterator callback, return the last seen device. Stop and return
1187 * the IOMMU group if we find one along the way.
1188 */
1189static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1190{
1191 struct group_for_pci_data *data = opaque;
1192
1193 data->pdev = pdev;
1194 data->group = iommu_group_get(&pdev->dev);
1195
1196 return data->group != NULL;
1197}
1198
1199/*
Joerg Roedel6eab5562015-10-21 23:51:38 +02001200 * Generic device_group call-back function. It just allocates one
1201 * iommu-group per device.
1202 */
1203struct iommu_group *generic_device_group(struct device *dev)
1204{
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001205 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +02001206}
1207
1208/*
Alex Williamson104a1c12014-07-03 09:51:18 -06001209 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1210 * to find or create an IOMMU group for a device.
1211 */
Joerg Roedel5e622922015-10-21 23:51:37 +02001212struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -06001213{
Joerg Roedel5e622922015-10-21 23:51:37 +02001214 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001215 struct group_for_pci_data data;
1216 struct pci_bus *bus;
1217 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -06001218 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -06001219
Joerg Roedel5e622922015-10-21 23:51:37 +02001220 if (WARN_ON(!dev_is_pci(dev)))
1221 return ERR_PTR(-EINVAL);
1222
Alex Williamson104a1c12014-07-03 09:51:18 -06001223 /*
1224 * Find the upstream DMA alias for the device. A device must not
1225 * be aliased due to topology in order to have its own IOMMU group.
1226 * If we find an alias along the way that already belongs to a
1227 * group, use it.
1228 */
1229 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1230 return data.group;
1231
1232 pdev = data.pdev;
1233
1234 /*
1235 * Continue upstream from the point of minimum IOMMU granularity
1236 * due to aliases to the point where devices are protected from
1237 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1238 * group, use it.
1239 */
1240 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1241 if (!bus->self)
1242 continue;
1243
1244 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1245 break;
1246
1247 pdev = bus->self;
1248
1249 group = iommu_group_get(&pdev->dev);
1250 if (group)
1251 return group;
1252 }
1253
1254 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001255 * Look for existing groups on device aliases. If we alias another
1256 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001257 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001258 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1259 if (group)
1260 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001261
1262 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001263 * Look for existing groups on non-isolated functions on the same
1264 * slot and aliases of those funcions, if any. No need to clear
1265 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001266 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001267 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1268 if (group)
1269 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001270
1271 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001272 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001273}
1274
Nipun Guptaeab03e22018-09-10 19:19:18 +05301275/* Get the IOMMU group for device on fsl-mc bus */
1276struct iommu_group *fsl_mc_device_group(struct device *dev)
1277{
1278 struct device *cont_dev = fsl_mc_cont_dev(dev);
1279 struct iommu_group *group;
1280
1281 group = iommu_group_get(cont_dev);
1282 if (!group)
1283 group = iommu_group_alloc();
1284 return group;
1285}
1286
Alex Williamson104a1c12014-07-03 09:51:18 -06001287/**
1288 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1289 * @dev: target device
1290 *
1291 * This function is intended to be called by IOMMU drivers and extended to
1292 * support common, bus-defined algorithms when determining or creating the
1293 * IOMMU group for a device. On success, the caller will hold a reference
1294 * to the returned IOMMU group, which will already include the provided
1295 * device. The reference should be released with iommu_group_put().
1296 */
1297struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1298{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001299 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001300 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001301 int ret;
1302
1303 group = iommu_group_get(dev);
1304 if (group)
1305 return group;
1306
Robin Murphy05f803002017-07-21 13:12:38 +01001307 if (!ops)
1308 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001309
Robin Murphy05f803002017-07-21 13:12:38 +01001310 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001311 if (WARN_ON_ONCE(group == NULL))
1312 return ERR_PTR(-EINVAL);
1313
Alex Williamson104a1c12014-07-03 09:51:18 -06001314 if (IS_ERR(group))
1315 return group;
1316
Joerg Roedel12282362015-10-21 23:51:43 +02001317 /*
1318 * Try to allocate a default domain - needs support from the
1319 * IOMMU driver.
1320 */
1321 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001322 struct iommu_domain *dom;
1323
1324 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1325 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001326 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
Joerg Roedel8bc32a22019-03-22 16:52:17 +01001327 if (dom) {
1328 dev_warn(dev,
1329 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1330 iommu_def_domain_type);
1331 }
Will Deaconfccb4e32017-01-05 18:38:26 +00001332 }
1333
1334 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001335 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001336 group->domain = dom;
Zhen Lei68a6efe2018-09-20 17:10:23 +01001337
1338 if (dom && !iommu_dma_strict) {
1339 int attr = 1;
1340 iommu_domain_set_attr(dom,
1341 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1342 &attr);
1343 }
Joerg Roedel12282362015-10-21 23:51:43 +02001344 }
1345
Alex Williamson104a1c12014-07-03 09:51:18 -06001346 ret = iommu_group_add_device(group, dev);
1347 if (ret) {
1348 iommu_group_put(group);
1349 return ERR_PTR(ret);
1350 }
1351
1352 return group;
1353}
1354
Joerg Roedel6827ca82015-05-28 18:41:35 +02001355struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1356{
1357 return group->default_domain;
1358}
1359
Alex Williamson14604322011-10-21 15:56:05 -04001360static int add_iommu_group(struct device *dev, void *data)
1361{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001362 int ret = iommu_probe_device(dev);
Joerg Roedel38667f12015-06-29 10:16:08 +02001363
1364 /*
1365 * We ignore -ENODEV errors for now, as they just mean that the
1366 * device is not translated by an IOMMU. We still care about
1367 * other errors and fail to initialize when they happen.
1368 */
1369 if (ret == -ENODEV)
1370 ret = 0;
1371
1372 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001373}
1374
Joerg Roedel8da30142015-05-28 18:41:27 +02001375static int remove_iommu_group(struct device *dev, void *data)
1376{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001377 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001378
1379 return 0;
1380}
1381
Alex Williamsond72e31c2012-05-30 14:18:53 -06001382static int iommu_bus_notifier(struct notifier_block *nb,
1383 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001384{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001385 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001386 struct device *dev = data;
1387 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001388
Alex Williamsond72e31c2012-05-30 14:18:53 -06001389 /*
1390 * ADD/DEL call into iommu driver ops if provided, which may
1391 * result in ADD/DEL notifiers to group->notifier
1392 */
1393 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001394 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001395
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001396 ret = iommu_probe_device(dev);
1397 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001398 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001399 iommu_release_device(dev);
1400 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001401 }
Alex Williamson14604322011-10-21 15:56:05 -04001402
Alex Williamsond72e31c2012-05-30 14:18:53 -06001403 /*
1404 * Remaining BUS_NOTIFYs get filtered and republished to the
1405 * group, if anyone is listening
1406 */
1407 group = iommu_group_get(dev);
1408 if (!group)
1409 return 0;
1410
1411 switch (action) {
1412 case BUS_NOTIFY_BIND_DRIVER:
1413 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1414 break;
1415 case BUS_NOTIFY_BOUND_DRIVER:
1416 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1417 break;
1418 case BUS_NOTIFY_UNBIND_DRIVER:
1419 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1420 break;
1421 case BUS_NOTIFY_UNBOUND_DRIVER:
1422 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1423 break;
1424 }
1425
1426 if (group_action)
1427 blocking_notifier_call_chain(&group->notifier,
1428 group_action, dev);
1429
1430 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001431 return 0;
1432}
1433
Mark Salterfb3e3062014-09-21 13:58:24 -04001434static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001435{
Mark Salterfb3e3062014-09-21 13:58:24 -04001436 int err;
1437 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001438
Mark Salterfb3e3062014-09-21 13:58:24 -04001439 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1440 if (!nb)
1441 return -ENOMEM;
1442
1443 nb->notifier_call = iommu_bus_notifier;
1444
1445 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001446 if (err)
1447 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001448
Lu Baolu8cec63e2019-03-20 09:40:24 +08001449 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001450 if (err)
1451 goto out_err;
1452
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001453
1454 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001455
1456out_err:
1457 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001458 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001459 bus_unregister_notifier(bus, nb);
1460
1461out_free:
1462 kfree(nb);
1463
1464 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001465}
1466
Joerg Roedelff217762011-08-26 16:48:26 +02001467/**
1468 * bus_set_iommu - set iommu-callbacks for the bus
1469 * @bus: bus.
1470 * @ops: the callbacks provided by the iommu-driver
1471 *
1472 * This function is called by an iommu driver to set the iommu methods
1473 * used for a particular bus. Drivers for devices on that bus can use
1474 * the iommu-api after these ops are registered.
1475 * This special function is needed because IOMMUs are usually devices on
1476 * the bus itself, so the iommu drivers are not initialized when the bus
1477 * is set up. With this function the iommu-driver can set the iommu-ops
1478 * afterwards.
1479 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001480int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001481{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001482 int err;
1483
Joerg Roedelff217762011-08-26 16:48:26 +02001484 if (bus->iommu_ops != NULL)
1485 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001486
Joerg Roedelff217762011-08-26 16:48:26 +02001487 bus->iommu_ops = ops;
1488
1489 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001490 err = iommu_bus_init(bus, ops);
1491 if (err)
1492 bus->iommu_ops = NULL;
1493
1494 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001495}
Joerg Roedelff217762011-08-26 16:48:26 +02001496EXPORT_SYMBOL_GPL(bus_set_iommu);
1497
Joerg Roedela1b60c12011-09-06 18:46:34 +02001498bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001499{
Joerg Roedel94441c32011-09-06 18:58:54 +02001500 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001501}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001502EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001503
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001504bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1505{
1506 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1507 return false;
1508
1509 return bus->iommu_ops->capable(cap);
1510}
1511EXPORT_SYMBOL_GPL(iommu_capable);
1512
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001513/**
1514 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1515 * @domain: iommu domain
1516 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001517 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001518 *
1519 * This function should be used by IOMMU users which want to be notified
1520 * whenever an IOMMU fault happens.
1521 *
1522 * The fault handler itself should return 0 on success, and an appropriate
1523 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001524 */
1525void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001526 iommu_fault_handler_t handler,
1527 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001528{
1529 BUG_ON(!domain);
1530
1531 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001532 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001533}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001534EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001535
Joerg Roedel53723dc2015-05-28 18:41:29 +02001536static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1537 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001538{
1539 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001540
Joerg Roedel94441c32011-09-06 18:58:54 +02001541 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001542 return NULL;
1543
Joerg Roedel53723dc2015-05-28 18:41:29 +02001544 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001545 if (!domain)
1546 return NULL;
1547
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001548 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001549 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001550 /* Assume all sizes by default; the driver may override this later */
1551 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001552
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001553 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001554}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001555
Joerg Roedel53723dc2015-05-28 18:41:29 +02001556struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1557{
1558 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001559}
1560EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1561
1562void iommu_domain_free(struct iommu_domain *domain)
1563{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001564 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001565}
1566EXPORT_SYMBOL_GPL(iommu_domain_free);
1567
Joerg Roedel426a2732015-05-28 18:41:30 +02001568static int __iommu_attach_device(struct iommu_domain *domain,
1569 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001570{
Shuah Khanb54db772013-08-15 11:59:26 -06001571 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001572 if ((domain->ops->is_attach_deferred != NULL) &&
1573 domain->ops->is_attach_deferred(domain, dev))
1574 return 0;
1575
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001576 if (unlikely(domain->ops->attach_dev == NULL))
1577 return -ENODEV;
1578
Shuah Khanb54db772013-08-15 11:59:26 -06001579 ret = domain->ops->attach_dev(domain, dev);
1580 if (!ret)
1581 trace_attach_device_to_domain(dev);
1582 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001583}
Joerg Roedel426a2732015-05-28 18:41:30 +02001584
1585int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1586{
1587 struct iommu_group *group;
1588 int ret;
1589
1590 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001591 if (!group)
1592 return -ENODEV;
1593
Joerg Roedel426a2732015-05-28 18:41:30 +02001594 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001595 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001596 * change while we are attaching
1597 */
1598 mutex_lock(&group->mutex);
1599 ret = -EINVAL;
1600 if (iommu_group_device_count(group) != 1)
1601 goto out_unlock;
1602
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001603 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001604
1605out_unlock:
1606 mutex_unlock(&group->mutex);
1607 iommu_group_put(group);
1608
1609 return ret;
1610}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001611EXPORT_SYMBOL_GPL(iommu_attach_device);
1612
Joerg Roedel426a2732015-05-28 18:41:30 +02001613static void __iommu_detach_device(struct iommu_domain *domain,
1614 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001615{
Baoquan Hee01d1912017-08-09 16:33:40 +08001616 if ((domain->ops->is_attach_deferred != NULL) &&
1617 domain->ops->is_attach_deferred(domain, dev))
1618 return;
1619
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001620 if (unlikely(domain->ops->detach_dev == NULL))
1621 return;
1622
1623 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001624 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001625}
Joerg Roedel426a2732015-05-28 18:41:30 +02001626
1627void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1628{
1629 struct iommu_group *group;
1630
1631 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001632 if (!group)
1633 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001634
1635 mutex_lock(&group->mutex);
1636 if (iommu_group_device_count(group) != 1) {
1637 WARN_ON(1);
1638 goto out_unlock;
1639 }
1640
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001641 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001642
1643out_unlock:
1644 mutex_unlock(&group->mutex);
1645 iommu_group_put(group);
1646}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001647EXPORT_SYMBOL_GPL(iommu_detach_device);
1648
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001649struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1650{
1651 struct iommu_domain *domain;
1652 struct iommu_group *group;
1653
1654 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01001655 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001656 return NULL;
1657
1658 domain = group->domain;
1659
1660 iommu_group_put(group);
1661
1662 return domain;
1663}
1664EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1665
Alex Williamsond72e31c2012-05-30 14:18:53 -06001666/*
Robin Murphy6af588f2018-09-12 16:24:12 +01001667 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1668 * guarantees that the group and its default domain are valid and correct.
1669 */
1670struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1671{
1672 return dev->iommu_group->default_domain;
1673}
1674
1675/*
Rami Rosen35449ad2018-09-18 17:38:49 +03001676 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06001677 * the IOMMU API works on domains and devices. Bridge that gap by
1678 * iterating over the devices in a group. Ideally we'd have a single
1679 * device which represents the requestor ID of the group, but we also
1680 * allow IOMMU drivers to create policy defined minimum sets, where
1681 * the physical hardware may be able to distiguish members, but we
1682 * wish to group them at a higher level (ex. untrusted multi-function
1683 * PCI devices). Thus we attach each device.
1684 */
1685static int iommu_group_do_attach_device(struct device *dev, void *data)
1686{
1687 struct iommu_domain *domain = data;
1688
Joerg Roedel426a2732015-05-28 18:41:30 +02001689 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001690}
1691
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001692static int __iommu_attach_group(struct iommu_domain *domain,
1693 struct iommu_group *group)
1694{
1695 int ret;
1696
1697 if (group->default_domain && group->domain != group->default_domain)
1698 return -EBUSY;
1699
1700 ret = __iommu_group_for_each_dev(group, domain,
1701 iommu_group_do_attach_device);
1702 if (ret == 0)
1703 group->domain = domain;
1704
1705 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001706}
1707
1708int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1709{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001710 int ret;
1711
1712 mutex_lock(&group->mutex);
1713 ret = __iommu_attach_group(domain, group);
1714 mutex_unlock(&group->mutex);
1715
1716 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001717}
1718EXPORT_SYMBOL_GPL(iommu_attach_group);
1719
1720static int iommu_group_do_detach_device(struct device *dev, void *data)
1721{
1722 struct iommu_domain *domain = data;
1723
Joerg Roedel426a2732015-05-28 18:41:30 +02001724 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001725
1726 return 0;
1727}
1728
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001729static void __iommu_detach_group(struct iommu_domain *domain,
1730 struct iommu_group *group)
1731{
1732 int ret;
1733
1734 if (!group->default_domain) {
1735 __iommu_group_for_each_dev(group, domain,
1736 iommu_group_do_detach_device);
1737 group->domain = NULL;
1738 return;
1739 }
1740
1741 if (group->domain == group->default_domain)
1742 return;
1743
1744 /* Detach by re-attaching to the default domain */
1745 ret = __iommu_group_for_each_dev(group, group->default_domain,
1746 iommu_group_do_attach_device);
1747 if (ret != 0)
1748 WARN_ON(1);
1749 else
1750 group->domain = group->default_domain;
1751}
1752
Alex Williamsond72e31c2012-05-30 14:18:53 -06001753void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1754{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001755 mutex_lock(&group->mutex);
1756 __iommu_detach_group(domain, group);
1757 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001758}
1759EXPORT_SYMBOL_GPL(iommu_detach_group);
1760
Varun Sethibb5547a2013-03-29 01:23:58 +05301761phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001762{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001763 if (unlikely(domain->ops->iova_to_phys == NULL))
1764 return 0;
1765
1766 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001767}
1768EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001769
Alex Williamsonbd139692013-06-17 19:57:34 -06001770static size_t iommu_pgsize(struct iommu_domain *domain,
1771 unsigned long addr_merge, size_t size)
1772{
1773 unsigned int pgsize_idx;
1774 size_t pgsize;
1775
1776 /* Max page size that still fits into 'size' */
1777 pgsize_idx = __fls(size);
1778
1779 /* need to consider alignment requirements ? */
1780 if (likely(addr_merge)) {
1781 /* Max page size allowed by address */
1782 unsigned int align_pgsize_idx = __ffs(addr_merge);
1783 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1784 }
1785
1786 /* build a mask of acceptable page sizes */
1787 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1788
1789 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001790 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001791
1792 /* make sure we're still sane */
1793 BUG_ON(!pgsize);
1794
1795 /* pick the biggest page */
1796 pgsize_idx = __fls(pgsize);
1797 pgsize = 1UL << pgsize_idx;
1798
1799 return pgsize;
1800}
1801
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001802int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001803 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001804{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001805 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001806 unsigned long orig_iova = iova;
1807 unsigned int min_pagesz;
1808 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001809 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001810 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001811
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001812 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001813 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001814 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001815
Joerg Roedela10315e2015-03-26 13:43:06 +01001816 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1817 return -EINVAL;
1818
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001819 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001820 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001821
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001822 /*
1823 * both the virtual address and the physical one, as well as
1824 * the size of the mapping, must be aligned (at least) to the
1825 * size of the smallest page supported by the hardware
1826 */
1827 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001828 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001829 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001830 return -EINVAL;
1831 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001832
Fabio Estevamabedb042013-08-22 10:25:42 -03001833 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001834
1835 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001836 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001837
Fabio Estevamabedb042013-08-22 10:25:42 -03001838 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001839 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001840
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001841 ret = ops->map(domain, iova, paddr, pgsize, prot);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001842 if (ret)
1843 break;
1844
1845 iova += pgsize;
1846 paddr += pgsize;
1847 size -= pgsize;
1848 }
1849
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001850 if (ops->iotlb_sync_map)
1851 ops->iotlb_sync_map(domain);
1852
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001853 /* unroll mapping in case something went wrong */
1854 if (ret)
1855 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001856 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001857 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001858
1859 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001860}
1861EXPORT_SYMBOL_GPL(iommu_map);
1862
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001863static size_t __iommu_unmap(struct iommu_domain *domain,
1864 unsigned long iova, size_t size,
1865 bool sync)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001866{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001867 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001868 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001869 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001870 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001871
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001872 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001873 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001874 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001875
Joerg Roedela10315e2015-03-26 13:43:06 +01001876 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001877 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01001878
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001879 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001880 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001881
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001882 /*
1883 * The virtual address, as well as the size of the mapping, must be
1884 * aligned (at least) to the size of the smallest page supported
1885 * by the hardware
1886 */
1887 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001888 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1889 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001890 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001891 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001892
Joe Perches6197ca82013-06-23 12:29:04 -07001893 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001894
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001895 /*
1896 * Keep iterating until we either unmap 'size' bytes (or more)
1897 * or we hit an area that isn't mapped.
1898 */
1899 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001900 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001901
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001902 unmapped_page = ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001903 if (!unmapped_page)
1904 break;
1905
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001906 if (sync && ops->iotlb_range_add)
1907 ops->iotlb_range_add(domain, iova, pgsize);
1908
Joe Perches6197ca82013-06-23 12:29:04 -07001909 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1910 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001911
1912 iova += unmapped_page;
1913 unmapped += unmapped_page;
1914 }
1915
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001916 if (sync && ops->iotlb_sync)
1917 ops->iotlb_sync(domain);
1918
Shuah Khandb8614d2015-01-16 20:53:17 -07001919 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001920 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001921}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001922
1923size_t iommu_unmap(struct iommu_domain *domain,
1924 unsigned long iova, size_t size)
1925{
1926 return __iommu_unmap(domain, iova, size, true);
1927}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001928EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001929
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001930size_t iommu_unmap_fast(struct iommu_domain *domain,
1931 unsigned long iova, size_t size)
1932{
1933 return __iommu_unmap(domain, iova, size, false);
1934}
1935EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1936
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001937size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1938 struct scatterlist *sg, unsigned int nents, int prot)
Olav Haugan315786e2014-10-25 09:55:16 -07001939{
Robin Murphy5d95f402018-10-11 16:56:42 +01001940 size_t len = 0, mapped = 0;
1941 phys_addr_t start;
1942 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001943 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001944
Robin Murphy5d95f402018-10-11 16:56:42 +01001945 while (i <= nents) {
1946 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07001947
Robin Murphy5d95f402018-10-11 16:56:42 +01001948 if (len && s_phys != start + len) {
1949 ret = iommu_map(domain, iova + mapped, start, len, prot);
1950 if (ret)
1951 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00001952
Robin Murphy5d95f402018-10-11 16:56:42 +01001953 mapped += len;
1954 len = 0;
1955 }
Robin Murphy18f23402014-11-25 17:50:55 +00001956
Robin Murphy5d95f402018-10-11 16:56:42 +01001957 if (len) {
1958 len += sg->length;
1959 } else {
1960 len = sg->length;
1961 start = s_phys;
1962 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01001963
Robin Murphy5d95f402018-10-11 16:56:42 +01001964 if (++i < nents)
1965 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07001966 }
1967
1968 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001969
1970out_err:
1971 /* undo mappings already done */
1972 iommu_unmap(domain, iova, mapped);
1973
1974 return 0;
1975
Olav Haugan315786e2014-10-25 09:55:16 -07001976}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001977EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001978
1979int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301980 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001981{
1982 if (unlikely(domain->ops->domain_window_enable == NULL))
1983 return -ENODEV;
1984
Varun Sethi80f97f02013-03-29 01:24:00 +05301985 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1986 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001987}
1988EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1989
1990void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1991{
1992 if (unlikely(domain->ops->domain_window_disable == NULL))
1993 return;
1994
1995 return domain->ops->domain_window_disable(domain, wnd_nr);
1996}
1997EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1998
Joerg Roedel207c6e32017-04-26 15:39:28 +02001999/**
2000 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2001 * @domain: the iommu domain where the fault has happened
2002 * @dev: the device where the fault has happened
2003 * @iova: the faulting address
2004 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2005 *
2006 * This function should be called by the low-level IOMMU implementations
2007 * whenever IOMMU faults happen, to allow high-level users, that are
2008 * interested in such events, to know about them.
2009 *
2010 * This event may be useful for several possible use cases:
2011 * - mere logging of the event
2012 * - dynamic TLB/PTE loading
2013 * - if restarting of the faulting device is required
2014 *
2015 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2016 * PTE/TLB loading will one day be supported, implementations will be able
2017 * to tell whether it succeeded or not according to this return value).
2018 *
2019 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2020 * (though fault handlers can also return -ENOSYS, in case they want to
2021 * elicit the default behavior of the IOMMU drivers).
2022 */
2023int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2024 unsigned long iova, int flags)
2025{
2026 int ret = -ENOSYS;
2027
2028 /*
2029 * if upper layers showed interest and installed a fault handler,
2030 * invoke it.
2031 */
2032 if (domain->handler)
2033 ret = domain->handler(domain, dev, iova, flags,
2034 domain->handler_token);
2035
2036 trace_io_page_fault(dev, iova, flags);
2037 return ret;
2038}
2039EXPORT_SYMBOL_GPL(report_iommu_fault);
2040
Alex Williamsond72e31c2012-05-30 14:18:53 -06002041static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04002042{
Alex Williamsond72e31c2012-05-30 14:18:53 -06002043 iommu_group_kset = kset_create_and_add("iommu_groups",
2044 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06002045 BUG_ON(!iommu_group_kset);
2046
Gary R Hookbad614b2018-06-12 16:41:21 -05002047 iommu_debugfs_setup();
2048
Alex Williamsond72e31c2012-05-30 14:18:53 -06002049 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04002050}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02002051core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002052
2053int iommu_domain_get_attr(struct iommu_domain *domain,
2054 enum iommu_attr attr, void *data)
2055{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002056 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002057 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002058 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002059
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002060 switch (attr) {
2061 case DOMAIN_ATTR_GEOMETRY:
2062 geometry = data;
2063 *geometry = domain->geometry;
2064
2065 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01002066 case DOMAIN_ATTR_PAGING:
2067 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01002068 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01002069 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01002070 default:
2071 if (!domain->ops->domain_get_attr)
2072 return -EINVAL;
2073
2074 ret = domain->ops->domain_get_attr(domain, attr, data);
2075 }
2076
2077 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002078}
2079EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2080
2081int iommu_domain_set_attr(struct iommu_domain *domain,
2082 enum iommu_attr attr, void *data)
2083{
Joerg Roedel69356712013-02-04 14:00:01 +01002084 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002085
Joerg Roedel69356712013-02-04 14:00:01 +01002086 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01002087 default:
2088 if (domain->ops->domain_set_attr == NULL)
2089 return -EINVAL;
2090
2091 ret = domain->ops->domain_set_attr(domain, attr, data);
2092 }
2093
2094 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01002095}
2096EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02002097
Eric Augere5b52342017-01-19 20:57:47 +00002098void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002099{
2100 const struct iommu_ops *ops = dev->bus->iommu_ops;
2101
Eric Augere5b52342017-01-19 20:57:47 +00002102 if (ops && ops->get_resv_regions)
2103 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002104}
2105
Eric Augere5b52342017-01-19 20:57:47 +00002106void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02002107{
2108 const struct iommu_ops *ops = dev->bus->iommu_ops;
2109
Eric Augere5b52342017-01-19 20:57:47 +00002110 if (ops && ops->put_resv_regions)
2111 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02002112}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002113
Eric Auger2b20cbb2017-01-19 20:57:49 +00002114struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00002115 size_t length, int prot,
2116 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00002117{
2118 struct iommu_resv_region *region;
2119
2120 region = kzalloc(sizeof(*region), GFP_KERNEL);
2121 if (!region)
2122 return NULL;
2123
2124 INIT_LIST_HEAD(&region->list);
2125 region->start = start;
2126 region->length = length;
2127 region->prot = prot;
2128 region->type = type;
2129 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01002130}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002131
Lu Baolu7423e012019-05-25 13:41:22 +08002132static int
2133request_default_domain_for_dev(struct device *dev, unsigned long type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002134{
Lu Baolu7423e012019-05-25 13:41:22 +08002135 struct iommu_domain *domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002136 struct iommu_group *group;
2137 int ret;
2138
2139 /* Device must already be in a group before calling this function */
Lu Baolu57274ea2019-05-21 15:27:35 +08002140 group = iommu_group_get(dev);
2141 if (!group)
2142 return -EINVAL;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002143
2144 mutex_lock(&group->mutex);
2145
2146 /* Check if the default domain is already direct mapped */
2147 ret = 0;
Lu Baolu7423e012019-05-25 13:41:22 +08002148 if (group->default_domain && group->default_domain->type == type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002149 goto out;
2150
2151 /* Don't change mappings of existing devices */
2152 ret = -EBUSY;
2153 if (iommu_group_device_count(group) != 1)
2154 goto out;
2155
2156 /* Allocate a direct mapped domain */
2157 ret = -ENOMEM;
Lu Baolu7423e012019-05-25 13:41:22 +08002158 domain = __iommu_domain_alloc(dev->bus, type);
2159 if (!domain)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002160 goto out;
2161
2162 /* Attach the device to the domain */
Lu Baolu7423e012019-05-25 13:41:22 +08002163 ret = __iommu_attach_group(domain, group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002164 if (ret) {
Lu Baolu7423e012019-05-25 13:41:22 +08002165 iommu_domain_free(domain);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002166 goto out;
2167 }
2168
Lu Baolu7423e012019-05-25 13:41:22 +08002169 iommu_group_create_direct_mappings(group, dev);
2170
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002171 /* Make the direct mapped domain the default for this group */
2172 if (group->default_domain)
2173 iommu_domain_free(group->default_domain);
Lu Baolu7423e012019-05-25 13:41:22 +08002174 group->default_domain = domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002175
Lu Baolu7423e012019-05-25 13:41:22 +08002176 dev_info(dev, "Using iommu %s mapping\n",
2177 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
Joerg Roedeld290f1e2015-05-28 18:41:36 +02002178
2179 ret = 0;
2180out:
2181 mutex_unlock(&group->mutex);
2182 iommu_group_put(group);
2183
2184 return ret;
2185}
Robin Murphy57f98d22016-09-13 10:54:14 +01002186
Lu Baolu7423e012019-05-25 13:41:22 +08002187/* Request that a device is direct mapped by the IOMMU */
2188int iommu_request_dm_for_dev(struct device *dev)
2189{
2190 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2191}
2192
2193/* Request that a device can't be direct mapped by the IOMMU */
2194int iommu_request_dma_domain_for_dev(struct device *dev)
2195{
2196 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2197}
2198
Joerg Roedel534766d2017-01-31 16:58:42 +01002199const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002200{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002201 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002202 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002203
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002204 spin_lock(&iommu_device_lock);
2205 list_for_each_entry(iommu, &iommu_device_list, list)
2206 if (iommu->fwnode == fwnode) {
2207 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002208 break;
2209 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01002210 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00002211 return ops;
2212}
2213
Robin Murphy57f98d22016-09-13 10:54:14 +01002214int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2215 const struct iommu_ops *ops)
2216{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002217 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002218
2219 if (fwspec)
2220 return ops == fwspec->ops ? 0 : -EINVAL;
2221
2222 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2223 if (!fwspec)
2224 return -ENOMEM;
2225
2226 of_node_get(to_of_node(iommu_fwnode));
2227 fwspec->iommu_fwnode = iommu_fwnode;
2228 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002229 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2233
2234void iommu_fwspec_free(struct device *dev)
2235{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002236 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002237
2238 if (fwspec) {
2239 fwnode_handle_put(fwspec->iommu_fwnode);
2240 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002241 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002242 }
2243}
2244EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2245
2246int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2247{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002248 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002249 size_t size;
2250 int i;
2251
2252 if (!fwspec)
2253 return -EINVAL;
2254
2255 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2256 if (size > sizeof(*fwspec)) {
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002257 fwspec = krealloc(fwspec, size, GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002258 if (!fwspec)
2259 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002260
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002261 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002262 }
2263
2264 for (i = 0; i < num_ids; i++)
2265 fwspec->ids[fwspec->num_ids + i] = ids[i];
2266
2267 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01002268 return 0;
2269}
2270EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002271
2272/*
2273 * Per device IOMMU features.
2274 */
2275bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2276{
2277 const struct iommu_ops *ops = dev->bus->iommu_ops;
2278
2279 if (ops && ops->dev_has_feat)
2280 return ops->dev_has_feat(dev, feat);
2281
2282 return false;
2283}
2284EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2285
2286int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2287{
2288 const struct iommu_ops *ops = dev->bus->iommu_ops;
2289
2290 if (ops && ops->dev_enable_feat)
2291 return ops->dev_enable_feat(dev, feat);
2292
2293 return -ENODEV;
2294}
2295EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2296
2297/*
2298 * The device drivers should do the necessary cleanups before calling this.
2299 * For example, before disabling the aux-domain feature, the device driver
2300 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2301 */
2302int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2303{
2304 const struct iommu_ops *ops = dev->bus->iommu_ops;
2305
2306 if (ops && ops->dev_disable_feat)
2307 return ops->dev_disable_feat(dev, feat);
2308
2309 return -EBUSY;
2310}
2311EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2312
2313bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2314{
2315 const struct iommu_ops *ops = dev->bus->iommu_ops;
2316
2317 if (ops && ops->dev_feat_enabled)
2318 return ops->dev_feat_enabled(dev, feat);
2319
2320 return false;
2321}
2322EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2323
2324/*
2325 * Aux-domain specific attach/detach.
2326 *
2327 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2328 * true. Also, as long as domains are attached to a device through this
2329 * interface, any tries to call iommu_attach_device() should fail
2330 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2331 * This should make us safe against a device being attached to a guest as a
2332 * whole while there are still pasid users on it (aux and sva).
2333 */
2334int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2335{
2336 int ret = -ENODEV;
2337
2338 if (domain->ops->aux_attach_dev)
2339 ret = domain->ops->aux_attach_dev(domain, dev);
2340
2341 if (!ret)
2342 trace_attach_device_to_domain(dev);
2343
2344 return ret;
2345}
2346EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2347
2348void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2349{
2350 if (domain->ops->aux_detach_dev) {
2351 domain->ops->aux_detach_dev(domain, dev);
2352 trace_detach_device_from_domain(dev);
2353 }
2354}
2355EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2356
2357int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2358{
2359 int ret = -ENODEV;
2360
2361 if (domain->ops->aux_get_pasid)
2362 ret = domain->ops->aux_get_pasid(domain, dev);
2363
2364 return ret;
2365}
2366EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002367
2368/**
2369 * iommu_sva_bind_device() - Bind a process address space to a device
2370 * @dev: the device
2371 * @mm: the mm to bind, caller must hold a reference to it
2372 *
2373 * Create a bond between device and address space, allowing the device to access
2374 * the mm using the returned PASID. If a bond already exists between @device and
2375 * @mm, it is returned and an additional reference is taken. Caller must call
2376 * iommu_sva_unbind_device() to release each reference.
2377 *
2378 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2379 * initialize the required SVA features.
2380 *
2381 * On error, returns an ERR_PTR value.
2382 */
2383struct iommu_sva *
2384iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2385{
2386 struct iommu_group *group;
2387 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2388 const struct iommu_ops *ops = dev->bus->iommu_ops;
2389
2390 if (!ops || !ops->sva_bind)
2391 return ERR_PTR(-ENODEV);
2392
2393 group = iommu_group_get(dev);
2394 if (!group)
2395 return ERR_PTR(-ENODEV);
2396
2397 /* Ensure device count and domain don't change while we're binding */
2398 mutex_lock(&group->mutex);
2399
2400 /*
2401 * To keep things simple, SVA currently doesn't support IOMMU groups
2402 * with more than one device. Existing SVA-capable systems are not
2403 * affected by the problems that required IOMMU groups (lack of ACS
2404 * isolation, device ID aliasing and other hardware issues).
2405 */
2406 if (iommu_group_device_count(group) != 1)
2407 goto out_unlock;
2408
2409 handle = ops->sva_bind(dev, mm, drvdata);
2410
2411out_unlock:
2412 mutex_unlock(&group->mutex);
2413 iommu_group_put(group);
2414
2415 return handle;
2416}
2417EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2418
2419/**
2420 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2421 * @handle: the handle returned by iommu_sva_bind_device()
2422 *
2423 * Put reference to a bond between device and address space. The device should
2424 * not be issuing any more transaction for this PASID. All outstanding page
2425 * requests for this PASID must have been flushed to the IOMMU.
2426 *
2427 * Returns 0 on success, or an error value
2428 */
2429void iommu_sva_unbind_device(struct iommu_sva *handle)
2430{
2431 struct iommu_group *group;
2432 struct device *dev = handle->dev;
2433 const struct iommu_ops *ops = dev->bus->iommu_ops;
2434
2435 if (!ops || !ops->sva_unbind)
2436 return;
2437
2438 group = iommu_group_get(dev);
2439 if (!group)
2440 return;
2441
2442 mutex_lock(&group->mutex);
2443 ops->sva_unbind(handle);
2444 mutex_unlock(&group->mutex);
2445
2446 iommu_group_put(group);
2447}
2448EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2449
2450int iommu_sva_set_ops(struct iommu_sva *handle,
2451 const struct iommu_sva_ops *sva_ops)
2452{
2453 if (handle->ops && handle->ops != sva_ops)
2454 return -EEXIST;
2455
2456 handle->ops = sva_ops;
2457 return 0;
2458}
2459EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2460
2461int iommu_sva_get_pasid(struct iommu_sva *handle)
2462{
2463 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2464
2465 if (!ops || !ops->sva_get_pasid)
2466 return IOMMU_PASID_INVALID;
2467
2468 return ops->sva_get_pasid(handle);
2469}
2470EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);