blob: c7c7da7397449a78ca729bb725b1b97f4f14db56 [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");
Robin Murphyd7fcb302017-01-16 12:58:07 +0000389 if (ret)
390 goto err_free_device;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600391
392 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
393rename:
394 if (!device->name) {
Robin Murphyd7fcb302017-01-16 12:58:07 +0000395 ret = -ENOMEM;
396 goto err_remove_link;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600397 }
398
399 ret = sysfs_create_link_nowarn(group->devices_kobj,
400 &dev->kobj, device->name);
401 if (ret) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600402 if (ret == -EEXIST && i >= 0) {
403 /*
404 * Account for the slim chance of collision
405 * and append an instance to the name.
406 */
Robin Murphyd7fcb302017-01-16 12:58:07 +0000407 kfree(device->name);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600408 device->name = kasprintf(GFP_KERNEL, "%s.%d",
409 kobject_name(&dev->kobj), i++);
410 goto rename;
411 }
Robin Murphyd7fcb302017-01-16 12:58:07 +0000412 goto err_free_name;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600413 }
414
415 kobject_get(group->devices_kobj);
416
417 dev->iommu_group = group;
418
Joerg Roedelbeed2822015-05-28 18:41:34 +0200419 iommu_group_create_direct_mappings(group, dev);
420
Alex Williamsond72e31c2012-05-30 14:18:53 -0600421 mutex_lock(&group->mutex);
422 list_add_tail(&device->list, &group->devices);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200423 if (group->domain)
Robin Murphyd7fcb302017-01-16 12:58:07 +0000424 ret = __iommu_attach_device(group->domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600425 mutex_unlock(&group->mutex);
Robin Murphyd7fcb302017-01-16 12:58:07 +0000426 if (ret)
427 goto err_put_group;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600428
Liam Mark599835a2015-06-05 17:10:00 -0700429
Alex Williamsond72e31c2012-05-30 14:18:53 -0600430 /* Notify any listeners about change to group. */
431 blocking_notifier_call_chain(&group->notifier,
432 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600433
434 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200435
436 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
437
Alex Williamsond72e31c2012-05-30 14:18:53 -0600438 return 0;
Robin Murphyd7fcb302017-01-16 12:58:07 +0000439
440err_put_group:
441 mutex_lock(&group->mutex);
442 list_del(&device->list);
443 mutex_unlock(&group->mutex);
444 dev->iommu_group = NULL;
445 kobject_put(group->devices_kobj);
Jon Derrickc664cdb2019-12-31 13:24:19 -0700446 sysfs_remove_link(group->devices_kobj, device->name);
Robin Murphyd7fcb302017-01-16 12:58:07 +0000447err_free_name:
448 kfree(device->name);
449err_remove_link:
450 sysfs_remove_link(&dev->kobj, "iommu_group");
451err_free_device:
452 kfree(device);
453 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
454 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600455}
456EXPORT_SYMBOL_GPL(iommu_group_add_device);
457
458/**
459 * iommu_group_remove_device - remove a device from it's current group
460 * @dev: device to be removed
461 *
462 * This function is called by an iommu driver to remove the device from
463 * it's current group. This decrements the iommu group reference count.
464 */
465void iommu_group_remove_device(struct device *dev)
466{
467 struct iommu_group *group = dev->iommu_group;
468 struct iommu_device *tmp_device, *device = NULL;
469
Joerg Roedel269aa802015-05-28 18:41:25 +0200470 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
471
Alex Williamsond72e31c2012-05-30 14:18:53 -0600472 /* Pre-notify listeners that a device is being removed. */
473 blocking_notifier_call_chain(&group->notifier,
474 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
475
476 mutex_lock(&group->mutex);
477 list_for_each_entry(tmp_device, &group->devices, list) {
478 if (tmp_device->dev == dev) {
479 device = tmp_device;
480 list_del(&device->list);
481 break;
482 }
483 }
484 mutex_unlock(&group->mutex);
485
486 if (!device)
487 return;
488
489 sysfs_remove_link(group->devices_kobj, device->name);
490 sysfs_remove_link(&dev->kobj, "iommu_group");
491
Shuah Khan2e757082013-08-15 11:59:25 -0600492 trace_remove_device_from_group(group->id, dev);
493
Alex Williamsond72e31c2012-05-30 14:18:53 -0600494 kfree(device->name);
495 kfree(device);
496 dev->iommu_group = NULL;
497 kobject_put(group->devices_kobj);
498}
499EXPORT_SYMBOL_GPL(iommu_group_remove_device);
500
Joerg Roedel426a2732015-05-28 18:41:30 +0200501static int iommu_group_device_count(struct iommu_group *group)
502{
503 struct iommu_device *entry;
504 int ret = 0;
505
506 list_for_each_entry(entry, &group->devices, list)
507 ret++;
508
509 return ret;
510}
511
Alex Williamsond72e31c2012-05-30 14:18:53 -0600512/**
513 * iommu_group_for_each_dev - iterate over each device in the group
514 * @group: the group
515 * @data: caller opaque data to be passed to callback function
516 * @fn: caller supplied callback function
517 *
518 * This function is called by group users to iterate over group devices.
519 * Callers should hold a reference count to the group during callback.
520 * The group->mutex is held across callbacks, which will block calls to
521 * iommu_group_add/remove_device.
522 */
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200523static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
524 int (*fn)(struct device *, void *))
Alex Williamsond72e31c2012-05-30 14:18:53 -0600525{
526 struct iommu_device *device;
527 int ret = 0;
528
Alex Williamsond72e31c2012-05-30 14:18:53 -0600529 list_for_each_entry(device, &group->devices, list) {
530 ret = fn(device->dev, data);
531 if (ret)
532 break;
533 }
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200534 return ret;
535}
536
537
538int iommu_group_for_each_dev(struct iommu_group *group, void *data,
539 int (*fn)(struct device *, void *))
540{
541 int ret;
542
543 mutex_lock(&group->mutex);
544 ret = __iommu_group_for_each_dev(group, data, fn);
Alex Williamsond72e31c2012-05-30 14:18:53 -0600545 mutex_unlock(&group->mutex);
Joerg Roedele39cb8a2015-05-28 18:41:31 +0200546
Alex Williamsond72e31c2012-05-30 14:18:53 -0600547 return ret;
548}
549EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
550
551/**
552 * iommu_group_get - Return the group for a device and increment reference
553 * @dev: get the group that this device belongs to
554 *
555 * This function is called by iommu drivers and users to get the group
556 * for the specified device. If found, the group is returned and the group
557 * reference in incremented, else NULL.
558 */
559struct iommu_group *iommu_group_get(struct device *dev)
560{
561 struct iommu_group *group = dev->iommu_group;
562
563 if (group)
564 kobject_get(group->devices_kobj);
565
566 return group;
567}
568EXPORT_SYMBOL_GPL(iommu_group_get);
569
570/**
571 * iommu_group_put - Decrement group reference
572 * @group: the group to use
573 *
574 * This function is called by iommu drivers and users to release the
575 * iommu group. Once the reference count is zero, the group is released.
576 */
577void iommu_group_put(struct iommu_group *group)
578{
579 if (group)
580 kobject_put(group->devices_kobj);
581}
582EXPORT_SYMBOL_GPL(iommu_group_put);
583
584/**
585 * iommu_group_register_notifier - Register a notifier for group changes
586 * @group: the group to watch
587 * @nb: notifier block to signal
588 *
589 * This function allows iommu group users to track changes in a group.
590 * See include/linux/iommu.h for actions sent via this notifier. Caller
591 * should hold a reference to the group throughout notifier registration.
592 */
593int iommu_group_register_notifier(struct iommu_group *group,
594 struct notifier_block *nb)
595{
596 return blocking_notifier_chain_register(&group->notifier, nb);
597}
598EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
599
600/**
601 * iommu_group_unregister_notifier - Unregister a notifier
602 * @group: the group to watch
603 * @nb: notifier block to signal
604 *
605 * Unregister a previously registered group notifier block.
606 */
607int iommu_group_unregister_notifier(struct iommu_group *group,
608 struct notifier_block *nb)
609{
610 return blocking_notifier_chain_unregister(&group->notifier, nb);
611}
612EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
613
614/**
615 * iommu_group_id - Return ID for a group
616 * @group: the group to ID
617 *
618 * Return the unique ID for the group matching the sysfs group number.
619 */
620int iommu_group_id(struct iommu_group *group)
621{
622 return group->id;
623}
624EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400625
Alex Williamsonf096c062014-09-19 10:03:06 -0600626static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
627 unsigned long *devfns);
628
Alex Williamson104a1c12014-07-03 09:51:18 -0600629/*
630 * To consider a PCI device isolated, we require ACS to support Source
631 * Validation, Request Redirection, Completer Redirection, and Upstream
632 * Forwarding. This effectively means that devices cannot spoof their
633 * requester ID, requests and completions cannot be redirected, and all
634 * transactions are forwarded upstream, even as it passes through a
635 * bridge where the target device is downstream.
636 */
637#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
638
Alex Williamsonf096c062014-09-19 10:03:06 -0600639/*
640 * For multifunction devices which are not isolated from each other, find
641 * all the other non-isolated functions and look for existing groups. For
642 * each function, we also need to look for aliases to or from other devices
643 * that may already have a group.
644 */
645static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
646 unsigned long *devfns)
647{
648 struct pci_dev *tmp = NULL;
649 struct iommu_group *group;
650
651 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
652 return NULL;
653
654 for_each_pci_dev(tmp) {
655 if (tmp == pdev || tmp->bus != pdev->bus ||
656 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
657 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
658 continue;
659
660 group = get_pci_alias_group(tmp, devfns);
661 if (group) {
662 pci_dev_put(tmp);
663 return group;
664 }
665 }
666
667 return NULL;
668}
669
670/*
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100671 * Look for aliases to or from the given device for existing groups. DMA
672 * aliases are only supported on the same bus, therefore the search
Alex Williamsonf096c062014-09-19 10:03:06 -0600673 * space is quite small (especially since we're really only looking at pcie
674 * device, and therefore only expect multiple slots on the root complex or
675 * downstream switch ports). It's conceivable though that a pair of
676 * multifunction devices could have aliases between them that would cause a
677 * loop. To prevent this, we use a bitmap to track where we've been.
678 */
679static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
680 unsigned long *devfns)
681{
682 struct pci_dev *tmp = NULL;
683 struct iommu_group *group;
684
685 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
686 return NULL;
687
688 group = iommu_group_get(&pdev->dev);
689 if (group)
690 return group;
691
692 for_each_pci_dev(tmp) {
693 if (tmp == pdev || tmp->bus != pdev->bus)
694 continue;
695
696 /* We alias them or they alias us */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +0100697 if (pci_devs_are_dma_aliases(pdev, tmp)) {
Alex Williamsonf096c062014-09-19 10:03:06 -0600698 group = get_pci_alias_group(tmp, devfns);
699 if (group) {
700 pci_dev_put(tmp);
701 return group;
702 }
703
704 group = get_pci_function_alias_group(tmp, devfns);
705 if (group) {
706 pci_dev_put(tmp);
707 return group;
708 }
709 }
710 }
711
712 return NULL;
713}
714
Alex Williamson104a1c12014-07-03 09:51:18 -0600715struct group_for_pci_data {
716 struct pci_dev *pdev;
717 struct iommu_group *group;
718};
719
720/*
721 * DMA alias iterator callback, return the last seen device. Stop and return
722 * the IOMMU group if we find one along the way.
723 */
724static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
725{
726 struct group_for_pci_data *data = opaque;
727
728 data->pdev = pdev;
729 data->group = iommu_group_get(&pdev->dev);
730
731 return data->group != NULL;
732}
733
734/*
Joerg Roedel6eab5562015-10-21 23:51:38 +0200735 * Generic device_group call-back function. It just allocates one
736 * iommu-group per device.
737 */
738struct iommu_group *generic_device_group(struct device *dev)
739{
740 struct iommu_group *group;
741
742 group = iommu_group_alloc();
743 if (IS_ERR(group))
744 return NULL;
745
746 return group;
747}
748
749/*
Alex Williamson104a1c12014-07-03 09:51:18 -0600750 * Use standard PCI bus topology, isolation features, and DMA alias quirks
751 * to find or create an IOMMU group for a device.
752 */
Joerg Roedel5e622922015-10-21 23:51:37 +0200753struct iommu_group *pci_device_group(struct device *dev)
Alex Williamson104a1c12014-07-03 09:51:18 -0600754{
Joerg Roedel5e622922015-10-21 23:51:37 +0200755 struct pci_dev *pdev = to_pci_dev(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600756 struct group_for_pci_data data;
757 struct pci_bus *bus;
758 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600759 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600760
Joerg Roedel5e622922015-10-21 23:51:37 +0200761 if (WARN_ON(!dev_is_pci(dev)))
762 return ERR_PTR(-EINVAL);
763
Alex Williamson104a1c12014-07-03 09:51:18 -0600764 /*
765 * Find the upstream DMA alias for the device. A device must not
766 * be aliased due to topology in order to have its own IOMMU group.
767 * If we find an alias along the way that already belongs to a
768 * group, use it.
769 */
770 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
771 return data.group;
772
773 pdev = data.pdev;
774
775 /*
776 * Continue upstream from the point of minimum IOMMU granularity
777 * due to aliases to the point where devices are protected from
778 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
779 * group, use it.
780 */
781 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
782 if (!bus->self)
783 continue;
784
785 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
786 break;
787
788 pdev = bus->self;
789
790 group = iommu_group_get(&pdev->dev);
791 if (group)
792 return group;
793 }
794
795 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600796 * Look for existing groups on device aliases. If we alias another
797 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600798 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600799 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
800 if (group)
801 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600802
803 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600804 * Look for existing groups on non-isolated functions on the same
805 * slot and aliases of those funcions, if any. No need to clear
806 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600807 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600808 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
809 if (group)
810 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600811
812 /* No shared group found, allocate new */
Joerg Roedel53723dc2015-05-28 18:41:29 +0200813 group = iommu_group_alloc();
Dan Carpenter409e5532015-06-10 13:59:27 +0300814 if (IS_ERR(group))
815 return NULL;
816
Joerg Roedel53723dc2015-05-28 18:41:29 +0200817 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600818}
819
820/**
821 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
822 * @dev: target device
823 *
824 * This function is intended to be called by IOMMU drivers and extended to
825 * support common, bus-defined algorithms when determining or creating the
826 * IOMMU group for a device. On success, the caller will hold a reference
827 * to the returned IOMMU group, which will already include the provided
828 * device. The reference should be released with iommu_group_put().
829 */
830struct iommu_group *iommu_group_get_for_dev(struct device *dev)
831{
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200832 const struct iommu_ops *ops = dev->bus->iommu_ops;
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200833 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600834 int ret;
835
836 group = iommu_group_get(dev);
837 if (group)
838 return group;
839
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200840 group = ERR_PTR(-EINVAL);
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200841
Joerg Roedel46c6b2b2015-10-21 23:51:36 +0200842 if (ops && ops->device_group)
843 group = ops->device_group(dev);
Alex Williamson104a1c12014-07-03 09:51:18 -0600844
845 if (IS_ERR(group))
846 return group;
847
Joerg Roedel12282362015-10-21 23:51:43 +0200848 /*
849 * Try to allocate a default domain - needs support from the
850 * IOMMU driver.
851 */
852 if (!group->default_domain) {
853 group->default_domain = __iommu_domain_alloc(dev->bus,
854 IOMMU_DOMAIN_DMA);
Joerg Roedeleebb8032016-04-04 15:47:48 +0200855 if (!group->domain)
856 group->domain = group->default_domain;
Joerg Roedel12282362015-10-21 23:51:43 +0200857 }
858
Alex Williamson104a1c12014-07-03 09:51:18 -0600859 ret = iommu_group_add_device(group, dev);
860 if (ret) {
861 iommu_group_put(group);
862 return ERR_PTR(ret);
863 }
864
865 return group;
866}
867
Joerg Roedel6827ca82015-05-28 18:41:35 +0200868struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
869{
870 return group->default_domain;
871}
872
Alex Williamson14604322011-10-21 15:56:05 -0400873static int add_iommu_group(struct device *dev, void *data)
874{
Thierry Redingb22f6432014-06-27 09:03:12 +0200875 struct iommu_callback_data *cb = data;
876 const struct iommu_ops *ops = cb->ops;
Joerg Roedel38667f12015-06-29 10:16:08 +0200877 int ret;
Alex Williamson14604322011-10-21 15:56:05 -0400878
Alex Williamsond72e31c2012-05-30 14:18:53 -0600879 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +0000880 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600881
882 WARN_ON(dev->iommu_group);
883
Joerg Roedel38667f12015-06-29 10:16:08 +0200884 ret = ops->add_device(dev);
885
886 /*
887 * We ignore -ENODEV errors for now, as they just mean that the
888 * device is not translated by an IOMMU. We still care about
889 * other errors and fail to initialize when they happen.
890 */
891 if (ret == -ENODEV)
892 ret = 0;
893
894 return ret;
Alex Williamson14604322011-10-21 15:56:05 -0400895}
896
Joerg Roedel8da30142015-05-28 18:41:27 +0200897static int remove_iommu_group(struct device *dev, void *data)
898{
899 struct iommu_callback_data *cb = data;
900 const struct iommu_ops *ops = cb->ops;
901
902 if (ops->remove_device && dev->iommu_group)
903 ops->remove_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400904
905 return 0;
906}
907
Alex Williamsond72e31c2012-05-30 14:18:53 -0600908static int iommu_bus_notifier(struct notifier_block *nb,
909 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400910{
911 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +0200912 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600913 struct iommu_group *group;
914 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400915
Alex Williamsond72e31c2012-05-30 14:18:53 -0600916 /*
917 * ADD/DEL call into iommu driver ops if provided, which may
918 * result in ADD/DEL notifiers to group->notifier
919 */
920 if (action == BUS_NOTIFY_ADD_DEVICE) {
921 if (ops->add_device)
922 return ops->add_device(dev);
Joerg Roedel843cb6d2015-05-28 18:41:28 +0200923 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600924 if (ops->remove_device && dev->iommu_group) {
925 ops->remove_device(dev);
926 return 0;
927 }
928 }
Alex Williamson14604322011-10-21 15:56:05 -0400929
Alex Williamsond72e31c2012-05-30 14:18:53 -0600930 /*
931 * Remaining BUS_NOTIFYs get filtered and republished to the
932 * group, if anyone is listening
933 */
934 group = iommu_group_get(dev);
935 if (!group)
936 return 0;
937
938 switch (action) {
939 case BUS_NOTIFY_BIND_DRIVER:
940 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
941 break;
942 case BUS_NOTIFY_BOUND_DRIVER:
943 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
944 break;
945 case BUS_NOTIFY_UNBIND_DRIVER:
946 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
947 break;
948 case BUS_NOTIFY_UNBOUND_DRIVER:
949 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
950 break;
951 }
952
953 if (group_action)
954 blocking_notifier_call_chain(&group->notifier,
955 group_action, dev);
956
957 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400958 return 0;
959}
960
Mark Salterfb3e3062014-09-21 13:58:24 -0400961static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100962{
Mark Salterfb3e3062014-09-21 13:58:24 -0400963 int err;
964 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +0200965 struct iommu_callback_data cb = {
966 .ops = ops,
967 };
968
Mark Salterfb3e3062014-09-21 13:58:24 -0400969 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
970 if (!nb)
971 return -ENOMEM;
972
973 nb->notifier_call = iommu_bus_notifier;
974
975 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +0200976 if (err)
977 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100978
979 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +0200980 if (err)
981 goto out_err;
982
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100983
984 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +0200985
986out_err:
987 /* Clean up */
988 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
989 bus_unregister_notifier(bus, nb);
990
991out_free:
992 kfree(nb);
993
994 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100995}
996
Joerg Roedelff217762011-08-26 16:48:26 +0200997/**
998 * bus_set_iommu - set iommu-callbacks for the bus
999 * @bus: bus.
1000 * @ops: the callbacks provided by the iommu-driver
1001 *
1002 * This function is called by an iommu driver to set the iommu methods
1003 * used for a particular bus. Drivers for devices on that bus can use
1004 * the iommu-api after these ops are registered.
1005 * This special function is needed because IOMMUs are usually devices on
1006 * the bus itself, so the iommu drivers are not initialized when the bus
1007 * is set up. With this function the iommu-driver can set the iommu-ops
1008 * afterwards.
1009 */
Thierry Redingb22f6432014-06-27 09:03:12 +02001010int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001011{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001012 int err;
1013
Joerg Roedelff217762011-08-26 16:48:26 +02001014 if (bus->iommu_ops != NULL)
1015 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001016
Joerg Roedelff217762011-08-26 16:48:26 +02001017 bus->iommu_ops = ops;
1018
1019 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +01001020 err = iommu_bus_init(bus, ops);
1021 if (err)
1022 bus->iommu_ops = NULL;
1023
1024 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001025}
Joerg Roedelff217762011-08-26 16:48:26 +02001026EXPORT_SYMBOL_GPL(bus_set_iommu);
1027
Joerg Roedela1b60c12011-09-06 18:46:34 +02001028bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001029{
Joerg Roedel94441c32011-09-06 18:58:54 +02001030 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001031}
Joerg Roedela1b60c12011-09-06 18:46:34 +02001032EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001033
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +02001034bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1035{
1036 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1037 return false;
1038
1039 return bus->iommu_ops->capable(cap);
1040}
1041EXPORT_SYMBOL_GPL(iommu_capable);
1042
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001043/**
1044 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1045 * @domain: iommu domain
1046 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001047 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -04001048 *
1049 * This function should be used by IOMMU users which want to be notified
1050 * whenever an IOMMU fault happens.
1051 *
1052 * The fault handler itself should return 0 on success, and an appropriate
1053 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001054 */
1055void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001056 iommu_fault_handler_t handler,
1057 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001058{
1059 BUG_ON(!domain);
1060
1061 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +03001062 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001063}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -04001064EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -04001065
Joerg Roedel53723dc2015-05-28 18:41:29 +02001066static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1067 unsigned type)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001068{
1069 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001070
Joerg Roedel94441c32011-09-06 18:58:54 +02001071 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +02001072 return NULL;
1073
Joerg Roedel53723dc2015-05-28 18:41:29 +02001074 domain = bus->iommu_ops->domain_alloc(type);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001075 if (!domain)
1076 return NULL;
1077
Joerg Roedel8539c7c2015-03-26 13:43:05 +01001078 domain->ops = bus->iommu_ops;
Joerg Roedel53723dc2015-05-28 18:41:29 +02001079 domain->type = type;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001080 /* Assume all sizes by default; the driver may override this later */
1081 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
Liam Mark599835a2015-06-05 17:10:00 -07001082 memset(domain->name, 0, IOMMU_DOMAIN_NAME_LEN);
Joerg Roedel905d66c2011-09-06 16:03:26 +02001083
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001084 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001085}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001086
Joerg Roedel53723dc2015-05-28 18:41:29 +02001087struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1088{
1089 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001090}
1091EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1092
1093void iommu_domain_free(struct iommu_domain *domain)
1094{
Susheel Khianie66aa5b2015-08-25 17:25:42 +05301095 iommu_debug_domain_remove(domain);
Joerg Roedel89be34a2015-03-26 13:43:19 +01001096 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001097}
1098EXPORT_SYMBOL_GPL(iommu_domain_free);
1099
Joerg Roedel426a2732015-05-28 18:41:30 +02001100static int __iommu_attach_device(struct iommu_domain *domain,
1101 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001102{
Shuah Khanb54db772013-08-15 11:59:26 -06001103 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001104 if (unlikely(domain->ops->attach_dev == NULL))
1105 return -ENODEV;
1106
Shuah Khanb54db772013-08-15 11:59:26 -06001107 ret = domain->ops->attach_dev(domain, dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001108 if (!ret) {
Shuah Khanb54db772013-08-15 11:59:26 -06001109 trace_attach_device_to_domain(dev);
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001110 iommu_debug_attach_device(domain, dev);
Liam Mark599835a2015-06-05 17:10:00 -07001111
1112 if (!strnlen(domain->name, IOMMU_DOMAIN_NAME_LEN)) {
1113 strlcpy(domain->name, dev_name(dev),
1114 IOMMU_DOMAIN_NAME_LEN);
1115 }
Mitchel Humpherys42296fb2015-06-23 16:29:16 -07001116 }
Shuah Khanb54db772013-08-15 11:59:26 -06001117 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001118}
Joerg Roedel426a2732015-05-28 18:41:30 +02001119
1120int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1121{
1122 struct iommu_group *group;
1123 int ret;
1124
1125 group = iommu_group_get(dev);
1126 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1127 if (group == NULL)
1128 return __iommu_attach_device(domain, dev);
1129
1130 /*
1131 * We have a group - lock it to make sure the device-count doesn't
1132 * change while we are attaching
1133 */
1134 mutex_lock(&group->mutex);
1135 ret = -EINVAL;
1136 if (iommu_group_device_count(group) != 1)
1137 goto out_unlock;
1138
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001139 ret = __iommu_attach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001140
1141out_unlock:
1142 mutex_unlock(&group->mutex);
1143 iommu_group_put(group);
1144
1145 return ret;
1146}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001147EXPORT_SYMBOL_GPL(iommu_attach_device);
1148
Joerg Roedel426a2732015-05-28 18:41:30 +02001149static void __iommu_detach_device(struct iommu_domain *domain,
1150 struct device *dev)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001151{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001152 if (unlikely(domain->ops->detach_dev == NULL))
1153 return;
1154
1155 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -06001156 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001157}
Joerg Roedel426a2732015-05-28 18:41:30 +02001158
1159void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1160{
1161 struct iommu_group *group;
1162
1163 group = iommu_group_get(dev);
1164 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1165 if (group == NULL)
1166 return __iommu_detach_device(domain, dev);
1167
1168 mutex_lock(&group->mutex);
1169 if (iommu_group_device_count(group) != 1) {
1170 WARN_ON(1);
1171 goto out_unlock;
1172 }
1173
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001174 __iommu_detach_group(domain, group);
Joerg Roedel426a2732015-05-28 18:41:30 +02001175
1176out_unlock:
1177 mutex_unlock(&group->mutex);
1178 iommu_group_put(group);
1179}
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001180EXPORT_SYMBOL_GPL(iommu_detach_device);
1181
Joerg Roedel2c1296d2015-05-28 18:41:32 +02001182struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1183{
1184 struct iommu_domain *domain;
1185 struct iommu_group *group;
1186
1187 group = iommu_group_get(dev);
1188 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1189 if (group == NULL)
1190 return NULL;
1191
1192 domain = group->domain;
1193
1194 iommu_group_put(group);
1195
1196 return domain;
1197}
1198EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1199
Alex Williamsond72e31c2012-05-30 14:18:53 -06001200/*
1201 * IOMMU groups are really the natrual working unit of the IOMMU, but
1202 * the IOMMU API works on domains and devices. Bridge that gap by
1203 * iterating over the devices in a group. Ideally we'd have a single
1204 * device which represents the requestor ID of the group, but we also
1205 * allow IOMMU drivers to create policy defined minimum sets, where
1206 * the physical hardware may be able to distiguish members, but we
1207 * wish to group them at a higher level (ex. untrusted multi-function
1208 * PCI devices). Thus we attach each device.
1209 */
1210static int iommu_group_do_attach_device(struct device *dev, void *data)
1211{
1212 struct iommu_domain *domain = data;
1213
Joerg Roedel426a2732015-05-28 18:41:30 +02001214 return __iommu_attach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001215}
1216
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001217static int __iommu_attach_group(struct iommu_domain *domain,
1218 struct iommu_group *group)
1219{
1220 int ret;
1221
1222 if (group->default_domain && group->domain != group->default_domain)
1223 return -EBUSY;
1224
1225 ret = __iommu_group_for_each_dev(group, domain,
1226 iommu_group_do_attach_device);
1227 if (ret == 0)
1228 group->domain = domain;
1229
1230 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001231}
1232
1233int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1234{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001235 int ret;
1236
1237 mutex_lock(&group->mutex);
1238 ret = __iommu_attach_group(domain, group);
1239 mutex_unlock(&group->mutex);
1240
1241 return ret;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001242}
1243EXPORT_SYMBOL_GPL(iommu_attach_group);
1244
1245static int iommu_group_do_detach_device(struct device *dev, void *data)
1246{
1247 struct iommu_domain *domain = data;
1248
Joerg Roedel426a2732015-05-28 18:41:30 +02001249 __iommu_detach_device(domain, dev);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001250
1251 return 0;
1252}
1253
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001254static void __iommu_detach_group(struct iommu_domain *domain,
1255 struct iommu_group *group)
1256{
1257 int ret;
1258
1259 if (!group->default_domain) {
1260 __iommu_group_for_each_dev(group, domain,
1261 iommu_group_do_detach_device);
1262 group->domain = NULL;
1263 return;
1264 }
1265
1266 if (group->domain == group->default_domain)
1267 return;
1268
1269 /* Detach by re-attaching to the default domain */
1270 ret = __iommu_group_for_each_dev(group, group->default_domain,
1271 iommu_group_do_attach_device);
1272 if (ret != 0)
1273 WARN_ON(1);
1274 else
1275 group->domain = group->default_domain;
1276}
1277
Alex Williamsond72e31c2012-05-30 14:18:53 -06001278void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1279{
Joerg Roedele39cb8a2015-05-28 18:41:31 +02001280 mutex_lock(&group->mutex);
1281 __iommu_detach_group(domain, group);
1282 mutex_unlock(&group->mutex);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001283}
1284EXPORT_SYMBOL_GPL(iommu_detach_group);
1285
Varun Sethibb5547ac2013-03-29 01:23:58 +05301286phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001287{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001288 if (unlikely(domain->ops->iova_to_phys == NULL))
1289 return 0;
1290
1291 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001292}
1293EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001294
Mitchel Humpherys36b8c322015-07-06 15:24:22 -07001295phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain,
1296 dma_addr_t iova)
1297{
1298 if (unlikely(domain->ops->iova_to_phys_hard == NULL))
1299 return 0;
1300
1301 return domain->ops->iova_to_phys_hard(domain, iova);
1302}
1303
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -07001304uint64_t iommu_iova_to_pte(struct iommu_domain *domain,
1305 dma_addr_t iova)
1306{
1307 if (unlikely(domain->ops->iova_to_pte == NULL))
1308 return 0;
1309
1310 return domain->ops->iova_to_pte(domain, iova);
1311}
1312
Liam Mark5bb64222016-12-09 14:36:07 -08001313bool iommu_is_iova_coherent(struct iommu_domain *domain, dma_addr_t iova)
1314{
1315 if (unlikely(domain->ops->is_iova_coherent == NULL))
1316 return 0;
1317
1318 return domain->ops->is_iova_coherent(domain, iova);
1319}
1320
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001321size_t iommu_pgsize(unsigned long pgsize_bitmap,
1322 unsigned long addr_merge, size_t size)
Alex Williamsonbd139692013-06-17 19:57:34 -06001323{
1324 unsigned int pgsize_idx;
1325 size_t pgsize;
1326
1327 /* Max page size that still fits into 'size' */
1328 pgsize_idx = __fls(size);
1329
1330 /* need to consider alignment requirements ? */
1331 if (likely(addr_merge)) {
1332 /* Max page size allowed by address */
1333 unsigned int align_pgsize_idx = __ffs(addr_merge);
1334 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1335 }
1336
1337 /* build a mask of acceptable page sizes */
1338 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1339
1340 /* throw away page sizes not supported by the hardware */
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001341 pgsize &= pgsize_bitmap;
Alex Williamsonbd139692013-06-17 19:57:34 -06001342
1343 /* make sure we're still sane */
Mitchel Humpherys76e77fb2015-05-12 09:44:32 -07001344 if (!pgsize) {
1345 pr_err("invalid pgsize/addr/size! 0x%lx 0x%lx 0x%zx\n",
1346 pgsize_bitmap, addr_merge, size);
1347 BUG();
1348 }
Alex Williamsonbd139692013-06-17 19:57:34 -06001349
1350 /* pick the biggest page */
1351 pgsize_idx = __fls(pgsize);
1352 pgsize = 1UL << pgsize_idx;
1353
1354 return pgsize;
1355}
1356
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001357int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001358 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001359{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001360 unsigned long orig_iova = iova;
1361 unsigned int min_pagesz;
1362 size_t orig_size = size;
Yoshihiro Shimoda06bfcaa2016-02-10 10:18:04 +09001363 phys_addr_t orig_paddr = paddr;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001364 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001365
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001366 if (unlikely(domain->ops->map == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001367 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001368 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001369
Joerg Roedela10315e2015-03-26 13:43:06 +01001370 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1371 return -EINVAL;
1372
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001373 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001374 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001375
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001376 /*
1377 * both the virtual address and the physical one, as well as
1378 * the size of the mapping, must be aligned (at least) to the
1379 * size of the smallest page supported by the hardware
1380 */
1381 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001382 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001383 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001384 return -EINVAL;
1385 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001386
Fabio Estevamabedb042013-08-22 10:25:42 -03001387 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001388
1389 while (size) {
Mitchel Humpherysfad25922015-05-01 17:13:21 -07001390 size_t pgsize = iommu_pgsize(domain->pgsize_bitmap,
1391 iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001392
Fabio Estevamabedb042013-08-22 10:25:42 -03001393 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001394 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001395
1396 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1397 if (ret)
1398 break;
1399
1400 iova += pgsize;
1401 paddr += pgsize;
1402 size -= pgsize;
1403 }
1404
1405 /* unroll mapping in case something went wrong */
1406 if (ret)
1407 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001408 else
Liam Mark599835a2015-06-05 17:10:00 -07001409 trace_map(domain, orig_iova, orig_paddr, orig_size, prot);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001410
1411 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001412}
1413EXPORT_SYMBOL_GPL(iommu_map);
1414
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001415size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001416{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001417 size_t unmapped_page, unmapped = 0;
1418 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001419 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001420
Joerg Roedel57886512013-01-29 13:41:09 +01001421 if (unlikely(domain->ops->unmap == NULL ||
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001422 domain->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001423 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001424
Joerg Roedela10315e2015-03-26 13:43:06 +01001425 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1426 return -EINVAL;
1427
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001428 /* find out the minimum page size supported */
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001429 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001430
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001431 /*
1432 * The virtual address, as well as the size of the mapping, must be
1433 * aligned (at least) to the size of the smallest page supported
1434 * by the hardware
1435 */
1436 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001437 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1438 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001439 return -EINVAL;
1440 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001441
Joe Perches6197ca82013-06-23 12:29:04 -07001442 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001443
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001444 /*
1445 * Keep iterating until we either unmap 'size' bytes (or more)
1446 * or we hit an area that isn't mapped.
1447 */
1448 while (unmapped < size) {
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001449 size_t left = size - unmapped;
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001450
Mitchel Humpherys5f92f322015-04-30 09:49:29 -07001451 unmapped_page = domain->ops->unmap(domain, iova, left);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001452 if (!unmapped_page)
1453 break;
1454
Joe Perches6197ca82013-06-23 12:29:04 -07001455 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1456 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001457
1458 iova += unmapped_page;
1459 unmapped += unmapped_page;
1460 }
1461
Liam Mark599835a2015-06-05 17:10:00 -07001462 trace_unmap(domain, orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001463 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001464}
1465EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001466
Liam Mark599835a2015-06-05 17:10:00 -07001467size_t iommu_map_sg(struct iommu_domain *domain,
1468 unsigned long iova, struct scatterlist *sg,
1469 unsigned int nents, int prot)
1470{
1471 size_t mapped;
1472
1473 mapped = domain->ops->map_sg(domain, iova, sg, nents, prot);
1474 trace_map_sg(domain, iova, mapped, prot);
1475 return mapped;
1476}
1477EXPORT_SYMBOL(iommu_map_sg);
1478
Olav Haugan315786e2014-10-25 09:55:16 -07001479size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1480 struct scatterlist *sg, unsigned int nents, int prot)
1481{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001482 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001483 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001484 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001485 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001486
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001487 if (unlikely(domain->pgsize_bitmap == 0UL))
Robin Murphy18f23402014-11-25 17:50:55 +00001488 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001489
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001490 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
Robin Murphy18f23402014-11-25 17:50:55 +00001491
1492 for_each_sg(sg, s, nents, i) {
Dan Williams3e6110f2015-12-15 12:54:06 -08001493 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
Robin Murphy18f23402014-11-25 17:50:55 +00001494
1495 /*
1496 * We are mapping on IOMMU page boundaries, so offset within
1497 * the page must be 0. However, the IOMMU may support pages
1498 * smaller than PAGE_SIZE, so s->offset may still represent
1499 * an offset of that boundary within the CPU page.
1500 */
1501 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001502 goto out_err;
1503
1504 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1505 if (ret)
1506 goto out_err;
1507
1508 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001509 }
1510
1511 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001512
1513out_err:
1514 /* undo mappings already done */
1515 iommu_unmap(domain, iova, mapped);
1516
1517 return 0;
1518
Olav Haugan315786e2014-10-25 09:55:16 -07001519}
1520EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001521
1522int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301523 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001524{
1525 if (unlikely(domain->ops->domain_window_enable == NULL))
1526 return -ENODEV;
1527
Varun Sethi80f97f02013-03-29 01:24:00 +05301528 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1529 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001530}
1531EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1532
1533void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1534{
1535 if (unlikely(domain->ops->domain_window_disable == NULL))
1536 return;
1537
1538 return domain->ops->domain_window_disable(domain, wnd_nr);
1539}
1540EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1541
Joerg Roedel2f4ae01d2017-04-26 15:39:28 +02001542/**
1543 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1544 * @domain: the iommu domain where the fault has happened
1545 * @dev: the device where the fault has happened
1546 * @iova: the faulting address
1547 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1548 *
1549 * This function should be called by the low-level IOMMU implementations
1550 * whenever IOMMU faults happen, to allow high-level users, that are
1551 * interested in such events, to know about them.
1552 *
1553 * This event may be useful for several possible use cases:
1554 * - mere logging of the event
1555 * - dynamic TLB/PTE loading
1556 * - if restarting of the faulting device is required
1557 *
1558 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1559 * PTE/TLB loading will one day be supported, implementations will be able
1560 * to tell whether it succeeded or not according to this return value).
1561 *
1562 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1563 * (though fault handlers can also return -ENOSYS, in case they want to
1564 * elicit the default behavior of the IOMMU drivers).
1565 */
1566int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1567 unsigned long iova, int flags)
1568{
1569 int ret = -ENOSYS;
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001570
Joerg Roedel2f4ae01d2017-04-26 15:39:28 +02001571 /*
1572 * if upper layers showed interest and installed a fault handler,
1573 * invoke it.
1574 */
1575 if (domain->handler)
1576 ret = domain->handler(domain, dev, iova, flags,
1577 domain->handler_token);
1578
1579 trace_io_page_fault(dev, iova, flags);
1580 return ret;
1581}
1582EXPORT_SYMBOL_GPL(report_iommu_fault);
1583
1584struct dentry *iommu_debugfs_top;
Alex Williamsond72e31c2012-05-30 14:18:53 -06001585static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001586{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001587 iommu_group_kset = kset_create_and_add("iommu_groups",
1588 NULL, kernel_kobj);
Alex Williamsond72e31c2012-05-30 14:18:53 -06001589 BUG_ON(!iommu_group_kset);
1590
Mitchel Humpherysc75ae492015-07-15 18:27:36 -07001591 iommu_debugfs_top = debugfs_create_dir("iommu", NULL);
1592 if (!iommu_debugfs_top) {
1593 pr_err("Couldn't create iommu debugfs directory\n");
1594 return -ENODEV;
1595 }
1596
Alex Williamsond72e31c2012-05-30 14:18:53 -06001597 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001598}
Marek Szyprowskid7ef9992015-05-19 15:20:23 +02001599core_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001600
1601int iommu_domain_get_attr(struct iommu_domain *domain,
1602 enum iommu_attr attr, void *data)
1603{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001604 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001605 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001606 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001607 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001608
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001609 switch (attr) {
1610 case DOMAIN_ATTR_GEOMETRY:
1611 geometry = data;
1612 *geometry = domain->geometry;
1613
1614 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001615 case DOMAIN_ATTR_PAGING:
1616 paging = data;
Robin Murphyd16e0fa2016-04-07 18:42:06 +01001617 *paging = (domain->pgsize_bitmap != 0UL);
Joerg Roedeld2e12162013-01-29 13:49:04 +01001618 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001619 case DOMAIN_ATTR_WINDOWS:
1620 count = data;
1621
1622 if (domain->ops->domain_get_windows != NULL)
1623 *count = domain->ops->domain_get_windows(domain);
1624 else
1625 ret = -ENODEV;
1626
1627 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001628 default:
1629 if (!domain->ops->domain_get_attr)
1630 return -EINVAL;
1631
1632 ret = domain->ops->domain_get_attr(domain, attr, data);
1633 }
1634
1635 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001636}
1637EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1638
1639int iommu_domain_set_attr(struct iommu_domain *domain,
1640 enum iommu_attr attr, void *data)
1641{
Joerg Roedel69356712013-02-04 14:00:01 +01001642 int ret = 0;
1643 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001644
Joerg Roedel69356712013-02-04 14:00:01 +01001645 switch (attr) {
1646 case DOMAIN_ATTR_WINDOWS:
1647 count = data;
1648
1649 if (domain->ops->domain_set_windows != NULL)
1650 ret = domain->ops->domain_set_windows(domain, *count);
1651 else
1652 ret = -ENODEV;
1653
1654 break;
1655 default:
1656 if (domain->ops->domain_set_attr == NULL)
1657 return -EINVAL;
1658
1659 ret = domain->ops->domain_set_attr(domain, attr, data);
1660 }
1661
1662 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001663}
1664EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
Joerg Roedela1015c22015-05-28 18:41:33 +02001665
Mitchel Humpherys8488df22015-07-09 16:59:02 -07001666/**
1667 * iommu_trigger_fault() - trigger an IOMMU fault
1668 * @domain: iommu domain
1669 *
1670 * Triggers a fault on the device to which this domain is attached.
1671 *
1672 * This function should only be used for debugging purposes, for obvious
1673 * reasons.
1674 */
1675void iommu_trigger_fault(struct iommu_domain *domain, unsigned long flags)
1676{
1677 if (domain->ops->trigger_fault)
1678 domain->ops->trigger_fault(domain, flags);
1679}
1680
Joerg Roedela1015c22015-05-28 18:41:33 +02001681void iommu_get_dm_regions(struct device *dev, struct list_head *list)
1682{
1683 const struct iommu_ops *ops = dev->bus->iommu_ops;
1684
1685 if (ops && ops->get_dm_regions)
1686 ops->get_dm_regions(dev, list);
1687}
1688
1689void iommu_put_dm_regions(struct device *dev, struct list_head *list)
1690{
1691 const struct iommu_ops *ops = dev->bus->iommu_ops;
1692
1693 if (ops && ops->put_dm_regions)
1694 ops->put_dm_regions(dev, list);
1695}
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001696
1697/* Request that a device is direct mapped by the IOMMU */
1698int iommu_request_dm_for_dev(struct device *dev)
1699{
1700 struct iommu_domain *dm_domain;
1701 struct iommu_group *group;
1702 int ret;
1703
1704 /* Device must already be in a group before calling this function */
Lu Baolu497372c2019-05-21 15:27:35 +08001705 group = iommu_group_get(dev);
1706 if (!group)
1707 return -EINVAL;
Joerg Roedeld290f1e2015-05-28 18:41:36 +02001708
1709 mutex_lock(&group->mutex);
1710
1711 /* Check if the default domain is already direct mapped */
1712 ret = 0;
1713 if (group->default_domain &&
1714 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1715 goto out;
1716
1717 /* Don't change mappings of existing devices */
1718 ret = -EBUSY;
1719 if (iommu_group_device_count(group) != 1)
1720 goto out;
1721
1722 /* Allocate a direct mapped domain */
1723 ret = -ENOMEM;
1724 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1725 if (!dm_domain)
1726 goto out;
1727
1728 /* Attach the device to the domain */
1729 ret = __iommu_attach_group(dm_domain, group);
1730 if (ret) {
1731 iommu_domain_free(dm_domain);
1732 goto out;
1733 }
1734
1735 /* Make the direct mapped domain the default for this group */
1736 if (group->default_domain)
1737 iommu_domain_free(group->default_domain);
1738 group->default_domain = dm_domain;
1739
1740 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1741
1742 ret = 0;
1743out:
1744 mutex_unlock(&group->mutex);
1745 iommu_group_put(group);
1746
1747 return ret;
1748}
Robin Murphy57f98d22016-09-13 10:54:14 +01001749
1750int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1751 const struct iommu_ops *ops)
1752{
1753 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1754
1755 if (fwspec)
1756 return ops == fwspec->ops ? 0 : -EINVAL;
1757
1758 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1759 if (!fwspec)
1760 return -ENOMEM;
1761
1762 of_node_get(to_of_node(iommu_fwnode));
1763 fwspec->iommu_fwnode = iommu_fwnode;
1764 fwspec->ops = ops;
1765 dev->iommu_fwspec = fwspec;
1766 return 0;
1767}
1768EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1769
1770void iommu_fwspec_free(struct device *dev)
1771{
1772 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1773
1774 if (fwspec) {
1775 fwnode_handle_put(fwspec->iommu_fwnode);
1776 kfree(fwspec);
1777 dev->iommu_fwspec = NULL;
1778 }
1779}
1780EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1781
1782int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1783{
1784 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1785 size_t size;
1786 int i;
1787
1788 if (!fwspec)
1789 return -EINVAL;
1790
1791 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1792 if (size > sizeof(*fwspec)) {
1793 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1794 if (!fwspec)
1795 return -ENOMEM;
1796 }
1797
1798 for (i = 0; i < num_ids; i++)
1799 fwspec->ids[fwspec->num_ids + i] = ids[i];
1800
1801 fwspec->num_ids += num_ids;
1802 dev->iommu_fwspec = fwspec;
1803 return 0;
1804}
1805EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
Patrick Daly1e3c16c2017-04-12 17:59:07 -07001806
1807/*
1808 * Return the id asoociated with a pci device.
1809 */
1810int iommu_fwspec_get_id(struct device *dev, u32 *id)
1811{
1812 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1813
1814 if (!fwspec)
1815 return -EINVAL;
1816
1817 if (!dev_is_pci(dev))
1818 return -EINVAL;
1819
1820 if (fwspec->num_ids != 1)
1821 return -EINVAL;
1822
1823 *id = fwspec->ids[0];
1824 return 0;
1825}
Patrick Daly958973c2017-04-28 19:45:18 -07001826
1827/*
1828 * Until a formal solution for probe deferral becomes part
1829 * of the iommu framework...
1830 */
1831int iommu_is_available(struct device *dev)
1832{
1833 if (!dev->bus->iommu_ops ||
1834 !dev->iommu_fwspec ||
1835 !dev->iommu_group)
1836 return -EPROBE_DEFER;
1837 return 0;
1838}