blob: 7f50013b0bcf8cc4f9f7439a11ed975b1ae19a80 [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);
Will Deaconfccb4e32017-01-05 18:38:26 +000039static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
Alex Williamsond72e31c2012-05-30 14:18:53 -060040
Thierry Redingb22f6432014-06-27 09:03:12 +020041struct iommu_callback_data {
42 const struct iommu_ops *ops;
43};
44
Alex Williamsond72e31c2012-05-30 14:18:53 -060045struct iommu_group {
46 struct kobject kobj;
47 struct kobject *devices_kobj;
48 struct list_head devices;
49 struct mutex mutex;
50 struct blocking_notifier_head notifier;
51 void *iommu_data;
52 void (*iommu_data_release)(void *iommu_data);
53 char *name;
54 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020055 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020056 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060057};
58
Joerg Roedelc09e22d2017-02-01 12:19:46 +010059struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060060 struct list_head list;
61 struct device *dev;
62 char *name;
63};
64
65struct iommu_group_attribute {
66 struct attribute attr;
67 ssize_t (*show)(struct iommu_group *group, char *buf);
68 ssize_t (*store)(struct iommu_group *group,
69 const char *buf, size_t count);
70};
71
Eric Augerbc7d12b92017-01-19 20:57:52 +000072static const char * const iommu_group_resv_type_string[] = {
73 [IOMMU_RESV_DIRECT] = "direct",
74 [IOMMU_RESV_RESERVED] = "reserved",
75 [IOMMU_RESV_MSI] = "msi",
Robin Murphy9d3a4de2017-03-16 17:00:16 +000076 [IOMMU_RESV_SW_MSI] = "msi",
Eric Augerbc7d12b92017-01-19 20:57:52 +000077};
78
Alex Williamsond72e31c2012-05-30 14:18:53 -060079#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
80struct iommu_group_attribute iommu_group_attr_##_name = \
81 __ATTR(_name, _mode, _show, _store)
82
83#define to_iommu_group_attr(_attr) \
84 container_of(_attr, struct iommu_group_attribute, attr)
85#define to_iommu_group(_kobj) \
86 container_of(_kobj, struct iommu_group, kobj)
87
Joerg Roedelb0119e82017-02-01 13:23:08 +010088static LIST_HEAD(iommu_device_list);
89static DEFINE_SPINLOCK(iommu_device_lock);
90
91int iommu_device_register(struct iommu_device *iommu)
92{
93 spin_lock(&iommu_device_lock);
94 list_add_tail(&iommu->list, &iommu_device_list);
95 spin_unlock(&iommu_device_lock);
96
97 return 0;
98}
99
100void iommu_device_unregister(struct iommu_device *iommu)
101{
102 spin_lock(&iommu_device_lock);
103 list_del(&iommu->list);
104 spin_unlock(&iommu_device_lock);
105}
106
Joerg Roedel53723dc2015-05-28 18:41:29 +0200107static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
108 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200109static int __iommu_attach_device(struct iommu_domain *domain,
110 struct device *dev);
111static int __iommu_attach_group(struct iommu_domain *domain,
112 struct iommu_group *group);
113static void __iommu_detach_group(struct iommu_domain *domain,
114 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +0200115
Will Deaconfccb4e32017-01-05 18:38:26 +0000116static int __init iommu_set_def_domain_type(char *str)
117{
118 bool pt;
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300119 int ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000120
Andy Shevchenko7f9584d2018-05-14 19:22:25 +0300121 ret = kstrtobool(str, &pt);
122 if (ret)
123 return ret;
Will Deaconfccb4e32017-01-05 18:38:26 +0000124
125 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
126 return 0;
127}
128early_param("iommu.passthrough", iommu_set_def_domain_type);
129
Alex Williamsond72e31c2012-05-30 14:18:53 -0600130static ssize_t iommu_group_attr_show(struct kobject *kobj,
131 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400132{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600133 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
134 struct iommu_group *group = to_iommu_group(kobj);
135 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400136
Alex Williamsond72e31c2012-05-30 14:18:53 -0600137 if (attr->show)
138 ret = attr->show(group, buf);
139 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400140}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600141
142static ssize_t iommu_group_attr_store(struct kobject *kobj,
143 struct attribute *__attr,
144 const char *buf, size_t count)
145{
146 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
147 struct iommu_group *group = to_iommu_group(kobj);
148 ssize_t ret = -EIO;
149
150 if (attr->store)
151 ret = attr->store(group, buf, count);
152 return ret;
153}
154
155static const struct sysfs_ops iommu_group_sysfs_ops = {
156 .show = iommu_group_attr_show,
157 .store = iommu_group_attr_store,
158};
159
160static int iommu_group_create_file(struct iommu_group *group,
161 struct iommu_group_attribute *attr)
162{
163 return sysfs_create_file(&group->kobj, &attr->attr);
164}
165
166static void iommu_group_remove_file(struct iommu_group *group,
167 struct iommu_group_attribute *attr)
168{
169 sysfs_remove_file(&group->kobj, &attr->attr);
170}
171
172static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
173{
174 return sprintf(buf, "%s\n", group->name);
175}
176
Eric Auger6c65fb32017-01-19 20:57:51 +0000177/**
178 * iommu_insert_resv_region - Insert a new region in the
179 * list of reserved regions.
180 * @new: new region to insert
181 * @regions: list of regions
182 *
183 * The new element is sorted by address with respect to the other
184 * regions of the same type. In case it overlaps with another
185 * region of the same type, regions are merged. In case it
186 * overlaps with another region of different type, regions are
187 * not merged.
188 */
189static int iommu_insert_resv_region(struct iommu_resv_region *new,
190 struct list_head *regions)
191{
192 struct iommu_resv_region *region;
193 phys_addr_t start = new->start;
194 phys_addr_t end = new->start + new->length - 1;
195 struct list_head *pos = regions->next;
196
197 while (pos != regions) {
198 struct iommu_resv_region *entry =
199 list_entry(pos, struct iommu_resv_region, list);
200 phys_addr_t a = entry->start;
201 phys_addr_t b = entry->start + entry->length - 1;
202 int type = entry->type;
203
204 if (end < a) {
205 goto insert;
206 } else if (start > b) {
207 pos = pos->next;
208 } else if ((start >= a) && (end <= b)) {
209 if (new->type == type)
210 goto done;
211 else
212 pos = pos->next;
213 } else {
214 if (new->type == type) {
215 phys_addr_t new_start = min(a, start);
216 phys_addr_t new_end = max(b, end);
217
218 list_del(&entry->list);
219 entry->start = new_start;
220 entry->length = new_end - new_start + 1;
221 iommu_insert_resv_region(entry, regions);
222 } else {
223 pos = pos->next;
224 }
225 }
226 }
227insert:
228 region = iommu_alloc_resv_region(new->start, new->length,
229 new->prot, new->type);
230 if (!region)
231 return -ENOMEM;
232
233 list_add_tail(&region->list, pos);
234done:
235 return 0;
236}
237
238static int
239iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
240 struct list_head *group_resv_regions)
241{
242 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100243 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000244
245 list_for_each_entry(entry, dev_resv_regions, list) {
246 ret = iommu_insert_resv_region(entry, group_resv_regions);
247 if (ret)
248 break;
249 }
250 return ret;
251}
252
253int iommu_get_group_resv_regions(struct iommu_group *group,
254 struct list_head *head)
255{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100256 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000257 int ret = 0;
258
259 mutex_lock(&group->mutex);
260 list_for_each_entry(device, &group->devices, list) {
261 struct list_head dev_resv_regions;
262
263 INIT_LIST_HEAD(&dev_resv_regions);
264 iommu_get_resv_regions(device->dev, &dev_resv_regions);
265 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
266 iommu_put_resv_regions(device->dev, &dev_resv_regions);
267 if (ret)
268 break;
269 }
270 mutex_unlock(&group->mutex);
271 return ret;
272}
273EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
274
Eric Augerbc7d12b92017-01-19 20:57:52 +0000275static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
276 char *buf)
277{
278 struct iommu_resv_region *region, *next;
279 struct list_head group_resv_regions;
280 char *str = buf;
281
282 INIT_LIST_HEAD(&group_resv_regions);
283 iommu_get_group_resv_regions(group, &group_resv_regions);
284
285 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
286 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
287 (long long int)region->start,
288 (long long int)(region->start +
289 region->length - 1),
290 iommu_group_resv_type_string[region->type]);
291 kfree(region);
292 }
293
294 return (str - buf);
295}
296
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700297static ssize_t iommu_group_show_type(struct iommu_group *group,
298 char *buf)
299{
300 char *type = "unknown\n";
301
302 if (group->default_domain) {
303 switch (group->default_domain->type) {
304 case IOMMU_DOMAIN_BLOCKED:
305 type = "blocked\n";
306 break;
307 case IOMMU_DOMAIN_IDENTITY:
308 type = "identity\n";
309 break;
310 case IOMMU_DOMAIN_UNMANAGED:
311 type = "unmanaged\n";
312 break;
313 case IOMMU_DOMAIN_DMA:
314 type = "DMA";
315 break;
316 }
317 }
318 strcpy(buf, type);
319
320 return strlen(type);
321}
322
Alex Williamsond72e31c2012-05-30 14:18:53 -0600323static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
324
Eric Augerbc7d12b92017-01-19 20:57:52 +0000325static IOMMU_GROUP_ATTR(reserved_regions, 0444,
326 iommu_group_show_resv_regions, NULL);
327
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700328static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
329
Alex Williamsond72e31c2012-05-30 14:18:53 -0600330static void iommu_group_release(struct kobject *kobj)
331{
332 struct iommu_group *group = to_iommu_group(kobj);
333
Joerg Roedel269aa802015-05-28 18:41:25 +0200334 pr_debug("Releasing group %d\n", group->id);
335
Alex Williamsond72e31c2012-05-30 14:18:53 -0600336 if (group->iommu_data_release)
337 group->iommu_data_release(group->iommu_data);
338
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200339 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600340
Joerg Roedel53723dc2015-05-28 18:41:29 +0200341 if (group->default_domain)
342 iommu_domain_free(group->default_domain);
343
Alex Williamsond72e31c2012-05-30 14:18:53 -0600344 kfree(group->name);
345 kfree(group);
346}
347
348static struct kobj_type iommu_group_ktype = {
349 .sysfs_ops = &iommu_group_sysfs_ops,
350 .release = iommu_group_release,
351};
352
353/**
354 * iommu_group_alloc - Allocate a new group
Alex Williamsond72e31c2012-05-30 14:18:53 -0600355 *
356 * This function is called by an iommu driver to allocate a new iommu
357 * group. The iommu group represents the minimum granularity of the iommu.
358 * Upon successful return, the caller holds a reference to the supplied
359 * group in order to hold the group until devices are added. Use
360 * iommu_group_put() to release this extra reference count, allowing the
361 * group to be automatically reclaimed once it has no devices or external
362 * references.
363 */
364struct iommu_group *iommu_group_alloc(void)
365{
366 struct iommu_group *group;
367 int ret;
368
369 group = kzalloc(sizeof(*group), GFP_KERNEL);
370 if (!group)
371 return ERR_PTR(-ENOMEM);
372
373 group->kobj.kset = iommu_group_kset;
374 mutex_init(&group->mutex);
375 INIT_LIST_HEAD(&group->devices);
376 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
377
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200378 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
379 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600380 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200381 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600382 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200383 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600384
385 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
386 NULL, "%d", group->id);
387 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200388 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600389 kfree(group);
390 return ERR_PTR(ret);
391 }
392
393 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
394 if (!group->devices_kobj) {
395 kobject_put(&group->kobj); /* triggers .release & free */
396 return ERR_PTR(-ENOMEM);
397 }
398
399 /*
400 * The devices_kobj holds a reference on the group kobject, so
401 * as long as that exists so will the group. We can therefore
402 * use the devices_kobj for reference counting.
403 */
404 kobject_put(&group->kobj);
405
Eric Augerbc7d12b92017-01-19 20:57:52 +0000406 ret = iommu_group_create_file(group,
407 &iommu_group_attr_reserved_regions);
408 if (ret)
409 return ERR_PTR(ret);
410
Olof Johanssonc52c72d2018-07-11 13:59:36 -0700411 ret = iommu_group_create_file(group, &iommu_group_attr_type);
412 if (ret)
413 return ERR_PTR(ret);
414
Joerg Roedel269aa802015-05-28 18:41:25 +0200415 pr_debug("Allocated group %d\n", group->id);
416
Alex Williamsond72e31c2012-05-30 14:18:53 -0600417 return group;
418}
419EXPORT_SYMBOL_GPL(iommu_group_alloc);
420
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100421struct iommu_group *iommu_group_get_by_id(int id)
422{
423 struct kobject *group_kobj;
424 struct iommu_group *group;
425 const char *name;
426
427 if (!iommu_group_kset)
428 return NULL;
429
430 name = kasprintf(GFP_KERNEL, "%d", id);
431 if (!name)
432 return NULL;
433
434 group_kobj = kset_find_obj(iommu_group_kset, name);
435 kfree(name);
436
437 if (!group_kobj)
438 return NULL;
439
440 group = container_of(group_kobj, struct iommu_group, kobj);
441 BUG_ON(group->id != id);
442
443 kobject_get(group->devices_kobj);
444 kobject_put(&group->kobj);
445
446 return group;
447}
448EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
449
Alex Williamsond72e31c2012-05-30 14:18:53 -0600450/**
451 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
452 * @group: the group
453 *
454 * iommu drivers can store data in the group for use when doing iommu
455 * operations. This function provides a way to retrieve it. Caller
456 * should hold a group reference.
457 */
458void *iommu_group_get_iommudata(struct iommu_group *group)
459{
460 return group->iommu_data;
461}
462EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
463
464/**
465 * iommu_group_set_iommudata - set iommu_data for a group
466 * @group: the group
467 * @iommu_data: new data
468 * @release: release function for iommu_data
469 *
470 * iommu drivers can store data in the group for use when doing iommu
471 * operations. This function provides a way to set the data after
472 * the group has been allocated. Caller should hold a group reference.
473 */
474void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
475 void (*release)(void *iommu_data))
476{
477 group->iommu_data = iommu_data;
478 group->iommu_data_release = release;
479}
480EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
481
482/**
483 * iommu_group_set_name - set name for a group
484 * @group: the group
485 * @name: name
486 *
487 * Allow iommu driver to set a name for a group. When set it will
488 * appear in a name attribute file under the group in sysfs.
489 */
490int iommu_group_set_name(struct iommu_group *group, const char *name)
491{
492 int ret;
493
494 if (group->name) {
495 iommu_group_remove_file(group, &iommu_group_attr_name);
496 kfree(group->name);
497 group->name = NULL;
498 if (!name)
499 return 0;
500 }
501
502 group->name = kstrdup(name, GFP_KERNEL);
503 if (!group->name)
504 return -ENOMEM;
505
506 ret = iommu_group_create_file(group, &iommu_group_attr_name);
507 if (ret) {
508 kfree(group->name);
509 group->name = NULL;
510 return ret;
511 }
512
513 return 0;
514}
515EXPORT_SYMBOL_GPL(iommu_group_set_name);
516
Joerg Roedelbeed2822015-05-28 18:41:34 +0200517static int iommu_group_create_direct_mappings(struct iommu_group *group,
518 struct device *dev)
519{
520 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000521 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200522 struct list_head mappings;
523 unsigned long pg_size;
524 int ret = 0;
525
526 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
527 return 0;
528
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100529 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200530
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100531 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200532 INIT_LIST_HEAD(&mappings);
533
Eric Augere5b52342017-01-19 20:57:47 +0000534 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200535
536 /* We need to consider overlapping regions for different devices */
537 list_for_each_entry(entry, &mappings, list) {
538 dma_addr_t start, end, addr;
539
Eric Augere5b52342017-01-19 20:57:47 +0000540 if (domain->ops->apply_resv_region)
541 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200542
Joerg Roedelbeed2822015-05-28 18:41:34 +0200543 start = ALIGN(entry->start, pg_size);
544 end = ALIGN(entry->start + entry->length, pg_size);
545
Eric Auger544a25d2017-01-19 20:57:50 +0000546 if (entry->type != IOMMU_RESV_DIRECT)
547 continue;
548
Joerg Roedelbeed2822015-05-28 18:41:34 +0200549 for (addr = start; addr < end; addr += pg_size) {
550 phys_addr_t phys_addr;
551
552 phys_addr = iommu_iova_to_phys(domain, addr);
553 if (phys_addr)
554 continue;
555
556 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
557 if (ret)
558 goto out;
559 }
560
561 }
562
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200563 iommu_flush_tlb_all(domain);
564
Joerg Roedelbeed2822015-05-28 18:41:34 +0200565out:
Eric Augere5b52342017-01-19 20:57:47 +0000566 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200567
568 return ret;
569}
570
Alex Williamsond72e31c2012-05-30 14:18:53 -0600571/**
572 * iommu_group_add_device - add a device to an iommu group
573 * @group: the group into which to add the device (reference should be held)
574 * @dev: the device
575 *
576 * This function is called by an iommu driver to add a device into a
577 * group. Adding a device increments the group reference count.
578 */
579int iommu_group_add_device(struct iommu_group *group, struct device *dev)
580{
581 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100582 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600583
584 device = kzalloc(sizeof(*device), GFP_KERNEL);
585 if (!device)
586 return -ENOMEM;
587
588 device->dev = dev;
589
590 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000591 if (ret)
592 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600593
594 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
595rename:
596 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000597 ret = -ENOMEM;
598 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600599 }
600
601 ret = sysfs_create_link_nowarn(group->devices_kobj,
602 &dev->kobj, device->name);
603 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600604 if (ret == -EEXIST && i >= 0) {
605 /*
606 * Account for the slim chance of collision
607 * and append an instance to the name.
608 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000609 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600610 device->name = kasprintf(GFP_KERNEL, "%s.%d",
611 kobject_name(&dev->kobj), i++);
612 goto rename;
613 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000614 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600615 }
616
617 kobject_get(group->devices_kobj);
618
619 dev->iommu_group = group;
620
Joerg Roedelbeed2822015-05-28 18:41:34 +0200621 iommu_group_create_direct_mappings(group, dev);
622
Alex Williamsond72e31c2012-05-30 14:18:53 -0600623 mutex_lock(&group->mutex);
624 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200625 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000626 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600627 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000628 if (ret)
629 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600630
631 /* Notify any listeners about change to group. */
632 blocking_notifier_call_chain(&group->notifier,
633 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600634
635 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200636
637 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
638
Alex Williamsond72e31c2012-05-30 14:18:53 -0600639 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000640
641err_put_group:
642 mutex_lock(&group->mutex);
643 list_del(&device->list);
644 mutex_unlock(&group->mutex);
645 dev->iommu_group = NULL;
646 kobject_put(group->devices_kobj);
647err_free_name:
648 kfree(device->name);
649err_remove_link:
650 sysfs_remove_link(&dev->kobj, "iommu_group");
651err_free_device:
652 kfree(device);
653 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
654 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600655}
656EXPORT_SYMBOL_GPL(iommu_group_add_device);
657
658/**
659 * iommu_group_remove_device - remove a device from it's current group
660 * @dev: device to be removed
661 *
662 * This function is called by an iommu driver to remove the device from
663 * it's current group. This decrements the iommu group reference count.
664 */
665void iommu_group_remove_device(struct device *dev)
666{
667 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100668 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600669
Joerg Roedel269aa802015-05-28 18:41:25 +0200670 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
671
Alex Williamsond72e31c2012-05-30 14:18:53 -0600672 /* Pre-notify listeners that a device is being removed. */
673 blocking_notifier_call_chain(&group->notifier,
674 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
675
676 mutex_lock(&group->mutex);
677 list_for_each_entry(tmp_device, &group->devices, list) {
678 if (tmp_device->dev == dev) {
679 device = tmp_device;
680 list_del(&device->list);
681 break;
682 }
683 }
684 mutex_unlock(&group->mutex);
685
686 if (!device)
687 return;
688
689 sysfs_remove_link(group->devices_kobj, device->name);
690 sysfs_remove_link(&dev->kobj, "iommu_group");
691
Shuah Khan2e757082013-08-15 11:59:25 -0600692 trace_remove_device_from_group(group->id, dev);
693
Alex Williamsond72e31c2012-05-30 14:18:53 -0600694 kfree(device->name);
695 kfree(device);
696 dev->iommu_group = NULL;
697 kobject_put(group->devices_kobj);
698}
699EXPORT_SYMBOL_GPL(iommu_group_remove_device);
700
Joerg Roedel426a2732015-05-28 18:41:30 +0200701static int iommu_group_device_count(struct iommu_group *group)
702{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100703 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200704 int ret = 0;
705
706 list_for_each_entry(entry, &group->devices, list)
707 ret++;
708
709 return ret;
710}
711
Alex Williamsond72e31c2012-05-30 14:18:53 -0600712/**
713 * iommu_group_for_each_dev - iterate over each device in the group
714 * @group: the group
715 * @data: caller opaque data to be passed to callback function
716 * @fn: caller supplied callback function
717 *
718 * This function is called by group users to iterate over group devices.
719 * Callers should hold a reference count to the group during callback.
720 * The group->mutex is held across callbacks, which will block calls to
721 * iommu_group_add/remove_device.
722 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200723static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
724 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600725{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100726 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600727 int ret = 0;
728
Alex Williamsond72e31c2012-05-30 14:18:53 -0600729 list_for_each_entry(device, &group->devices, list) {
730 ret = fn(device->dev, data);
731 if (ret)
732 break;
733 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200734 return ret;
735}
736
737
738int iommu_group_for_each_dev(struct iommu_group *group, void *data,
739 int (*fn)(struct device *, void *))
740{
741 int ret;
742
743 mutex_lock(&group->mutex);
744 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600745 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200746
Alex Williamsond72e31c2012-05-30 14:18:53 -0600747 return ret;
748}
749EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
750
751/**
752 * iommu_group_get - Return the group for a device and increment reference
753 * @dev: get the group that this device belongs to
754 *
755 * This function is called by iommu drivers and users to get the group
756 * for the specified device. If found, the group is returned and the group
757 * reference in incremented, else NULL.
758 */
759struct iommu_group *iommu_group_get(struct device *dev)
760{
761 struct iommu_group *group = dev->iommu_group;
762
763 if (group)
764 kobject_get(group->devices_kobj);
765
766 return group;
767}
768EXPORT_SYMBOL_GPL(iommu_group_get);
769
770/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000771 * iommu_group_ref_get - Increment reference on a group
772 * @group: the group to use, must not be NULL
773 *
774 * This function is called by iommu drivers to take additional references on an
775 * existing group. Returns the given group for convenience.
776 */
777struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
778{
779 kobject_get(group->devices_kobj);
780 return group;
781}
782
783/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600784 * iommu_group_put - Decrement group reference
785 * @group: the group to use
786 *
787 * This function is called by iommu drivers and users to release the
788 * iommu group. Once the reference count is zero, the group is released.
789 */
790void iommu_group_put(struct iommu_group *group)
791{
792 if (group)
793 kobject_put(group->devices_kobj);
794}
795EXPORT_SYMBOL_GPL(iommu_group_put);
796
797/**
798 * iommu_group_register_notifier - Register a notifier for group changes
799 * @group: the group to watch
800 * @nb: notifier block to signal
801 *
802 * This function allows iommu group users to track changes in a group.
803 * See include/linux/iommu.h for actions sent via this notifier. Caller
804 * should hold a reference to the group throughout notifier registration.
805 */
806int iommu_group_register_notifier(struct iommu_group *group,
807 struct notifier_block *nb)
808{
809 return blocking_notifier_chain_register(&group->notifier, nb);
810}
811EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
812
813/**
814 * iommu_group_unregister_notifier - Unregister a notifier
815 * @group: the group to watch
816 * @nb: notifier block to signal
817 *
818 * Unregister a previously registered group notifier block.
819 */
820int iommu_group_unregister_notifier(struct iommu_group *group,
821 struct notifier_block *nb)
822{
823 return blocking_notifier_chain_unregister(&group->notifier, nb);
824}
825EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
826
827/**
828 * iommu_group_id - Return ID for a group
829 * @group: the group to ID
830 *
831 * Return the unique ID for the group matching the sysfs group number.
832 */
833int iommu_group_id(struct iommu_group *group)
834{
835 return group->id;
836}
837EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400838
Alex Williamsonf096c062014-09-19 10:03:06 -0600839static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
840 unsigned long *devfns);
841
Alex Williamson104a1c12014-07-03 09:51:18 -0600842/*
843 * To consider a PCI device isolated, we require ACS to support Source
844 * Validation, Request Redirection, Completer Redirection, and Upstream
845 * Forwarding. This effectively means that devices cannot spoof their
846 * requester ID, requests and completions cannot be redirected, and all
847 * transactions are forwarded upstream, even as it passes through a
848 * bridge where the target device is downstream.
849 */
850#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
851
Alex Williamsonf096c062014-09-19 10:03:06 -0600852/*
853 * For multifunction devices which are not isolated from each other, find
854 * all the other non-isolated functions and look for existing groups. For
855 * each function, we also need to look for aliases to or from other devices
856 * that may already have a group.
857 */
858static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
859 unsigned long *devfns)
860{
861 struct pci_dev *tmp = NULL;
862 struct iommu_group *group;
863
864 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
865 return NULL;
866
867 for_each_pci_dev(tmp) {
868 if (tmp == pdev || tmp->bus != pdev->bus ||
869 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
870 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
871 continue;
872
873 group = get_pci_alias_group(tmp, devfns);
874 if (group) {
875 pci_dev_put(tmp);
876 return group;
877 }
878 }
879
880 return NULL;
881}
882
883/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100884 * Look for aliases to or from the given device for existing groups. DMA
885 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600886 * space is quite small (especially since we're really only looking at pcie
887 * device, and therefore only expect multiple slots on the root complex or
888 * downstream switch ports). It's conceivable though that a pair of
889 * multifunction devices could have aliases between them that would cause a
890 * loop. To prevent this, we use a bitmap to track where we've been.
891 */
892static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
893 unsigned long *devfns)
894{
895 struct pci_dev *tmp = NULL;
896 struct iommu_group *group;
897
898 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
899 return NULL;
900
901 group = iommu_group_get(&pdev->dev);
902 if (group)
903 return group;
904
905 for_each_pci_dev(tmp) {
906 if (tmp == pdev || tmp->bus != pdev->bus)
907 continue;
908
909 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100910 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600911 group = get_pci_alias_group(tmp, devfns);
912 if (group) {
913 pci_dev_put(tmp);
914 return group;
915 }
916
917 group = get_pci_function_alias_group(tmp, devfns);
918 if (group) {
919 pci_dev_put(tmp);
920 return group;
921 }
922 }
923 }
924
925 return NULL;
926}
927
Alex Williamson104a1c12014-07-03 09:51:18 -0600928struct group_for_pci_data {
929 struct pci_dev *pdev;
930 struct iommu_group *group;
931};
932
933/*
934 * DMA alias iterator callback, return the last seen device. Stop and return
935 * the IOMMU group if we find one along the way.
936 */
937static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
938{
939 struct group_for_pci_data *data = opaque;
940
941 data->pdev = pdev;
942 data->group = iommu_group_get(&pdev->dev);
943
944 return data->group != NULL;
945}
946
947/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200948 * Generic device_group call-back function. It just allocates one
949 * iommu-group per device.
950 */
951struct iommu_group *generic_device_group(struct device *dev)
952{
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200953 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +0200954}
955
956/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600957 * Use standard PCI bus topology, isolation features, and DMA alias quirks
958 * to find or create an IOMMU group for a device.
959 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200960struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600961{
Joerg Roedel5e622922015-10-21 23:51:37 +0200962 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600963 struct group_for_pci_data data;
964 struct pci_bus *bus;
965 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600966 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600967
Joerg Roedel5e622922015-10-21 23:51:37 +0200968 if (WARN_ON(!dev_is_pci(dev)))
969 return ERR_PTR(-EINVAL);
970
Alex Williamson104a1c12014-07-03 09:51:18 -0600971 /*
972 * Find the upstream DMA alias for the device. A device must not
973 * be aliased due to topology in order to have its own IOMMU group.
974 * If we find an alias along the way that already belongs to a
975 * group, use it.
976 */
977 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
978 return data.group;
979
980 pdev = data.pdev;
981
982 /*
983 * Continue upstream from the point of minimum IOMMU granularity
984 * due to aliases to the point where devices are protected from
985 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
986 * group, use it.
987 */
988 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
989 if (!bus->self)
990 continue;
991
992 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
993 break;
994
995 pdev = bus->self;
996
997 group = iommu_group_get(&pdev->dev);
998 if (group)
999 return group;
1000 }
1001
1002 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001003 * Look for existing groups on device aliases. If we alias another
1004 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -06001005 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001006 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1007 if (group)
1008 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001009
1010 /*
Alex Williamsonf096c062014-09-19 10:03:06 -06001011 * Look for existing groups on non-isolated functions on the same
1012 * slot and aliases of those funcions, if any. No need to clear
1013 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -06001014 */
Alex Williamsonf096c062014-09-19 10:03:06 -06001015 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1016 if (group)
1017 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001018
1019 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +02001020 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -06001021}
1022
1023/**
1024 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1025 * @dev: target device
1026 *
1027 * This function is intended to be called by IOMMU drivers and extended to
1028 * support common, bus-defined algorithms when determining or creating the
1029 * IOMMU group for a device. On success, the caller will hold a reference
1030 * to the returned IOMMU group, which will already include the provided
1031 * device. The reference should be released with iommu_group_put().
1032 */
1033struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1034{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001035 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001036 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001037 int ret;
1038
1039 group = iommu_group_get(dev);
1040 if (group)
1041 return group;
1042
Robin Murphy05f803002017-07-21 13:12:38 +01001043 if (!ops)
1044 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001045
Robin Murphy05f803002017-07-21 13:12:38 +01001046 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001047 if (WARN_ON_ONCE(group == NULL))
1048 return ERR_PTR(-EINVAL);
1049
Alex Williamson104a1c12014-07-03 09:51:18 -06001050 if (IS_ERR(group))
1051 return group;
1052
Joerg Roedel12282362015-10-21 23:51:43 +02001053 /*
1054 * Try to allocate a default domain - needs support from the
1055 * IOMMU driver.
1056 */
1057 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001058 struct iommu_domain *dom;
1059
1060 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1061 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1062 dev_warn(dev,
1063 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1064 iommu_def_domain_type);
1065 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1066 }
1067
1068 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001069 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001070 group->domain = dom;
Joerg Roedel12282362015-10-21 23:51:43 +02001071 }
1072
Alex Williamson104a1c12014-07-03 09:51:18 -06001073 ret = iommu_group_add_device(group, dev);
1074 if (ret) {
1075 iommu_group_put(group);
1076 return ERR_PTR(ret);
1077 }
1078
1079 return group;
1080}
1081
Joerg Roedel6827ca82015-05-28 18:41:35 +02001082struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1083{
1084 return group->default_domain;
1085}
1086
Alex Williamson14604322011-10-21 15:56:05 -04001087static int add_iommu_group(struct device *dev, void *data)
1088{
Thierry Redingb22f6432014-06-27 09:03:12 +02001089 struct iommu_callback_data *cb = data;
1090 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +02001091 int ret;
Alex Williamson14604322011-10-21 15:56:05 -04001092
Alex Williamsond72e31c2012-05-30 14:18:53 -06001093 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +00001094 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001095
1096 WARN_ON(dev->iommu_group);
1097
Joerg Roedel38667f12015-06-29 10:16:08 +02001098 ret = ops->add_device(dev);
1099
1100 /*
1101 * We ignore -ENODEV errors for now, as they just mean that the
1102 * device is not translated by an IOMMU. We still care about
1103 * other errors and fail to initialize when they happen.
1104 */
1105 if (ret == -ENODEV)
1106 ret = 0;
1107
1108 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001109}
1110
Joerg Roedel8da30142015-05-28 18:41:27 +02001111static int remove_iommu_group(struct device *dev, void *data)
1112{
1113 struct iommu_callback_data *cb = data;
1114 const struct iommu_ops *ops = cb->ops;
1115
1116 if (ops->remove_device && dev->iommu_group)
1117 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001118
1119 return 0;
1120}
1121
Alex Williamsond72e31c2012-05-30 14:18:53 -06001122static int iommu_bus_notifier(struct notifier_block *nb,
1123 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001124{
1125 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +02001126 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001127 struct iommu_group *group;
1128 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -04001129
Alex Williamsond72e31c2012-05-30 14:18:53 -06001130 /*
1131 * ADD/DEL call into iommu driver ops if provided, which may
1132 * result in ADD/DEL notifiers to group->notifier
1133 */
1134 if (action == BUS_NOTIFY_ADD_DEVICE) {
zhichang.yuan3ba87752017-04-18 20:51:48 +08001135 if (ops->add_device) {
1136 int ret;
1137
1138 ret = ops->add_device(dev);
1139 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1140 }
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001141 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -06001142 if (ops->remove_device && dev->iommu_group) {
1143 ops->remove_device(dev);
1144 return 0;
1145 }
1146 }
Alex Williamson14604322011-10-21 15:56:05 -04001147
Alex Williamsond72e31c2012-05-30 14:18:53 -06001148 /*
1149 * Remaining BUS_NOTIFYs get filtered and republished to the
1150 * group, if anyone is listening
1151 */
1152 group = iommu_group_get(dev);
1153 if (!group)
1154 return 0;
1155
1156 switch (action) {
1157 case BUS_NOTIFY_BIND_DRIVER:
1158 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1159 break;
1160 case BUS_NOTIFY_BOUND_DRIVER:
1161 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1162 break;
1163 case BUS_NOTIFY_UNBIND_DRIVER:
1164 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1165 break;
1166 case BUS_NOTIFY_UNBOUND_DRIVER:
1167 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1168 break;
1169 }
1170
1171 if (group_action)
1172 blocking_notifier_call_chain(&group->notifier,
1173 group_action, dev);
1174
1175 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001176 return 0;
1177}
1178
Mark Salterfb3e3062014-09-21 13:58:24 -04001179static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001180{
Mark Salterfb3e3062014-09-21 13:58:24 -04001181 int err;
1182 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001183 struct iommu_callback_data cb = {
1184 .ops = ops,
1185 };
1186
Mark Salterfb3e3062014-09-21 13:58:24 -04001187 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1188 if (!nb)
1189 return -ENOMEM;
1190
1191 nb->notifier_call = iommu_bus_notifier;
1192
1193 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001194 if (err)
1195 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001196
1197 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001198 if (err)
1199 goto out_err;
1200
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001201
1202 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001203
1204out_err:
1205 /* Clean up */
1206 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1207 bus_unregister_notifier(bus, nb);
1208
1209out_free:
1210 kfree(nb);
1211
1212 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001213}
1214
Joerg Roedelff217762011-08-26 16:48:26 +02001215/**
1216 * bus_set_iommu - set iommu-callbacks for the bus
1217 * @bus: bus.
1218 * @ops: the callbacks provided by the iommu-driver
1219 *
1220 * This function is called by an iommu driver to set the iommu methods
1221 * used for a particular bus. Drivers for devices on that bus can use
1222 * the iommu-api after these ops are registered.
1223 * This special function is needed because IOMMUs are usually devices on
1224 * the bus itself, so the iommu drivers are not initialized when the bus
1225 * is set up. With this function the iommu-driver can set the iommu-ops
1226 * afterwards.
1227 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001228int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001229{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001230 int err;
1231
Joerg Roedelff217762011-08-26 16:48:26 +02001232 if (bus->iommu_ops != NULL)
1233 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001234
Joerg Roedelff217762011-08-26 16:48:26 +02001235 bus->iommu_ops = ops;
1236
1237 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001238 err = iommu_bus_init(bus, ops);
1239 if (err)
1240 bus->iommu_ops = NULL;
1241
1242 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001243}
Joerg Roedelff217762011-08-26 16:48:26 +02001244EXPORT_SYMBOL_GPL(bus_set_iommu);
1245
Joerg Roedela1b60c12011-09-06 18:46:34 +02001246bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001247{
Joerg Roedel94441c32011-09-06 18:58:54 +02001248 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001249}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001250EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001251
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001252bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1253{
1254 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1255 return false;
1256
1257 return bus->iommu_ops->capable(cap);
1258}
1259EXPORT_SYMBOL_GPL(iommu_capable);
1260
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001261/**
1262 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1263 * @domain: iommu domain
1264 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001265 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001266 *
1267 * This function should be used by IOMMU users which want to be notified
1268 * whenever an IOMMU fault happens.
1269 *
1270 * The fault handler itself should return 0 on success, and an appropriate
1271 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001272 */
1273void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001274 iommu_fault_handler_t handler,
1275 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001276{
1277 BUG_ON(!domain);
1278
1279 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001280 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001281}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001282EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001283
Joerg Roedel53723dc2015-05-28 18:41:29 +02001284static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1285 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001286{
1287 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001288
Joerg Roedel94441c32011-09-06 18:58:54 +02001289 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001290 return NULL;
1291
Joerg Roedel53723dc2015-05-28 18:41:29 +02001292 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001293 if (!domain)
1294 return NULL;
1295
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001296 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001297 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001298 /* Assume all sizes by default; the driver may override this later */
1299 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001300
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001301 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001302}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001303
Joerg Roedel53723dc2015-05-28 18:41:29 +02001304struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1305{
1306 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001307}
1308EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1309
1310void iommu_domain_free(struct iommu_domain *domain)
1311{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001312 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001313}
1314EXPORT_SYMBOL_GPL(iommu_domain_free);
1315
Joerg Roedel426a2732015-05-28 18:41:30 +02001316static int __iommu_attach_device(struct iommu_domain *domain,
1317 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001318{
Shuah Khanb54db772013-08-15 11:59:26 -06001319 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001320 if ((domain->ops->is_attach_deferred != NULL) &&
1321 domain->ops->is_attach_deferred(domain, dev))
1322 return 0;
1323
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001324 if (unlikely(domain->ops->attach_dev == NULL))
1325 return -ENODEV;
1326
Shuah Khanb54db772013-08-15 11:59:26 -06001327 ret = domain->ops->attach_dev(domain, dev);
1328 if (!ret)
1329 trace_attach_device_to_domain(dev);
1330 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001331}
Joerg Roedel426a2732015-05-28 18:41:30 +02001332
1333int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1334{
1335 struct iommu_group *group;
1336 int ret;
1337
1338 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001339 if (!group)
1340 return -ENODEV;
1341
Joerg Roedel426a2732015-05-28 18:41:30 +02001342 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001343 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001344 * change while we are attaching
1345 */
1346 mutex_lock(&group->mutex);
1347 ret = -EINVAL;
1348 if (iommu_group_device_count(group) != 1)
1349 goto out_unlock;
1350
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001351 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001352
1353out_unlock:
1354 mutex_unlock(&group->mutex);
1355 iommu_group_put(group);
1356
1357 return ret;
1358}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001359EXPORT_SYMBOL_GPL(iommu_attach_device);
1360
Joerg Roedel426a2732015-05-28 18:41:30 +02001361static void __iommu_detach_device(struct iommu_domain *domain,
1362 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001363{
Baoquan Hee01d1912017-08-09 16:33:40 +08001364 if ((domain->ops->is_attach_deferred != NULL) &&
1365 domain->ops->is_attach_deferred(domain, dev))
1366 return;
1367
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001368 if (unlikely(domain->ops->detach_dev == NULL))
1369 return;
1370
1371 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001372 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001373}
Joerg Roedel426a2732015-05-28 18:41:30 +02001374
1375void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1376{
1377 struct iommu_group *group;
1378
1379 group = iommu_group_get(dev);
Jordan Crouse9ae9df02017-12-20 09:48:36 -07001380 if (!group)
1381 return;
Joerg Roedel426a2732015-05-28 18:41:30 +02001382
1383 mutex_lock(&group->mutex);
1384 if (iommu_group_device_count(group) != 1) {
1385 WARN_ON(1);
1386 goto out_unlock;
1387 }
1388
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001389 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001390
1391out_unlock:
1392 mutex_unlock(&group->mutex);
1393 iommu_group_put(group);
1394}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001395EXPORT_SYMBOL_GPL(iommu_detach_device);
1396
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001397struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1398{
1399 struct iommu_domain *domain;
1400 struct iommu_group *group;
1401
1402 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01001403 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001404 return NULL;
1405
1406 domain = group->domain;
1407
1408 iommu_group_put(group);
1409
1410 return domain;
1411}
1412EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1413
Alex Williamsond72e31c2012-05-30 14:18:53 -06001414/*
1415 * IOMMU groups are really the natrual working unit of the IOMMU, but
1416 * the IOMMU API works on domains and devices. Bridge that gap by
1417 * iterating over the devices in a group. Ideally we'd have a single
1418 * device which represents the requestor ID of the group, but we also
1419 * allow IOMMU drivers to create policy defined minimum sets, where
1420 * the physical hardware may be able to distiguish members, but we
1421 * wish to group them at a higher level (ex. untrusted multi-function
1422 * PCI devices). Thus we attach each device.
1423 */
1424static int iommu_group_do_attach_device(struct device *dev, void *data)
1425{
1426 struct iommu_domain *domain = data;
1427
Joerg Roedel426a2732015-05-28 18:41:30 +02001428 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001429}
1430
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001431static int __iommu_attach_group(struct iommu_domain *domain,
1432 struct iommu_group *group)
1433{
1434 int ret;
1435
1436 if (group->default_domain && group->domain != group->default_domain)
1437 return -EBUSY;
1438
1439 ret = __iommu_group_for_each_dev(group, domain,
1440 iommu_group_do_attach_device);
1441 if (ret == 0)
1442 group->domain = domain;
1443
1444 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001445}
1446
1447int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1448{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001449 int ret;
1450
1451 mutex_lock(&group->mutex);
1452 ret = __iommu_attach_group(domain, group);
1453 mutex_unlock(&group->mutex);
1454
1455 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001456}
1457EXPORT_SYMBOL_GPL(iommu_attach_group);
1458
1459static int iommu_group_do_detach_device(struct device *dev, void *data)
1460{
1461 struct iommu_domain *domain = data;
1462
Joerg Roedel426a2732015-05-28 18:41:30 +02001463 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001464
1465 return 0;
1466}
1467
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001468static void __iommu_detach_group(struct iommu_domain *domain,
1469 struct iommu_group *group)
1470{
1471 int ret;
1472
1473 if (!group->default_domain) {
1474 __iommu_group_for_each_dev(group, domain,
1475 iommu_group_do_detach_device);
1476 group->domain = NULL;
1477 return;
1478 }
1479
1480 if (group->domain == group->default_domain)
1481 return;
1482
1483 /* Detach by re-attaching to the default domain */
1484 ret = __iommu_group_for_each_dev(group, group->default_domain,
1485 iommu_group_do_attach_device);
1486 if (ret != 0)
1487 WARN_ON(1);
1488 else
1489 group->domain = group->default_domain;
1490}
1491
Alex Williamsond72e31c2012-05-30 14:18:53 -06001492void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1493{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001494 mutex_lock(&group->mutex);
1495 __iommu_detach_group(domain, group);
1496 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001497}
1498EXPORT_SYMBOL_GPL(iommu_detach_group);
1499
Varun Sethibb5547a2013-03-29 01:23:58 +05301500phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001501{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001502 if (unlikely(domain->ops->iova_to_phys == NULL))
1503 return 0;
1504
1505 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001506}
1507EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001508
Alex Williamsonbd139692013-06-17 19:57:34 -06001509static size_t iommu_pgsize(struct iommu_domain *domain,
1510 unsigned long addr_merge, size_t size)
1511{
1512 unsigned int pgsize_idx;
1513 size_t pgsize;
1514
1515 /* Max page size that still fits into 'size' */
1516 pgsize_idx = __fls(size);
1517
1518 /* need to consider alignment requirements ? */
1519 if (likely(addr_merge)) {
1520 /* Max page size allowed by address */
1521 unsigned int align_pgsize_idx = __ffs(addr_merge);
1522 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1523 }
1524
1525 /* build a mask of acceptable page sizes */
1526 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1527
1528 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001529 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001530
1531 /* make sure we're still sane */
1532 BUG_ON(!pgsize);
1533
1534 /* pick the biggest page */
1535 pgsize_idx = __fls(pgsize);
1536 pgsize = 1UL << pgsize_idx;
1537
1538 return pgsize;
1539}
1540
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001541int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001542 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001543{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001544 unsigned long orig_iova = iova;
1545 unsigned int min_pagesz;
1546 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001547 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001548 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001549
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001550 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001551 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001552 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001553
Joerg Roedela10315e2015-03-26 13:43:06 +01001554 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1555 return -EINVAL;
1556
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001557 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001558 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001559
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001560 /*
1561 * both the virtual address and the physical one, as well as
1562 * the size of the mapping, must be aligned (at least) to the
1563 * size of the smallest page supported by the hardware
1564 */
1565 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001566 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001567 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001568 return -EINVAL;
1569 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001570
Fabio Estevamabedb042013-08-22 10:25:42 -03001571 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001572
1573 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001574 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001575
Fabio Estevamabedb042013-08-22 10:25:42 -03001576 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001577 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001578
1579 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1580 if (ret)
1581 break;
1582
1583 iova += pgsize;
1584 paddr += pgsize;
1585 size -= pgsize;
1586 }
1587
1588 /* unroll mapping in case something went wrong */
1589 if (ret)
1590 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001591 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001592 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001593
1594 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001595}
1596EXPORT_SYMBOL_GPL(iommu_map);
1597
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001598static size_t __iommu_unmap(struct iommu_domain *domain,
1599 unsigned long iova, size_t size,
1600 bool sync)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001601{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001602 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001603 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001604 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001605 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001606
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001607 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001608 domain->pgsize_bitmap == 0UL))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001609 return 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001610
Joerg Roedela10315e2015-03-26 13:43:06 +01001611 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001612 return 0;
Joerg Roedela10315e2015-03-26 13:43:06 +01001613
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001614 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001615 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001616
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001617 /*
1618 * The virtual address, as well as the size of the mapping, must be
1619 * aligned (at least) to the size of the smallest page supported
1620 * by the hardware
1621 */
1622 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001623 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1624 iova, size, min_pagesz);
Suravee Suthikulpanitc5611a82018-02-05 05:45:53 -05001625 return 0;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001626 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001627
Joe Perches6197ca82013-06-23 12:29:04 -07001628 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001629
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001630 /*
1631 * Keep iterating until we either unmap 'size' bytes (or more)
1632 * or we hit an area that isn't mapped.
1633 */
1634 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001635 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001636
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001637 unmapped_page = ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001638 if (!unmapped_page)
1639 break;
1640
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001641 if (sync && ops->iotlb_range_add)
1642 ops->iotlb_range_add(domain, iova, pgsize);
1643
Joe Perches6197ca82013-06-23 12:29:04 -07001644 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1645 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001646
1647 iova += unmapped_page;
1648 unmapped += unmapped_page;
1649 }
1650
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001651 if (sync && ops->iotlb_sync)
1652 ops->iotlb_sync(domain);
1653
Shuah Khandb8614d2015-01-16 20:53:17 -07001654 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001655 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001656}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001657
1658size_t iommu_unmap(struct iommu_domain *domain,
1659 unsigned long iova, size_t size)
1660{
1661 return __iommu_unmap(domain, iova, size, true);
1662}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001663EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001664
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001665size_t iommu_unmap_fast(struct iommu_domain *domain,
1666 unsigned long iova, size_t size)
1667{
1668 return __iommu_unmap(domain, iova, size, false);
1669}
1670EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1671
Olav Haugan315786e2014-10-25 09:55:16 -07001672size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1673 struct scatterlist *sg, unsigned int nents, int prot)
1674{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001675 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001676 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001677 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001678 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001679
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001680 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001681 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001682
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001683 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001684
1685 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001686 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001687
1688 /*
1689 * We are mapping on IOMMU page boundaries, so offset within
1690 * the page must be 0. However, the IOMMU may support pages
1691 * smaller than PAGE_SIZE, so s->offset may still represent
1692 * an offset of that boundary within the CPU page.
1693 */
1694 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001695 goto out_err;
1696
1697 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1698 if (ret)
1699 goto out_err;
1700
1701 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001702 }
1703
1704 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001705
1706out_err:
1707 /* undo mappings already done */
1708 iommu_unmap(domain, iova, mapped);
1709
1710 return 0;
1711
Olav Haugan315786e2014-10-25 09:55:16 -07001712}
1713EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001714
1715int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301716 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001717{
1718 if (unlikely(domain->ops->domain_window_enable == NULL))
1719 return -ENODEV;
1720
Varun Sethi80f97f02013-03-29 01:24:00 +05301721 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1722 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001723}
1724EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1725
1726void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1727{
1728 if (unlikely(domain->ops->domain_window_disable == NULL))
1729 return;
1730
1731 return domain->ops->domain_window_disable(domain, wnd_nr);
1732}
1733EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1734
Joerg Roedel207c6e32017-04-26 15:39:28 +02001735/**
1736 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1737 * @domain: the iommu domain where the fault has happened
1738 * @dev: the device where the fault has happened
1739 * @iova: the faulting address
1740 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1741 *
1742 * This function should be called by the low-level IOMMU implementations
1743 * whenever IOMMU faults happen, to allow high-level users, that are
1744 * interested in such events, to know about them.
1745 *
1746 * This event may be useful for several possible use cases:
1747 * - mere logging of the event
1748 * - dynamic TLB/PTE loading
1749 * - if restarting of the faulting device is required
1750 *
1751 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1752 * PTE/TLB loading will one day be supported, implementations will be able
1753 * to tell whether it succeeded or not according to this return value).
1754 *
1755 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1756 * (though fault handlers can also return -ENOSYS, in case they want to
1757 * elicit the default behavior of the IOMMU drivers).
1758 */
1759int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1760 unsigned long iova, int flags)
1761{
1762 int ret = -ENOSYS;
1763
1764 /*
1765 * if upper layers showed interest and installed a fault handler,
1766 * invoke it.
1767 */
1768 if (domain->handler)
1769 ret = domain->handler(domain, dev, iova, flags,
1770 domain->handler_token);
1771
1772 trace_io_page_fault(dev, iova, flags);
1773 return ret;
1774}
1775EXPORT_SYMBOL_GPL(report_iommu_fault);
1776
Alex Williamsond72e31c2012-05-30 14:18:53 -06001777static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001778{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001779 iommu_group_kset = kset_create_and_add("iommu_groups",
1780 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001781 BUG_ON(!iommu_group_kset);
1782
Gary R Hookbad614b2018-06-12 16:41:21 -05001783 iommu_debugfs_setup();
1784
Alex Williamsond72e31c2012-05-30 14:18:53 -06001785 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001786}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001787core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001788
1789int iommu_domain_get_attr(struct iommu_domain *domain,
1790 enum iommu_attr attr, void *data)
1791{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001792 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001793 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001794 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001795 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001796
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001797 switch (attr) {
1798 case DOMAIN_ATTR_GEOMETRY:
1799 geometry = data;
1800 *geometry = domain->geometry;
1801
1802 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001803 case DOMAIN_ATTR_PAGING:
1804 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001805 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001806 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001807 case DOMAIN_ATTR_WINDOWS:
1808 count = data;
1809
1810 if (domain->ops->domain_get_windows != NULL)
1811 *count = domain->ops->domain_get_windows(domain);
1812 else
1813 ret = -ENODEV;
1814
1815 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001816 default:
1817 if (!domain->ops->domain_get_attr)
1818 return -EINVAL;
1819
1820 ret = domain->ops->domain_get_attr(domain, attr, data);
1821 }
1822
1823 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001824}
1825EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1826
1827int iommu_domain_set_attr(struct iommu_domain *domain,
1828 enum iommu_attr attr, void *data)
1829{
Joerg Roedel69356712013-02-04 14:00:01 +01001830 int ret = 0;
1831 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001832
Joerg Roedel69356712013-02-04 14:00:01 +01001833 switch (attr) {
1834 case DOMAIN_ATTR_WINDOWS:
1835 count = data;
1836
1837 if (domain->ops->domain_set_windows != NULL)
1838 ret = domain->ops->domain_set_windows(domain, *count);
1839 else
1840 ret = -ENODEV;
1841
1842 break;
1843 default:
1844 if (domain->ops->domain_set_attr == NULL)
1845 return -EINVAL;
1846
1847 ret = domain->ops->domain_set_attr(domain, attr, data);
1848 }
1849
1850 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001851}
1852EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001853
Eric Augere5b52342017-01-19 20:57:47 +00001854void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001855{
1856 const struct iommu_ops *ops = dev->bus->iommu_ops;
1857
Eric Augere5b52342017-01-19 20:57:47 +00001858 if (ops && ops->get_resv_regions)
1859 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001860}
1861
Eric Augere5b52342017-01-19 20:57:47 +00001862void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001863{
1864 const struct iommu_ops *ops = dev->bus->iommu_ops;
1865
Eric Augere5b52342017-01-19 20:57:47 +00001866 if (ops && ops->put_resv_regions)
1867 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001868}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001869
Eric Auger2b20cbb2017-01-19 20:57:49 +00001870struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001871 size_t length, int prot,
1872 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00001873{
1874 struct iommu_resv_region *region;
1875
1876 region = kzalloc(sizeof(*region), GFP_KERNEL);
1877 if (!region)
1878 return NULL;
1879
1880 INIT_LIST_HEAD(&region->list);
1881 region->start = start;
1882 region->length = length;
1883 region->prot = prot;
1884 region->type = type;
1885 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001886}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001887
1888/* Request that a device is direct mapped by the IOMMU */
1889int iommu_request_dm_for_dev(struct device *dev)
1890{
1891 struct iommu_domain *dm_domain;
1892 struct iommu_group *group;
1893 int ret;
1894
1895 /* Device must already be in a group before calling this function */
1896 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001897 if (IS_ERR(group))
1898 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001899
1900 mutex_lock(&group->mutex);
1901
1902 /* Check if the default domain is already direct mapped */
1903 ret = 0;
1904 if (group->default_domain &&
1905 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1906 goto out;
1907
1908 /* Don't change mappings of existing devices */
1909 ret = -EBUSY;
1910 if (iommu_group_device_count(group) != 1)
1911 goto out;
1912
1913 /* Allocate a direct mapped domain */
1914 ret = -ENOMEM;
1915 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1916 if (!dm_domain)
1917 goto out;
1918
1919 /* Attach the device to the domain */
1920 ret = __iommu_attach_group(dm_domain, group);
1921 if (ret) {
1922 iommu_domain_free(dm_domain);
1923 goto out;
1924 }
1925
1926 /* Make the direct mapped domain the default for this group */
1927 if (group->default_domain)
1928 iommu_domain_free(group->default_domain);
1929 group->default_domain = dm_domain;
1930
1931 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1932
1933 ret = 0;
1934out:
1935 mutex_unlock(&group->mutex);
1936 iommu_group_put(group);
1937
1938 return ret;
1939}
Robin Murphy57f98d22016-09-13 10:54:14 +01001940
Joerg Roedel534766d2017-01-31 16:58:42 +01001941const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001942{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001943 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001944 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001945
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001946 spin_lock(&iommu_device_lock);
1947 list_for_each_entry(iommu, &iommu_device_list, list)
1948 if (iommu->fwnode == fwnode) {
1949 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001950 break;
1951 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001952 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001953 return ops;
1954}
1955
Robin Murphy57f98d22016-09-13 10:54:14 +01001956int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1957 const struct iommu_ops *ops)
1958{
1959 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1960
1961 if (fwspec)
1962 return ops == fwspec->ops ? 0 : -EINVAL;
1963
1964 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1965 if (!fwspec)
1966 return -ENOMEM;
1967
1968 of_node_get(to_of_node(iommu_fwnode));
1969 fwspec->iommu_fwnode = iommu_fwnode;
1970 fwspec->ops = ops;
1971 dev->iommu_fwspec = fwspec;
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1975
1976void iommu_fwspec_free(struct device *dev)
1977{
1978 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1979
1980 if (fwspec) {
1981 fwnode_handle_put(fwspec->iommu_fwnode);
1982 kfree(fwspec);
1983 dev->iommu_fwspec = NULL;
1984 }
1985}
1986EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1987
1988int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1989{
1990 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1991 size_t size;
1992 int i;
1993
1994 if (!fwspec)
1995 return -EINVAL;
1996
1997 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1998 if (size > sizeof(*fwspec)) {
1999 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
2000 if (!fwspec)
2001 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08002002
2003 dev->iommu_fwspec = fwspec;
Robin Murphy57f98d22016-09-13 10:54:14 +01002004 }
2005
2006 for (i = 0; i < num_ids; i++)
2007 fwspec->ids[fwspec->num_ids + i] = ids[i];
2008
2009 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01002010 return 0;
2011}
2012EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);