]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/iommu/iommu.c
Merge branches 'for-5.1/upstream-fixes', 'for-5.2/core', 'for-5.2/ish', 'for-5.2...
[mirror_ubuntu-kernels.git] / drivers / iommu / iommu.c
CommitLineData
fc2100eb
JR
1/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
63ce3ae8 3 * Author: Joerg Roedel <jroedel@suse.de>
fc2100eb
JR
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
92e7066f 19#define pr_fmt(fmt) "iommu: " fmt
7d3002cc 20
905d66c1 21#include <linux/device.h>
40998188 22#include <linux/kernel.h>
fc2100eb
JR
23#include <linux/bug.h>
24#include <linux/types.h>
c1af7b40
PG
25#include <linux/init.h>
26#include <linux/export.h>
60db4027 27#include <linux/slab.h>
fc2100eb
JR
28#include <linux/errno.h>
29#include <linux/iommu.h>
d72e31c9
AW
30#include <linux/idr.h>
31#include <linux/notifier.h>
32#include <linux/err.h>
104a1c13 33#include <linux/pci.h>
f096c061 34#include <linux/bitops.h>
57f98d2f 35#include <linux/property.h>
eab03e2a 36#include <linux/fsl/mc.h>
7f6db171 37#include <trace/events/iommu.h>
d72e31c9
AW
38
39static struct kset *iommu_group_kset;
e38d1f13 40static DEFINE_IDA(iommu_group_ida);
58d11317
OJ
41#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
42static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
43#else
fccb4e3b 44static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
58d11317 45#endif
68a6efe8 46static bool iommu_dma_strict __read_mostly = true;
d72e31c9 47
b22f6434
TR
48struct iommu_callback_data {
49 const struct iommu_ops *ops;
50};
51
d72e31c9
AW
52struct iommu_group {
53 struct kobject kobj;
54 struct kobject *devices_kobj;
55 struct list_head devices;
56 struct mutex mutex;
57 struct blocking_notifier_head notifier;
58 void *iommu_data;
59 void (*iommu_data_release)(void *iommu_data);
60 char *name;
61 int id;
53723dc5 62 struct iommu_domain *default_domain;
e39cb8a3 63 struct iommu_domain *domain;
d72e31c9
AW
64};
65
c09e22d5 66struct group_device {
d72e31c9
AW
67 struct list_head list;
68 struct device *dev;
69 char *name;
70};
71
72struct iommu_group_attribute {
73 struct attribute attr;
74 ssize_t (*show)(struct iommu_group *group, char *buf);
75 ssize_t (*store)(struct iommu_group *group,
76 const char *buf, size_t count);
77};
78
bc7d12b9
EA
79static const char * const iommu_group_resv_type_string[] = {
80 [IOMMU_RESV_DIRECT] = "direct",
81 [IOMMU_RESV_RESERVED] = "reserved",
82 [IOMMU_RESV_MSI] = "msi",
9d3a4de4 83 [IOMMU_RESV_SW_MSI] = "msi",
bc7d12b9
EA
84};
85
d72e31c9
AW
86#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
87struct iommu_group_attribute iommu_group_attr_##_name = \
88 __ATTR(_name, _mode, _show, _store)
fc2100eb 89
d72e31c9
AW
90#define to_iommu_group_attr(_attr) \
91 container_of(_attr, struct iommu_group_attribute, attr)
92#define to_iommu_group(_kobj) \
93 container_of(_kobj, struct iommu_group, kobj)
fc2100eb 94
b0119e87
JR
95static LIST_HEAD(iommu_device_list);
96static DEFINE_SPINLOCK(iommu_device_lock);
97
98int iommu_device_register(struct iommu_device *iommu)
99{
100 spin_lock(&iommu_device_lock);
101 list_add_tail(&iommu->list, &iommu_device_list);
102 spin_unlock(&iommu_device_lock);
103
104 return 0;
105}
106
107void iommu_device_unregister(struct iommu_device *iommu)
108{
109 spin_lock(&iommu_device_lock);
110 list_del(&iommu->list);
111 spin_unlock(&iommu_device_lock);
112}
113
cc5aed44
JR
114int iommu_probe_device(struct device *dev)
115{
116 const struct iommu_ops *ops = dev->bus->iommu_ops;
dc9de8a2 117 int ret = -EINVAL;
cc5aed44
JR
118
119 WARN_ON(dev->iommu_group);
120
dc9de8a2
JR
121 if (ops)
122 ret = ops->add_device(dev);
123
124 return ret;
cc5aed44
JR
125}
126
127void iommu_release_device(struct device *dev)
128{
129 const struct iommu_ops *ops = dev->bus->iommu_ops;
130
131 if (dev->iommu_group)
132 ops->remove_device(dev);
133}
134
53723dc5
JR
135static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
136 unsigned type);
e39cb8a3
JR
137static int __iommu_attach_device(struct iommu_domain *domain,
138 struct device *dev);
139static int __iommu_attach_group(struct iommu_domain *domain,
140 struct iommu_group *group);
141static void __iommu_detach_group(struct iommu_domain *domain,
142 struct iommu_group *group);
53723dc5 143
fccb4e3b
WD
144static int __init iommu_set_def_domain_type(char *str)
145{
146 bool pt;
7f9584df 147 int ret;
fccb4e3b 148
7f9584df
AS
149 ret = kstrtobool(str, &pt);
150 if (ret)
151 return ret;
fccb4e3b
WD
152
153 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
154 return 0;
155}
156early_param("iommu.passthrough", iommu_set_def_domain_type);
157
68a6efe8
ZL
158static int __init iommu_dma_setup(char *str)
159{
160 return kstrtobool(str, &iommu_dma_strict);
161}
162early_param("iommu.strict", iommu_dma_setup);
163
d72e31c9
AW
164static ssize_t iommu_group_attr_show(struct kobject *kobj,
165 struct attribute *__attr, char *buf)
1460432c 166{
d72e31c9
AW
167 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
168 struct iommu_group *group = to_iommu_group(kobj);
169 ssize_t ret = -EIO;
1460432c 170
d72e31c9
AW
171 if (attr->show)
172 ret = attr->show(group, buf);
173 return ret;
174}
175
176static ssize_t iommu_group_attr_store(struct kobject *kobj,
177 struct attribute *__attr,
178 const char *buf, size_t count)
179{
180 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
181 struct iommu_group *group = to_iommu_group(kobj);
182 ssize_t ret = -EIO;
1460432c 183
d72e31c9
AW
184 if (attr->store)
185 ret = attr->store(group, buf, count);
186 return ret;
1460432c 187}
1460432c 188
d72e31c9
AW
189static const struct sysfs_ops iommu_group_sysfs_ops = {
190 .show = iommu_group_attr_show,
191 .store = iommu_group_attr_store,
192};
1460432c 193
d72e31c9
AW
194static int iommu_group_create_file(struct iommu_group *group,
195 struct iommu_group_attribute *attr)
196{
197 return sysfs_create_file(&group->kobj, &attr->attr);
1460432c 198}
1460432c 199
d72e31c9
AW
200static void iommu_group_remove_file(struct iommu_group *group,
201 struct iommu_group_attribute *attr)
202{
203 sysfs_remove_file(&group->kobj, &attr->attr);
204}
205
206static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
207{
208 return sprintf(buf, "%s\n", group->name);
209}
210
6c65fb31
EA
211/**
212 * iommu_insert_resv_region - Insert a new region in the
213 * list of reserved regions.
214 * @new: new region to insert
215 * @regions: list of regions
216 *
217 * The new element is sorted by address with respect to the other
218 * regions of the same type. In case it overlaps with another
219 * region of the same type, regions are merged. In case it
220 * overlaps with another region of different type, regions are
221 * not merged.
222 */
223static int iommu_insert_resv_region(struct iommu_resv_region *new,
224 struct list_head *regions)
225{
226 struct iommu_resv_region *region;
227 phys_addr_t start = new->start;
228 phys_addr_t end = new->start + new->length - 1;
229 struct list_head *pos = regions->next;
230
231 while (pos != regions) {
232 struct iommu_resv_region *entry =
233 list_entry(pos, struct iommu_resv_region, list);
234 phys_addr_t a = entry->start;
235 phys_addr_t b = entry->start + entry->length - 1;
236 int type = entry->type;
237
238 if (end < a) {
239 goto insert;
240 } else if (start > b) {
241 pos = pos->next;
242 } else if ((start >= a) && (end <= b)) {
243 if (new->type == type)
244 goto done;
245 else
246 pos = pos->next;
247 } else {
248 if (new->type == type) {
249 phys_addr_t new_start = min(a, start);
250 phys_addr_t new_end = max(b, end);
251
252 list_del(&entry->list);
253 entry->start = new_start;
254 entry->length = new_end - new_start + 1;
255 iommu_insert_resv_region(entry, regions);
256 } else {
257 pos = pos->next;
258 }
259 }
260 }
261insert:
262 region = iommu_alloc_resv_region(new->start, new->length,
263 new->prot, new->type);
264 if (!region)
265 return -ENOMEM;
266
267 list_add_tail(&region->list, pos);
268done:
269 return 0;
270}
271
272static int
273iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
274 struct list_head *group_resv_regions)
275{
276 struct iommu_resv_region *entry;
a514a6e2 277 int ret = 0;
6c65fb31
EA
278
279 list_for_each_entry(entry, dev_resv_regions, list) {
280 ret = iommu_insert_resv_region(entry, group_resv_regions);
281 if (ret)
282 break;
283 }
284 return ret;
285}
286
287int iommu_get_group_resv_regions(struct iommu_group *group,
288 struct list_head *head)
289{
8d2932dd 290 struct group_device *device;
6c65fb31
EA
291 int ret = 0;
292
293 mutex_lock(&group->mutex);
294 list_for_each_entry(device, &group->devices, list) {
295 struct list_head dev_resv_regions;
296
297 INIT_LIST_HEAD(&dev_resv_regions);
298 iommu_get_resv_regions(device->dev, &dev_resv_regions);
299 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
300 iommu_put_resv_regions(device->dev, &dev_resv_regions);
301 if (ret)
302 break;
303 }
304 mutex_unlock(&group->mutex);
305 return ret;
306}
307EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
308
bc7d12b9
EA
309static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
310 char *buf)
311{
312 struct iommu_resv_region *region, *next;
313 struct list_head group_resv_regions;
314 char *str = buf;
315
316 INIT_LIST_HEAD(&group_resv_regions);
317 iommu_get_group_resv_regions(group, &group_resv_regions);
318
319 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
320 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
321 (long long int)region->start,
322 (long long int)(region->start +
323 region->length - 1),
324 iommu_group_resv_type_string[region->type]);
325 kfree(region);
326 }
327
328 return (str - buf);
329}
330
c52c72d3
OJ
331static ssize_t iommu_group_show_type(struct iommu_group *group,
332 char *buf)
333{
334 char *type = "unknown\n";
335
336 if (group->default_domain) {
337 switch (group->default_domain->type) {
338 case IOMMU_DOMAIN_BLOCKED:
339 type = "blocked\n";
340 break;
341 case IOMMU_DOMAIN_IDENTITY:
342 type = "identity\n";
343 break;
344 case IOMMU_DOMAIN_UNMANAGED:
345 type = "unmanaged\n";
346 break;
347 case IOMMU_DOMAIN_DMA:
348 type = "DMA";
349 break;
350 }
351 }
352 strcpy(buf, type);
353
354 return strlen(type);
355}
356
d72e31c9
AW
357static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
358
bc7d12b9
EA
359static IOMMU_GROUP_ATTR(reserved_regions, 0444,
360 iommu_group_show_resv_regions, NULL);
361
c52c72d3
OJ
362static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
363
d72e31c9
AW
364static void iommu_group_release(struct kobject *kobj)
365{
366 struct iommu_group *group = to_iommu_group(kobj);
367
269aa808
JR
368 pr_debug("Releasing group %d\n", group->id);
369
d72e31c9
AW
370 if (group->iommu_data_release)
371 group->iommu_data_release(group->iommu_data);
372
feccf398 373 ida_simple_remove(&iommu_group_ida, group->id);
d72e31c9 374
53723dc5
JR
375 if (group->default_domain)
376 iommu_domain_free(group->default_domain);
377
d72e31c9
AW
378 kfree(group->name);
379 kfree(group);
380}
381
382static struct kobj_type iommu_group_ktype = {
383 .sysfs_ops = &iommu_group_sysfs_ops,
384 .release = iommu_group_release,
385};
386
387/**
388 * iommu_group_alloc - Allocate a new group
d72e31c9
AW
389 *
390 * This function is called by an iommu driver to allocate a new iommu
391 * group. The iommu group represents the minimum granularity of the iommu.
392 * Upon successful return, the caller holds a reference to the supplied
393 * group in order to hold the group until devices are added. Use
394 * iommu_group_put() to release this extra reference count, allowing the
395 * group to be automatically reclaimed once it has no devices or external
396 * references.
397 */
398struct iommu_group *iommu_group_alloc(void)
1460432c 399{
d72e31c9
AW
400 struct iommu_group *group;
401 int ret;
402
403 group = kzalloc(sizeof(*group), GFP_KERNEL);
404 if (!group)
405 return ERR_PTR(-ENOMEM);
406
407 group->kobj.kset = iommu_group_kset;
408 mutex_init(&group->mutex);
409 INIT_LIST_HEAD(&group->devices);
410 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
411
feccf398
HK
412 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
413 if (ret < 0) {
d72e31c9 414 kfree(group);
feccf398 415 return ERR_PTR(ret);
d72e31c9 416 }
feccf398 417 group->id = ret;
1460432c 418
d72e31c9
AW
419 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
420 NULL, "%d", group->id);
421 if (ret) {
feccf398 422 ida_simple_remove(&iommu_group_ida, group->id);
d72e31c9
AW
423 kfree(group);
424 return ERR_PTR(ret);
425 }
426
427 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
428 if (!group->devices_kobj) {
429 kobject_put(&group->kobj); /* triggers .release & free */
430 return ERR_PTR(-ENOMEM);
431 }
432
433 /*
434 * The devices_kobj holds a reference on the group kobject, so
435 * as long as that exists so will the group. We can therefore
436 * use the devices_kobj for reference counting.
437 */
438 kobject_put(&group->kobj);
439
bc7d12b9
EA
440 ret = iommu_group_create_file(group,
441 &iommu_group_attr_reserved_regions);
442 if (ret)
443 return ERR_PTR(ret);
444
c52c72d3
OJ
445 ret = iommu_group_create_file(group, &iommu_group_attr_type);
446 if (ret)
447 return ERR_PTR(ret);
448
269aa808
JR
449 pr_debug("Allocated group %d\n", group->id);
450
d72e31c9
AW
451 return group;
452}
453EXPORT_SYMBOL_GPL(iommu_group_alloc);
454
aa16bea9
AK
455struct iommu_group *iommu_group_get_by_id(int id)
456{
457 struct kobject *group_kobj;
458 struct iommu_group *group;
459 const char *name;
460
461 if (!iommu_group_kset)
462 return NULL;
463
464 name = kasprintf(GFP_KERNEL, "%d", id);
465 if (!name)
466 return NULL;
467
468 group_kobj = kset_find_obj(iommu_group_kset, name);
469 kfree(name);
470
471 if (!group_kobj)
472 return NULL;
473
474 group = container_of(group_kobj, struct iommu_group, kobj);
475 BUG_ON(group->id != id);
476
477 kobject_get(group->devices_kobj);
478 kobject_put(&group->kobj);
479
480 return group;
481}
482EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
483
d72e31c9
AW
484/**
485 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
486 * @group: the group
487 *
488 * iommu drivers can store data in the group for use when doing iommu
489 * operations. This function provides a way to retrieve it. Caller
490 * should hold a group reference.
491 */
492void *iommu_group_get_iommudata(struct iommu_group *group)
493{
494 return group->iommu_data;
495}
496EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
497
498/**
499 * iommu_group_set_iommudata - set iommu_data for a group
500 * @group: the group
501 * @iommu_data: new data
502 * @release: release function for iommu_data
503 *
504 * iommu drivers can store data in the group for use when doing iommu
505 * operations. This function provides a way to set the data after
506 * the group has been allocated. Caller should hold a group reference.
507 */
508void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
509 void (*release)(void *iommu_data))
1460432c 510{
d72e31c9
AW
511 group->iommu_data = iommu_data;
512 group->iommu_data_release = release;
513}
514EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1460432c 515
d72e31c9
AW
516/**
517 * iommu_group_set_name - set name for a group
518 * @group: the group
519 * @name: name
520 *
521 * Allow iommu driver to set a name for a group. When set it will
522 * appear in a name attribute file under the group in sysfs.
523 */
524int iommu_group_set_name(struct iommu_group *group, const char *name)
525{
526 int ret;
527
528 if (group->name) {
529 iommu_group_remove_file(group, &iommu_group_attr_name);
530 kfree(group->name);
531 group->name = NULL;
532 if (!name)
533 return 0;
534 }
535
536 group->name = kstrdup(name, GFP_KERNEL);
537 if (!group->name)
538 return -ENOMEM;
539
540 ret = iommu_group_create_file(group, &iommu_group_attr_name);
541 if (ret) {
542 kfree(group->name);
543 group->name = NULL;
544 return ret;
545 }
1460432c
AW
546
547 return 0;
548}
d72e31c9 549EXPORT_SYMBOL_GPL(iommu_group_set_name);
1460432c 550
beed2821
JR
551static int iommu_group_create_direct_mappings(struct iommu_group *group,
552 struct device *dev)
553{
554 struct iommu_domain *domain = group->default_domain;
e5b5234a 555 struct iommu_resv_region *entry;
beed2821
JR
556 struct list_head mappings;
557 unsigned long pg_size;
558 int ret = 0;
559
560 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
561 return 0;
562
d16e0faa 563 BUG_ON(!domain->pgsize_bitmap);
beed2821 564
d16e0faa 565 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
beed2821
JR
566 INIT_LIST_HEAD(&mappings);
567
e5b5234a 568 iommu_get_resv_regions(dev, &mappings);
beed2821
JR
569
570 /* We need to consider overlapping regions for different devices */
571 list_for_each_entry(entry, &mappings, list) {
572 dma_addr_t start, end, addr;
573
e5b5234a
EA
574 if (domain->ops->apply_resv_region)
575 domain->ops->apply_resv_region(dev, domain, entry);
33b21a6b 576
beed2821
JR
577 start = ALIGN(entry->start, pg_size);
578 end = ALIGN(entry->start + entry->length, pg_size);
579
544a25d9
EA
580 if (entry->type != IOMMU_RESV_DIRECT)
581 continue;
582
beed2821
JR
583 for (addr = start; addr < end; addr += pg_size) {
584 phys_addr_t phys_addr;
585
586 phys_addr = iommu_iova_to_phys(domain, addr);
587 if (phys_addr)
588 continue;
589
590 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
591 if (ret)
592 goto out;
593 }
594
595 }
596
add02cfd
JR
597 iommu_flush_tlb_all(domain);
598
beed2821 599out:
e5b5234a 600 iommu_put_resv_regions(dev, &mappings);
beed2821
JR
601
602 return ret;
603}
604
d72e31c9
AW
605/**
606 * iommu_group_add_device - add a device to an iommu group
607 * @group: the group into which to add the device (reference should be held)
608 * @dev: the device
609 *
610 * This function is called by an iommu driver to add a device into a
611 * group. Adding a device increments the group reference count.
612 */
613int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1460432c 614{
d72e31c9 615 int ret, i = 0;
c09e22d5 616 struct group_device *device;
d72e31c9
AW
617
618 device = kzalloc(sizeof(*device), GFP_KERNEL);
619 if (!device)
620 return -ENOMEM;
621
622 device->dev = dev;
1460432c 623
d72e31c9 624 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
797a8b4d
RM
625 if (ret)
626 goto err_free_device;
d72e31c9
AW
627
628 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
629rename:
630 if (!device->name) {
797a8b4d
RM
631 ret = -ENOMEM;
632 goto err_remove_link;
d72e31c9 633 }
1460432c 634
d72e31c9
AW
635 ret = sysfs_create_link_nowarn(group->devices_kobj,
636 &dev->kobj, device->name);
637 if (ret) {
d72e31c9
AW
638 if (ret == -EEXIST && i >= 0) {
639 /*
640 * Account for the slim chance of collision
641 * and append an instance to the name.
642 */
797a8b4d 643 kfree(device->name);
d72e31c9
AW
644 device->name = kasprintf(GFP_KERNEL, "%s.%d",
645 kobject_name(&dev->kobj), i++);
646 goto rename;
647 }
797a8b4d 648 goto err_free_name;
d72e31c9
AW
649 }
650
651 kobject_get(group->devices_kobj);
652
653 dev->iommu_group = group;
654
beed2821
JR
655 iommu_group_create_direct_mappings(group, dev);
656
d72e31c9
AW
657 mutex_lock(&group->mutex);
658 list_add_tail(&device->list, &group->devices);
e39cb8a3 659 if (group->domain)
797a8b4d 660 ret = __iommu_attach_device(group->domain, dev);
d72e31c9 661 mutex_unlock(&group->mutex);
797a8b4d
RM
662 if (ret)
663 goto err_put_group;
d72e31c9
AW
664
665 /* Notify any listeners about change to group. */
666 blocking_notifier_call_chain(&group->notifier,
667 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
d1cf7e82
SK
668
669 trace_add_device_to_group(group->id, dev);
269aa808 670
780da9e4 671 dev_info(dev, "Adding to iommu group %d\n", group->id);
269aa808 672
1460432c 673 return 0;
797a8b4d
RM
674
675err_put_group:
676 mutex_lock(&group->mutex);
677 list_del(&device->list);
678 mutex_unlock(&group->mutex);
679 dev->iommu_group = NULL;
680 kobject_put(group->devices_kobj);
681err_free_name:
682 kfree(device->name);
683err_remove_link:
684 sysfs_remove_link(&dev->kobj, "iommu_group");
685err_free_device:
686 kfree(device);
780da9e4 687 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
797a8b4d 688 return ret;
1460432c 689}
d72e31c9 690EXPORT_SYMBOL_GPL(iommu_group_add_device);
1460432c 691
d72e31c9
AW
692/**
693 * iommu_group_remove_device - remove a device from it's current group
694 * @dev: device to be removed
695 *
696 * This function is called by an iommu driver to remove the device from
697 * it's current group. This decrements the iommu group reference count.
698 */
699void iommu_group_remove_device(struct device *dev)
700{
701 struct iommu_group *group = dev->iommu_group;
c09e22d5 702 struct group_device *tmp_device, *device = NULL;
d72e31c9 703
780da9e4 704 dev_info(dev, "Removing from iommu group %d\n", group->id);
269aa808 705
d72e31c9
AW
706 /* Pre-notify listeners that a device is being removed. */
707 blocking_notifier_call_chain(&group->notifier,
708 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
709
710 mutex_lock(&group->mutex);
711 list_for_each_entry(tmp_device, &group->devices, list) {
712 if (tmp_device->dev == dev) {
713 device = tmp_device;
714 list_del(&device->list);
715 break;
716 }
717 }
718 mutex_unlock(&group->mutex);
719
720 if (!device)
721 return;
722
723 sysfs_remove_link(group->devices_kobj, device->name);
724 sysfs_remove_link(&dev->kobj, "iommu_group");
725
2e757086
SK
726 trace_remove_device_from_group(group->id, dev);
727
d72e31c9
AW
728 kfree(device->name);
729 kfree(device);
730 dev->iommu_group = NULL;
731 kobject_put(group->devices_kobj);
732}
733EXPORT_SYMBOL_GPL(iommu_group_remove_device);
734
426a2738
JR
735static int iommu_group_device_count(struct iommu_group *group)
736{
c09e22d5 737 struct group_device *entry;
426a2738
JR
738 int ret = 0;
739
740 list_for_each_entry(entry, &group->devices, list)
741 ret++;
742
743 return ret;
744}
745
d72e31c9
AW
746/**
747 * iommu_group_for_each_dev - iterate over each device in the group
748 * @group: the group
749 * @data: caller opaque data to be passed to callback function
750 * @fn: caller supplied callback function
751 *
752 * This function is called by group users to iterate over group devices.
753 * Callers should hold a reference count to the group during callback.
754 * The group->mutex is held across callbacks, which will block calls to
755 * iommu_group_add/remove_device.
756 */
e39cb8a3
JR
757static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
758 int (*fn)(struct device *, void *))
d72e31c9 759{
c09e22d5 760 struct group_device *device;
d72e31c9
AW
761 int ret = 0;
762
d72e31c9
AW
763 list_for_each_entry(device, &group->devices, list) {
764 ret = fn(device->dev, data);
765 if (ret)
766 break;
767 }
e39cb8a3
JR
768 return ret;
769}
770
771
772int iommu_group_for_each_dev(struct iommu_group *group, void *data,
773 int (*fn)(struct device *, void *))
774{
775 int ret;
776
777 mutex_lock(&group->mutex);
778 ret = __iommu_group_for_each_dev(group, data, fn);
d72e31c9 779 mutex_unlock(&group->mutex);
e39cb8a3 780
d72e31c9
AW
781 return ret;
782}
783EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
784
785/**
786 * iommu_group_get - Return the group for a device and increment reference
787 * @dev: get the group that this device belongs to
788 *
789 * This function is called by iommu drivers and users to get the group
790 * for the specified device. If found, the group is returned and the group
791 * reference in incremented, else NULL.
792 */
793struct iommu_group *iommu_group_get(struct device *dev)
794{
795 struct iommu_group *group = dev->iommu_group;
796
797 if (group)
798 kobject_get(group->devices_kobj);
799
800 return group;
801}
802EXPORT_SYMBOL_GPL(iommu_group_get);
803
13f59a78
RM
804/**
805 * iommu_group_ref_get - Increment reference on a group
806 * @group: the group to use, must not be NULL
807 *
808 * This function is called by iommu drivers to take additional references on an
809 * existing group. Returns the given group for convenience.
810 */
811struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
812{
813 kobject_get(group->devices_kobj);
814 return group;
815}
816
d72e31c9
AW
817/**
818 * iommu_group_put - Decrement group reference
819 * @group: the group to use
820 *
821 * This function is called by iommu drivers and users to release the
822 * iommu group. Once the reference count is zero, the group is released.
823 */
824void iommu_group_put(struct iommu_group *group)
825{
826 if (group)
827 kobject_put(group->devices_kobj);
828}
829EXPORT_SYMBOL_GPL(iommu_group_put);
830
831/**
832 * iommu_group_register_notifier - Register a notifier for group changes
833 * @group: the group to watch
834 * @nb: notifier block to signal
835 *
836 * This function allows iommu group users to track changes in a group.
837 * See include/linux/iommu.h for actions sent via this notifier. Caller
838 * should hold a reference to the group throughout notifier registration.
839 */
840int iommu_group_register_notifier(struct iommu_group *group,
841 struct notifier_block *nb)
842{
843 return blocking_notifier_chain_register(&group->notifier, nb);
844}
845EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
846
847/**
848 * iommu_group_unregister_notifier - Unregister a notifier
849 * @group: the group to watch
850 * @nb: notifier block to signal
851 *
852 * Unregister a previously registered group notifier block.
853 */
854int iommu_group_unregister_notifier(struct iommu_group *group,
855 struct notifier_block *nb)
856{
857 return blocking_notifier_chain_unregister(&group->notifier, nb);
858}
859EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
860
861/**
862 * iommu_group_id - Return ID for a group
863 * @group: the group to ID
864 *
865 * Return the unique ID for the group matching the sysfs group number.
866 */
867int iommu_group_id(struct iommu_group *group)
868{
869 return group->id;
870}
871EXPORT_SYMBOL_GPL(iommu_group_id);
1460432c 872
f096c061
AW
873static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
874 unsigned long *devfns);
875
104a1c13
AW
876/*
877 * To consider a PCI device isolated, we require ACS to support Source
878 * Validation, Request Redirection, Completer Redirection, and Upstream
879 * Forwarding. This effectively means that devices cannot spoof their
880 * requester ID, requests and completions cannot be redirected, and all
881 * transactions are forwarded upstream, even as it passes through a
882 * bridge where the target device is downstream.
883 */
884#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
885
f096c061
AW
886/*
887 * For multifunction devices which are not isolated from each other, find
888 * all the other non-isolated functions and look for existing groups. For
889 * each function, we also need to look for aliases to or from other devices
890 * that may already have a group.
891 */
892static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
893 unsigned long *devfns)
894{
895 struct pci_dev *tmp = NULL;
896 struct iommu_group *group;
897
898 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
899 return NULL;
900
901 for_each_pci_dev(tmp) {
902 if (tmp == pdev || tmp->bus != pdev->bus ||
903 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
904 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
905 continue;
906
907 group = get_pci_alias_group(tmp, devfns);
908 if (group) {
909 pci_dev_put(tmp);
910 return group;
911 }
912 }
913
914 return NULL;
915}
916
917/*
338c3149
JL
918 * Look for aliases to or from the given device for existing groups. DMA
919 * aliases are only supported on the same bus, therefore the search
f096c061
AW
920 * space is quite small (especially since we're really only looking at pcie
921 * device, and therefore only expect multiple slots on the root complex or
922 * downstream switch ports). It's conceivable though that a pair of
923 * multifunction devices could have aliases between them that would cause a
924 * loop. To prevent this, we use a bitmap to track where we've been.
925 */
926static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
927 unsigned long *devfns)
928{
929 struct pci_dev *tmp = NULL;
930 struct iommu_group *group;
931
932 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
933 return NULL;
934
935 group = iommu_group_get(&pdev->dev);
936 if (group)
937 return group;
938
939 for_each_pci_dev(tmp) {
940 if (tmp == pdev || tmp->bus != pdev->bus)
941 continue;
942
943 /* We alias them or they alias us */
338c3149 944 if (pci_devs_are_dma_aliases(pdev, tmp)) {
f096c061
AW
945 group = get_pci_alias_group(tmp, devfns);
946 if (group) {
947 pci_dev_put(tmp);
948 return group;
949 }
950
951 group = get_pci_function_alias_group(tmp, devfns);
952 if (group) {
953 pci_dev_put(tmp);
954 return group;
955 }
956 }
957 }
958
959 return NULL;
960}
961
104a1c13
AW
962struct group_for_pci_data {
963 struct pci_dev *pdev;
964 struct iommu_group *group;
965};
966
967/*
968 * DMA alias iterator callback, return the last seen device. Stop and return
969 * the IOMMU group if we find one along the way.
970 */
971static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
972{
973 struct group_for_pci_data *data = opaque;
974
975 data->pdev = pdev;
976 data->group = iommu_group_get(&pdev->dev);
977
978 return data->group != NULL;
979}
980
6eab556a
JR
981/*
982 * Generic device_group call-back function. It just allocates one
983 * iommu-group per device.
984 */
985struct iommu_group *generic_device_group(struct device *dev)
986{
7f7a2304 987 return iommu_group_alloc();
6eab556a
JR
988}
989
104a1c13
AW
990/*
991 * Use standard PCI bus topology, isolation features, and DMA alias quirks
992 * to find or create an IOMMU group for a device.
993 */
5e62292b 994struct iommu_group *pci_device_group(struct device *dev)
104a1c13 995{
5e62292b 996 struct pci_dev *pdev = to_pci_dev(dev);
104a1c13
AW
997 struct group_for_pci_data data;
998 struct pci_bus *bus;
999 struct iommu_group *group = NULL;
f096c061 1000 u64 devfns[4] = { 0 };
104a1c13 1001
5e62292b
JR
1002 if (WARN_ON(!dev_is_pci(dev)))
1003 return ERR_PTR(-EINVAL);
1004
104a1c13
AW
1005 /*
1006 * Find the upstream DMA alias for the device. A device must not
1007 * be aliased due to topology in order to have its own IOMMU group.
1008 * If we find an alias along the way that already belongs to a
1009 * group, use it.
1010 */
1011 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1012 return data.group;
1013
1014 pdev = data.pdev;
1015
1016 /*
1017 * Continue upstream from the point of minimum IOMMU granularity
1018 * due to aliases to the point where devices are protected from
1019 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1020 * group, use it.
1021 */
1022 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1023 if (!bus->self)
1024 continue;
1025
1026 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1027 break;
1028
1029 pdev = bus->self;
1030
1031 group = iommu_group_get(&pdev->dev);
1032 if (group)
1033 return group;
1034 }
1035
1036 /*
f096c061
AW
1037 * Look for existing groups on device aliases. If we alias another
1038 * device or another device aliases us, use the same group.
104a1c13 1039 */
f096c061
AW
1040 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1041 if (group)
1042 return group;
104a1c13
AW
1043
1044 /*
f096c061
AW
1045 * Look for existing groups on non-isolated functions on the same
1046 * slot and aliases of those funcions, if any. No need to clear
1047 * the search bitmap, the tested devfns are still valid.
104a1c13 1048 */
f096c061
AW
1049 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1050 if (group)
1051 return group;
104a1c13
AW
1052
1053 /* No shared group found, allocate new */
7f7a2304 1054 return iommu_group_alloc();
104a1c13
AW
1055}
1056
eab03e2a
NG
1057/* Get the IOMMU group for device on fsl-mc bus */
1058struct iommu_group *fsl_mc_device_group(struct device *dev)
1059{
1060 struct device *cont_dev = fsl_mc_cont_dev(dev);
1061 struct iommu_group *group;
1062
1063 group = iommu_group_get(cont_dev);
1064 if (!group)
1065 group = iommu_group_alloc();
1066 return group;
1067}
1068
104a1c13
AW
1069/**
1070 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1071 * @dev: target device
1072 *
1073 * This function is intended to be called by IOMMU drivers and extended to
1074 * support common, bus-defined algorithms when determining or creating the
1075 * IOMMU group for a device. On success, the caller will hold a reference
1076 * to the returned IOMMU group, which will already include the provided
1077 * device. The reference should be released with iommu_group_put().
1078 */
1079struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1080{
46c6b2bc 1081 const struct iommu_ops *ops = dev->bus->iommu_ops;
c4a783b8 1082 struct iommu_group *group;
104a1c13
AW
1083 int ret;
1084
1085 group = iommu_group_get(dev);
1086 if (group)
1087 return group;
1088
05f80300
RM
1089 if (!ops)
1090 return ERR_PTR(-EINVAL);
104a1c13 1091
05f80300 1092 group = ops->device_group(dev);
72dcac63
JR
1093 if (WARN_ON_ONCE(group == NULL))
1094 return ERR_PTR(-EINVAL);
1095
104a1c13
AW
1096 if (IS_ERR(group))
1097 return group;
1098
1228236d
JR
1099 /*
1100 * Try to allocate a default domain - needs support from the
1101 * IOMMU driver.
1102 */
1103 if (!group->default_domain) {
fccb4e3b
WD
1104 struct iommu_domain *dom;
1105
1106 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1107 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
fccb4e3b 1108 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
8bc32a28
JR
1109 if (dom) {
1110 dev_warn(dev,
1111 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1112 iommu_def_domain_type);
1113 }
fccb4e3b
WD
1114 }
1115
1116 group->default_domain = dom;
eebb8034 1117 if (!group->domain)
fccb4e3b 1118 group->domain = dom;
68a6efe8
ZL
1119
1120 if (dom && !iommu_dma_strict) {
1121 int attr = 1;
1122 iommu_domain_set_attr(dom,
1123 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1124 &attr);
1125 }
1228236d
JR
1126 }
1127
104a1c13
AW
1128 ret = iommu_group_add_device(group, dev);
1129 if (ret) {
1130 iommu_group_put(group);
1131 return ERR_PTR(ret);
1132 }
1133
1134 return group;
1135}
1136
6827ca83
JR
1137struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1138{
1139 return group->default_domain;
1140}
1141
d72e31c9 1142static int add_iommu_group(struct device *dev, void *data)
1460432c 1143{
cc5aed44 1144 int ret = iommu_probe_device(dev);
38667f18
JR
1145
1146 /*
1147 * We ignore -ENODEV errors for now, as they just mean that the
1148 * device is not translated by an IOMMU. We still care about
1149 * other errors and fail to initialize when they happen.
1150 */
1151 if (ret == -ENODEV)
1152 ret = 0;
1153
1154 return ret;
1460432c
AW
1155}
1156
8da30142
JR
1157static int remove_iommu_group(struct device *dev, void *data)
1158{
cc5aed44 1159 iommu_release_device(dev);
1460432c
AW
1160
1161 return 0;
1162}
1163
d72e31c9
AW
1164static int iommu_bus_notifier(struct notifier_block *nb,
1165 unsigned long action, void *data)
1460432c 1166{
cc5aed44 1167 unsigned long group_action = 0;
1460432c 1168 struct device *dev = data;
d72e31c9 1169 struct iommu_group *group;
d72e31c9
AW
1170
1171 /*
1172 * ADD/DEL call into iommu driver ops if provided, which may
1173 * result in ADD/DEL notifiers to group->notifier
1174 */
1175 if (action == BUS_NOTIFY_ADD_DEVICE) {
cc5aed44 1176 int ret;
3ba8775f 1177
cc5aed44
JR
1178 ret = iommu_probe_device(dev);
1179 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
843cb6dc 1180 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
cc5aed44
JR
1181 iommu_release_device(dev);
1182 return NOTIFY_OK;
d72e31c9 1183 }
1460432c 1184
d72e31c9
AW
1185 /*
1186 * Remaining BUS_NOTIFYs get filtered and republished to the
1187 * group, if anyone is listening
1188 */
1189 group = iommu_group_get(dev);
1190 if (!group)
1191 return 0;
1460432c 1192
d72e31c9
AW
1193 switch (action) {
1194 case BUS_NOTIFY_BIND_DRIVER:
1195 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1196 break;
1197 case BUS_NOTIFY_BOUND_DRIVER:
1198 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1199 break;
1200 case BUS_NOTIFY_UNBIND_DRIVER:
1201 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1202 break;
1203 case BUS_NOTIFY_UNBOUND_DRIVER:
1204 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1205 break;
1206 }
1460432c 1207
d72e31c9
AW
1208 if (group_action)
1209 blocking_notifier_call_chain(&group->notifier,
1210 group_action, dev);
1460432c 1211
d72e31c9 1212 iommu_group_put(group);
1460432c
AW
1213 return 0;
1214}
1215
fb3e3065 1216static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
ff21776d 1217{
fb3e3065
MS
1218 int err;
1219 struct notifier_block *nb;
b22f6434
TR
1220 struct iommu_callback_data cb = {
1221 .ops = ops,
1222 };
1223
fb3e3065
MS
1224 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1225 if (!nb)
1226 return -ENOMEM;
1227
1228 nb->notifier_call = iommu_bus_notifier;
1229
1230 err = bus_register_notifier(bus, nb);
8da30142
JR
1231 if (err)
1232 goto out_free;
d7da6bdc
HS
1233
1234 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
8da30142
JR
1235 if (err)
1236 goto out_err;
1237
d7da6bdc
HS
1238
1239 return 0;
8da30142
JR
1240
1241out_err:
1242 /* Clean up */
1243 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1244 bus_unregister_notifier(bus, nb);
1245
1246out_free:
1247 kfree(nb);
1248
1249 return err;
ff21776d 1250}
fc2100eb 1251
ff21776d
JR
1252/**
1253 * bus_set_iommu - set iommu-callbacks for the bus
1254 * @bus: bus.
1255 * @ops: the callbacks provided by the iommu-driver
1256 *
1257 * This function is called by an iommu driver to set the iommu methods
1258 * used for a particular bus. Drivers for devices on that bus can use
1259 * the iommu-api after these ops are registered.
1260 * This special function is needed because IOMMUs are usually devices on
1261 * the bus itself, so the iommu drivers are not initialized when the bus
1262 * is set up. With this function the iommu-driver can set the iommu-ops
1263 * afterwards.
1264 */
b22f6434 1265int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
fc2100eb 1266{
d7da6bdc
HS
1267 int err;
1268
ff21776d
JR
1269 if (bus->iommu_ops != NULL)
1270 return -EBUSY;
fc2100eb 1271
ff21776d
JR
1272 bus->iommu_ops = ops;
1273
1274 /* Do IOMMU specific setup for this bus-type */
d7da6bdc
HS
1275 err = iommu_bus_init(bus, ops);
1276 if (err)
1277 bus->iommu_ops = NULL;
1278
1279 return err;
fc2100eb 1280}
ff21776d 1281EXPORT_SYMBOL_GPL(bus_set_iommu);
fc2100eb 1282
a1b60c1c 1283bool iommu_present(struct bus_type *bus)
fc2100eb 1284{
94441c3b 1285 return bus->iommu_ops != NULL;
fc2100eb 1286}
a1b60c1c 1287EXPORT_SYMBOL_GPL(iommu_present);
fc2100eb 1288
3c0e0ca0
JR
1289bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1290{
1291 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1292 return false;
1293
1294 return bus->iommu_ops->capable(cap);
1295}
1296EXPORT_SYMBOL_GPL(iommu_capable);
1297
4f3f8d9d
OBC
1298/**
1299 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1300 * @domain: iommu domain
1301 * @handler: fault handler
77ca2332 1302 * @token: user data, will be passed back to the fault handler
0ed6d2d2
OBC
1303 *
1304 * This function should be used by IOMMU users which want to be notified
1305 * whenever an IOMMU fault happens.
1306 *
1307 * The fault handler itself should return 0 on success, and an appropriate
1308 * error code otherwise.
4f3f8d9d
OBC
1309 */
1310void iommu_set_fault_handler(struct iommu_domain *domain,
77ca2332
OBC
1311 iommu_fault_handler_t handler,
1312 void *token)
4f3f8d9d
OBC
1313{
1314 BUG_ON(!domain);
1315
1316 domain->handler = handler;
77ca2332 1317 domain->handler_token = token;
4f3f8d9d 1318}
30bd918c 1319EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
4f3f8d9d 1320
53723dc5
JR
1321static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1322 unsigned type)
fc2100eb
JR
1323{
1324 struct iommu_domain *domain;
fc2100eb 1325
94441c3b 1326 if (bus == NULL || bus->iommu_ops == NULL)
905d66c1
JR
1327 return NULL;
1328
53723dc5 1329 domain = bus->iommu_ops->domain_alloc(type);
fc2100eb
JR
1330 if (!domain)
1331 return NULL;
1332
8539c7c1 1333 domain->ops = bus->iommu_ops;
53723dc5 1334 domain->type = type;
d16e0faa
RM
1335 /* Assume all sizes by default; the driver may override this later */
1336 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
905d66c1 1337
fc2100eb 1338 return domain;
fc2100eb 1339}
fc2100eb 1340
53723dc5
JR
1341struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1342{
1343 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
fc2100eb
JR
1344}
1345EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1346
1347void iommu_domain_free(struct iommu_domain *domain)
1348{
89be34a1 1349 domain->ops->domain_free(domain);
fc2100eb
JR
1350}
1351EXPORT_SYMBOL_GPL(iommu_domain_free);
1352
426a2738
JR
1353static int __iommu_attach_device(struct iommu_domain *domain,
1354 struct device *dev)
fc2100eb 1355{
b54db778 1356 int ret;
e01d1913
BH
1357 if ((domain->ops->is_attach_deferred != NULL) &&
1358 domain->ops->is_attach_deferred(domain, dev))
1359 return 0;
1360
e5aa7f00
JR
1361 if (unlikely(domain->ops->attach_dev == NULL))
1362 return -ENODEV;
1363
b54db778
SK
1364 ret = domain->ops->attach_dev(domain, dev);
1365 if (!ret)
1366 trace_attach_device_to_domain(dev);
1367 return ret;
fc2100eb 1368}
426a2738
JR
1369
1370int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1371{
1372 struct iommu_group *group;
1373 int ret;
1374
1375 group = iommu_group_get(dev);
9ae9df03
JC
1376 if (!group)
1377 return -ENODEV;
1378
426a2738 1379 /*
05f80300 1380 * Lock the group to make sure the device-count doesn't
426a2738
JR
1381 * change while we are attaching
1382 */
1383 mutex_lock(&group->mutex);
1384 ret = -EINVAL;
1385 if (iommu_group_device_count(group) != 1)
1386 goto out_unlock;
1387
e39cb8a3 1388 ret = __iommu_attach_group(domain, group);
426a2738
JR
1389
1390out_unlock:
1391 mutex_unlock(&group->mutex);
1392 iommu_group_put(group);
1393
1394 return ret;
1395}
fc2100eb
JR
1396EXPORT_SYMBOL_GPL(iommu_attach_device);
1397
426a2738
JR
1398static void __iommu_detach_device(struct iommu_domain *domain,
1399 struct device *dev)
fc2100eb 1400{
e01d1913
BH
1401 if ((domain->ops->is_attach_deferred != NULL) &&
1402 domain->ops->is_attach_deferred(domain, dev))
1403 return;
1404
e5aa7f00
JR
1405 if (unlikely(domain->ops->detach_dev == NULL))
1406 return;
1407
1408 domain->ops->detach_dev(domain, dev);
69980630 1409 trace_detach_device_from_domain(dev);
fc2100eb 1410}
426a2738
JR
1411
1412void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1413{
1414 struct iommu_group *group;
1415
1416 group = iommu_group_get(dev);
9ae9df03
JC
1417 if (!group)
1418 return;
426a2738
JR
1419
1420 mutex_lock(&group->mutex);
1421 if (iommu_group_device_count(group) != 1) {
1422 WARN_ON(1);
1423 goto out_unlock;
1424 }
1425
e39cb8a3 1426 __iommu_detach_group(domain, group);
426a2738
JR
1427
1428out_unlock:
1429 mutex_unlock(&group->mutex);
1430 iommu_group_put(group);
1431}
fc2100eb
JR
1432EXPORT_SYMBOL_GPL(iommu_detach_device);
1433
2c1296d9
JR
1434struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1435{
1436 struct iommu_domain *domain;
1437 struct iommu_group *group;
1438
1439 group = iommu_group_get(dev);
1464d0b1 1440 if (!group)
2c1296d9
JR
1441 return NULL;
1442
1443 domain = group->domain;
1444
1445 iommu_group_put(group);
1446
1447 return domain;
1448}
1449EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
fc2100eb 1450
d72e31c9 1451/*
6af588fe
RM
1452 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1453 * guarantees that the group and its default domain are valid and correct.
1454 */
1455struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1456{
1457 return dev->iommu_group->default_domain;
1458}
1459
d72e31c9 1460/*
35449adc 1461 * IOMMU groups are really the natural working unit of the IOMMU, but
d72e31c9
AW
1462 * the IOMMU API works on domains and devices. Bridge that gap by
1463 * iterating over the devices in a group. Ideally we'd have a single
1464 * device which represents the requestor ID of the group, but we also
1465 * allow IOMMU drivers to create policy defined minimum sets, where
1466 * the physical hardware may be able to distiguish members, but we
1467 * wish to group them at a higher level (ex. untrusted multi-function
1468 * PCI devices). Thus we attach each device.
1469 */
1470static int iommu_group_do_attach_device(struct device *dev, void *data)
1471{
1472 struct iommu_domain *domain = data;
1473
426a2738 1474 return __iommu_attach_device(domain, dev);
d72e31c9
AW
1475}
1476
e39cb8a3
JR
1477static int __iommu_attach_group(struct iommu_domain *domain,
1478 struct iommu_group *group)
1479{
1480 int ret;
1481
1482 if (group->default_domain && group->domain != group->default_domain)
1483 return -EBUSY;
1484
1485 ret = __iommu_group_for_each_dev(group, domain,
1486 iommu_group_do_attach_device);
1487 if (ret == 0)
1488 group->domain = domain;
1489
1490 return ret;
d72e31c9
AW
1491}
1492
1493int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1494{
e39cb8a3
JR
1495 int ret;
1496
1497 mutex_lock(&group->mutex);
1498 ret = __iommu_attach_group(domain, group);
1499 mutex_unlock(&group->mutex);
1500
1501 return ret;
d72e31c9
AW
1502}
1503EXPORT_SYMBOL_GPL(iommu_attach_group);
1504
1505static int iommu_group_do_detach_device(struct device *dev, void *data)
1506{
1507 struct iommu_domain *domain = data;
1508
426a2738 1509 __iommu_detach_device(domain, dev);
d72e31c9
AW
1510
1511 return 0;
1512}
1513
e39cb8a3
JR
1514static void __iommu_detach_group(struct iommu_domain *domain,
1515 struct iommu_group *group)
1516{
1517 int ret;
1518
1519 if (!group->default_domain) {
1520 __iommu_group_for_each_dev(group, domain,
1521 iommu_group_do_detach_device);
1522 group->domain = NULL;
1523 return;
1524 }
1525
1526 if (group->domain == group->default_domain)
1527 return;
1528
1529 /* Detach by re-attaching to the default domain */
1530 ret = __iommu_group_for_each_dev(group, group->default_domain,
1531 iommu_group_do_attach_device);
1532 if (ret != 0)
1533 WARN_ON(1);
1534 else
1535 group->domain = group->default_domain;
1536}
1537
d72e31c9
AW
1538void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1539{
e39cb8a3
JR
1540 mutex_lock(&group->mutex);
1541 __iommu_detach_group(domain, group);
1542 mutex_unlock(&group->mutex);
d72e31c9
AW
1543}
1544EXPORT_SYMBOL_GPL(iommu_detach_group);
1545
bb5547ac 1546phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
fc2100eb 1547{
e5aa7f00
JR
1548 if (unlikely(domain->ops->iova_to_phys == NULL))
1549 return 0;
1550
1551 return domain->ops->iova_to_phys(domain, iova);
fc2100eb
JR
1552}
1553EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
dbb9fd86 1554
bd13969b
AW
1555static size_t iommu_pgsize(struct iommu_domain *domain,
1556 unsigned long addr_merge, size_t size)
1557{
1558 unsigned int pgsize_idx;
1559 size_t pgsize;
1560
1561 /* Max page size that still fits into 'size' */
1562 pgsize_idx = __fls(size);
1563
1564 /* need to consider alignment requirements ? */
1565 if (likely(addr_merge)) {
1566 /* Max page size allowed by address */
1567 unsigned int align_pgsize_idx = __ffs(addr_merge);
1568 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1569 }
1570
1571 /* build a mask of acceptable page sizes */
1572 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1573
1574 /* throw away page sizes not supported by the hardware */
d16e0faa 1575 pgsize &= domain->pgsize_bitmap;
bd13969b
AW
1576
1577 /* make sure we're still sane */
1578 BUG_ON(!pgsize);
1579
1580 /* pick the biggest page */
1581 pgsize_idx = __fls(pgsize);
1582 pgsize = 1UL << pgsize_idx;
1583
1584 return pgsize;
1585}
1586
cefc53c7 1587int iommu_map(struct iommu_domain *domain, unsigned long iova,
7d3002cc 1588 phys_addr_t paddr, size_t size, int prot)
cefc53c7 1589{
1d7ae53b 1590 const struct iommu_ops *ops = domain->ops;
7d3002cc
OBC
1591 unsigned long orig_iova = iova;
1592 unsigned int min_pagesz;
1593 size_t orig_size = size;
06bfcaa9 1594 phys_addr_t orig_paddr = paddr;
7d3002cc 1595 int ret = 0;
cefc53c7 1596
1d7ae53b 1597 if (unlikely(ops->map == NULL ||
d16e0faa 1598 domain->pgsize_bitmap == 0UL))
e5aa7f00 1599 return -ENODEV;
cefc53c7 1600
a10315e5
JR
1601 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1602 return -EINVAL;
1603
7d3002cc 1604 /* find out the minimum page size supported */
d16e0faa 1605 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
7d3002cc
OBC
1606
1607 /*
1608 * both the virtual address and the physical one, as well as
1609 * the size of the mapping, must be aligned (at least) to the
1610 * size of the smallest page supported by the hardware
1611 */
1612 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
abedb049 1613 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
6197ca82 1614 iova, &paddr, size, min_pagesz);
7d3002cc
OBC
1615 return -EINVAL;
1616 }
1617
abedb049 1618 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
7d3002cc
OBC
1619
1620 while (size) {
bd13969b 1621 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
7d3002cc 1622
abedb049 1623 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
6197ca82 1624 iova, &paddr, pgsize);
7d3002cc 1625
1d7ae53b 1626 ret = ops->map(domain, iova, paddr, pgsize, prot);
7d3002cc
OBC
1627 if (ret)
1628 break;
1629
1630 iova += pgsize;
1631 paddr += pgsize;
1632 size -= pgsize;
1633 }
1634
1d7ae53b
DO
1635 if (ops->iotlb_sync_map)
1636 ops->iotlb_sync_map(domain);
1637
7d3002cc
OBC
1638 /* unroll mapping in case something went wrong */
1639 if (ret)
1640 iommu_unmap(domain, orig_iova, orig_size - size);
e0be7c86 1641 else
06bfcaa9 1642 trace_map(orig_iova, orig_paddr, orig_size);
7d3002cc
OBC
1643
1644 return ret;
cefc53c7
JR
1645}
1646EXPORT_SYMBOL_GPL(iommu_map);
1647
add02cfd
JR
1648static size_t __iommu_unmap(struct iommu_domain *domain,
1649 unsigned long iova, size_t size,
1650 bool sync)
cefc53c7 1651{
add02cfd 1652 const struct iommu_ops *ops = domain->ops;
7d3002cc 1653 size_t unmapped_page, unmapped = 0;
6fd492fd 1654 unsigned long orig_iova = iova;
add02cfd 1655 unsigned int min_pagesz;
cefc53c7 1656
add02cfd 1657 if (unlikely(ops->unmap == NULL ||
d16e0faa 1658 domain->pgsize_bitmap == 0UL))
c5611a87 1659 return 0;
e5aa7f00 1660
a10315e5 1661 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
c5611a87 1662 return 0;
a10315e5 1663
7d3002cc 1664 /* find out the minimum page size supported */
d16e0faa 1665 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
7d3002cc
OBC
1666
1667 /*
1668 * The virtual address, as well as the size of the mapping, must be
1669 * aligned (at least) to the size of the smallest page supported
1670 * by the hardware
1671 */
1672 if (!IS_ALIGNED(iova | size, min_pagesz)) {
6197ca82
JP
1673 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1674 iova, size, min_pagesz);
c5611a87 1675 return 0;
7d3002cc
OBC
1676 }
1677
6197ca82 1678 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
7d3002cc
OBC
1679
1680 /*
1681 * Keep iterating until we either unmap 'size' bytes (or more)
1682 * or we hit an area that isn't mapped.
1683 */
1684 while (unmapped < size) {
bd13969b 1685 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
7d3002cc 1686
add02cfd 1687 unmapped_page = ops->unmap(domain, iova, pgsize);
7d3002cc
OBC
1688 if (!unmapped_page)
1689 break;
1690
add02cfd
JR
1691 if (sync && ops->iotlb_range_add)
1692 ops->iotlb_range_add(domain, iova, pgsize);
1693
6197ca82
JP
1694 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1695 iova, unmapped_page);
7d3002cc
OBC
1696
1697 iova += unmapped_page;
1698 unmapped += unmapped_page;
1699 }
1700
add02cfd
JR
1701 if (sync && ops->iotlb_sync)
1702 ops->iotlb_sync(domain);
1703
db8614d3 1704 trace_unmap(orig_iova, size, unmapped);
7d3002cc 1705 return unmapped;
cefc53c7 1706}
add02cfd
JR
1707
1708size_t iommu_unmap(struct iommu_domain *domain,
1709 unsigned long iova, size_t size)
1710{
1711 return __iommu_unmap(domain, iova, size, true);
1712}
cefc53c7 1713EXPORT_SYMBOL_GPL(iommu_unmap);
1460432c 1714
add02cfd
JR
1715size_t iommu_unmap_fast(struct iommu_domain *domain,
1716 unsigned long iova, size_t size)
1717{
1718 return __iommu_unmap(domain, iova, size, false);
1719}
1720EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1721
d88e61fa
CH
1722size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1723 struct scatterlist *sg, unsigned int nents, int prot)
315786eb 1724{
5d95f40e
RM
1725 size_t len = 0, mapped = 0;
1726 phys_addr_t start;
1727 unsigned int i = 0;
38ec010d 1728 int ret;
315786eb 1729
5d95f40e
RM
1730 while (i <= nents) {
1731 phys_addr_t s_phys = sg_phys(sg);
18f23409 1732
5d95f40e
RM
1733 if (len && s_phys != start + len) {
1734 ret = iommu_map(domain, iova + mapped, start, len, prot);
1735 if (ret)
1736 goto out_err;
18f23409 1737
5d95f40e
RM
1738 mapped += len;
1739 len = 0;
1740 }
38ec010d 1741
5d95f40e
RM
1742 if (len) {
1743 len += sg->length;
1744 } else {
1745 len = sg->length;
1746 start = s_phys;
1747 }
38ec010d 1748
5d95f40e
RM
1749 if (++i < nents)
1750 sg = sg_next(sg);
315786eb
OH
1751 }
1752
1753 return mapped;
38ec010d
JR
1754
1755out_err:
1756 /* undo mappings already done */
1757 iommu_unmap(domain, iova, mapped);
1758
1759 return 0;
1760
315786eb 1761}
d88e61fa 1762EXPORT_SYMBOL_GPL(iommu_map_sg);
d7787d57
JR
1763
1764int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
80f97f0f 1765 phys_addr_t paddr, u64 size, int prot)
d7787d57
JR
1766{
1767 if (unlikely(domain->ops->domain_window_enable == NULL))
1768 return -ENODEV;
1769
80f97f0f
VS
1770 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1771 prot);
d7787d57
JR
1772}
1773EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1774
1775void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1776{
1777 if (unlikely(domain->ops->domain_window_disable == NULL))
1778 return;
1779
1780 return domain->ops->domain_window_disable(domain, wnd_nr);
1781}
1782EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1783
207c6e36
JR
1784/**
1785 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1786 * @domain: the iommu domain where the fault has happened
1787 * @dev: the device where the fault has happened
1788 * @iova: the faulting address
1789 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1790 *
1791 * This function should be called by the low-level IOMMU implementations
1792 * whenever IOMMU faults happen, to allow high-level users, that are
1793 * interested in such events, to know about them.
1794 *
1795 * This event may be useful for several possible use cases:
1796 * - mere logging of the event
1797 * - dynamic TLB/PTE loading
1798 * - if restarting of the faulting device is required
1799 *
1800 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1801 * PTE/TLB loading will one day be supported, implementations will be able
1802 * to tell whether it succeeded or not according to this return value).
1803 *
1804 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1805 * (though fault handlers can also return -ENOSYS, in case they want to
1806 * elicit the default behavior of the IOMMU drivers).
1807 */
1808int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1809 unsigned long iova, int flags)
1810{
1811 int ret = -ENOSYS;
1812
1813 /*
1814 * if upper layers showed interest and installed a fault handler,
1815 * invoke it.
1816 */
1817 if (domain->handler)
1818 ret = domain->handler(domain, dev, iova, flags,
1819 domain->handler_token);
1820
1821 trace_io_page_fault(dev, iova, flags);
1822 return ret;
1823}
1824EXPORT_SYMBOL_GPL(report_iommu_fault);
1825
d72e31c9 1826static int __init iommu_init(void)
1460432c 1827{
d72e31c9
AW
1828 iommu_group_kset = kset_create_and_add("iommu_groups",
1829 NULL, kernel_kobj);
d72e31c9
AW
1830 BUG_ON(!iommu_group_kset);
1831
bad614b2
GH
1832 iommu_debugfs_setup();
1833
d72e31c9 1834 return 0;
1460432c 1835}
d7ef9995 1836core_initcall(iommu_init);
0cd76dd1
JR
1837
1838int iommu_domain_get_attr(struct iommu_domain *domain,
1839 enum iommu_attr attr, void *data)
1840{
0ff64f80 1841 struct iommu_domain_geometry *geometry;
d2e12160 1842 bool *paging;
0ff64f80
JR
1843 int ret = 0;
1844
1845 switch (attr) {
1846 case DOMAIN_ATTR_GEOMETRY:
1847 geometry = data;
1848 *geometry = domain->geometry;
1849
d2e12160
JR
1850 break;
1851 case DOMAIN_ATTR_PAGING:
1852 paging = data;
d16e0faa 1853 *paging = (domain->pgsize_bitmap != 0UL);
0ff64f80
JR
1854 break;
1855 default:
1856 if (!domain->ops->domain_get_attr)
1857 return -EINVAL;
0cd76dd1 1858
0ff64f80
JR
1859 ret = domain->ops->domain_get_attr(domain, attr, data);
1860 }
1861
1862 return ret;
0cd76dd1
JR
1863}
1864EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1865
1866int iommu_domain_set_attr(struct iommu_domain *domain,
1867 enum iommu_attr attr, void *data)
1868{
69356712 1869 int ret = 0;
69356712
JR
1870
1871 switch (attr) {
69356712
JR
1872 default:
1873 if (domain->ops->domain_set_attr == NULL)
1874 return -EINVAL;
1875
1876 ret = domain->ops->domain_set_attr(domain, attr, data);
1877 }
1878
1879 return ret;
1460432c 1880}
0cd76dd1 1881EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
a1015c2b 1882
e5b5234a 1883void iommu_get_resv_regions(struct device *dev, struct list_head *list)
a1015c2b
JR
1884{
1885 const struct iommu_ops *ops = dev->bus->iommu_ops;
1886
e5b5234a
EA
1887 if (ops && ops->get_resv_regions)
1888 ops->get_resv_regions(dev, list);
a1015c2b
JR
1889}
1890
e5b5234a 1891void iommu_put_resv_regions(struct device *dev, struct list_head *list)
a1015c2b
JR
1892{
1893 const struct iommu_ops *ops = dev->bus->iommu_ops;
1894
e5b5234a
EA
1895 if (ops && ops->put_resv_regions)
1896 ops->put_resv_regions(dev, list);
a1015c2b 1897}
d290f1e7 1898
2b20cbba 1899struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
9d3a4de4
RM
1900 size_t length, int prot,
1901 enum iommu_resv_type type)
2b20cbba
EA
1902{
1903 struct iommu_resv_region *region;
1904
1905 region = kzalloc(sizeof(*region), GFP_KERNEL);
1906 if (!region)
1907 return NULL;
1908
1909 INIT_LIST_HEAD(&region->list);
1910 region->start = start;
1911 region->length = length;
1912 region->prot = prot;
1913 region->type = type;
1914 return region;
a1015c2b 1915}
d290f1e7
JR
1916
1917/* Request that a device is direct mapped by the IOMMU */
1918int iommu_request_dm_for_dev(struct device *dev)
1919{
1920 struct iommu_domain *dm_domain;
1921 struct iommu_group *group;
1922 int ret;
1923
1924 /* Device must already be in a group before calling this function */
1925 group = iommu_group_get_for_dev(dev);
409e553d
DC
1926 if (IS_ERR(group))
1927 return PTR_ERR(group);
d290f1e7
JR
1928
1929 mutex_lock(&group->mutex);
1930
1931 /* Check if the default domain is already direct mapped */
1932 ret = 0;
1933 if (group->default_domain &&
1934 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1935 goto out;
1936
1937 /* Don't change mappings of existing devices */
1938 ret = -EBUSY;
1939 if (iommu_group_device_count(group) != 1)
1940 goto out;
1941
1942 /* Allocate a direct mapped domain */
1943 ret = -ENOMEM;
1944 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1945 if (!dm_domain)
1946 goto out;
1947
1948 /* Attach the device to the domain */
1949 ret = __iommu_attach_group(dm_domain, group);
1950 if (ret) {
1951 iommu_domain_free(dm_domain);
1952 goto out;
1953 }
1954
1955 /* Make the direct mapped domain the default for this group */
1956 if (group->default_domain)
1957 iommu_domain_free(group->default_domain);
1958 group->default_domain = dm_domain;
1959
780da9e4 1960 dev_info(dev, "Using iommu direct mapping\n");
d290f1e7
JR
1961
1962 ret = 0;
1963out:
1964 mutex_unlock(&group->mutex);
1965 iommu_group_put(group);
1966
1967 return ret;
1968}
57f98d2f 1969
534766df 1970const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
e4f10ffe 1971{
e4f10ffe 1972 const struct iommu_ops *ops = NULL;
d0f6f583 1973 struct iommu_device *iommu;
e4f10ffe 1974
d0f6f583
JR
1975 spin_lock(&iommu_device_lock);
1976 list_for_each_entry(iommu, &iommu_device_list, list)
1977 if (iommu->fwnode == fwnode) {
1978 ops = iommu->ops;
e4f10ffe
LP
1979 break;
1980 }
d0f6f583 1981 spin_unlock(&iommu_device_lock);
e4f10ffe
LP
1982 return ops;
1983}
1984
57f98d2f
RM
1985int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1986 const struct iommu_ops *ops)
1987{
b4ef725e 1988 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
1989
1990 if (fwspec)
1991 return ops == fwspec->ops ? 0 : -EINVAL;
1992
1993 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1994 if (!fwspec)
1995 return -ENOMEM;
1996
1997 of_node_get(to_of_node(iommu_fwnode));
1998 fwspec->iommu_fwnode = iommu_fwnode;
1999 fwspec->ops = ops;
b4ef725e 2000 dev_iommu_fwspec_set(dev, fwspec);
57f98d2f
RM
2001 return 0;
2002}
2003EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2004
2005void iommu_fwspec_free(struct device *dev)
2006{
b4ef725e 2007 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
2008
2009 if (fwspec) {
2010 fwnode_handle_put(fwspec->iommu_fwnode);
2011 kfree(fwspec);
b4ef725e 2012 dev_iommu_fwspec_set(dev, NULL);
57f98d2f
RM
2013 }
2014}
2015EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2016
2017int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2018{
b4ef725e 2019 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
57f98d2f
RM
2020 size_t size;
2021 int i;
2022
2023 if (!fwspec)
2024 return -EINVAL;
2025
2026 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2027 if (size > sizeof(*fwspec)) {
b4ef725e 2028 fwspec = krealloc(fwspec, size, GFP_KERNEL);
57f98d2f
RM
2029 if (!fwspec)
2030 return -ENOMEM;
909111ba 2031
b4ef725e 2032 dev_iommu_fwspec_set(dev, fwspec);
57f98d2f
RM
2033 }
2034
2035 for (i = 0; i < num_ids; i++)
2036 fwspec->ids[fwspec->num_ids + i] = ids[i];
2037
2038 fwspec->num_ids += num_ids;
57f98d2f
RM
2039 return 0;
2040}
2041EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);