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