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