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