blob: 54c550f3b2691cb3b54134f47c65a443561722ea [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>
Mitchel Humpherysc75ae492015-07-15 18:27:36 -070034#include <linux/debugfs.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060035#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010036
Mitchel Humpherys42296fb2015-06-23 16:29:16 -070037#include "iommu-debug.h"
38
Alex Williamsond72e31c2012-05-30 14:18:53 -060039static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020040static DEFINE_IDA(iommu_group_ida);
Alex Williamsond72e31c2012-05-30 14:18:53 -060041
Thierry Redingb22f6432014-06-27 09:03:12 +020042struct iommu_callback_data {
43 const struct iommu_ops *ops;
44};
45
Alex Williamsond72e31c2012-05-30 14:18:53 -060046struct iommu_group {
47 struct kobject kobj;
48 struct kobject *devices_kobj;
49 struct list_head devices;
50 struct mutex mutex;
51 struct blocking_notifier_head notifier;
52 void *iommu_data;
53 void (*iommu_data_release)(void *iommu_data);
54 char *name;
55 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020056 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020057 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060058};
59
60struct iommu_device {
61 struct list_head list;
62 struct device *dev;
63 char *name;
64};
65
66struct iommu_group_attribute {
67 struct attribute attr;
68 ssize_t (*show)(struct iommu_group *group, char *buf);
69 ssize_t (*store)(struct iommu_group *group,
70 const char *buf, size_t count);
71};
72
73#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
74struct iommu_group_attribute iommu_group_attr_##_name = \
75 __ATTR(_name, _mode, _show, _store)
76
77#define to_iommu_group_attr(_attr) \
78 container_of(_attr, struct iommu_group_attribute, attr)
79#define to_iommu_group(_kobj) \
80 container_of(_kobj, struct iommu_group, kobj)
81
Joerg Roedel53723dc2015-05-28 18:41:29 +020082static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
83 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +020084static int __iommu_attach_device(struct iommu_domain *domain,
85 struct device *dev);
86static int __iommu_attach_group(struct iommu_domain *domain,
87 struct iommu_group *group);
88static void __iommu_detach_group(struct iommu_domain *domain,
89 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +020090
Alex Williamsond72e31c2012-05-30 14:18:53 -060091static ssize_t iommu_group_attr_show(struct kobject *kobj,
92 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -040093{
Alex Williamsond72e31c2012-05-30 14:18:53 -060094 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
95 struct iommu_group *group = to_iommu_group(kobj);
96 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -040097
Alex Williamsond72e31c2012-05-30 14:18:53 -060098 if (attr->show)
99 ret = attr->show(group, buf);
100 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400101}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600102
103static ssize_t iommu_group_attr_store(struct kobject *kobj,
104 struct attribute *__attr,
105 const char *buf, size_t count)
106{
107 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
108 struct iommu_group *group = to_iommu_group(kobj);
109 ssize_t ret = -EIO;
110
111 if (attr->store)
112 ret = attr->store(group, buf, count);
113 return ret;
114}
115
116static const struct sysfs_ops iommu_group_sysfs_ops = {
117 .show = iommu_group_attr_show,
118 .store = iommu_group_attr_store,
119};
120
121static int iommu_group_create_file(struct iommu_group *group,
122 struct iommu_group_attribute *attr)
123{
124 return sysfs_create_file(&group->kobj, &attr->attr);
125}
126
127static void iommu_group_remove_file(struct iommu_group *group,
128 struct iommu_group_attribute *attr)
129{
130 sysfs_remove_file(&group->kobj, &attr->attr);
131}
132
133static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
134{
135 return sprintf(buf, "%s\n", group->name);
136}
137
138static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
139
140static void iommu_group_release(struct kobject *kobj)
141{
142 struct iommu_group *group = to_iommu_group(kobj);
143
Joerg Roedel269aa802015-05-28 18:41:25 +0200144 pr_debug("Releasing group %d\n", group->id);
145
Alex Williamsond72e31c2012-05-30 14:18:53 -0600146 if (group->iommu_data_release)
147 group->iommu_data_release(group->iommu_data);
148
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200149 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600150
Joerg Roedel53723dc2015-05-28 18:41:29 +0200151 if (group->default_domain)
152 iommu_domain_free(group->default_domain);
153
Alex Williamsond72e31c2012-05-30 14:18:53 -0600154 kfree(group->name);
155 kfree(group);
156}
157
158static struct kobj_type iommu_group_ktype = {
159 .sysfs_ops = &iommu_group_sysfs_ops,
160 .release = iommu_group_release,
161};
162
163/**
164 * iommu_group_alloc - Allocate a new group
165 * @name: Optional name to associate with group, visible in sysfs
166 *
167 * This function is called by an iommu driver to allocate a new iommu
168 * group. The iommu group represents the minimum granularity of the iommu.
169 * Upon successful return, the caller holds a reference to the supplied
170 * group in order to hold the group until devices are added. Use
171 * iommu_group_put() to release this extra reference count, allowing the
172 * group to be automatically reclaimed once it has no devices or external
173 * references.
174 */
175struct iommu_group *iommu_group_alloc(void)
176{
177 struct iommu_group *group;
178 int ret;
179
180 group = kzalloc(sizeof(*group), GFP_KERNEL);
181 if (!group)
182 return ERR_PTR(-ENOMEM);
183
184 group->kobj.kset = iommu_group_kset;
185 mutex_init(&group->mutex);
186 INIT_LIST_HEAD(&group->devices);
187 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
188
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200189 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
190 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600191 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200192 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600193 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200194 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600195
196 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
197 NULL, "%d", group->id);
198 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200199 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600200 kfree(group);
201 return ERR_PTR(ret);
202 }
203
204 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
205 if (!group->devices_kobj) {
206 kobject_put(&group->kobj); /* triggers .release & free */
207 return ERR_PTR(-ENOMEM);
208 }
209
210 /*
211 * The devices_kobj holds a reference on the group kobject, so
212 * as long as that exists so will the group. We can therefore
213 * use the devices_kobj for reference counting.
214 */
215 kobject_put(&group->kobj);
216
Joerg Roedel269aa802015-05-28 18:41:25 +0200217 pr_debug("Allocated group %d\n", group->id);
218
Alex Williamsond72e31c2012-05-30 14:18:53 -0600219 return group;
220}
221EXPORT_SYMBOL_GPL(iommu_group_alloc);
222
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100223struct iommu_group *iommu_group_get_by_id(int id)
224{
225 struct kobject *group_kobj;
226 struct iommu_group *group;
227 const char *name;
228
229 if (!iommu_group_kset)
230 return NULL;
231
232 name = kasprintf(GFP_KERNEL, "%d", id);
233 if (!name)
234 return NULL;
235
236 group_kobj = kset_find_obj(iommu_group_kset, name);
237 kfree(name);
238
239 if (!group_kobj)
240 return NULL;
241
242 group = container_of(group_kobj, struct iommu_group, kobj);
243 BUG_ON(group->id != id);
244
245 kobject_get(group->devices_kobj);
246 kobject_put(&group->kobj);
247
248 return group;
249}
250EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
251
Alex Williamsond72e31c2012-05-30 14:18:53 -0600252/**
253 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
254 * @group: the group
255 *
256 * iommu drivers can store data in the group for use when doing iommu
257 * operations. This function provides a way to retrieve it. Caller
258 * should hold a group reference.
259 */
260void *iommu_group_get_iommudata(struct iommu_group *group)
261{
262 return group->iommu_data;
263}
264EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
265
266/**
267 * iommu_group_set_iommudata - set iommu_data for a group
268 * @group: the group
269 * @iommu_data: new data
270 * @release: release function for iommu_data
271 *
272 * iommu drivers can store data in the group for use when doing iommu
273 * operations. This function provides a way to set the data after
274 * the group has been allocated. Caller should hold a group reference.
275 */
276void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
277 void (*release)(void *iommu_data))
278{
279 group->iommu_data = iommu_data;
280 group->iommu_data_release = release;
281}
282EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
283
284/**
285 * iommu_group_set_name - set name for a group
286 * @group: the group
287 * @name: name
288 *
289 * Allow iommu driver to set a name for a group. When set it will
290 * appear in a name attribute file under the group in sysfs.
291 */
292int iommu_group_set_name(struct iommu_group *group, const char *name)
293{
294 int ret;
295
296 if (group->name) {
297 iommu_group_remove_file(group, &iommu_group_attr_name);
298 kfree(group->name);
299 group->name = NULL;
300 if (!name)
301 return 0;
302 }
303
304 group->name = kstrdup(name, GFP_KERNEL);
305 if (!group->name)
306 return -ENOMEM;
307
308 ret = iommu_group_create_file(group, &iommu_group_attr_name);
309 if (ret) {
310 kfree(group->name);
311 group->name = NULL;
312 return ret;
313 }
314
315 return 0;
316}
317EXPORT_SYMBOL_GPL(iommu_group_set_name);
318
Joerg Roedelbeed2822015-05-28 18:41:34 +0200319static int iommu_group_create_direct_mappings(struct iommu_group *group,
320 struct device *dev)
321{
322 struct iommu_domain *domain = group->default_domain;
323 struct iommu_dm_region *entry;
324 struct list_head mappings;
325 unsigned long pg_size;
326 int ret = 0;
327
328 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
329 return 0;
330
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100331 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200332
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100333 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200334 INIT_LIST_HEAD(&mappings);
335
336 iommu_get_dm_regions(dev, &mappings);
337
338 /* We need to consider overlapping regions for different devices */
339 list_for_each_entry(entry, &mappings, list) {
340 dma_addr_t start, end, addr;
341
Joerg Roedel33b21a62016-07-05 13:07:53 +0200342 if (domain->ops->apply_dm_region)
343 domain->ops->apply_dm_region(dev, domain, entry);
344
Joerg Roedelbeed2822015-05-28 18:41:34 +0200345 start = ALIGN(entry->start, pg_size);
346 end = ALIGN(entry->start + entry->length, pg_size);
347
348 for (addr = start; addr < end; addr += pg_size) {
349 phys_addr_t phys_addr;
350
351 phys_addr = iommu_iova_to_phys(domain, addr);
352 if (phys_addr)
353 continue;
354
355 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
356 if (ret)
357 goto out;
358 }
359
360 }
361
362out:
363 iommu_put_dm_regions(dev, &mappings);
364
365 return ret;
366}
367
Alex Williamsond72e31c2012-05-30 14:18:53 -0600368/**
369 * iommu_group_add_device - add a device to an iommu group
370 * @group: the group into which to add the device (reference should be held)
371 * @dev: the device
372 *
373 * This function is called by an iommu driver to add a device into a
374 * group. Adding a device increments the group reference count.
375 */
376int iommu_group_add_device(struct iommu_group *group, struct device *dev)
377{
378 int ret, i = 0;
379 struct iommu_device *device;
380
381 device = kzalloc(sizeof(*device), GFP_KERNEL);
382 if (!device)
383 return -ENOMEM;
384
385 device->dev = dev;
386
387 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
388 if (ret) {
389 kfree(device);
390 return ret;
391 }
392
393 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
394rename:
395 if (!device->name) {
396 sysfs_remove_link(&dev->kobj, "iommu_group");
397 kfree(device);
398 return -ENOMEM;
399 }
400
401 ret = sysfs_create_link_nowarn(group->devices_kobj,
402 &dev->kobj, device->name);
403 if (ret) {
404 kfree(device->name);
405 if (ret == -EEXIST && i >= 0) {
406 /*
407 * Account for the slim chance of collision
408 * and append an instance to the name.
409 */
410 device->name = kasprintf(GFP_KERNEL, "%s.%d",
411 kobject_name(&dev->kobj), i++);
412 goto rename;
413 }
414
415 sysfs_remove_link(&dev->kobj, "iommu_group");
416 kfree(device);
417 return ret;
418 }
419
420 kobject_get(group->devices_kobj);
421
422 dev->iommu_group = group;
423
Joerg Roedelbeed2822015-05-28 18:41:34 +0200424 iommu_group_create_direct_mappings(group, dev);
425
Alex Williamsond72e31c2012-05-30 14:18:53 -0600426 mutex_lock(&group->mutex);
427 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200428 if (group->domain)
429 __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600430 mutex_unlock(&group->mutex);
431
432 /* Notify any listeners about change to group. */
433 blocking_notifier_call_chain(&group->notifier,
434 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600435
436 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200437
438 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
439
Alex Williamsond72e31c2012-05-30 14:18:53 -0600440 return 0;
441}
442EXPORT_SYMBOL_GPL(iommu_group_add_device);
443
444/**
445 * iommu_group_remove_device - remove a device from it's current group
446 * @dev: device to be removed
447 *
448 * This function is called by an iommu driver to remove the device from
449 * it's current group. This decrements the iommu group reference count.
450 */
451void iommu_group_remove_device(struct device *dev)
452{
453 struct iommu_group *group = dev->iommu_group;
454 struct iommu_device *tmp_device, *device = NULL;
455
Joerg Roedel269aa802015-05-28 18:41:25 +0200456 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
457
Alex Williamsond72e31c2012-05-30 14:18:53 -0600458 /* Pre-notify listeners that a device is being removed. */
459 blocking_notifier_call_chain(&group->notifier,
460 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
461
462 mutex_lock(&group->mutex);
463 list_for_each_entry(tmp_device, &group->devices, list) {
464 if (tmp_device->dev == dev) {
465 device = tmp_device;
466 list_del(&device->list);
467 break;
468 }
469 }
470 mutex_unlock(&group->mutex);
471
472 if (!device)
473 return;
474
475 sysfs_remove_link(group->devices_kobj, device->name);
476 sysfs_remove_link(&dev->kobj, "iommu_group");
477
Shuah Khan2e757082013-08-15 11:59:25 -0600478 trace_remove_device_from_group(group->id, dev);
479
Alex Williamsond72e31c2012-05-30 14:18:53 -0600480 kfree(device->name);
481 kfree(device);
482 dev->iommu_group = NULL;
483 kobject_put(group->devices_kobj);
484}
485EXPORT_SYMBOL_GPL(iommu_group_remove_device);
486
Joerg Roedel426a2732015-05-28 18:41:30 +0200487static int iommu_group_device_count(struct iommu_group *group)
488{
489 struct iommu_device *entry;
490 int ret = 0;
491
492 list_for_each_entry(entry, &group->devices, list)
493 ret++;
494
495 return ret;
496}
497
Alex Williamsond72e31c2012-05-30 14:18:53 -0600498/**
499 * iommu_group_for_each_dev - iterate over each device in the group
500 * @group: the group
501 * @data: caller opaque data to be passed to callback function
502 * @fn: caller supplied callback function
503 *
504 * This function is called by group users to iterate over group devices.
505 * Callers should hold a reference count to the group during callback.
506 * The group->mutex is held across callbacks, which will block calls to
507 * iommu_group_add/remove_device.
508 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200509static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
510 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600511{
512 struct iommu_device *device;
513 int ret = 0;
514
Alex Williamsond72e31c2012-05-30 14:18:53 -0600515 list_for_each_entry(device, &group->devices, list) {
516 ret = fn(device->dev, data);
517 if (ret)
518 break;
519 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200520 return ret;
521}
522
523
524int iommu_group_for_each_dev(struct iommu_group *group, void *data,
525 int (*fn)(struct device *, void *))
526{
527 int ret;
528
529 mutex_lock(&group->mutex);
530 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600531 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200532
Alex Williamsond72e31c2012-05-30 14:18:53 -0600533 return ret;
534}
535EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
536
537/**
538 * iommu_group_get - Return the group for a device and increment reference
539 * @dev: get the group that this device belongs to
540 *
541 * This function is called by iommu drivers and users to get the group
542 * for the specified device. If found, the group is returned and the group
543 * reference in incremented, else NULL.
544 */
545struct iommu_group *iommu_group_get(struct device *dev)
546{
547 struct iommu_group *group = dev->iommu_group;
548
549 if (group)
550 kobject_get(group->devices_kobj);
551
552 return group;
553}
554EXPORT_SYMBOL_GPL(iommu_group_get);
555
556/**
557 * iommu_group_put - Decrement group reference
558 * @group: the group to use
559 *
560 * This function is called by iommu drivers and users to release the
561 * iommu group. Once the reference count is zero, the group is released.
562 */
563void iommu_group_put(struct iommu_group *group)
564{
565 if (group)
566 kobject_put(group->devices_kobj);
567}
568EXPORT_SYMBOL_GPL(iommu_group_put);
569
570/**
571 * iommu_group_register_notifier - Register a notifier for group changes
572 * @group: the group to watch
573 * @nb: notifier block to signal
574 *
575 * This function allows iommu group users to track changes in a group.
576 * See include/linux/iommu.h for actions sent via this notifier. Caller
577 * should hold a reference to the group throughout notifier registration.
578 */
579int iommu_group_register_notifier(struct iommu_group *group,
580 struct notifier_block *nb)
581{
582 return blocking_notifier_chain_register(&group->notifier, nb);
583}
584EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
585
586/**
587 * iommu_group_unregister_notifier - Unregister a notifier
588 * @group: the group to watch
589 * @nb: notifier block to signal
590 *
591 * Unregister a previously registered group notifier block.
592 */
593int iommu_group_unregister_notifier(struct iommu_group *group,
594 struct notifier_block *nb)
595{
596 return blocking_notifier_chain_unregister(&group->notifier, nb);
597}
598EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
599
600/**
601 * iommu_group_id - Return ID for a group
602 * @group: the group to ID
603 *
604 * Return the unique ID for the group matching the sysfs group number.
605 */
606int iommu_group_id(struct iommu_group *group)
607{
608 return group->id;
609}
610EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400611
Alex Williamsonf096c062014-09-19 10:03:06 -0600612static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
613 unsigned long *devfns);
614
Alex Williamson104a1c12014-07-03 09:51:18 -0600615/*
616 * To consider a PCI device isolated, we require ACS to support Source
617 * Validation, Request Redirection, Completer Redirection, and Upstream
618 * Forwarding. This effectively means that devices cannot spoof their
619 * requester ID, requests and completions cannot be redirected, and all
620 * transactions are forwarded upstream, even as it passes through a
621 * bridge where the target device is downstream.
622 */
623#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
624
Alex Williamsonf096c062014-09-19 10:03:06 -0600625/*
626 * For multifunction devices which are not isolated from each other, find
627 * all the other non-isolated functions and look for existing groups. For
628 * each function, we also need to look for aliases to or from other devices
629 * that may already have a group.
630 */
631static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
632 unsigned long *devfns)
633{
634 struct pci_dev *tmp = NULL;
635 struct iommu_group *group;
636
637 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
638 return NULL;
639
640 for_each_pci_dev(tmp) {
641 if (tmp == pdev || tmp->bus != pdev->bus ||
642 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
643 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
644 continue;
645
646 group = get_pci_alias_group(tmp, devfns);
647 if (group) {
648 pci_dev_put(tmp);
649 return group;
650 }
651 }
652
653 return NULL;
654}
655
656/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100657 * Look for aliases to or from the given device for existing groups. DMA
658 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600659 * space is quite small (especially since we're really only looking at pcie
660 * device, and therefore only expect multiple slots on the root complex or
661 * downstream switch ports). It's conceivable though that a pair of
662 * multifunction devices could have aliases between them that would cause a
663 * loop. To prevent this, we use a bitmap to track where we've been.
664 */
665static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
666 unsigned long *devfns)
667{
668 struct pci_dev *tmp = NULL;
669 struct iommu_group *group;
670
671 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
672 return NULL;
673
674 group = iommu_group_get(&pdev->dev);
675 if (group)
676 return group;
677
678 for_each_pci_dev(tmp) {
679 if (tmp == pdev || tmp->bus != pdev->bus)
680 continue;
681
682 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100683 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600684 group = get_pci_alias_group(tmp, devfns);
685 if (group) {
686 pci_dev_put(tmp);
687 return group;
688 }
689
690 group = get_pci_function_alias_group(tmp, devfns);
691 if (group) {
692 pci_dev_put(tmp);
693 return group;
694 }
695 }
696 }
697
698 return NULL;
699}
700
Alex Williamson104a1c12014-07-03 09:51:18 -0600701struct group_for_pci_data {
702 struct pci_dev *pdev;
703 struct iommu_group *group;
704};
705
706/*
707 * DMA alias iterator callback, return the last seen device. Stop and return
708 * the IOMMU group if we find one along the way.
709 */
710static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
711{
712 struct group_for_pci_data *data = opaque;
713
714 data->pdev = pdev;
715 data->group = iommu_group_get(&pdev->dev);
716
717 return data->group != NULL;
718}
719
720/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200721 * Generic device_group call-back function. It just allocates one
722 * iommu-group per device.
723 */
724struct iommu_group *generic_device_group(struct device *dev)
725{
726 struct iommu_group *group;
727
728 group = iommu_group_alloc();
729 if (IS_ERR(group))
730 return NULL;
731
732 return group;
733}
734
735/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600736 * Use standard PCI bus topology, isolation features, and DMA alias quirks
737 * to find or create an IOMMU group for a device.
738 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200739struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600740{
Joerg Roedel5e622922015-10-21 23:51:37 +0200741 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600742 struct group_for_pci_data data;
743 struct pci_bus *bus;
744 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600745 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600746
Joerg Roedel5e622922015-10-21 23:51:37 +0200747 if (WARN_ON(!dev_is_pci(dev)))
748 return ERR_PTR(-EINVAL);
749
Alex Williamson104a1c12014-07-03 09:51:18 -0600750 /*
751 * Find the upstream DMA alias for the device. A device must not
752 * be aliased due to topology in order to have its own IOMMU group.
753 * If we find an alias along the way that already belongs to a
754 * group, use it.
755 */
756 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
757 return data.group;
758
759 pdev = data.pdev;
760
761 /*
762 * Continue upstream from the point of minimum IOMMU granularity
763 * due to aliases to the point where devices are protected from
764 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
765 * group, use it.
766 */
767 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
768 if (!bus->self)
769 continue;
770
771 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
772 break;
773
774 pdev = bus->self;
775
776 group = iommu_group_get(&pdev->dev);
777 if (group)
778 return group;
779 }
780
781 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600782 * Look for existing groups on device aliases. If we alias another
783 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600784 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600785 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
786 if (group)
787 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600788
789 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600790 * Look for existing groups on non-isolated functions on the same
791 * slot and aliases of those funcions, if any. No need to clear
792 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600793 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600794 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
795 if (group)
796 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600797
798 /* No shared group found, allocate new */
Joerg Roedel53723dc2015-05-28 18:41:29 +0200799 group = iommu_group_alloc();
Dan Carpenter409e5532015-06-10 13:59:27 +0300800 if (IS_ERR(group))
801 return NULL;
802
Joerg Roedel53723dc2015-05-28 18:41:29 +0200803 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600804}
805
806/**
807 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
808 * @dev: target device
809 *
810 * This function is intended to be called by IOMMU drivers and extended to
811 * support common, bus-defined algorithms when determining or creating the
812 * IOMMU group for a device. On success, the caller will hold a reference
813 * to the returned IOMMU group, which will already include the provided
814 * device. The reference should be released with iommu_group_put().
815 */
816struct iommu_group *iommu_group_get_for_dev(struct device *dev)
817{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200818 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200819 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600820 int ret;
821
822 group = iommu_group_get(dev);
823 if (group)
824 return group;
825
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200826 group = ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200827
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200828 if (ops && ops->device_group)
829 group = ops->device_group(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600830
831 if (IS_ERR(group))
832 return group;
833
Joerg Roedel12282362015-10-21 23:51:43 +0200834 /*
835 * Try to allocate a default domain - needs support from the
836 * IOMMU driver.
837 */
838 if (!group->default_domain) {
839 group->default_domain = __iommu_domain_alloc(dev->bus,
840 IOMMU_DOMAIN_DMA);
Joerg Roedeleebb8032016-04-04 15:47:48 +0200841 if (!group->domain)
842 group->domain = group->default_domain;
Joerg Roedel12282362015-10-21 23:51:43 +0200843 }
844
Alex Williamson104a1c12014-07-03 09:51:18 -0600845 ret = iommu_group_add_device(group, dev);
846 if (ret) {
847 iommu_group_put(group);
848 return ERR_PTR(ret);
849 }
850
851 return group;
852}
853
Joerg Roedel6827ca82015-05-28 18:41:35 +0200854struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
855{
856 return group->default_domain;
857}
858
Alex Williamson14604322011-10-21 15:56:05 -0400859static int add_iommu_group(struct device *dev, void *data)
860{
Thierry Redingb22f6432014-06-27 09:03:12 +0200861 struct iommu_callback_data *cb = data;
862 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +0200863 int ret;
Alex Williamson14604322011-10-21 15:56:05 -0400864
Alex Williamsond72e31c2012-05-30 14:18:53 -0600865 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +0000866 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600867
868 WARN_ON(dev->iommu_group);
869
Joerg Roedel38667f12015-06-29 10:16:08 +0200870 ret = ops->add_device(dev);
871
872 /*
873 * We ignore -ENODEV errors for now, as they just mean that the
874 * device is not translated by an IOMMU. We still care about
875 * other errors and fail to initialize when they happen.
876 */
877 if (ret == -ENODEV)
878 ret = 0;
879
880 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400881}
882
Joerg Roedel8da30142015-05-28 18:41:27 +0200883static int remove_iommu_group(struct device *dev, void *data)
884{
885 struct iommu_callback_data *cb = data;
886 const struct iommu_ops *ops = cb->ops;
887
888 if (ops->remove_device && dev->iommu_group)
889 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400890
891 return 0;
892}
893
Alex Williamsond72e31c2012-05-30 14:18:53 -0600894static int iommu_bus_notifier(struct notifier_block *nb,
895 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400896{
897 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +0200898 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600899 struct iommu_group *group;
900 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400901
Alex Williamsond72e31c2012-05-30 14:18:53 -0600902 /*
903 * ADD/DEL call into iommu driver ops if provided, which may
904 * result in ADD/DEL notifiers to group->notifier
905 */
906 if (action == BUS_NOTIFY_ADD_DEVICE) {
907 if (ops->add_device)
908 return ops->add_device(dev);
Joerg Roedel843cb6d2015-05-28 18:41:28 +0200909 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600910 if (ops->remove_device && dev->iommu_group) {
911 ops->remove_device(dev);
912 return 0;
913 }
914 }
Alex Williamson14604322011-10-21 15:56:05 -0400915
Alex Williamsond72e31c2012-05-30 14:18:53 -0600916 /*
917 * Remaining BUS_NOTIFYs get filtered and republished to the
918 * group, if anyone is listening
919 */
920 group = iommu_group_get(dev);
921 if (!group)
922 return 0;
923
924 switch (action) {
925 case BUS_NOTIFY_BIND_DRIVER:
926 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
927 break;
928 case BUS_NOTIFY_BOUND_DRIVER:
929 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
930 break;
931 case BUS_NOTIFY_UNBIND_DRIVER:
932 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
933 break;
934 case BUS_NOTIFY_UNBOUND_DRIVER:
935 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
936 break;
937 }
938
939 if (group_action)
940 blocking_notifier_call_chain(&group->notifier,
941 group_action, dev);
942
943 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400944 return 0;
945}
946
Mark Salterfb3e3062014-09-21 13:58:24 -0400947static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100948{
Mark Salterfb3e3062014-09-21 13:58:24 -0400949 int err;
950 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +0200951 struct iommu_callback_data cb = {
952 .ops = ops,
953 };
954
Mark Salterfb3e3062014-09-21 13:58:24 -0400955 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
956 if (!nb)
957 return -ENOMEM;
958
959 nb->notifier_call = iommu_bus_notifier;
960
961 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +0200962 if (err)
963 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100964
965 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +0200966 if (err)
967 goto out_err;
968
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100969
970 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +0200971
972out_err:
973 /* Clean up */
974 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
975 bus_unregister_notifier(bus, nb);
976
977out_free:
978 kfree(nb);
979
980 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100981}
982
Joerg Roedelff217762011-08-26 16:48:26 +0200983/**
984 * bus_set_iommu - set iommu-callbacks for the bus
985 * @bus: bus.
986 * @ops: the callbacks provided by the iommu-driver
987 *
988 * This function is called by an iommu driver to set the iommu methods
989 * used for a particular bus. Drivers for devices on that bus can use
990 * the iommu-api after these ops are registered.
991 * This special function is needed because IOMMUs are usually devices on
992 * the bus itself, so the iommu drivers are not initialized when the bus
993 * is set up. With this function the iommu-driver can set the iommu-ops
994 * afterwards.
995 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200996int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100997{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100998 int err;
999
Joerg Roedelff217762011-08-26 16:48:26 +02001000 if (bus->iommu_ops != NULL)
1001 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001002
Joerg Roedelff217762011-08-26 16:48:26 +02001003 bus->iommu_ops = ops;
1004
1005 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001006 err = iommu_bus_init(bus, ops);
1007 if (err)
1008 bus->iommu_ops = NULL;
1009
1010 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001011}
Joerg Roedelff217762011-08-26 16:48:26 +02001012EXPORT_SYMBOL_GPL(bus_set_iommu);
1013
Joerg Roedela1b60c12011-09-06 18:46:34 +02001014bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001015{
Joerg Roedel94441c32011-09-06 18:58:54 +02001016 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001017}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001018EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001019
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001020bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1021{
1022 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1023 return false;
1024
1025 return bus->iommu_ops->capable(cap);
1026}
1027EXPORT_SYMBOL_GPL(iommu_capable);
1028
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001029/**
1030 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1031 * @domain: iommu domain
1032 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001033 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001034 *
1035 * This function should be used by IOMMU users which want to be notified
1036 * whenever an IOMMU fault happens.
1037 *
1038 * The fault handler itself should return 0 on success, and an appropriate
1039 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001040 */
1041void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001042 iommu_fault_handler_t handler,
1043 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001044{
1045 BUG_ON(!domain);
1046
1047 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001048 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001049}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001050EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001051
Joerg Roedel53723dc2015-05-28 18:41:29 +02001052static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1053 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001054{
1055 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001056
Joerg Roedel94441c32011-09-06 18:58:54 +02001057 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001058 return NULL;
1059
Joerg Roedel53723dc2015-05-28 18:41:29 +02001060 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001061 if (!domain)
1062 return NULL;
1063
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001064 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001065 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001066 /* Assume all sizes by default; the driver may override this later */
1067 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001068
Susheel Khianie66aa5b2015-08-25 17:25:42 +05301069 iommu_debug_domain_add(domain);
1070
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001071 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001072}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001073
Joerg Roedel53723dc2015-05-28 18:41:29 +02001074struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1075{
1076 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001077}
1078EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1079
1080void iommu_domain_free(struct iommu_domain *domain)
1081{
Susheel Khianie66aa5b2015-08-25 17:25:42 +05301082 iommu_debug_domain_remove(domain);
Joerg Roedel89be34a2015-03-26 13:43:19 +01001083 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001084}
1085EXPORT_SYMBOL_GPL(iommu_domain_free);
1086
Joerg Roedel426a2732015-05-28 18:41:30 +02001087static int __iommu_attach_device(struct iommu_domain *domain,
1088 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001089{
Shuah Khanb54db772013-08-15 11:59:26 -06001090 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001091 if (unlikely(domain->ops->attach_dev == NULL))
1092 return -ENODEV;
1093
Shuah Khanb54db772013-08-15 11:59:26 -06001094 ret = domain->ops->attach_dev(domain, dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001095 if (!ret) {
Shuah Khanb54db772013-08-15 11:59:26 -06001096 trace_attach_device_to_domain(dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001097 iommu_debug_attach_device(domain, dev);
1098 }
Shuah Khanb54db772013-08-15 11:59:26 -06001099 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001100}
Joerg Roedel426a2732015-05-28 18:41:30 +02001101
1102int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1103{
1104 struct iommu_group *group;
1105 int ret;
1106
1107 group = iommu_group_get(dev);
1108 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1109 if (group == NULL)
1110 return __iommu_attach_device(domain, dev);
1111
1112 /*
1113 * We have a group - lock it to make sure the device-count doesn't
1114 * change while we are attaching
1115 */
1116 mutex_lock(&group->mutex);
1117 ret = -EINVAL;
1118 if (iommu_group_device_count(group) != 1)
1119 goto out_unlock;
1120
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001121 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001122
1123out_unlock:
1124 mutex_unlock(&group->mutex);
1125 iommu_group_put(group);
1126
1127 return ret;
1128}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001129EXPORT_SYMBOL_GPL(iommu_attach_device);
1130
Joerg Roedel426a2732015-05-28 18:41:30 +02001131static void __iommu_detach_device(struct iommu_domain *domain,
1132 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001133{
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001134 iommu_debug_detach_device(domain, dev);
1135
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001136 if (unlikely(domain->ops->detach_dev == NULL))
1137 return;
1138
1139 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001140 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001141}
Joerg Roedel426a2732015-05-28 18:41:30 +02001142
1143void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1144{
1145 struct iommu_group *group;
1146
1147 group = iommu_group_get(dev);
1148 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1149 if (group == NULL)
1150 return __iommu_detach_device(domain, dev);
1151
1152 mutex_lock(&group->mutex);
1153 if (iommu_group_device_count(group) != 1) {
1154 WARN_ON(1);
1155 goto out_unlock;
1156 }
1157
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001158 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001159
1160out_unlock:
1161 mutex_unlock(&group->mutex);
1162 iommu_group_put(group);
1163}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001164EXPORT_SYMBOL_GPL(iommu_detach_device);
1165
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001166struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1167{
1168 struct iommu_domain *domain;
1169 struct iommu_group *group;
1170
1171 group = iommu_group_get(dev);
1172 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1173 if (group == NULL)
1174 return NULL;
1175
1176 domain = group->domain;
1177
1178 iommu_group_put(group);
1179
1180 return domain;
1181}
1182EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1183
Alex Williamsond72e31c2012-05-30 14:18:53 -06001184/*
1185 * IOMMU groups are really the natrual working unit of the IOMMU, but
1186 * the IOMMU API works on domains and devices. Bridge that gap by
1187 * iterating over the devices in a group. Ideally we'd have a single
1188 * device which represents the requestor ID of the group, but we also
1189 * allow IOMMU drivers to create policy defined minimum sets, where
1190 * the physical hardware may be able to distiguish members, but we
1191 * wish to group them at a higher level (ex. untrusted multi-function
1192 * PCI devices). Thus we attach each device.
1193 */
1194static int iommu_group_do_attach_device(struct device *dev, void *data)
1195{
1196 struct iommu_domain *domain = data;
1197
Joerg Roedel426a2732015-05-28 18:41:30 +02001198 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001199}
1200
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001201static int __iommu_attach_group(struct iommu_domain *domain,
1202 struct iommu_group *group)
1203{
1204 int ret;
1205
1206 if (group->default_domain && group->domain != group->default_domain)
1207 return -EBUSY;
1208
1209 ret = __iommu_group_for_each_dev(group, domain,
1210 iommu_group_do_attach_device);
1211 if (ret == 0)
1212 group->domain = domain;
1213
1214 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001215}
1216
1217int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1218{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001219 int ret;
1220
1221 mutex_lock(&group->mutex);
1222 ret = __iommu_attach_group(domain, group);
1223 mutex_unlock(&group->mutex);
1224
1225 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001226}
1227EXPORT_SYMBOL_GPL(iommu_attach_group);
1228
1229static int iommu_group_do_detach_device(struct device *dev, void *data)
1230{
1231 struct iommu_domain *domain = data;
1232
Joerg Roedel426a2732015-05-28 18:41:30 +02001233 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001234
1235 return 0;
1236}
1237
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001238static void __iommu_detach_group(struct iommu_domain *domain,
1239 struct iommu_group *group)
1240{
1241 int ret;
1242
1243 if (!group->default_domain) {
1244 __iommu_group_for_each_dev(group, domain,
1245 iommu_group_do_detach_device);
1246 group->domain = NULL;
1247 return;
1248 }
1249
1250 if (group->domain == group->default_domain)
1251 return;
1252
1253 /* Detach by re-attaching to the default domain */
1254 ret = __iommu_group_for_each_dev(group, group->default_domain,
1255 iommu_group_do_attach_device);
1256 if (ret != 0)
1257 WARN_ON(1);
1258 else
1259 group->domain = group->default_domain;
1260}
1261
Alex Williamsond72e31c2012-05-30 14:18:53 -06001262void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1263{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001264 mutex_lock(&group->mutex);
1265 __iommu_detach_group(domain, group);
1266 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001267}
1268EXPORT_SYMBOL_GPL(iommu_detach_group);
1269
Varun Sethibb5547ac2013-03-29 01:23:58 +05301270phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001271{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001272 if (unlikely(domain->ops->iova_to_phys == NULL))
1273 return 0;
1274
1275 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001276}
1277EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001278
Mitchel Humpherys36b8c322015-07-06 15:24:22 -07001279phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
1280 dma_addr_t iova)
1281{
1282 if (unlikely(domain->ops->iova_to_phys_hard == NULL))
1283 return 0;
1284
1285 return domain->ops->iova_to_phys_hard(domain, iova);
1286}
1287
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001288size_t iommu_pgsize(unsigned long pgsize_bitmap,
1289 unsigned long addr_merge, size_t size)
Alex Williamsonbd139692013-06-17 19:57:34 -06001290{
1291 unsigned int pgsize_idx;
1292 size_t pgsize;
1293
1294 /* Max page size that still fits into 'size' */
1295 pgsize_idx = __fls(size);
1296
1297 /* need to consider alignment requirements ? */
1298 if (likely(addr_merge)) {
1299 /* Max page size allowed by address */
1300 unsigned int align_pgsize_idx = __ffs(addr_merge);
1301 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1302 }
1303
1304 /* build a mask of acceptable page sizes */
1305 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1306
1307 /* throw away page sizes not supported by the hardware */
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001308 pgsize &= pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001309
1310 /* make sure we're still sane */
Mitchel Humpherys76e77fb2015-05-12 09:44:32 -07001311 if (!pgsize) {
1312 pr_err("invalid pgsize/addr/size! 0x%lx 0x%lx 0x%zx\n",
1313 pgsize_bitmap, addr_merge, size);
1314 BUG();
1315 }
Alex Williamsonbd139692013-06-17 19:57:34 -06001316
1317 /* pick the biggest page */
1318 pgsize_idx = __fls(pgsize);
1319 pgsize = 1UL << pgsize_idx;
1320
1321 return pgsize;
1322}
1323
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001324int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001325 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001326{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001327 unsigned long orig_iova = iova;
1328 unsigned int min_pagesz;
1329 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001330 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001331 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001332
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001333 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001334 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001335 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001336
Joerg Roedela10315e2015-03-26 13:43:06 +01001337 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1338 return -EINVAL;
1339
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001340 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001341 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001342
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001343 /*
1344 * both the virtual address and the physical one, as well as
1345 * the size of the mapping, must be aligned (at least) to the
1346 * size of the smallest page supported by the hardware
1347 */
1348 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001349 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001350 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001351 return -EINVAL;
1352 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001353
Fabio Estevamabedb042013-08-22 10:25:42 -03001354 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001355
1356 while (size) {
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001357 size_t pgsize = iommu_pgsize(domain->pgsize_bitmap,
1358 iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001359
Fabio Estevamabedb042013-08-22 10:25:42 -03001360 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001361 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001362
1363 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1364 if (ret)
1365 break;
1366
1367 iova += pgsize;
1368 paddr += pgsize;
1369 size -= pgsize;
1370 }
1371
1372 /* unroll mapping in case something went wrong */
1373 if (ret)
1374 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001375 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001376 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001377
1378 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001379}
1380EXPORT_SYMBOL_GPL(iommu_map);
1381
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001382size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001383{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001384 size_t unmapped_page, unmapped = 0;
1385 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001386 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001387
Joerg Roedel57886512013-01-29 13:41:09 +01001388 if (unlikely(domain->ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001389 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001390 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001391
Joerg Roedela10315e2015-03-26 13:43:06 +01001392 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1393 return -EINVAL;
1394
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001395 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001396 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001397
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001398 /*
1399 * The virtual address, as well as the size of the mapping, must be
1400 * aligned (at least) to the size of the smallest page supported
1401 * by the hardware
1402 */
1403 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001404 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1405 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001406 return -EINVAL;
1407 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001408
Joe Perches6197ca82013-06-23 12:29:04 -07001409 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001410
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001411 /*
1412 * Keep iterating until we either unmap 'size' bytes (or more)
1413 * or we hit an area that isn't mapped.
1414 */
1415 while (unmapped < size) {
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001416 size_t left = size - unmapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001417
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001418 unmapped_page = domain->ops->unmap(domain, iova, left);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001419 if (!unmapped_page)
1420 break;
1421
Joe Perches6197ca82013-06-23 12:29:04 -07001422 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1423 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001424
1425 iova += unmapped_page;
1426 unmapped += unmapped_page;
1427 }
1428
Shuah Khandb8614d2015-01-16 20:53:17 -07001429 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001430 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001431}
1432EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001433
Olav Haugan315786e2014-10-25 09:55:16 -07001434size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1435 struct scatterlist *sg, unsigned int nents, int prot)
1436{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001437 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001438 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001439 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001440 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001441
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001442 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001443 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001444
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001445 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001446
1447 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001448 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001449
1450 /*
1451 * We are mapping on IOMMU page boundaries, so offset within
1452 * the page must be 0. However, the IOMMU may support pages
1453 * smaller than PAGE_SIZE, so s->offset may still represent
1454 * an offset of that boundary within the CPU page.
1455 */
1456 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001457 goto out_err;
1458
1459 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1460 if (ret)
1461 goto out_err;
1462
1463 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001464 }
1465
1466 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001467
1468out_err:
1469 /* undo mappings already done */
1470 iommu_unmap(domain, iova, mapped);
1471
1472 return 0;
1473
Olav Haugan315786e2014-10-25 09:55:16 -07001474}
1475EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001476
1477int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301478 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001479{
1480 if (unlikely(domain->ops->domain_window_enable == NULL))
1481 return -ENODEV;
1482
Varun Sethi80f97f02013-03-29 01:24:00 +05301483 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1484 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001485}
1486EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1487
1488void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1489{
1490 if (unlikely(domain->ops->domain_window_disable == NULL))
1491 return;
1492
1493 return domain->ops->domain_window_disable(domain, wnd_nr);
1494}
1495EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1496
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001497struct dentry *iommu_debugfs_top;
1498
Alex Williamsond72e31c2012-05-30 14:18:53 -06001499static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001500{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001501 iommu_group_kset = kset_create_and_add("iommu_groups",
1502 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001503 BUG_ON(!iommu_group_kset);
1504
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001505 iommu_debugfs_top = debugfs_create_dir("iommu", NULL);
1506 if (!iommu_debugfs_top) {
1507 pr_err("Couldn't create iommu debugfs directory\n");
1508 return -ENODEV;
1509 }
1510
Alex Williamsond72e31c2012-05-30 14:18:53 -06001511 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001512}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001513core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001514
1515int iommu_domain_get_attr(struct iommu_domain *domain,
1516 enum iommu_attr attr, void *data)
1517{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001518 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001519 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001520 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001521 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001522
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001523 switch (attr) {
1524 case DOMAIN_ATTR_GEOMETRY:
1525 geometry = data;
1526 *geometry = domain->geometry;
1527
1528 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001529 case DOMAIN_ATTR_PAGING:
1530 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001531 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001532 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001533 case DOMAIN_ATTR_WINDOWS:
1534 count = data;
1535
1536 if (domain->ops->domain_get_windows != NULL)
1537 *count = domain->ops->domain_get_windows(domain);
1538 else
1539 ret = -ENODEV;
1540
1541 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001542 default:
1543 if (!domain->ops->domain_get_attr)
1544 return -EINVAL;
1545
1546 ret = domain->ops->domain_get_attr(domain, attr, data);
1547 }
1548
1549 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001550}
1551EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1552
1553int iommu_domain_set_attr(struct iommu_domain *domain,
1554 enum iommu_attr attr, void *data)
1555{
Joerg Roedel69356712013-02-04 14:00:01 +01001556 int ret = 0;
1557 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001558
Joerg Roedel69356712013-02-04 14:00:01 +01001559 switch (attr) {
1560 case DOMAIN_ATTR_WINDOWS:
1561 count = data;
1562
1563 if (domain->ops->domain_set_windows != NULL)
1564 ret = domain->ops->domain_set_windows(domain, *count);
1565 else
1566 ret = -ENODEV;
1567
1568 break;
1569 default:
1570 if (domain->ops->domain_set_attr == NULL)
1571 return -EINVAL;
1572
1573 ret = domain->ops->domain_set_attr(domain, attr, data);
1574 }
1575
1576 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001577}
1578EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001579
Mitchel Humpherys8488df22015-07-09 16:59:02 -07001580/**
1581 * iommu_trigger_fault() - trigger an IOMMU fault
1582 * @domain: iommu domain
1583 *
1584 * Triggers a fault on the device to which this domain is attached.
1585 *
1586 * This function should only be used for debugging purposes, for obvious
1587 * reasons.
1588 */
1589void iommu_trigger_fault(struct iommu_domain *domain, unsigned long flags)
1590{
1591 if (domain->ops->trigger_fault)
1592 domain->ops->trigger_fault(domain, flags);
1593}
1594
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -07001595/**
1596 * iommu_reg_read() - read an IOMMU register
1597 *
1598 * Reads the IOMMU register at the given offset.
1599 */
1600unsigned long iommu_reg_read(struct iommu_domain *domain, unsigned long offset)
1601{
1602 if (domain->ops->reg_read)
1603 return domain->ops->reg_read(domain, offset);
1604 return 0;
1605}
1606
1607/**
1608 * iommu_reg_write() - write an IOMMU register
1609 *
1610 * Writes the given value to the IOMMU register at the given offset.
1611 */
1612void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
1613 unsigned long val)
1614{
1615 if (domain->ops->reg_write)
1616 domain->ops->reg_write(domain, offset, val);
1617}
1618
Joerg Roedela1015c22015-05-28 18:41:33 +02001619void iommu_get_dm_regions(struct device *dev, struct list_head *list)
1620{
1621 const struct iommu_ops *ops = dev->bus->iommu_ops;
1622
1623 if (ops && ops->get_dm_regions)
1624 ops->get_dm_regions(dev, list);
1625}
1626
1627void iommu_put_dm_regions(struct device *dev, struct list_head *list)
1628{
1629 const struct iommu_ops *ops = dev->bus->iommu_ops;
1630
1631 if (ops && ops->put_dm_regions)
1632 ops->put_dm_regions(dev, list);
1633}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001634
1635/* Request that a device is direct mapped by the IOMMU */
1636int iommu_request_dm_for_dev(struct device *dev)
1637{
1638 struct iommu_domain *dm_domain;
1639 struct iommu_group *group;
1640 int ret;
1641
1642 /* Device must already be in a group before calling this function */
1643 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001644 if (IS_ERR(group))
1645 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001646
1647 mutex_lock(&group->mutex);
1648
1649 /* Check if the default domain is already direct mapped */
1650 ret = 0;
1651 if (group->default_domain &&
1652 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1653 goto out;
1654
1655 /* Don't change mappings of existing devices */
1656 ret = -EBUSY;
1657 if (iommu_group_device_count(group) != 1)
1658 goto out;
1659
1660 /* Allocate a direct mapped domain */
1661 ret = -ENOMEM;
1662 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1663 if (!dm_domain)
1664 goto out;
1665
1666 /* Attach the device to the domain */
1667 ret = __iommu_attach_group(dm_domain, group);
1668 if (ret) {
1669 iommu_domain_free(dm_domain);
1670 goto out;
1671 }
1672
1673 /* Make the direct mapped domain the default for this group */
1674 if (group->default_domain)
1675 iommu_domain_free(group->default_domain);
1676 group->default_domain = dm_domain;
1677
1678 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1679
1680 ret = 0;
1681out:
1682 mutex_unlock(&group->mutex);
1683 iommu_group_put(group);
1684
1685 return ret;
1686}