blob: 3de5c0bcb5cc9f32b6fb615ce38f5729ae86d407 [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
Joerg Roedeladd02cfd2017-08-23 15:50:04 +0200530 iommu_flush_tlb_all(domain);
531
Joerg Roedelbeed2822015-05-28 18:41:34 +0200532out:
Eric Augere5b52342017-01-19 20:57:47 +0000533 iommu_put_resv_regions(dev, &mappings);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200534
535 return ret;
536}
537
Alex Williamsond72e31c2012-05-30 14:18:53 -0600538/**
539 * iommu_group_add_device - add a device to an iommu group
540 * @group: the group into which to add the device (reference should be held)
541 * @dev: the device
542 *
543 * This function is called by an iommu driver to add a device into a
544 * group. Adding a device increments the group reference count.
545 */
546int iommu_group_add_device(struct iommu_group *group, struct device *dev)
547{
548 int ret, i = 0;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100549 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600550
551 device = kzalloc(sizeof(*device), GFP_KERNEL);
552 if (!device)
553 return -ENOMEM;
554
555 device->dev = dev;
556
557 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
Robin Murphy797a8b42017-01-16 12:58:07 +0000558 if (ret)
559 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600560
561 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
562rename:
563 if (!device->name) {
Robin Murphy797a8b42017-01-16 12:58:07 +0000564 ret = -ENOMEM;
565 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600566 }
567
568 ret = sysfs_create_link_nowarn(group->devices_kobj,
569 &dev->kobj, device->name);
570 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600571 if (ret == -EEXIST && i >= 0) {
572 /*
573 * Account for the slim chance of collision
574 * and append an instance to the name.
575 */
Robin Murphy797a8b42017-01-16 12:58:07 +0000576 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600577 device->name = kasprintf(GFP_KERNEL, "%s.%d",
578 kobject_name(&dev->kobj), i++);
579 goto rename;
580 }
Robin Murphy797a8b42017-01-16 12:58:07 +0000581 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600582 }
583
584 kobject_get(group->devices_kobj);
585
586 dev->iommu_group = group;
587
Joerg Roedelbeed2822015-05-28 18:41:34 +0200588 iommu_group_create_direct_mappings(group, dev);
589
Alex Williamsond72e31c2012-05-30 14:18:53 -0600590 mutex_lock(&group->mutex);
591 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200592 if (group->domain)
Robin Murphy797a8b42017-01-16 12:58:07 +0000593 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600594 mutex_unlock(&group->mutex);
Robin Murphy797a8b42017-01-16 12:58:07 +0000595 if (ret)
596 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600597
598 /* Notify any listeners about change to group. */
599 blocking_notifier_call_chain(&group->notifier,
600 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600601
602 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200603
604 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
605
Alex Williamsond72e31c2012-05-30 14:18:53 -0600606 return 0;
Robin Murphy797a8b42017-01-16 12:58:07 +0000607
608err_put_group:
609 mutex_lock(&group->mutex);
610 list_del(&device->list);
611 mutex_unlock(&group->mutex);
612 dev->iommu_group = NULL;
613 kobject_put(group->devices_kobj);
614err_free_name:
615 kfree(device->name);
616err_remove_link:
617 sysfs_remove_link(&dev->kobj, "iommu_group");
618err_free_device:
619 kfree(device);
620 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
621 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600622}
623EXPORT_SYMBOL_GPL(iommu_group_add_device);
624
625/**
626 * iommu_group_remove_device - remove a device from it's current group
627 * @dev: device to be removed
628 *
629 * This function is called by an iommu driver to remove the device from
630 * it's current group. This decrements the iommu group reference count.
631 */
632void iommu_group_remove_device(struct device *dev)
633{
634 struct iommu_group *group = dev->iommu_group;
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100635 struct group_device *tmp_device, *device = NULL;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600636
Joerg Roedel269aa802015-05-28 18:41:25 +0200637 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
638
Alex Williamsond72e31c2012-05-30 14:18:53 -0600639 /* Pre-notify listeners that a device is being removed. */
640 blocking_notifier_call_chain(&group->notifier,
641 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
642
643 mutex_lock(&group->mutex);
644 list_for_each_entry(tmp_device, &group->devices, list) {
645 if (tmp_device->dev == dev) {
646 device = tmp_device;
647 list_del(&device->list);
648 break;
649 }
650 }
651 mutex_unlock(&group->mutex);
652
653 if (!device)
654 return;
655
656 sysfs_remove_link(group->devices_kobj, device->name);
657 sysfs_remove_link(&dev->kobj, "iommu_group");
658
Shuah Khan2e757082013-08-15 11:59:25 -0600659 trace_remove_device_from_group(group->id, dev);
660
Alex Williamsond72e31c2012-05-30 14:18:53 -0600661 kfree(device->name);
662 kfree(device);
663 dev->iommu_group = NULL;
664 kobject_put(group->devices_kobj);
665}
666EXPORT_SYMBOL_GPL(iommu_group_remove_device);
667
Joerg Roedel426a2732015-05-28 18:41:30 +0200668static int iommu_group_device_count(struct iommu_group *group)
669{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100670 struct group_device *entry;
Joerg Roedel426a2732015-05-28 18:41:30 +0200671 int ret = 0;
672
673 list_for_each_entry(entry, &group->devices, list)
674 ret++;
675
676 return ret;
677}
678
Alex Williamsond72e31c2012-05-30 14:18:53 -0600679/**
680 * iommu_group_for_each_dev - iterate over each device in the group
681 * @group: the group
682 * @data: caller opaque data to be passed to callback function
683 * @fn: caller supplied callback function
684 *
685 * This function is called by group users to iterate over group devices.
686 * Callers should hold a reference count to the group during callback.
687 * The group->mutex is held across callbacks, which will block calls to
688 * iommu_group_add/remove_device.
689 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200690static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
691 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600692{
Joerg Roedelc09e22d2017-02-01 12:19:46 +0100693 struct group_device *device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600694 int ret = 0;
695
Alex Williamsond72e31c2012-05-30 14:18:53 -0600696 list_for_each_entry(device, &group->devices, list) {
697 ret = fn(device->dev, data);
698 if (ret)
699 break;
700 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200701 return ret;
702}
703
704
705int iommu_group_for_each_dev(struct iommu_group *group, void *data,
706 int (*fn)(struct device *, void *))
707{
708 int ret;
709
710 mutex_lock(&group->mutex);
711 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600712 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200713
Alex Williamsond72e31c2012-05-30 14:18:53 -0600714 return ret;
715}
716EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
717
718/**
719 * iommu_group_get - Return the group for a device and increment reference
720 * @dev: get the group that this device belongs to
721 *
722 * This function is called by iommu drivers and users to get the group
723 * for the specified device. If found, the group is returned and the group
724 * reference in incremented, else NULL.
725 */
726struct iommu_group *iommu_group_get(struct device *dev)
727{
728 struct iommu_group *group = dev->iommu_group;
729
730 if (group)
731 kobject_get(group->devices_kobj);
732
733 return group;
734}
735EXPORT_SYMBOL_GPL(iommu_group_get);
736
737/**
Robin Murphy13f59a72016-11-11 17:59:21 +0000738 * iommu_group_ref_get - Increment reference on a group
739 * @group: the group to use, must not be NULL
740 *
741 * This function is called by iommu drivers to take additional references on an
742 * existing group. Returns the given group for convenience.
743 */
744struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
745{
746 kobject_get(group->devices_kobj);
747 return group;
748}
749
750/**
Alex Williamsond72e31c2012-05-30 14:18:53 -0600751 * iommu_group_put - Decrement group reference
752 * @group: the group to use
753 *
754 * This function is called by iommu drivers and users to release the
755 * iommu group. Once the reference count is zero, the group is released.
756 */
757void iommu_group_put(struct iommu_group *group)
758{
759 if (group)
760 kobject_put(group->devices_kobj);
761}
762EXPORT_SYMBOL_GPL(iommu_group_put);
763
764/**
765 * iommu_group_register_notifier - Register a notifier for group changes
766 * @group: the group to watch
767 * @nb: notifier block to signal
768 *
769 * This function allows iommu group users to track changes in a group.
770 * See include/linux/iommu.h for actions sent via this notifier. Caller
771 * should hold a reference to the group throughout notifier registration.
772 */
773int iommu_group_register_notifier(struct iommu_group *group,
774 struct notifier_block *nb)
775{
776 return blocking_notifier_chain_register(&group->notifier, nb);
777}
778EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
779
780/**
781 * iommu_group_unregister_notifier - Unregister a notifier
782 * @group: the group to watch
783 * @nb: notifier block to signal
784 *
785 * Unregister a previously registered group notifier block.
786 */
787int iommu_group_unregister_notifier(struct iommu_group *group,
788 struct notifier_block *nb)
789{
790 return blocking_notifier_chain_unregister(&group->notifier, nb);
791}
792EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
793
794/**
795 * iommu_group_id - Return ID for a group
796 * @group: the group to ID
797 *
798 * Return the unique ID for the group matching the sysfs group number.
799 */
800int iommu_group_id(struct iommu_group *group)
801{
802 return group->id;
803}
804EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400805
Alex Williamsonf096c062014-09-19 10:03:06 -0600806static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
807 unsigned long *devfns);
808
Alex Williamson104a1c12014-07-03 09:51:18 -0600809/*
810 * To consider a PCI device isolated, we require ACS to support Source
811 * Validation, Request Redirection, Completer Redirection, and Upstream
812 * Forwarding. This effectively means that devices cannot spoof their
813 * requester ID, requests and completions cannot be redirected, and all
814 * transactions are forwarded upstream, even as it passes through a
815 * bridge where the target device is downstream.
816 */
817#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
818
Alex Williamsonf096c062014-09-19 10:03:06 -0600819/*
820 * For multifunction devices which are not isolated from each other, find
821 * all the other non-isolated functions and look for existing groups. For
822 * each function, we also need to look for aliases to or from other devices
823 * that may already have a group.
824 */
825static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
826 unsigned long *devfns)
827{
828 struct pci_dev *tmp = NULL;
829 struct iommu_group *group;
830
831 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
832 return NULL;
833
834 for_each_pci_dev(tmp) {
835 if (tmp == pdev || tmp->bus != pdev->bus ||
836 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
837 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
838 continue;
839
840 group = get_pci_alias_group(tmp, devfns);
841 if (group) {
842 pci_dev_put(tmp);
843 return group;
844 }
845 }
846
847 return NULL;
848}
849
850/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100851 * Look for aliases to or from the given device for existing groups. DMA
852 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600853 * space is quite small (especially since we're really only looking at pcie
854 * device, and therefore only expect multiple slots on the root complex or
855 * downstream switch ports). It's conceivable though that a pair of
856 * multifunction devices could have aliases between them that would cause a
857 * loop. To prevent this, we use a bitmap to track where we've been.
858 */
859static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
860 unsigned long *devfns)
861{
862 struct pci_dev *tmp = NULL;
863 struct iommu_group *group;
864
865 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
866 return NULL;
867
868 group = iommu_group_get(&pdev->dev);
869 if (group)
870 return group;
871
872 for_each_pci_dev(tmp) {
873 if (tmp == pdev || tmp->bus != pdev->bus)
874 continue;
875
876 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100877 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600878 group = get_pci_alias_group(tmp, devfns);
879 if (group) {
880 pci_dev_put(tmp);
881 return group;
882 }
883
884 group = get_pci_function_alias_group(tmp, devfns);
885 if (group) {
886 pci_dev_put(tmp);
887 return group;
888 }
889 }
890 }
891
892 return NULL;
893}
894
Alex Williamson104a1c12014-07-03 09:51:18 -0600895struct group_for_pci_data {
896 struct pci_dev *pdev;
897 struct iommu_group *group;
898};
899
900/*
901 * DMA alias iterator callback, return the last seen device. Stop and return
902 * the IOMMU group if we find one along the way.
903 */
904static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
905{
906 struct group_for_pci_data *data = opaque;
907
908 data->pdev = pdev;
909 data->group = iommu_group_get(&pdev->dev);
910
911 return data->group != NULL;
912}
913
914/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200915 * Generic device_group call-back function. It just allocates one
916 * iommu-group per device.
917 */
918struct iommu_group *generic_device_group(struct device *dev)
919{
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200920 return iommu_group_alloc();
Joerg Roedel6eab5562015-10-21 23:51:38 +0200921}
922
923/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600924 * Use standard PCI bus topology, isolation features, and DMA alias quirks
925 * to find or create an IOMMU group for a device.
926 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200927struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600928{
Joerg Roedel5e622922015-10-21 23:51:37 +0200929 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600930 struct group_for_pci_data data;
931 struct pci_bus *bus;
932 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600933 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600934
Joerg Roedel5e622922015-10-21 23:51:37 +0200935 if (WARN_ON(!dev_is_pci(dev)))
936 return ERR_PTR(-EINVAL);
937
Alex Williamson104a1c12014-07-03 09:51:18 -0600938 /*
939 * Find the upstream DMA alias for the device. A device must not
940 * be aliased due to topology in order to have its own IOMMU group.
941 * If we find an alias along the way that already belongs to a
942 * group, use it.
943 */
944 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
945 return data.group;
946
947 pdev = data.pdev;
948
949 /*
950 * Continue upstream from the point of minimum IOMMU granularity
951 * due to aliases to the point where devices are protected from
952 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
953 * group, use it.
954 */
955 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
956 if (!bus->self)
957 continue;
958
959 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
960 break;
961
962 pdev = bus->self;
963
964 group = iommu_group_get(&pdev->dev);
965 if (group)
966 return group;
967 }
968
969 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600970 * Look for existing groups on device aliases. If we alias another
971 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600972 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600973 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
974 if (group)
975 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600976
977 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600978 * Look for existing groups on non-isolated functions on the same
979 * slot and aliases of those funcions, if any. No need to clear
980 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600981 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600982 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
983 if (group)
984 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600985
986 /* No shared group found, allocate new */
Joerg Roedel7f7a2302017-06-28 12:45:31 +0200987 return iommu_group_alloc();
Alex Williamson104a1c12014-07-03 09:51:18 -0600988}
989
990/**
991 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
992 * @dev: target device
993 *
994 * This function is intended to be called by IOMMU drivers and extended to
995 * support common, bus-defined algorithms when determining or creating the
996 * IOMMU group for a device. On success, the caller will hold a reference
997 * to the returned IOMMU group, which will already include the provided
998 * device. The reference should be released with iommu_group_put().
999 */
1000struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1001{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +02001002 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001003 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -06001004 int ret;
1005
1006 group = iommu_group_get(dev);
1007 if (group)
1008 return group;
1009
Robin Murphy05f803002017-07-21 13:12:38 +01001010 if (!ops)
1011 return ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +02001012
Robin Murphy05f803002017-07-21 13:12:38 +01001013 group = ops->device_group(dev);
Joerg Roedel72dcac62017-06-28 12:52:48 +02001014 if (WARN_ON_ONCE(group == NULL))
1015 return ERR_PTR(-EINVAL);
1016
Alex Williamson104a1c12014-07-03 09:51:18 -06001017 if (IS_ERR(group))
1018 return group;
1019
Joerg Roedel12282362015-10-21 23:51:43 +02001020 /*
1021 * Try to allocate a default domain - needs support from the
1022 * IOMMU driver.
1023 */
1024 if (!group->default_domain) {
Will Deaconfccb4e32017-01-05 18:38:26 +00001025 struct iommu_domain *dom;
1026
1027 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1028 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1029 dev_warn(dev,
1030 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1031 iommu_def_domain_type);
1032 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1033 }
1034
1035 group->default_domain = dom;
Joerg Roedeleebb8032016-04-04 15:47:48 +02001036 if (!group->domain)
Will Deaconfccb4e32017-01-05 18:38:26 +00001037 group->domain = dom;
Joerg Roedel12282362015-10-21 23:51:43 +02001038 }
1039
Alex Williamson104a1c12014-07-03 09:51:18 -06001040 ret = iommu_group_add_device(group, dev);
1041 if (ret) {
1042 iommu_group_put(group);
1043 return ERR_PTR(ret);
1044 }
1045
1046 return group;
1047}
1048
Joerg Roedel6827ca82015-05-28 18:41:35 +02001049struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1050{
1051 return group->default_domain;
1052}
1053
Alex Williamson14604322011-10-21 15:56:05 -04001054static int add_iommu_group(struct device *dev, void *data)
1055{
Thierry Redingb22f6432014-06-27 09:03:12 +02001056 struct iommu_callback_data *cb = data;
1057 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +02001058 int ret;
Alex Williamson14604322011-10-21 15:56:05 -04001059
Alex Williamsond72e31c2012-05-30 14:18:53 -06001060 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +00001061 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001062
1063 WARN_ON(dev->iommu_group);
1064
Joerg Roedel38667f12015-06-29 10:16:08 +02001065 ret = ops->add_device(dev);
1066
1067 /*
1068 * We ignore -ENODEV errors for now, as they just mean that the
1069 * device is not translated by an IOMMU. We still care about
1070 * other errors and fail to initialize when they happen.
1071 */
1072 if (ret == -ENODEV)
1073 ret = 0;
1074
1075 return ret;
Alex Williamson14604322011-10-21 15:56:05 -04001076}
1077
Joerg Roedel8da30142015-05-28 18:41:27 +02001078static int remove_iommu_group(struct device *dev, void *data)
1079{
1080 struct iommu_callback_data *cb = data;
1081 const struct iommu_ops *ops = cb->ops;
1082
1083 if (ops->remove_device && dev->iommu_group)
1084 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -04001085
1086 return 0;
1087}
1088
Alex Williamsond72e31c2012-05-30 14:18:53 -06001089static int iommu_bus_notifier(struct notifier_block *nb,
1090 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -04001091{
1092 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +02001093 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001094 struct iommu_group *group;
1095 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -04001096
Alex Williamsond72e31c2012-05-30 14:18:53 -06001097 /*
1098 * ADD/DEL call into iommu driver ops if provided, which may
1099 * result in ADD/DEL notifiers to group->notifier
1100 */
1101 if (action == BUS_NOTIFY_ADD_DEVICE) {
zhichang.yuan3ba87752017-04-18 20:51:48 +08001102 if (ops->add_device) {
1103 int ret;
1104
1105 ret = ops->add_device(dev);
1106 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1107 }
Joerg Roedel843cb6d2015-05-28 18:41:28 +02001108 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -06001109 if (ops->remove_device && dev->iommu_group) {
1110 ops->remove_device(dev);
1111 return 0;
1112 }
1113 }
Alex Williamson14604322011-10-21 15:56:05 -04001114
Alex Williamsond72e31c2012-05-30 14:18:53 -06001115 /*
1116 * Remaining BUS_NOTIFYs get filtered and republished to the
1117 * group, if anyone is listening
1118 */
1119 group = iommu_group_get(dev);
1120 if (!group)
1121 return 0;
1122
1123 switch (action) {
1124 case BUS_NOTIFY_BIND_DRIVER:
1125 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1126 break;
1127 case BUS_NOTIFY_BOUND_DRIVER:
1128 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1129 break;
1130 case BUS_NOTIFY_UNBIND_DRIVER:
1131 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1132 break;
1133 case BUS_NOTIFY_UNBOUND_DRIVER:
1134 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1135 break;
1136 }
1137
1138 if (group_action)
1139 blocking_notifier_call_chain(&group->notifier,
1140 group_action, dev);
1141
1142 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -04001143 return 0;
1144}
1145
Mark Salterfb3e3062014-09-21 13:58:24 -04001146static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001147{
Mark Salterfb3e3062014-09-21 13:58:24 -04001148 int err;
1149 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +02001150 struct iommu_callback_data cb = {
1151 .ops = ops,
1152 };
1153
Mark Salterfb3e3062014-09-21 13:58:24 -04001154 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1155 if (!nb)
1156 return -ENOMEM;
1157
1158 nb->notifier_call = iommu_bus_notifier;
1159
1160 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +02001161 if (err)
1162 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001163
1164 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +02001165 if (err)
1166 goto out_err;
1167
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001168
1169 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +02001170
1171out_err:
1172 /* Clean up */
1173 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1174 bus_unregister_notifier(bus, nb);
1175
1176out_free:
1177 kfree(nb);
1178
1179 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001180}
1181
Joerg Roedelff217762011-08-26 16:48:26 +02001182/**
1183 * bus_set_iommu - set iommu-callbacks for the bus
1184 * @bus: bus.
1185 * @ops: the callbacks provided by the iommu-driver
1186 *
1187 * This function is called by an iommu driver to set the iommu methods
1188 * used for a particular bus. Drivers for devices on that bus can use
1189 * the iommu-api after these ops are registered.
1190 * This special function is needed because IOMMUs are usually devices on
1191 * the bus itself, so the iommu drivers are not initialized when the bus
1192 * is set up. With this function the iommu-driver can set the iommu-ops
1193 * afterwards.
1194 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001195int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001196{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001197 int err;
1198
Joerg Roedelff217762011-08-26 16:48:26 +02001199 if (bus->iommu_ops != NULL)
1200 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001201
Joerg Roedelff217762011-08-26 16:48:26 +02001202 bus->iommu_ops = ops;
1203
1204 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001205 err = iommu_bus_init(bus, ops);
1206 if (err)
1207 bus->iommu_ops = NULL;
1208
1209 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001210}
Joerg Roedelff217762011-08-26 16:48:26 +02001211EXPORT_SYMBOL_GPL(bus_set_iommu);
1212
Joerg Roedela1b60c12011-09-06 18:46:34 +02001213bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001214{
Joerg Roedel94441c32011-09-06 18:58:54 +02001215 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001216}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001217EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001218
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001219bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1220{
1221 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1222 return false;
1223
1224 return bus->iommu_ops->capable(cap);
1225}
1226EXPORT_SYMBOL_GPL(iommu_capable);
1227
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001228/**
1229 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1230 * @domain: iommu domain
1231 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001232 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001233 *
1234 * This function should be used by IOMMU users which want to be notified
1235 * whenever an IOMMU fault happens.
1236 *
1237 * The fault handler itself should return 0 on success, and an appropriate
1238 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001239 */
1240void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001241 iommu_fault_handler_t handler,
1242 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001243{
1244 BUG_ON(!domain);
1245
1246 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001247 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001248}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001249EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001250
Joerg Roedel53723dc2015-05-28 18:41:29 +02001251static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1252 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001253{
1254 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001255
Joerg Roedel94441c32011-09-06 18:58:54 +02001256 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001257 return NULL;
1258
Joerg Roedel53723dc2015-05-28 18:41:29 +02001259 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001260 if (!domain)
1261 return NULL;
1262
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001263 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001264 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001265 /* Assume all sizes by default; the driver may override this later */
1266 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001267
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001268 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001269}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001270
Joerg Roedel53723dc2015-05-28 18:41:29 +02001271struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1272{
1273 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001274}
1275EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1276
1277void iommu_domain_free(struct iommu_domain *domain)
1278{
Joerg Roedel89be34a2015-03-26 13:43:19 +01001279 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001280}
1281EXPORT_SYMBOL_GPL(iommu_domain_free);
1282
Joerg Roedel426a2732015-05-28 18:41:30 +02001283static int __iommu_attach_device(struct iommu_domain *domain,
1284 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001285{
Shuah Khanb54db772013-08-15 11:59:26 -06001286 int ret;
Baoquan Hee01d1912017-08-09 16:33:40 +08001287 if ((domain->ops->is_attach_deferred != NULL) &&
1288 domain->ops->is_attach_deferred(domain, dev))
1289 return 0;
1290
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001291 if (unlikely(domain->ops->attach_dev == NULL))
1292 return -ENODEV;
1293
Shuah Khanb54db772013-08-15 11:59:26 -06001294 ret = domain->ops->attach_dev(domain, dev);
1295 if (!ret)
1296 trace_attach_device_to_domain(dev);
1297 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001298}
Joerg Roedel426a2732015-05-28 18:41:30 +02001299
1300int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1301{
1302 struct iommu_group *group;
1303 int ret;
1304
1305 group = iommu_group_get(dev);
Joerg Roedel426a2732015-05-28 18:41:30 +02001306 /*
Robin Murphy05f803002017-07-21 13:12:38 +01001307 * Lock the group to make sure the device-count doesn't
Joerg Roedel426a2732015-05-28 18:41:30 +02001308 * change while we are attaching
1309 */
1310 mutex_lock(&group->mutex);
1311 ret = -EINVAL;
1312 if (iommu_group_device_count(group) != 1)
1313 goto out_unlock;
1314
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001315 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001316
1317out_unlock:
1318 mutex_unlock(&group->mutex);
1319 iommu_group_put(group);
1320
1321 return ret;
1322}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001323EXPORT_SYMBOL_GPL(iommu_attach_device);
1324
Joerg Roedel426a2732015-05-28 18:41:30 +02001325static void __iommu_detach_device(struct iommu_domain *domain,
1326 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001327{
Baoquan Hee01d1912017-08-09 16:33:40 +08001328 if ((domain->ops->is_attach_deferred != NULL) &&
1329 domain->ops->is_attach_deferred(domain, dev))
1330 return;
1331
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001332 if (unlikely(domain->ops->detach_dev == NULL))
1333 return;
1334
1335 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001336 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001337}
Joerg Roedel426a2732015-05-28 18:41:30 +02001338
1339void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1340{
1341 struct iommu_group *group;
1342
1343 group = iommu_group_get(dev);
Joerg Roedel426a2732015-05-28 18:41:30 +02001344
1345 mutex_lock(&group->mutex);
1346 if (iommu_group_device_count(group) != 1) {
1347 WARN_ON(1);
1348 goto out_unlock;
1349 }
1350
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001351 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001352
1353out_unlock:
1354 mutex_unlock(&group->mutex);
1355 iommu_group_put(group);
1356}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001357EXPORT_SYMBOL_GPL(iommu_detach_device);
1358
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001359struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1360{
1361 struct iommu_domain *domain;
1362 struct iommu_group *group;
1363
1364 group = iommu_group_get(dev);
Robin Murphy1464d0b2017-08-17 11:40:08 +01001365 if (!group)
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001366 return NULL;
1367
1368 domain = group->domain;
1369
1370 iommu_group_put(group);
1371
1372 return domain;
1373}
1374EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1375
Alex Williamsond72e31c2012-05-30 14:18:53 -06001376/*
1377 * IOMMU groups are really the natrual working unit of the IOMMU, but
1378 * the IOMMU API works on domains and devices. Bridge that gap by
1379 * iterating over the devices in a group. Ideally we'd have a single
1380 * device which represents the requestor ID of the group, but we also
1381 * allow IOMMU drivers to create policy defined minimum sets, where
1382 * the physical hardware may be able to distiguish members, but we
1383 * wish to group them at a higher level (ex. untrusted multi-function
1384 * PCI devices). Thus we attach each device.
1385 */
1386static int iommu_group_do_attach_device(struct device *dev, void *data)
1387{
1388 struct iommu_domain *domain = data;
1389
Joerg Roedel426a2732015-05-28 18:41:30 +02001390 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001391}
1392
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001393static int __iommu_attach_group(struct iommu_domain *domain,
1394 struct iommu_group *group)
1395{
1396 int ret;
1397
1398 if (group->default_domain && group->domain != group->default_domain)
1399 return -EBUSY;
1400
1401 ret = __iommu_group_for_each_dev(group, domain,
1402 iommu_group_do_attach_device);
1403 if (ret == 0)
1404 group->domain = domain;
1405
1406 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001407}
1408
1409int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1410{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001411 int ret;
1412
1413 mutex_lock(&group->mutex);
1414 ret = __iommu_attach_group(domain, group);
1415 mutex_unlock(&group->mutex);
1416
1417 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001418}
1419EXPORT_SYMBOL_GPL(iommu_attach_group);
1420
1421static int iommu_group_do_detach_device(struct device *dev, void *data)
1422{
1423 struct iommu_domain *domain = data;
1424
Joerg Roedel426a2732015-05-28 18:41:30 +02001425 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001426
1427 return 0;
1428}
1429
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001430static void __iommu_detach_group(struct iommu_domain *domain,
1431 struct iommu_group *group)
1432{
1433 int ret;
1434
1435 if (!group->default_domain) {
1436 __iommu_group_for_each_dev(group, domain,
1437 iommu_group_do_detach_device);
1438 group->domain = NULL;
1439 return;
1440 }
1441
1442 if (group->domain == group->default_domain)
1443 return;
1444
1445 /* Detach by re-attaching to the default domain */
1446 ret = __iommu_group_for_each_dev(group, group->default_domain,
1447 iommu_group_do_attach_device);
1448 if (ret != 0)
1449 WARN_ON(1);
1450 else
1451 group->domain = group->default_domain;
1452}
1453
Alex Williamsond72e31c2012-05-30 14:18:53 -06001454void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1455{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001456 mutex_lock(&group->mutex);
1457 __iommu_detach_group(domain, group);
1458 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001459}
1460EXPORT_SYMBOL_GPL(iommu_detach_group);
1461
Varun Sethibb5547a2013-03-29 01:23:58 +05301462phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001463{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001464 if (unlikely(domain->ops->iova_to_phys == NULL))
1465 return 0;
1466
1467 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001468}
1469EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001470
Alex Williamsonbd139692013-06-17 19:57:34 -06001471static size_t iommu_pgsize(struct iommu_domain *domain,
1472 unsigned long addr_merge, size_t size)
1473{
1474 unsigned int pgsize_idx;
1475 size_t pgsize;
1476
1477 /* Max page size that still fits into 'size' */
1478 pgsize_idx = __fls(size);
1479
1480 /* need to consider alignment requirements ? */
1481 if (likely(addr_merge)) {
1482 /* Max page size allowed by address */
1483 unsigned int align_pgsize_idx = __ffs(addr_merge);
1484 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1485 }
1486
1487 /* build a mask of acceptable page sizes */
1488 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1489
1490 /* throw away page sizes not supported by the hardware */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001491 pgsize &= domain->pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001492
1493 /* make sure we're still sane */
1494 BUG_ON(!pgsize);
1495
1496 /* pick the biggest page */
1497 pgsize_idx = __fls(pgsize);
1498 pgsize = 1UL << pgsize_idx;
1499
1500 return pgsize;
1501}
1502
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001503int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001504 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001505{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001506 unsigned long orig_iova = iova;
1507 unsigned int min_pagesz;
1508 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001509 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001510 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001511
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001512 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001513 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001514 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001515
Joerg Roedela10315e2015-03-26 13:43:06 +01001516 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1517 return -EINVAL;
1518
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001519 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001520 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001521
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001522 /*
1523 * both the virtual address and the physical one, as well as
1524 * the size of the mapping, must be aligned (at least) to the
1525 * size of the smallest page supported by the hardware
1526 */
1527 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001528 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001529 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001530 return -EINVAL;
1531 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001532
Fabio Estevamabedb042013-08-22 10:25:42 -03001533 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001534
1535 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001536 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001537
Fabio Estevamabedb042013-08-22 10:25:42 -03001538 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001539 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001540
1541 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1542 if (ret)
1543 break;
1544
1545 iova += pgsize;
1546 paddr += pgsize;
1547 size -= pgsize;
1548 }
1549
1550 /* unroll mapping in case something went wrong */
1551 if (ret)
1552 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001553 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001554 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001555
1556 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001557}
1558EXPORT_SYMBOL_GPL(iommu_map);
1559
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001560static size_t __iommu_unmap(struct iommu_domain *domain,
1561 unsigned long iova, size_t size,
1562 bool sync)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001563{
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001564 const struct iommu_ops *ops = domain->ops;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001565 size_t unmapped_page, unmapped = 0;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001566 unsigned long orig_iova = iova;
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001567 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001568
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001569 if (unlikely(ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001570 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001571 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001572
Joerg Roedela10315e2015-03-26 13:43:06 +01001573 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1574 return -EINVAL;
1575
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001576 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001577 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001578
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001579 /*
1580 * The virtual address, as well as the size of the mapping, must be
1581 * aligned (at least) to the size of the smallest page supported
1582 * by the hardware
1583 */
1584 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001585 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1586 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001587 return -EINVAL;
1588 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001589
Joe Perches6197ca82013-06-23 12:29:04 -07001590 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001591
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001592 /*
1593 * Keep iterating until we either unmap 'size' bytes (or more)
1594 * or we hit an area that isn't mapped.
1595 */
1596 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001597 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001598
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001599 unmapped_page = ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001600 if (!unmapped_page)
1601 break;
1602
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001603 if (sync && ops->iotlb_range_add)
1604 ops->iotlb_range_add(domain, iova, pgsize);
1605
Joe Perches6197ca82013-06-23 12:29:04 -07001606 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1607 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001608
1609 iova += unmapped_page;
1610 unmapped += unmapped_page;
1611 }
1612
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001613 if (sync && ops->iotlb_sync)
1614 ops->iotlb_sync(domain);
1615
Shuah Khandb8614d2015-01-16 20:53:17 -07001616 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001617 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001618}
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001619
1620size_t iommu_unmap(struct iommu_domain *domain,
1621 unsigned long iova, size_t size)
1622{
1623 return __iommu_unmap(domain, iova, size, true);
1624}
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001625EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001626
Joerg Roedeladd02cfd2017-08-23 15:50:04 +02001627size_t iommu_unmap_fast(struct iommu_domain *domain,
1628 unsigned long iova, size_t size)
1629{
1630 return __iommu_unmap(domain, iova, size, false);
1631}
1632EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1633
Olav Haugan315786e2014-10-25 09:55:16 -07001634size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1635 struct scatterlist *sg, unsigned int nents, int prot)
1636{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001637 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001638 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001639 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001640 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001641
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001642 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001643 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001644
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001645 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001646
1647 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001648 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001649
1650 /*
1651 * We are mapping on IOMMU page boundaries, so offset within
1652 * the page must be 0. However, the IOMMU may support pages
1653 * smaller than PAGE_SIZE, so s->offset may still represent
1654 * an offset of that boundary within the CPU page.
1655 */
1656 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001657 goto out_err;
1658
1659 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1660 if (ret)
1661 goto out_err;
1662
1663 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001664 }
1665
1666 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001667
1668out_err:
1669 /* undo mappings already done */
1670 iommu_unmap(domain, iova, mapped);
1671
1672 return 0;
1673
Olav Haugan315786e2014-10-25 09:55:16 -07001674}
1675EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001676
1677int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301678 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001679{
1680 if (unlikely(domain->ops->domain_window_enable == NULL))
1681 return -ENODEV;
1682
Varun Sethi80f97f02013-03-29 01:24:00 +05301683 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1684 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001685}
1686EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1687
1688void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1689{
1690 if (unlikely(domain->ops->domain_window_disable == NULL))
1691 return;
1692
1693 return domain->ops->domain_window_disable(domain, wnd_nr);
1694}
1695EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1696
Joerg Roedel207c6e32017-04-26 15:39:28 +02001697/**
1698 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1699 * @domain: the iommu domain where the fault has happened
1700 * @dev: the device where the fault has happened
1701 * @iova: the faulting address
1702 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1703 *
1704 * This function should be called by the low-level IOMMU implementations
1705 * whenever IOMMU faults happen, to allow high-level users, that are
1706 * interested in such events, to know about them.
1707 *
1708 * This event may be useful for several possible use cases:
1709 * - mere logging of the event
1710 * - dynamic TLB/PTE loading
1711 * - if restarting of the faulting device is required
1712 *
1713 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1714 * PTE/TLB loading will one day be supported, implementations will be able
1715 * to tell whether it succeeded or not according to this return value).
1716 *
1717 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1718 * (though fault handlers can also return -ENOSYS, in case they want to
1719 * elicit the default behavior of the IOMMU drivers).
1720 */
1721int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1722 unsigned long iova, int flags)
1723{
1724 int ret = -ENOSYS;
1725
1726 /*
1727 * if upper layers showed interest and installed a fault handler,
1728 * invoke it.
1729 */
1730 if (domain->handler)
1731 ret = domain->handler(domain, dev, iova, flags,
1732 domain->handler_token);
1733
1734 trace_io_page_fault(dev, iova, flags);
1735 return ret;
1736}
1737EXPORT_SYMBOL_GPL(report_iommu_fault);
1738
Alex Williamsond72e31c2012-05-30 14:18:53 -06001739static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001740{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001741 iommu_group_kset = kset_create_and_add("iommu_groups",
1742 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001743 BUG_ON(!iommu_group_kset);
1744
1745 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001746}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001747core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001748
1749int iommu_domain_get_attr(struct iommu_domain *domain,
1750 enum iommu_attr attr, void *data)
1751{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001752 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001753 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001754 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001755 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001756
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001757 switch (attr) {
1758 case DOMAIN_ATTR_GEOMETRY:
1759 geometry = data;
1760 *geometry = domain->geometry;
1761
1762 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001763 case DOMAIN_ATTR_PAGING:
1764 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001765 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001766 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001767 case DOMAIN_ATTR_WINDOWS:
1768 count = data;
1769
1770 if (domain->ops->domain_get_windows != NULL)
1771 *count = domain->ops->domain_get_windows(domain);
1772 else
1773 ret = -ENODEV;
1774
1775 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001776 default:
1777 if (!domain->ops->domain_get_attr)
1778 return -EINVAL;
1779
1780 ret = domain->ops->domain_get_attr(domain, attr, data);
1781 }
1782
1783 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001784}
1785EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1786
1787int iommu_domain_set_attr(struct iommu_domain *domain,
1788 enum iommu_attr attr, void *data)
1789{
Joerg Roedel69356712013-02-04 14:00:01 +01001790 int ret = 0;
1791 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001792
Joerg Roedel69356712013-02-04 14:00:01 +01001793 switch (attr) {
1794 case DOMAIN_ATTR_WINDOWS:
1795 count = data;
1796
1797 if (domain->ops->domain_set_windows != NULL)
1798 ret = domain->ops->domain_set_windows(domain, *count);
1799 else
1800 ret = -ENODEV;
1801
1802 break;
1803 default:
1804 if (domain->ops->domain_set_attr == NULL)
1805 return -EINVAL;
1806
1807 ret = domain->ops->domain_set_attr(domain, attr, data);
1808 }
1809
1810 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001811}
1812EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001813
Eric Augere5b52342017-01-19 20:57:47 +00001814void iommu_get_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001815{
1816 const struct iommu_ops *ops = dev->bus->iommu_ops;
1817
Eric Augere5b52342017-01-19 20:57:47 +00001818 if (ops && ops->get_resv_regions)
1819 ops->get_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001820}
1821
Eric Augere5b52342017-01-19 20:57:47 +00001822void iommu_put_resv_regions(struct device *dev, struct list_head *list)
Joerg Roedela1015c22015-05-28 18:41:33 +02001823{
1824 const struct iommu_ops *ops = dev->bus->iommu_ops;
1825
Eric Augere5b52342017-01-19 20:57:47 +00001826 if (ops && ops->put_resv_regions)
1827 ops->put_resv_regions(dev, list);
Joerg Roedela1015c22015-05-28 18:41:33 +02001828}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001829
Eric Auger2b20cbb2017-01-19 20:57:49 +00001830struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001831 size_t length, int prot,
1832 enum iommu_resv_type type)
Eric Auger2b20cbb2017-01-19 20:57:49 +00001833{
1834 struct iommu_resv_region *region;
1835
1836 region = kzalloc(sizeof(*region), GFP_KERNEL);
1837 if (!region)
1838 return NULL;
1839
1840 INIT_LIST_HEAD(&region->list);
1841 region->start = start;
1842 region->length = length;
1843 region->prot = prot;
1844 region->type = type;
1845 return region;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001846}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001847
1848/* Request that a device is direct mapped by the IOMMU */
1849int iommu_request_dm_for_dev(struct device *dev)
1850{
1851 struct iommu_domain *dm_domain;
1852 struct iommu_group *group;
1853 int ret;
1854
1855 /* Device must already be in a group before calling this function */
1856 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001857 if (IS_ERR(group))
1858 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001859
1860 mutex_lock(&group->mutex);
1861
1862 /* Check if the default domain is already direct mapped */
1863 ret = 0;
1864 if (group->default_domain &&
1865 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1866 goto out;
1867
1868 /* Don't change mappings of existing devices */
1869 ret = -EBUSY;
1870 if (iommu_group_device_count(group) != 1)
1871 goto out;
1872
1873 /* Allocate a direct mapped domain */
1874 ret = -ENOMEM;
1875 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1876 if (!dm_domain)
1877 goto out;
1878
1879 /* Attach the device to the domain */
1880 ret = __iommu_attach_group(dm_domain, group);
1881 if (ret) {
1882 iommu_domain_free(dm_domain);
1883 goto out;
1884 }
1885
1886 /* Make the direct mapped domain the default for this group */
1887 if (group->default_domain)
1888 iommu_domain_free(group->default_domain);
1889 group->default_domain = dm_domain;
1890
1891 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1892
1893 ret = 0;
1894out:
1895 mutex_unlock(&group->mutex);
1896 iommu_group_put(group);
1897
1898 return ret;
1899}
Robin Murphy57f98d22016-09-13 10:54:14 +01001900
Joerg Roedel534766d2017-01-31 16:58:42 +01001901const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001902{
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001903 const struct iommu_ops *ops = NULL;
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001904 struct iommu_device *iommu;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001905
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001906 spin_lock(&iommu_device_lock);
1907 list_for_each_entry(iommu, &iommu_device_list, list)
1908 if (iommu->fwnode == fwnode) {
1909 ops = iommu->ops;
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001910 break;
1911 }
Joerg Roedeld0f6f582017-02-02 12:19:12 +01001912 spin_unlock(&iommu_device_lock);
Lorenzo Pieralisie4f10ff2016-11-21 10:01:36 +00001913 return ops;
1914}
1915
Robin Murphy57f98d22016-09-13 10:54:14 +01001916int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1917 const struct iommu_ops *ops)
1918{
1919 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1920
1921 if (fwspec)
1922 return ops == fwspec->ops ? 0 : -EINVAL;
1923
1924 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1925 if (!fwspec)
1926 return -ENOMEM;
1927
1928 of_node_get(to_of_node(iommu_fwnode));
1929 fwspec->iommu_fwnode = iommu_fwnode;
1930 fwspec->ops = ops;
1931 dev->iommu_fwspec = fwspec;
1932 return 0;
1933}
1934EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1935
1936void iommu_fwspec_free(struct device *dev)
1937{
1938 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1939
1940 if (fwspec) {
1941 fwnode_handle_put(fwspec->iommu_fwnode);
1942 kfree(fwspec);
1943 dev->iommu_fwspec = NULL;
1944 }
1945}
1946EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1947
1948int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1949{
1950 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1951 size_t size;
1952 int i;
1953
1954 if (!fwspec)
1955 return -EINVAL;
1956
1957 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1958 if (size > sizeof(*fwspec)) {
1959 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1960 if (!fwspec)
1961 return -ENOMEM;
Zhen Lei909111b2017-02-03 17:35:02 +08001962
1963 dev->iommu_fwspec = fwspec;
Robin Murphy57f98d22016-09-13 10:54:14 +01001964 }
1965
1966 for (i = 0; i < num_ids; i++)
1967 fwspec->ids[fwspec->num_ids + i] = ids[i];
1968
1969 fwspec->num_ids += num_ids;
Robin Murphy57f98d22016-09-13 10:54:14 +01001970 return 0;
1971}
1972EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);