blob: de09e1e35830f7e3481af6c2b8ab19ded585a822 [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;
119
120 if (!str || strtobool(str, &pt))
121 return -EINVAL;
122
123 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
124 return 0;
125}
126early_param("iommu.passthrough", iommu_set_def_domain_type);
127
Alex Williamsond72e31c2012-05-30 14:18:53 -0600128static ssize_t iommu_group_attr_show(struct kobject *kobj,
129 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -0400130{
Alex Williamsond72e31c2012-05-30 14:18:53 -0600131 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
132 struct iommu_group *group = to_iommu_group(kobj);
133 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -0400134
Alex Williamsond72e31c2012-05-30 14:18:53 -0600135 if (attr->show)
136 ret = attr->show(group, buf);
137 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400138}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600139
140static ssize_t iommu_group_attr_store(struct kobject *kobj,
141 struct attribute *__attr,
142 const char *buf, size_t count)
143{
144 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
145 struct iommu_group *group = to_iommu_group(kobj);
146 ssize_t ret = -EIO;
147
148 if (attr->store)
149 ret = attr->store(group, buf, count);
150 return ret;
151}
152
153static const struct sysfs_ops iommu_group_sysfs_ops = {
154 .show = iommu_group_attr_show,
155 .store = iommu_group_attr_store,
156};
157
158static int iommu_group_create_file(struct iommu_group *group,
159 struct iommu_group_attribute *attr)
160{
161 return sysfs_create_file(&group->kobj, &attr->attr);
162}
163
164static void iommu_group_remove_file(struct iommu_group *group,
165 struct iommu_group_attribute *attr)
166{
167 sysfs_remove_file(&group->kobj, &attr->attr);
168}
169
170static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
171{
172 return sprintf(buf, "%s\n", group->name);
173}
174
Eric Auger6c65fb32017-01-19 20:57:51 +0000175/**
176 * iommu_insert_resv_region - Insert a new region in the
177 * list of reserved regions.
178 * @new: new region to insert
179 * @regions: list of regions
180 *
181 * The new element is sorted by address with respect to the other
182 * regions of the same type. In case it overlaps with another
183 * region of the same type, regions are merged. In case it
184 * overlaps with another region of different type, regions are
185 * not merged.
186 */
187static int iommu_insert_resv_region(struct iommu_resv_region *new,
188 struct list_head *regions)
189{
190 struct iommu_resv_region *region;
191 phys_addr_t start = new->start;
192 phys_addr_t end = new->start + new->length - 1;
193 struct list_head *pos = regions->next;
194
195 while (pos != regions) {
196 struct iommu_resv_region *entry =
197 list_entry(pos, struct iommu_resv_region, list);
198 phys_addr_t a = entry->start;
199 phys_addr_t b = entry->start + entry->length - 1;
200 int type = entry->type;
201
202 if (end < a) {
203 goto insert;
204 } else if (start > b) {
205 pos = pos->next;
206 } else if ((start >= a) && (end <= b)) {
207 if (new->type == type)
208 goto done;
209 else
210 pos = pos->next;
211 } else {
212 if (new->type == type) {
213 phys_addr_t new_start = min(a, start);
214 phys_addr_t new_end = max(b, end);
215
216 list_del(&entry->list);
217 entry->start = new_start;
218 entry->length = new_end - new_start + 1;
219 iommu_insert_resv_region(entry, regions);
220 } else {
221 pos = pos->next;
222 }
223 }
224 }
225insert:
226 region = iommu_alloc_resv_region(new->start, new->length,
227 new->prot, new->type);
228 if (!region)
229 return -ENOMEM;
230
231 list_add_tail(&region->list, pos);
232done:
233 return 0;
234}
235
236static int
237iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
238 struct list_head *group_resv_regions)
239{
240 struct iommu_resv_region *entry;
Eric Augera514a6e2017-02-06 10:11:38 +0100241 int ret = 0;
Eric Auger6c65fb32017-01-19 20:57:51 +0000242
243 list_for_each_entry(entry, dev_resv_regions, list) {
244 ret = iommu_insert_resv_region(entry, group_resv_regions);
245 if (ret)
246 break;
247 }
248 return ret;
249}
250
251int iommu_get_group_resv_regions(struct iommu_group *group,
252 struct list_head *head)
253{
Joerg Roedel8d2932d2017-02-10 15:13:10 +0100254 struct group_device *device;
Eric Auger6c65fb32017-01-19 20:57:51 +0000255 int ret = 0;
256
257 mutex_lock(&group->mutex);
258 list_for_each_entry(device, &group->devices, list) {
259 struct list_head dev_resv_regions;
260
261 INIT_LIST_HEAD(&dev_resv_regions);
262 iommu_get_resv_regions(device->dev, &dev_resv_regions);
263 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
264 iommu_put_resv_regions(device->dev, &dev_resv_regions);
265 if (ret)
266 break;
267 }
268 mutex_unlock(&group->mutex);
269 return ret;
270}
271EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
272
Eric Augerbc7d12b92017-01-19 20:57:52 +0000273static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
274 char *buf)
275{
276 struct iommu_resv_region *region, *next;
277 struct list_head group_resv_regions;
278 char *str = buf;
279
280 INIT_LIST_HEAD(&group_resv_regions);
281 iommu_get_group_resv_regions(group, &group_resv_regions);
282
283 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
284 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
285 (long long int)region->start,
286 (long long int)(region->start +
287 region->length - 1),
288 iommu_group_resv_type_string[region->type]);
289 kfree(region);
290 }
291
292 return (str - buf);
293}
294
Alex Williamsond72e31c2012-05-30 14:18:53 -0600295static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
296
Eric Augerbc7d12b92017-01-19 20:57:52 +0000297static IOMMU_GROUP_ATTR(reserved_regions, 0444,
298 iommu_group_show_resv_regions, NULL);
299
Alex Williamsond72e31c2012-05-30 14:18:53 -0600300static void iommu_group_release(struct kobject *kobj)
301{
302 struct iommu_group *group = to_iommu_group(kobj);
303
Joerg Roedel269aa802015-05-28 18:41:25 +0200304 pr_debug("Releasing group %d\n", group->id);
305
Alex Williamsond72e31c2012-05-30 14:18:53 -0600306 if (group->iommu_data_release)
307 group->iommu_data_release(group->iommu_data);
308
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200309 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600310
Joerg Roedel53723dc2015-05-28 18:41:29 +0200311 if (group->default_domain)
312 iommu_domain_free(group->default_domain);
313
Alex Williamsond72e31c2012-05-30 14:18:53 -0600314 kfree(group->name);
315 kfree(group);
316}
317
318static struct kobj_type iommu_group_ktype = {
319 .sysfs_ops = &iommu_group_sysfs_ops,
320 .release = iommu_group_release,
321};
322
323/**
324 * iommu_group_alloc - Allocate a new group
325 * @name: Optional name to associate with group, visible in sysfs
326 *
327 * This function is called by an iommu driver to allocate a new iommu
328 * group. The iommu group represents the minimum granularity of the iommu.
329 * Upon successful return, the caller holds a reference to the supplied
330 * group in order to hold the group until devices are added. Use
331 * iommu_group_put() to release this extra reference count, allowing the
332 * group to be automatically reclaimed once it has no devices or external
333 * references.
334 */
335struct iommu_group *iommu_group_alloc(void)
336{
337 struct iommu_group *group;
338 int ret;
339
340 group = kzalloc(sizeof(*group), GFP_KERNEL);
341 if (!group)
342 return ERR_PTR(-ENOMEM);
343
344 group->kobj.kset = iommu_group_kset;
345 mutex_init(&group->mutex);
346 INIT_LIST_HEAD(&group->devices);
347 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
348
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200349 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
350 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600351 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200352 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600353 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200354 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600355
356 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
357 NULL, "%d", group->id);
358 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200359 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600360 kfree(group);
361 return ERR_PTR(ret);
362 }
363
364 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
365 if (!group->devices_kobj) {
366 kobject_put(&group->kobj); /* triggers .release & free */
367 return ERR_PTR(-ENOMEM);
368 }
369
370 /*
371 * The devices_kobj holds a reference on the group kobject, so
372 * as long as that exists so will the group. We can therefore
373 * use the devices_kobj for reference counting.
374 */
375 kobject_put(&group->kobj);
376
Eric Augerbc7d12b92017-01-19 20:57:52 +0000377 ret = iommu_group_create_file(group,
378 &iommu_group_attr_reserved_regions);
379 if (ret)
380 return ERR_PTR(ret);
381
Joerg Roedel269aa802015-05-28 18:41:25 +0200382 pr_debug("Allocated group %d\n", group->id);
383
Alex Williamsond72e31c2012-05-30 14:18:53 -0600384 return group;
385}
386EXPORT_SYMBOL_GPL(iommu_group_alloc);
387
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100388struct iommu_group *iommu_group_get_by_id(int id)
389{
390 struct kobject *group_kobj;
391 struct iommu_group *group;
392 const char *name;
393
394 if (!iommu_group_kset)
395 return NULL;
396
397 name = kasprintf(GFP_KERNEL, "%d", id);
398 if (!name)
399 return NULL;
400
401 group_kobj = kset_find_obj(iommu_group_kset, name);
402 kfree(name);
403
404 if (!group_kobj)
405 return NULL;
406
407 group = container_of(group_kobj, struct iommu_group, kobj);
408 BUG_ON(group->id != id);
409
410 kobject_get(group->devices_kobj);
411 kobject_put(&group->kobj);
412
413 return group;
414}
415EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
416
Alex Williamsond72e31c2012-05-30 14:18:53 -0600417/**
418 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
419 * @group: the group
420 *
421 * iommu drivers can store data in the group for use when doing iommu
422 * operations. This function provides a way to retrieve it. Caller
423 * should hold a group reference.
424 */
425void *iommu_group_get_iommudata(struct iommu_group *group)
426{
427 return group->iommu_data;
428}
429EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
430
431/**
432 * iommu_group_set_iommudata - set iommu_data for a group
433 * @group: the group
434 * @iommu_data: new data
435 * @release: release function for iommu_data
436 *
437 * iommu drivers can store data in the group for use when doing iommu
438 * operations. This function provides a way to set the data after
439 * the group has been allocated. Caller should hold a group reference.
440 */
441void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
442 void (*release)(void *iommu_data))
443{
444 group->iommu_data = iommu_data;
445 group->iommu_data_release = release;
446}
447EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
448
449/**
450 * iommu_group_set_name - set name for a group
451 * @group: the group
452 * @name: name
453 *
454 * Allow iommu driver to set a name for a group. When set it will
455 * appear in a name attribute file under the group in sysfs.
456 */
457int iommu_group_set_name(struct iommu_group *group, const char *name)
458{
459 int ret;
460
461 if (group->name) {
462 iommu_group_remove_file(group, &iommu_group_attr_name);
463 kfree(group->name);
464 group->name = NULL;
465 if (!name)
466 return 0;
467 }
468
469 group->name = kstrdup(name, GFP_KERNEL);
470 if (!group->name)
471 return -ENOMEM;
472
473 ret = iommu_group_create_file(group, &iommu_group_attr_name);
474 if (ret) {
475 kfree(group->name);
476 group->name = NULL;
477 return ret;
478 }
479
480 return 0;
481}
482EXPORT_SYMBOL_GPL(iommu_group_set_name);
483
Joerg Roedelbeed2822015-05-28 18:41:34 +0200484static int iommu_group_create_direct_mappings(struct iommu_group *group,
485 struct device *dev)
486{
487 struct iommu_domain *domain = group->default_domain;
Eric Augere5b52342017-01-19 20:57:47 +0000488 struct iommu_resv_region *entry;
Joerg Roedelbeed2822015-05-28 18:41:34 +0200489 struct list_head mappings;
490 unsigned long pg_size;
491 int ret = 0;
492
493 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
494 return 0;
495
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100496 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200497
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100498 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200499 INIT_LIST_HEAD(&mappings);
500
Eric Augere5b52342017-01-19 20:57:47 +0000501 iommu_get_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200502
503 /* We need to consider overlapping regions for different devices */
504 list_for_each_entry(entry, &mappings, list) {
505 dma_addr_t start, end, addr;
506
Eric Augere5b52342017-01-19 20:57:47 +0000507 if (domain->ops->apply_resv_region)
508 domain->ops->apply_resv_region(dev, domain, entry);
Joerg Roedel33b21a62016-07-05 13:07:53 +0200509
Joerg Roedelbeed2822015-05-28 18:41:34 +0200510 start = ALIGN(entry->start, pg_size);
511 end = ALIGN(entry->start + entry->length, pg_size);
512
Eric Auger544a25d2017-01-19 20:57:50 +0000513 if (entry->type != IOMMU_RESV_DIRECT)
514 continue;
515
Joerg Roedelbeed2822015-05-28 18:41:34 +0200516 for (addr = start; addr < end; addr += pg_size) {
517 phys_addr_t phys_addr;
518
519 phys_addr = iommu_iova_to_phys(domain, addr);
520 if (phys_addr)
521 continue;
522
523 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
524 if (ret)
525 goto out;
526 }
527
528 }
529
530out:
Eric Augere5b52342017-01-19 20:57:47 +0000531 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200532
533 return ret;
534}
535
Alex Williamsond72e31c2012-05-30 14:18:53 -0600536/**
537 * iommu_group_add_device - add a device to an iommu group
538 * @group: the group into which to add the device (reference should be held)
539 * @dev: the device
540 *
541 * This function is called by an iommu driver to add a device into a
542 * group. Adding a device increments the group reference count.
543 */
544int iommu_group_add_device(struct iommu_group *group, struct device *dev)
545{
546 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100547 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600548
549 device = kzalloc(sizeof(*device), GFP_KERNEL);
550 if (!device)
551 return -ENOMEM;
552
553 device->dev = dev;
554
555 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000556 if (ret)
557 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600558
559 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
560rename:
561 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000562 ret = -ENOMEM;
563 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600564 }
565
566 ret = sysfs_create_link_nowarn(group->devices_kobj,
567 &dev->kobj, device->name);
568 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600569 if (ret == -EEXIST && i >= 0) {
570 /*
571 * Account for the slim chance of collision
572 * and append an instance to the name.
573 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000574 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600575 device->name = kasprintf(GFP_KERNEL, "%s.%d",
576 kobject_name(&dev->kobj), i++);
577 goto rename;
578 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000579 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600580 }
581
582 kobject_get(group->devices_kobj);
583
584 dev->iommu_group = group;
585
Joerg Roedelbeed2822015-05-28 18:41:34 +0200586 iommu_group_create_direct_mappings(group, dev);
587
Alex Williamsond72e31c2012-05-30 14:18:53 -0600588 mutex_lock(&group->mutex);
589 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200590 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000591 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600592 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000593 if (ret)
594 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600595
596 /* Notify any listeners about change to group. */
597 blocking_notifier_call_chain(&group->notifier,
598 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600599
600 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200601
602 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
603
Alex Williamsond72e31c2012-05-30 14:18:53 -0600604 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000605
606err_put_group:
607 mutex_lock(&group->mutex);
608 list_del(&device->list);
609 mutex_unlock(&group->mutex);
610 dev->iommu_group = NULL;
611 kobject_put(group->devices_kobj);
612err_free_name:
613 kfree(device->name);
614err_remove_link:
615 sysfs_remove_link(&dev->kobj, "iommu_group");
616err_free_device:
617 kfree(device);
618 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
619 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600620}
621EXPORT_SYMBOL_GPL(iommu_group_add_device);
622
623/**
624 * iommu_group_remove_device - remove a device from it's current group
625 * @dev: device to be removed
626 *
627 * This function is called by an iommu driver to remove the device from
628 * it's current group. This decrements the iommu group reference count.
629 */
630void iommu_group_remove_device(struct device *dev)
631{
632 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100633 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600634
Joerg Roedel269aa802015-05-28 18:41:25 +0200635 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
636
Alex Williamsond72e31c2012-05-30 14:18:53 -0600637 /* Pre-notify listeners that a device is being removed. */
638 blocking_notifier_call_chain(&group->notifier,
639 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
640
641 mutex_lock(&group->mutex);
642 list_for_each_entry(tmp_device, &group->devices, list) {
643 if (tmp_device->dev == dev) {
644 device = tmp_device;
645 list_del(&device->list);
646 break;
647 }
648 }
649 mutex_unlock(&group->mutex);
650
651 if (!device)
652 return;
653
654 sysfs_remove_link(group->devices_kobj, device->name);
655 sysfs_remove_link(&dev->kobj, "iommu_group");
656
Shuah Khan2e757082013-08-15 11:59:25 -0600657 trace_remove_device_from_group(group->id, dev);
658
Alex Williamsond72e31c2012-05-30 14:18:53 -0600659 kfree(device->name);
660 kfree(device);
661 dev->iommu_group = NULL;
662 kobject_put(group->devices_kobj);
663}
664EXPORT_SYMBOL_GPL(iommu_group_remove_device);
665
Joerg Roedel426a2732015-05-28 18:41:30 +0200666static int iommu_group_device_count(struct iommu_group *group)
667{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100668 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200669 int ret = 0;
670
671 list_for_each_entry(entry, &group->devices, list)
672 ret++;
673
674 return ret;
675}
676
Alex Williamsond72e31c2012-05-30 14:18:53 -0600677/**
678 * iommu_group_for_each_dev - iterate over each device in the group
679 * @group: the group
680 * @data: caller opaque data to be passed to callback function
681 * @fn: caller supplied callback function
682 *
683 * This function is called by group users to iterate over group devices.
684 * Callers should hold a reference count to the group during callback.
685 * The group->mutex is held across callbacks, which will block calls to
686 * iommu_group_add/remove_device.
687 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200688static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
689 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600690{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100691 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600692 int ret = 0;
693
Alex Williamsond72e31c2012-05-30 14:18:53 -0600694 list_for_each_entry(device, &group->devices, list) {
695 ret = fn(device->dev, data);
696 if (ret)
697 break;
698 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200699 return ret;
700}
701
702
703int iommu_group_for_each_dev(struct iommu_group *group, void *data,
704 int (*fn)(struct device *, void *))
705{
706 int ret;
707
708 mutex_lock(&group->mutex);
709 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600710 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200711
Alex Williamsond72e31c2012-05-30 14:18:53 -0600712 return ret;
713}
714EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
715
716/**
717 * iommu_group_get - Return the group for a device and increment reference
718 * @dev: get the group that this device belongs to
719 *
720 * This function is called by iommu drivers and users to get the group
721 * for the specified device. If found, the group is returned and the group
722 * reference in incremented, else NULL.
723 */
724struct iommu_group *iommu_group_get(struct device *dev)
725{
726 struct iommu_group *group = dev->iommu_group;
727
728 if (group)
729 kobject_get(group->devices_kobj);
730
731 return group;
732}
733EXPORT_SYMBOL_GPL(iommu_group_get);
734
735/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000736 * iommu_group_ref_get - Increment reference on a group
737 * @group: the group to use, must not be NULL
738 *
739 * This function is called by iommu drivers to take additional references on an
740 * existing group. Returns the given group for convenience.
741 */
742struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
743{
744 kobject_get(group->devices_kobj);
745 return group;
746}
747
748/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600749 * iommu_group_put - Decrement group reference
750 * @group: the group to use
751 *
752 * This function is called by iommu drivers and users to release the
753 * iommu group. Once the reference count is zero, the group is released.
754 */
755void iommu_group_put(struct iommu_group *group)
756{
757 if (group)
758 kobject_put(group->devices_kobj);
759}
760EXPORT_SYMBOL_GPL(iommu_group_put);
761
762/**
763 * iommu_group_register_notifier - Register a notifier for group changes
764 * @group: the group to watch
765 * @nb: notifier block to signal
766 *
767 * This function allows iommu group users to track changes in a group.
768 * See include/linux/iommu.h for actions sent via this notifier. Caller
769 * should hold a reference to the group throughout notifier registration.
770 */
771int iommu_group_register_notifier(struct iommu_group *group,
772 struct notifier_block *nb)
773{
774 return blocking_notifier_chain_register(&group->notifier, nb);
775}
776EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
777
778/**
779 * iommu_group_unregister_notifier - Unregister a notifier
780 * @group: the group to watch
781 * @nb: notifier block to signal
782 *
783 * Unregister a previously registered group notifier block.
784 */
785int iommu_group_unregister_notifier(struct iommu_group *group,
786 struct notifier_block *nb)
787{
788 return blocking_notifier_chain_unregister(&group->notifier, nb);
789}
790EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
791
792/**
793 * iommu_group_id - Return ID for a group
794 * @group: the group to ID
795 *
796 * Return the unique ID for the group matching the sysfs group number.
797 */
798int iommu_group_id(struct iommu_group *group)
799{
800 return group->id;
801}
802EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400803
Alex Williamsonf096c062014-09-19 10:03:06 -0600804static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
805 unsigned long *devfns);
806
Alex Williamson104a1c12014-07-03 09:51:18 -0600807/*
808 * To consider a PCI device isolated, we require ACS to support Source
809 * Validation, Request Redirection, Completer Redirection, and Upstream
810 * Forwarding. This effectively means that devices cannot spoof their
811 * requester ID, requests and completions cannot be redirected, and all
812 * transactions are forwarded upstream, even as it passes through a
813 * bridge where the target device is downstream.
814 */
815#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
816
Alex Williamsonf096c062014-09-19 10:03:06 -0600817/*
818 * For multifunction devices which are not isolated from each other, find
819 * all the other non-isolated functions and look for existing groups. For
820 * each function, we also need to look for aliases to or from other devices
821 * that may already have a group.
822 */
823static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
824 unsigned long *devfns)
825{
826 struct pci_dev *tmp = NULL;
827 struct iommu_group *group;
828
829 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
830 return NULL;
831
832 for_each_pci_dev(tmp) {
833 if (tmp == pdev || tmp->bus != pdev->bus ||
834 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
835 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
836 continue;
837
838 group = get_pci_alias_group(tmp, devfns);
839 if (group) {
840 pci_dev_put(tmp);
841 return group;
842 }
843 }
844
845 return NULL;
846}
847
848/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100849 * Look for aliases to or from the given device for existing groups. DMA
850 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600851 * space is quite small (especially since we're really only looking at pcie
852 * device, and therefore only expect multiple slots on the root complex or
853 * downstream switch ports). It's conceivable though that a pair of
854 * multifunction devices could have aliases between them that would cause a
855 * loop. To prevent this, we use a bitmap to track where we've been.
856 */
857static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
858 unsigned long *devfns)
859{
860 struct pci_dev *tmp = NULL;
861 struct iommu_group *group;
862
863 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
864 return NULL;
865
866 group = iommu_group_get(&pdev->dev);
867 if (group)
868 return group;
869
870 for_each_pci_dev(tmp) {
871 if (tmp == pdev || tmp->bus != pdev->bus)
872 continue;
873
874 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100875 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600876 group = get_pci_alias_group(tmp, devfns);
877 if (group) {
878 pci_dev_put(tmp);
879 return group;
880 }
881
882 group = get_pci_function_alias_group(tmp, devfns);
883 if (group) {
884 pci_dev_put(tmp);
885 return group;
886 }
887 }
888 }
889
890 return NULL;
891}
892
Alex Williamson104a1c12014-07-03 09:51:18 -0600893struct group_for_pci_data {
894 struct pci_dev *pdev;
895 struct iommu_group *group;
896};
897
898/*
899 * DMA alias iterator callback, return the last seen device. Stop and return
900 * the IOMMU group if we find one along the way.
901 */
902static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
903{
904 struct group_for_pci_data *data = opaque;
905
906 data->pdev = pdev;
907 data->group = iommu_group_get(&pdev->dev);
908
909 return data->group != NULL;
910}
911
912/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200913 * Generic device_group call-back function. It just allocates one
914 * iommu-group per device.
915 */
916struct iommu_group *generic_device_group(struct device *dev)
917{
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200918 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +0200919}
920
921/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600922 * Use standard PCI bus topology, isolation features, and DMA alias quirks
923 * to find or create an IOMMU group for a device.
924 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200925struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600926{
Joerg Roedel5e622922015-10-21 23:51:37 +0200927 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600928 struct group_for_pci_data data;
929 struct pci_bus *bus;
930 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600931 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600932
Joerg Roedel5e622922015-10-21 23:51:37 +0200933 if (WARN_ON(!dev_is_pci(dev)))
934 return ERR_PTR(-EINVAL);
935
Alex Williamson104a1c12014-07-03 09:51:18 -0600936 /*
937 * Find the upstream DMA alias for the device. A device must not
938 * be aliased due to topology in order to have its own IOMMU group.
939 * If we find an alias along the way that already belongs to a
940 * group, use it.
941 */
942 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
943 return data.group;
944
945 pdev = data.pdev;
946
947 /*
948 * Continue upstream from the point of minimum IOMMU granularity
949 * due to aliases to the point where devices are protected from
950 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
951 * group, use it.
952 */
953 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
954 if (!bus->self)
955 continue;
956
957 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
958 break;
959
960 pdev = bus->self;
961
962 group = iommu_group_get(&pdev->dev);
963 if (group)
964 return group;
965 }
966
967 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600968 * Look for existing groups on device aliases. If we alias another
969 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600970 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600971 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
972 if (group)
973 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600974
975 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600976 * Look for existing groups on non-isolated functions on the same
977 * slot and aliases of those funcions, if any. No need to clear
978 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600979 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600980 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
981 if (group)
982 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600983
984 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200985 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -0600986}
987
988/**
989 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
990 * @dev: target device
991 *
992 * This function is intended to be called by IOMMU drivers and extended to
993 * support common, bus-defined algorithms when determining or creating the
994 * IOMMU group for a device. On success, the caller will hold a reference
995 * to the returned IOMMU group, which will already include the provided
996 * device. The reference should be released with iommu_group_put().
997 */
998struct iommu_group *iommu_group_get_for_dev(struct device *dev)
999{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001000 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001001 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001002 int ret;
1003
1004 group = iommu_group_get(dev);
1005 if (group)
1006 return group;
1007
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001008 group = ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001009
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001010 if (ops && ops->device_group)
1011 group = ops->device_group(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -06001012
1013 if (IS_ERR(group))
1014 return group;
1015
Joerg Roedel12282362015-10-21 23:51:43 +02001016 /*
1017 * Try to allocate a default domain - needs support from the
1018 * IOMMU driver.
1019 */
1020 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001021 struct iommu_domain *dom;
1022
1023 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1024 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1025 dev_warn(dev,
1026 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1027 iommu_def_domain_type);
1028 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1029 }
1030
1031 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001032 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001033 group->domain = dom;
Joerg Roedel12282362015-10-21 23:51:43 +02001034 }
1035
Alex Williamson104a1c12014-07-03 09:51:18 -06001036 ret = iommu_group_add_device(group, dev);
1037 if (ret) {
1038 iommu_group_put(group);
1039 return ERR_PTR(ret);
1040 }
1041
1042 return group;
1043}
1044
Joerg Roedel6827ca82015-05-28 18:41:35 +02001045struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1046{
1047 return group->default_domain;
1048}
1049
Alex Williamson14604322011-10-21 15:56:05 -04001050static int add_iommu_group(struct device *dev, void *data)
1051{
Thierry Redingb22f6432014-06-27 09:03:12 +02001052 struct iommu_callback_data *cb = data;
1053 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +02001054 int ret;
Alex Williamson14604322011-10-21 15:56:05 -04001055
Alex Williamsond72e31c2012-05-30 14:18:53 -06001056 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +00001057 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001058
1059 WARN_ON(dev->iommu_group);
1060
Joerg Roedel38667f12015-06-29 10:16:08 +02001061 ret = ops->add_device(dev);
1062
1063 /*
1064 * We ignore -ENODEV errors for now, as they just mean that the
1065 * device is not translated by an IOMMU. We still care about
1066 * other errors and fail to initialize when they happen.
1067 */
1068 if (ret == -ENODEV)
1069 ret = 0;
1070
1071 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001072}
1073
Joerg Roedel8da30142015-05-28 18:41:27 +02001074static int remove_iommu_group(struct device *dev, void *data)
1075{
1076 struct iommu_callback_data *cb = data;
1077 const struct iommu_ops *ops = cb->ops;
1078
1079 if (ops->remove_device && dev->iommu_group)
1080 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001081
1082 return 0;
1083}
1084
Alex Williamsond72e31c2012-05-30 14:18:53 -06001085static int iommu_bus_notifier(struct notifier_block *nb,
1086 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001087{
1088 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +02001089 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001090 struct iommu_group *group;
1091 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -04001092
Alex Williamsond72e31c2012-05-30 14:18:53 -06001093 /*
1094 * ADD/DEL call into iommu driver ops if provided, which may
1095 * result in ADD/DEL notifiers to group->notifier
1096 */
1097 if (action == BUS_NOTIFY_ADD_DEVICE) {
zhichang.yuan3ba87752017-04-18 20:51:48 +08001098 if (ops->add_device) {
1099 int ret;
1100
1101 ret = ops->add_device(dev);
1102 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1103 }
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001104 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -06001105 if (ops->remove_device && dev->iommu_group) {
1106 ops->remove_device(dev);
1107 return 0;
1108 }
1109 }
Alex Williamson14604322011-10-21 15:56:05 -04001110
Alex Williamsond72e31c2012-05-30 14:18:53 -06001111 /*
1112 * Remaining BUS_NOTIFYs get filtered and republished to the
1113 * group, if anyone is listening
1114 */
1115 group = iommu_group_get(dev);
1116 if (!group)
1117 return 0;
1118
1119 switch (action) {
1120 case BUS_NOTIFY_BIND_DRIVER:
1121 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1122 break;
1123 case BUS_NOTIFY_BOUND_DRIVER:
1124 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1125 break;
1126 case BUS_NOTIFY_UNBIND_DRIVER:
1127 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1128 break;
1129 case BUS_NOTIFY_UNBOUND_DRIVER:
1130 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1131 break;
1132 }
1133
1134 if (group_action)
1135 blocking_notifier_call_chain(&group->notifier,
1136 group_action, dev);
1137
1138 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001139 return 0;
1140}
1141
Mark Salterfb3e3062014-09-21 13:58:24 -04001142static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001143{
Mark Salterfb3e3062014-09-21 13:58:24 -04001144 int err;
1145 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001146 struct iommu_callback_data cb = {
1147 .ops = ops,
1148 };
1149
Mark Salterfb3e3062014-09-21 13:58:24 -04001150 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1151 if (!nb)
1152 return -ENOMEM;
1153
1154 nb->notifier_call = iommu_bus_notifier;
1155
1156 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001157 if (err)
1158 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001159
1160 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001161 if (err)
1162 goto out_err;
1163
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001164
1165 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001166
1167out_err:
1168 /* Clean up */
1169 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1170 bus_unregister_notifier(bus, nb);
1171
1172out_free:
1173 kfree(nb);
1174
1175 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001176}
1177
Joerg Roedelff217762011-08-26 16:48:26 +02001178/**
1179 * bus_set_iommu - set iommu-callbacks for the bus
1180 * @bus: bus.
1181 * @ops: the callbacks provided by the iommu-driver
1182 *
1183 * This function is called by an iommu driver to set the iommu methods
1184 * used for a particular bus. Drivers for devices on that bus can use
1185 * the iommu-api after these ops are registered.
1186 * This special function is needed because IOMMUs are usually devices on
1187 * the bus itself, so the iommu drivers are not initialized when the bus
1188 * is set up. With this function the iommu-driver can set the iommu-ops
1189 * afterwards.
1190 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001191int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001192{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001193 int err;
1194
Joerg Roedelff217762011-08-26 16:48:26 +02001195 if (bus->iommu_ops != NULL)
1196 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001197
Joerg Roedelff217762011-08-26 16:48:26 +02001198 bus->iommu_ops = ops;
1199
1200 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001201 err = iommu_bus_init(bus, ops);
1202 if (err)
1203 bus->iommu_ops = NULL;
1204
1205 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001206}
Joerg Roedelff217762011-08-26 16:48:26 +02001207EXPORT_SYMBOL_GPL(bus_set_iommu);
1208
Joerg Roedela1b60c12011-09-06 18:46:34 +02001209bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001210{
Joerg Roedel94441c32011-09-06 18:58:54 +02001211 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001212}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001213EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001214
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001215bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1216{
1217 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1218 return false;
1219
1220 return bus->iommu_ops->capable(cap);
1221}
1222EXPORT_SYMBOL_GPL(iommu_capable);
1223
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001224/**
1225 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1226 * @domain: iommu domain
1227 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001228 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001229 *
1230 * This function should be used by IOMMU users which want to be notified
1231 * whenever an IOMMU fault happens.
1232 *
1233 * The fault handler itself should return 0 on success, and an appropriate
1234 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001235 */
1236void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001237 iommu_fault_handler_t handler,
1238 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001239{
1240 BUG_ON(!domain);
1241
1242 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001243 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001244}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001245EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001246
Joerg Roedel53723dc2015-05-28 18:41:29 +02001247static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1248 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001249{
1250 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001251
Joerg Roedel94441c32011-09-06 18:58:54 +02001252 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001253 return NULL;
1254
Joerg Roedel53723dc2015-05-28 18:41:29 +02001255 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001256 if (!domain)
1257 return NULL;
1258
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001259 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001260 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001261 /* Assume all sizes by default; the driver may override this later */
1262 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001263
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001264 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001265}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001266
Joerg Roedel53723dc2015-05-28 18:41:29 +02001267struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1268{
1269 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001270}
1271EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1272
1273void iommu_domain_free(struct iommu_domain *domain)
1274{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001275 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001276}
1277EXPORT_SYMBOL_GPL(iommu_domain_free);
1278
Joerg Roedel426a2732015-05-28 18:41:30 +02001279static int __iommu_attach_device(struct iommu_domain *domain,
1280 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001281{
Shuah Khanb54db772013-08-15 11:59:26 -06001282 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001283 if (unlikely(domain->ops->attach_dev == NULL))
1284 return -ENODEV;
1285
Shuah Khanb54db772013-08-15 11:59:26 -06001286 ret = domain->ops->attach_dev(domain, dev);
1287 if (!ret)
1288 trace_attach_device_to_domain(dev);
1289 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001290}
Joerg Roedel426a2732015-05-28 18:41:30 +02001291
1292int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1293{
1294 struct iommu_group *group;
1295 int ret;
1296
1297 group = iommu_group_get(dev);
1298 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1299 if (group == NULL)
1300 return __iommu_attach_device(domain, dev);
1301
1302 /*
1303 * We have a group - lock it to make sure the device-count doesn't
1304 * change while we are attaching
1305 */
1306 mutex_lock(&group->mutex);
1307 ret = -EINVAL;
1308 if (iommu_group_device_count(group) != 1)
1309 goto out_unlock;
1310
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001311 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001312
1313out_unlock:
1314 mutex_unlock(&group->mutex);
1315 iommu_group_put(group);
1316
1317 return ret;
1318}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001319EXPORT_SYMBOL_GPL(iommu_attach_device);
1320
Joerg Roedel426a2732015-05-28 18:41:30 +02001321static void __iommu_detach_device(struct iommu_domain *domain,
1322 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001323{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001324 if (unlikely(domain->ops->detach_dev == NULL))
1325 return;
1326
1327 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001328 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001329}
Joerg Roedel426a2732015-05-28 18:41:30 +02001330
1331void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1332{
1333 struct iommu_group *group;
1334
1335 group = iommu_group_get(dev);
1336 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1337 if (group == NULL)
1338 return __iommu_detach_device(domain, dev);
1339
1340 mutex_lock(&group->mutex);
1341 if (iommu_group_device_count(group) != 1) {
1342 WARN_ON(1);
1343 goto out_unlock;
1344 }
1345
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001346 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001347
1348out_unlock:
1349 mutex_unlock(&group->mutex);
1350 iommu_group_put(group);
1351}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001352EXPORT_SYMBOL_GPL(iommu_detach_device);
1353
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001354struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1355{
1356 struct iommu_domain *domain;
1357 struct iommu_group *group;
1358
1359 group = iommu_group_get(dev);
1360 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1361 if (group == NULL)
1362 return NULL;
1363
1364 domain = group->domain;
1365
1366 iommu_group_put(group);
1367
1368 return domain;
1369}
1370EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1371
Alex Williamsond72e31c2012-05-30 14:18:53 -06001372/*
1373 * IOMMU groups are really the natrual working unit of the IOMMU, but
1374 * the IOMMU API works on domains and devices. Bridge that gap by
1375 * iterating over the devices in a group. Ideally we'd have a single
1376 * device which represents the requestor ID of the group, but we also
1377 * allow IOMMU drivers to create policy defined minimum sets, where
1378 * the physical hardware may be able to distiguish members, but we
1379 * wish to group them at a higher level (ex. untrusted multi-function
1380 * PCI devices). Thus we attach each device.
1381 */
1382static int iommu_group_do_attach_device(struct device *dev, void *data)
1383{
1384 struct iommu_domain *domain = data;
1385
Joerg Roedel426a2732015-05-28 18:41:30 +02001386 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001387}
1388
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001389static int __iommu_attach_group(struct iommu_domain *domain,
1390 struct iommu_group *group)
1391{
1392 int ret;
1393
1394 if (group->default_domain && group->domain != group->default_domain)
1395 return -EBUSY;
1396
1397 ret = __iommu_group_for_each_dev(group, domain,
1398 iommu_group_do_attach_device);
1399 if (ret == 0)
1400 group->domain = domain;
1401
1402 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001403}
1404
1405int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1406{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001407 int ret;
1408
1409 mutex_lock(&group->mutex);
1410 ret = __iommu_attach_group(domain, group);
1411 mutex_unlock(&group->mutex);
1412
1413 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001414}
1415EXPORT_SYMBOL_GPL(iommu_attach_group);
1416
1417static int iommu_group_do_detach_device(struct device *dev, void *data)
1418{
1419 struct iommu_domain *domain = data;
1420
Joerg Roedel426a2732015-05-28 18:41:30 +02001421 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001422
1423 return 0;
1424}
1425
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001426static void __iommu_detach_group(struct iommu_domain *domain,
1427 struct iommu_group *group)
1428{
1429 int ret;
1430
1431 if (!group->default_domain) {
1432 __iommu_group_for_each_dev(group, domain,
1433 iommu_group_do_detach_device);
1434 group->domain = NULL;
1435 return;
1436 }
1437
1438 if (group->domain == group->default_domain)
1439 return;
1440
1441 /* Detach by re-attaching to the default domain */
1442 ret = __iommu_group_for_each_dev(group, group->default_domain,
1443 iommu_group_do_attach_device);
1444 if (ret != 0)
1445 WARN_ON(1);
1446 else
1447 group->domain = group->default_domain;
1448}
1449
Alex Williamsond72e31c2012-05-30 14:18:53 -06001450void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1451{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001452 mutex_lock(&group->mutex);
1453 __iommu_detach_group(domain, group);
1454 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001455}
1456EXPORT_SYMBOL_GPL(iommu_detach_group);
1457
Varun Sethibb5547a2013-03-29 01:23:58 +05301458phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001459{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001460 if (unlikely(domain->ops->iova_to_phys == NULL))
1461 return 0;
1462
1463 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001464}
1465EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001466
Alex Williamsonbd139692013-06-17 19:57:34 -06001467static size_t iommu_pgsize(struct iommu_domain *domain,
1468 unsigned long addr_merge, size_t size)
1469{
1470 unsigned int pgsize_idx;
1471 size_t pgsize;
1472
1473 /* Max page size that still fits into 'size' */
1474 pgsize_idx = __fls(size);
1475
1476 /* need to consider alignment requirements ? */
1477 if (likely(addr_merge)) {
1478 /* Max page size allowed by address */
1479 unsigned int align_pgsize_idx = __ffs(addr_merge);
1480 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1481 }
1482
1483 /* build a mask of acceptable page sizes */
1484 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1485
1486 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001487 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001488
1489 /* make sure we're still sane */
1490 BUG_ON(!pgsize);
1491
1492 /* pick the biggest page */
1493 pgsize_idx = __fls(pgsize);
1494 pgsize = 1UL << pgsize_idx;
1495
1496 return pgsize;
1497}
1498
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001499int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001500 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001501{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001502 unsigned long orig_iova = iova;
1503 unsigned int min_pagesz;
1504 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001505 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001506 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001507
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001508 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001509 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001510 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001511
Joerg Roedela10315e2015-03-26 13:43:06 +01001512 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1513 return -EINVAL;
1514
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001515 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001516 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001517
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001518 /*
1519 * both the virtual address and the physical one, as well as
1520 * the size of the mapping, must be aligned (at least) to the
1521 * size of the smallest page supported by the hardware
1522 */
1523 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001524 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001525 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001526 return -EINVAL;
1527 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001528
Fabio Estevamabedb042013-08-22 10:25:42 -03001529 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001530
1531 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001532 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001533
Fabio Estevamabedb042013-08-22 10:25:42 -03001534 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001535 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001536
1537 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1538 if (ret)
1539 break;
1540
1541 iova += pgsize;
1542 paddr += pgsize;
1543 size -= pgsize;
1544 }
1545
1546 /* unroll mapping in case something went wrong */
1547 if (ret)
1548 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001549 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001550 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001551
1552 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001553}
1554EXPORT_SYMBOL_GPL(iommu_map);
1555
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001556size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001557{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001558 size_t unmapped_page, unmapped = 0;
1559 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001560 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001561
Joerg Roedel57886512013-01-29 13:41:09 +01001562 if (unlikely(domain->ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001563 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001564 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001565
Joerg Roedela10315e2015-03-26 13:43:06 +01001566 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1567 return -EINVAL;
1568
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001569 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001570 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001571
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001572 /*
1573 * The virtual address, as well as the size of the mapping, must be
1574 * aligned (at least) to the size of the smallest page supported
1575 * by the hardware
1576 */
1577 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001578 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1579 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001580 return -EINVAL;
1581 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001582
Joe Perches6197ca82013-06-23 12:29:04 -07001583 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001584
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001585 /*
1586 * Keep iterating until we either unmap 'size' bytes (or more)
1587 * or we hit an area that isn't mapped.
1588 */
1589 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001590 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001591
Alex Williamsonbd139692013-06-17 19:57:34 -06001592 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001593 if (!unmapped_page)
1594 break;
1595
Joe Perches6197ca82013-06-23 12:29:04 -07001596 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1597 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001598
1599 iova += unmapped_page;
1600 unmapped += unmapped_page;
1601 }
1602
Shuah Khandb8614d2015-01-16 20:53:17 -07001603 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001604 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001605}
1606EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001607
Olav Haugan315786e2014-10-25 09:55:16 -07001608size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1609 struct scatterlist *sg, unsigned int nents, int prot)
1610{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001611 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001612 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001613 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001614 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001615
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001616 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001617 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001618
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001619 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001620
1621 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001622 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001623
1624 /*
1625 * We are mapping on IOMMU page boundaries, so offset within
1626 * the page must be 0. However, the IOMMU may support pages
1627 * smaller than PAGE_SIZE, so s->offset may still represent
1628 * an offset of that boundary within the CPU page.
1629 */
1630 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001631 goto out_err;
1632
1633 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1634 if (ret)
1635 goto out_err;
1636
1637 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001638 }
1639
1640 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001641
1642out_err:
1643 /* undo mappings already done */
1644 iommu_unmap(domain, iova, mapped);
1645
1646 return 0;
1647
Olav Haugan315786e2014-10-25 09:55:16 -07001648}
1649EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001650
1651int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301652 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001653{
1654 if (unlikely(domain->ops->domain_window_enable == NULL))
1655 return -ENODEV;
1656
Varun Sethi80f97f02013-03-29 01:24:00 +05301657 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1658 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001659}
1660EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1661
1662void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1663{
1664 if (unlikely(domain->ops->domain_window_disable == NULL))
1665 return;
1666
1667 return domain->ops->domain_window_disable(domain, wnd_nr);
1668}
1669EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1670
Joerg Roedel207c6e32017-04-26 15:39:28 +02001671/**
1672 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1673 * @domain: the iommu domain where the fault has happened
1674 * @dev: the device where the fault has happened
1675 * @iova: the faulting address
1676 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1677 *
1678 * This function should be called by the low-level IOMMU implementations
1679 * whenever IOMMU faults happen, to allow high-level users, that are
1680 * interested in such events, to know about them.
1681 *
1682 * This event may be useful for several possible use cases:
1683 * - mere logging of the event
1684 * - dynamic TLB/PTE loading
1685 * - if restarting of the faulting device is required
1686 *
1687 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1688 * PTE/TLB loading will one day be supported, implementations will be able
1689 * to tell whether it succeeded or not according to this return value).
1690 *
1691 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1692 * (though fault handlers can also return -ENOSYS, in case they want to
1693 * elicit the default behavior of the IOMMU drivers).
1694 */
1695int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1696 unsigned long iova, int flags)
1697{
1698 int ret = -ENOSYS;
1699
1700 /*
1701 * if upper layers showed interest and installed a fault handler,
1702 * invoke it.
1703 */
1704 if (domain->handler)
1705 ret = domain->handler(domain, dev, iova, flags,
1706 domain->handler_token);
1707
1708 trace_io_page_fault(dev, iova, flags);
1709 return ret;
1710}
1711EXPORT_SYMBOL_GPL(report_iommu_fault);
1712
Alex Williamsond72e31c2012-05-30 14:18:53 -06001713static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001714{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001715 iommu_group_kset = kset_create_and_add("iommu_groups",
1716 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001717 BUG_ON(!iommu_group_kset);
1718
1719 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001720}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001721core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001722
1723int iommu_domain_get_attr(struct iommu_domain *domain,
1724 enum iommu_attr attr, void *data)
1725{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001726 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001727 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001728 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001729 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001730
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001731 switch (attr) {
1732 case DOMAIN_ATTR_GEOMETRY:
1733 geometry = data;
1734 *geometry = domain->geometry;
1735
1736 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001737 case DOMAIN_ATTR_PAGING:
1738 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001739 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001740 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001741 case DOMAIN_ATTR_WINDOWS:
1742 count = data;
1743
1744 if (domain->ops->domain_get_windows != NULL)
1745 *count = domain->ops->domain_get_windows(domain);
1746 else
1747 ret = -ENODEV;
1748
1749 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001750 default:
1751 if (!domain->ops->domain_get_attr)
1752 return -EINVAL;
1753
1754 ret = domain->ops->domain_get_attr(domain, attr, data);
1755 }
1756
1757 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001758}
1759EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1760
1761int iommu_domain_set_attr(struct iommu_domain *domain,
1762 enum iommu_attr attr, void *data)
1763{
Joerg Roedel69356712013-02-04 14:00:01 +01001764 int ret = 0;
1765 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001766
Joerg Roedel69356712013-02-04 14:00:01 +01001767 switch (attr) {
1768 case DOMAIN_ATTR_WINDOWS:
1769 count = data;
1770
1771 if (domain->ops->domain_set_windows != NULL)
1772 ret = domain->ops->domain_set_windows(domain, *count);
1773 else
1774 ret = -ENODEV;
1775
1776 break;
1777 default:
1778 if (domain->ops->domain_set_attr == NULL)
1779 return -EINVAL;
1780
1781 ret = domain->ops->domain_set_attr(domain, attr, data);
1782 }
1783
1784 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001785}
1786EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001787
Eric Augere5b52342017-01-19 20:57:47 +00001788void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001789{
1790 const struct iommu_ops *ops = dev->bus->iommu_ops;
1791
Eric Augere5b52342017-01-19 20:57:47 +00001792 if (ops && ops->get_resv_regions)
1793 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001794}
1795
Eric Augere5b52342017-01-19 20:57:47 +00001796void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001797{
1798 const struct iommu_ops *ops = dev->bus->iommu_ops;
1799
Eric Augere5b52342017-01-19 20:57:47 +00001800 if (ops && ops->put_resv_regions)
1801 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001802}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001803
Eric Auger2b20cbb2017-01-19 20:57:49 +00001804struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001805 size_t length, int prot,
1806 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00001807{
1808 struct iommu_resv_region *region;
1809
1810 region = kzalloc(sizeof(*region), GFP_KERNEL);
1811 if (!region)
1812 return NULL;
1813
1814 INIT_LIST_HEAD(&region->list);
1815 region->start = start;
1816 region->length = length;
1817 region->prot = prot;
1818 region->type = type;
1819 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001820}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001821
1822/* Request that a device is direct mapped by the IOMMU */
1823int iommu_request_dm_for_dev(struct device *dev)
1824{
1825 struct iommu_domain *dm_domain;
1826 struct iommu_group *group;
1827 int ret;
1828
1829 /* Device must already be in a group before calling this function */
1830 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001831 if (IS_ERR(group))
1832 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001833
1834 mutex_lock(&group->mutex);
1835
1836 /* Check if the default domain is already direct mapped */
1837 ret = 0;
1838 if (group->default_domain &&
1839 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1840 goto out;
1841
1842 /* Don't change mappings of existing devices */
1843 ret = -EBUSY;
1844 if (iommu_group_device_count(group) != 1)
1845 goto out;
1846
1847 /* Allocate a direct mapped domain */
1848 ret = -ENOMEM;
1849 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1850 if (!dm_domain)
1851 goto out;
1852
1853 /* Attach the device to the domain */
1854 ret = __iommu_attach_group(dm_domain, group);
1855 if (ret) {
1856 iommu_domain_free(dm_domain);
1857 goto out;
1858 }
1859
1860 /* Make the direct mapped domain the default for this group */
1861 if (group->default_domain)
1862 iommu_domain_free(group->default_domain);
1863 group->default_domain = dm_domain;
1864
1865 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1866
1867 ret = 0;
1868out:
1869 mutex_unlock(&group->mutex);
1870 iommu_group_put(group);
1871
1872 return ret;
1873}
Robin Murphy57f98d22016-09-13 10:54:14 +01001874
Joerg Roedel534766d2017-01-31 16:58:42 +01001875const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001876{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001877 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001878 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001879
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001880 spin_lock(&iommu_device_lock);
1881 list_for_each_entry(iommu, &iommu_device_list, list)
1882 if (iommu->fwnode == fwnode) {
1883 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001884 break;
1885 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001886 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001887 return ops;
1888}
1889
Robin Murphy57f98d22016-09-13 10:54:14 +01001890int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1891 const struct iommu_ops *ops)
1892{
1893 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1894
1895 if (fwspec)
1896 return ops == fwspec->ops ? 0 : -EINVAL;
1897
1898 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1899 if (!fwspec)
1900 return -ENOMEM;
1901
1902 of_node_get(to_of_node(iommu_fwnode));
1903 fwspec->iommu_fwnode = iommu_fwnode;
1904 fwspec->ops = ops;
1905 dev->iommu_fwspec = fwspec;
1906 return 0;
1907}
1908EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1909
1910void iommu_fwspec_free(struct device *dev)
1911{
1912 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1913
1914 if (fwspec) {
1915 fwnode_handle_put(fwspec->iommu_fwnode);
1916 kfree(fwspec);
1917 dev->iommu_fwspec = NULL;
1918 }
1919}
1920EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1921
1922int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1923{
1924 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1925 size_t size;
1926 int i;
1927
1928 if (!fwspec)
1929 return -EINVAL;
1930
1931 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1932 if (size > sizeof(*fwspec)) {
1933 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1934 if (!fwspec)
1935 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08001936
1937 dev->iommu_fwspec = fwspec;
Robin Murphy57f98d22016-09-13 10:54:14 +01001938 }
1939
1940 for (i = 0; i < num_ids; i++)
1941 fwspec->ids[fwspec->num_ids + i] = ids[i];
1942
1943 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01001944 return 0;
1945}
1946EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);