blob: 9170fd498f460fb52f147b3c1076e1721eb026b5 [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);
Alex Williamsond72e31c2012-05-30 14:18:53 -060039
Thierry Redingb22f6432014-06-27 09:03:12 +020040struct iommu_callback_data {
41 const struct iommu_ops *ops;
42};
43
Alex Williamsond72e31c2012-05-30 14:18:53 -060044struct iommu_group {
45 struct kobject kobj;
46 struct kobject *devices_kobj;
47 struct list_head devices;
48 struct mutex mutex;
49 struct blocking_notifier_head notifier;
50 void *iommu_data;
51 void (*iommu_data_release)(void *iommu_data);
52 char *name;
53 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020054 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020055 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060056};
57
Joerg Roedelc09e22d2017-02-01 12:19:46 +010058struct group_device {
Alex Williamsond72e31c2012-05-30 14:18:53 -060059 struct list_head list;
60 struct device *dev;
61 char *name;
62};
63
64struct iommu_group_attribute {
65 struct attribute attr;
66 ssize_t (*show)(struct iommu_group *group, char *buf);
67 ssize_t (*store)(struct iommu_group *group,
68 const char *buf, size_t count);
69};
70
Eric Augerbc7d12b92017-01-19 20:57:52 +000071static const char * const iommu_group_resv_type_string[] = {
72 [IOMMU_RESV_DIRECT] = "direct",
73 [IOMMU_RESV_RESERVED] = "reserved",
74 [IOMMU_RESV_MSI] = "msi",
75};
76
Alex Williamsond72e31c2012-05-30 14:18:53 -060077#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
78struct iommu_group_attribute iommu_group_attr_##_name = \
79 __ATTR(_name, _mode, _show, _store)
80
81#define to_iommu_group_attr(_attr) \
82 container_of(_attr, struct iommu_group_attribute, attr)
83#define to_iommu_group(_kobj) \
84 container_of(_kobj, struct iommu_group, kobj)
85
Joerg Roedelb0119e82017-02-01 13:23:08 +010086static LIST_HEAD(iommu_device_list);
87static DEFINE_SPINLOCK(iommu_device_lock);
88
89int iommu_device_register(struct iommu_device *iommu)
90{
91 spin_lock(&iommu_device_lock);
92 list_add_tail(&iommu->list, &iommu_device_list);
93 spin_unlock(&iommu_device_lock);
94
95 return 0;
96}
97
98void iommu_device_unregister(struct iommu_device *iommu)
99{
100 spin_lock(&iommu_device_lock);
101 list_del(&iommu->list);
102 spin_unlock(&iommu_device_lock);
103}
104
Joerg Roedel53723dc2015-05-28 18:41:29 +0200105static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
106 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200107static int __iommu_attach_device(struct iommu_domain *domain,
108 struct device *dev);
109static int __iommu_attach_group(struct iommu_domain *domain,
110 struct iommu_group *group);
111static void __iommu_detach_group(struct iommu_domain *domain,
112 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +0200113
Alex Williamsond72e31c2012-05-30 14:18:53 -0600114static ssize_t iommu_group_attr_show(struct kobject *kobj,
115 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400116{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600117 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
118 struct iommu_group *group = to_iommu_group(kobj);
119 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400120
Alex Williamsond72e31c2012-05-30 14:18:53 -0600121 if (attr->show)
122 ret = attr->show(group, buf);
123 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400124}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600125
126static ssize_t iommu_group_attr_store(struct kobject *kobj,
127 struct attribute *__attr,
128 const char *buf, size_t count)
129{
130 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
131 struct iommu_group *group = to_iommu_group(kobj);
132 ssize_t ret = -EIO;
133
134 if (attr->store)
135 ret = attr->store(group, buf, count);
136 return ret;
137}
138
139static const struct sysfs_ops iommu_group_sysfs_ops = {
140 .show = iommu_group_attr_show,
141 .store = iommu_group_attr_store,
142};
143
144static int iommu_group_create_file(struct iommu_group *group,
145 struct iommu_group_attribute *attr)
146{
147 return sysfs_create_file(&group->kobj, &attr->attr);
148}
149
150static void iommu_group_remove_file(struct iommu_group *group,
151 struct iommu_group_attribute *attr)
152{
153 sysfs_remove_file(&group->kobj, &attr->attr);
154}
155
156static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
157{
158 return sprintf(buf, "%s\n", group->name);
159}
160
Eric Auger6c65fb32017-01-19 20:57:51 +0000161/**
162 * iommu_insert_resv_region - Insert a new region in the
163 * list of reserved regions.
164 * @new: new region to insert
165 * @regions: list of regions
166 *
167 * The new element is sorted by address with respect to the other
168 * regions of the same type. In case it overlaps with another
169 * region of the same type, regions are merged. In case it
170 * overlaps with another region of different type, regions are
171 * not merged.
172 */
173static int iommu_insert_resv_region(struct iommu_resv_region *new,
174 struct list_head *regions)
175{
176 struct iommu_resv_region *region;
177 phys_addr_t start = new->start;
178 phys_addr_t end = new->start + new->length - 1;
179 struct list_head *pos = regions->next;
180
181 while (pos != regions) {
182 struct iommu_resv_region *entry =
183 list_entry(pos, struct iommu_resv_region, list);
184 phys_addr_t a = entry->start;
185 phys_addr_t b = entry->start + entry->length - 1;
186 int type = entry->type;
187
188 if (end < a) {
189 goto insert;
190 } else if (start > b) {
191 pos = pos->next;
192 } else if ((start >= a) && (end <= b)) {
193 if (new->type == type)
194 goto done;
195 else
196 pos = pos->next;
197 } else {
198 if (new->type == type) {
199 phys_addr_t new_start = min(a, start);
200 phys_addr_t new_end = max(b, end);
201
202 list_del(&entry->list);
203 entry->start = new_start;
204 entry->length = new_end - new_start + 1;
205 iommu_insert_resv_region(entry, regions);
206 } else {
207 pos = pos->next;
208 }
209 }
210 }
211insert:
212 region = iommu_alloc_resv_region(new->start, new->length,
213 new->prot, new->type);
214 if (!region)
215 return -ENOMEM;
216
217 list_add_tail(&region->list, pos);
218done:
219 return 0;
220}
221
222static int
223iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
224 struct list_head *group_resv_regions)
225{
226 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100227 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000228
229 list_for_each_entry(entry, dev_resv_regions, list) {
230 ret = iommu_insert_resv_region(entry, group_resv_regions);
231 if (ret)
232 break;
233 }
234 return ret;
235}
236
237int iommu_get_group_resv_regions(struct iommu_group *group,
238 struct list_head *head)
239{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100240 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000241 int ret = 0;
242
243 mutex_lock(&group->mutex);
244 list_for_each_entry(device, &group->devices, list) {
245 struct list_head dev_resv_regions;
246
247 INIT_LIST_HEAD(&dev_resv_regions);
248 iommu_get_resv_regions(device->dev, &dev_resv_regions);
249 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
250 iommu_put_resv_regions(device->dev, &dev_resv_regions);
251 if (ret)
252 break;
253 }
254 mutex_unlock(&group->mutex);
255 return ret;
256}
257EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
258
Eric Augerbc7d12b92017-01-19 20:57:52 +0000259static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
260 char *buf)
261{
262 struct iommu_resv_region *region, *next;
263 struct list_head group_resv_regions;
264 char *str = buf;
265
266 INIT_LIST_HEAD(&group_resv_regions);
267 iommu_get_group_resv_regions(group, &group_resv_regions);
268
269 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
270 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
271 (long long int)region->start,
272 (long long int)(region->start +
273 region->length - 1),
274 iommu_group_resv_type_string[region->type]);
275 kfree(region);
276 }
277
278 return (str - buf);
279}
280
Alex Williamsond72e31c2012-05-30 14:18:53 -0600281static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
282
Eric Augerbc7d12b92017-01-19 20:57:52 +0000283static IOMMU_GROUP_ATTR(reserved_regions, 0444,
284 iommu_group_show_resv_regions, NULL);
285
Alex Williamsond72e31c2012-05-30 14:18:53 -0600286static void iommu_group_release(struct kobject *kobj)
287{
288 struct iommu_group *group = to_iommu_group(kobj);
289
Joerg Roedel269aa802015-05-28 18:41:25 +0200290 pr_debug("Releasing group %d\n", group->id);
291
Alex Williamsond72e31c2012-05-30 14:18:53 -0600292 if (group->iommu_data_release)
293 group->iommu_data_release(group->iommu_data);
294
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200295 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600296
Joerg Roedel53723dc2015-05-28 18:41:29 +0200297 if (group->default_domain)
298 iommu_domain_free(group->default_domain);
299
Alex Williamsond72e31c2012-05-30 14:18:53 -0600300 kfree(group->name);
301 kfree(group);
302}
303
304static struct kobj_type iommu_group_ktype = {
305 .sysfs_ops = &iommu_group_sysfs_ops,
306 .release = iommu_group_release,
307};
308
309/**
310 * iommu_group_alloc - Allocate a new group
311 * @name: Optional name to associate with group, visible in sysfs
312 *
313 * This function is called by an iommu driver to allocate a new iommu
314 * group. The iommu group represents the minimum granularity of the iommu.
315 * Upon successful return, the caller holds a reference to the supplied
316 * group in order to hold the group until devices are added. Use
317 * iommu_group_put() to release this extra reference count, allowing the
318 * group to be automatically reclaimed once it has no devices or external
319 * references.
320 */
321struct iommu_group *iommu_group_alloc(void)
322{
323 struct iommu_group *group;
324 int ret;
325
326 group = kzalloc(sizeof(*group), GFP_KERNEL);
327 if (!group)
328 return ERR_PTR(-ENOMEM);
329
330 group->kobj.kset = iommu_group_kset;
331 mutex_init(&group->mutex);
332 INIT_LIST_HEAD(&group->devices);
333 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
334
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200335 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
336 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600337 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200338 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600339 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200340 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600341
342 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
343 NULL, "%d", group->id);
344 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200345 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600346 kfree(group);
347 return ERR_PTR(ret);
348 }
349
350 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
351 if (!group->devices_kobj) {
352 kobject_put(&group->kobj); /* triggers .release & free */
353 return ERR_PTR(-ENOMEM);
354 }
355
356 /*
357 * The devices_kobj holds a reference on the group kobject, so
358 * as long as that exists so will the group. We can therefore
359 * use the devices_kobj for reference counting.
360 */
361 kobject_put(&group->kobj);
362
Eric Augerbc7d12b92017-01-19 20:57:52 +0000363 ret = iommu_group_create_file(group,
364 &iommu_group_attr_reserved_regions);
365 if (ret)
366 return ERR_PTR(ret);
367
Joerg Roedel269aa802015-05-28 18:41:25 +0200368 pr_debug("Allocated group %d\n", group->id);
369
Alex Williamsond72e31c2012-05-30 14:18:53 -0600370 return group;
371}
372EXPORT_SYMBOL_GPL(iommu_group_alloc);
373
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100374struct iommu_group *iommu_group_get_by_id(int id)
375{
376 struct kobject *group_kobj;
377 struct iommu_group *group;
378 const char *name;
379
380 if (!iommu_group_kset)
381 return NULL;
382
383 name = kasprintf(GFP_KERNEL, "%d", id);
384 if (!name)
385 return NULL;
386
387 group_kobj = kset_find_obj(iommu_group_kset, name);
388 kfree(name);
389
390 if (!group_kobj)
391 return NULL;
392
393 group = container_of(group_kobj, struct iommu_group, kobj);
394 BUG_ON(group->id != id);
395
396 kobject_get(group->devices_kobj);
397 kobject_put(&group->kobj);
398
399 return group;
400}
401EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
402
Alex Williamsond72e31c2012-05-30 14:18:53 -0600403/**
404 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
405 * @group: the group
406 *
407 * iommu drivers can store data in the group for use when doing iommu
408 * operations. This function provides a way to retrieve it. Caller
409 * should hold a group reference.
410 */
411void *iommu_group_get_iommudata(struct iommu_group *group)
412{
413 return group->iommu_data;
414}
415EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
416
417/**
418 * iommu_group_set_iommudata - set iommu_data for a group
419 * @group: the group
420 * @iommu_data: new data
421 * @release: release function for iommu_data
422 *
423 * iommu drivers can store data in the group for use when doing iommu
424 * operations. This function provides a way to set the data after
425 * the group has been allocated. Caller should hold a group reference.
426 */
427void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
428 void (*release)(void *iommu_data))
429{
430 group->iommu_data = iommu_data;
431 group->iommu_data_release = release;
432}
433EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
434
435/**
436 * iommu_group_set_name - set name for a group
437 * @group: the group
438 * @name: name
439 *
440 * Allow iommu driver to set a name for a group. When set it will
441 * appear in a name attribute file under the group in sysfs.
442 */
443int iommu_group_set_name(struct iommu_group *group, const char *name)
444{
445 int ret;
446
447 if (group->name) {
448 iommu_group_remove_file(group, &iommu_group_attr_name);
449 kfree(group->name);
450 group->name = NULL;
451 if (!name)
452 return 0;
453 }
454
455 group->name = kstrdup(name, GFP_KERNEL);
456 if (!group->name)
457 return -ENOMEM;
458
459 ret = iommu_group_create_file(group, &iommu_group_attr_name);
460 if (ret) {
461 kfree(group->name);
462 group->name = NULL;
463 return ret;
464 }
465
466 return 0;
467}
468EXPORT_SYMBOL_GPL(iommu_group_set_name);
469
Joerg Roedelbeed2822015-05-28 18:41:34 +0200470static int iommu_group_create_direct_mappings(struct iommu_group *group,
471 struct device *dev)
472{
473 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000474 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200475 struct list_head mappings;
476 unsigned long pg_size;
477 int ret = 0;
478
479 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
480 return 0;
481
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100482 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200483
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100484 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200485 INIT_LIST_HEAD(&mappings);
486
Eric Augere5b52342017-01-19 20:57:47 +0000487 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200488
489 /* We need to consider overlapping regions for different devices */
490 list_for_each_entry(entry, &mappings, list) {
491 dma_addr_t start, end, addr;
492
Eric Augere5b52342017-01-19 20:57:47 +0000493 if (domain->ops->apply_resv_region)
494 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200495
Joerg Roedelbeed2822015-05-28 18:41:34 +0200496 start = ALIGN(entry->start, pg_size);
497 end = ALIGN(entry->start + entry->length, pg_size);
498
Eric Auger544a25d2017-01-19 20:57:50 +0000499 if (entry->type != IOMMU_RESV_DIRECT)
500 continue;
501
Joerg Roedelbeed2822015-05-28 18:41:34 +0200502 for (addr = start; addr < end; addr += pg_size) {
503 phys_addr_t phys_addr;
504
505 phys_addr = iommu_iova_to_phys(domain, addr);
506 if (phys_addr)
507 continue;
508
509 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
510 if (ret)
511 goto out;
512 }
513
514 }
515
516out:
Eric Augere5b52342017-01-19 20:57:47 +0000517 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200518
519 return ret;
520}
521
Alex Williamsond72e31c2012-05-30 14:18:53 -0600522/**
523 * iommu_group_add_device - add a device to an iommu group
524 * @group: the group into which to add the device (reference should be held)
525 * @dev: the device
526 *
527 * This function is called by an iommu driver to add a device into a
528 * group. Adding a device increments the group reference count.
529 */
530int iommu_group_add_device(struct iommu_group *group, struct device *dev)
531{
532 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100533 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600534
535 device = kzalloc(sizeof(*device), GFP_KERNEL);
536 if (!device)
537 return -ENOMEM;
538
539 device->dev = dev;
540
541 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000542 if (ret)
543 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600544
545 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
546rename:
547 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000548 ret = -ENOMEM;
549 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600550 }
551
552 ret = sysfs_create_link_nowarn(group->devices_kobj,
553 &dev->kobj, device->name);
554 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600555 if (ret == -EEXIST && i >= 0) {
556 /*
557 * Account for the slim chance of collision
558 * and append an instance to the name.
559 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000560 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600561 device->name = kasprintf(GFP_KERNEL, "%s.%d",
562 kobject_name(&dev->kobj), i++);
563 goto rename;
564 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000565 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600566 }
567
568 kobject_get(group->devices_kobj);
569
570 dev->iommu_group = group;
571
Joerg Roedelbeed2822015-05-28 18:41:34 +0200572 iommu_group_create_direct_mappings(group, dev);
573
Alex Williamsond72e31c2012-05-30 14:18:53 -0600574 mutex_lock(&group->mutex);
575 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200576 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000577 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600578 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000579 if (ret)
580 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600581
582 /* Notify any listeners about change to group. */
583 blocking_notifier_call_chain(&group->notifier,
584 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600585
586 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200587
588 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
589
Alex Williamsond72e31c2012-05-30 14:18:53 -0600590 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000591
592err_put_group:
593 mutex_lock(&group->mutex);
594 list_del(&device->list);
595 mutex_unlock(&group->mutex);
596 dev->iommu_group = NULL;
597 kobject_put(group->devices_kobj);
598err_free_name:
599 kfree(device->name);
600err_remove_link:
601 sysfs_remove_link(&dev->kobj, "iommu_group");
602err_free_device:
603 kfree(device);
604 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
605 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600606}
607EXPORT_SYMBOL_GPL(iommu_group_add_device);
608
609/**
610 * iommu_group_remove_device - remove a device from it's current group
611 * @dev: device to be removed
612 *
613 * This function is called by an iommu driver to remove the device from
614 * it's current group. This decrements the iommu group reference count.
615 */
616void iommu_group_remove_device(struct device *dev)
617{
618 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100619 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600620
Joerg Roedel269aa802015-05-28 18:41:25 +0200621 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
622
Alex Williamsond72e31c2012-05-30 14:18:53 -0600623 /* Pre-notify listeners that a device is being removed. */
624 blocking_notifier_call_chain(&group->notifier,
625 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
626
627 mutex_lock(&group->mutex);
628 list_for_each_entry(tmp_device, &group->devices, list) {
629 if (tmp_device->dev == dev) {
630 device = tmp_device;
631 list_del(&device->list);
632 break;
633 }
634 }
635 mutex_unlock(&group->mutex);
636
637 if (!device)
638 return;
639
640 sysfs_remove_link(group->devices_kobj, device->name);
641 sysfs_remove_link(&dev->kobj, "iommu_group");
642
Shuah Khan2e757082013-08-15 11:59:25 -0600643 trace_remove_device_from_group(group->id, dev);
644
Alex Williamsond72e31c2012-05-30 14:18:53 -0600645 kfree(device->name);
646 kfree(device);
647 dev->iommu_group = NULL;
648 kobject_put(group->devices_kobj);
649}
650EXPORT_SYMBOL_GPL(iommu_group_remove_device);
651
Joerg Roedel426a2732015-05-28 18:41:30 +0200652static int iommu_group_device_count(struct iommu_group *group)
653{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100654 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200655 int ret = 0;
656
657 list_for_each_entry(entry, &group->devices, list)
658 ret++;
659
660 return ret;
661}
662
Alex Williamsond72e31c2012-05-30 14:18:53 -0600663/**
664 * iommu_group_for_each_dev - iterate over each device in the group
665 * @group: the group
666 * @data: caller opaque data to be passed to callback function
667 * @fn: caller supplied callback function
668 *
669 * This function is called by group users to iterate over group devices.
670 * Callers should hold a reference count to the group during callback.
671 * The group->mutex is held across callbacks, which will block calls to
672 * iommu_group_add/remove_device.
673 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200674static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
675 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600676{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100677 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600678 int ret = 0;
679
Alex Williamsond72e31c2012-05-30 14:18:53 -0600680 list_for_each_entry(device, &group->devices, list) {
681 ret = fn(device->dev, data);
682 if (ret)
683 break;
684 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200685 return ret;
686}
687
688
689int iommu_group_for_each_dev(struct iommu_group *group, void *data,
690 int (*fn)(struct device *, void *))
691{
692 int ret;
693
694 mutex_lock(&group->mutex);
695 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600696 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200697
Alex Williamsond72e31c2012-05-30 14:18:53 -0600698 return ret;
699}
700EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
701
702/**
703 * iommu_group_get - Return the group for a device and increment reference
704 * @dev: get the group that this device belongs to
705 *
706 * This function is called by iommu drivers and users to get the group
707 * for the specified device. If found, the group is returned and the group
708 * reference in incremented, else NULL.
709 */
710struct iommu_group *iommu_group_get(struct device *dev)
711{
712 struct iommu_group *group = dev->iommu_group;
713
714 if (group)
715 kobject_get(group->devices_kobj);
716
717 return group;
718}
719EXPORT_SYMBOL_GPL(iommu_group_get);
720
721/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000722 * iommu_group_ref_get - Increment reference on a group
723 * @group: the group to use, must not be NULL
724 *
725 * This function is called by iommu drivers to take additional references on an
726 * existing group. Returns the given group for convenience.
727 */
728struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
729{
730 kobject_get(group->devices_kobj);
731 return group;
732}
733
734/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600735 * iommu_group_put - Decrement group reference
736 * @group: the group to use
737 *
738 * This function is called by iommu drivers and users to release the
739 * iommu group. Once the reference count is zero, the group is released.
740 */
741void iommu_group_put(struct iommu_group *group)
742{
743 if (group)
744 kobject_put(group->devices_kobj);
745}
746EXPORT_SYMBOL_GPL(iommu_group_put);
747
748/**
749 * iommu_group_register_notifier - Register a notifier for group changes
750 * @group: the group to watch
751 * @nb: notifier block to signal
752 *
753 * This function allows iommu group users to track changes in a group.
754 * See include/linux/iommu.h for actions sent via this notifier. Caller
755 * should hold a reference to the group throughout notifier registration.
756 */
757int iommu_group_register_notifier(struct iommu_group *group,
758 struct notifier_block *nb)
759{
760 return blocking_notifier_chain_register(&group->notifier, nb);
761}
762EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
763
764/**
765 * iommu_group_unregister_notifier - Unregister a notifier
766 * @group: the group to watch
767 * @nb: notifier block to signal
768 *
769 * Unregister a previously registered group notifier block.
770 */
771int iommu_group_unregister_notifier(struct iommu_group *group,
772 struct notifier_block *nb)
773{
774 return blocking_notifier_chain_unregister(&group->notifier, nb);
775}
776EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
777
778/**
779 * iommu_group_id - Return ID for a group
780 * @group: the group to ID
781 *
782 * Return the unique ID for the group matching the sysfs group number.
783 */
784int iommu_group_id(struct iommu_group *group)
785{
786 return group->id;
787}
788EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400789
Alex Williamsonf096c062014-09-19 10:03:06 -0600790static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
791 unsigned long *devfns);
792
Alex Williamson104a1c12014-07-03 09:51:18 -0600793/*
794 * To consider a PCI device isolated, we require ACS to support Source
795 * Validation, Request Redirection, Completer Redirection, and Upstream
796 * Forwarding. This effectively means that devices cannot spoof their
797 * requester ID, requests and completions cannot be redirected, and all
798 * transactions are forwarded upstream, even as it passes through a
799 * bridge where the target device is downstream.
800 */
801#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
802
Alex Williamsonf096c062014-09-19 10:03:06 -0600803/*
804 * For multifunction devices which are not isolated from each other, find
805 * all the other non-isolated functions and look for existing groups. For
806 * each function, we also need to look for aliases to or from other devices
807 * that may already have a group.
808 */
809static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
810 unsigned long *devfns)
811{
812 struct pci_dev *tmp = NULL;
813 struct iommu_group *group;
814
815 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
816 return NULL;
817
818 for_each_pci_dev(tmp) {
819 if (tmp == pdev || tmp->bus != pdev->bus ||
820 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
821 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
822 continue;
823
824 group = get_pci_alias_group(tmp, devfns);
825 if (group) {
826 pci_dev_put(tmp);
827 return group;
828 }
829 }
830
831 return NULL;
832}
833
834/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100835 * Look for aliases to or from the given device for existing groups. DMA
836 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600837 * space is quite small (especially since we're really only looking at pcie
838 * device, and therefore only expect multiple slots on the root complex or
839 * downstream switch ports). It's conceivable though that a pair of
840 * multifunction devices could have aliases between them that would cause a
841 * loop. To prevent this, we use a bitmap to track where we've been.
842 */
843static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
844 unsigned long *devfns)
845{
846 struct pci_dev *tmp = NULL;
847 struct iommu_group *group;
848
849 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
850 return NULL;
851
852 group = iommu_group_get(&pdev->dev);
853 if (group)
854 return group;
855
856 for_each_pci_dev(tmp) {
857 if (tmp == pdev || tmp->bus != pdev->bus)
858 continue;
859
860 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100861 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600862 group = get_pci_alias_group(tmp, devfns);
863 if (group) {
864 pci_dev_put(tmp);
865 return group;
866 }
867
868 group = get_pci_function_alias_group(tmp, devfns);
869 if (group) {
870 pci_dev_put(tmp);
871 return group;
872 }
873 }
874 }
875
876 return NULL;
877}
878
Alex Williamson104a1c12014-07-03 09:51:18 -0600879struct group_for_pci_data {
880 struct pci_dev *pdev;
881 struct iommu_group *group;
882};
883
884/*
885 * DMA alias iterator callback, return the last seen device. Stop and return
886 * the IOMMU group if we find one along the way.
887 */
888static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
889{
890 struct group_for_pci_data *data = opaque;
891
892 data->pdev = pdev;
893 data->group = iommu_group_get(&pdev->dev);
894
895 return data->group != NULL;
896}
897
898/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200899 * Generic device_group call-back function. It just allocates one
900 * iommu-group per device.
901 */
902struct iommu_group *generic_device_group(struct device *dev)
903{
904 struct iommu_group *group;
905
906 group = iommu_group_alloc();
907 if (IS_ERR(group))
908 return NULL;
909
910 return group;
911}
912
913/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600914 * Use standard PCI bus topology, isolation features, and DMA alias quirks
915 * to find or create an IOMMU group for a device.
916 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200917struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600918{
Joerg Roedel5e622922015-10-21 23:51:37 +0200919 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600920 struct group_for_pci_data data;
921 struct pci_bus *bus;
922 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600923 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600924
Joerg Roedel5e622922015-10-21 23:51:37 +0200925 if (WARN_ON(!dev_is_pci(dev)))
926 return ERR_PTR(-EINVAL);
927
Alex Williamson104a1c12014-07-03 09:51:18 -0600928 /*
929 * Find the upstream DMA alias for the device. A device must not
930 * be aliased due to topology in order to have its own IOMMU group.
931 * If we find an alias along the way that already belongs to a
932 * group, use it.
933 */
934 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
935 return data.group;
936
937 pdev = data.pdev;
938
939 /*
940 * Continue upstream from the point of minimum IOMMU granularity
941 * due to aliases to the point where devices are protected from
942 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
943 * group, use it.
944 */
945 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
946 if (!bus->self)
947 continue;
948
949 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
950 break;
951
952 pdev = bus->self;
953
954 group = iommu_group_get(&pdev->dev);
955 if (group)
956 return group;
957 }
958
959 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600960 * Look for existing groups on device aliases. If we alias another
961 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600962 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600963 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
964 if (group)
965 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600966
967 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600968 * Look for existing groups on non-isolated functions on the same
969 * slot and aliases of those funcions, if any. No need to clear
970 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600971 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600972 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
973 if (group)
974 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600975
976 /* No shared group found, allocate new */
Joerg Roedel53723dc2015-05-28 18:41:29 +0200977 group = iommu_group_alloc();
Dan Carpenter409e5532015-06-10 13:59:27 +0300978 if (IS_ERR(group))
979 return NULL;
980
Joerg Roedel53723dc2015-05-28 18:41:29 +0200981 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600982}
983
984/**
985 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
986 * @dev: target device
987 *
988 * This function is intended to be called by IOMMU drivers and extended to
989 * support common, bus-defined algorithms when determining or creating the
990 * IOMMU group for a device. On success, the caller will hold a reference
991 * to the returned IOMMU group, which will already include the provided
992 * device. The reference should be released with iommu_group_put().
993 */
994struct iommu_group *iommu_group_get_for_dev(struct device *dev)
995{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200996 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200997 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600998 int ret;
999
1000 group = iommu_group_get(dev);
1001 if (group)
1002 return group;
1003
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001004 group = ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001005
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001006 if (ops && ops->device_group)
1007 group = ops->device_group(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001008
1009 if (IS_ERR(group))
1010 return group;
1011
Joerg Roedel12282362015-10-21 23:51:43 +02001012 /*
1013 * Try to allocate a default domain - needs support from the
1014 * IOMMU driver.
1015 */
1016 if (!group->default_domain) {
1017 group->default_domain = __iommu_domain_alloc(dev->bus,
1018 IOMMU_DOMAIN_DMA);
Joerg Roedeleebb8032016-04-04 15:47:48 +02001019 if (!group->domain)
1020 group->domain = group->default_domain;
Joerg Roedel12282362015-10-21 23:51:43 +02001021 }
1022
Alex Williamson104a1c12014-07-03 09:51:18 -06001023 ret = iommu_group_add_device(group, dev);
1024 if (ret) {
1025 iommu_group_put(group);
1026 return ERR_PTR(ret);
1027 }
1028
1029 return group;
1030}
1031
Joerg Roedel6827ca82015-05-28 18:41:35 +02001032struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1033{
1034 return group->default_domain;
1035}
1036
Alex Williamson14604322011-10-21 15:56:05 -04001037static int add_iommu_group(struct device *dev, void *data)
1038{
Thierry Redingb22f6432014-06-27 09:03:12 +02001039 struct iommu_callback_data *cb = data;
1040 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +02001041 int ret;
Alex Williamson14604322011-10-21 15:56:05 -04001042
Alex Williamsond72e31c2012-05-30 14:18:53 -06001043 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +00001044 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001045
1046 WARN_ON(dev->iommu_group);
1047
Joerg Roedel38667f12015-06-29 10:16:08 +02001048 ret = ops->add_device(dev);
1049
1050 /*
1051 * We ignore -ENODEV errors for now, as they just mean that the
1052 * device is not translated by an IOMMU. We still care about
1053 * other errors and fail to initialize when they happen.
1054 */
1055 if (ret == -ENODEV)
1056 ret = 0;
1057
1058 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001059}
1060
Joerg Roedel8da30142015-05-28 18:41:27 +02001061static int remove_iommu_group(struct device *dev, void *data)
1062{
1063 struct iommu_callback_data *cb = data;
1064 const struct iommu_ops *ops = cb->ops;
1065
1066 if (ops->remove_device && dev->iommu_group)
1067 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001068
1069 return 0;
1070}
1071
Alex Williamsond72e31c2012-05-30 14:18:53 -06001072static int iommu_bus_notifier(struct notifier_block *nb,
1073 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001074{
1075 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +02001076 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001077 struct iommu_group *group;
1078 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -04001079
Alex Williamsond72e31c2012-05-30 14:18:53 -06001080 /*
1081 * ADD/DEL call into iommu driver ops if provided, which may
1082 * result in ADD/DEL notifiers to group->notifier
1083 */
1084 if (action == BUS_NOTIFY_ADD_DEVICE) {
zhichang.yuan3ba87752017-04-18 20:51:48 +08001085 if (ops->add_device) {
1086 int ret;
1087
1088 ret = ops->add_device(dev);
1089 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1090 }
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001091 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -06001092 if (ops->remove_device && dev->iommu_group) {
1093 ops->remove_device(dev);
1094 return 0;
1095 }
1096 }
Alex Williamson14604322011-10-21 15:56:05 -04001097
Alex Williamsond72e31c2012-05-30 14:18:53 -06001098 /*
1099 * Remaining BUS_NOTIFYs get filtered and republished to the
1100 * group, if anyone is listening
1101 */
1102 group = iommu_group_get(dev);
1103 if (!group)
1104 return 0;
1105
1106 switch (action) {
1107 case BUS_NOTIFY_BIND_DRIVER:
1108 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1109 break;
1110 case BUS_NOTIFY_BOUND_DRIVER:
1111 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1112 break;
1113 case BUS_NOTIFY_UNBIND_DRIVER:
1114 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1115 break;
1116 case BUS_NOTIFY_UNBOUND_DRIVER:
1117 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1118 break;
1119 }
1120
1121 if (group_action)
1122 blocking_notifier_call_chain(&group->notifier,
1123 group_action, dev);
1124
1125 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001126 return 0;
1127}
1128
Mark Salterfb3e3062014-09-21 13:58:24 -04001129static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001130{
Mark Salterfb3e3062014-09-21 13:58:24 -04001131 int err;
1132 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001133 struct iommu_callback_data cb = {
1134 .ops = ops,
1135 };
1136
Mark Salterfb3e3062014-09-21 13:58:24 -04001137 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1138 if (!nb)
1139 return -ENOMEM;
1140
1141 nb->notifier_call = iommu_bus_notifier;
1142
1143 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001144 if (err)
1145 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001146
1147 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001148 if (err)
1149 goto out_err;
1150
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001151
1152 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001153
1154out_err:
1155 /* Clean up */
1156 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1157 bus_unregister_notifier(bus, nb);
1158
1159out_free:
1160 kfree(nb);
1161
1162 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001163}
1164
Joerg Roedelff217762011-08-26 16:48:26 +02001165/**
1166 * bus_set_iommu - set iommu-callbacks for the bus
1167 * @bus: bus.
1168 * @ops: the callbacks provided by the iommu-driver
1169 *
1170 * This function is called by an iommu driver to set the iommu methods
1171 * used for a particular bus. Drivers for devices on that bus can use
1172 * the iommu-api after these ops are registered.
1173 * This special function is needed because IOMMUs are usually devices on
1174 * the bus itself, so the iommu drivers are not initialized when the bus
1175 * is set up. With this function the iommu-driver can set the iommu-ops
1176 * afterwards.
1177 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001178int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001179{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001180 int err;
1181
Joerg Roedelff217762011-08-26 16:48:26 +02001182 if (bus->iommu_ops != NULL)
1183 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001184
Joerg Roedelff217762011-08-26 16:48:26 +02001185 bus->iommu_ops = ops;
1186
1187 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001188 err = iommu_bus_init(bus, ops);
1189 if (err)
1190 bus->iommu_ops = NULL;
1191
1192 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001193}
Joerg Roedelff217762011-08-26 16:48:26 +02001194EXPORT_SYMBOL_GPL(bus_set_iommu);
1195
Joerg Roedela1b60c12011-09-06 18:46:34 +02001196bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001197{
Joerg Roedel94441c32011-09-06 18:58:54 +02001198 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001199}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001200EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001201
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001202bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1203{
1204 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1205 return false;
1206
1207 return bus->iommu_ops->capable(cap);
1208}
1209EXPORT_SYMBOL_GPL(iommu_capable);
1210
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001211/**
1212 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1213 * @domain: iommu domain
1214 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001215 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001216 *
1217 * This function should be used by IOMMU users which want to be notified
1218 * whenever an IOMMU fault happens.
1219 *
1220 * The fault handler itself should return 0 on success, and an appropriate
1221 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001222 */
1223void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001224 iommu_fault_handler_t handler,
1225 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001226{
1227 BUG_ON(!domain);
1228
1229 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001230 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001231}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001232EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001233
Joerg Roedel53723dc2015-05-28 18:41:29 +02001234static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1235 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001236{
1237 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001238
Joerg Roedel94441c32011-09-06 18:58:54 +02001239 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001240 return NULL;
1241
Joerg Roedel53723dc2015-05-28 18:41:29 +02001242 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001243 if (!domain)
1244 return NULL;
1245
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001246 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001247 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001248 /* Assume all sizes by default; the driver may override this later */
1249 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001250
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001251 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001252}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001253
Joerg Roedel53723dc2015-05-28 18:41:29 +02001254struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1255{
1256 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001257}
1258EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1259
1260void iommu_domain_free(struct iommu_domain *domain)
1261{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001262 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001263}
1264EXPORT_SYMBOL_GPL(iommu_domain_free);
1265
Joerg Roedel426a2732015-05-28 18:41:30 +02001266static int __iommu_attach_device(struct iommu_domain *domain,
1267 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001268{
Shuah Khanb54db772013-08-15 11:59:26 -06001269 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001270 if (unlikely(domain->ops->attach_dev == NULL))
1271 return -ENODEV;
1272
Shuah Khanb54db772013-08-15 11:59:26 -06001273 ret = domain->ops->attach_dev(domain, dev);
1274 if (!ret)
1275 trace_attach_device_to_domain(dev);
1276 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001277}
Joerg Roedel426a2732015-05-28 18:41:30 +02001278
1279int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1280{
1281 struct iommu_group *group;
1282 int ret;
1283
1284 group = iommu_group_get(dev);
1285 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1286 if (group == NULL)
1287 return __iommu_attach_device(domain, dev);
1288
1289 /*
1290 * We have a group - lock it to make sure the device-count doesn't
1291 * change while we are attaching
1292 */
1293 mutex_lock(&group->mutex);
1294 ret = -EINVAL;
1295 if (iommu_group_device_count(group) != 1)
1296 goto out_unlock;
1297
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001298 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001299
1300out_unlock:
1301 mutex_unlock(&group->mutex);
1302 iommu_group_put(group);
1303
1304 return ret;
1305}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001306EXPORT_SYMBOL_GPL(iommu_attach_device);
1307
Joerg Roedel426a2732015-05-28 18:41:30 +02001308static void __iommu_detach_device(struct iommu_domain *domain,
1309 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001310{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001311 if (unlikely(domain->ops->detach_dev == NULL))
1312 return;
1313
1314 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001315 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001316}
Joerg Roedel426a2732015-05-28 18:41:30 +02001317
1318void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1319{
1320 struct iommu_group *group;
1321
1322 group = iommu_group_get(dev);
1323 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1324 if (group == NULL)
1325 return __iommu_detach_device(domain, dev);
1326
1327 mutex_lock(&group->mutex);
1328 if (iommu_group_device_count(group) != 1) {
1329 WARN_ON(1);
1330 goto out_unlock;
1331 }
1332
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001333 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001334
1335out_unlock:
1336 mutex_unlock(&group->mutex);
1337 iommu_group_put(group);
1338}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001339EXPORT_SYMBOL_GPL(iommu_detach_device);
1340
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001341struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1342{
1343 struct iommu_domain *domain;
1344 struct iommu_group *group;
1345
1346 group = iommu_group_get(dev);
1347 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1348 if (group == NULL)
1349 return NULL;
1350
1351 domain = group->domain;
1352
1353 iommu_group_put(group);
1354
1355 return domain;
1356}
1357EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1358
Alex Williamsond72e31c2012-05-30 14:18:53 -06001359/*
1360 * IOMMU groups are really the natrual working unit of the IOMMU, but
1361 * the IOMMU API works on domains and devices. Bridge that gap by
1362 * iterating over the devices in a group. Ideally we'd have a single
1363 * device which represents the requestor ID of the group, but we also
1364 * allow IOMMU drivers to create policy defined minimum sets, where
1365 * the physical hardware may be able to distiguish members, but we
1366 * wish to group them at a higher level (ex. untrusted multi-function
1367 * PCI devices). Thus we attach each device.
1368 */
1369static int iommu_group_do_attach_device(struct device *dev, void *data)
1370{
1371 struct iommu_domain *domain = data;
1372
Joerg Roedel426a2732015-05-28 18:41:30 +02001373 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001374}
1375
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001376static int __iommu_attach_group(struct iommu_domain *domain,
1377 struct iommu_group *group)
1378{
1379 int ret;
1380
1381 if (group->default_domain && group->domain != group->default_domain)
1382 return -EBUSY;
1383
1384 ret = __iommu_group_for_each_dev(group, domain,
1385 iommu_group_do_attach_device);
1386 if (ret == 0)
1387 group->domain = domain;
1388
1389 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001390}
1391
1392int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1393{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001394 int ret;
1395
1396 mutex_lock(&group->mutex);
1397 ret = __iommu_attach_group(domain, group);
1398 mutex_unlock(&group->mutex);
1399
1400 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001401}
1402EXPORT_SYMBOL_GPL(iommu_attach_group);
1403
1404static int iommu_group_do_detach_device(struct device *dev, void *data)
1405{
1406 struct iommu_domain *domain = data;
1407
Joerg Roedel426a2732015-05-28 18:41:30 +02001408 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001409
1410 return 0;
1411}
1412
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001413static void __iommu_detach_group(struct iommu_domain *domain,
1414 struct iommu_group *group)
1415{
1416 int ret;
1417
1418 if (!group->default_domain) {
1419 __iommu_group_for_each_dev(group, domain,
1420 iommu_group_do_detach_device);
1421 group->domain = NULL;
1422 return;
1423 }
1424
1425 if (group->domain == group->default_domain)
1426 return;
1427
1428 /* Detach by re-attaching to the default domain */
1429 ret = __iommu_group_for_each_dev(group, group->default_domain,
1430 iommu_group_do_attach_device);
1431 if (ret != 0)
1432 WARN_ON(1);
1433 else
1434 group->domain = group->default_domain;
1435}
1436
Alex Williamsond72e31c2012-05-30 14:18:53 -06001437void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1438{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001439 mutex_lock(&group->mutex);
1440 __iommu_detach_group(domain, group);
1441 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001442}
1443EXPORT_SYMBOL_GPL(iommu_detach_group);
1444
Varun Sethibb5547a2013-03-29 01:23:58 +05301445phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001446{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001447 if (unlikely(domain->ops->iova_to_phys == NULL))
1448 return 0;
1449
1450 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001451}
1452EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001453
Alex Williamsonbd139692013-06-17 19:57:34 -06001454static size_t iommu_pgsize(struct iommu_domain *domain,
1455 unsigned long addr_merge, size_t size)
1456{
1457 unsigned int pgsize_idx;
1458 size_t pgsize;
1459
1460 /* Max page size that still fits into 'size' */
1461 pgsize_idx = __fls(size);
1462
1463 /* need to consider alignment requirements ? */
1464 if (likely(addr_merge)) {
1465 /* Max page size allowed by address */
1466 unsigned int align_pgsize_idx = __ffs(addr_merge);
1467 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1468 }
1469
1470 /* build a mask of acceptable page sizes */
1471 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1472
1473 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001474 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001475
1476 /* make sure we're still sane */
1477 BUG_ON(!pgsize);
1478
1479 /* pick the biggest page */
1480 pgsize_idx = __fls(pgsize);
1481 pgsize = 1UL << pgsize_idx;
1482
1483 return pgsize;
1484}
1485
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001486int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001487 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001488{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001489 unsigned long orig_iova = iova;
1490 unsigned int min_pagesz;
1491 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001492 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001493 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001494
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001495 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001496 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001497 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001498
Joerg Roedela10315e2015-03-26 13:43:06 +01001499 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1500 return -EINVAL;
1501
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001502 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001503 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001504
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001505 /*
1506 * both the virtual address and the physical one, as well as
1507 * the size of the mapping, must be aligned (at least) to the
1508 * size of the smallest page supported by the hardware
1509 */
1510 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001511 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001512 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001513 return -EINVAL;
1514 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001515
Fabio Estevamabedb042013-08-22 10:25:42 -03001516 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001517
1518 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001519 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001520
Fabio Estevamabedb042013-08-22 10:25:42 -03001521 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001522 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001523
1524 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1525 if (ret)
1526 break;
1527
1528 iova += pgsize;
1529 paddr += pgsize;
1530 size -= pgsize;
1531 }
1532
1533 /* unroll mapping in case something went wrong */
1534 if (ret)
1535 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001536 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001537 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001538
1539 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001540}
1541EXPORT_SYMBOL_GPL(iommu_map);
1542
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001543size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001544{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001545 size_t unmapped_page, unmapped = 0;
1546 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001547 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001548
Joerg Roedel57886512013-01-29 13:41:09 +01001549 if (unlikely(domain->ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001550 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001551 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001552
Joerg Roedela10315e2015-03-26 13:43:06 +01001553 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1554 return -EINVAL;
1555
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001556 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001557 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001558
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001559 /*
1560 * The virtual address, as well as the size of the mapping, must be
1561 * aligned (at least) to the size of the smallest page supported
1562 * by the hardware
1563 */
1564 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001565 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1566 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001567 return -EINVAL;
1568 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001569
Joe Perches6197ca82013-06-23 12:29:04 -07001570 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001571
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001572 /*
1573 * Keep iterating until we either unmap 'size' bytes (or more)
1574 * or we hit an area that isn't mapped.
1575 */
1576 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001577 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001578
Alex Williamsonbd139692013-06-17 19:57:34 -06001579 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001580 if (!unmapped_page)
1581 break;
1582
Joe Perches6197ca82013-06-23 12:29:04 -07001583 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1584 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001585
1586 iova += unmapped_page;
1587 unmapped += unmapped_page;
1588 }
1589
Shuah Khandb8614d2015-01-16 20:53:17 -07001590 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001591 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001592}
1593EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001594
Olav Haugan315786e2014-10-25 09:55:16 -07001595size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1596 struct scatterlist *sg, unsigned int nents, int prot)
1597{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001598 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001599 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001600 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001601 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001602
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001603 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001604 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001605
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001606 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001607
1608 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001609 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001610
1611 /*
1612 * We are mapping on IOMMU page boundaries, so offset within
1613 * the page must be 0. However, the IOMMU may support pages
1614 * smaller than PAGE_SIZE, so s->offset may still represent
1615 * an offset of that boundary within the CPU page.
1616 */
1617 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001618 goto out_err;
1619
1620 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1621 if (ret)
1622 goto out_err;
1623
1624 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001625 }
1626
1627 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001628
1629out_err:
1630 /* undo mappings already done */
1631 iommu_unmap(domain, iova, mapped);
1632
1633 return 0;
1634
Olav Haugan315786e2014-10-25 09:55:16 -07001635}
1636EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001637
1638int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301639 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001640{
1641 if (unlikely(domain->ops->domain_window_enable == NULL))
1642 return -ENODEV;
1643
Varun Sethi80f97f02013-03-29 01:24:00 +05301644 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1645 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001646}
1647EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1648
1649void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1650{
1651 if (unlikely(domain->ops->domain_window_disable == NULL))
1652 return;
1653
1654 return domain->ops->domain_window_disable(domain, wnd_nr);
1655}
1656EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1657
Alex Williamsond72e31c2012-05-30 14:18:53 -06001658static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001659{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001660 iommu_group_kset = kset_create_and_add("iommu_groups",
1661 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001662 BUG_ON(!iommu_group_kset);
1663
1664 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001665}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001666core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001667
1668int iommu_domain_get_attr(struct iommu_domain *domain,
1669 enum iommu_attr attr, void *data)
1670{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001671 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001672 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001673 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001674 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001675
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001676 switch (attr) {
1677 case DOMAIN_ATTR_GEOMETRY:
1678 geometry = data;
1679 *geometry = domain->geometry;
1680
1681 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001682 case DOMAIN_ATTR_PAGING:
1683 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001684 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001685 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001686 case DOMAIN_ATTR_WINDOWS:
1687 count = data;
1688
1689 if (domain->ops->domain_get_windows != NULL)
1690 *count = domain->ops->domain_get_windows(domain);
1691 else
1692 ret = -ENODEV;
1693
1694 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001695 default:
1696 if (!domain->ops->domain_get_attr)
1697 return -EINVAL;
1698
1699 ret = domain->ops->domain_get_attr(domain, attr, data);
1700 }
1701
1702 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001703}
1704EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1705
1706int iommu_domain_set_attr(struct iommu_domain *domain,
1707 enum iommu_attr attr, void *data)
1708{
Joerg Roedel69356712013-02-04 14:00:01 +01001709 int ret = 0;
1710 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001711
Joerg Roedel69356712013-02-04 14:00:01 +01001712 switch (attr) {
1713 case DOMAIN_ATTR_WINDOWS:
1714 count = data;
1715
1716 if (domain->ops->domain_set_windows != NULL)
1717 ret = domain->ops->domain_set_windows(domain, *count);
1718 else
1719 ret = -ENODEV;
1720
1721 break;
1722 default:
1723 if (domain->ops->domain_set_attr == NULL)
1724 return -EINVAL;
1725
1726 ret = domain->ops->domain_set_attr(domain, attr, data);
1727 }
1728
1729 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001730}
1731EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001732
Eric Augere5b52342017-01-19 20:57:47 +00001733void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001734{
1735 const struct iommu_ops *ops = dev->bus->iommu_ops;
1736
Eric Augere5b52342017-01-19 20:57:47 +00001737 if (ops && ops->get_resv_regions)
1738 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001739}
1740
Eric Augere5b52342017-01-19 20:57:47 +00001741void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001742{
1743 const struct iommu_ops *ops = dev->bus->iommu_ops;
1744
Eric Augere5b52342017-01-19 20:57:47 +00001745 if (ops && ops->put_resv_regions)
1746 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001747}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001748
Eric Auger2b20cbb2017-01-19 20:57:49 +00001749struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
1750 size_t length,
1751 int prot, int type)
1752{
1753 struct iommu_resv_region *region;
1754
1755 region = kzalloc(sizeof(*region), GFP_KERNEL);
1756 if (!region)
1757 return NULL;
1758
1759 INIT_LIST_HEAD(&region->list);
1760 region->start = start;
1761 region->length = length;
1762 region->prot = prot;
1763 region->type = type;
1764 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001765}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001766
1767/* Request that a device is direct mapped by the IOMMU */
1768int iommu_request_dm_for_dev(struct device *dev)
1769{
1770 struct iommu_domain *dm_domain;
1771 struct iommu_group *group;
1772 int ret;
1773
1774 /* Device must already be in a group before calling this function */
1775 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001776 if (IS_ERR(group))
1777 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001778
1779 mutex_lock(&group->mutex);
1780
1781 /* Check if the default domain is already direct mapped */
1782 ret = 0;
1783 if (group->default_domain &&
1784 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1785 goto out;
1786
1787 /* Don't change mappings of existing devices */
1788 ret = -EBUSY;
1789 if (iommu_group_device_count(group) != 1)
1790 goto out;
1791
1792 /* Allocate a direct mapped domain */
1793 ret = -ENOMEM;
1794 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1795 if (!dm_domain)
1796 goto out;
1797
1798 /* Attach the device to the domain */
1799 ret = __iommu_attach_group(dm_domain, group);
1800 if (ret) {
1801 iommu_domain_free(dm_domain);
1802 goto out;
1803 }
1804
1805 /* Make the direct mapped domain the default for this group */
1806 if (group->default_domain)
1807 iommu_domain_free(group->default_domain);
1808 group->default_domain = dm_domain;
1809
1810 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1811
1812 ret = 0;
1813out:
1814 mutex_unlock(&group->mutex);
1815 iommu_group_put(group);
1816
1817 return ret;
1818}
Robin Murphy57f98d22016-09-13 10:54:14 +01001819
Joerg Roedel534766d2017-01-31 16:58:42 +01001820const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001821{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001822 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001823 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001824
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001825 spin_lock(&iommu_device_lock);
1826 list_for_each_entry(iommu, &iommu_device_list, list)
1827 if (iommu->fwnode == fwnode) {
1828 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001829 break;
1830 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001831 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001832 return ops;
1833}
1834
Robin Murphy57f98d22016-09-13 10:54:14 +01001835int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1836 const struct iommu_ops *ops)
1837{
1838 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1839
1840 if (fwspec)
1841 return ops == fwspec->ops ? 0 : -EINVAL;
1842
1843 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1844 if (!fwspec)
1845 return -ENOMEM;
1846
1847 of_node_get(to_of_node(iommu_fwnode));
1848 fwspec->iommu_fwnode = iommu_fwnode;
1849 fwspec->ops = ops;
1850 dev->iommu_fwspec = fwspec;
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1854
1855void iommu_fwspec_free(struct device *dev)
1856{
1857 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1858
1859 if (fwspec) {
1860 fwnode_handle_put(fwspec->iommu_fwnode);
1861 kfree(fwspec);
1862 dev->iommu_fwspec = NULL;
1863 }
1864}
1865EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1866
1867int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1868{
1869 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1870 size_t size;
1871 int i;
1872
1873 if (!fwspec)
1874 return -EINVAL;
1875
1876 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1877 if (size > sizeof(*fwspec)) {
1878 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1879 if (!fwspec)
1880 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08001881
1882 dev->iommu_fwspec = fwspec;
Robin Murphy57f98d22016-09-13 10:54:14 +01001883 }
1884
1885 for (i = 0; i < num_ids; i++)
1886 fwspec->ids[fwspec->num_ids + i] = ids[i];
1887
1888 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01001889 return 0;
1890}
1891EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);