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