blob: ba0661744a3d9373dd5607cd4e102469f9a4dca1 [file] [log] [blame]
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelfc2100e2008-11-26 17:21:24 +01004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Joerg Roedel92e70662015-05-28 18:41:24 +020019#define pr_fmt(fmt) "iommu: " fmt
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020020
Joerg Roedel905d66c2011-09-06 16:03:26 +020021#include <linux/device.h>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040022#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010023#include <linux/bug.h>
24#include <linux/types.h>
Paul Gortmakerc1af7b42018-12-01 14:19:09 -050025#include <linux/init.h>
26#include <linux/export.h>
Andrew Morton60db4022009-05-06 16:03:07 -070027#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010028#include <linux/errno.h>
29#include <linux/iommu.h>
Alex Williamsond72e31c2012-05-30 14:18:53 -060030#include <linux/idr.h>
31#include <linux/notifier.h>
32#include <linux/err.h>
Alex Williamson104a1c12014-07-03 09:51:18 -060033#include <linux/pci.h>
Alex Williamsonf096c062014-09-19 10:03:06 -060034#include <linux/bitops.h>
Robin Murphy57f98d22016-09-13 10:54:14 +010035#include <linux/property.h>
Nipun Guptaeab03e22018-09-10 19:19:18 +053036#include <linux/fsl/mc.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060037#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010038
Alex Williamsond72e31c2012-05-30 14:18:53 -060039static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020040static DEFINE_IDA(iommu_group_ida);
Olof Johansson58d11312018-07-20 11:02:23 -070041#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
42static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
43#else
Will Deaconfccb4e32017-01-05 18:38:26 +000044static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Olof Johansson58d11312018-07-20 11:02:23 -070045#endif
Zhen Lei68a6efe2018-09-20 17:10:23 +010046static bool iommu_dma_strict __read_mostly = true;
Alex Williamsond72e31c2012-05-30 14:18:53 -060047
48struct iommu_group {
49 struct kobject kobj;
50 struct kobject *devices_kobj;
51 struct list_head devices;
52 struct mutex mutex;
53 struct blocking_notifier_head notifier;
54 void *iommu_data;
55 void (*iommu_data_release)(void *iommu_data);
56 char *name;
57 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020058 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020059 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060060};
61
Joerg Roedelc09e22d2017-02-01 12:19:46 +010062struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060063 struct list_head list;
64 struct device *dev;
65 char *name;
66};
67
68struct iommu_group_attribute {
69 struct attribute attr;
70 ssize_t (*show)(struct iommu_group *group, char *buf);
71 ssize_t (*store)(struct iommu_group *group,
72 const char *buf, size_t count);
73};
74
Eric Augerbc7d12b92017-01-19 20:57:52 +000075static const char * const iommu_group_resv_type_string[] = {
76 [IOMMU_RESV_DIRECT] = "direct",
77 [IOMMU_RESV_RESERVED] = "reserved",
78 [IOMMU_RESV_MSI] = "msi",
Robin Murphy9d3a4de2017-03-16 17:00:16 +000079 [IOMMU_RESV_SW_MSI] = "msi",
Eric Augerbc7d12b92017-01-19 20:57:52 +000080};
81
Alex Williamsond72e31c2012-05-30 14:18:53 -060082#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
83struct iommu_group_attribute iommu_group_attr_##_name = \
84 __ATTR(_name, _mode, _show, _store)
85
86#define to_iommu_group_attr(_attr) \
87 container_of(_attr, struct iommu_group_attribute, attr)
88#define to_iommu_group(_kobj) \
89 container_of(_kobj, struct iommu_group, kobj)
90
Joerg Roedelb0119e82017-02-01 13:23:08 +010091static LIST_HEAD(iommu_device_list);
92static DEFINE_SPINLOCK(iommu_device_lock);
93
94int iommu_device_register(struct iommu_device *iommu)
95{
96 spin_lock(&iommu_device_lock);
97 list_add_tail(&iommu->list, &iommu_device_list);
98 spin_unlock(&iommu_device_lock);
99
100 return 0;
101}
102
103void iommu_device_unregister(struct iommu_device *iommu)
104{
105 spin_lock(&iommu_device_lock);
106 list_del(&iommu->list);
107 spin_unlock(&iommu_device_lock);
108}
109
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100110int iommu_probe_device(struct device *dev)
111{
112 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100113 int ret = -EINVAL;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100114
115 WARN_ON(dev->iommu_group);
116
Joerg Roedeldc9de8a2018-12-20 10:02:20 +0100117 if (ops)
118 ret = ops->add_device(dev);
119
120 return ret;
Joerg Roedelcc5aed42018-11-30 10:31:59 +0100121}
122
123void iommu_release_device(struct device *dev)
124{
125 const struct iommu_ops *ops = dev->bus->iommu_ops;
126
127 if (dev->iommu_group)
128 ops->remove_device(dev);
129}
130
Joerg Roedel53723dc2015-05-28 18:41:29 +0200131static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
132 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200133static int __iommu_attach_device(struct iommu_domain *domain,
134 struct device *dev);
135static int __iommu_attach_group(struct iommu_domain *domain,
136 struct iommu_group *group);
137static void __iommu_detach_group(struct iommu_domain *domain,
138 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +0200139
Will Deaconfccb4e32017-01-05 18:38:26 +0000140static int __init iommu_set_def_domain_type(char *str)
141{
142 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300143 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000144
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300145 ret = kstrtobool(str, &pt);
146 if (ret)
147 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000148
149 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
150 return 0;
151}
152early_param("iommu.passthrough", iommu_set_def_domain_type);
153
Zhen Lei68a6efe2018-09-20 17:10:23 +0100154static int __init iommu_dma_setup(char *str)
155{
156 return kstrtobool(str, &iommu_dma_strict);
157}
158early_param("iommu.strict", iommu_dma_setup);
159
Alex Williamsond72e31c2012-05-30 14:18:53 -0600160static ssize_t iommu_group_attr_show(struct kobject *kobj,
161 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400162{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600163 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
164 struct iommu_group *group = to_iommu_group(kobj);
165 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400166
Alex Williamsond72e31c2012-05-30 14:18:53 -0600167 if (attr->show)
168 ret = attr->show(group, buf);
169 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400170}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600171
172static ssize_t iommu_group_attr_store(struct kobject *kobj,
173 struct attribute *__attr,
174 const char *buf, size_t count)
175{
176 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
177 struct iommu_group *group = to_iommu_group(kobj);
178 ssize_t ret = -EIO;
179
180 if (attr->store)
181 ret = attr->store(group, buf, count);
182 return ret;
183}
184
185static const struct sysfs_ops iommu_group_sysfs_ops = {
186 .show = iommu_group_attr_show,
187 .store = iommu_group_attr_store,
188};
189
190static int iommu_group_create_file(struct iommu_group *group,
191 struct iommu_group_attribute *attr)
192{
193 return sysfs_create_file(&group->kobj, &attr->attr);
194}
195
196static void iommu_group_remove_file(struct iommu_group *group,
197 struct iommu_group_attribute *attr)
198{
199 sysfs_remove_file(&group->kobj, &attr->attr);
200}
201
202static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
203{
204 return sprintf(buf, "%s\n", group->name);
205}
206
Eric Auger6c65fb32017-01-19 20:57:51 +0000207/**
208 * iommu_insert_resv_region - Insert a new region in the
209 * list of reserved regions.
210 * @new: new region to insert
211 * @regions: list of regions
212 *
213 * The new element is sorted by address with respect to the other
214 * regions of the same type. In case it overlaps with another
215 * region of the same type, regions are merged. In case it
216 * overlaps with another region of different type, regions are
217 * not merged.
218 */
219static int iommu_insert_resv_region(struct iommu_resv_region *new,
220 struct list_head *regions)
221{
222 struct iommu_resv_region *region;
223 phys_addr_t start = new->start;
224 phys_addr_t end = new->start + new->length - 1;
225 struct list_head *pos = regions->next;
226
227 while (pos != regions) {
228 struct iommu_resv_region *entry =
229 list_entry(pos, struct iommu_resv_region, list);
230 phys_addr_t a = entry->start;
231 phys_addr_t b = entry->start + entry->length - 1;
232 int type = entry->type;
233
234 if (end < a) {
235 goto insert;
236 } else if (start > b) {
237 pos = pos->next;
238 } else if ((start >= a) && (end <= b)) {
239 if (new->type == type)
Eric Augerad0834d2019-06-03 08:53:30 +0200240 return 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000241 else
242 pos = pos->next;
243 } else {
244 if (new->type == type) {
245 phys_addr_t new_start = min(a, start);
246 phys_addr_t new_end = max(b, end);
Eric Augerad0834d2019-06-03 08:53:30 +0200247 int ret;
Eric Auger6c65fb32017-01-19 20:57:51 +0000248
249 list_del(&entry->list);
250 entry->start = new_start;
251 entry->length = new_end - new_start + 1;
Eric Augerad0834d2019-06-03 08:53:30 +0200252 ret = iommu_insert_resv_region(entry, regions);
253 kfree(entry);
254 return ret;
Eric Auger6c65fb32017-01-19 20:57:51 +0000255 } else {
256 pos = pos->next;
257 }
258 }
259 }
260insert:
261 region = iommu_alloc_resv_region(new->start, new->length,
262 new->prot, new->type);
263 if (!region)
264 return -ENOMEM;
265
266 list_add_tail(&region->list, pos);
Eric Auger6c65fb32017-01-19 20:57:51 +0000267 return 0;
268}
269
270static int
271iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
272 struct list_head *group_resv_regions)
273{
274 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100275 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000276
277 list_for_each_entry(entry, dev_resv_regions, list) {
278 ret = iommu_insert_resv_region(entry, group_resv_regions);
279 if (ret)
280 break;
281 }
282 return ret;
283}
284
285int iommu_get_group_resv_regions(struct iommu_group *group,
286 struct list_head *head)
287{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100288 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000289 int ret = 0;
290
291 mutex_lock(&group->mutex);
292 list_for_each_entry(device, &group->devices, list) {
293 struct list_head dev_resv_regions;
294
295 INIT_LIST_HEAD(&dev_resv_regions);
296 iommu_get_resv_regions(device->dev, &dev_resv_regions);
297 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
298 iommu_put_resv_regions(device->dev, &dev_resv_regions);
299 if (ret)
300 break;
301 }
302 mutex_unlock(&group->mutex);
303 return ret;
304}
305EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
306
Eric Augerbc7d12b92017-01-19 20:57:52 +0000307static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
308 char *buf)
309{
310 struct iommu_resv_region *region, *next;
311 struct list_head group_resv_regions;
312 char *str = buf;
313
314 INIT_LIST_HEAD(&group_resv_regions);
315 iommu_get_group_resv_regions(group, &group_resv_regions);
316
317 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
318 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
319 (long long int)region->start,
320 (long long int)(region->start +
321 region->length - 1),
322 iommu_group_resv_type_string[region->type]);
323 kfree(region);
324 }
325
326 return (str - buf);
327}
328
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700329static ssize_t iommu_group_show_type(struct iommu_group *group,
330 char *buf)
331{
332 char *type = "unknown\n";
333
334 if (group->default_domain) {
335 switch (group->default_domain->type) {
336 case IOMMU_DOMAIN_BLOCKED:
337 type = "blocked\n";
338 break;
339 case IOMMU_DOMAIN_IDENTITY:
340 type = "identity\n";
341 break;
342 case IOMMU_DOMAIN_UNMANAGED:
343 type = "unmanaged\n";
344 break;
345 case IOMMU_DOMAIN_DMA:
346 type = "DMA";
347 break;
348 }
349 }
350 strcpy(buf, type);
351
352 return strlen(type);
353}
354
Alex Williamsond72e31c2012-05-30 14:18:53 -0600355static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
356
Eric Augerbc7d12b92017-01-19 20:57:52 +0000357static IOMMU_GROUP_ATTR(reserved_regions, 0444,
358 iommu_group_show_resv_regions, NULL);
359
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700360static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
361
Alex Williamsond72e31c2012-05-30 14:18:53 -0600362static void iommu_group_release(struct kobject *kobj)
363{
364 struct iommu_group *group = to_iommu_group(kobj);
365
Joerg Roedel269aa802015-05-28 18:41:25 +0200366 pr_debug("Releasing group %d\n", group->id);
367
Alex Williamsond72e31c2012-05-30 14:18:53 -0600368 if (group->iommu_data_release)
369 group->iommu_data_release(group->iommu_data);
370
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200371 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600372
Joerg Roedel53723dc2015-05-28 18:41:29 +0200373 if (group->default_domain)
374 iommu_domain_free(group->default_domain);
375
Alex Williamsond72e31c2012-05-30 14:18:53 -0600376 kfree(group->name);
377 kfree(group);
378}
379
380static struct kobj_type iommu_group_ktype = {
381 .sysfs_ops = &iommu_group_sysfs_ops,
382 .release = iommu_group_release,
383};
384
385/**
386 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600387 *
388 * This function is called by an iommu driver to allocate a new iommu
389 * group. The iommu group represents the minimum granularity of the iommu.
390 * Upon successful return, the caller holds a reference to the supplied
391 * group in order to hold the group until devices are added. Use
392 * iommu_group_put() to release this extra reference count, allowing the
393 * group to be automatically reclaimed once it has no devices or external
394 * references.
395 */
396struct iommu_group *iommu_group_alloc(void)
397{
398 struct iommu_group *group;
399 int ret;
400
401 group = kzalloc(sizeof(*group), GFP_KERNEL);
402 if (!group)
403 return ERR_PTR(-ENOMEM);
404
405 group->kobj.kset = iommu_group_kset;
406 mutex_init(&group->mutex);
407 INIT_LIST_HEAD(&group->devices);
408 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
409
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200410 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
411 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600412 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200413 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600414 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200415 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600416
417 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
418 NULL, "%d", group->id);
419 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200420 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600421 kfree(group);
422 return ERR_PTR(ret);
423 }
424
425 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
426 if (!group->devices_kobj) {
427 kobject_put(&group->kobj); /* triggers .release & free */
428 return ERR_PTR(-ENOMEM);
429 }
430
431 /*
432 * The devices_kobj holds a reference on the group kobject, so
433 * as long as that exists so will the group. We can therefore
434 * use the devices_kobj for reference counting.
435 */
436 kobject_put(&group->kobj);
437
Eric Augerbc7d12b92017-01-19 20:57:52 +0000438 ret = iommu_group_create_file(group,
439 &iommu_group_attr_reserved_regions);
440 if (ret)
441 return ERR_PTR(ret);
442
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700443 ret = iommu_group_create_file(group, &iommu_group_attr_type);
444 if (ret)
445 return ERR_PTR(ret);
446
Joerg Roedel269aa802015-05-28 18:41:25 +0200447 pr_debug("Allocated group %d\n", group->id);
448
Alex Williamsond72e31c2012-05-30 14:18:53 -0600449 return group;
450}
451EXPORT_SYMBOL_GPL(iommu_group_alloc);
452
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100453struct iommu_group *iommu_group_get_by_id(int id)
454{
455 struct kobject *group_kobj;
456 struct iommu_group *group;
457 const char *name;
458
459 if (!iommu_group_kset)
460 return NULL;
461
462 name = kasprintf(GFP_KERNEL, "%d", id);
463 if (!name)
464 return NULL;
465
466 group_kobj = kset_find_obj(iommu_group_kset, name);
467 kfree(name);
468
469 if (!group_kobj)
470 return NULL;
471
472 group = container_of(group_kobj, struct iommu_group, kobj);
473 BUG_ON(group->id != id);
474
475 kobject_get(group->devices_kobj);
476 kobject_put(&group->kobj);
477
478 return group;
479}
480EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
481
Alex Williamsond72e31c2012-05-30 14:18:53 -0600482/**
483 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
484 * @group: the group
485 *
486 * iommu drivers can store data in the group for use when doing iommu
487 * operations. This function provides a way to retrieve it. Caller
488 * should hold a group reference.
489 */
490void *iommu_group_get_iommudata(struct iommu_group *group)
491{
492 return group->iommu_data;
493}
494EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
495
496/**
497 * iommu_group_set_iommudata - set iommu_data for a group
498 * @group: the group
499 * @iommu_data: new data
500 * @release: release function for iommu_data
501 *
502 * iommu drivers can store data in the group for use when doing iommu
503 * operations. This function provides a way to set the data after
504 * the group has been allocated. Caller should hold a group reference.
505 */
506void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
507 void (*release)(void *iommu_data))
508{
509 group->iommu_data = iommu_data;
510 group->iommu_data_release = release;
511}
512EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
513
514/**
515 * iommu_group_set_name - set name for a group
516 * @group: the group
517 * @name: name
518 *
519 * Allow iommu driver to set a name for a group. When set it will
520 * appear in a name attribute file under the group in sysfs.
521 */
522int iommu_group_set_name(struct iommu_group *group, const char *name)
523{
524 int ret;
525
526 if (group->name) {
527 iommu_group_remove_file(group, &iommu_group_attr_name);
528 kfree(group->name);
529 group->name = NULL;
530 if (!name)
531 return 0;
532 }
533
534 group->name = kstrdup(name, GFP_KERNEL);
535 if (!group->name)
536 return -ENOMEM;
537
538 ret = iommu_group_create_file(group, &iommu_group_attr_name);
539 if (ret) {
540 kfree(group->name);
541 group->name = NULL;
542 return ret;
543 }
544
545 return 0;
546}
547EXPORT_SYMBOL_GPL(iommu_group_set_name);
548
Joerg Roedelbeed2822015-05-28 18:41:34 +0200549static int iommu_group_create_direct_mappings(struct iommu_group *group,
550 struct device *dev)
551{
552 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000553 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200554 struct list_head mappings;
555 unsigned long pg_size;
556 int ret = 0;
557
558 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
559 return 0;
560
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100561 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200562
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100563 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200564 INIT_LIST_HEAD(&mappings);
565
Eric Augere5b52342017-01-19 20:57:47 +0000566 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200567
568 /* We need to consider overlapping regions for different devices */
569 list_for_each_entry(entry, &mappings, list) {
570 dma_addr_t start, end, addr;
571
Eric Augere5b52342017-01-19 20:57:47 +0000572 if (domain->ops->apply_resv_region)
573 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200574
Joerg Roedelbeed2822015-05-28 18:41:34 +0200575 start = ALIGN(entry->start, pg_size);
576 end = ALIGN(entry->start + entry->length, pg_size);
577
Eric Auger544a25d2017-01-19 20:57:50 +0000578 if (entry->type != IOMMU_RESV_DIRECT)
579 continue;
580
Joerg Roedelbeed2822015-05-28 18:41:34 +0200581 for (addr = start; addr < end; addr += pg_size) {
582 phys_addr_t phys_addr;
583
584 phys_addr = iommu_iova_to_phys(domain, addr);
585 if (phys_addr)
586 continue;
587
588 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
589 if (ret)
590 goto out;
591 }
592
593 }
594
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200595 iommu_flush_tlb_all(domain);
596
Joerg Roedelbeed2822015-05-28 18:41:34 +0200597out:
Eric Augere5b52342017-01-19 20:57:47 +0000598 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200599
600 return ret;
601}
602
Alex Williamsond72e31c2012-05-30 14:18:53 -0600603/**
604 * iommu_group_add_device - add a device to an iommu group
605 * @group: the group into which to add the device (reference should be held)
606 * @dev: the device
607 *
608 * This function is called by an iommu driver to add a device into a
609 * group. Adding a device increments the group reference count.
610 */
611int iommu_group_add_device(struct iommu_group *group, struct device *dev)
612{
613 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100614 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600615
616 device = kzalloc(sizeof(*device), GFP_KERNEL);
617 if (!device)
618 return -ENOMEM;
619
620 device->dev = dev;
621
622 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000623 if (ret)
624 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600625
626 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
627rename:
628 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000629 ret = -ENOMEM;
630 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600631 }
632
633 ret = sysfs_create_link_nowarn(group->devices_kobj,
634 &dev->kobj, device->name);
635 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600636 if (ret == -EEXIST && i >= 0) {
637 /*
638 * Account for the slim chance of collision
639 * and append an instance to the name.
640 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000641 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600642 device->name = kasprintf(GFP_KERNEL, "%s.%d",
643 kobject_name(&dev->kobj), i++);
644 goto rename;
645 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000646 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600647 }
648
649 kobject_get(group->devices_kobj);
650
651 dev->iommu_group = group;
652
Joerg Roedelbeed2822015-05-28 18:41:34 +0200653 iommu_group_create_direct_mappings(group, dev);
654
Alex Williamsond72e31c2012-05-30 14:18:53 -0600655 mutex_lock(&group->mutex);
656 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200657 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000658 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600659 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000660 if (ret)
661 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600662
663 /* Notify any listeners about change to group. */
664 blocking_notifier_call_chain(&group->notifier,
665 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600666
667 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200668
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600669 dev_info(dev, "Adding to iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200670
Alex Williamsond72e31c2012-05-30 14:18:53 -0600671 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000672
673err_put_group:
674 mutex_lock(&group->mutex);
675 list_del(&device->list);
676 mutex_unlock(&group->mutex);
677 dev->iommu_group = NULL;
678 kobject_put(group->devices_kobj);
679err_free_name:
680 kfree(device->name);
681err_remove_link:
682 sysfs_remove_link(&dev->kobj, "iommu_group");
683err_free_device:
684 kfree(device);
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600685 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
Robin Murphy797a8b42017-01-16 12:58:07 +0000686 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600687}
688EXPORT_SYMBOL_GPL(iommu_group_add_device);
689
690/**
691 * iommu_group_remove_device - remove a device from it's current group
692 * @dev: device to be removed
693 *
694 * This function is called by an iommu driver to remove the device from
695 * it's current group. This decrements the iommu group reference count.
696 */
697void iommu_group_remove_device(struct device *dev)
698{
699 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100700 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600701
Bjorn Helgaas780da9e2019-02-08 16:05:45 -0600702 dev_info(dev, "Removing from iommu group %d\n", group->id);
Joerg Roedel269aa802015-05-28 18:41:25 +0200703
Alex Williamsond72e31c2012-05-30 14:18:53 -0600704 /* Pre-notify listeners that a device is being removed. */
705 blocking_notifier_call_chain(&group->notifier,
706 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
707
708 mutex_lock(&group->mutex);
709 list_for_each_entry(tmp_device, &group->devices, list) {
710 if (tmp_device->dev == dev) {
711 device = tmp_device;
712 list_del(&device->list);
713 break;
714 }
715 }
716 mutex_unlock(&group->mutex);
717
718 if (!device)
719 return;
720
721 sysfs_remove_link(group->devices_kobj, device->name);
722 sysfs_remove_link(&dev->kobj, "iommu_group");
723
Shuah Khan2e757082013-08-15 11:59:25 -0600724 trace_remove_device_from_group(group->id, dev);
725
Alex Williamsond72e31c2012-05-30 14:18:53 -0600726 kfree(device->name);
727 kfree(device);
728 dev->iommu_group = NULL;
729 kobject_put(group->devices_kobj);
730}
731EXPORT_SYMBOL_GPL(iommu_group_remove_device);
732
Joerg Roedel426a2732015-05-28 18:41:30 +0200733static int iommu_group_device_count(struct iommu_group *group)
734{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100735 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200736 int ret = 0;
737
738 list_for_each_entry(entry, &group->devices, list)
739 ret++;
740
741 return ret;
742}
743
Alex Williamsond72e31c2012-05-30 14:18:53 -0600744/**
745 * iommu_group_for_each_dev - iterate over each device in the group
746 * @group: the group
747 * @data: caller opaque data to be passed to callback function
748 * @fn: caller supplied callback function
749 *
750 * This function is called by group users to iterate over group devices.
751 * Callers should hold a reference count to the group during callback.
752 * The group->mutex is held across callbacks, which will block calls to
753 * iommu_group_add/remove_device.
754 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200755static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
756 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600757{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100758 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600759 int ret = 0;
760
Alex Williamsond72e31c2012-05-30 14:18:53 -0600761 list_for_each_entry(device, &group->devices, list) {
762 ret = fn(device->dev, data);
763 if (ret)
764 break;
765 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200766 return ret;
767}
768
769
770int iommu_group_for_each_dev(struct iommu_group *group, void *data,
771 int (*fn)(struct device *, void *))
772{
773 int ret;
774
775 mutex_lock(&group->mutex);
776 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600777 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200778
Alex Williamsond72e31c2012-05-30 14:18:53 -0600779 return ret;
780}
781EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
782
783/**
784 * iommu_group_get - Return the group for a device and increment reference
785 * @dev: get the group that this device belongs to
786 *
787 * This function is called by iommu drivers and users to get the group
788 * for the specified device. If found, the group is returned and the group
789 * reference in incremented, else NULL.
790 */
791struct iommu_group *iommu_group_get(struct device *dev)
792{
793 struct iommu_group *group = dev->iommu_group;
794
795 if (group)
796 kobject_get(group->devices_kobj);
797
798 return group;
799}
800EXPORT_SYMBOL_GPL(iommu_group_get);
801
802/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000803 * iommu_group_ref_get - Increment reference on a group
804 * @group: the group to use, must not be NULL
805 *
806 * This function is called by iommu drivers to take additional references on an
807 * existing group. Returns the given group for convenience.
808 */
809struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
810{
811 kobject_get(group->devices_kobj);
812 return group;
813}
814
815/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600816 * iommu_group_put - Decrement group reference
817 * @group: the group to use
818 *
819 * This function is called by iommu drivers and users to release the
820 * iommu group. Once the reference count is zero, the group is released.
821 */
822void iommu_group_put(struct iommu_group *group)
823{
824 if (group)
825 kobject_put(group->devices_kobj);
826}
827EXPORT_SYMBOL_GPL(iommu_group_put);
828
829/**
830 * iommu_group_register_notifier - Register a notifier for group changes
831 * @group: the group to watch
832 * @nb: notifier block to signal
833 *
834 * This function allows iommu group users to track changes in a group.
835 * See include/linux/iommu.h for actions sent via this notifier. Caller
836 * should hold a reference to the group throughout notifier registration.
837 */
838int iommu_group_register_notifier(struct iommu_group *group,
839 struct notifier_block *nb)
840{
841 return blocking_notifier_chain_register(&group->notifier, nb);
842}
843EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
844
845/**
846 * iommu_group_unregister_notifier - Unregister a notifier
847 * @group: the group to watch
848 * @nb: notifier block to signal
849 *
850 * Unregister a previously registered group notifier block.
851 */
852int iommu_group_unregister_notifier(struct iommu_group *group,
853 struct notifier_block *nb)
854{
855 return blocking_notifier_chain_unregister(&group->notifier, nb);
856}
857EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
858
859/**
860 * iommu_group_id - Return ID for a group
861 * @group: the group to ID
862 *
863 * Return the unique ID for the group matching the sysfs group number.
864 */
865int iommu_group_id(struct iommu_group *group)
866{
867 return group->id;
868}
869EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400870
Alex Williamsonf096c062014-09-19 10:03:06 -0600871static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
872 unsigned long *devfns);
873
Alex Williamson104a1c12014-07-03 09:51:18 -0600874/*
875 * To consider a PCI device isolated, we require ACS to support Source
876 * Validation, Request Redirection, Completer Redirection, and Upstream
877 * Forwarding. This effectively means that devices cannot spoof their
878 * requester ID, requests and completions cannot be redirected, and all
879 * transactions are forwarded upstream, even as it passes through a
880 * bridge where the target device is downstream.
881 */
882#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
883
Alex Williamsonf096c062014-09-19 10:03:06 -0600884/*
885 * For multifunction devices which are not isolated from each other, find
886 * all the other non-isolated functions and look for existing groups. For
887 * each function, we also need to look for aliases to or from other devices
888 * that may already have a group.
889 */
890static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
891 unsigned long *devfns)
892{
893 struct pci_dev *tmp = NULL;
894 struct iommu_group *group;
895
896 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
897 return NULL;
898
899 for_each_pci_dev(tmp) {
900 if (tmp == pdev || tmp->bus != pdev->bus ||
901 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
902 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
903 continue;
904
905 group = get_pci_alias_group(tmp, devfns);
906 if (group) {
907 pci_dev_put(tmp);
908 return group;
909 }
910 }
911
912 return NULL;
913}
914
915/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100916 * Look for aliases to or from the given device for existing groups. DMA
917 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600918 * space is quite small (especially since we're really only looking at pcie
919 * device, and therefore only expect multiple slots on the root complex or
920 * downstream switch ports). It's conceivable though that a pair of
921 * multifunction devices could have aliases between them that would cause a
922 * loop. To prevent this, we use a bitmap to track where we've been.
923 */
924static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
925 unsigned long *devfns)
926{
927 struct pci_dev *tmp = NULL;
928 struct iommu_group *group;
929
930 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
931 return NULL;
932
933 group = iommu_group_get(&pdev->dev);
934 if (group)
935 return group;
936
937 for_each_pci_dev(tmp) {
938 if (tmp == pdev || tmp->bus != pdev->bus)
939 continue;
940
941 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100942 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600943 group = get_pci_alias_group(tmp, devfns);
944 if (group) {
945 pci_dev_put(tmp);
946 return group;
947 }
948
949 group = get_pci_function_alias_group(tmp, devfns);
950 if (group) {
951 pci_dev_put(tmp);
952 return group;
953 }
954 }
955 }
956
957 return NULL;
958}
959
Alex Williamson104a1c12014-07-03 09:51:18 -0600960struct group_for_pci_data {
961 struct pci_dev *pdev;
962 struct iommu_group *group;
963};
964
965/*
966 * DMA alias iterator callback, return the last seen device. Stop and return
967 * the IOMMU group if we find one along the way.
968 */
969static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
970{
971 struct group_for_pci_data *data = opaque;
972
973 data->pdev = pdev;
974 data->group = iommu_group_get(&pdev->dev);
975
976 return data->group != NULL;
977}
978
979/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200980 * Generic device_group call-back function. It just allocates one
981 * iommu-group per device.
982 */
983struct iommu_group *generic_device_group(struct device *dev)
984{
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200985 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +0200986}
987
988/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600989 * Use standard PCI bus topology, isolation features, and DMA alias quirks
990 * to find or create an IOMMU group for a device.
991 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200992struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600993{
Joerg Roedel5e622922015-10-21 23:51:37 +0200994 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600995 struct group_for_pci_data data;
996 struct pci_bus *bus;
997 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600998 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600999
Joerg Roedel5e622922015-10-21 23:51:37 +02001000 if (WARN_ON(!dev_is_pci(dev)))
1001 return ERR_PTR(-EINVAL);
1002
Alex Williamson104a1c12014-07-03 09:51:18 -06001003 /*
1004 * Find the upstream DMA alias for the device. A device must not
1005 * be aliased due to topology in order to have its own IOMMU group.
1006 * If we find an alias along the way that already belongs to a
1007 * group, use it.
1008 */
1009 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1010 return data.group;
1011
1012 pdev = data.pdev;
1013
1014 /*
1015 * Continue upstream from the point of minimum IOMMU granularity
1016 * due to aliases to the point where devices are protected from
1017 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1018 * group, use it.
1019 */
1020 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1021 if (!bus->self)
1022 continue;
1023
1024 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1025 break;
1026
1027 pdev = bus->self;
1028
1029 group = iommu_group_get(&pdev->dev);
1030 if (group)
1031 return group;
1032 }
1033
1034 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001035 * Look for existing groups on device aliases. If we alias another
1036 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001037 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001038 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1039 if (group)
1040 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001041
1042 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001043 * Look for existing groups on non-isolated functions on the same
1044 * slot and aliases of those funcions, if any. No need to clear
1045 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001046 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001047 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1048 if (group)
1049 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001050
1051 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001052 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001053}
1054
Nipun Guptaeab03e22018-09-10 19:19:18 +05301055/* Get the IOMMU group for device on fsl-mc bus */
1056struct iommu_group *fsl_mc_device_group(struct device *dev)
1057{
1058 struct device *cont_dev = fsl_mc_cont_dev(dev);
1059 struct iommu_group *group;
1060
1061 group = iommu_group_get(cont_dev);
1062 if (!group)
1063 group = iommu_group_alloc();
1064 return group;
1065}
1066
Alex Williamson104a1c12014-07-03 09:51:18 -06001067/**
1068 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1069 * @dev: target device
1070 *
1071 * This function is intended to be called by IOMMU drivers and extended to
1072 * support common, bus-defined algorithms when determining or creating the
1073 * IOMMU group for a device. On success, the caller will hold a reference
1074 * to the returned IOMMU group, which will already include the provided
1075 * device. The reference should be released with iommu_group_put().
1076 */
1077struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1078{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001079 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001080 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001081 int ret;
1082
1083 group = iommu_group_get(dev);
1084 if (group)
1085 return group;
1086
Robin Murphy05f803002017-07-21 13:12:38 +01001087 if (!ops)
1088 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001089
Robin Murphy05f803002017-07-21 13:12:38 +01001090 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001091 if (WARN_ON_ONCE(group == NULL))
1092 return ERR_PTR(-EINVAL);
1093
Alex Williamson104a1c12014-07-03 09:51:18 -06001094 if (IS_ERR(group))
1095 return group;
1096
Joerg Roedel12282362015-10-21 23:51:43 +02001097 /*
1098 * Try to allocate a default domain - needs support from the
1099 * IOMMU driver.
1100 */
1101 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001102 struct iommu_domain *dom;
1103
1104 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1105 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001106 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
Joerg Roedel8bc32a22019-03-22 16:52:17 +01001107 if (dom) {
1108 dev_warn(dev,
1109 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1110 iommu_def_domain_type);
1111 }
Will Deaconfccb4e32017-01-05 18:38:26 +00001112 }
1113
1114 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001115 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001116 group->domain = dom;
Zhen Lei68a6efe2018-09-20 17:10:23 +01001117
1118 if (dom && !iommu_dma_strict) {
1119 int attr = 1;
1120 iommu_domain_set_attr(dom,
1121 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1122 &attr);
1123 }
Joerg Roedel12282362015-10-21 23:51:43 +02001124 }
1125
Alex Williamson104a1c12014-07-03 09:51:18 -06001126 ret = iommu_group_add_device(group, dev);
1127 if (ret) {
1128 iommu_group_put(group);
1129 return ERR_PTR(ret);
1130 }
1131
1132 return group;
1133}
1134
Joerg Roedel6827ca82015-05-28 18:41:35 +02001135struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1136{
1137 return group->default_domain;
1138}
1139
Alex Williamson14604322011-10-21 15:56:05 -04001140static int add_iommu_group(struct device *dev, void *data)
1141{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001142 int ret = iommu_probe_device(dev);
Joerg Roedel38667f12015-06-29 10:16:08 +02001143
1144 /*
1145 * We ignore -ENODEV errors for now, as they just mean that the
1146 * device is not translated by an IOMMU. We still care about
1147 * other errors and fail to initialize when they happen.
1148 */
1149 if (ret == -ENODEV)
1150 ret = 0;
1151
1152 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001153}
1154
Joerg Roedel8da30142015-05-28 18:41:27 +02001155static int remove_iommu_group(struct device *dev, void *data)
1156{
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001157 iommu_release_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001158
1159 return 0;
1160}
1161
Alex Williamsond72e31c2012-05-30 14:18:53 -06001162static int iommu_bus_notifier(struct notifier_block *nb,
1163 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001164{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001165 unsigned long group_action = 0;
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001166 struct device *dev = data;
1167 struct iommu_group *group;
Alex Williamson14604322011-10-21 15:56:05 -04001168
Alex Williamsond72e31c2012-05-30 14:18:53 -06001169 /*
1170 * ADD/DEL call into iommu driver ops if provided, which may
1171 * result in ADD/DEL notifiers to group->notifier
1172 */
1173 if (action == BUS_NOTIFY_ADD_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001174 int ret;
zhichang.yuan3ba87752017-04-18 20:51:48 +08001175
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001176 ret = iommu_probe_device(dev);
1177 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001178 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Joerg Roedelcc5aed42018-11-30 10:31:59 +01001179 iommu_release_device(dev);
1180 return NOTIFY_OK;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001181 }
Alex Williamson14604322011-10-21 15:56:05 -04001182
Alex Williamsond72e31c2012-05-30 14:18:53 -06001183 /*
1184 * Remaining BUS_NOTIFYs get filtered and republished to the
1185 * group, if anyone is listening
1186 */
1187 group = iommu_group_get(dev);
1188 if (!group)
1189 return 0;
1190
1191 switch (action) {
1192 case BUS_NOTIFY_BIND_DRIVER:
1193 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1194 break;
1195 case BUS_NOTIFY_BOUND_DRIVER:
1196 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1197 break;
1198 case BUS_NOTIFY_UNBIND_DRIVER:
1199 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1200 break;
1201 case BUS_NOTIFY_UNBOUND_DRIVER:
1202 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1203 break;
1204 }
1205
1206 if (group_action)
1207 blocking_notifier_call_chain(&group->notifier,
1208 group_action, dev);
1209
1210 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001211 return 0;
1212}
1213
Mark Salterfb3e3062014-09-21 13:58:24 -04001214static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001215{
Mark Salterfb3e3062014-09-21 13:58:24 -04001216 int err;
1217 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001218
Mark Salterfb3e3062014-09-21 13:58:24 -04001219 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1220 if (!nb)
1221 return -ENOMEM;
1222
1223 nb->notifier_call = iommu_bus_notifier;
1224
1225 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001226 if (err)
1227 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001228
Lu Baolu8cec63e2019-03-20 09:40:24 +08001229 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001230 if (err)
1231 goto out_err;
1232
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001233
1234 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001235
1236out_err:
1237 /* Clean up */
Lu Baolu8cec63e2019-03-20 09:40:24 +08001238 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001239 bus_unregister_notifier(bus, nb);
1240
1241out_free:
1242 kfree(nb);
1243
1244 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001245}
1246
Joerg Roedelff217762011-08-26 16:48:26 +02001247/**
1248 * bus_set_iommu - set iommu-callbacks for the bus
1249 * @bus: bus.
1250 * @ops: the callbacks provided by the iommu-driver
1251 *
1252 * This function is called by an iommu driver to set the iommu methods
1253 * used for a particular bus. Drivers for devices on that bus can use
1254 * the iommu-api after these ops are registered.
1255 * This special function is needed because IOMMUs are usually devices on
1256 * the bus itself, so the iommu drivers are not initialized when the bus
1257 * is set up. With this function the iommu-driver can set the iommu-ops
1258 * afterwards.
1259 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001260int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001261{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001262 int err;
1263
Joerg Roedelff217762011-08-26 16:48:26 +02001264 if (bus->iommu_ops != NULL)
1265 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001266
Joerg Roedelff217762011-08-26 16:48:26 +02001267 bus->iommu_ops = ops;
1268
1269 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001270 err = iommu_bus_init(bus, ops);
1271 if (err)
1272 bus->iommu_ops = NULL;
1273
1274 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001275}
Joerg Roedelff217762011-08-26 16:48:26 +02001276EXPORT_SYMBOL_GPL(bus_set_iommu);
1277
Joerg Roedela1b60c12011-09-06 18:46:34 +02001278bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001279{
Joerg Roedel94441c32011-09-06 18:58:54 +02001280 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001281}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001282EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001283
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001284bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1285{
1286 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1287 return false;
1288
1289 return bus->iommu_ops->capable(cap);
1290}
1291EXPORT_SYMBOL_GPL(iommu_capable);
1292
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001293/**
1294 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1295 * @domain: iommu domain
1296 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001297 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001298 *
1299 * This function should be used by IOMMU users which want to be notified
1300 * whenever an IOMMU fault happens.
1301 *
1302 * The fault handler itself should return 0 on success, and an appropriate
1303 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001304 */
1305void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001306 iommu_fault_handler_t handler,
1307 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001308{
1309 BUG_ON(!domain);
1310
1311 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001312 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001313}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001314EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001315
Joerg Roedel53723dc2015-05-28 18:41:29 +02001316static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1317 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001318{
1319 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001320
Joerg Roedel94441c32011-09-06 18:58:54 +02001321 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001322 return NULL;
1323
Joerg Roedel53723dc2015-05-28 18:41:29 +02001324 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001325 if (!domain)
1326 return NULL;
1327
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001328 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001329 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001330 /* Assume all sizes by default; the driver may override this later */
1331 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001332
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001333 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001334}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001335
Joerg Roedel53723dc2015-05-28 18:41:29 +02001336struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1337{
1338 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001339}
1340EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1341
1342void iommu_domain_free(struct iommu_domain *domain)
1343{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001344 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001345}
1346EXPORT_SYMBOL_GPL(iommu_domain_free);
1347
Joerg Roedel426a2732015-05-28 18:41:30 +02001348static int __iommu_attach_device(struct iommu_domain *domain,
1349 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001350{
Shuah Khanb54db772013-08-15 11:59:26 -06001351 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001352 if ((domain->ops->is_attach_deferred != NULL) &&
1353 domain->ops->is_attach_deferred(domain, dev))
1354 return 0;
1355
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001356 if (unlikely(domain->ops->attach_dev == NULL))
1357 return -ENODEV;
1358
Shuah Khanb54db772013-08-15 11:59:26 -06001359 ret = domain->ops->attach_dev(domain, dev);
1360 if (!ret)
1361 trace_attach_device_to_domain(dev);
1362 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001363}
Joerg Roedel426a2732015-05-28 18:41:30 +02001364
1365int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1366{
1367 struct iommu_group *group;
1368 int ret;
1369
1370 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001371 if (!group)
1372 return -ENODEV;
1373
Joerg Roedel426a2732015-05-28 18:41:30 +02001374 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001375 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001376 * change while we are attaching
1377 */
1378 mutex_lock(&group->mutex);
1379 ret = -EINVAL;
1380 if (iommu_group_device_count(group) != 1)
1381 goto out_unlock;
1382
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001383 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001384
1385out_unlock:
1386 mutex_unlock(&group->mutex);
1387 iommu_group_put(group);
1388
1389 return ret;
1390}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001391EXPORT_SYMBOL_GPL(iommu_attach_device);
1392
Joerg Roedel426a2732015-05-28 18:41:30 +02001393static void __iommu_detach_device(struct iommu_domain *domain,
1394 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001395{
Baoquan Hee01d1912017-08-09 16:33:40 +08001396 if ((domain->ops->is_attach_deferred != NULL) &&
1397 domain->ops->is_attach_deferred(domain, dev))
1398 return;
1399
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001400 if (unlikely(domain->ops->detach_dev == NULL))
1401 return;
1402
1403 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001404 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001405}
Joerg Roedel426a2732015-05-28 18:41:30 +02001406
1407void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1408{
1409 struct iommu_group *group;
1410
1411 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001412 if (!group)
1413 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001414
1415 mutex_lock(&group->mutex);
1416 if (iommu_group_device_count(group) != 1) {
1417 WARN_ON(1);
1418 goto out_unlock;
1419 }
1420
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001421 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001422
1423out_unlock:
1424 mutex_unlock(&group->mutex);
1425 iommu_group_put(group);
1426}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001427EXPORT_SYMBOL_GPL(iommu_detach_device);
1428
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001429struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1430{
1431 struct iommu_domain *domain;
1432 struct iommu_group *group;
1433
1434 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01001435 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001436 return NULL;
1437
1438 domain = group->domain;
1439
1440 iommu_group_put(group);
1441
1442 return domain;
1443}
1444EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1445
Alex Williamsond72e31c2012-05-30 14:18:53 -06001446/*
Robin Murphy6af588f2018-09-12 16:24:12 +01001447 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1448 * guarantees that the group and its default domain are valid and correct.
1449 */
1450struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1451{
1452 return dev->iommu_group->default_domain;
1453}
1454
1455/*
Rami Rosen35449ad2018-09-18 17:38:49 +03001456 * IOMMU groups are really the natural working unit of the IOMMU, but
Alex Williamsond72e31c2012-05-30 14:18:53 -06001457 * the IOMMU API works on domains and devices. Bridge that gap by
1458 * iterating over the devices in a group. Ideally we'd have a single
1459 * device which represents the requestor ID of the group, but we also
1460 * allow IOMMU drivers to create policy defined minimum sets, where
1461 * the physical hardware may be able to distiguish members, but we
1462 * wish to group them at a higher level (ex. untrusted multi-function
1463 * PCI devices). Thus we attach each device.
1464 */
1465static int iommu_group_do_attach_device(struct device *dev, void *data)
1466{
1467 struct iommu_domain *domain = data;
1468
Joerg Roedel426a2732015-05-28 18:41:30 +02001469 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001470}
1471
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001472static int __iommu_attach_group(struct iommu_domain *domain,
1473 struct iommu_group *group)
1474{
1475 int ret;
1476
1477 if (group->default_domain && group->domain != group->default_domain)
1478 return -EBUSY;
1479
1480 ret = __iommu_group_for_each_dev(group, domain,
1481 iommu_group_do_attach_device);
1482 if (ret == 0)
1483 group->domain = domain;
1484
1485 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001486}
1487
1488int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1489{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001490 int ret;
1491
1492 mutex_lock(&group->mutex);
1493 ret = __iommu_attach_group(domain, group);
1494 mutex_unlock(&group->mutex);
1495
1496 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001497}
1498EXPORT_SYMBOL_GPL(iommu_attach_group);
1499
1500static int iommu_group_do_detach_device(struct device *dev, void *data)
1501{
1502 struct iommu_domain *domain = data;
1503
Joerg Roedel426a2732015-05-28 18:41:30 +02001504 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001505
1506 return 0;
1507}
1508
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001509static void __iommu_detach_group(struct iommu_domain *domain,
1510 struct iommu_group *group)
1511{
1512 int ret;
1513
1514 if (!group->default_domain) {
1515 __iommu_group_for_each_dev(group, domain,
1516 iommu_group_do_detach_device);
1517 group->domain = NULL;
1518 return;
1519 }
1520
1521 if (group->domain == group->default_domain)
1522 return;
1523
1524 /* Detach by re-attaching to the default domain */
1525 ret = __iommu_group_for_each_dev(group, group->default_domain,
1526 iommu_group_do_attach_device);
1527 if (ret != 0)
1528 WARN_ON(1);
1529 else
1530 group->domain = group->default_domain;
1531}
1532
Alex Williamsond72e31c2012-05-30 14:18:53 -06001533void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1534{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001535 mutex_lock(&group->mutex);
1536 __iommu_detach_group(domain, group);
1537 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001538}
1539EXPORT_SYMBOL_GPL(iommu_detach_group);
1540
Varun Sethibb5547a2013-03-29 01:23:58 +05301541phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001542{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001543 if (unlikely(domain->ops->iova_to_phys == NULL))
1544 return 0;
1545
1546 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001547}
1548EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001549
Alex Williamsonbd139692013-06-17 19:57:34 -06001550static size_t iommu_pgsize(struct iommu_domain *domain,
1551 unsigned long addr_merge, size_t size)
1552{
1553 unsigned int pgsize_idx;
1554 size_t pgsize;
1555
1556 /* Max page size that still fits into 'size' */
1557 pgsize_idx = __fls(size);
1558
1559 /* need to consider alignment requirements ? */
1560 if (likely(addr_merge)) {
1561 /* Max page size allowed by address */
1562 unsigned int align_pgsize_idx = __ffs(addr_merge);
1563 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1564 }
1565
1566 /* build a mask of acceptable page sizes */
1567 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1568
1569 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001570 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001571
1572 /* make sure we're still sane */
1573 BUG_ON(!pgsize);
1574
1575 /* pick the biggest page */
1576 pgsize_idx = __fls(pgsize);
1577 pgsize = 1UL << pgsize_idx;
1578
1579 return pgsize;
1580}
1581
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001582int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001583 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001584{
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001585 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001586 unsigned long orig_iova = iova;
1587 unsigned int min_pagesz;
1588 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001589 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001590 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001591
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001592 if (unlikely(ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001593 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001594 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001595
Joerg Roedela10315e2015-03-26 13:43:06 +01001596 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1597 return -EINVAL;
1598
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001599 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001600 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001601
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001602 /*
1603 * both the virtual address and the physical one, as well as
1604 * the size of the mapping, must be aligned (at least) to the
1605 * size of the smallest page supported by the hardware
1606 */
1607 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001608 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001609 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001610 return -EINVAL;
1611 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001612
Fabio Estevamabedb042013-08-22 10:25:42 -03001613 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001614
1615 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001616 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001617
Fabio Estevamabedb042013-08-22 10:25:42 -03001618 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001619 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001620
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001621 ret = ops->map(domain, iova, paddr, pgsize, prot);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001622 if (ret)
1623 break;
1624
1625 iova += pgsize;
1626 paddr += pgsize;
1627 size -= pgsize;
1628 }
1629
Dmitry Osipenko1d7ae532018-12-12 23:38:47 +03001630 if (ops->iotlb_sync_map)
1631 ops->iotlb_sync_map(domain);
1632
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001633 /* unroll mapping in case something went wrong */
1634 if (ret)
1635 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001636 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001637 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001638
1639 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001640}
1641EXPORT_SYMBOL_GPL(iommu_map);
1642
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001643static size_t __iommu_unmap(struct iommu_domain *domain,
1644 unsigned long iova, size_t size,
1645 bool sync)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001646{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001647 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001648 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001649 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001650 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001651
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001652 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001653 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001654 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001655
Joerg Roedela10315e2015-03-26 13:43:06 +01001656 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001657 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01001658
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001659 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001660 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001661
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001662 /*
1663 * The virtual address, as well as the size of the mapping, must be
1664 * aligned (at least) to the size of the smallest page supported
1665 * by the hardware
1666 */
1667 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001668 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1669 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001670 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001671 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001672
Joe Perches6197ca82013-06-23 12:29:04 -07001673 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001674
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001675 /*
1676 * Keep iterating until we either unmap 'size' bytes (or more)
1677 * or we hit an area that isn't mapped.
1678 */
1679 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001680 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001681
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001682 unmapped_page = ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001683 if (!unmapped_page)
1684 break;
1685
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001686 if (sync && ops->iotlb_range_add)
1687 ops->iotlb_range_add(domain, iova, pgsize);
1688
Joe Perches6197ca82013-06-23 12:29:04 -07001689 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1690 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001691
1692 iova += unmapped_page;
1693 unmapped += unmapped_page;
1694 }
1695
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001696 if (sync && ops->iotlb_sync)
1697 ops->iotlb_sync(domain);
1698
Shuah Khandb8614d2015-01-16 20:53:17 -07001699 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001700 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001701}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001702
1703size_t iommu_unmap(struct iommu_domain *domain,
1704 unsigned long iova, size_t size)
1705{
1706 return __iommu_unmap(domain, iova, size, true);
1707}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001708EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001709
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001710size_t iommu_unmap_fast(struct iommu_domain *domain,
1711 unsigned long iova, size_t size)
1712{
1713 return __iommu_unmap(domain, iova, size, false);
1714}
1715EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1716
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001717size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1718 struct scatterlist *sg, unsigned int nents, int prot)
Olav Haugan315786e2014-10-25 09:55:16 -07001719{
Robin Murphy5d95f402018-10-11 16:56:42 +01001720 size_t len = 0, mapped = 0;
1721 phys_addr_t start;
1722 unsigned int i = 0;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001723 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001724
Robin Murphy5d95f402018-10-11 16:56:42 +01001725 while (i <= nents) {
1726 phys_addr_t s_phys = sg_phys(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07001727
Robin Murphy5d95f402018-10-11 16:56:42 +01001728 if (len && s_phys != start + len) {
1729 ret = iommu_map(domain, iova + mapped, start, len, prot);
1730 if (ret)
1731 goto out_err;
Robin Murphy18f23402014-11-25 17:50:55 +00001732
Robin Murphy5d95f402018-10-11 16:56:42 +01001733 mapped += len;
1734 len = 0;
1735 }
Robin Murphy18f23402014-11-25 17:50:55 +00001736
Robin Murphy5d95f402018-10-11 16:56:42 +01001737 if (len) {
1738 len += sg->length;
1739 } else {
1740 len = sg->length;
1741 start = s_phys;
1742 }
Joerg Roedel38ec0102014-11-04 14:53:51 +01001743
Robin Murphy5d95f402018-10-11 16:56:42 +01001744 if (++i < nents)
1745 sg = sg_next(sg);
Olav Haugan315786e2014-10-25 09:55:16 -07001746 }
1747
1748 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001749
1750out_err:
1751 /* undo mappings already done */
1752 iommu_unmap(domain, iova, mapped);
1753
1754 return 0;
1755
Olav Haugan315786e2014-10-25 09:55:16 -07001756}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001757EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001758
1759int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301760 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001761{
1762 if (unlikely(domain->ops->domain_window_enable == NULL))
1763 return -ENODEV;
1764
Varun Sethi80f97f02013-03-29 01:24:00 +05301765 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1766 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001767}
1768EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1769
1770void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1771{
1772 if (unlikely(domain->ops->domain_window_disable == NULL))
1773 return;
1774
1775 return domain->ops->domain_window_disable(domain, wnd_nr);
1776}
1777EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1778
Joerg Roedel207c6e32017-04-26 15:39:28 +02001779/**
1780 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1781 * @domain: the iommu domain where the fault has happened
1782 * @dev: the device where the fault has happened
1783 * @iova: the faulting address
1784 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1785 *
1786 * This function should be called by the low-level IOMMU implementations
1787 * whenever IOMMU faults happen, to allow high-level users, that are
1788 * interested in such events, to know about them.
1789 *
1790 * This event may be useful for several possible use cases:
1791 * - mere logging of the event
1792 * - dynamic TLB/PTE loading
1793 * - if restarting of the faulting device is required
1794 *
1795 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1796 * PTE/TLB loading will one day be supported, implementations will be able
1797 * to tell whether it succeeded or not according to this return value).
1798 *
1799 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1800 * (though fault handlers can also return -ENOSYS, in case they want to
1801 * elicit the default behavior of the IOMMU drivers).
1802 */
1803int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1804 unsigned long iova, int flags)
1805{
1806 int ret = -ENOSYS;
1807
1808 /*
1809 * if upper layers showed interest and installed a fault handler,
1810 * invoke it.
1811 */
1812 if (domain->handler)
1813 ret = domain->handler(domain, dev, iova, flags,
1814 domain->handler_token);
1815
1816 trace_io_page_fault(dev, iova, flags);
1817 return ret;
1818}
1819EXPORT_SYMBOL_GPL(report_iommu_fault);
1820
Alex Williamsond72e31c2012-05-30 14:18:53 -06001821static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001822{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001823 iommu_group_kset = kset_create_and_add("iommu_groups",
1824 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001825 BUG_ON(!iommu_group_kset);
1826
Gary R Hookbad614b2018-06-12 16:41:21 -05001827 iommu_debugfs_setup();
1828
Alex Williamsond72e31c2012-05-30 14:18:53 -06001829 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001830}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001831core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001832
1833int iommu_domain_get_attr(struct iommu_domain *domain,
1834 enum iommu_attr attr, void *data)
1835{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001836 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001837 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001838 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001839
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001840 switch (attr) {
1841 case DOMAIN_ATTR_GEOMETRY:
1842 geometry = data;
1843 *geometry = domain->geometry;
1844
1845 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001846 case DOMAIN_ATTR_PAGING:
1847 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001848 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001849 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001850 default:
1851 if (!domain->ops->domain_get_attr)
1852 return -EINVAL;
1853
1854 ret = domain->ops->domain_get_attr(domain, attr, data);
1855 }
1856
1857 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001858}
1859EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1860
1861int iommu_domain_set_attr(struct iommu_domain *domain,
1862 enum iommu_attr attr, void *data)
1863{
Joerg Roedel69356712013-02-04 14:00:01 +01001864 int ret = 0;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001865
Joerg Roedel69356712013-02-04 14:00:01 +01001866 switch (attr) {
Joerg Roedel69356712013-02-04 14:00:01 +01001867 default:
1868 if (domain->ops->domain_set_attr == NULL)
1869 return -EINVAL;
1870
1871 ret = domain->ops->domain_set_attr(domain, attr, data);
1872 }
1873
1874 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001875}
1876EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001877
Eric Augere5b52342017-01-19 20:57:47 +00001878void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001879{
1880 const struct iommu_ops *ops = dev->bus->iommu_ops;
1881
Eric Augere5b52342017-01-19 20:57:47 +00001882 if (ops && ops->get_resv_regions)
1883 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001884}
1885
Eric Augere5b52342017-01-19 20:57:47 +00001886void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001887{
1888 const struct iommu_ops *ops = dev->bus->iommu_ops;
1889
Eric Augere5b52342017-01-19 20:57:47 +00001890 if (ops && ops->put_resv_regions)
1891 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001892}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001893
Eric Auger2b20cbb2017-01-19 20:57:49 +00001894struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001895 size_t length, int prot,
1896 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00001897{
1898 struct iommu_resv_region *region;
1899
1900 region = kzalloc(sizeof(*region), GFP_KERNEL);
1901 if (!region)
1902 return NULL;
1903
1904 INIT_LIST_HEAD(&region->list);
1905 region->start = start;
1906 region->length = length;
1907 region->prot = prot;
1908 region->type = type;
1909 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001910}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001911
Lu Baolu7423e012019-05-25 13:41:22 +08001912static int
1913request_default_domain_for_dev(struct device *dev, unsigned long type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001914{
Lu Baolu7423e012019-05-25 13:41:22 +08001915 struct iommu_domain *domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001916 struct iommu_group *group;
1917 int ret;
1918
1919 /* Device must already be in a group before calling this function */
1920 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001921 if (IS_ERR(group))
1922 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001923
1924 mutex_lock(&group->mutex);
1925
1926 /* Check if the default domain is already direct mapped */
1927 ret = 0;
Lu Baolu7423e012019-05-25 13:41:22 +08001928 if (group->default_domain && group->default_domain->type == type)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001929 goto out;
1930
1931 /* Don't change mappings of existing devices */
1932 ret = -EBUSY;
1933 if (iommu_group_device_count(group) != 1)
1934 goto out;
1935
1936 /* Allocate a direct mapped domain */
1937 ret = -ENOMEM;
Lu Baolu7423e012019-05-25 13:41:22 +08001938 domain = __iommu_domain_alloc(dev->bus, type);
1939 if (!domain)
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001940 goto out;
1941
1942 /* Attach the device to the domain */
Lu Baolu7423e012019-05-25 13:41:22 +08001943 ret = __iommu_attach_group(domain, group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001944 if (ret) {
Lu Baolu7423e012019-05-25 13:41:22 +08001945 iommu_domain_free(domain);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001946 goto out;
1947 }
1948
Lu Baolu7423e012019-05-25 13:41:22 +08001949 iommu_group_create_direct_mappings(group, dev);
1950
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001951 /* Make the direct mapped domain the default for this group */
1952 if (group->default_domain)
1953 iommu_domain_free(group->default_domain);
Lu Baolu7423e012019-05-25 13:41:22 +08001954 group->default_domain = domain;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001955
Lu Baolu7423e012019-05-25 13:41:22 +08001956 dev_info(dev, "Using iommu %s mapping\n",
1957 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001958
1959 ret = 0;
1960out:
1961 mutex_unlock(&group->mutex);
1962 iommu_group_put(group);
1963
1964 return ret;
1965}
Robin Murphy57f98d22016-09-13 10:54:14 +01001966
Lu Baolu7423e012019-05-25 13:41:22 +08001967/* Request that a device is direct mapped by the IOMMU */
1968int iommu_request_dm_for_dev(struct device *dev)
1969{
1970 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
1971}
1972
1973/* Request that a device can't be direct mapped by the IOMMU */
1974int iommu_request_dma_domain_for_dev(struct device *dev)
1975{
1976 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
1977}
1978
Joerg Roedel534766d2017-01-31 16:58:42 +01001979const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001980{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001981 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001982 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001983
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001984 spin_lock(&iommu_device_lock);
1985 list_for_each_entry(iommu, &iommu_device_list, list)
1986 if (iommu->fwnode == fwnode) {
1987 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001988 break;
1989 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001990 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001991 return ops;
1992}
1993
Robin Murphy57f98d22016-09-13 10:54:14 +01001994int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1995 const struct iommu_ops *ops)
1996{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01001997 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01001998
1999 if (fwspec)
2000 return ops == fwspec->ops ? 0 : -EINVAL;
2001
2002 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2003 if (!fwspec)
2004 return -ENOMEM;
2005
2006 of_node_get(to_of_node(iommu_fwnode));
2007 fwspec->iommu_fwnode = iommu_fwnode;
2008 fwspec->ops = ops;
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002009 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002010 return 0;
2011}
2012EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2013
2014void iommu_fwspec_free(struct device *dev)
2015{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002016 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002017
2018 if (fwspec) {
2019 fwnode_handle_put(fwspec->iommu_fwnode);
2020 kfree(fwspec);
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002021 dev_iommu_fwspec_set(dev, NULL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002022 }
2023}
2024EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2025
2026int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2027{
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002028 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy57f98d22016-09-13 10:54:14 +01002029 size_t size;
2030 int i;
2031
2032 if (!fwspec)
2033 return -EINVAL;
2034
2035 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2036 if (size > sizeof(*fwspec)) {
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002037 fwspec = krealloc(fwspec, size, GFP_KERNEL);
Robin Murphy57f98d22016-09-13 10:54:14 +01002038 if (!fwspec)
2039 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002040
Joerg Roedelb4ef7252018-11-28 13:35:24 +01002041 dev_iommu_fwspec_set(dev, fwspec);
Robin Murphy57f98d22016-09-13 10:54:14 +01002042 }
2043
2044 for (i = 0; i < num_ids; i++)
2045 fwspec->ids[fwspec->num_ids + i] = ids[i];
2046
2047 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01002048 return 0;
2049}
2050EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Lu Baolua3a19592019-03-25 09:30:28 +08002051
2052/*
2053 * Per device IOMMU features.
2054 */
2055bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2056{
2057 const struct iommu_ops *ops = dev->bus->iommu_ops;
2058
2059 if (ops && ops->dev_has_feat)
2060 return ops->dev_has_feat(dev, feat);
2061
2062 return false;
2063}
2064EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2065
2066int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2067{
2068 const struct iommu_ops *ops = dev->bus->iommu_ops;
2069
2070 if (ops && ops->dev_enable_feat)
2071 return ops->dev_enable_feat(dev, feat);
2072
2073 return -ENODEV;
2074}
2075EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2076
2077/*
2078 * The device drivers should do the necessary cleanups before calling this.
2079 * For example, before disabling the aux-domain feature, the device driver
2080 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2081 */
2082int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2083{
2084 const struct iommu_ops *ops = dev->bus->iommu_ops;
2085
2086 if (ops && ops->dev_disable_feat)
2087 return ops->dev_disable_feat(dev, feat);
2088
2089 return -EBUSY;
2090}
2091EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2092
2093bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2094{
2095 const struct iommu_ops *ops = dev->bus->iommu_ops;
2096
2097 if (ops && ops->dev_feat_enabled)
2098 return ops->dev_feat_enabled(dev, feat);
2099
2100 return false;
2101}
2102EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2103
2104/*
2105 * Aux-domain specific attach/detach.
2106 *
2107 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2108 * true. Also, as long as domains are attached to a device through this
2109 * interface, any tries to call iommu_attach_device() should fail
2110 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2111 * This should make us safe against a device being attached to a guest as a
2112 * whole while there are still pasid users on it (aux and sva).
2113 */
2114int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2115{
2116 int ret = -ENODEV;
2117
2118 if (domain->ops->aux_attach_dev)
2119 ret = domain->ops->aux_attach_dev(domain, dev);
2120
2121 if (!ret)
2122 trace_attach_device_to_domain(dev);
2123
2124 return ret;
2125}
2126EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2127
2128void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2129{
2130 if (domain->ops->aux_detach_dev) {
2131 domain->ops->aux_detach_dev(domain, dev);
2132 trace_detach_device_from_domain(dev);
2133 }
2134}
2135EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2136
2137int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2138{
2139 int ret = -ENODEV;
2140
2141 if (domain->ops->aux_get_pasid)
2142 ret = domain->ops->aux_get_pasid(domain, dev);
2143
2144 return ret;
2145}
2146EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
Jean-Philippe Brucker26b25a22019-04-10 16:15:16 +01002147
2148/**
2149 * iommu_sva_bind_device() - Bind a process address space to a device
2150 * @dev: the device
2151 * @mm: the mm to bind, caller must hold a reference to it
2152 *
2153 * Create a bond between device and address space, allowing the device to access
2154 * the mm using the returned PASID. If a bond already exists between @device and
2155 * @mm, it is returned and an additional reference is taken. Caller must call
2156 * iommu_sva_unbind_device() to release each reference.
2157 *
2158 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2159 * initialize the required SVA features.
2160 *
2161 * On error, returns an ERR_PTR value.
2162 */
2163struct iommu_sva *
2164iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2165{
2166 struct iommu_group *group;
2167 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2168 const struct iommu_ops *ops = dev->bus->iommu_ops;
2169
2170 if (!ops || !ops->sva_bind)
2171 return ERR_PTR(-ENODEV);
2172
2173 group = iommu_group_get(dev);
2174 if (!group)
2175 return ERR_PTR(-ENODEV);
2176
2177 /* Ensure device count and domain don't change while we're binding */
2178 mutex_lock(&group->mutex);
2179
2180 /*
2181 * To keep things simple, SVA currently doesn't support IOMMU groups
2182 * with more than one device. Existing SVA-capable systems are not
2183 * affected by the problems that required IOMMU groups (lack of ACS
2184 * isolation, device ID aliasing and other hardware issues).
2185 */
2186 if (iommu_group_device_count(group) != 1)
2187 goto out_unlock;
2188
2189 handle = ops->sva_bind(dev, mm, drvdata);
2190
2191out_unlock:
2192 mutex_unlock(&group->mutex);
2193 iommu_group_put(group);
2194
2195 return handle;
2196}
2197EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2198
2199/**
2200 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2201 * @handle: the handle returned by iommu_sva_bind_device()
2202 *
2203 * Put reference to a bond between device and address space. The device should
2204 * not be issuing any more transaction for this PASID. All outstanding page
2205 * requests for this PASID must have been flushed to the IOMMU.
2206 *
2207 * Returns 0 on success, or an error value
2208 */
2209void iommu_sva_unbind_device(struct iommu_sva *handle)
2210{
2211 struct iommu_group *group;
2212 struct device *dev = handle->dev;
2213 const struct iommu_ops *ops = dev->bus->iommu_ops;
2214
2215 if (!ops || !ops->sva_unbind)
2216 return;
2217
2218 group = iommu_group_get(dev);
2219 if (!group)
2220 return;
2221
2222 mutex_lock(&group->mutex);
2223 ops->sva_unbind(handle);
2224 mutex_unlock(&group->mutex);
2225
2226 iommu_group_put(group);
2227}
2228EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2229
2230int iommu_sva_set_ops(struct iommu_sva *handle,
2231 const struct iommu_sva_ops *sva_ops)
2232{
2233 if (handle->ops && handle->ops != sva_ops)
2234 return -EEXIST;
2235
2236 handle->ops = sva_ops;
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2240
2241int iommu_sva_get_pasid(struct iommu_sva *handle)
2242{
2243 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2244
2245 if (!ops || !ops->sva_get_pasid)
2246 return IOMMU_PASID_INVALID;
2247
2248 return ops->sva_get_pasid(handle);
2249}
2250EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);