]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/iommu/iommu.c
media: Revert "media: em28xx: add missing em28xx_close_extension"
[mirror_ubuntu-focal-kernel.git] / drivers / iommu / iommu.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
fc2100eb
JR
2/*
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
63ce3ae8 4 * Author: Joerg Roedel <jroedel@suse.de>
fc2100eb
JR
5 */
6
92e7066f 7#define pr_fmt(fmt) "iommu: " fmt
7d3002cc 8
905d66c1 9#include <linux/device.h>
40998188 10#include <linux/kernel.h>
fc2100eb
JR
11#include <linux/bug.h>
12#include <linux/types.h>
c1af7b40
PG
13#include <linux/init.h>
14#include <linux/export.h>
60db4027 15#include <linux/slab.h>
fc2100eb
JR
16#include <linux/errno.h>
17#include <linux/iommu.h>
d72e31c9
AW
18#include <linux/idr.h>
19#include <linux/notifier.h>
20#include <linux/err.h>
104a1c13 21#include <linux/pci.h>
f096c061 22#include <linux/bitops.h>
57f98d2f 23#include <linux/property.h>
eab03e2a 24#include <linux/fsl/mc.h>
7f6db171 25#include <trace/events/iommu.h>
d72e31c9
AW
26
27static struct kset *iommu_group_kset;
e38d1f13 28static DEFINE_IDA(iommu_group_ida);
22bb182c
JR
29
30static unsigned int iommu_def_domain_type __read_mostly;
68a6efe8 31static bool iommu_dma_strict __read_mostly = true;
faf14989 32static u32 iommu_cmd_line __read_mostly;
d72e31c9
AW
33
34struct iommu_group {
35 struct kobject kobj;
36 struct kobject *devices_kobj;
37 struct list_head devices;
38 struct mutex mutex;
39 struct blocking_notifier_head notifier;
40 void *iommu_data;
41 void (*iommu_data_release)(void *iommu_data);
42 char *name;
43 int id;
53723dc5 44 struct iommu_domain *default_domain;
e39cb8a3 45 struct iommu_domain *domain;
d72e31c9
AW
46};
47
c09e22d5 48struct group_device {
d72e31c9
AW
49 struct list_head list;
50 struct device *dev;
51 char *name;
52};
53
54struct iommu_group_attribute {
55 struct attribute attr;
56 ssize_t (*show)(struct iommu_group *group, char *buf);
57 ssize_t (*store)(struct iommu_group *group,
58 const char *buf, size_t count);
59};
60
bc7d12b9 61static const char * const iommu_group_resv_type_string[] = {
adfd3738
EA
62 [IOMMU_RESV_DIRECT] = "direct",
63 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
64 [IOMMU_RESV_RESERVED] = "reserved",
65 [IOMMU_RESV_MSI] = "msi",
66 [IOMMU_RESV_SW_MSI] = "msi",
bc7d12b9
EA
67};
68
faf14989
JR
69#define IOMMU_CMD_LINE_DMA_API BIT(0)
70
71static void iommu_set_cmd_line_dma_api(void)
72{
73 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
74}
75
22bb182c 76static bool iommu_cmd_line_dma_api(void)
faf14989
JR
77{
78 return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
79}
80
d72e31c9
AW
81#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
82struct iommu_group_attribute iommu_group_attr_##_name = \
83 __ATTR(_name, _mode, _show, _store)
fc2100eb 84
d72e31c9
AW
85#define to_iommu_group_attr(_attr) \
86 container_of(_attr, struct iommu_group_attribute, attr)
87#define to_iommu_group(_kobj) \
88 container_of(_kobj, struct iommu_group, kobj)
fc2100eb 89
b0119e87
JR
90static LIST_HEAD(iommu_device_list);
91static DEFINE_SPINLOCK(iommu_device_lock);
92
5fa9e7c5
JR
93/*
94 * Use a function instead of an array here because the domain-type is a
95 * bit-field, so an array would waste memory.
96 */
97static const char *iommu_domain_type_str(unsigned int t)
98{
99 switch (t) {
100 case IOMMU_DOMAIN_BLOCKED:
101 return "Blocked";
102 case IOMMU_DOMAIN_IDENTITY:
103 return "Passthrough";
104 case IOMMU_DOMAIN_UNMANAGED:
105 return "Unmanaged";
106 case IOMMU_DOMAIN_DMA:
107 return "Translated";
108 default:
109 return "Unknown";
110 }
111}
112
113static int __init iommu_subsys_init(void)
114{
22bb182c
JR
115 bool cmd_line = iommu_cmd_line_dma_api();
116
117 if (!cmd_line) {
118 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
119 iommu_set_default_passthrough(false);
120 else
121 iommu_set_default_translated(false);
2cc13bb4 122
2896ba40
JR
123 if (iommu_default_passthrough() && mem_encrypt_active()) {
124 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
2cc13bb4
JR
125 iommu_set_default_translated(false);
126 }
22bb182c
JR
127 }
128
129 pr_info("Default domain type: %s %s\n",
130 iommu_domain_type_str(iommu_def_domain_type),
131 cmd_line ? "(set via kernel command line)" : "");
5fa9e7c5
JR
132
133 return 0;
134}
135subsys_initcall(iommu_subsys_init);
136
b0119e87
JR
137int iommu_device_register(struct iommu_device *iommu)
138{
139 spin_lock(&iommu_device_lock);
140 list_add_tail(&iommu->list, &iommu_device_list);
141 spin_unlock(&iommu_device_lock);
b0119e87
JR
142 return 0;
143}
144
145void iommu_device_unregister(struct iommu_device *iommu)
146{
147 spin_lock(&iommu_device_lock);
148 list_del(&iommu->list);
149 spin_unlock(&iommu_device_lock);
150}
151
0c830e6b
JP
152static struct iommu_param *iommu_get_dev_param(struct device *dev)
153{
154 struct iommu_param *param = dev->iommu_param;
155
156 if (param)
157 return param;
158
159 param = kzalloc(sizeof(*param), GFP_KERNEL);
160 if (!param)
161 return NULL;
162
163 mutex_init(&param->lock);
164 dev->iommu_param = param;
165 return param;
166}
167
168static void iommu_free_dev_param(struct device *dev)
169{
170 kfree(dev->iommu_param);
171 dev->iommu_param = NULL;
172}
173
cc5aed44
JR
174int iommu_probe_device(struct device *dev)
175{
176 const struct iommu_ops *ops = dev->bus->iommu_ops;
0c830e6b 177 int ret;
cc5aed44
JR
178
179 WARN_ON(dev->iommu_group);
0c830e6b
JP
180 if (!ops)
181 return -EINVAL;
cc5aed44 182
0c830e6b
JP
183 if (!iommu_get_dev_param(dev))
184 return -ENOMEM;
cc5aed44 185
0c830e6b
JP
186 ret = ops->add_device(dev);
187 if (ret)
188 iommu_free_dev_param(dev);
dc9de8a2
JR
189
190 return ret;
cc5aed44
JR
191}
192
193void iommu_release_device(struct device *dev)
194{
195 const struct iommu_ops *ops = dev->bus->iommu_ops;
196
197 if (dev->iommu_group)
198 ops->remove_device(dev);
0c830e6b
JP
199
200 iommu_free_dev_param(dev);
cc5aed44
JR
201}
202
53723dc5
JR
203static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
204 unsigned type);
e39cb8a3
JR
205static int __iommu_attach_device(struct iommu_domain *domain,
206 struct device *dev);
207static int __iommu_attach_group(struct iommu_domain *domain,
208 struct iommu_group *group);
209static void __iommu_detach_group(struct iommu_domain *domain,
210 struct iommu_group *group);
53723dc5 211
fccb4e3b
WD
212static int __init iommu_set_def_domain_type(char *str)
213{
214 bool pt;
7f9584df 215 int ret;
fccb4e3b 216
7f9584df
AS
217 ret = kstrtobool(str, &pt);
218 if (ret)
219 return ret;
fccb4e3b 220
adab0b07
JR
221 if (pt)
222 iommu_set_default_passthrough(true);
223 else
224 iommu_set_default_translated(true);
faf14989 225
fccb4e3b
WD
226 return 0;
227}
228early_param("iommu.passthrough", iommu_set_def_domain_type);
229
68a6efe8
ZL
230static int __init iommu_dma_setup(char *str)
231{
232 return kstrtobool(str, &iommu_dma_strict);
233}
234early_param("iommu.strict", iommu_dma_setup);
235
d72e31c9
AW
236static ssize_t iommu_group_attr_show(struct kobject *kobj,
237 struct attribute *__attr, char *buf)
1460432c 238{
d72e31c9
AW
239 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
240 struct iommu_group *group = to_iommu_group(kobj);
241 ssize_t ret = -EIO;
1460432c 242
d72e31c9
AW
243 if (attr->show)
244 ret = attr->show(group, buf);
245 return ret;
246}
247
248static ssize_t iommu_group_attr_store(struct kobject *kobj,
249 struct attribute *__attr,
250 const char *buf, size_t count)
251{
252 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
253 struct iommu_group *group = to_iommu_group(kobj);
254 ssize_t ret = -EIO;
1460432c 255
d72e31c9
AW
256 if (attr->store)
257 ret = attr->store(group, buf, count);
258 return ret;
1460432c 259}
1460432c 260
d72e31c9
AW
261static const struct sysfs_ops iommu_group_sysfs_ops = {
262 .show = iommu_group_attr_show,
263 .store = iommu_group_attr_store,
264};
1460432c 265
d72e31c9
AW
266static int iommu_group_create_file(struct iommu_group *group,
267 struct iommu_group_attribute *attr)
268{
269 return sysfs_create_file(&group->kobj, &attr->attr);
1460432c 270}
1460432c 271
d72e31c9
AW
272static void iommu_group_remove_file(struct iommu_group *group,
273 struct iommu_group_attribute *attr)
274{
275 sysfs_remove_file(&group->kobj, &attr->attr);
276}
277
278static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
279{
280 return sprintf(buf, "%s\n", group->name);
281}
282
6c65fb31
EA
283/**
284 * iommu_insert_resv_region - Insert a new region in the
285 * list of reserved regions.
286 * @new: new region to insert
287 * @regions: list of regions
288 *
4dbd258f
EA
289 * Elements are sorted by start address and overlapping segments
290 * of the same type are merged.
6c65fb31 291 */
4dbd258f
EA
292int iommu_insert_resv_region(struct iommu_resv_region *new,
293 struct list_head *regions)
6c65fb31 294{
4dbd258f
EA
295 struct iommu_resv_region *iter, *tmp, *nr, *top;
296 LIST_HEAD(stack);
297
298 nr = iommu_alloc_resv_region(new->start, new->length,
299 new->prot, new->type);
300 if (!nr)
301 return -ENOMEM;
302
303 /* First add the new element based on start address sorting */
304 list_for_each_entry(iter, regions, list) {
305 if (nr->start < iter->start ||
306 (nr->start == iter->start && nr->type <= iter->type))
307 break;
308 }
309 list_add_tail(&nr->list, &iter->list);
310
311 /* Merge overlapping segments of type nr->type in @regions, if any */
312 list_for_each_entry_safe(iter, tmp, regions, list) {
313 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
314
6a71ce7b
EA
315 /* no merge needed on elements of different types than @new */
316 if (iter->type != new->type) {
4dbd258f
EA
317 list_move_tail(&iter->list, &stack);
318 continue;
319 }
320
321 /* look for the last stack element of same type as @iter */
322 list_for_each_entry_reverse(top, &stack, list)
323 if (top->type == iter->type)
324 goto check_overlap;
325
326 list_move_tail(&iter->list, &stack);
327 continue;
328
329check_overlap:
330 top_end = top->start + top->length - 1;
331
332 if (iter->start > top_end + 1) {
333 list_move_tail(&iter->list, &stack);
6c65fb31 334 } else {
4dbd258f
EA
335 top->length = max(top_end, iter_end) - top->start + 1;
336 list_del(&iter->list);
337 kfree(iter);
6c65fb31
EA
338 }
339 }
4dbd258f 340 list_splice(&stack, regions);
6c65fb31
EA
341 return 0;
342}
343
344static int
345iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
346 struct list_head *group_resv_regions)
347{
348 struct iommu_resv_region *entry;
a514a6e2 349 int ret = 0;
6c65fb31
EA
350
351 list_for_each_entry(entry, dev_resv_regions, list) {
352 ret = iommu_insert_resv_region(entry, group_resv_regions);
353 if (ret)
354 break;
355 }
356 return ret;
357}
358
359int iommu_get_group_resv_regions(struct iommu_group *group,
360 struct list_head *head)
361{
8d2932dd 362 struct group_device *device;
6c65fb31
EA
363 int ret = 0;
364
365 mutex_lock(&group->mutex);
366 list_for_each_entry(device, &group->devices, list) {
367 struct list_head dev_resv_regions;
368
369 INIT_LIST_HEAD(&dev_resv_regions);
370 iommu_get_resv_regions(device->dev, &dev_resv_regions);
371 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
372 iommu_put_resv_regions(device->dev, &dev_resv_regions);
373 if (ret)
374 break;
375 }
376 mutex_unlock(&group->mutex);
377 return ret;
378}
379EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
380
bc7d12b9
EA
381static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
382 char *buf)
383{
384 struct iommu_resv_region *region, *next;
385 struct list_head group_resv_regions;
386 char *str = buf;
387
388 INIT_LIST_HEAD(&group_resv_regions);
389 iommu_get_group_resv_regions(group, &group_resv_regions);
390
391 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
392 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
393 (long long int)region->start,
394 (long long int)(region->start +
395 region->length - 1),
396 iommu_group_resv_type_string[region->type]);
397 kfree(region);
398 }
399
400 return (str - buf);
401}
402
c52c72d3
OJ
403static ssize_t iommu_group_show_type(struct iommu_group *group,
404 char *buf)
405{
406 char *type = "unknown\n";
407
408 if (group->default_domain) {
409 switch (group->default_domain->type) {
410 case IOMMU_DOMAIN_BLOCKED:
411 type = "blocked\n";
412 break;
413 case IOMMU_DOMAIN_IDENTITY:
414 type = "identity\n";
415 break;
416 case IOMMU_DOMAIN_UNMANAGED:
417 type = "unmanaged\n";
418 break;
419 case IOMMU_DOMAIN_DMA:
24f307d8 420 type = "DMA\n";
c52c72d3
OJ
421 break;
422 }
423 }
424 strcpy(buf, type);
425
426 return strlen(type);
427}
428
d72e31c9
AW
429static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
430
bc7d12b9
EA
431static IOMMU_GROUP_ATTR(reserved_regions, 0444,
432 iommu_group_show_resv_regions, NULL);
433
c52c72d3
OJ
434static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
435
d72e31c9
AW
436static void iommu_group_release(struct kobject *kobj)
437{
438 struct iommu_group *group = to_iommu_group(kobj);
439
269aa808
JR
440 pr_debug("Releasing group %d\n", group->id);
441
d72e31c9
AW
442 if (group->iommu_data_release)
443 group->iommu_data_release(group->iommu_data);
444
feccf398 445 ida_simple_remove(&iommu_group_ida, group->id);
d72e31c9 446
53723dc5
JR
447 if (group->default_domain)
448 iommu_domain_free(group->default_domain);
449
d72e31c9
AW
450 kfree(group->name);
451 kfree(group);
452}
453
454static struct kobj_type iommu_group_ktype = {
455 .sysfs_ops = &iommu_group_sysfs_ops,
456 .release = iommu_group_release,
457};
458
459/**
460 * iommu_group_alloc - Allocate a new group
d72e31c9
AW
461 *
462 * This function is called by an iommu driver to allocate a new iommu
463 * group. The iommu group represents the minimum granularity of the iommu.
464 * Upon successful return, the caller holds a reference to the supplied
465 * group in order to hold the group until devices are added. Use
466 * iommu_group_put() to release this extra reference count, allowing the
467 * group to be automatically reclaimed once it has no devices or external
468 * references.
469 */
470struct iommu_group *iommu_group_alloc(void)
1460432c 471{
d72e31c9
AW
472 struct iommu_group *group;
473 int ret;
474
475 group = kzalloc(sizeof(*group), GFP_KERNEL);
476 if (!group)
477 return ERR_PTR(-ENOMEM);
478
479 group->kobj.kset = iommu_group_kset;
480 mutex_init(&group->mutex);
481 INIT_LIST_HEAD(&group->devices);
482 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
483
feccf398
HK
484 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
485 if (ret < 0) {
d72e31c9 486 kfree(group);
feccf398 487 return ERR_PTR(ret);
d72e31c9 488 }
feccf398 489 group->id = ret;
1460432c 490
d72e31c9
AW
491 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
492 NULL, "%d", group->id);
493 if (ret) {
feccf398 494 ida_simple_remove(&iommu_group_ida, group->id);
52db2e28 495 kobject_put(&group->kobj);
d72e31c9
AW
496 return ERR_PTR(ret);
497 }
498
499 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
500 if (!group->devices_kobj) {
501 kobject_put(&group->kobj); /* triggers .release & free */
502 return ERR_PTR(-ENOMEM);
503 }
504
505 /*
506 * The devices_kobj holds a reference on the group kobject, so
507 * as long as that exists so will the group. We can therefore
508 * use the devices_kobj for reference counting.
509 */
510 kobject_put(&group->kobj);
511
bc7d12b9
EA
512 ret = iommu_group_create_file(group,
513 &iommu_group_attr_reserved_regions);
514 if (ret)
515 return ERR_PTR(ret);
516
c52c72d3
OJ
517 ret = iommu_group_create_file(group, &iommu_group_attr_type);
518 if (ret)
519 return ERR_PTR(ret);
520
269aa808
JR
521 pr_debug("Allocated group %d\n", group->id);
522
d72e31c9
AW
523 return group;
524}
525EXPORT_SYMBOL_GPL(iommu_group_alloc);
526
aa16bea9
AK
527struct iommu_group *iommu_group_get_by_id(int id)
528{
529 struct kobject *group_kobj;
530 struct iommu_group *group;
531 const char *name;
532
533 if (!iommu_group_kset)
534 return NULL;
535
536 name = kasprintf(GFP_KERNEL, "%d", id);
537 if (!name)
538 return NULL;
539
540 group_kobj = kset_find_obj(iommu_group_kset, name);
541 kfree(name);
542
543 if (!group_kobj)
544 return NULL;
545
546 group = container_of(group_kobj, struct iommu_group, kobj);
547 BUG_ON(group->id != id);
548
549 kobject_get(group->devices_kobj);
550 kobject_put(&group->kobj);
551
552 return group;
553}
554EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
555
d72e31c9
AW
556/**
557 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
558 * @group: the group
559 *
560 * iommu drivers can store data in the group for use when doing iommu
561 * operations. This function provides a way to retrieve it. Caller
562 * should hold a group reference.
563 */
564void *iommu_group_get_iommudata(struct iommu_group *group)
565{
566 return group->iommu_data;
567}
568EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
569
570/**
571 * iommu_group_set_iommudata - set iommu_data for a group
572 * @group: the group
573 * @iommu_data: new data
574 * @release: release function for iommu_data
575 *
576 * iommu drivers can store data in the group for use when doing iommu
577 * operations. This function provides a way to set the data after
578 * the group has been allocated. Caller should hold a group reference.
579 */
580void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
581 void (*release)(void *iommu_data))
1460432c 582{
d72e31c9
AW
583 group->iommu_data = iommu_data;
584 group->iommu_data_release = release;
585}
586EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1460432c 587
d72e31c9
AW
588/**
589 * iommu_group_set_name - set name for a group
590 * @group: the group
591 * @name: name
592 *
593 * Allow iommu driver to set a name for a group. When set it will
594 * appear in a name attribute file under the group in sysfs.
595 */
596int iommu_group_set_name(struct iommu_group *group, const char *name)
597{
598 int ret;
599
600 if (group->name) {
601 iommu_group_remove_file(group, &iommu_group_attr_name);
602 kfree(group->name);
603 group->name = NULL;
604 if (!name)
605 return 0;
606 }
607
608 group->name = kstrdup(name, GFP_KERNEL);
609 if (!group->name)
610 return -ENOMEM;
611
612 ret = iommu_group_create_file(group, &iommu_group_attr_name);
613 if (ret) {
614 kfree(group->name);
615 group->name = NULL;
616 return ret;
617 }
1460432c
AW
618
619 return 0;
620}
d72e31c9 621EXPORT_SYMBOL_GPL(iommu_group_set_name);
1460432c 622
beed2821
JR
623static int iommu_group_create_direct_mappings(struct iommu_group *group,
624 struct device *dev)
625{
626 struct iommu_domain *domain = group->default_domain;
e5b5234a 627 struct iommu_resv_region *entry;
beed2821
JR
628 struct list_head mappings;
629 unsigned long pg_size;
630 int ret = 0;
631
632 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
633 return 0;
634
d16e0faa 635 BUG_ON(!domain->pgsize_bitmap);
beed2821 636
d16e0faa 637 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
beed2821
JR
638 INIT_LIST_HEAD(&mappings);
639
e5b5234a 640 iommu_get_resv_regions(dev, &mappings);
beed2821
JR
641
642 /* We need to consider overlapping regions for different devices */
643 list_for_each_entry(entry, &mappings, list) {
644 dma_addr_t start, end, addr;
645
e5b5234a
EA
646 if (domain->ops->apply_resv_region)
647 domain->ops->apply_resv_region(dev, domain, entry);
33b21a6b 648
beed2821
JR
649 start = ALIGN(entry->start, pg_size);
650 end = ALIGN(entry->start + entry->length, pg_size);
651
adfd3738
EA
652 if (entry->type != IOMMU_RESV_DIRECT &&
653 entry->type != IOMMU_RESV_DIRECT_RELAXABLE)
544a25d9
EA
654 continue;
655
beed2821
JR
656 for (addr = start; addr < end; addr += pg_size) {
657 phys_addr_t phys_addr;
658
659 phys_addr = iommu_iova_to_phys(domain, addr);
660 if (phys_addr)
661 continue;
662
663 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
664 if (ret)
665 goto out;
666 }
667
668 }
669
add02cfd
JR
670 iommu_flush_tlb_all(domain);
671
beed2821 672out:
e5b5234a 673 iommu_put_resv_regions(dev, &mappings);
beed2821
JR
674
675 return ret;
676}
677
d72e31c9
AW
678/**
679 * iommu_group_add_device - add a device to an iommu group
680 * @group: the group into which to add the device (reference should be held)
681 * @dev: the device
682 *
683 * This function is called by an iommu driver to add a device into a
684 * group. Adding a device increments the group reference count.
685 */
686int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1460432c 687{
d72e31c9 688 int ret, i = 0;
c09e22d5 689 struct group_device *device;
d72e31c9
AW
690
691 device = kzalloc(sizeof(*device), GFP_KERNEL);
692 if (!device)
693 return -ENOMEM;
694
695 device->dev = dev;
1460432c 696
d72e31c9 697 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
797a8b4d
RM
698 if (ret)
699 goto err_free_device;
d72e31c9
AW
700
701 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
702rename:
703 if (!device->name) {
797a8b4d
RM
704 ret = -ENOMEM;
705 goto err_remove_link;
d72e31c9 706 }
1460432c 707
d72e31c9
AW
708 ret = sysfs_create_link_nowarn(group->devices_kobj,
709 &dev->kobj, device->name);
710 if (ret) {
d72e31c9
AW
711 if (ret == -EEXIST && i >= 0) {
712 /*
713 * Account for the slim chance of collision
714 * and append an instance to the name.
715 */
797a8b4d 716 kfree(device->name);
d72e31c9
AW
717 device->name = kasprintf(GFP_KERNEL, "%s.%d",
718 kobject_name(&dev->kobj), i++);
719 goto rename;
720 }
797a8b4d 721 goto err_free_name;
d72e31c9
AW
722 }
723
724 kobject_get(group->devices_kobj);
725
726 dev->iommu_group = group;
727
beed2821
JR
728 iommu_group_create_direct_mappings(group, dev);
729
d72e31c9
AW
730 mutex_lock(&group->mutex);
731 list_add_tail(&device->list, &group->devices);
e39cb8a3 732 if (group->domain)
797a8b4d 733 ret = __iommu_attach_device(group->domain, dev);
d72e31c9 734 mutex_unlock(&group->mutex);
797a8b4d
RM
735 if (ret)
736 goto err_put_group;
d72e31c9
AW
737
738 /* Notify any listeners about change to group. */
739 blocking_notifier_call_chain(&group->notifier,
740 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
d1cf7e82
SK
741
742 trace_add_device_to_group(group->id, dev);
269aa808 743
780da9e4 744 dev_info(dev, "Adding to iommu group %d\n", group->id);
269aa808 745
1460432c 746 return 0;
797a8b4d
RM
747
748err_put_group:
749 mutex_lock(&group->mutex);
750 list_del(&device->list);
751 mutex_unlock(&group->mutex);
752 dev->iommu_group = NULL;
753 kobject_put(group->devices_kobj);
0e928c95 754 sysfs_remove_link(group->devices_kobj, device->name);
797a8b4d
RM
755err_free_name:
756 kfree(device->name);
757err_remove_link:
758 sysfs_remove_link(&dev->kobj, "iommu_group");
759err_free_device:
760 kfree(device);
780da9e4 761 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
797a8b4d 762 return ret;
1460432c 763}
d72e31c9 764EXPORT_SYMBOL_GPL(iommu_group_add_device);
1460432c 765
d72e31c9
AW
766/**
767 * iommu_group_remove_device - remove a device from it's current group
768 * @dev: device to be removed
769 *
770 * This function is called by an iommu driver to remove the device from
771 * it's current group. This decrements the iommu group reference count.
772 */
773void iommu_group_remove_device(struct device *dev)
774{
775 struct iommu_group *group = dev->iommu_group;
c09e22d5 776 struct group_device *tmp_device, *device = NULL;
d72e31c9 777
f0c64176
FW
778 if (!group)
779 return;
780
780da9e4 781 dev_info(dev, "Removing from iommu group %d\n", group->id);
269aa808 782
d72e31c9
AW
783 /* Pre-notify listeners that a device is being removed. */
784 blocking_notifier_call_chain(&group->notifier,
785 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
786
787 mutex_lock(&group->mutex);
788 list_for_each_entry(tmp_device, &group->devices, list) {
789 if (tmp_device->dev == dev) {
790 device = tmp_device;
791 list_del(&device->list);
792 break;
793 }
794 }
795 mutex_unlock(&group->mutex);
796
797 if (!device)
798 return;
799
800 sysfs_remove_link(group->devices_kobj, device->name);
801 sysfs_remove_link(&dev->kobj, "iommu_group");
802
2e757086
SK
803 trace_remove_device_from_group(group->id, dev);
804
d72e31c9
AW
805 kfree(device->name);
806 kfree(device);
807 dev->iommu_group = NULL;
808 kobject_put(group->devices_kobj);
809}
810EXPORT_SYMBOL_GPL(iommu_group_remove_device);
811
426a2738
JR
812static int iommu_group_device_count(struct iommu_group *group)
813{
c09e22d5 814 struct group_device *entry;
426a2738
JR
815 int ret = 0;
816
817 list_for_each_entry(entry, &group->devices, list)
818 ret++;
819
820 return ret;
821}
822
d72e31c9
AW
823/**
824 * iommu_group_for_each_dev - iterate over each device in the group
825 * @group: the group
826 * @data: caller opaque data to be passed to callback function
827 * @fn: caller supplied callback function
828 *
829 * This function is called by group users to iterate over group devices.
830 * Callers should hold a reference count to the group during callback.
831 * The group->mutex is held across callbacks, which will block calls to
832 * iommu_group_add/remove_device.
833 */
e39cb8a3
JR
834static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
835 int (*fn)(struct device *, void *))
d72e31c9 836{
c09e22d5 837 struct group_device *device;
d72e31c9
AW
838 int ret = 0;
839
d72e31c9
AW
840 list_for_each_entry(device, &group->devices, list) {
841 ret = fn(device->dev, data);
842 if (ret)
843 break;
844 }
e39cb8a3
JR
845 return ret;
846}
847
848
849int iommu_group_for_each_dev(struct iommu_group *group, void *data,
850 int (*fn)(struct device *, void *))
851{
852 int ret;
853
854 mutex_lock(&group->mutex);
855 ret = __iommu_group_for_each_dev(group, data, fn);
d72e31c9 856 mutex_unlock(&group->mutex);
e39cb8a3 857
d72e31c9
AW
858 return ret;
859}
860EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
861
862/**
863 * iommu_group_get - Return the group for a device and increment reference
864 * @dev: get the group that this device belongs to
865 *
866 * This function is called by iommu drivers and users to get the group
867 * for the specified device. If found, the group is returned and the group
868 * reference in incremented, else NULL.
869 */
870struct iommu_group *iommu_group_get(struct device *dev)
871{
872 struct iommu_group *group = dev->iommu_group;
873
874 if (group)
875 kobject_get(group->devices_kobj);
876
877 return group;
878}
879EXPORT_SYMBOL_GPL(iommu_group_get);
880
13f59a78
RM
881/**
882 * iommu_group_ref_get - Increment reference on a group
883 * @group: the group to use, must not be NULL
884 *
885 * This function is called by iommu drivers to take additional references on an
886 * existing group. Returns the given group for convenience.
887 */
888struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
889{
890 kobject_get(group->devices_kobj);
891 return group;
892}
893
d72e31c9
AW
894/**
895 * iommu_group_put - Decrement group reference
896 * @group: the group to use
897 *
898 * This function is called by iommu drivers and users to release the
899 * iommu group. Once the reference count is zero, the group is released.
900 */
901void iommu_group_put(struct iommu_group *group)
902{
903 if (group)
904 kobject_put(group->devices_kobj);
905}
906EXPORT_SYMBOL_GPL(iommu_group_put);
907
908/**
909 * iommu_group_register_notifier - Register a notifier for group changes
910 * @group: the group to watch
911 * @nb: notifier block to signal
912 *
913 * This function allows iommu group users to track changes in a group.
914 * See include/linux/iommu.h for actions sent via this notifier. Caller
915 * should hold a reference to the group throughout notifier registration.
916 */
917int iommu_group_register_notifier(struct iommu_group *group,
918 struct notifier_block *nb)
919{
920 return blocking_notifier_chain_register(&group->notifier, nb);
921}
922EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
923
924/**
925 * iommu_group_unregister_notifier - Unregister a notifier
926 * @group: the group to watch
927 * @nb: notifier block to signal
928 *
929 * Unregister a previously registered group notifier block.
930 */
931int iommu_group_unregister_notifier(struct iommu_group *group,
932 struct notifier_block *nb)
933{
934 return blocking_notifier_chain_unregister(&group->notifier, nb);
935}
936EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
937
0c830e6b
JP
938/**
939 * iommu_register_device_fault_handler() - Register a device fault handler
940 * @dev: the device
941 * @handler: the fault handler
942 * @data: private data passed as argument to the handler
943 *
944 * When an IOMMU fault event is received, this handler gets called with the
bf3255b3
JPB
945 * fault event and data as argument. The handler should return 0 on success. If
946 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
947 * complete the fault by calling iommu_page_response() with one of the following
948 * response code:
949 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
950 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
951 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
952 * page faults if possible.
0c830e6b
JP
953 *
954 * Return 0 if the fault handler was installed successfully, or an error.
955 */
956int iommu_register_device_fault_handler(struct device *dev,
957 iommu_dev_fault_handler_t handler,
958 void *data)
959{
960 struct iommu_param *param = dev->iommu_param;
961 int ret = 0;
962
963 if (!param)
964 return -EINVAL;
965
966 mutex_lock(&param->lock);
967 /* Only allow one fault handler registered for each device */
968 if (param->fault_param) {
969 ret = -EBUSY;
970 goto done_unlock;
971 }
972
973 get_device(dev);
974 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
975 if (!param->fault_param) {
976 put_device(dev);
977 ret = -ENOMEM;
978 goto done_unlock;
979 }
980 param->fault_param->handler = handler;
981 param->fault_param->data = data;
bf3255b3
JPB
982 mutex_init(&param->fault_param->lock);
983 INIT_LIST_HEAD(&param->fault_param->faults);
0c830e6b
JP
984
985done_unlock:
986 mutex_unlock(&param->lock);
987
988 return ret;
989}
990EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
991
992/**
993 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
994 * @dev: the device
995 *
996 * Remove the device fault handler installed with
997 * iommu_register_device_fault_handler().
998 *
999 * Return 0 on success, or an error.
1000 */
1001int iommu_unregister_device_fault_handler(struct device *dev)
1002{
1003 struct iommu_param *param = dev->iommu_param;
1004 int ret = 0;
1005
1006 if (!param)
1007 return -EINVAL;
1008
1009 mutex_lock(&param->lock);
1010
1011 if (!param->fault_param)
1012 goto unlock;
1013
bf3255b3
JPB
1014 /* we cannot unregister handler if there are pending faults */
1015 if (!list_empty(&param->fault_param->faults)) {
1016 ret = -EBUSY;
1017 goto unlock;
1018 }
1019
0c830e6b
JP
1020 kfree(param->fault_param);
1021 param->fault_param = NULL;
1022 put_device(dev);
1023unlock:
1024 mutex_unlock(&param->lock);
1025
1026 return ret;
1027}
1028EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1029
1030/**
1031 * iommu_report_device_fault() - Report fault event to device driver
1032 * @dev: the device
1033 * @evt: fault event data
1034 *
1035 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
bf3255b3
JPB
1036 * handler. When this function fails and the fault is recoverable, it is the
1037 * caller's responsibility to complete the fault.
0c830e6b
JP
1038 *
1039 * Return 0 on success, or an error.
1040 */
1041int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1042{
1043 struct iommu_param *param = dev->iommu_param;
bf3255b3 1044 struct iommu_fault_event *evt_pending = NULL;
0c830e6b
JP
1045 struct iommu_fault_param *fparam;
1046 int ret = 0;
1047
1048 if (!param || !evt)
1049 return -EINVAL;
1050
1051 /* we only report device fault if there is a handler registered */
1052 mutex_lock(&param->lock);
1053 fparam = param->fault_param;
1054 if (!fparam || !fparam->handler) {
1055 ret = -EINVAL;
1056 goto done_unlock;
1057 }
bf3255b3
JPB
1058
1059 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1060 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1061 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1062 GFP_KERNEL);
1063 if (!evt_pending) {
1064 ret = -ENOMEM;
1065 goto done_unlock;
1066 }
1067 mutex_lock(&fparam->lock);
1068 list_add_tail(&evt_pending->list, &fparam->faults);
1069 mutex_unlock(&fparam->lock);
1070 }
1071
0c830e6b 1072 ret = fparam->handler(&evt->fault, fparam->data);
bf3255b3
JPB
1073 if (ret && evt_pending) {
1074 mutex_lock(&fparam->lock);
1075 list_del(&evt_pending->list);
1076 mutex_unlock(&fparam->lock);
1077 kfree(evt_pending);
1078 }
0c830e6b
JP
1079done_unlock:
1080 mutex_unlock(&param->lock);
1081 return ret;
1082}
1083EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1084
bf3255b3
JPB
1085int iommu_page_response(struct device *dev,
1086 struct iommu_page_response *msg)
1087{
1088 bool pasid_valid;
1089 int ret = -EINVAL;
1090 struct iommu_fault_event *evt;
1091 struct iommu_fault_page_request *prm;
1092 struct iommu_param *param = dev->iommu_param;
1093 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1094
1095 if (!domain || !domain->ops->page_response)
1096 return -ENODEV;
1097
1098 if (!param || !param->fault_param)
1099 return -EINVAL;
1100
1101 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1102 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1103 return -EINVAL;
1104
1105 /* Only send response if there is a fault report pending */
1106 mutex_lock(&param->fault_param->lock);
1107 if (list_empty(&param->fault_param->faults)) {
1108 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1109 goto done_unlock;
1110 }
1111 /*
1112 * Check if we have a matching page request pending to respond,
1113 * otherwise return -EINVAL
1114 */
1115 list_for_each_entry(evt, &param->fault_param->faults, list) {
1116 prm = &evt->fault.prm;
1117 pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1118
1119 if ((pasid_valid && prm->pasid != msg->pasid) ||
1120 prm->grpid != msg->grpid)
1121 continue;
1122
1123 /* Sanitize the reply */
1124 msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0;
1125
1126 ret = domain->ops->page_response(dev, evt, msg);
1127 list_del(&evt->list);
1128 kfree(evt);
1129 break;
1130 }
1131
1132done_unlock:
1133 mutex_unlock(&param->fault_param->lock);
1134 return ret;
1135}
1136EXPORT_SYMBOL_GPL(iommu_page_response);
1137
d72e31c9
AW
1138/**
1139 * iommu_group_id - Return ID for a group
1140 * @group: the group to ID
1141 *
1142 * Return the unique ID for the group matching the sysfs group number.
1143 */
1144int iommu_group_id(struct iommu_group *group)
1145{
1146 return group->id;
1147}
1148EXPORT_SYMBOL_GPL(iommu_group_id);
1460432c 1149
f096c061
AW
1150static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1151 unsigned long *devfns);
1152
104a1c13
AW
1153/*
1154 * To consider a PCI device isolated, we require ACS to support Source
1155 * Validation, Request Redirection, Completer Redirection, and Upstream
1156 * Forwarding. This effectively means that devices cannot spoof their
1157 * requester ID, requests and completions cannot be redirected, and all
1158 * transactions are forwarded upstream, even as it passes through a
1159 * bridge where the target device is downstream.
1160 */
1161#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1162
f096c061
AW
1163/*
1164 * For multifunction devices which are not isolated from each other, find
1165 * all the other non-isolated functions and look for existing groups. For
1166 * each function, we also need to look for aliases to or from other devices
1167 * that may already have a group.
1168 */
1169static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1170 unsigned long *devfns)
1171{
1172 struct pci_dev *tmp = NULL;
1173 struct iommu_group *group;
1174
1175 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1176 return NULL;
1177
1178 for_each_pci_dev(tmp) {
1179 if (tmp == pdev || tmp->bus != pdev->bus ||
1180 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1181 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1182 continue;
1183
1184 group = get_pci_alias_group(tmp, devfns);
1185 if (group) {
1186 pci_dev_put(tmp);
1187 return group;
1188 }
1189 }
1190
1191 return NULL;
1192}
1193
1194/*
338c3149
JL
1195 * Look for aliases to or from the given device for existing groups. DMA
1196 * aliases are only supported on the same bus, therefore the search
f096c061
AW
1197 * space is quite small (especially since we're really only looking at pcie
1198 * device, and therefore only expect multiple slots on the root complex or
1199 * downstream switch ports). It's conceivable though that a pair of
1200 * multifunction devices could have aliases between them that would cause a
1201 * loop. To prevent this, we use a bitmap to track where we've been.
1202 */
1203static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1204 unsigned long *devfns)
1205{
1206 struct pci_dev *tmp = NULL;
1207 struct iommu_group *group;
1208
1209 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1210 return NULL;
1211
1212 group = iommu_group_get(&pdev->dev);
1213 if (group)
1214 return group;
1215
1216 for_each_pci_dev(tmp) {
1217 if (tmp == pdev || tmp->bus != pdev->bus)
1218 continue;
1219
1220 /* We alias them or they alias us */
338c3149 1221 if (pci_devs_are_dma_aliases(pdev, tmp)) {
f096c061
AW
1222 group = get_pci_alias_group(tmp, devfns);
1223 if (group) {
1224 pci_dev_put(tmp);
1225 return group;
1226 }
1227
1228 group = get_pci_function_alias_group(tmp, devfns);
1229 if (group) {
1230 pci_dev_put(tmp);
1231 return group;
1232 }
1233 }
1234 }
1235
1236 return NULL;
1237}
1238
104a1c13
AW
1239struct group_for_pci_data {
1240 struct pci_dev *pdev;
1241 struct iommu_group *group;
1242};
1243
1244/*
1245 * DMA alias iterator callback, return the last seen device. Stop and return
1246 * the IOMMU group if we find one along the way.
1247 */
1248static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1249{
1250 struct group_for_pci_data *data = opaque;
1251
1252 data->pdev = pdev;
1253 data->group = iommu_group_get(&pdev->dev);
1254
1255 return data->group != NULL;
1256}
1257
6eab556a
JR
1258/*
1259 * Generic device_group call-back function. It just allocates one
1260 * iommu-group per device.
1261 */
1262struct iommu_group *generic_device_group(struct device *dev)
1263{
7f7a2304 1264 return iommu_group_alloc();
6eab556a
JR
1265}
1266
104a1c13
AW
1267/*
1268 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1269 * to find or create an IOMMU group for a device.
1270 */
5e62292b 1271struct iommu_group *pci_device_group(struct device *dev)
104a1c13 1272{
5e62292b 1273 struct pci_dev *pdev = to_pci_dev(dev);
104a1c13
AW
1274 struct group_for_pci_data data;
1275 struct pci_bus *bus;
1276 struct iommu_group *group = NULL;
f096c061 1277 u64 devfns[4] = { 0 };
104a1c13 1278
5e62292b
JR
1279 if (WARN_ON(!dev_is_pci(dev)))
1280 return ERR_PTR(-EINVAL);
1281
104a1c13
AW
1282 /*
1283 * Find the upstream DMA alias for the device. A device must not
1284 * be aliased due to topology in order to have its own IOMMU group.
1285 * If we find an alias along the way that already belongs to a
1286 * group, use it.
1287 */
1288 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1289 return data.group;
1290
1291 pdev = data.pdev;
1292
1293 /*
1294 * Continue upstream from the point of minimum IOMMU granularity
1295 * due to aliases to the point where devices are protected from
1296 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1297 * group, use it.
1298 */
1299 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1300 if (!bus->self)
1301 continue;
1302
1303 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1304 break;
1305
1306 pdev = bus->self;
1307
1308 group = iommu_group_get(&pdev->dev);
1309 if (group)
1310 return group;
1311 }
1312
1313 /*
f096c061
AW
1314 * Look for existing groups on device aliases. If we alias another
1315 * device or another device aliases us, use the same group.
104a1c13 1316 */
f096c061
AW
1317 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1318 if (group)
1319 return group;
104a1c13
AW
1320
1321 /*
f096c061
AW
1322 * Look for existing groups on non-isolated functions on the same
1323 * slot and aliases of those funcions, if any. No need to clear
1324 * the search bitmap, the tested devfns are still valid.
104a1c13 1325 */
f096c061
AW
1326 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1327 if (group)
1328 return group;
104a1c13
AW
1329
1330 /* No shared group found, allocate new */
7f7a2304 1331 return iommu_group_alloc();
104a1c13
AW
1332}
1333
eab03e2a
NG
1334/* Get the IOMMU group for device on fsl-mc bus */
1335struct iommu_group *fsl_mc_device_group(struct device *dev)
1336{
1337 struct device *cont_dev = fsl_mc_cont_dev(dev);
1338 struct iommu_group *group;
1339
1340 group = iommu_group_get(cont_dev);
1341 if (!group)
1342 group = iommu_group_alloc();
1343 return group;
1344}
1345
104a1c13
AW
1346/**
1347 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1348 * @dev: target device
1349 *
1350 * This function is intended to be called by IOMMU drivers and extended to
1351 * support common, bus-defined algorithms when determining or creating the
1352 * IOMMU group for a device. On success, the caller will hold a reference
1353 * to the returned IOMMU group, which will already include the provided
1354 * device. The reference should be released with iommu_group_put().
1355 */
1356struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1357{
46c6b2bc 1358 const struct iommu_ops *ops = dev->bus->iommu_ops;
c4a783b8 1359 struct iommu_group *group;
104a1c13
AW
1360 int ret;
1361
1362 group = iommu_group_get(dev);
1363 if (group)
1364 return group;
1365
05f80300
RM
1366 if (!ops)
1367 return ERR_PTR(-EINVAL);
104a1c13 1368
05f80300 1369 group = ops->device_group(dev);
72dcac63
JR
1370 if (WARN_ON_ONCE(group == NULL))
1371 return ERR_PTR(-EINVAL);
1372
104a1c13
AW
1373 if (IS_ERR(group))
1374 return group;
1375
1228236d
JR
1376 /*
1377 * Try to allocate a default domain - needs support from the
1378 * IOMMU driver.
1379 */
1380 if (!group->default_domain) {
fccb4e3b
WD
1381 struct iommu_domain *dom;
1382
1383 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1384 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
fccb4e3b 1385 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
8bc32a28
JR
1386 if (dom) {
1387 dev_warn(dev,
1388 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1389 iommu_def_domain_type);
1390 }
fccb4e3b
WD
1391 }
1392
1393 group->default_domain = dom;
eebb8034 1394 if (!group->domain)
fccb4e3b 1395 group->domain = dom;
68a6efe8
ZL
1396
1397 if (dom && !iommu_dma_strict) {
1398 int attr = 1;
1399 iommu_domain_set_attr(dom,
1400 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1401 &attr);
1402 }
1228236d
JR
1403 }
1404
104a1c13
AW
1405 ret = iommu_group_add_device(group, dev);
1406 if (ret) {
1407 iommu_group_put(group);
1408 return ERR_PTR(ret);
1409 }
1410
1411 return group;
1412}
1413
6827ca83
JR
1414struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1415{
1416 return group->default_domain;
1417}
1418
d72e31c9 1419static int add_iommu_group(struct device *dev, void *data)
1460432c 1420{
cc5aed44 1421 int ret = iommu_probe_device(dev);
38667f18
JR
1422
1423 /*
1424 * We ignore -ENODEV errors for now, as they just mean that the
1425 * device is not translated by an IOMMU. We still care about
1426 * other errors and fail to initialize when they happen.
1427 */
1428 if (ret == -ENODEV)
1429 ret = 0;
1430
1431 return ret;
1460432c
AW
1432}
1433
8da30142
JR
1434static int remove_iommu_group(struct device *dev, void *data)
1435{
cc5aed44 1436 iommu_release_device(dev);
1460432c
AW
1437
1438 return 0;
1439}
1440
d72e31c9
AW
1441static int iommu_bus_notifier(struct notifier_block *nb,
1442 unsigned long action, void *data)
1460432c 1443{
cc5aed44 1444 unsigned long group_action = 0;
1460432c 1445 struct device *dev = data;
d72e31c9 1446 struct iommu_group *group;
d72e31c9
AW
1447
1448 /*
1449 * ADD/DEL call into iommu driver ops if provided, which may
1450 * result in ADD/DEL notifiers to group->notifier
1451 */
1452 if (action == BUS_NOTIFY_ADD_DEVICE) {
cc5aed44 1453 int ret;
3ba8775f 1454
cc5aed44
JR
1455 ret = iommu_probe_device(dev);
1456 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
843cb6dc 1457 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
cc5aed44
JR
1458 iommu_release_device(dev);
1459 return NOTIFY_OK;
d72e31c9 1460 }
1460432c 1461
d72e31c9
AW
1462 /*
1463 * Remaining BUS_NOTIFYs get filtered and republished to the
1464 * group, if anyone is listening
1465 */
1466 group = iommu_group_get(dev);
1467 if (!group)
1468 return 0;
1460432c 1469
d72e31c9
AW
1470 switch (action) {
1471 case BUS_NOTIFY_BIND_DRIVER:
1472 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1473 break;
1474 case BUS_NOTIFY_BOUND_DRIVER:
1475 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1476 break;
1477 case BUS_NOTIFY_UNBIND_DRIVER:
1478 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1479 break;
1480 case BUS_NOTIFY_UNBOUND_DRIVER:
1481 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1482 break;
1483 }
1460432c 1484
d72e31c9
AW
1485 if (group_action)
1486 blocking_notifier_call_chain(&group->notifier,
1487 group_action, dev);
1460432c 1488
d72e31c9 1489 iommu_group_put(group);
1460432c
AW
1490 return 0;
1491}
1492
fb3e3065 1493static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
ff21776d 1494{
fb3e3065
MS
1495 int err;
1496 struct notifier_block *nb;
b22f6434 1497
fb3e3065
MS
1498 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1499 if (!nb)
1500 return -ENOMEM;
1501
1502 nb->notifier_call = iommu_bus_notifier;
1503
1504 err = bus_register_notifier(bus, nb);
8da30142
JR
1505 if (err)
1506 goto out_free;
d7da6bdc 1507
8cec63e5 1508 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
8da30142
JR
1509 if (err)
1510 goto out_err;
1511
d7da6bdc
HS
1512
1513 return 0;
8da30142
JR
1514
1515out_err:
1516 /* Clean up */
8cec63e5 1517 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
8da30142
JR
1518 bus_unregister_notifier(bus, nb);
1519
1520out_free:
1521 kfree(nb);
1522
1523 return err;
ff21776d 1524}
fc2100eb 1525
ff21776d
JR
1526/**
1527 * bus_set_iommu - set iommu-callbacks for the bus
1528 * @bus: bus.
1529 * @ops: the callbacks provided by the iommu-driver
1530 *
1531 * This function is called by an iommu driver to set the iommu methods
1532 * used for a particular bus. Drivers for devices on that bus can use
1533 * the iommu-api after these ops are registered.
1534 * This special function is needed because IOMMUs are usually devices on
1535 * the bus itself, so the iommu drivers are not initialized when the bus
1536 * is set up. With this function the iommu-driver can set the iommu-ops
1537 * afterwards.
1538 */
b22f6434 1539int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
fc2100eb 1540{
d7da6bdc
HS
1541 int err;
1542
ff21776d
JR
1543 if (bus->iommu_ops != NULL)
1544 return -EBUSY;
fc2100eb 1545
ff21776d
JR
1546 bus->iommu_ops = ops;
1547
1548 /* Do IOMMU specific setup for this bus-type */
d7da6bdc
HS
1549 err = iommu_bus_init(bus, ops);
1550 if (err)
1551 bus->iommu_ops = NULL;
1552
1553 return err;
fc2100eb 1554}
ff21776d 1555EXPORT_SYMBOL_GPL(bus_set_iommu);
fc2100eb 1556
a1b60c1c 1557bool iommu_present(struct bus_type *bus)
fc2100eb 1558{
94441c3b 1559 return bus->iommu_ops != NULL;
fc2100eb 1560}
a1b60c1c 1561EXPORT_SYMBOL_GPL(iommu_present);
fc2100eb 1562
3c0e0ca0
JR
1563bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1564{
1565 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1566 return false;
1567
1568 return bus->iommu_ops->capable(cap);
1569}
1570EXPORT_SYMBOL_GPL(iommu_capable);
1571
4f3f8d9d
OBC
1572/**
1573 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1574 * @domain: iommu domain
1575 * @handler: fault handler
77ca2332 1576 * @token: user data, will be passed back to the fault handler
0ed6d2d2
OBC
1577 *
1578 * This function should be used by IOMMU users which want to be notified
1579 * whenever an IOMMU fault happens.
1580 *
1581 * The fault handler itself should return 0 on success, and an appropriate
1582 * error code otherwise.
4f3f8d9d
OBC
1583 */
1584void iommu_set_fault_handler(struct iommu_domain *domain,
77ca2332
OBC
1585 iommu_fault_handler_t handler,
1586 void *token)
4f3f8d9d
OBC
1587{
1588 BUG_ON(!domain);
1589
1590 domain->handler = handler;
77ca2332 1591 domain->handler_token = token;
4f3f8d9d 1592}
30bd918c 1593EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
4f3f8d9d 1594
53723dc5
JR
1595static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1596 unsigned type)
fc2100eb
JR
1597{
1598 struct iommu_domain *domain;
fc2100eb 1599
94441c3b 1600 if (bus == NULL || bus->iommu_ops == NULL)
905d66c1
JR
1601 return NULL;
1602
53723dc5 1603 domain = bus->iommu_ops->domain_alloc(type);
fc2100eb
JR
1604 if (!domain)
1605 return NULL;
1606
8539c7c1 1607 domain->ops = bus->iommu_ops;
53723dc5 1608 domain->type = type;
d16e0faa
RM
1609 /* Assume all sizes by default; the driver may override this later */
1610 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
905d66c1 1611
fc2100eb 1612 return domain;
fc2100eb 1613}
fc2100eb 1614
53723dc5
JR
1615struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1616{
1617 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
fc2100eb
JR
1618}
1619EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1620
1621void iommu_domain_free(struct iommu_domain *domain)
1622{
89be34a1 1623 domain->ops->domain_free(domain);
fc2100eb
JR
1624}
1625EXPORT_SYMBOL_GPL(iommu_domain_free);
1626
426a2738
JR
1627static int __iommu_attach_device(struct iommu_domain *domain,
1628 struct device *dev)
fc2100eb 1629{
b54db778 1630 int ret;
e01d1913
BH
1631 if ((domain->ops->is_attach_deferred != NULL) &&
1632 domain->ops->is_attach_deferred(domain, dev))
1633 return 0;
1634
e5aa7f00
JR
1635 if (unlikely(domain->ops->attach_dev == NULL))
1636 return -ENODEV;
1637
b54db778
SK
1638 ret = domain->ops->attach_dev(domain, dev);
1639 if (!ret)
1640 trace_attach_device_to_domain(dev);
1641 return ret;
fc2100eb 1642}
426a2738
JR
1643
1644int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1645{
1646 struct iommu_group *group;
1647 int ret;
1648
1649 group = iommu_group_get(dev);
9ae9df03
JC
1650 if (!group)
1651 return -ENODEV;
1652
426a2738 1653 /*
05f80300 1654 * Lock the group to make sure the device-count doesn't
426a2738
JR
1655 * change while we are attaching
1656 */
1657 mutex_lock(&group->mutex);
1658 ret = -EINVAL;
1659 if (iommu_group_device_count(group) != 1)
1660 goto out_unlock;
1661
e39cb8a3 1662 ret = __iommu_attach_group(domain, group);
426a2738
JR
1663
1664out_unlock:
1665 mutex_unlock(&group->mutex);
1666 iommu_group_put(group);
1667
1668 return ret;
1669}
fc2100eb
JR
1670EXPORT_SYMBOL_GPL(iommu_attach_device);
1671
426a2738
JR
1672static void __iommu_detach_device(struct iommu_domain *domain,
1673 struct device *dev)
fc2100eb 1674{
e01d1913
BH
1675 if ((domain->ops->is_attach_deferred != NULL) &&
1676 domain->ops->is_attach_deferred(domain, dev))
1677 return;
1678
e5aa7f00
JR
1679 if (unlikely(domain->ops->detach_dev == NULL))
1680 return;
1681
1682 domain->ops->detach_dev(domain, dev);
69980630 1683 trace_detach_device_from_domain(dev);
fc2100eb 1684}
426a2738
JR
1685
1686void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1687{
1688 struct iommu_group *group;
1689
1690 group = iommu_group_get(dev);
9ae9df03
JC
1691 if (!group)
1692 return;
426a2738
JR
1693
1694 mutex_lock(&group->mutex);
1695 if (iommu_group_device_count(group) != 1) {
1696 WARN_ON(1);
1697 goto out_unlock;
1698 }
1699
e39cb8a3 1700 __iommu_detach_group(domain, group);
426a2738
JR
1701
1702out_unlock:
1703 mutex_unlock(&group->mutex);
1704 iommu_group_put(group);
1705}
fc2100eb
JR
1706EXPORT_SYMBOL_GPL(iommu_detach_device);
1707
2c1296d9
JR
1708struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1709{
1710 struct iommu_domain *domain;
1711 struct iommu_group *group;
1712
1713 group = iommu_group_get(dev);
1464d0b1 1714 if (!group)
2c1296d9
JR
1715 return NULL;
1716
1717 domain = group->domain;
1718
1719 iommu_group_put(group);
1720
1721 return domain;
1722}
1723EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
fc2100eb 1724
d72e31c9 1725/*
6af588fe
RM
1726 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1727 * guarantees that the group and its default domain are valid and correct.
1728 */
1729struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1730{
1731 return dev->iommu_group->default_domain;
1732}
1733
d72e31c9 1734/*
35449adc 1735 * IOMMU groups are really the natural working unit of the IOMMU, but
d72e31c9
AW
1736 * the IOMMU API works on domains and devices. Bridge that gap by
1737 * iterating over the devices in a group. Ideally we'd have a single
1738 * device which represents the requestor ID of the group, but we also
1739 * allow IOMMU drivers to create policy defined minimum sets, where
1740 * the physical hardware may be able to distiguish members, but we
1741 * wish to group them at a higher level (ex. untrusted multi-function
1742 * PCI devices). Thus we attach each device.
1743 */
1744static int iommu_group_do_attach_device(struct device *dev, void *data)
1745{
1746 struct iommu_domain *domain = data;
1747
426a2738 1748 return __iommu_attach_device(domain, dev);
d72e31c9
AW
1749}
1750
e39cb8a3
JR
1751static int __iommu_attach_group(struct iommu_domain *domain,
1752 struct iommu_group *group)
1753{
1754 int ret;
1755
1756 if (group->default_domain && group->domain != group->default_domain)
1757 return -EBUSY;
1758
1759 ret = __iommu_group_for_each_dev(group, domain,
1760 iommu_group_do_attach_device);
1761 if (ret == 0)
1762 group->domain = domain;
1763
1764 return ret;
d72e31c9
AW
1765}
1766
1767int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1768{
e39cb8a3
JR
1769 int ret;
1770
1771 mutex_lock(&group->mutex);
1772 ret = __iommu_attach_group(domain, group);
1773 mutex_unlock(&group->mutex);
1774
1775 return ret;
d72e31c9
AW
1776}
1777EXPORT_SYMBOL_GPL(iommu_attach_group);
1778
1779static int iommu_group_do_detach_device(struct device *dev, void *data)
1780{
1781 struct iommu_domain *domain = data;
1782
426a2738 1783 __iommu_detach_device(domain, dev);
d72e31c9
AW
1784
1785 return 0;
1786}
1787
e39cb8a3
JR
1788static void __iommu_detach_group(struct iommu_domain *domain,
1789 struct iommu_group *group)
1790{
1791 int ret;
1792
1793 if (!group->default_domain) {
1794 __iommu_group_for_each_dev(group, domain,
1795 iommu_group_do_detach_device);
1796 group->domain = NULL;
1797 return;
1798 }
1799
1800 if (group->domain == group->default_domain)
1801 return;
1802
1803 /* Detach by re-attaching to the default domain */
1804 ret = __iommu_group_for_each_dev(group, group->default_domain,
1805 iommu_group_do_attach_device);
1806 if (ret != 0)
1807 WARN_ON(1);
1808 else
1809 group->domain = group->default_domain;
1810}
1811
d72e31c9
AW
1812void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1813{
e39cb8a3
JR
1814 mutex_lock(&group->mutex);
1815 __iommu_detach_group(domain, group);
1816 mutex_unlock(&group->mutex);
d72e31c9
AW
1817}
1818EXPORT_SYMBOL_GPL(iommu_detach_group);
1819
bb5547ac 1820phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
fc2100eb 1821{
e5aa7f00
JR
1822 if (unlikely(domain->ops->iova_to_phys == NULL))
1823 return 0;
1824
1825 return domain->ops->iova_to_phys(domain, iova);
fc2100eb
JR
1826}
1827EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
dbb9fd86 1828
bd13969b
AW
1829static size_t iommu_pgsize(struct iommu_domain *domain,
1830 unsigned long addr_merge, size_t size)
1831{
1832 unsigned int pgsize_idx;
1833 size_t pgsize;
1834
1835 /* Max page size that still fits into 'size' */
1836 pgsize_idx = __fls(size);
1837
1838 /* need to consider alignment requirements ? */
1839 if (likely(addr_merge)) {
1840 /* Max page size allowed by address */
1841 unsigned int align_pgsize_idx = __ffs(addr_merge);
1842 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1843 }
1844
1845 /* build a mask of acceptable page sizes */
1846 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1847
1848 /* throw away page sizes not supported by the hardware */
d16e0faa 1849 pgsize &= domain->pgsize_bitmap;
bd13969b
AW
1850
1851 /* make sure we're still sane */
1852 BUG_ON(!pgsize);
1853
1854 /* pick the biggest page */
1855 pgsize_idx = __fls(pgsize);
1856 pgsize = 1UL << pgsize_idx;
1857
1858 return pgsize;
1859}
1860
cefc53c7 1861int iommu_map(struct iommu_domain *domain, unsigned long iova,
7d3002cc 1862 phys_addr_t paddr, size_t size, int prot)
cefc53c7 1863{
1d7ae53b 1864 const struct iommu_ops *ops = domain->ops;
7d3002cc
OBC
1865 unsigned long orig_iova = iova;
1866 unsigned int min_pagesz;
1867 size_t orig_size = size;
06bfcaa9 1868 phys_addr_t orig_paddr = paddr;
7d3002cc 1869 int ret = 0;
cefc53c7 1870
1d7ae53b 1871 if (unlikely(ops->map == NULL ||
d16e0faa 1872 domain->pgsize_bitmap == 0UL))
e5aa7f00 1873 return -ENODEV;
cefc53c7 1874
a10315e5
JR
1875 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1876 return -EINVAL;
1877
7d3002cc 1878 /* find out the minimum page size supported */
d16e0faa 1879 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
7d3002cc
OBC
1880
1881 /*
1882 * both the virtual address and the physical one, as well as
1883 * the size of the mapping, must be aligned (at least) to the
1884 * size of the smallest page supported by the hardware
1885 */
1886 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
abedb049 1887 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
6197ca82 1888 iova, &paddr, size, min_pagesz);
7d3002cc
OBC
1889 return -EINVAL;
1890 }
1891
abedb049 1892 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
7d3002cc
OBC
1893
1894 while (size) {
bd13969b 1895 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
7d3002cc 1896
abedb049 1897 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
6197ca82 1898 iova, &paddr, pgsize);
7d3002cc 1899
1d7ae53b 1900 ret = ops->map(domain, iova, paddr, pgsize, prot);
7d3002cc
OBC
1901 if (ret)
1902 break;
1903
1904 iova += pgsize;
1905 paddr += pgsize;
1906 size -= pgsize;
1907 }
1908
1d7ae53b
DO
1909 if (ops->iotlb_sync_map)
1910 ops->iotlb_sync_map(domain);
1911
7d3002cc
OBC
1912 /* unroll mapping in case something went wrong */
1913 if (ret)
1914 iommu_unmap(domain, orig_iova, orig_size - size);
e0be7c86 1915 else
06bfcaa9 1916 trace_map(orig_iova, orig_paddr, orig_size);
7d3002cc
OBC
1917
1918 return ret;
cefc53c7
JR
1919}
1920EXPORT_SYMBOL_GPL(iommu_map);
1921
add02cfd
JR
1922static size_t __iommu_unmap(struct iommu_domain *domain,
1923 unsigned long iova, size_t size,
a7d20dc1 1924 struct iommu_iotlb_gather *iotlb_gather)
cefc53c7 1925{
add02cfd 1926 const struct iommu_ops *ops = domain->ops;
7d3002cc 1927 size_t unmapped_page, unmapped = 0;
6fd492fd 1928 unsigned long orig_iova = iova;
add02cfd 1929 unsigned int min_pagesz;
cefc53c7 1930
add02cfd 1931 if (unlikely(ops->unmap == NULL ||
d16e0faa 1932 domain->pgsize_bitmap == 0UL))
c5611a87 1933 return 0;
e5aa7f00 1934
a10315e5 1935 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
c5611a87 1936 return 0;
a10315e5 1937
7d3002cc 1938 /* find out the minimum page size supported */
d16e0faa 1939 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
7d3002cc
OBC
1940
1941 /*
1942 * The virtual address, as well as the size of the mapping, must be
1943 * aligned (at least) to the size of the smallest page supported
1944 * by the hardware
1945 */
1946 if (!IS_ALIGNED(iova | size, min_pagesz)) {
6197ca82
JP
1947 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1948 iova, size, min_pagesz);
c5611a87 1949 return 0;
7d3002cc
OBC
1950 }
1951
6197ca82 1952 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
7d3002cc
OBC
1953
1954 /*
1955 * Keep iterating until we either unmap 'size' bytes (or more)
1956 * or we hit an area that isn't mapped.
1957 */
1958 while (unmapped < size) {
bd13969b 1959 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
7d3002cc 1960
56f8af5e 1961 unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather);
7d3002cc
OBC
1962 if (!unmapped_page)
1963 break;
1964
6197ca82
JP
1965 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1966 iova, unmapped_page);
7d3002cc
OBC
1967
1968 iova += unmapped_page;
1969 unmapped += unmapped_page;
1970 }
1971
db8614d3 1972 trace_unmap(orig_iova, size, unmapped);
7d3002cc 1973 return unmapped;
cefc53c7 1974}
add02cfd
JR
1975
1976size_t iommu_unmap(struct iommu_domain *domain,
1977 unsigned long iova, size_t size)
1978{
a7d20dc1
WD
1979 struct iommu_iotlb_gather iotlb_gather;
1980 size_t ret;
1981
1982 iommu_iotlb_gather_init(&iotlb_gather);
1983 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
1984 iommu_tlb_sync(domain, &iotlb_gather);
1985
1986 return ret;
add02cfd 1987}
cefc53c7 1988EXPORT_SYMBOL_GPL(iommu_unmap);
1460432c 1989
add02cfd 1990size_t iommu_unmap_fast(struct iommu_domain *domain,
a7d20dc1
WD
1991 unsigned long iova, size_t size,
1992 struct iommu_iotlb_gather *iotlb_gather)
add02cfd 1993{
a7d20dc1 1994 return __iommu_unmap(domain, iova, size, iotlb_gather);
add02cfd
JR
1995}
1996EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1997
d88e61fa
CH
1998size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1999 struct scatterlist *sg, unsigned int nents, int prot)
315786eb 2000{
5d95f40e
RM
2001 size_t len = 0, mapped = 0;
2002 phys_addr_t start;
2003 unsigned int i = 0;
38ec010d 2004 int ret;
315786eb 2005
5d95f40e
RM
2006 while (i <= nents) {
2007 phys_addr_t s_phys = sg_phys(sg);
18f23409 2008
5d95f40e
RM
2009 if (len && s_phys != start + len) {
2010 ret = iommu_map(domain, iova + mapped, start, len, prot);
2011 if (ret)
2012 goto out_err;
18f23409 2013
5d95f40e
RM
2014 mapped += len;
2015 len = 0;
2016 }
38ec010d 2017
5d95f40e
RM
2018 if (len) {
2019 len += sg->length;
2020 } else {
2021 len = sg->length;
2022 start = s_phys;
2023 }
38ec010d 2024
5d95f40e
RM
2025 if (++i < nents)
2026 sg = sg_next(sg);
315786eb
OH
2027 }
2028
2029 return mapped;
38ec010d
JR
2030
2031out_err:
2032 /* undo mappings already done */
2033 iommu_unmap(domain, iova, mapped);
2034
2035 return 0;
2036
315786eb 2037}
d88e61fa 2038EXPORT_SYMBOL_GPL(iommu_map_sg);
d7787d57
JR
2039
2040int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
80f97f0f 2041 phys_addr_t paddr, u64 size, int prot)
d7787d57
JR
2042{
2043 if (unlikely(domain->ops->domain_window_enable == NULL))
2044 return -ENODEV;
2045
80f97f0f
VS
2046 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
2047 prot);
d7787d57
JR
2048}
2049EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
2050
2051void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
2052{
2053 if (unlikely(domain->ops->domain_window_disable == NULL))
2054 return;
2055
2056 return domain->ops->domain_window_disable(domain, wnd_nr);
2057}
2058EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
2059
207c6e36
JR
2060/**
2061 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2062 * @domain: the iommu domain where the fault has happened
2063 * @dev: the device where the fault has happened
2064 * @iova: the faulting address
2065 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2066 *
2067 * This function should be called by the low-level IOMMU implementations
2068 * whenever IOMMU faults happen, to allow high-level users, that are
2069 * interested in such events, to know about them.
2070 *
2071 * This event may be useful for several possible use cases:
2072 * - mere logging of the event
2073 * - dynamic TLB/PTE loading
2074 * - if restarting of the faulting device is required
2075 *
2076 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2077 * PTE/TLB loading will one day be supported, implementations will be able
2078 * to tell whether it succeeded or not according to this return value).
2079 *
2080 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2081 * (though fault handlers can also return -ENOSYS, in case they want to
2082 * elicit the default behavior of the IOMMU drivers).
2083 */
2084int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2085 unsigned long iova, int flags)
2086{
2087 int ret = -ENOSYS;
2088
2089 /*
2090 * if upper layers showed interest and installed a fault handler,
2091 * invoke it.
2092 */
2093 if (domain->handler)
2094 ret = domain->handler(domain, dev, iova, flags,
2095 domain->handler_token);
2096
2097 trace_io_page_fault(dev, iova, flags);
2098 return ret;
2099}
2100EXPORT_SYMBOL_GPL(report_iommu_fault);
2101
d72e31c9 2102static int __init iommu_init(void)
1460432c 2103{
d72e31c9
AW
2104 iommu_group_kset = kset_create_and_add("iommu_groups",
2105 NULL, kernel_kobj);
d72e31c9
AW
2106 BUG_ON(!iommu_group_kset);
2107
bad614b2
GH
2108 iommu_debugfs_setup();
2109
d72e31c9 2110 return 0;
1460432c 2111}
d7ef9995 2112core_initcall(iommu_init);
0cd76dd1
JR
2113
2114int iommu_domain_get_attr(struct iommu_domain *domain,
2115 enum iommu_attr attr, void *data)
2116{
0ff64f80 2117 struct iommu_domain_geometry *geometry;
d2e12160 2118 bool *paging;
0ff64f80
JR
2119 int ret = 0;
2120
2121 switch (attr) {
2122 case DOMAIN_ATTR_GEOMETRY:
2123 geometry = data;
2124 *geometry = domain->geometry;
2125
d2e12160
JR
2126 break;
2127 case DOMAIN_ATTR_PAGING:
2128 paging = data;
d16e0faa 2129 *paging = (domain->pgsize_bitmap != 0UL);
0ff64f80
JR
2130 break;
2131 default:
2132 if (!domain->ops->domain_get_attr)
2133 return -EINVAL;
0cd76dd1 2134
0ff64f80
JR
2135 ret = domain->ops->domain_get_attr(domain, attr, data);
2136 }
2137
2138 return ret;
0cd76dd1
JR
2139}
2140EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
2141
2142int iommu_domain_set_attr(struct iommu_domain *domain,
2143 enum iommu_attr attr, void *data)
2144{
69356712 2145 int ret = 0;
69356712
JR
2146
2147 switch (attr) {
69356712
JR
2148 default:
2149 if (domain->ops->domain_set_attr == NULL)
2150 return -EINVAL;
2151
2152 ret = domain->ops->domain_set_attr(domain, attr, data);
2153 }
2154
2155 return ret;
1460432c 2156}
0cd76dd1 2157EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
a1015c2b 2158
e5b5234a 2159void iommu_get_resv_regions(struct device *dev, struct list_head *list)
a1015c2b
JR
2160{
2161 const struct iommu_ops *ops = dev->bus->iommu_ops;
2162
e5b5234a
EA
2163 if (ops && ops->get_resv_regions)
2164 ops->get_resv_regions(dev, list);
a1015c2b
JR
2165}
2166
e5b5234a 2167void iommu_put_resv_regions(struct device *dev, struct list_head *list)
a1015c2b
JR
2168{
2169 const struct iommu_ops *ops = dev->bus->iommu_ops;
2170
e5b5234a
EA
2171 if (ops && ops->put_resv_regions)
2172 ops->put_resv_regions(dev, list);
a1015c2b 2173}
d290f1e7 2174
2b20cbba 2175struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
9d3a4de4
RM
2176 size_t length, int prot,
2177 enum iommu_resv_type type)
2b20cbba
EA
2178{
2179 struct iommu_resv_region *region;
2180
2181 region = kzalloc(sizeof(*region), GFP_KERNEL);
2182 if (!region)
2183 return NULL;
2184
2185 INIT_LIST_HEAD(&region->list);
2186 region->start = start;
2187 region->length = length;
2188 region->prot = prot;
2189 region->type = type;
2190 return region;
a1015c2b 2191}
d290f1e7 2192
7423e017
LB
2193static int
2194request_default_domain_for_dev(struct device *dev, unsigned long type)
d290f1e7 2195{
7423e017 2196 struct iommu_domain *domain;
d290f1e7
JR
2197 struct iommu_group *group;
2198 int ret;
2199
2200 /* Device must already be in a group before calling this function */
57274ea2
LB
2201 group = iommu_group_get(dev);
2202 if (!group)
2203 return -EINVAL;
d290f1e7
JR
2204
2205 mutex_lock(&group->mutex);
2206
d290f1e7 2207 ret = 0;
7423e017 2208 if (group->default_domain && group->default_domain->type == type)
d290f1e7
JR
2209 goto out;
2210
2211 /* Don't change mappings of existing devices */
2212 ret = -EBUSY;
2213 if (iommu_group_device_count(group) != 1)
2214 goto out;
2215
d290f1e7 2216 ret = -ENOMEM;
7423e017
LB
2217 domain = __iommu_domain_alloc(dev->bus, type);
2218 if (!domain)
d290f1e7
JR
2219 goto out;
2220
2221 /* Attach the device to the domain */
7423e017 2222 ret = __iommu_attach_group(domain, group);
d290f1e7 2223 if (ret) {
7423e017 2224 iommu_domain_free(domain);
d290f1e7
JR
2225 goto out;
2226 }
2227
d127bc9b 2228 /* Make the domain the default for this group */
d290f1e7
JR
2229 if (group->default_domain)
2230 iommu_domain_free(group->default_domain);
7423e017 2231 group->default_domain = domain;
d290f1e7 2232
53c4126c
JS
2233 iommu_group_create_direct_mappings(group, dev);
2234
7423e017
LB
2235 dev_info(dev, "Using iommu %s mapping\n",
2236 type == IOMMU_DOMAIN_DMA ? "dma" : "direct");
d290f1e7
JR
2237
2238 ret = 0;
2239out:
2240 mutex_unlock(&group->mutex);
2241 iommu_group_put(group);
2242
2243 return ret;
2244}
57f98d2f 2245
7423e017
LB
2246/* Request that a device is direct mapped by the IOMMU */
2247int iommu_request_dm_for_dev(struct device *dev)
2248{
2249 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY);
2250}
2251
2252/* Request that a device can't be direct mapped by the IOMMU */
2253int iommu_request_dma_domain_for_dev(struct device *dev)
2254{
2255 return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA);
2256}
2257
8a69961c
JR
2258void iommu_set_default_passthrough(bool cmd_line)
2259{
2260 if (cmd_line)
2261 iommu_set_cmd_line_dma_api();
2262
2263 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2264}
2265
2266void iommu_set_default_translated(bool cmd_line)
2267{
2268 if (cmd_line)
2269 iommu_set_cmd_line_dma_api();
2270
2271 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2272}
2273
2274bool iommu_default_passthrough(void)
2275{
2276 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2277}
2278EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2279
534766df 2280const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
e4f10ffe 2281{
e4f10ffe 2282 const struct iommu_ops *ops = NULL;
d0f6f583 2283 struct iommu_device *iommu;
e4f10ffe 2284
d0f6f583
JR
2285 spin_lock(&iommu_device_lock);
2286 list_for_each_entry(iommu, &iommu_device_list, list)
2287 if (iommu->fwnode == fwnode) {
2288 ops = iommu->ops;
e4f10ffe
LP
2289 break;
2290 }
d0f6f583 2291 spin_unlock(&iommu_device_lock);
e4f10ffe
LP
2292 return ops;
2293}
2294
57f98d2f
RM
2295int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2296 const struct iommu_ops *ops)
2297{
b4ef725e 2298 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
2299
2300 if (fwspec)
2301 return ops == fwspec->ops ? 0 : -EINVAL;
2302
2303 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
2304 if (!fwspec)
2305 return -ENOMEM;
2306
2307 of_node_get(to_of_node(iommu_fwnode));
2308 fwspec->iommu_fwnode = iommu_fwnode;
2309 fwspec->ops = ops;
b4ef725e 2310 dev_iommu_fwspec_set(dev, fwspec);
57f98d2f
RM
2311 return 0;
2312}
2313EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2314
2315void iommu_fwspec_free(struct device *dev)
2316{
b4ef725e 2317 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
2318
2319 if (fwspec) {
2320 fwnode_handle_put(fwspec->iommu_fwnode);
2321 kfree(fwspec);
b4ef725e 2322 dev_iommu_fwspec_set(dev, NULL);
57f98d2f
RM
2323 }
2324}
2325EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2326
2327int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2328{
b4ef725e 2329 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
2330 size_t size;
2331 int i;
2332
2333 if (!fwspec)
2334 return -EINVAL;
2335
2336 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2337 if (size > sizeof(*fwspec)) {
b4ef725e 2338 fwspec = krealloc(fwspec, size, GFP_KERNEL);
57f98d2f
RM
2339 if (!fwspec)
2340 return -ENOMEM;
909111ba 2341
b4ef725e 2342 dev_iommu_fwspec_set(dev, fwspec);
57f98d2f
RM
2343 }
2344
2345 for (i = 0; i < num_ids; i++)
2346 fwspec->ids[fwspec->num_ids + i] = ids[i];
2347
2348 fwspec->num_ids += num_ids;
57f98d2f
RM
2349 return 0;
2350}
2351EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
a3a19592
LB
2352
2353/*
2354 * Per device IOMMU features.
2355 */
2356bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2357{
2358 const struct iommu_ops *ops = dev->bus->iommu_ops;
2359
2360 if (ops && ops->dev_has_feat)
2361 return ops->dev_has_feat(dev, feat);
2362
2363 return false;
2364}
2365EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2366
2367int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2368{
2369 const struct iommu_ops *ops = dev->bus->iommu_ops;
2370
2371 if (ops && ops->dev_enable_feat)
2372 return ops->dev_enable_feat(dev, feat);
2373
2374 return -ENODEV;
2375}
2376EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2377
2378/*
2379 * The device drivers should do the necessary cleanups before calling this.
2380 * For example, before disabling the aux-domain feature, the device driver
2381 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2382 */
2383int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2384{
2385 const struct iommu_ops *ops = dev->bus->iommu_ops;
2386
2387 if (ops && ops->dev_disable_feat)
2388 return ops->dev_disable_feat(dev, feat);
2389
2390 return -EBUSY;
2391}
2392EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2393
2394bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2395{
2396 const struct iommu_ops *ops = dev->bus->iommu_ops;
2397
2398 if (ops && ops->dev_feat_enabled)
2399 return ops->dev_feat_enabled(dev, feat);
2400
2401 return false;
2402}
2403EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2404
2405/*
2406 * Aux-domain specific attach/detach.
2407 *
2408 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2409 * true. Also, as long as domains are attached to a device through this
2410 * interface, any tries to call iommu_attach_device() should fail
2411 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2412 * This should make us safe against a device being attached to a guest as a
2413 * whole while there are still pasid users on it (aux and sva).
2414 */
2415int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2416{
2417 int ret = -ENODEV;
2418
2419 if (domain->ops->aux_attach_dev)
2420 ret = domain->ops->aux_attach_dev(domain, dev);
2421
2422 if (!ret)
2423 trace_attach_device_to_domain(dev);
2424
2425 return ret;
2426}
2427EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2428
2429void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2430{
2431 if (domain->ops->aux_detach_dev) {
2432 domain->ops->aux_detach_dev(domain, dev);
2433 trace_detach_device_from_domain(dev);
2434 }
2435}
2436EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2437
2438int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2439{
2440 int ret = -ENODEV;
2441
2442 if (domain->ops->aux_get_pasid)
2443 ret = domain->ops->aux_get_pasid(domain, dev);
2444
2445 return ret;
2446}
2447EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
26b25a2b
JPB
2448
2449/**
2450 * iommu_sva_bind_device() - Bind a process address space to a device
2451 * @dev: the device
2452 * @mm: the mm to bind, caller must hold a reference to it
2453 *
2454 * Create a bond between device and address space, allowing the device to access
2455 * the mm using the returned PASID. If a bond already exists between @device and
2456 * @mm, it is returned and an additional reference is taken. Caller must call
2457 * iommu_sva_unbind_device() to release each reference.
2458 *
2459 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2460 * initialize the required SVA features.
2461 *
2462 * On error, returns an ERR_PTR value.
2463 */
2464struct iommu_sva *
2465iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2466{
2467 struct iommu_group *group;
2468 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2469 const struct iommu_ops *ops = dev->bus->iommu_ops;
2470
2471 if (!ops || !ops->sva_bind)
2472 return ERR_PTR(-ENODEV);
2473
2474 group = iommu_group_get(dev);
2475 if (!group)
2476 return ERR_PTR(-ENODEV);
2477
2478 /* Ensure device count and domain don't change while we're binding */
2479 mutex_lock(&group->mutex);
2480
2481 /*
2482 * To keep things simple, SVA currently doesn't support IOMMU groups
2483 * with more than one device. Existing SVA-capable systems are not
2484 * affected by the problems that required IOMMU groups (lack of ACS
2485 * isolation, device ID aliasing and other hardware issues).
2486 */
2487 if (iommu_group_device_count(group) != 1)
2488 goto out_unlock;
2489
2490 handle = ops->sva_bind(dev, mm, drvdata);
2491
2492out_unlock:
2493 mutex_unlock(&group->mutex);
2494 iommu_group_put(group);
2495
2496 return handle;
2497}
2498EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2499
2500/**
2501 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2502 * @handle: the handle returned by iommu_sva_bind_device()
2503 *
2504 * Put reference to a bond between device and address space. The device should
2505 * not be issuing any more transaction for this PASID. All outstanding page
2506 * requests for this PASID must have been flushed to the IOMMU.
2507 *
2508 * Returns 0 on success, or an error value
2509 */
2510void iommu_sva_unbind_device(struct iommu_sva *handle)
2511{
2512 struct iommu_group *group;
2513 struct device *dev = handle->dev;
2514 const struct iommu_ops *ops = dev->bus->iommu_ops;
2515
2516 if (!ops || !ops->sva_unbind)
2517 return;
2518
2519 group = iommu_group_get(dev);
2520 if (!group)
2521 return;
2522
2523 mutex_lock(&group->mutex);
2524 ops->sva_unbind(handle);
2525 mutex_unlock(&group->mutex);
2526
2527 iommu_group_put(group);
2528}
2529EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2530
2531int iommu_sva_set_ops(struct iommu_sva *handle,
2532 const struct iommu_sva_ops *sva_ops)
2533{
2534 if (handle->ops && handle->ops != sva_ops)
2535 return -EEXIST;
2536
2537 handle->ops = sva_ops;
2538 return 0;
2539}
2540EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2541
2542int iommu_sva_get_pasid(struct iommu_sva *handle)
2543{
2544 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2545
2546 if (!ops || !ops->sva_get_pasid)
2547 return IOMMU_PASID_INVALID;
2548
2549 return ops->sva_get_pasid(handle);
2550}
2551EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);