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