blob: 8c15c598029934484e26520aa4aa7aa703a04ca3 [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>
Andrew Morton60db4022009-05-06 16:03:07 -070025#include <linux/module.h>
26#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010027#include <linux/errno.h>
28#include <linux/iommu.h>
Alex Williamsond72e31c2012-05-30 14:18:53 -060029#include <linux/idr.h>
30#include <linux/notifier.h>
31#include <linux/err.h>
Alex Williamson104a1c12014-07-03 09:51:18 -060032#include <linux/pci.h>
Alex Williamsonf096c062014-09-19 10:03:06 -060033#include <linux/bitops.h>
Robin Murphy57f98d22016-09-13 10:54:14 +010034#include <linux/property.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060035#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010036
Alex Williamsond72e31c2012-05-30 14:18:53 -060037static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020038static DEFINE_IDA(iommu_group_ida);
Olof Johansson58d11312018-07-20 11:02:23 -070039#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
40static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
41#else
Will Deaconfccb4e32017-01-05 18:38:26 +000042static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Olof Johansson58d11312018-07-20 11:02:23 -070043#endif
Alex Williamsond72e31c2012-05-30 14:18:53 -060044
Thierry Redingb22f6432014-06-27 09:03:12 +020045struct iommu_callback_data {
46 const struct iommu_ops *ops;
47};
48
Alex Williamsond72e31c2012-05-30 14:18:53 -060049struct iommu_group {
50 struct kobject kobj;
51 struct kobject *devices_kobj;
52 struct list_head devices;
53 struct mutex mutex;
54 struct blocking_notifier_head notifier;
55 void *iommu_data;
56 void (*iommu_data_release)(void *iommu_data);
57 char *name;
58 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020059 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020060 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060061};
62
Joerg Roedelc09e22d2017-02-01 12:19:46 +010063struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060064 struct list_head list;
65 struct device *dev;
66 char *name;
67};
68
69struct iommu_group_attribute {
70 struct attribute attr;
71 ssize_t (*show)(struct iommu_group *group, char *buf);
72 ssize_t (*store)(struct iommu_group *group,
73 const char *buf, size_t count);
74};
75
Eric Augerbc7d12b92017-01-19 20:57:52 +000076static const char * const iommu_group_resv_type_string[] = {
77 [IOMMU_RESV_DIRECT] = "direct",
78 [IOMMU_RESV_RESERVED] = "reserved",
79 [IOMMU_RESV_MSI] = "msi",
Robin Murphy9d3a4de2017-03-16 17:00:16 +000080 [IOMMU_RESV_SW_MSI] = "msi",
Eric Augerbc7d12b92017-01-19 20:57:52 +000081};
82
Alex Williamsond72e31c2012-05-30 14:18:53 -060083#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
84struct iommu_group_attribute iommu_group_attr_##_name = \
85 __ATTR(_name, _mode, _show, _store)
86
87#define to_iommu_group_attr(_attr) \
88 container_of(_attr, struct iommu_group_attribute, attr)
89#define to_iommu_group(_kobj) \
90 container_of(_kobj, struct iommu_group, kobj)
91
Joerg Roedelb0119e82017-02-01 13:23:08 +010092static LIST_HEAD(iommu_device_list);
93static DEFINE_SPINLOCK(iommu_device_lock);
94
95int iommu_device_register(struct iommu_device *iommu)
96{
97 spin_lock(&iommu_device_lock);
98 list_add_tail(&iommu->list, &iommu_device_list);
99 spin_unlock(&iommu_device_lock);
100
101 return 0;
102}
103
104void iommu_device_unregister(struct iommu_device *iommu)
105{
106 spin_lock(&iommu_device_lock);
107 list_del(&iommu->list);
108 spin_unlock(&iommu_device_lock);
109}
110
Joerg Roedel53723dc2015-05-28 18:41:29 +0200111static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
112 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200113static int __iommu_attach_device(struct iommu_domain *domain,
114 struct device *dev);
115static int __iommu_attach_group(struct iommu_domain *domain,
116 struct iommu_group *group);
117static void __iommu_detach_group(struct iommu_domain *domain,
118 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +0200119
Will Deaconfccb4e32017-01-05 18:38:26 +0000120static int __init iommu_set_def_domain_type(char *str)
121{
122 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300123 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000124
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300125 ret = kstrtobool(str, &pt);
126 if (ret)
127 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000128
129 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
130 return 0;
131}
132early_param("iommu.passthrough", iommu_set_def_domain_type);
133
Alex Williamsond72e31c2012-05-30 14:18:53 -0600134static ssize_t iommu_group_attr_show(struct kobject *kobj,
135 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400136{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600137 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
138 struct iommu_group *group = to_iommu_group(kobj);
139 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400140
Alex Williamsond72e31c2012-05-30 14:18:53 -0600141 if (attr->show)
142 ret = attr->show(group, buf);
143 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400144}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600145
146static ssize_t iommu_group_attr_store(struct kobject *kobj,
147 struct attribute *__attr,
148 const char *buf, size_t count)
149{
150 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
151 struct iommu_group *group = to_iommu_group(kobj);
152 ssize_t ret = -EIO;
153
154 if (attr->store)
155 ret = attr->store(group, buf, count);
156 return ret;
157}
158
159static const struct sysfs_ops iommu_group_sysfs_ops = {
160 .show = iommu_group_attr_show,
161 .store = iommu_group_attr_store,
162};
163
164static int iommu_group_create_file(struct iommu_group *group,
165 struct iommu_group_attribute *attr)
166{
167 return sysfs_create_file(&group->kobj, &attr->attr);
168}
169
170static void iommu_group_remove_file(struct iommu_group *group,
171 struct iommu_group_attribute *attr)
172{
173 sysfs_remove_file(&group->kobj, &attr->attr);
174}
175
176static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
177{
178 return sprintf(buf, "%s\n", group->name);
179}
180
Eric Auger6c65fb32017-01-19 20:57:51 +0000181/**
182 * iommu_insert_resv_region - Insert a new region in the
183 * list of reserved regions.
184 * @new: new region to insert
185 * @regions: list of regions
186 *
187 * The new element is sorted by address with respect to the other
188 * regions of the same type. In case it overlaps with another
189 * region of the same type, regions are merged. In case it
190 * overlaps with another region of different type, regions are
191 * not merged.
192 */
193static int iommu_insert_resv_region(struct iommu_resv_region *new,
194 struct list_head *regions)
195{
196 struct iommu_resv_region *region;
197 phys_addr_t start = new->start;
198 phys_addr_t end = new->start + new->length - 1;
199 struct list_head *pos = regions->next;
200
201 while (pos != regions) {
202 struct iommu_resv_region *entry =
203 list_entry(pos, struct iommu_resv_region, list);
204 phys_addr_t a = entry->start;
205 phys_addr_t b = entry->start + entry->length - 1;
206 int type = entry->type;
207
208 if (end < a) {
209 goto insert;
210 } else if (start > b) {
211 pos = pos->next;
212 } else if ((start >= a) && (end <= b)) {
213 if (new->type == type)
214 goto done;
215 else
216 pos = pos->next;
217 } else {
218 if (new->type == type) {
219 phys_addr_t new_start = min(a, start);
220 phys_addr_t new_end = max(b, end);
221
222 list_del(&entry->list);
223 entry->start = new_start;
224 entry->length = new_end - new_start + 1;
225 iommu_insert_resv_region(entry, regions);
226 } else {
227 pos = pos->next;
228 }
229 }
230 }
231insert:
232 region = iommu_alloc_resv_region(new->start, new->length,
233 new->prot, new->type);
234 if (!region)
235 return -ENOMEM;
236
237 list_add_tail(&region->list, pos);
238done:
239 return 0;
240}
241
242static int
243iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
244 struct list_head *group_resv_regions)
245{
246 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100247 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000248
249 list_for_each_entry(entry, dev_resv_regions, list) {
250 ret = iommu_insert_resv_region(entry, group_resv_regions);
251 if (ret)
252 break;
253 }
254 return ret;
255}
256
257int iommu_get_group_resv_regions(struct iommu_group *group,
258 struct list_head *head)
259{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100260 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000261 int ret = 0;
262
263 mutex_lock(&group->mutex);
264 list_for_each_entry(device, &group->devices, list) {
265 struct list_head dev_resv_regions;
266
267 INIT_LIST_HEAD(&dev_resv_regions);
268 iommu_get_resv_regions(device->dev, &dev_resv_regions);
269 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
270 iommu_put_resv_regions(device->dev, &dev_resv_regions);
271 if (ret)
272 break;
273 }
274 mutex_unlock(&group->mutex);
275 return ret;
276}
277EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
278
Eric Augerbc7d12b92017-01-19 20:57:52 +0000279static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
280 char *buf)
281{
282 struct iommu_resv_region *region, *next;
283 struct list_head group_resv_regions;
284 char *str = buf;
285
286 INIT_LIST_HEAD(&group_resv_regions);
287 iommu_get_group_resv_regions(group, &group_resv_regions);
288
289 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
290 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
291 (long long int)region->start,
292 (long long int)(region->start +
293 region->length - 1),
294 iommu_group_resv_type_string[region->type]);
295 kfree(region);
296 }
297
298 return (str - buf);
299}
300
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700301static ssize_t iommu_group_show_type(struct iommu_group *group,
302 char *buf)
303{
304 char *type = "unknown\n";
305
306 if (group->default_domain) {
307 switch (group->default_domain->type) {
308 case IOMMU_DOMAIN_BLOCKED:
309 type = "blocked\n";
310 break;
311 case IOMMU_DOMAIN_IDENTITY:
312 type = "identity\n";
313 break;
314 case IOMMU_DOMAIN_UNMANAGED:
315 type = "unmanaged\n";
316 break;
317 case IOMMU_DOMAIN_DMA:
318 type = "DMA";
319 break;
320 }
321 }
322 strcpy(buf, type);
323
324 return strlen(type);
325}
326
Alex Williamsond72e31c2012-05-30 14:18:53 -0600327static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
328
Eric Augerbc7d12b92017-01-19 20:57:52 +0000329static IOMMU_GROUP_ATTR(reserved_regions, 0444,
330 iommu_group_show_resv_regions, NULL);
331
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700332static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
333
Alex Williamsond72e31c2012-05-30 14:18:53 -0600334static void iommu_group_release(struct kobject *kobj)
335{
336 struct iommu_group *group = to_iommu_group(kobj);
337
Joerg Roedel269aa802015-05-28 18:41:25 +0200338 pr_debug("Releasing group %d\n", group->id);
339
Alex Williamsond72e31c2012-05-30 14:18:53 -0600340 if (group->iommu_data_release)
341 group->iommu_data_release(group->iommu_data);
342
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200343 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600344
Joerg Roedel53723dc2015-05-28 18:41:29 +0200345 if (group->default_domain)
346 iommu_domain_free(group->default_domain);
347
Alex Williamsond72e31c2012-05-30 14:18:53 -0600348 kfree(group->name);
349 kfree(group);
350}
351
352static struct kobj_type iommu_group_ktype = {
353 .sysfs_ops = &iommu_group_sysfs_ops,
354 .release = iommu_group_release,
355};
356
357/**
358 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600359 *
360 * This function is called by an iommu driver to allocate a new iommu
361 * group. The iommu group represents the minimum granularity of the iommu.
362 * Upon successful return, the caller holds a reference to the supplied
363 * group in order to hold the group until devices are added. Use
364 * iommu_group_put() to release this extra reference count, allowing the
365 * group to be automatically reclaimed once it has no devices or external
366 * references.
367 */
368struct iommu_group *iommu_group_alloc(void)
369{
370 struct iommu_group *group;
371 int ret;
372
373 group = kzalloc(sizeof(*group), GFP_KERNEL);
374 if (!group)
375 return ERR_PTR(-ENOMEM);
376
377 group->kobj.kset = iommu_group_kset;
378 mutex_init(&group->mutex);
379 INIT_LIST_HEAD(&group->devices);
380 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
381
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200382 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
383 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600384 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200385 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600386 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200387 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600388
389 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
390 NULL, "%d", group->id);
391 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200392 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600393 kfree(group);
394 return ERR_PTR(ret);
395 }
396
397 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
398 if (!group->devices_kobj) {
399 kobject_put(&group->kobj); /* triggers .release & free */
400 return ERR_PTR(-ENOMEM);
401 }
402
403 /*
404 * The devices_kobj holds a reference on the group kobject, so
405 * as long as that exists so will the group. We can therefore
406 * use the devices_kobj for reference counting.
407 */
408 kobject_put(&group->kobj);
409
Eric Augerbc7d12b92017-01-19 20:57:52 +0000410 ret = iommu_group_create_file(group,
411 &iommu_group_attr_reserved_regions);
412 if (ret)
413 return ERR_PTR(ret);
414
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700415 ret = iommu_group_create_file(group, &iommu_group_attr_type);
416 if (ret)
417 return ERR_PTR(ret);
418
Joerg Roedel269aa802015-05-28 18:41:25 +0200419 pr_debug("Allocated group %d\n", group->id);
420
Alex Williamsond72e31c2012-05-30 14:18:53 -0600421 return group;
422}
423EXPORT_SYMBOL_GPL(iommu_group_alloc);
424
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100425struct iommu_group *iommu_group_get_by_id(int id)
426{
427 struct kobject *group_kobj;
428 struct iommu_group *group;
429 const char *name;
430
431 if (!iommu_group_kset)
432 return NULL;
433
434 name = kasprintf(GFP_KERNEL, "%d", id);
435 if (!name)
436 return NULL;
437
438 group_kobj = kset_find_obj(iommu_group_kset, name);
439 kfree(name);
440
441 if (!group_kobj)
442 return NULL;
443
444 group = container_of(group_kobj, struct iommu_group, kobj);
445 BUG_ON(group->id != id);
446
447 kobject_get(group->devices_kobj);
448 kobject_put(&group->kobj);
449
450 return group;
451}
452EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
453
Alex Williamsond72e31c2012-05-30 14:18:53 -0600454/**
455 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
456 * @group: the group
457 *
458 * iommu drivers can store data in the group for use when doing iommu
459 * operations. This function provides a way to retrieve it. Caller
460 * should hold a group reference.
461 */
462void *iommu_group_get_iommudata(struct iommu_group *group)
463{
464 return group->iommu_data;
465}
466EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
467
468/**
469 * iommu_group_set_iommudata - set iommu_data for a group
470 * @group: the group
471 * @iommu_data: new data
472 * @release: release function for iommu_data
473 *
474 * iommu drivers can store data in the group for use when doing iommu
475 * operations. This function provides a way to set the data after
476 * the group has been allocated. Caller should hold a group reference.
477 */
478void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
479 void (*release)(void *iommu_data))
480{
481 group->iommu_data = iommu_data;
482 group->iommu_data_release = release;
483}
484EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
485
486/**
487 * iommu_group_set_name - set name for a group
488 * @group: the group
489 * @name: name
490 *
491 * Allow iommu driver to set a name for a group. When set it will
492 * appear in a name attribute file under the group in sysfs.
493 */
494int iommu_group_set_name(struct iommu_group *group, const char *name)
495{
496 int ret;
497
498 if (group->name) {
499 iommu_group_remove_file(group, &iommu_group_attr_name);
500 kfree(group->name);
501 group->name = NULL;
502 if (!name)
503 return 0;
504 }
505
506 group->name = kstrdup(name, GFP_KERNEL);
507 if (!group->name)
508 return -ENOMEM;
509
510 ret = iommu_group_create_file(group, &iommu_group_attr_name);
511 if (ret) {
512 kfree(group->name);
513 group->name = NULL;
514 return ret;
515 }
516
517 return 0;
518}
519EXPORT_SYMBOL_GPL(iommu_group_set_name);
520
Joerg Roedelbeed2822015-05-28 18:41:34 +0200521static int iommu_group_create_direct_mappings(struct iommu_group *group,
522 struct device *dev)
523{
524 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000525 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200526 struct list_head mappings;
527 unsigned long pg_size;
528 int ret = 0;
529
530 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
531 return 0;
532
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100533 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200534
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100535 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200536 INIT_LIST_HEAD(&mappings);
537
Eric Augere5b52342017-01-19 20:57:47 +0000538 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200539
540 /* We need to consider overlapping regions for different devices */
541 list_for_each_entry(entry, &mappings, list) {
542 dma_addr_t start, end, addr;
543
Eric Augere5b52342017-01-19 20:57:47 +0000544 if (domain->ops->apply_resv_region)
545 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200546
Joerg Roedelbeed2822015-05-28 18:41:34 +0200547 start = ALIGN(entry->start, pg_size);
548 end = ALIGN(entry->start + entry->length, pg_size);
549
Eric Auger544a25d2017-01-19 20:57:50 +0000550 if (entry->type != IOMMU_RESV_DIRECT)
551 continue;
552
Joerg Roedelbeed2822015-05-28 18:41:34 +0200553 for (addr = start; addr < end; addr += pg_size) {
554 phys_addr_t phys_addr;
555
556 phys_addr = iommu_iova_to_phys(domain, addr);
557 if (phys_addr)
558 continue;
559
560 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
561 if (ret)
562 goto out;
563 }
564
565 }
566
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200567 iommu_flush_tlb_all(domain);
568
Joerg Roedelbeed2822015-05-28 18:41:34 +0200569out:
Eric Augere5b52342017-01-19 20:57:47 +0000570 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200571
572 return ret;
573}
574
Alex Williamsond72e31c2012-05-30 14:18:53 -0600575/**
576 * iommu_group_add_device - add a device to an iommu group
577 * @group: the group into which to add the device (reference should be held)
578 * @dev: the device
579 *
580 * This function is called by an iommu driver to add a device into a
581 * group. Adding a device increments the group reference count.
582 */
583int iommu_group_add_device(struct iommu_group *group, struct device *dev)
584{
585 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100586 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600587
588 device = kzalloc(sizeof(*device), GFP_KERNEL);
589 if (!device)
590 return -ENOMEM;
591
592 device->dev = dev;
593
594 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000595 if (ret)
596 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600597
598 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
599rename:
600 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000601 ret = -ENOMEM;
602 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600603 }
604
605 ret = sysfs_create_link_nowarn(group->devices_kobj,
606 &dev->kobj, device->name);
607 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600608 if (ret == -EEXIST && i >= 0) {
609 /*
610 * Account for the slim chance of collision
611 * and append an instance to the name.
612 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000613 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600614 device->name = kasprintf(GFP_KERNEL, "%s.%d",
615 kobject_name(&dev->kobj), i++);
616 goto rename;
617 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000618 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600619 }
620
621 kobject_get(group->devices_kobj);
622
623 dev->iommu_group = group;
624
Joerg Roedelbeed2822015-05-28 18:41:34 +0200625 iommu_group_create_direct_mappings(group, dev);
626
Alex Williamsond72e31c2012-05-30 14:18:53 -0600627 mutex_lock(&group->mutex);
628 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200629 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000630 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600631 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000632 if (ret)
633 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600634
635 /* Notify any listeners about change to group. */
636 blocking_notifier_call_chain(&group->notifier,
637 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600638
639 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200640
641 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
642
Alex Williamsond72e31c2012-05-30 14:18:53 -0600643 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000644
645err_put_group:
646 mutex_lock(&group->mutex);
647 list_del(&device->list);
648 mutex_unlock(&group->mutex);
649 dev->iommu_group = NULL;
650 kobject_put(group->devices_kobj);
651err_free_name:
652 kfree(device->name);
653err_remove_link:
654 sysfs_remove_link(&dev->kobj, "iommu_group");
655err_free_device:
656 kfree(device);
657 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
658 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600659}
660EXPORT_SYMBOL_GPL(iommu_group_add_device);
661
662/**
663 * iommu_group_remove_device - remove a device from it's current group
664 * @dev: device to be removed
665 *
666 * This function is called by an iommu driver to remove the device from
667 * it's current group. This decrements the iommu group reference count.
668 */
669void iommu_group_remove_device(struct device *dev)
670{
671 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100672 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600673
Joerg Roedel269aa802015-05-28 18:41:25 +0200674 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
675
Alex Williamsond72e31c2012-05-30 14:18:53 -0600676 /* Pre-notify listeners that a device is being removed. */
677 blocking_notifier_call_chain(&group->notifier,
678 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
679
680 mutex_lock(&group->mutex);
681 list_for_each_entry(tmp_device, &group->devices, list) {
682 if (tmp_device->dev == dev) {
683 device = tmp_device;
684 list_del(&device->list);
685 break;
686 }
687 }
688 mutex_unlock(&group->mutex);
689
690 if (!device)
691 return;
692
693 sysfs_remove_link(group->devices_kobj, device->name);
694 sysfs_remove_link(&dev->kobj, "iommu_group");
695
Shuah Khan2e757082013-08-15 11:59:25 -0600696 trace_remove_device_from_group(group->id, dev);
697
Alex Williamsond72e31c2012-05-30 14:18:53 -0600698 kfree(device->name);
699 kfree(device);
700 dev->iommu_group = NULL;
701 kobject_put(group->devices_kobj);
702}
703EXPORT_SYMBOL_GPL(iommu_group_remove_device);
704
Joerg Roedel426a2732015-05-28 18:41:30 +0200705static int iommu_group_device_count(struct iommu_group *group)
706{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100707 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200708 int ret = 0;
709
710 list_for_each_entry(entry, &group->devices, list)
711 ret++;
712
713 return ret;
714}
715
Alex Williamsond72e31c2012-05-30 14:18:53 -0600716/**
717 * iommu_group_for_each_dev - iterate over each device in the group
718 * @group: the group
719 * @data: caller opaque data to be passed to callback function
720 * @fn: caller supplied callback function
721 *
722 * This function is called by group users to iterate over group devices.
723 * Callers should hold a reference count to the group during callback.
724 * The group->mutex is held across callbacks, which will block calls to
725 * iommu_group_add/remove_device.
726 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200727static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
728 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600729{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100730 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600731 int ret = 0;
732
Alex Williamsond72e31c2012-05-30 14:18:53 -0600733 list_for_each_entry(device, &group->devices, list) {
734 ret = fn(device->dev, data);
735 if (ret)
736 break;
737 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200738 return ret;
739}
740
741
742int iommu_group_for_each_dev(struct iommu_group *group, void *data,
743 int (*fn)(struct device *, void *))
744{
745 int ret;
746
747 mutex_lock(&group->mutex);
748 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600749 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200750
Alex Williamsond72e31c2012-05-30 14:18:53 -0600751 return ret;
752}
753EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
754
755/**
756 * iommu_group_get - Return the group for a device and increment reference
757 * @dev: get the group that this device belongs to
758 *
759 * This function is called by iommu drivers and users to get the group
760 * for the specified device. If found, the group is returned and the group
761 * reference in incremented, else NULL.
762 */
763struct iommu_group *iommu_group_get(struct device *dev)
764{
765 struct iommu_group *group = dev->iommu_group;
766
767 if (group)
768 kobject_get(group->devices_kobj);
769
770 return group;
771}
772EXPORT_SYMBOL_GPL(iommu_group_get);
773
774/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000775 * iommu_group_ref_get - Increment reference on a group
776 * @group: the group to use, must not be NULL
777 *
778 * This function is called by iommu drivers to take additional references on an
779 * existing group. Returns the given group for convenience.
780 */
781struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
782{
783 kobject_get(group->devices_kobj);
784 return group;
785}
786
787/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600788 * iommu_group_put - Decrement group reference
789 * @group: the group to use
790 *
791 * This function is called by iommu drivers and users to release the
792 * iommu group. Once the reference count is zero, the group is released.
793 */
794void iommu_group_put(struct iommu_group *group)
795{
796 if (group)
797 kobject_put(group->devices_kobj);
798}
799EXPORT_SYMBOL_GPL(iommu_group_put);
800
801/**
802 * iommu_group_register_notifier - Register a notifier for group changes
803 * @group: the group to watch
804 * @nb: notifier block to signal
805 *
806 * This function allows iommu group users to track changes in a group.
807 * See include/linux/iommu.h for actions sent via this notifier. Caller
808 * should hold a reference to the group throughout notifier registration.
809 */
810int iommu_group_register_notifier(struct iommu_group *group,
811 struct notifier_block *nb)
812{
813 return blocking_notifier_chain_register(&group->notifier, nb);
814}
815EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
816
817/**
818 * iommu_group_unregister_notifier - Unregister a notifier
819 * @group: the group to watch
820 * @nb: notifier block to signal
821 *
822 * Unregister a previously registered group notifier block.
823 */
824int iommu_group_unregister_notifier(struct iommu_group *group,
825 struct notifier_block *nb)
826{
827 return blocking_notifier_chain_unregister(&group->notifier, nb);
828}
829EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
830
831/**
832 * iommu_group_id - Return ID for a group
833 * @group: the group to ID
834 *
835 * Return the unique ID for the group matching the sysfs group number.
836 */
837int iommu_group_id(struct iommu_group *group)
838{
839 return group->id;
840}
841EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400842
Alex Williamsonf096c062014-09-19 10:03:06 -0600843static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
844 unsigned long *devfns);
845
Alex Williamson104a1c12014-07-03 09:51:18 -0600846/*
847 * To consider a PCI device isolated, we require ACS to support Source
848 * Validation, Request Redirection, Completer Redirection, and Upstream
849 * Forwarding. This effectively means that devices cannot spoof their
850 * requester ID, requests and completions cannot be redirected, and all
851 * transactions are forwarded upstream, even as it passes through a
852 * bridge where the target device is downstream.
853 */
854#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
855
Alex Williamsonf096c062014-09-19 10:03:06 -0600856/*
857 * For multifunction devices which are not isolated from each other, find
858 * all the other non-isolated functions and look for existing groups. For
859 * each function, we also need to look for aliases to or from other devices
860 * that may already have a group.
861 */
862static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
863 unsigned long *devfns)
864{
865 struct pci_dev *tmp = NULL;
866 struct iommu_group *group;
867
868 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
869 return NULL;
870
871 for_each_pci_dev(tmp) {
872 if (tmp == pdev || tmp->bus != pdev->bus ||
873 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
874 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
875 continue;
876
877 group = get_pci_alias_group(tmp, devfns);
878 if (group) {
879 pci_dev_put(tmp);
880 return group;
881 }
882 }
883
884 return NULL;
885}
886
887/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100888 * Look for aliases to or from the given device for existing groups. DMA
889 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600890 * space is quite small (especially since we're really only looking at pcie
891 * device, and therefore only expect multiple slots on the root complex or
892 * downstream switch ports). It's conceivable though that a pair of
893 * multifunction devices could have aliases between them that would cause a
894 * loop. To prevent this, we use a bitmap to track where we've been.
895 */
896static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
897 unsigned long *devfns)
898{
899 struct pci_dev *tmp = NULL;
900 struct iommu_group *group;
901
902 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
903 return NULL;
904
905 group = iommu_group_get(&pdev->dev);
906 if (group)
907 return group;
908
909 for_each_pci_dev(tmp) {
910 if (tmp == pdev || tmp->bus != pdev->bus)
911 continue;
912
913 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100914 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600915 group = get_pci_alias_group(tmp, devfns);
916 if (group) {
917 pci_dev_put(tmp);
918 return group;
919 }
920
921 group = get_pci_function_alias_group(tmp, devfns);
922 if (group) {
923 pci_dev_put(tmp);
924 return group;
925 }
926 }
927 }
928
929 return NULL;
930}
931
Alex Williamson104a1c12014-07-03 09:51:18 -0600932struct group_for_pci_data {
933 struct pci_dev *pdev;
934 struct iommu_group *group;
935};
936
937/*
938 * DMA alias iterator callback, return the last seen device. Stop and return
939 * the IOMMU group if we find one along the way.
940 */
941static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
942{
943 struct group_for_pci_data *data = opaque;
944
945 data->pdev = pdev;
946 data->group = iommu_group_get(&pdev->dev);
947
948 return data->group != NULL;
949}
950
951/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200952 * Generic device_group call-back function. It just allocates one
953 * iommu-group per device.
954 */
955struct iommu_group *generic_device_group(struct device *dev)
956{
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200957 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +0200958}
959
960/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600961 * Use standard PCI bus topology, isolation features, and DMA alias quirks
962 * to find or create an IOMMU group for a device.
963 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200964struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600965{
Joerg Roedel5e622922015-10-21 23:51:37 +0200966 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600967 struct group_for_pci_data data;
968 struct pci_bus *bus;
969 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600970 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600971
Joerg Roedel5e622922015-10-21 23:51:37 +0200972 if (WARN_ON(!dev_is_pci(dev)))
973 return ERR_PTR(-EINVAL);
974
Alex Williamson104a1c12014-07-03 09:51:18 -0600975 /*
976 * Find the upstream DMA alias for the device. A device must not
977 * be aliased due to topology in order to have its own IOMMU group.
978 * If we find an alias along the way that already belongs to a
979 * group, use it.
980 */
981 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
982 return data.group;
983
984 pdev = data.pdev;
985
986 /*
987 * Continue upstream from the point of minimum IOMMU granularity
988 * due to aliases to the point where devices are protected from
989 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
990 * group, use it.
991 */
992 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
993 if (!bus->self)
994 continue;
995
996 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
997 break;
998
999 pdev = bus->self;
1000
1001 group = iommu_group_get(&pdev->dev);
1002 if (group)
1003 return group;
1004 }
1005
1006 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001007 * Look for existing groups on device aliases. If we alias another
1008 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001009 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001010 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1011 if (group)
1012 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001013
1014 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001015 * Look for existing groups on non-isolated functions on the same
1016 * slot and aliases of those funcions, if any. No need to clear
1017 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001018 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001019 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1020 if (group)
1021 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001022
1023 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001024 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001025}
1026
1027/**
1028 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1029 * @dev: target device
1030 *
1031 * This function is intended to be called by IOMMU drivers and extended to
1032 * support common, bus-defined algorithms when determining or creating the
1033 * IOMMU group for a device. On success, the caller will hold a reference
1034 * to the returned IOMMU group, which will already include the provided
1035 * device. The reference should be released with iommu_group_put().
1036 */
1037struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1038{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001039 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001040 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001041 int ret;
1042
1043 group = iommu_group_get(dev);
1044 if (group)
1045 return group;
1046
Robin Murphy05f803002017-07-21 13:12:38 +01001047 if (!ops)
1048 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001049
Robin Murphy05f803002017-07-21 13:12:38 +01001050 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001051 if (WARN_ON_ONCE(group == NULL))
1052 return ERR_PTR(-EINVAL);
1053
Alex Williamson104a1c12014-07-03 09:51:18 -06001054 if (IS_ERR(group))
1055 return group;
1056
Joerg Roedel12282362015-10-21 23:51:43 +02001057 /*
1058 * Try to allocate a default domain - needs support from the
1059 * IOMMU driver.
1060 */
1061 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001062 struct iommu_domain *dom;
1063
1064 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1065 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1066 dev_warn(dev,
1067 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1068 iommu_def_domain_type);
1069 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1070 }
1071
1072 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001073 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001074 group->domain = dom;
Joerg Roedel12282362015-10-21 23:51:43 +02001075 }
1076
Alex Williamson104a1c12014-07-03 09:51:18 -06001077 ret = iommu_group_add_device(group, dev);
1078 if (ret) {
1079 iommu_group_put(group);
1080 return ERR_PTR(ret);
1081 }
1082
1083 return group;
1084}
1085
Joerg Roedel6827ca82015-05-28 18:41:35 +02001086struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1087{
1088 return group->default_domain;
1089}
1090
Alex Williamson14604322011-10-21 15:56:05 -04001091static int add_iommu_group(struct device *dev, void *data)
1092{
Thierry Redingb22f6432014-06-27 09:03:12 +02001093 struct iommu_callback_data *cb = data;
1094 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +02001095 int ret;
Alex Williamson14604322011-10-21 15:56:05 -04001096
Alex Williamsond72e31c2012-05-30 14:18:53 -06001097 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +00001098 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001099
1100 WARN_ON(dev->iommu_group);
1101
Joerg Roedel38667f12015-06-29 10:16:08 +02001102 ret = ops->add_device(dev);
1103
1104 /*
1105 * We ignore -ENODEV errors for now, as they just mean that the
1106 * device is not translated by an IOMMU. We still care about
1107 * other errors and fail to initialize when they happen.
1108 */
1109 if (ret == -ENODEV)
1110 ret = 0;
1111
1112 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001113}
1114
Joerg Roedel8da30142015-05-28 18:41:27 +02001115static int remove_iommu_group(struct device *dev, void *data)
1116{
1117 struct iommu_callback_data *cb = data;
1118 const struct iommu_ops *ops = cb->ops;
1119
1120 if (ops->remove_device && dev->iommu_group)
1121 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001122
1123 return 0;
1124}
1125
Alex Williamsond72e31c2012-05-30 14:18:53 -06001126static int iommu_bus_notifier(struct notifier_block *nb,
1127 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001128{
1129 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +02001130 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001131 struct iommu_group *group;
1132 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -04001133
Alex Williamsond72e31c2012-05-30 14:18:53 -06001134 /*
1135 * ADD/DEL call into iommu driver ops if provided, which may
1136 * result in ADD/DEL notifiers to group->notifier
1137 */
1138 if (action == BUS_NOTIFY_ADD_DEVICE) {
zhichang.yuan3ba87752017-04-18 20:51:48 +08001139 if (ops->add_device) {
1140 int ret;
1141
1142 ret = ops->add_device(dev);
1143 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1144 }
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001145 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -06001146 if (ops->remove_device && dev->iommu_group) {
1147 ops->remove_device(dev);
1148 return 0;
1149 }
1150 }
Alex Williamson14604322011-10-21 15:56:05 -04001151
Alex Williamsond72e31c2012-05-30 14:18:53 -06001152 /*
1153 * Remaining BUS_NOTIFYs get filtered and republished to the
1154 * group, if anyone is listening
1155 */
1156 group = iommu_group_get(dev);
1157 if (!group)
1158 return 0;
1159
1160 switch (action) {
1161 case BUS_NOTIFY_BIND_DRIVER:
1162 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1163 break;
1164 case BUS_NOTIFY_BOUND_DRIVER:
1165 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1166 break;
1167 case BUS_NOTIFY_UNBIND_DRIVER:
1168 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1169 break;
1170 case BUS_NOTIFY_UNBOUND_DRIVER:
1171 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1172 break;
1173 }
1174
1175 if (group_action)
1176 blocking_notifier_call_chain(&group->notifier,
1177 group_action, dev);
1178
1179 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001180 return 0;
1181}
1182
Mark Salterfb3e3062014-09-21 13:58:24 -04001183static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001184{
Mark Salterfb3e3062014-09-21 13:58:24 -04001185 int err;
1186 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001187 struct iommu_callback_data cb = {
1188 .ops = ops,
1189 };
1190
Mark Salterfb3e3062014-09-21 13:58:24 -04001191 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1192 if (!nb)
1193 return -ENOMEM;
1194
1195 nb->notifier_call = iommu_bus_notifier;
1196
1197 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001198 if (err)
1199 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001200
1201 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001202 if (err)
1203 goto out_err;
1204
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001205
1206 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001207
1208out_err:
1209 /* Clean up */
1210 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1211 bus_unregister_notifier(bus, nb);
1212
1213out_free:
1214 kfree(nb);
1215
1216 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001217}
1218
Joerg Roedelff217762011-08-26 16:48:26 +02001219/**
1220 * bus_set_iommu - set iommu-callbacks for the bus
1221 * @bus: bus.
1222 * @ops: the callbacks provided by the iommu-driver
1223 *
1224 * This function is called by an iommu driver to set the iommu methods
1225 * used for a particular bus. Drivers for devices on that bus can use
1226 * the iommu-api after these ops are registered.
1227 * This special function is needed because IOMMUs are usually devices on
1228 * the bus itself, so the iommu drivers are not initialized when the bus
1229 * is set up. With this function the iommu-driver can set the iommu-ops
1230 * afterwards.
1231 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001232int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001233{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001234 int err;
1235
Joerg Roedelff217762011-08-26 16:48:26 +02001236 if (bus->iommu_ops != NULL)
1237 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001238
Joerg Roedelff217762011-08-26 16:48:26 +02001239 bus->iommu_ops = ops;
1240
1241 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001242 err = iommu_bus_init(bus, ops);
1243 if (err)
1244 bus->iommu_ops = NULL;
1245
1246 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001247}
Joerg Roedelff217762011-08-26 16:48:26 +02001248EXPORT_SYMBOL_GPL(bus_set_iommu);
1249
Joerg Roedela1b60c12011-09-06 18:46:34 +02001250bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001251{
Joerg Roedel94441c32011-09-06 18:58:54 +02001252 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001253}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001254EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001255
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001256bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1257{
1258 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1259 return false;
1260
1261 return bus->iommu_ops->capable(cap);
1262}
1263EXPORT_SYMBOL_GPL(iommu_capable);
1264
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001265/**
1266 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1267 * @domain: iommu domain
1268 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001269 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001270 *
1271 * This function should be used by IOMMU users which want to be notified
1272 * whenever an IOMMU fault happens.
1273 *
1274 * The fault handler itself should return 0 on success, and an appropriate
1275 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001276 */
1277void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001278 iommu_fault_handler_t handler,
1279 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001280{
1281 BUG_ON(!domain);
1282
1283 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001284 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001285}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001286EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001287
Joerg Roedel53723dc2015-05-28 18:41:29 +02001288static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1289 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001290{
1291 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001292
Joerg Roedel94441c32011-09-06 18:58:54 +02001293 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001294 return NULL;
1295
Joerg Roedel53723dc2015-05-28 18:41:29 +02001296 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001297 if (!domain)
1298 return NULL;
1299
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001300 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001301 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001302 /* Assume all sizes by default; the driver may override this later */
1303 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001304
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001305 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001306}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001307
Joerg Roedel53723dc2015-05-28 18:41:29 +02001308struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1309{
1310 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001311}
1312EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1313
1314void iommu_domain_free(struct iommu_domain *domain)
1315{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001316 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001317}
1318EXPORT_SYMBOL_GPL(iommu_domain_free);
1319
Joerg Roedel426a2732015-05-28 18:41:30 +02001320static int __iommu_attach_device(struct iommu_domain *domain,
1321 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001322{
Shuah Khanb54db772013-08-15 11:59:26 -06001323 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001324 if ((domain->ops->is_attach_deferred != NULL) &&
1325 domain->ops->is_attach_deferred(domain, dev))
1326 return 0;
1327
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001328 if (unlikely(domain->ops->attach_dev == NULL))
1329 return -ENODEV;
1330
Shuah Khanb54db772013-08-15 11:59:26 -06001331 ret = domain->ops->attach_dev(domain, dev);
1332 if (!ret)
1333 trace_attach_device_to_domain(dev);
1334 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001335}
Joerg Roedel426a2732015-05-28 18:41:30 +02001336
1337int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1338{
1339 struct iommu_group *group;
1340 int ret;
1341
1342 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001343 if (!group)
1344 return -ENODEV;
1345
Joerg Roedel426a2732015-05-28 18:41:30 +02001346 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001347 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001348 * change while we are attaching
1349 */
1350 mutex_lock(&group->mutex);
1351 ret = -EINVAL;
1352 if (iommu_group_device_count(group) != 1)
1353 goto out_unlock;
1354
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001355 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001356
1357out_unlock:
1358 mutex_unlock(&group->mutex);
1359 iommu_group_put(group);
1360
1361 return ret;
1362}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001363EXPORT_SYMBOL_GPL(iommu_attach_device);
1364
Joerg Roedel426a2732015-05-28 18:41:30 +02001365static void __iommu_detach_device(struct iommu_domain *domain,
1366 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001367{
Baoquan Hee01d1912017-08-09 16:33:40 +08001368 if ((domain->ops->is_attach_deferred != NULL) &&
1369 domain->ops->is_attach_deferred(domain, dev))
1370 return;
1371
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001372 if (unlikely(domain->ops->detach_dev == NULL))
1373 return;
1374
1375 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001376 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001377}
Joerg Roedel426a2732015-05-28 18:41:30 +02001378
1379void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1380{
1381 struct iommu_group *group;
1382
1383 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001384 if (!group)
1385 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001386
1387 mutex_lock(&group->mutex);
1388 if (iommu_group_device_count(group) != 1) {
1389 WARN_ON(1);
1390 goto out_unlock;
1391 }
1392
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001393 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001394
1395out_unlock:
1396 mutex_unlock(&group->mutex);
1397 iommu_group_put(group);
1398}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001399EXPORT_SYMBOL_GPL(iommu_detach_device);
1400
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001401struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1402{
1403 struct iommu_domain *domain;
1404 struct iommu_group *group;
1405
1406 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01001407 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001408 return NULL;
1409
1410 domain = group->domain;
1411
1412 iommu_group_put(group);
1413
1414 return domain;
1415}
1416EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1417
Alex Williamsond72e31c2012-05-30 14:18:53 -06001418/*
1419 * IOMMU groups are really the natrual working unit of the IOMMU, but
1420 * the IOMMU API works on domains and devices. Bridge that gap by
1421 * iterating over the devices in a group. Ideally we'd have a single
1422 * device which represents the requestor ID of the group, but we also
1423 * allow IOMMU drivers to create policy defined minimum sets, where
1424 * the physical hardware may be able to distiguish members, but we
1425 * wish to group them at a higher level (ex. untrusted multi-function
1426 * PCI devices). Thus we attach each device.
1427 */
1428static int iommu_group_do_attach_device(struct device *dev, void *data)
1429{
1430 struct iommu_domain *domain = data;
1431
Joerg Roedel426a2732015-05-28 18:41:30 +02001432 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001433}
1434
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001435static int __iommu_attach_group(struct iommu_domain *domain,
1436 struct iommu_group *group)
1437{
1438 int ret;
1439
1440 if (group->default_domain && group->domain != group->default_domain)
1441 return -EBUSY;
1442
1443 ret = __iommu_group_for_each_dev(group, domain,
1444 iommu_group_do_attach_device);
1445 if (ret == 0)
1446 group->domain = domain;
1447
1448 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001449}
1450
1451int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1452{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001453 int ret;
1454
1455 mutex_lock(&group->mutex);
1456 ret = __iommu_attach_group(domain, group);
1457 mutex_unlock(&group->mutex);
1458
1459 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001460}
1461EXPORT_SYMBOL_GPL(iommu_attach_group);
1462
1463static int iommu_group_do_detach_device(struct device *dev, void *data)
1464{
1465 struct iommu_domain *domain = data;
1466
Joerg Roedel426a2732015-05-28 18:41:30 +02001467 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001468
1469 return 0;
1470}
1471
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001472static void __iommu_detach_group(struct iommu_domain *domain,
1473 struct iommu_group *group)
1474{
1475 int ret;
1476
1477 if (!group->default_domain) {
1478 __iommu_group_for_each_dev(group, domain,
1479 iommu_group_do_detach_device);
1480 group->domain = NULL;
1481 return;
1482 }
1483
1484 if (group->domain == group->default_domain)
1485 return;
1486
1487 /* Detach by re-attaching to the default domain */
1488 ret = __iommu_group_for_each_dev(group, group->default_domain,
1489 iommu_group_do_attach_device);
1490 if (ret != 0)
1491 WARN_ON(1);
1492 else
1493 group->domain = group->default_domain;
1494}
1495
Alex Williamsond72e31c2012-05-30 14:18:53 -06001496void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1497{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001498 mutex_lock(&group->mutex);
1499 __iommu_detach_group(domain, group);
1500 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001501}
1502EXPORT_SYMBOL_GPL(iommu_detach_group);
1503
Varun Sethibb5547a2013-03-29 01:23:58 +05301504phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001505{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001506 if (unlikely(domain->ops->iova_to_phys == NULL))
1507 return 0;
1508
1509 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001510}
1511EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001512
Alex Williamsonbd139692013-06-17 19:57:34 -06001513static size_t iommu_pgsize(struct iommu_domain *domain,
1514 unsigned long addr_merge, size_t size)
1515{
1516 unsigned int pgsize_idx;
1517 size_t pgsize;
1518
1519 /* Max page size that still fits into 'size' */
1520 pgsize_idx = __fls(size);
1521
1522 /* need to consider alignment requirements ? */
1523 if (likely(addr_merge)) {
1524 /* Max page size allowed by address */
1525 unsigned int align_pgsize_idx = __ffs(addr_merge);
1526 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1527 }
1528
1529 /* build a mask of acceptable page sizes */
1530 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1531
1532 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001533 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001534
1535 /* make sure we're still sane */
1536 BUG_ON(!pgsize);
1537
1538 /* pick the biggest page */
1539 pgsize_idx = __fls(pgsize);
1540 pgsize = 1UL << pgsize_idx;
1541
1542 return pgsize;
1543}
1544
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001545int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001546 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001547{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001548 unsigned long orig_iova = iova;
1549 unsigned int min_pagesz;
1550 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001551 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001552 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001553
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001554 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001555 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001556 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001557
Joerg Roedela10315e2015-03-26 13:43:06 +01001558 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1559 return -EINVAL;
1560
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001561 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001562 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001563
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001564 /*
1565 * both the virtual address and the physical one, as well as
1566 * the size of the mapping, must be aligned (at least) to the
1567 * size of the smallest page supported by the hardware
1568 */
1569 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001570 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001571 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001572 return -EINVAL;
1573 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001574
Fabio Estevamabedb042013-08-22 10:25:42 -03001575 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001576
1577 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001578 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001579
Fabio Estevamabedb042013-08-22 10:25:42 -03001580 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001581 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001582
1583 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1584 if (ret)
1585 break;
1586
1587 iova += pgsize;
1588 paddr += pgsize;
1589 size -= pgsize;
1590 }
1591
1592 /* unroll mapping in case something went wrong */
1593 if (ret)
1594 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001595 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001596 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001597
1598 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001599}
1600EXPORT_SYMBOL_GPL(iommu_map);
1601
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001602static size_t __iommu_unmap(struct iommu_domain *domain,
1603 unsigned long iova, size_t size,
1604 bool sync)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001605{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001606 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001607 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001608 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001609 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001610
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001611 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001612 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001613 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001614
Joerg Roedela10315e2015-03-26 13:43:06 +01001615 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001616 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01001617
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001618 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001619 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001620
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001621 /*
1622 * The virtual address, as well as the size of the mapping, must be
1623 * aligned (at least) to the size of the smallest page supported
1624 * by the hardware
1625 */
1626 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001627 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1628 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001629 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001630 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001631
Joe Perches6197ca82013-06-23 12:29:04 -07001632 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001633
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001634 /*
1635 * Keep iterating until we either unmap 'size' bytes (or more)
1636 * or we hit an area that isn't mapped.
1637 */
1638 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001639 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001640
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001641 unmapped_page = ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001642 if (!unmapped_page)
1643 break;
1644
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001645 if (sync && ops->iotlb_range_add)
1646 ops->iotlb_range_add(domain, iova, pgsize);
1647
Joe Perches6197ca82013-06-23 12:29:04 -07001648 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1649 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001650
1651 iova += unmapped_page;
1652 unmapped += unmapped_page;
1653 }
1654
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001655 if (sync && ops->iotlb_sync)
1656 ops->iotlb_sync(domain);
1657
Shuah Khandb8614d2015-01-16 20:53:17 -07001658 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001659 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001660}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001661
1662size_t iommu_unmap(struct iommu_domain *domain,
1663 unsigned long iova, size_t size)
1664{
1665 return __iommu_unmap(domain, iova, size, true);
1666}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001667EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001668
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001669size_t iommu_unmap_fast(struct iommu_domain *domain,
1670 unsigned long iova, size_t size)
1671{
1672 return __iommu_unmap(domain, iova, size, false);
1673}
1674EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1675
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001676size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1677 struct scatterlist *sg, unsigned int nents, int prot)
Olav Haugan315786e2014-10-25 09:55:16 -07001678{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001679 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001680 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001681 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001682 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001683
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001684 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001685 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001686
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001687 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001688
1689 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001690 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001691
1692 /*
1693 * We are mapping on IOMMU page boundaries, so offset within
1694 * the page must be 0. However, the IOMMU may support pages
1695 * smaller than PAGE_SIZE, so s->offset may still represent
1696 * an offset of that boundary within the CPU page.
1697 */
1698 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001699 goto out_err;
1700
1701 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1702 if (ret)
1703 goto out_err;
1704
1705 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001706 }
1707
1708 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001709
1710out_err:
1711 /* undo mappings already done */
1712 iommu_unmap(domain, iova, mapped);
1713
1714 return 0;
1715
Olav Haugan315786e2014-10-25 09:55:16 -07001716}
Christoph Hellwigd88e61f2018-07-30 09:36:26 +02001717EXPORT_SYMBOL_GPL(iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001718
1719int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301720 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001721{
1722 if (unlikely(domain->ops->domain_window_enable == NULL))
1723 return -ENODEV;
1724
Varun Sethi80f97f02013-03-29 01:24:00 +05301725 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1726 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001727}
1728EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1729
1730void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1731{
1732 if (unlikely(domain->ops->domain_window_disable == NULL))
1733 return;
1734
1735 return domain->ops->domain_window_disable(domain, wnd_nr);
1736}
1737EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1738
Joerg Roedel207c6e32017-04-26 15:39:28 +02001739/**
1740 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1741 * @domain: the iommu domain where the fault has happened
1742 * @dev: the device where the fault has happened
1743 * @iova: the faulting address
1744 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1745 *
1746 * This function should be called by the low-level IOMMU implementations
1747 * whenever IOMMU faults happen, to allow high-level users, that are
1748 * interested in such events, to know about them.
1749 *
1750 * This event may be useful for several possible use cases:
1751 * - mere logging of the event
1752 * - dynamic TLB/PTE loading
1753 * - if restarting of the faulting device is required
1754 *
1755 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1756 * PTE/TLB loading will one day be supported, implementations will be able
1757 * to tell whether it succeeded or not according to this return value).
1758 *
1759 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1760 * (though fault handlers can also return -ENOSYS, in case they want to
1761 * elicit the default behavior of the IOMMU drivers).
1762 */
1763int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1764 unsigned long iova, int flags)
1765{
1766 int ret = -ENOSYS;
1767
1768 /*
1769 * if upper layers showed interest and installed a fault handler,
1770 * invoke it.
1771 */
1772 if (domain->handler)
1773 ret = domain->handler(domain, dev, iova, flags,
1774 domain->handler_token);
1775
1776 trace_io_page_fault(dev, iova, flags);
1777 return ret;
1778}
1779EXPORT_SYMBOL_GPL(report_iommu_fault);
1780
Alex Williamsond72e31c2012-05-30 14:18:53 -06001781static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001782{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001783 iommu_group_kset = kset_create_and_add("iommu_groups",
1784 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001785 BUG_ON(!iommu_group_kset);
1786
Gary R Hookbad614b2018-06-12 16:41:21 -05001787 iommu_debugfs_setup();
1788
Alex Williamsond72e31c2012-05-30 14:18:53 -06001789 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001790}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001791core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001792
1793int iommu_domain_get_attr(struct iommu_domain *domain,
1794 enum iommu_attr attr, void *data)
1795{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001796 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001797 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001798 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001799 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001800
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001801 switch (attr) {
1802 case DOMAIN_ATTR_GEOMETRY:
1803 geometry = data;
1804 *geometry = domain->geometry;
1805
1806 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001807 case DOMAIN_ATTR_PAGING:
1808 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001809 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001810 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001811 case DOMAIN_ATTR_WINDOWS:
1812 count = data;
1813
1814 if (domain->ops->domain_get_windows != NULL)
1815 *count = domain->ops->domain_get_windows(domain);
1816 else
1817 ret = -ENODEV;
1818
1819 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001820 default:
1821 if (!domain->ops->domain_get_attr)
1822 return -EINVAL;
1823
1824 ret = domain->ops->domain_get_attr(domain, attr, data);
1825 }
1826
1827 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001828}
1829EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1830
1831int iommu_domain_set_attr(struct iommu_domain *domain,
1832 enum iommu_attr attr, void *data)
1833{
Joerg Roedel69356712013-02-04 14:00:01 +01001834 int ret = 0;
1835 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001836
Joerg Roedel69356712013-02-04 14:00:01 +01001837 switch (attr) {
1838 case DOMAIN_ATTR_WINDOWS:
1839 count = data;
1840
1841 if (domain->ops->domain_set_windows != NULL)
1842 ret = domain->ops->domain_set_windows(domain, *count);
1843 else
1844 ret = -ENODEV;
1845
1846 break;
1847 default:
1848 if (domain->ops->domain_set_attr == NULL)
1849 return -EINVAL;
1850
1851 ret = domain->ops->domain_set_attr(domain, attr, data);
1852 }
1853
1854 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001855}
1856EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001857
Eric Augere5b52342017-01-19 20:57:47 +00001858void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001859{
1860 const struct iommu_ops *ops = dev->bus->iommu_ops;
1861
Eric Augere5b52342017-01-19 20:57:47 +00001862 if (ops && ops->get_resv_regions)
1863 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001864}
1865
Eric Augere5b52342017-01-19 20:57:47 +00001866void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001867{
1868 const struct iommu_ops *ops = dev->bus->iommu_ops;
1869
Eric Augere5b52342017-01-19 20:57:47 +00001870 if (ops && ops->put_resv_regions)
1871 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001872}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001873
Eric Auger2b20cbb2017-01-19 20:57:49 +00001874struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001875 size_t length, int prot,
1876 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00001877{
1878 struct iommu_resv_region *region;
1879
1880 region = kzalloc(sizeof(*region), GFP_KERNEL);
1881 if (!region)
1882 return NULL;
1883
1884 INIT_LIST_HEAD(&region->list);
1885 region->start = start;
1886 region->length = length;
1887 region->prot = prot;
1888 region->type = type;
1889 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001890}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001891
1892/* Request that a device is direct mapped by the IOMMU */
1893int iommu_request_dm_for_dev(struct device *dev)
1894{
1895 struct iommu_domain *dm_domain;
1896 struct iommu_group *group;
1897 int ret;
1898
1899 /* Device must already be in a group before calling this function */
1900 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001901 if (IS_ERR(group))
1902 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001903
1904 mutex_lock(&group->mutex);
1905
1906 /* Check if the default domain is already direct mapped */
1907 ret = 0;
1908 if (group->default_domain &&
1909 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1910 goto out;
1911
1912 /* Don't change mappings of existing devices */
1913 ret = -EBUSY;
1914 if (iommu_group_device_count(group) != 1)
1915 goto out;
1916
1917 /* Allocate a direct mapped domain */
1918 ret = -ENOMEM;
1919 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1920 if (!dm_domain)
1921 goto out;
1922
1923 /* Attach the device to the domain */
1924 ret = __iommu_attach_group(dm_domain, group);
1925 if (ret) {
1926 iommu_domain_free(dm_domain);
1927 goto out;
1928 }
1929
1930 /* Make the direct mapped domain the default for this group */
1931 if (group->default_domain)
1932 iommu_domain_free(group->default_domain);
1933 group->default_domain = dm_domain;
1934
1935 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1936
1937 ret = 0;
1938out:
1939 mutex_unlock(&group->mutex);
1940 iommu_group_put(group);
1941
1942 return ret;
1943}
Robin Murphy57f98d22016-09-13 10:54:14 +01001944
Joerg Roedel534766d2017-01-31 16:58:42 +01001945const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001946{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001947 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001948 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001949
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001950 spin_lock(&iommu_device_lock);
1951 list_for_each_entry(iommu, &iommu_device_list, list)
1952 if (iommu->fwnode == fwnode) {
1953 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001954 break;
1955 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001956 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001957 return ops;
1958}
1959
Robin Murphy57f98d22016-09-13 10:54:14 +01001960int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1961 const struct iommu_ops *ops)
1962{
1963 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1964
1965 if (fwspec)
1966 return ops == fwspec->ops ? 0 : -EINVAL;
1967
1968 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1969 if (!fwspec)
1970 return -ENOMEM;
1971
1972 of_node_get(to_of_node(iommu_fwnode));
1973 fwspec->iommu_fwnode = iommu_fwnode;
1974 fwspec->ops = ops;
1975 dev->iommu_fwspec = fwspec;
1976 return 0;
1977}
1978EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1979
1980void iommu_fwspec_free(struct device *dev)
1981{
1982 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1983
1984 if (fwspec) {
1985 fwnode_handle_put(fwspec->iommu_fwnode);
1986 kfree(fwspec);
1987 dev->iommu_fwspec = NULL;
1988 }
1989}
1990EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1991
1992int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1993{
1994 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1995 size_t size;
1996 int i;
1997
1998 if (!fwspec)
1999 return -EINVAL;
2000
2001 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2002 if (size > sizeof(*fwspec)) {
2003 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
2004 if (!fwspec)
2005 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002006
2007 dev->iommu_fwspec = fwspec;
Robin Murphy57f98d22016-09-13 10:54:14 +01002008 }
2009
2010 for (i = 0; i < num_ids; i++)
2011 fwspec->ids[fwspec->num_ids + i] = ids[i];
2012
2013 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01002014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);