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