blob: 261c125a4f97642c34845ff66fca5d797e1285bd [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>
Robin Murphy57f98d22016-09-13 10:54:14 +010035#include <linux/property.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060036#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010037
Mitchel Humpherys42296fb2015-06-23 16:29:16 -070038#include "iommu-debug.h"
39
Alex Williamsond72e31c2012-05-30 14:18:53 -060040static struct kset *iommu_group_kset;
Heiner Kallweite38d1f12016-06-28 20:38:36 +020041static DEFINE_IDA(iommu_group_ida);
Alex Williamsond72e31c2012-05-30 14:18:53 -060042
Thierry Redingb22f6432014-06-27 09:03:12 +020043struct iommu_callback_data {
44 const struct iommu_ops *ops;
45};
46
Alex Williamsond72e31c2012-05-30 14:18:53 -060047struct iommu_group {
48 struct kobject kobj;
49 struct kobject *devices_kobj;
50 struct list_head devices;
51 struct mutex mutex;
52 struct blocking_notifier_head notifier;
53 void *iommu_data;
54 void (*iommu_data_release)(void *iommu_data);
55 char *name;
56 int id;
Joerg Roedel53723dc2015-05-28 18:41:29 +020057 struct iommu_domain *default_domain;
Joerg Roedele39cb8a2015-05-28 18:41:31 +020058 struct iommu_domain *domain;
Alex Williamsond72e31c2012-05-30 14:18:53 -060059};
60
61struct iommu_device {
62 struct list_head list;
63 struct device *dev;
64 char *name;
65};
66
67struct iommu_group_attribute {
68 struct attribute attr;
69 ssize_t (*show)(struct iommu_group *group, char *buf);
70 ssize_t (*store)(struct iommu_group *group,
71 const char *buf, size_t count);
72};
73
74#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
75struct iommu_group_attribute iommu_group_attr_##_name = \
76 __ATTR(_name, _mode, _show, _store)
77
78#define to_iommu_group_attr(_attr) \
79 container_of(_attr, struct iommu_group_attribute, attr)
80#define to_iommu_group(_kobj) \
81 container_of(_kobj, struct iommu_group, kobj)
82
Joerg Roedel53723dc2015-05-28 18:41:29 +020083static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
84 unsigned type);
Joerg Roedele39cb8a2015-05-28 18:41:31 +020085static int __iommu_attach_device(struct iommu_domain *domain,
86 struct device *dev);
87static int __iommu_attach_group(struct iommu_domain *domain,
88 struct iommu_group *group);
89static void __iommu_detach_group(struct iommu_domain *domain,
90 struct iommu_group *group);
Joerg Roedel53723dc2015-05-28 18:41:29 +020091
Alex Williamsond72e31c2012-05-30 14:18:53 -060092static ssize_t iommu_group_attr_show(struct kobject *kobj,
93 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -040094{
Alex Williamsond72e31c2012-05-30 14:18:53 -060095 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
96 struct iommu_group *group = to_iommu_group(kobj);
97 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -040098
Alex Williamsond72e31c2012-05-30 14:18:53 -060099 if (attr->show)
100 ret = attr->show(group, buf);
101 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400102}
Alex Williamsond72e31c2012-05-30 14:18:53 -0600103
104static ssize_t iommu_group_attr_store(struct kobject *kobj,
105 struct attribute *__attr,
106 const char *buf, size_t count)
107{
108 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
109 struct iommu_group *group = to_iommu_group(kobj);
110 ssize_t ret = -EIO;
111
112 if (attr->store)
113 ret = attr->store(group, buf, count);
114 return ret;
115}
116
117static const struct sysfs_ops iommu_group_sysfs_ops = {
118 .show = iommu_group_attr_show,
119 .store = iommu_group_attr_store,
120};
121
122static int iommu_group_create_file(struct iommu_group *group,
123 struct iommu_group_attribute *attr)
124{
125 return sysfs_create_file(&group->kobj, &attr->attr);
126}
127
128static void iommu_group_remove_file(struct iommu_group *group,
129 struct iommu_group_attribute *attr)
130{
131 sysfs_remove_file(&group->kobj, &attr->attr);
132}
133
134static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
135{
136 return sprintf(buf, "%s\n", group->name);
137}
138
139static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
140
141static void iommu_group_release(struct kobject *kobj)
142{
143 struct iommu_group *group = to_iommu_group(kobj);
144
Joerg Roedel269aa802015-05-28 18:41:25 +0200145 pr_debug("Releasing group %d\n", group->id);
146
Alex Williamsond72e31c2012-05-30 14:18:53 -0600147 if (group->iommu_data_release)
148 group->iommu_data_release(group->iommu_data);
149
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200150 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600151
Joerg Roedel53723dc2015-05-28 18:41:29 +0200152 if (group->default_domain)
153 iommu_domain_free(group->default_domain);
154
Alex Williamsond72e31c2012-05-30 14:18:53 -0600155 kfree(group->name);
156 kfree(group);
157}
158
159static struct kobj_type iommu_group_ktype = {
160 .sysfs_ops = &iommu_group_sysfs_ops,
161 .release = iommu_group_release,
162};
163
164/**
165 * iommu_group_alloc - Allocate a new group
166 * @name: Optional name to associate with group, visible in sysfs
167 *
168 * This function is called by an iommu driver to allocate a new iommu
169 * group. The iommu group represents the minimum granularity of the iommu.
170 * Upon successful return, the caller holds a reference to the supplied
171 * group in order to hold the group until devices are added. Use
172 * iommu_group_put() to release this extra reference count, allowing the
173 * group to be automatically reclaimed once it has no devices or external
174 * references.
175 */
176struct iommu_group *iommu_group_alloc(void)
177{
178 struct iommu_group *group;
179 int ret;
180
181 group = kzalloc(sizeof(*group), GFP_KERNEL);
182 if (!group)
183 return ERR_PTR(-ENOMEM);
184
185 group->kobj.kset = iommu_group_kset;
186 mutex_init(&group->mutex);
187 INIT_LIST_HEAD(&group->devices);
188 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
189
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200190 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
191 if (ret < 0) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600192 kfree(group);
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200193 return ERR_PTR(ret);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600194 }
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200195 group->id = ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600196
197 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
198 NULL, "%d", group->id);
199 if (ret) {
Heiner Kallweitfeccf392016-06-29 21:13:59 +0200200 ida_simple_remove(&iommu_group_ida, group->id);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600201 kfree(group);
202 return ERR_PTR(ret);
203 }
204
205 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
206 if (!group->devices_kobj) {
207 kobject_put(&group->kobj); /* triggers .release & free */
208 return ERR_PTR(-ENOMEM);
209 }
210
211 /*
212 * The devices_kobj holds a reference on the group kobject, so
213 * as long as that exists so will the group. We can therefore
214 * use the devices_kobj for reference counting.
215 */
216 kobject_put(&group->kobj);
217
Joerg Roedel269aa802015-05-28 18:41:25 +0200218 pr_debug("Allocated group %d\n", group->id);
219
Alex Williamsond72e31c2012-05-30 14:18:53 -0600220 return group;
221}
222EXPORT_SYMBOL_GPL(iommu_group_alloc);
223
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100224struct iommu_group *iommu_group_get_by_id(int id)
225{
226 struct kobject *group_kobj;
227 struct iommu_group *group;
228 const char *name;
229
230 if (!iommu_group_kset)
231 return NULL;
232
233 name = kasprintf(GFP_KERNEL, "%d", id);
234 if (!name)
235 return NULL;
236
237 group_kobj = kset_find_obj(iommu_group_kset, name);
238 kfree(name);
239
240 if (!group_kobj)
241 return NULL;
242
243 group = container_of(group_kobj, struct iommu_group, kobj);
244 BUG_ON(group->id != id);
245
246 kobject_get(group->devices_kobj);
247 kobject_put(&group->kobj);
248
249 return group;
250}
251EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
252
Alex Williamsond72e31c2012-05-30 14:18:53 -0600253/**
254 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
255 * @group: the group
256 *
257 * iommu drivers can store data in the group for use when doing iommu
258 * operations. This function provides a way to retrieve it. Caller
259 * should hold a group reference.
260 */
261void *iommu_group_get_iommudata(struct iommu_group *group)
262{
263 return group->iommu_data;
264}
265EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
266
267/**
268 * iommu_group_set_iommudata - set iommu_data for a group
269 * @group: the group
270 * @iommu_data: new data
271 * @release: release function for iommu_data
272 *
273 * iommu drivers can store data in the group for use when doing iommu
274 * operations. This function provides a way to set the data after
275 * the group has been allocated. Caller should hold a group reference.
276 */
277void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
278 void (*release)(void *iommu_data))
279{
280 group->iommu_data = iommu_data;
281 group->iommu_data_release = release;
282}
283EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
284
285/**
286 * iommu_group_set_name - set name for a group
287 * @group: the group
288 * @name: name
289 *
290 * Allow iommu driver to set a name for a group. When set it will
291 * appear in a name attribute file under the group in sysfs.
292 */
293int iommu_group_set_name(struct iommu_group *group, const char *name)
294{
295 int ret;
296
297 if (group->name) {
298 iommu_group_remove_file(group, &iommu_group_attr_name);
299 kfree(group->name);
300 group->name = NULL;
301 if (!name)
302 return 0;
303 }
304
305 group->name = kstrdup(name, GFP_KERNEL);
306 if (!group->name)
307 return -ENOMEM;
308
309 ret = iommu_group_create_file(group, &iommu_group_attr_name);
310 if (ret) {
311 kfree(group->name);
312 group->name = NULL;
313 return ret;
314 }
315
316 return 0;
317}
318EXPORT_SYMBOL_GPL(iommu_group_set_name);
319
Joerg Roedelbeed2822015-05-28 18:41:34 +0200320static int iommu_group_create_direct_mappings(struct iommu_group *group,
321 struct device *dev)
322{
323 struct iommu_domain *domain = group->default_domain;
324 struct iommu_dm_region *entry;
325 struct list_head mappings;
326 unsigned long pg_size;
327 int ret = 0;
328
329 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
330 return 0;
331
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100332 BUG_ON(!domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200333
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100334 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
Joerg Roedelbeed2822015-05-28 18:41:34 +0200335 INIT_LIST_HEAD(&mappings);
336
337 iommu_get_dm_regions(dev, &mappings);
338
339 /* We need to consider overlapping regions for different devices */
340 list_for_each_entry(entry, &mappings, list) {
341 dma_addr_t start, end, addr;
342
Joerg Roedel33b21a62016-07-05 13:07:53 +0200343 if (domain->ops->apply_dm_region)
344 domain->ops->apply_dm_region(dev, domain, entry);
345
Joerg Roedelbeed2822015-05-28 18:41:34 +0200346 start = ALIGN(entry->start, pg_size);
347 end = ALIGN(entry->start + entry->length, pg_size);
348
349 for (addr = start; addr < end; addr += pg_size) {
350 phys_addr_t phys_addr;
351
352 phys_addr = iommu_iova_to_phys(domain, addr);
353 if (phys_addr)
354 continue;
355
356 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
357 if (ret)
358 goto out;
359 }
360
361 }
362
363out:
364 iommu_put_dm_regions(dev, &mappings);
365
366 return ret;
367}
368
Alex Williamsond72e31c2012-05-30 14:18:53 -0600369/**
370 * iommu_group_add_device - add a device to an iommu group
371 * @group: the group into which to add the device (reference should be held)
372 * @dev: the device
373 *
374 * This function is called by an iommu driver to add a device into a
375 * group. Adding a device increments the group reference count.
376 */
377int iommu_group_add_device(struct iommu_group *group, struct device *dev)
378{
379 int ret, i = 0;
380 struct iommu_device *device;
381
382 device = kzalloc(sizeof(*device), GFP_KERNEL);
383 if (!device)
384 return -ENOMEM;
385
386 device->dev = dev;
387
388 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
389 if (ret) {
390 kfree(device);
391 return ret;
392 }
393
394 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
395rename:
396 if (!device->name) {
397 sysfs_remove_link(&dev->kobj, "iommu_group");
398 kfree(device);
399 return -ENOMEM;
400 }
401
402 ret = sysfs_create_link_nowarn(group->devices_kobj,
403 &dev->kobj, device->name);
404 if (ret) {
405 kfree(device->name);
406 if (ret == -EEXIST && i >= 0) {
407 /*
408 * Account for the slim chance of collision
409 * and append an instance to the name.
410 */
411 device->name = kasprintf(GFP_KERNEL, "%s.%d",
412 kobject_name(&dev->kobj), i++);
413 goto rename;
414 }
415
416 sysfs_remove_link(&dev->kobj, "iommu_group");
417 kfree(device);
418 return ret;
419 }
420
421 kobject_get(group->devices_kobj);
422
423 dev->iommu_group = group;
424
Joerg Roedelbeed2822015-05-28 18:41:34 +0200425 iommu_group_create_direct_mappings(group, dev);
426
Alex Williamsond72e31c2012-05-30 14:18:53 -0600427 mutex_lock(&group->mutex);
428 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200429 if (group->domain)
430 __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600431 mutex_unlock(&group->mutex);
432
433 /* Notify any listeners about change to group. */
434 blocking_notifier_call_chain(&group->notifier,
435 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600436
437 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200438
439 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
440
Alex Williamsond72e31c2012-05-30 14:18:53 -0600441 return 0;
442}
443EXPORT_SYMBOL_GPL(iommu_group_add_device);
444
445/**
446 * iommu_group_remove_device - remove a device from it's current group
447 * @dev: device to be removed
448 *
449 * This function is called by an iommu driver to remove the device from
450 * it's current group. This decrements the iommu group reference count.
451 */
452void iommu_group_remove_device(struct device *dev)
453{
454 struct iommu_group *group = dev->iommu_group;
455 struct iommu_device *tmp_device, *device = NULL;
456
Joerg Roedel269aa802015-05-28 18:41:25 +0200457 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
458
Alex Williamsond72e31c2012-05-30 14:18:53 -0600459 /* Pre-notify listeners that a device is being removed. */
460 blocking_notifier_call_chain(&group->notifier,
461 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
462
463 mutex_lock(&group->mutex);
464 list_for_each_entry(tmp_device, &group->devices, list) {
465 if (tmp_device->dev == dev) {
466 device = tmp_device;
467 list_del(&device->list);
468 break;
469 }
470 }
471 mutex_unlock(&group->mutex);
472
473 if (!device)
474 return;
475
476 sysfs_remove_link(group->devices_kobj, device->name);
477 sysfs_remove_link(&dev->kobj, "iommu_group");
478
Shuah Khan2e757082013-08-15 11:59:25 -0600479 trace_remove_device_from_group(group->id, dev);
480
Alex Williamsond72e31c2012-05-30 14:18:53 -0600481 kfree(device->name);
482 kfree(device);
483 dev->iommu_group = NULL;
484 kobject_put(group->devices_kobj);
485}
486EXPORT_SYMBOL_GPL(iommu_group_remove_device);
487
Joerg Roedel426a2732015-05-28 18:41:30 +0200488static int iommu_group_device_count(struct iommu_group *group)
489{
490 struct iommu_device *entry;
491 int ret = 0;
492
493 list_for_each_entry(entry, &group->devices, list)
494 ret++;
495
496 return ret;
497}
498
Alex Williamsond72e31c2012-05-30 14:18:53 -0600499/**
500 * iommu_group_for_each_dev - iterate over each device in the group
501 * @group: the group
502 * @data: caller opaque data to be passed to callback function
503 * @fn: caller supplied callback function
504 *
505 * This function is called by group users to iterate over group devices.
506 * Callers should hold a reference count to the group during callback.
507 * The group->mutex is held across callbacks, which will block calls to
508 * iommu_group_add/remove_device.
509 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200510static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
511 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600512{
513 struct iommu_device *device;
514 int ret = 0;
515
Alex Williamsond72e31c2012-05-30 14:18:53 -0600516 list_for_each_entry(device, &group->devices, list) {
517 ret = fn(device->dev, data);
518 if (ret)
519 break;
520 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200521 return ret;
522}
523
524
525int iommu_group_for_each_dev(struct iommu_group *group, void *data,
526 int (*fn)(struct device *, void *))
527{
528 int ret;
529
530 mutex_lock(&group->mutex);
531 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600532 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200533
Alex Williamsond72e31c2012-05-30 14:18:53 -0600534 return ret;
535}
536EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
537
538/**
539 * iommu_group_get - Return the group for a device and increment reference
540 * @dev: get the group that this device belongs to
541 *
542 * This function is called by iommu drivers and users to get the group
543 * for the specified device. If found, the group is returned and the group
544 * reference in incremented, else NULL.
545 */
546struct iommu_group *iommu_group_get(struct device *dev)
547{
548 struct iommu_group *group = dev->iommu_group;
549
550 if (group)
551 kobject_get(group->devices_kobj);
552
553 return group;
554}
555EXPORT_SYMBOL_GPL(iommu_group_get);
556
557/**
558 * iommu_group_put - Decrement group reference
559 * @group: the group to use
560 *
561 * This function is called by iommu drivers and users to release the
562 * iommu group. Once the reference count is zero, the group is released.
563 */
564void iommu_group_put(struct iommu_group *group)
565{
566 if (group)
567 kobject_put(group->devices_kobj);
568}
569EXPORT_SYMBOL_GPL(iommu_group_put);
570
571/**
572 * iommu_group_register_notifier - Register a notifier for group changes
573 * @group: the group to watch
574 * @nb: notifier block to signal
575 *
576 * This function allows iommu group users to track changes in a group.
577 * See include/linux/iommu.h for actions sent via this notifier. Caller
578 * should hold a reference to the group throughout notifier registration.
579 */
580int iommu_group_register_notifier(struct iommu_group *group,
581 struct notifier_block *nb)
582{
583 return blocking_notifier_chain_register(&group->notifier, nb);
584}
585EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
586
587/**
588 * iommu_group_unregister_notifier - Unregister a notifier
589 * @group: the group to watch
590 * @nb: notifier block to signal
591 *
592 * Unregister a previously registered group notifier block.
593 */
594int iommu_group_unregister_notifier(struct iommu_group *group,
595 struct notifier_block *nb)
596{
597 return blocking_notifier_chain_unregister(&group->notifier, nb);
598}
599EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
600
601/**
602 * iommu_group_id - Return ID for a group
603 * @group: the group to ID
604 *
605 * Return the unique ID for the group matching the sysfs group number.
606 */
607int iommu_group_id(struct iommu_group *group)
608{
609 return group->id;
610}
611EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400612
Alex Williamsonf096c062014-09-19 10:03:06 -0600613static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
614 unsigned long *devfns);
615
Alex Williamson104a1c12014-07-03 09:51:18 -0600616/*
617 * To consider a PCI device isolated, we require ACS to support Source
618 * Validation, Request Redirection, Completer Redirection, and Upstream
619 * Forwarding. This effectively means that devices cannot spoof their
620 * requester ID, requests and completions cannot be redirected, and all
621 * transactions are forwarded upstream, even as it passes through a
622 * bridge where the target device is downstream.
623 */
624#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
625
Alex Williamsonf096c062014-09-19 10:03:06 -0600626/*
627 * For multifunction devices which are not isolated from each other, find
628 * all the other non-isolated functions and look for existing groups. For
629 * each function, we also need to look for aliases to or from other devices
630 * that may already have a group.
631 */
632static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
633 unsigned long *devfns)
634{
635 struct pci_dev *tmp = NULL;
636 struct iommu_group *group;
637
638 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
639 return NULL;
640
641 for_each_pci_dev(tmp) {
642 if (tmp == pdev || tmp->bus != pdev->bus ||
643 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
644 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
645 continue;
646
647 group = get_pci_alias_group(tmp, devfns);
648 if (group) {
649 pci_dev_put(tmp);
650 return group;
651 }
652 }
653
654 return NULL;
655}
656
657/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100658 * Look for aliases to or from the given device for existing groups. DMA
659 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600660 * space is quite small (especially since we're really only looking at pcie
661 * device, and therefore only expect multiple slots on the root complex or
662 * downstream switch ports). It's conceivable though that a pair of
663 * multifunction devices could have aliases between them that would cause a
664 * loop. To prevent this, we use a bitmap to track where we've been.
665 */
666static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
667 unsigned long *devfns)
668{
669 struct pci_dev *tmp = NULL;
670 struct iommu_group *group;
671
672 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
673 return NULL;
674
675 group = iommu_group_get(&pdev->dev);
676 if (group)
677 return group;
678
679 for_each_pci_dev(tmp) {
680 if (tmp == pdev || tmp->bus != pdev->bus)
681 continue;
682
683 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100684 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600685 group = get_pci_alias_group(tmp, devfns);
686 if (group) {
687 pci_dev_put(tmp);
688 return group;
689 }
690
691 group = get_pci_function_alias_group(tmp, devfns);
692 if (group) {
693 pci_dev_put(tmp);
694 return group;
695 }
696 }
697 }
698
699 return NULL;
700}
701
Alex Williamson104a1c12014-07-03 09:51:18 -0600702struct group_for_pci_data {
703 struct pci_dev *pdev;
704 struct iommu_group *group;
705};
706
707/*
708 * DMA alias iterator callback, return the last seen device. Stop and return
709 * the IOMMU group if we find one along the way.
710 */
711static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
712{
713 struct group_for_pci_data *data = opaque;
714
715 data->pdev = pdev;
716 data->group = iommu_group_get(&pdev->dev);
717
718 return data->group != NULL;
719}
720
721/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200722 * Generic device_group call-back function. It just allocates one
723 * iommu-group per device.
724 */
725struct iommu_group *generic_device_group(struct device *dev)
726{
727 struct iommu_group *group;
728
729 group = iommu_group_alloc();
730 if (IS_ERR(group))
731 return NULL;
732
733 return group;
734}
735
736/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600737 * Use standard PCI bus topology, isolation features, and DMA alias quirks
738 * to find or create an IOMMU group for a device.
739 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200740struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600741{
Joerg Roedel5e622922015-10-21 23:51:37 +0200742 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600743 struct group_for_pci_data data;
744 struct pci_bus *bus;
745 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600746 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600747
Joerg Roedel5e622922015-10-21 23:51:37 +0200748 if (WARN_ON(!dev_is_pci(dev)))
749 return ERR_PTR(-EINVAL);
750
Alex Williamson104a1c12014-07-03 09:51:18 -0600751 /*
752 * Find the upstream DMA alias for the device. A device must not
753 * be aliased due to topology in order to have its own IOMMU group.
754 * If we find an alias along the way that already belongs to a
755 * group, use it.
756 */
757 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
758 return data.group;
759
760 pdev = data.pdev;
761
762 /*
763 * Continue upstream from the point of minimum IOMMU granularity
764 * due to aliases to the point where devices are protected from
765 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
766 * group, use it.
767 */
768 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
769 if (!bus->self)
770 continue;
771
772 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
773 break;
774
775 pdev = bus->self;
776
777 group = iommu_group_get(&pdev->dev);
778 if (group)
779 return group;
780 }
781
782 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600783 * Look for existing groups on device aliases. If we alias another
784 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600785 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600786 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
787 if (group)
788 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600789
790 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600791 * Look for existing groups on non-isolated functions on the same
792 * slot and aliases of those funcions, if any. No need to clear
793 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600794 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600795 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
796 if (group)
797 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600798
799 /* No shared group found, allocate new */
Joerg Roedel53723dc2015-05-28 18:41:29 +0200800 group = iommu_group_alloc();
Dan Carpenter409e5532015-06-10 13:59:27 +0300801 if (IS_ERR(group))
802 return NULL;
803
Joerg Roedel53723dc2015-05-28 18:41:29 +0200804 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600805}
806
807/**
808 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
809 * @dev: target device
810 *
811 * This function is intended to be called by IOMMU drivers and extended to
812 * support common, bus-defined algorithms when determining or creating the
813 * IOMMU group for a device. On success, the caller will hold a reference
814 * to the returned IOMMU group, which will already include the provided
815 * device. The reference should be released with iommu_group_put().
816 */
817struct iommu_group *iommu_group_get_for_dev(struct device *dev)
818{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200819 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200820 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600821 int ret;
822
823 group = iommu_group_get(dev);
824 if (group)
825 return group;
826
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200827 group = ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200828
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200829 if (ops && ops->device_group)
830 group = ops->device_group(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600831
832 if (IS_ERR(group))
833 return group;
834
Joerg Roedel12282362015-10-21 23:51:43 +0200835 /*
836 * Try to allocate a default domain - needs support from the
837 * IOMMU driver.
838 */
839 if (!group->default_domain) {
840 group->default_domain = __iommu_domain_alloc(dev->bus,
841 IOMMU_DOMAIN_DMA);
Joerg Roedeleebb8032016-04-04 15:47:48 +0200842 if (!group->domain)
843 group->domain = group->default_domain;
Joerg Roedel12282362015-10-21 23:51:43 +0200844 }
845
Alex Williamson104a1c12014-07-03 09:51:18 -0600846 ret = iommu_group_add_device(group, dev);
847 if (ret) {
848 iommu_group_put(group);
849 return ERR_PTR(ret);
850 }
851
852 return group;
853}
854
Joerg Roedel6827ca82015-05-28 18:41:35 +0200855struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
856{
857 return group->default_domain;
858}
859
Alex Williamson14604322011-10-21 15:56:05 -0400860static int add_iommu_group(struct device *dev, void *data)
861{
Thierry Redingb22f6432014-06-27 09:03:12 +0200862 struct iommu_callback_data *cb = data;
863 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +0200864 int ret;
Alex Williamson14604322011-10-21 15:56:05 -0400865
Alex Williamsond72e31c2012-05-30 14:18:53 -0600866 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +0000867 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600868
869 WARN_ON(dev->iommu_group);
870
Joerg Roedel38667f12015-06-29 10:16:08 +0200871 ret = ops->add_device(dev);
872
873 /*
874 * We ignore -ENODEV errors for now, as they just mean that the
875 * device is not translated by an IOMMU. We still care about
876 * other errors and fail to initialize when they happen.
877 */
878 if (ret == -ENODEV)
879 ret = 0;
880
881 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400882}
883
Joerg Roedel8da30142015-05-28 18:41:27 +0200884static int remove_iommu_group(struct device *dev, void *data)
885{
886 struct iommu_callback_data *cb = data;
887 const struct iommu_ops *ops = cb->ops;
888
889 if (ops->remove_device && dev->iommu_group)
890 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400891
892 return 0;
893}
894
Alex Williamsond72e31c2012-05-30 14:18:53 -0600895static int iommu_bus_notifier(struct notifier_block *nb,
896 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400897{
898 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +0200899 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600900 struct iommu_group *group;
901 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400902
Alex Williamsond72e31c2012-05-30 14:18:53 -0600903 /*
904 * ADD/DEL call into iommu driver ops if provided, which may
905 * result in ADD/DEL notifiers to group->notifier
906 */
907 if (action == BUS_NOTIFY_ADD_DEVICE) {
908 if (ops->add_device)
909 return ops->add_device(dev);
Joerg Roedel843cb6d2015-05-28 18:41:28 +0200910 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600911 if (ops->remove_device && dev->iommu_group) {
912 ops->remove_device(dev);
913 return 0;
914 }
915 }
Alex Williamson14604322011-10-21 15:56:05 -0400916
Alex Williamsond72e31c2012-05-30 14:18:53 -0600917 /*
918 * Remaining BUS_NOTIFYs get filtered and republished to the
919 * group, if anyone is listening
920 */
921 group = iommu_group_get(dev);
922 if (!group)
923 return 0;
924
925 switch (action) {
926 case BUS_NOTIFY_BIND_DRIVER:
927 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
928 break;
929 case BUS_NOTIFY_BOUND_DRIVER:
930 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
931 break;
932 case BUS_NOTIFY_UNBIND_DRIVER:
933 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
934 break;
935 case BUS_NOTIFY_UNBOUND_DRIVER:
936 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
937 break;
938 }
939
940 if (group_action)
941 blocking_notifier_call_chain(&group->notifier,
942 group_action, dev);
943
944 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400945 return 0;
946}
947
Mark Salterfb3e3062014-09-21 13:58:24 -0400948static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100949{
Mark Salterfb3e3062014-09-21 13:58:24 -0400950 int err;
951 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +0200952 struct iommu_callback_data cb = {
953 .ops = ops,
954 };
955
Mark Salterfb3e3062014-09-21 13:58:24 -0400956 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
957 if (!nb)
958 return -ENOMEM;
959
960 nb->notifier_call = iommu_bus_notifier;
961
962 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +0200963 if (err)
964 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100965
966 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +0200967 if (err)
968 goto out_err;
969
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100970
971 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +0200972
973out_err:
974 /* Clean up */
975 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
976 bus_unregister_notifier(bus, nb);
977
978out_free:
979 kfree(nb);
980
981 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100982}
983
Joerg Roedelff217762011-08-26 16:48:26 +0200984/**
985 * bus_set_iommu - set iommu-callbacks for the bus
986 * @bus: bus.
987 * @ops: the callbacks provided by the iommu-driver
988 *
989 * This function is called by an iommu driver to set the iommu methods
990 * used for a particular bus. Drivers for devices on that bus can use
991 * the iommu-api after these ops are registered.
992 * This special function is needed because IOMMUs are usually devices on
993 * the bus itself, so the iommu drivers are not initialized when the bus
994 * is set up. With this function the iommu-driver can set the iommu-ops
995 * afterwards.
996 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200997int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100998{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100999 int err;
1000
Joerg Roedelff217762011-08-26 16:48:26 +02001001 if (bus->iommu_ops != NULL)
1002 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001003
Joerg Roedelff217762011-08-26 16:48:26 +02001004 bus->iommu_ops = ops;
1005
1006 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001007 err = iommu_bus_init(bus, ops);
1008 if (err)
1009 bus->iommu_ops = NULL;
1010
1011 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001012}
Joerg Roedelff217762011-08-26 16:48:26 +02001013EXPORT_SYMBOL_GPL(bus_set_iommu);
1014
Joerg Roedela1b60c12011-09-06 18:46:34 +02001015bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001016{
Joerg Roedel94441c32011-09-06 18:58:54 +02001017 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001018}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001019EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001020
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001021bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1022{
1023 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1024 return false;
1025
1026 return bus->iommu_ops->capable(cap);
1027}
1028EXPORT_SYMBOL_GPL(iommu_capable);
1029
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001030/**
1031 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1032 * @domain: iommu domain
1033 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001034 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001035 *
1036 * This function should be used by IOMMU users which want to be notified
1037 * whenever an IOMMU fault happens.
1038 *
1039 * The fault handler itself should return 0 on success, and an appropriate
1040 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001041 */
1042void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001043 iommu_fault_handler_t handler,
1044 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001045{
1046 BUG_ON(!domain);
1047
1048 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001049 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001050}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001051EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001052
Joerg Roedel53723dc2015-05-28 18:41:29 +02001053static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1054 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001055{
1056 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001057
Joerg Roedel94441c32011-09-06 18:58:54 +02001058 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001059 return NULL;
1060
Joerg Roedel53723dc2015-05-28 18:41:29 +02001061 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001062 if (!domain)
1063 return NULL;
1064
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001065 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001066 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001067 /* Assume all sizes by default; the driver may override this later */
1068 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Joerg Roedel905d66c2011-09-06 16:03:26 +02001069
Susheel Khianie66aa5b2015-08-25 17:25:42 +05301070 iommu_debug_domain_add(domain);
1071
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001072 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001073}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001074
Joerg Roedel53723dc2015-05-28 18:41:29 +02001075struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1076{
1077 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001078}
1079EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1080
1081void iommu_domain_free(struct iommu_domain *domain)
1082{
Susheel Khianie66aa5b2015-08-25 17:25:42 +05301083 iommu_debug_domain_remove(domain);
Joerg Roedel89be34a2015-03-26 13:43:19 +01001084 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001085}
1086EXPORT_SYMBOL_GPL(iommu_domain_free);
1087
Joerg Roedel426a2732015-05-28 18:41:30 +02001088static int __iommu_attach_device(struct iommu_domain *domain,
1089 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001090{
Shuah Khanb54db772013-08-15 11:59:26 -06001091 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001092 if (unlikely(domain->ops->attach_dev == NULL))
1093 return -ENODEV;
1094
Shuah Khanb54db772013-08-15 11:59:26 -06001095 ret = domain->ops->attach_dev(domain, dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001096 if (!ret) {
Shuah Khanb54db772013-08-15 11:59:26 -06001097 trace_attach_device_to_domain(dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001098 iommu_debug_attach_device(domain, dev);
1099 }
Shuah Khanb54db772013-08-15 11:59:26 -06001100 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001101}
Joerg Roedel426a2732015-05-28 18:41:30 +02001102
1103int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1104{
1105 struct iommu_group *group;
1106 int ret;
1107
1108 group = iommu_group_get(dev);
1109 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1110 if (group == NULL)
1111 return __iommu_attach_device(domain, dev);
1112
1113 /*
1114 * We have a group - lock it to make sure the device-count doesn't
1115 * change while we are attaching
1116 */
1117 mutex_lock(&group->mutex);
1118 ret = -EINVAL;
1119 if (iommu_group_device_count(group) != 1)
1120 goto out_unlock;
1121
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001122 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001123
1124out_unlock:
1125 mutex_unlock(&group->mutex);
1126 iommu_group_put(group);
1127
1128 return ret;
1129}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001130EXPORT_SYMBOL_GPL(iommu_attach_device);
1131
Joerg Roedel426a2732015-05-28 18:41:30 +02001132static void __iommu_detach_device(struct iommu_domain *domain,
1133 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001134{
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001135 iommu_debug_detach_device(domain, dev);
1136
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001137 if (unlikely(domain->ops->detach_dev == NULL))
1138 return;
1139
1140 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001141 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001142}
Joerg Roedel426a2732015-05-28 18:41:30 +02001143
1144void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1145{
1146 struct iommu_group *group;
1147
1148 group = iommu_group_get(dev);
1149 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1150 if (group == NULL)
1151 return __iommu_detach_device(domain, dev);
1152
1153 mutex_lock(&group->mutex);
1154 if (iommu_group_device_count(group) != 1) {
1155 WARN_ON(1);
1156 goto out_unlock;
1157 }
1158
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001159 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001160
1161out_unlock:
1162 mutex_unlock(&group->mutex);
1163 iommu_group_put(group);
1164}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001165EXPORT_SYMBOL_GPL(iommu_detach_device);
1166
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001167struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1168{
1169 struct iommu_domain *domain;
1170 struct iommu_group *group;
1171
1172 group = iommu_group_get(dev);
1173 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1174 if (group == NULL)
1175 return NULL;
1176
1177 domain = group->domain;
1178
1179 iommu_group_put(group);
1180
1181 return domain;
1182}
1183EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1184
Alex Williamsond72e31c2012-05-30 14:18:53 -06001185/*
1186 * IOMMU groups are really the natrual working unit of the IOMMU, but
1187 * the IOMMU API works on domains and devices. Bridge that gap by
1188 * iterating over the devices in a group. Ideally we'd have a single
1189 * device which represents the requestor ID of the group, but we also
1190 * allow IOMMU drivers to create policy defined minimum sets, where
1191 * the physical hardware may be able to distiguish members, but we
1192 * wish to group them at a higher level (ex. untrusted multi-function
1193 * PCI devices). Thus we attach each device.
1194 */
1195static int iommu_group_do_attach_device(struct device *dev, void *data)
1196{
1197 struct iommu_domain *domain = data;
1198
Joerg Roedel426a2732015-05-28 18:41:30 +02001199 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001200}
1201
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001202static int __iommu_attach_group(struct iommu_domain *domain,
1203 struct iommu_group *group)
1204{
1205 int ret;
1206
1207 if (group->default_domain && group->domain != group->default_domain)
1208 return -EBUSY;
1209
1210 ret = __iommu_group_for_each_dev(group, domain,
1211 iommu_group_do_attach_device);
1212 if (ret == 0)
1213 group->domain = domain;
1214
1215 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001216}
1217
1218int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1219{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001220 int ret;
1221
1222 mutex_lock(&group->mutex);
1223 ret = __iommu_attach_group(domain, group);
1224 mutex_unlock(&group->mutex);
1225
1226 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001227}
1228EXPORT_SYMBOL_GPL(iommu_attach_group);
1229
1230static int iommu_group_do_detach_device(struct device *dev, void *data)
1231{
1232 struct iommu_domain *domain = data;
1233
Joerg Roedel426a2732015-05-28 18:41:30 +02001234 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001235
1236 return 0;
1237}
1238
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001239static void __iommu_detach_group(struct iommu_domain *domain,
1240 struct iommu_group *group)
1241{
1242 int ret;
1243
1244 if (!group->default_domain) {
1245 __iommu_group_for_each_dev(group, domain,
1246 iommu_group_do_detach_device);
1247 group->domain = NULL;
1248 return;
1249 }
1250
1251 if (group->domain == group->default_domain)
1252 return;
1253
1254 /* Detach by re-attaching to the default domain */
1255 ret = __iommu_group_for_each_dev(group, group->default_domain,
1256 iommu_group_do_attach_device);
1257 if (ret != 0)
1258 WARN_ON(1);
1259 else
1260 group->domain = group->default_domain;
1261}
1262
Alex Williamsond72e31c2012-05-30 14:18:53 -06001263void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1264{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001265 mutex_lock(&group->mutex);
1266 __iommu_detach_group(domain, group);
1267 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001268}
1269EXPORT_SYMBOL_GPL(iommu_detach_group);
1270
Varun Sethibb5547ac2013-03-29 01:23:58 +05301271phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001272{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001273 if (unlikely(domain->ops->iova_to_phys == NULL))
1274 return 0;
1275
1276 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001277}
1278EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001279
Mitchel Humpherys36b8c322015-07-06 15:24:22 -07001280phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
1281 dma_addr_t iova)
1282{
1283 if (unlikely(domain->ops->iova_to_phys_hard == NULL))
1284 return 0;
1285
1286 return domain->ops->iova_to_phys_hard(domain, iova);
1287}
1288
Liam Mark5bb64222016-12-09 14:36:07 -08001289bool iommu_is_iova_coherent(struct iommu_domain *domain, dma_addr_t iova)
1290{
1291 if (unlikely(domain->ops->is_iova_coherent == NULL))
1292 return 0;
1293
1294 return domain->ops->is_iova_coherent(domain, iova);
1295}
1296
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001297size_t iommu_pgsize(unsigned long pgsize_bitmap,
1298 unsigned long addr_merge, size_t size)
Alex Williamsonbd139692013-06-17 19:57:34 -06001299{
1300 unsigned int pgsize_idx;
1301 size_t pgsize;
1302
1303 /* Max page size that still fits into 'size' */
1304 pgsize_idx = __fls(size);
1305
1306 /* need to consider alignment requirements ? */
1307 if (likely(addr_merge)) {
1308 /* Max page size allowed by address */
1309 unsigned int align_pgsize_idx = __ffs(addr_merge);
1310 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1311 }
1312
1313 /* build a mask of acceptable page sizes */
1314 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1315
1316 /* throw away page sizes not supported by the hardware */
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001317 pgsize &= pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001318
1319 /* make sure we're still sane */
Mitchel Humpherys76e77fb2015-05-12 09:44:32 -07001320 if (!pgsize) {
1321 pr_err("invalid pgsize/addr/size! 0x%lx 0x%lx 0x%zx\n",
1322 pgsize_bitmap, addr_merge, size);
1323 BUG();
1324 }
Alex Williamsonbd139692013-06-17 19:57:34 -06001325
1326 /* pick the biggest page */
1327 pgsize_idx = __fls(pgsize);
1328 pgsize = 1UL << pgsize_idx;
1329
1330 return pgsize;
1331}
1332
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001333int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001334 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001335{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001336 unsigned long orig_iova = iova;
1337 unsigned int min_pagesz;
1338 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001339 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001340 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001341
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001342 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001343 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001344 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001345
Joerg Roedela10315e2015-03-26 13:43:06 +01001346 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1347 return -EINVAL;
1348
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001349 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001350 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001351
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001352 /*
1353 * both the virtual address and the physical one, as well as
1354 * the size of the mapping, must be aligned (at least) to the
1355 * size of the smallest page supported by the hardware
1356 */
1357 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001358 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001359 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001360 return -EINVAL;
1361 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001362
Fabio Estevamabedb042013-08-22 10:25:42 -03001363 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001364
1365 while (size) {
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001366 size_t pgsize = iommu_pgsize(domain->pgsize_bitmap,
1367 iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001368
Fabio Estevamabedb042013-08-22 10:25:42 -03001369 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001370 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001371
1372 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1373 if (ret)
1374 break;
1375
1376 iova += pgsize;
1377 paddr += pgsize;
1378 size -= pgsize;
1379 }
1380
1381 /* unroll mapping in case something went wrong */
1382 if (ret)
1383 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001384 else
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001385 trace_map(orig_iova, orig_paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001386
1387 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001388}
1389EXPORT_SYMBOL_GPL(iommu_map);
1390
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001391size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001392{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001393 size_t unmapped_page, unmapped = 0;
1394 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001395 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001396
Joerg Roedel57886512013-01-29 13:41:09 +01001397 if (unlikely(domain->ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001398 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001399 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001400
Joerg Roedela10315e2015-03-26 13:43:06 +01001401 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1402 return -EINVAL;
1403
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001404 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001405 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001406
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001407 /*
1408 * The virtual address, as well as the size of the mapping, must be
1409 * aligned (at least) to the size of the smallest page supported
1410 * by the hardware
1411 */
1412 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001413 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1414 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001415 return -EINVAL;
1416 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001417
Joe Perches6197ca82013-06-23 12:29:04 -07001418 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001419
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001420 /*
1421 * Keep iterating until we either unmap 'size' bytes (or more)
1422 * or we hit an area that isn't mapped.
1423 */
1424 while (unmapped < size) {
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001425 size_t left = size - unmapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001426
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001427 unmapped_page = domain->ops->unmap(domain, iova, left);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001428 if (!unmapped_page)
1429 break;
1430
Joe Perches6197ca82013-06-23 12:29:04 -07001431 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1432 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001433
1434 iova += unmapped_page;
1435 unmapped += unmapped_page;
1436 }
1437
Shuah Khandb8614d2015-01-16 20:53:17 -07001438 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001439 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001440}
1441EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001442
Olav Haugan315786e2014-10-25 09:55:16 -07001443size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1444 struct scatterlist *sg, unsigned int nents, int prot)
1445{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001446 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001447 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001448 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001449 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001450
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001451 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001452 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001453
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001454 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001455
1456 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001457 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001458
1459 /*
1460 * We are mapping on IOMMU page boundaries, so offset within
1461 * the page must be 0. However, the IOMMU may support pages
1462 * smaller than PAGE_SIZE, so s->offset may still represent
1463 * an offset of that boundary within the CPU page.
1464 */
1465 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001466 goto out_err;
1467
1468 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1469 if (ret)
1470 goto out_err;
1471
1472 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001473 }
1474
1475 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001476
1477out_err:
1478 /* undo mappings already done */
1479 iommu_unmap(domain, iova, mapped);
1480
1481 return 0;
1482
Olav Haugan315786e2014-10-25 09:55:16 -07001483}
1484EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001485
1486int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301487 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001488{
1489 if (unlikely(domain->ops->domain_window_enable == NULL))
1490 return -ENODEV;
1491
Varun Sethi80f97f02013-03-29 01:24:00 +05301492 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1493 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001494}
1495EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1496
1497void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1498{
1499 if (unlikely(domain->ops->domain_window_disable == NULL))
1500 return;
1501
1502 return domain->ops->domain_window_disable(domain, wnd_nr);
1503}
1504EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1505
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001506struct dentry *iommu_debugfs_top;
1507
Alex Williamsond72e31c2012-05-30 14:18:53 -06001508static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001509{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001510 iommu_group_kset = kset_create_and_add("iommu_groups",
1511 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001512 BUG_ON(!iommu_group_kset);
1513
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001514 iommu_debugfs_top = debugfs_create_dir("iommu", NULL);
1515 if (!iommu_debugfs_top) {
1516 pr_err("Couldn't create iommu debugfs directory\n");
1517 return -ENODEV;
1518 }
1519
Alex Williamsond72e31c2012-05-30 14:18:53 -06001520 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001521}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001522core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001523
1524int iommu_domain_get_attr(struct iommu_domain *domain,
1525 enum iommu_attr attr, void *data)
1526{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001527 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001528 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001529 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001530 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001531
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001532 switch (attr) {
1533 case DOMAIN_ATTR_GEOMETRY:
1534 geometry = data;
1535 *geometry = domain->geometry;
1536
1537 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001538 case DOMAIN_ATTR_PAGING:
1539 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001540 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001541 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001542 case DOMAIN_ATTR_WINDOWS:
1543 count = data;
1544
1545 if (domain->ops->domain_get_windows != NULL)
1546 *count = domain->ops->domain_get_windows(domain);
1547 else
1548 ret = -ENODEV;
1549
1550 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001551 default:
1552 if (!domain->ops->domain_get_attr)
1553 return -EINVAL;
1554
1555 ret = domain->ops->domain_get_attr(domain, attr, data);
1556 }
1557
1558 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001559}
1560EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1561
1562int iommu_domain_set_attr(struct iommu_domain *domain,
1563 enum iommu_attr attr, void *data)
1564{
Joerg Roedel69356712013-02-04 14:00:01 +01001565 int ret = 0;
1566 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001567
Joerg Roedel69356712013-02-04 14:00:01 +01001568 switch (attr) {
1569 case DOMAIN_ATTR_WINDOWS:
1570 count = data;
1571
1572 if (domain->ops->domain_set_windows != NULL)
1573 ret = domain->ops->domain_set_windows(domain, *count);
1574 else
1575 ret = -ENODEV;
1576
1577 break;
1578 default:
1579 if (domain->ops->domain_set_attr == NULL)
1580 return -EINVAL;
1581
1582 ret = domain->ops->domain_set_attr(domain, attr, data);
1583 }
1584
1585 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001586}
1587EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001588
Mitchel Humpherys8488df22015-07-09 16:59:02 -07001589/**
1590 * iommu_trigger_fault() - trigger an IOMMU fault
1591 * @domain: iommu domain
1592 *
1593 * Triggers a fault on the device to which this domain is attached.
1594 *
1595 * This function should only be used for debugging purposes, for obvious
1596 * reasons.
1597 */
1598void iommu_trigger_fault(struct iommu_domain *domain, unsigned long flags)
1599{
1600 if (domain->ops->trigger_fault)
1601 domain->ops->trigger_fault(domain, flags);
1602}
1603
Mitchel Humpherys3ede5d92015-08-21 14:06:14 -07001604/**
1605 * iommu_reg_read() - read an IOMMU register
1606 *
1607 * Reads the IOMMU register at the given offset.
1608 */
1609unsigned long iommu_reg_read(struct iommu_domain *domain, unsigned long offset)
1610{
1611 if (domain->ops->reg_read)
1612 return domain->ops->reg_read(domain, offset);
1613 return 0;
1614}
1615
1616/**
1617 * iommu_reg_write() - write an IOMMU register
1618 *
1619 * Writes the given value to the IOMMU register at the given offset.
1620 */
1621void iommu_reg_write(struct iommu_domain *domain, unsigned long offset,
1622 unsigned long val)
1623{
1624 if (domain->ops->reg_write)
1625 domain->ops->reg_write(domain, offset, val);
1626}
1627
Joerg Roedela1015c22015-05-28 18:41:33 +02001628void iommu_get_dm_regions(struct device *dev, struct list_head *list)
1629{
1630 const struct iommu_ops *ops = dev->bus->iommu_ops;
1631
1632 if (ops && ops->get_dm_regions)
1633 ops->get_dm_regions(dev, list);
1634}
1635
1636void iommu_put_dm_regions(struct device *dev, struct list_head *list)
1637{
1638 const struct iommu_ops *ops = dev->bus->iommu_ops;
1639
1640 if (ops && ops->put_dm_regions)
1641 ops->put_dm_regions(dev, list);
1642}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001643
1644/* Request that a device is direct mapped by the IOMMU */
1645int iommu_request_dm_for_dev(struct device *dev)
1646{
1647 struct iommu_domain *dm_domain;
1648 struct iommu_group *group;
1649 int ret;
1650
1651 /* Device must already be in a group before calling this function */
1652 group = iommu_group_get_for_dev(dev);
Dan Carpenter409e5532015-06-10 13:59:27 +03001653 if (IS_ERR(group))
1654 return PTR_ERR(group);
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001655
1656 mutex_lock(&group->mutex);
1657
1658 /* Check if the default domain is already direct mapped */
1659 ret = 0;
1660 if (group->default_domain &&
1661 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1662 goto out;
1663
1664 /* Don't change mappings of existing devices */
1665 ret = -EBUSY;
1666 if (iommu_group_device_count(group) != 1)
1667 goto out;
1668
1669 /* Allocate a direct mapped domain */
1670 ret = -ENOMEM;
1671 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1672 if (!dm_domain)
1673 goto out;
1674
1675 /* Attach the device to the domain */
1676 ret = __iommu_attach_group(dm_domain, group);
1677 if (ret) {
1678 iommu_domain_free(dm_domain);
1679 goto out;
1680 }
1681
1682 /* Make the direct mapped domain the default for this group */
1683 if (group->default_domain)
1684 iommu_domain_free(group->default_domain);
1685 group->default_domain = dm_domain;
1686
1687 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1688
1689 ret = 0;
1690out:
1691 mutex_unlock(&group->mutex);
1692 iommu_group_put(group);
1693
1694 return ret;
1695}
Robin Murphy57f98d22016-09-13 10:54:14 +01001696
1697int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1698 const struct iommu_ops *ops)
1699{
1700 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1701
1702 if (fwspec)
1703 return ops == fwspec->ops ? 0 : -EINVAL;
1704
1705 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1706 if (!fwspec)
1707 return -ENOMEM;
1708
1709 of_node_get(to_of_node(iommu_fwnode));
1710 fwspec->iommu_fwnode = iommu_fwnode;
1711 fwspec->ops = ops;
1712 dev->iommu_fwspec = fwspec;
1713 return 0;
1714}
1715EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1716
1717void iommu_fwspec_free(struct device *dev)
1718{
1719 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1720
1721 if (fwspec) {
1722 fwnode_handle_put(fwspec->iommu_fwnode);
1723 kfree(fwspec);
1724 dev->iommu_fwspec = NULL;
1725 }
1726}
1727EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1728
1729int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1730{
1731 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1732 size_t size;
1733 int i;
1734
1735 if (!fwspec)
1736 return -EINVAL;
1737
1738 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1739 if (size > sizeof(*fwspec)) {
1740 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1741 if (!fwspec)
1742 return -ENOMEM;
1743 }
1744
1745 for (i = 0; i < num_ids; i++)
1746 fwspec->ids[fwspec->num_ids + i] = ids[i];
1747
1748 fwspec->num_ids += num_ids;
1749 dev->iommu_fwspec = fwspec;
1750 return 0;
1751}
1752EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Patrick Daly1e3c16c2017-04-12 17:59:07 -07001753
1754/*
1755 * Return the id asoociated with a pci device.
1756 */
1757int iommu_fwspec_get_id(struct device *dev, u32 *id)
1758{
1759 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1760
1761 if (!fwspec)
1762 return -EINVAL;
1763
1764 if (!dev_is_pci(dev))
1765 return -EINVAL;
1766
1767 if (fwspec->num_ids != 1)
1768 return -EINVAL;
1769
1770 *id = fwspec->ids[0];
1771 return 0;
1772}