iommu: Make sure a device is always attached to a domain
[deliverable/linux.git] / drivers / iommu / iommu.c
CommitLineData
fc2100eb
JR
1/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
63ce3ae8 3 * Author: Joerg Roedel <jroedel@suse.de>
fc2100eb
JR
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
92e7066f 19#define pr_fmt(fmt) "iommu: " fmt
7d3002cc 20
905d66c1 21#include <linux/device.h>
40998188 22#include <linux/kernel.h>
fc2100eb
JR
23#include <linux/bug.h>
24#include <linux/types.h>
60db4027
AM
25#include <linux/module.h>
26#include <linux/slab.h>
fc2100eb
JR
27#include <linux/errno.h>
28#include <linux/iommu.h>
d72e31c9
AW
29#include <linux/idr.h>
30#include <linux/notifier.h>
31#include <linux/err.h>
104a1c13 32#include <linux/pci.h>
f096c061 33#include <linux/bitops.h>
7f6db171 34#include <trace/events/iommu.h>
d72e31c9
AW
35
36static struct kset *iommu_group_kset;
37static struct ida iommu_group_ida;
38static struct mutex iommu_group_mutex;
39
b22f6434
TR
40struct iommu_callback_data {
41 const struct iommu_ops *ops;
42};
43
d72e31c9
AW
44struct 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;
53723dc5 54 struct iommu_domain *default_domain;
e39cb8a3 55 struct iommu_domain *domain;
d72e31c9
AW
56};
57
58struct iommu_device {
59 struct list_head list;
60 struct device *dev;
61 char *name;
62};
63
64struct 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#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
72struct iommu_group_attribute iommu_group_attr_##_name = \
73 __ATTR(_name, _mode, _show, _store)
fc2100eb 74
d72e31c9
AW
75#define to_iommu_group_attr(_attr) \
76 container_of(_attr, struct iommu_group_attribute, attr)
77#define to_iommu_group(_kobj) \
78 container_of(_kobj, struct iommu_group, kobj)
fc2100eb 79
53723dc5
JR
80static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
81 unsigned type);
e39cb8a3
JR
82static int __iommu_attach_device(struct iommu_domain *domain,
83 struct device *dev);
84static int __iommu_attach_group(struct iommu_domain *domain,
85 struct iommu_group *group);
86static void __iommu_detach_group(struct iommu_domain *domain,
87 struct iommu_group *group);
53723dc5 88
d72e31c9
AW
89static ssize_t iommu_group_attr_show(struct kobject *kobj,
90 struct attribute *__attr, char *buf)
1460432c 91{
d72e31c9
AW
92 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
93 struct iommu_group *group = to_iommu_group(kobj);
94 ssize_t ret = -EIO;
1460432c 95
d72e31c9
AW
96 if (attr->show)
97 ret = attr->show(group, buf);
98 return ret;
99}
100
101static ssize_t iommu_group_attr_store(struct kobject *kobj,
102 struct attribute *__attr,
103 const char *buf, size_t count)
104{
105 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
106 struct iommu_group *group = to_iommu_group(kobj);
107 ssize_t ret = -EIO;
1460432c 108
d72e31c9
AW
109 if (attr->store)
110 ret = attr->store(group, buf, count);
111 return ret;
1460432c 112}
1460432c 113
d72e31c9
AW
114static const struct sysfs_ops iommu_group_sysfs_ops = {
115 .show = iommu_group_attr_show,
116 .store = iommu_group_attr_store,
117};
1460432c 118
d72e31c9
AW
119static int iommu_group_create_file(struct iommu_group *group,
120 struct iommu_group_attribute *attr)
121{
122 return sysfs_create_file(&group->kobj, &attr->attr);
1460432c 123}
1460432c 124
d72e31c9
AW
125static void iommu_group_remove_file(struct iommu_group *group,
126 struct iommu_group_attribute *attr)
127{
128 sysfs_remove_file(&group->kobj, &attr->attr);
129}
130
131static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
132{
133 return sprintf(buf, "%s\n", group->name);
134}
135
136static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
137
138static void iommu_group_release(struct kobject *kobj)
139{
140 struct iommu_group *group = to_iommu_group(kobj);
141
269aa808
JR
142 pr_debug("Releasing group %d\n", group->id);
143
d72e31c9
AW
144 if (group->iommu_data_release)
145 group->iommu_data_release(group->iommu_data);
146
147 mutex_lock(&iommu_group_mutex);
148 ida_remove(&iommu_group_ida, group->id);
149 mutex_unlock(&iommu_group_mutex);
150
53723dc5
JR
151 if (group->default_domain)
152 iommu_domain_free(group->default_domain);
153
d72e31c9
AW
154 kfree(group->name);
155 kfree(group);
156}
157
158static struct kobj_type iommu_group_ktype = {
159 .sysfs_ops = &iommu_group_sysfs_ops,
160 .release = iommu_group_release,
161};
162
163/**
164 * iommu_group_alloc - Allocate a new group
165 * @name: Optional name to associate with group, visible in sysfs
166 *
167 * This function is called by an iommu driver to allocate a new iommu
168 * group. The iommu group represents the minimum granularity of the iommu.
169 * Upon successful return, the caller holds a reference to the supplied
170 * group in order to hold the group until devices are added. Use
171 * iommu_group_put() to release this extra reference count, allowing the
172 * group to be automatically reclaimed once it has no devices or external
173 * references.
174 */
175struct iommu_group *iommu_group_alloc(void)
1460432c 176{
d72e31c9
AW
177 struct iommu_group *group;
178 int ret;
179
180 group = kzalloc(sizeof(*group), GFP_KERNEL);
181 if (!group)
182 return ERR_PTR(-ENOMEM);
183
184 group->kobj.kset = iommu_group_kset;
185 mutex_init(&group->mutex);
186 INIT_LIST_HEAD(&group->devices);
187 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
188
189 mutex_lock(&iommu_group_mutex);
190
191again:
192 if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) {
193 kfree(group);
194 mutex_unlock(&iommu_group_mutex);
195 return ERR_PTR(-ENOMEM);
196 }
197
198 if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id))
199 goto again;
200
201 mutex_unlock(&iommu_group_mutex);
1460432c 202
d72e31c9
AW
203 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
204 NULL, "%d", group->id);
205 if (ret) {
206 mutex_lock(&iommu_group_mutex);
207 ida_remove(&iommu_group_ida, group->id);
208 mutex_unlock(&iommu_group_mutex);
209 kfree(group);
210 return ERR_PTR(ret);
211 }
212
213 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
214 if (!group->devices_kobj) {
215 kobject_put(&group->kobj); /* triggers .release & free */
216 return ERR_PTR(-ENOMEM);
217 }
218
219 /*
220 * The devices_kobj holds a reference on the group kobject, so
221 * as long as that exists so will the group. We can therefore
222 * use the devices_kobj for reference counting.
223 */
224 kobject_put(&group->kobj);
225
269aa808
JR
226 pr_debug("Allocated group %d\n", group->id);
227
d72e31c9
AW
228 return group;
229}
230EXPORT_SYMBOL_GPL(iommu_group_alloc);
231
aa16bea9
AK
232struct iommu_group *iommu_group_get_by_id(int id)
233{
234 struct kobject *group_kobj;
235 struct iommu_group *group;
236 const char *name;
237
238 if (!iommu_group_kset)
239 return NULL;
240
241 name = kasprintf(GFP_KERNEL, "%d", id);
242 if (!name)
243 return NULL;
244
245 group_kobj = kset_find_obj(iommu_group_kset, name);
246 kfree(name);
247
248 if (!group_kobj)
249 return NULL;
250
251 group = container_of(group_kobj, struct iommu_group, kobj);
252 BUG_ON(group->id != id);
253
254 kobject_get(group->devices_kobj);
255 kobject_put(&group->kobj);
256
257 return group;
258}
259EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
260
d72e31c9
AW
261/**
262 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
263 * @group: the group
264 *
265 * iommu drivers can store data in the group for use when doing iommu
266 * operations. This function provides a way to retrieve it. Caller
267 * should hold a group reference.
268 */
269void *iommu_group_get_iommudata(struct iommu_group *group)
270{
271 return group->iommu_data;
272}
273EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
274
275/**
276 * iommu_group_set_iommudata - set iommu_data for a group
277 * @group: the group
278 * @iommu_data: new data
279 * @release: release function for iommu_data
280 *
281 * iommu drivers can store data in the group for use when doing iommu
282 * operations. This function provides a way to set the data after
283 * the group has been allocated. Caller should hold a group reference.
284 */
285void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
286 void (*release)(void *iommu_data))
1460432c 287{
d72e31c9
AW
288 group->iommu_data = iommu_data;
289 group->iommu_data_release = release;
290}
291EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1460432c 292
d72e31c9
AW
293/**
294 * iommu_group_set_name - set name for a group
295 * @group: the group
296 * @name: name
297 *
298 * Allow iommu driver to set a name for a group. When set it will
299 * appear in a name attribute file under the group in sysfs.
300 */
301int iommu_group_set_name(struct iommu_group *group, const char *name)
302{
303 int ret;
304
305 if (group->name) {
306 iommu_group_remove_file(group, &iommu_group_attr_name);
307 kfree(group->name);
308 group->name = NULL;
309 if (!name)
310 return 0;
311 }
312
313 group->name = kstrdup(name, GFP_KERNEL);
314 if (!group->name)
315 return -ENOMEM;
316
317 ret = iommu_group_create_file(group, &iommu_group_attr_name);
318 if (ret) {
319 kfree(group->name);
320 group->name = NULL;
321 return ret;
322 }
1460432c
AW
323
324 return 0;
325}
d72e31c9 326EXPORT_SYMBOL_GPL(iommu_group_set_name);
1460432c 327
d72e31c9
AW
328/**
329 * iommu_group_add_device - add a device to an iommu group
330 * @group: the group into which to add the device (reference should be held)
331 * @dev: the device
332 *
333 * This function is called by an iommu driver to add a device into a
334 * group. Adding a device increments the group reference count.
335 */
336int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1460432c 337{
d72e31c9
AW
338 int ret, i = 0;
339 struct iommu_device *device;
340
341 device = kzalloc(sizeof(*device), GFP_KERNEL);
342 if (!device)
343 return -ENOMEM;
344
345 device->dev = dev;
1460432c 346
d72e31c9
AW
347 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
348 if (ret) {
349 kfree(device);
350 return ret;
351 }
352
353 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
354rename:
355 if (!device->name) {
356 sysfs_remove_link(&dev->kobj, "iommu_group");
357 kfree(device);
358 return -ENOMEM;
359 }
1460432c 360
d72e31c9
AW
361 ret = sysfs_create_link_nowarn(group->devices_kobj,
362 &dev->kobj, device->name);
363 if (ret) {
364 kfree(device->name);
365 if (ret == -EEXIST && i >= 0) {
366 /*
367 * Account for the slim chance of collision
368 * and append an instance to the name.
369 */
370 device->name = kasprintf(GFP_KERNEL, "%s.%d",
371 kobject_name(&dev->kobj), i++);
372 goto rename;
373 }
374
375 sysfs_remove_link(&dev->kobj, "iommu_group");
376 kfree(device);
377 return ret;
378 }
379
380 kobject_get(group->devices_kobj);
381
382 dev->iommu_group = group;
383
384 mutex_lock(&group->mutex);
385 list_add_tail(&device->list, &group->devices);
e39cb8a3
JR
386 if (group->domain)
387 __iommu_attach_device(group->domain, dev);
d72e31c9
AW
388 mutex_unlock(&group->mutex);
389
390 /* Notify any listeners about change to group. */
391 blocking_notifier_call_chain(&group->notifier,
392 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
d1cf7e82
SK
393
394 trace_add_device_to_group(group->id, dev);
269aa808
JR
395
396 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
397
1460432c
AW
398 return 0;
399}
d72e31c9 400EXPORT_SYMBOL_GPL(iommu_group_add_device);
1460432c 401
d72e31c9
AW
402/**
403 * iommu_group_remove_device - remove a device from it's current group
404 * @dev: device to be removed
405 *
406 * This function is called by an iommu driver to remove the device from
407 * it's current group. This decrements the iommu group reference count.
408 */
409void iommu_group_remove_device(struct device *dev)
410{
411 struct iommu_group *group = dev->iommu_group;
412 struct iommu_device *tmp_device, *device = NULL;
413
269aa808
JR
414 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
415
d72e31c9
AW
416 /* Pre-notify listeners that a device is being removed. */
417 blocking_notifier_call_chain(&group->notifier,
418 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
419
420 mutex_lock(&group->mutex);
421 list_for_each_entry(tmp_device, &group->devices, list) {
422 if (tmp_device->dev == dev) {
423 device = tmp_device;
424 list_del(&device->list);
425 break;
426 }
427 }
428 mutex_unlock(&group->mutex);
429
430 if (!device)
431 return;
432
433 sysfs_remove_link(group->devices_kobj, device->name);
434 sysfs_remove_link(&dev->kobj, "iommu_group");
435
2e757086
SK
436 trace_remove_device_from_group(group->id, dev);
437
d72e31c9
AW
438 kfree(device->name);
439 kfree(device);
440 dev->iommu_group = NULL;
441 kobject_put(group->devices_kobj);
442}
443EXPORT_SYMBOL_GPL(iommu_group_remove_device);
444
426a2738
JR
445static int iommu_group_device_count(struct iommu_group *group)
446{
447 struct iommu_device *entry;
448 int ret = 0;
449
450 list_for_each_entry(entry, &group->devices, list)
451 ret++;
452
453 return ret;
454}
455
d72e31c9
AW
456/**
457 * iommu_group_for_each_dev - iterate over each device in the group
458 * @group: the group
459 * @data: caller opaque data to be passed to callback function
460 * @fn: caller supplied callback function
461 *
462 * This function is called by group users to iterate over group devices.
463 * Callers should hold a reference count to the group during callback.
464 * The group->mutex is held across callbacks, which will block calls to
465 * iommu_group_add/remove_device.
466 */
e39cb8a3
JR
467static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
468 int (*fn)(struct device *, void *))
d72e31c9
AW
469{
470 struct iommu_device *device;
471 int ret = 0;
472
d72e31c9
AW
473 list_for_each_entry(device, &group->devices, list) {
474 ret = fn(device->dev, data);
475 if (ret)
476 break;
477 }
e39cb8a3
JR
478 return ret;
479}
480
481
482int iommu_group_for_each_dev(struct iommu_group *group, void *data,
483 int (*fn)(struct device *, void *))
484{
485 int ret;
486
487 mutex_lock(&group->mutex);
488 ret = __iommu_group_for_each_dev(group, data, fn);
d72e31c9 489 mutex_unlock(&group->mutex);
e39cb8a3 490
d72e31c9
AW
491 return ret;
492}
493EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
494
495/**
496 * iommu_group_get - Return the group for a device and increment reference
497 * @dev: get the group that this device belongs to
498 *
499 * This function is called by iommu drivers and users to get the group
500 * for the specified device. If found, the group is returned and the group
501 * reference in incremented, else NULL.
502 */
503struct iommu_group *iommu_group_get(struct device *dev)
504{
505 struct iommu_group *group = dev->iommu_group;
506
507 if (group)
508 kobject_get(group->devices_kobj);
509
510 return group;
511}
512EXPORT_SYMBOL_GPL(iommu_group_get);
513
514/**
515 * iommu_group_put - Decrement group reference
516 * @group: the group to use
517 *
518 * This function is called by iommu drivers and users to release the
519 * iommu group. Once the reference count is zero, the group is released.
520 */
521void iommu_group_put(struct iommu_group *group)
522{
523 if (group)
524 kobject_put(group->devices_kobj);
525}
526EXPORT_SYMBOL_GPL(iommu_group_put);
527
528/**
529 * iommu_group_register_notifier - Register a notifier for group changes
530 * @group: the group to watch
531 * @nb: notifier block to signal
532 *
533 * This function allows iommu group users to track changes in a group.
534 * See include/linux/iommu.h for actions sent via this notifier. Caller
535 * should hold a reference to the group throughout notifier registration.
536 */
537int iommu_group_register_notifier(struct iommu_group *group,
538 struct notifier_block *nb)
539{
540 return blocking_notifier_chain_register(&group->notifier, nb);
541}
542EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
543
544/**
545 * iommu_group_unregister_notifier - Unregister a notifier
546 * @group: the group to watch
547 * @nb: notifier block to signal
548 *
549 * Unregister a previously registered group notifier block.
550 */
551int iommu_group_unregister_notifier(struct iommu_group *group,
552 struct notifier_block *nb)
553{
554 return blocking_notifier_chain_unregister(&group->notifier, nb);
555}
556EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
557
558/**
559 * iommu_group_id - Return ID for a group
560 * @group: the group to ID
561 *
562 * Return the unique ID for the group matching the sysfs group number.
563 */
564int iommu_group_id(struct iommu_group *group)
565{
566 return group->id;
567}
568EXPORT_SYMBOL_GPL(iommu_group_id);
1460432c 569
f096c061
AW
570static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
571 unsigned long *devfns);
572
104a1c13
AW
573/*
574 * To consider a PCI device isolated, we require ACS to support Source
575 * Validation, Request Redirection, Completer Redirection, and Upstream
576 * Forwarding. This effectively means that devices cannot spoof their
577 * requester ID, requests and completions cannot be redirected, and all
578 * transactions are forwarded upstream, even as it passes through a
579 * bridge where the target device is downstream.
580 */
581#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
582
f096c061
AW
583/*
584 * For multifunction devices which are not isolated from each other, find
585 * all the other non-isolated functions and look for existing groups. For
586 * each function, we also need to look for aliases to or from other devices
587 * that may already have a group.
588 */
589static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
590 unsigned long *devfns)
591{
592 struct pci_dev *tmp = NULL;
593 struct iommu_group *group;
594
595 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
596 return NULL;
597
598 for_each_pci_dev(tmp) {
599 if (tmp == pdev || tmp->bus != pdev->bus ||
600 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
601 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
602 continue;
603
604 group = get_pci_alias_group(tmp, devfns);
605 if (group) {
606 pci_dev_put(tmp);
607 return group;
608 }
609 }
610
611 return NULL;
612}
613
614/*
615 * Look for aliases to or from the given device for exisiting groups. The
616 * dma_alias_devfn only supports aliases on the same bus, therefore the search
617 * space is quite small (especially since we're really only looking at pcie
618 * device, and therefore only expect multiple slots on the root complex or
619 * downstream switch ports). It's conceivable though that a pair of
620 * multifunction devices could have aliases between them that would cause a
621 * loop. To prevent this, we use a bitmap to track where we've been.
622 */
623static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
624 unsigned long *devfns)
625{
626 struct pci_dev *tmp = NULL;
627 struct iommu_group *group;
628
629 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
630 return NULL;
631
632 group = iommu_group_get(&pdev->dev);
633 if (group)
634 return group;
635
636 for_each_pci_dev(tmp) {
637 if (tmp == pdev || tmp->bus != pdev->bus)
638 continue;
639
640 /* We alias them or they alias us */
641 if (((pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
642 pdev->dma_alias_devfn == tmp->devfn) ||
643 ((tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
644 tmp->dma_alias_devfn == pdev->devfn)) {
645
646 group = get_pci_alias_group(tmp, devfns);
647 if (group) {
648 pci_dev_put(tmp);
649 return group;
650 }
651
652 group = get_pci_function_alias_group(tmp, devfns);
653 if (group) {
654 pci_dev_put(tmp);
655 return group;
656 }
657 }
658 }
659
660 return NULL;
661}
662
104a1c13
AW
663struct group_for_pci_data {
664 struct pci_dev *pdev;
665 struct iommu_group *group;
666};
667
668/*
669 * DMA alias iterator callback, return the last seen device. Stop and return
670 * the IOMMU group if we find one along the way.
671 */
672static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
673{
674 struct group_for_pci_data *data = opaque;
675
676 data->pdev = pdev;
677 data->group = iommu_group_get(&pdev->dev);
678
679 return data->group != NULL;
680}
681
682/*
683 * Use standard PCI bus topology, isolation features, and DMA alias quirks
684 * to find or create an IOMMU group for a device.
685 */
686static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
687{
688 struct group_for_pci_data data;
689 struct pci_bus *bus;
690 struct iommu_group *group = NULL;
f096c061 691 u64 devfns[4] = { 0 };
104a1c13
AW
692
693 /*
694 * Find the upstream DMA alias for the device. A device must not
695 * be aliased due to topology in order to have its own IOMMU group.
696 * If we find an alias along the way that already belongs to a
697 * group, use it.
698 */
699 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
700 return data.group;
701
702 pdev = data.pdev;
703
704 /*
705 * Continue upstream from the point of minimum IOMMU granularity
706 * due to aliases to the point where devices are protected from
707 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
708 * group, use it.
709 */
710 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
711 if (!bus->self)
712 continue;
713
714 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
715 break;
716
717 pdev = bus->self;
718
719 group = iommu_group_get(&pdev->dev);
720 if (group)
721 return group;
722 }
723
724 /*
f096c061
AW
725 * Look for existing groups on device aliases. If we alias another
726 * device or another device aliases us, use the same group.
104a1c13 727 */
f096c061
AW
728 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
729 if (group)
730 return group;
104a1c13
AW
731
732 /*
f096c061
AW
733 * Look for existing groups on non-isolated functions on the same
734 * slot and aliases of those funcions, if any. No need to clear
735 * the search bitmap, the tested devfns are still valid.
104a1c13 736 */
f096c061
AW
737 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
738 if (group)
739 return group;
104a1c13
AW
740
741 /* No shared group found, allocate new */
53723dc5
JR
742 group = iommu_group_alloc();
743 if (group) {
744 /*
745 * Try to allocate a default domain - needs support from the
746 * IOMMU driver.
747 */
748 group->default_domain = __iommu_domain_alloc(pdev->dev.bus,
749 IOMMU_DOMAIN_DMA);
e39cb8a3 750 group->domain = group->default_domain;
53723dc5
JR
751 }
752
753 return group;
104a1c13
AW
754}
755
756/**
757 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
758 * @dev: target device
759 *
760 * This function is intended to be called by IOMMU drivers and extended to
761 * support common, bus-defined algorithms when determining or creating the
762 * IOMMU group for a device. On success, the caller will hold a reference
763 * to the returned IOMMU group, which will already include the provided
764 * device. The reference should be released with iommu_group_put().
765 */
766struct iommu_group *iommu_group_get_for_dev(struct device *dev)
767{
c4a783b8 768 struct iommu_group *group;
104a1c13
AW
769 int ret;
770
771 group = iommu_group_get(dev);
772 if (group)
773 return group;
774
c4a783b8
JR
775 if (!dev_is_pci(dev))
776 return ERR_PTR(-EINVAL);
777
778 group = iommu_group_get_for_pci_dev(to_pci_dev(dev));
104a1c13
AW
779
780 if (IS_ERR(group))
781 return group;
782
783 ret = iommu_group_add_device(group, dev);
784 if (ret) {
785 iommu_group_put(group);
786 return ERR_PTR(ret);
787 }
788
789 return group;
790}
791
d72e31c9 792static int add_iommu_group(struct device *dev, void *data)
1460432c 793{
b22f6434
TR
794 struct iommu_callback_data *cb = data;
795 const struct iommu_ops *ops = cb->ops;
d72e31c9
AW
796
797 if (!ops->add_device)
461bfb3f 798 return 0;
1460432c 799
d72e31c9
AW
800 WARN_ON(dev->iommu_group);
801
19762d70 802 return ops->add_device(dev);
1460432c
AW
803}
804
8da30142
JR
805static int remove_iommu_group(struct device *dev, void *data)
806{
807 struct iommu_callback_data *cb = data;
808 const struct iommu_ops *ops = cb->ops;
809
810 if (ops->remove_device && dev->iommu_group)
811 ops->remove_device(dev);
812
813 return 0;
814}
815
d72e31c9
AW
816static int iommu_bus_notifier(struct notifier_block *nb,
817 unsigned long action, void *data)
1460432c
AW
818{
819 struct device *dev = data;
b22f6434 820 const struct iommu_ops *ops = dev->bus->iommu_ops;
d72e31c9
AW
821 struct iommu_group *group;
822 unsigned long group_action = 0;
823
824 /*
825 * ADD/DEL call into iommu driver ops if provided, which may
826 * result in ADD/DEL notifiers to group->notifier
827 */
828 if (action == BUS_NOTIFY_ADD_DEVICE) {
829 if (ops->add_device)
830 return ops->add_device(dev);
843cb6dc 831 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
d72e31c9
AW
832 if (ops->remove_device && dev->iommu_group) {
833 ops->remove_device(dev);
834 return 0;
835 }
836 }
1460432c 837
d72e31c9
AW
838 /*
839 * Remaining BUS_NOTIFYs get filtered and republished to the
840 * group, if anyone is listening
841 */
842 group = iommu_group_get(dev);
843 if (!group)
844 return 0;
1460432c 845
d72e31c9
AW
846 switch (action) {
847 case BUS_NOTIFY_BIND_DRIVER:
848 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
849 break;
850 case BUS_NOTIFY_BOUND_DRIVER:
851 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
852 break;
853 case BUS_NOTIFY_UNBIND_DRIVER:
854 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
855 break;
856 case BUS_NOTIFY_UNBOUND_DRIVER:
857 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
858 break;
859 }
1460432c 860
d72e31c9
AW
861 if (group_action)
862 blocking_notifier_call_chain(&group->notifier,
863 group_action, dev);
1460432c 864
d72e31c9 865 iommu_group_put(group);
1460432c
AW
866 return 0;
867}
868
fb3e3065 869static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
ff21776d 870{
fb3e3065
MS
871 int err;
872 struct notifier_block *nb;
b22f6434
TR
873 struct iommu_callback_data cb = {
874 .ops = ops,
875 };
876
fb3e3065
MS
877 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
878 if (!nb)
879 return -ENOMEM;
880
881 nb->notifier_call = iommu_bus_notifier;
882
883 err = bus_register_notifier(bus, nb);
8da30142
JR
884 if (err)
885 goto out_free;
d7da6bdc
HS
886
887 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
8da30142
JR
888 if (err)
889 goto out_err;
890
d7da6bdc
HS
891
892 return 0;
8da30142
JR
893
894out_err:
895 /* Clean up */
896 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
897 bus_unregister_notifier(bus, nb);
898
899out_free:
900 kfree(nb);
901
902 return err;
ff21776d 903}
fc2100eb 904
ff21776d
JR
905/**
906 * bus_set_iommu - set iommu-callbacks for the bus
907 * @bus: bus.
908 * @ops: the callbacks provided by the iommu-driver
909 *
910 * This function is called by an iommu driver to set the iommu methods
911 * used for a particular bus. Drivers for devices on that bus can use
912 * the iommu-api after these ops are registered.
913 * This special function is needed because IOMMUs are usually devices on
914 * the bus itself, so the iommu drivers are not initialized when the bus
915 * is set up. With this function the iommu-driver can set the iommu-ops
916 * afterwards.
917 */
b22f6434 918int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
fc2100eb 919{
d7da6bdc
HS
920 int err;
921
ff21776d
JR
922 if (bus->iommu_ops != NULL)
923 return -EBUSY;
fc2100eb 924
ff21776d
JR
925 bus->iommu_ops = ops;
926
927 /* Do IOMMU specific setup for this bus-type */
d7da6bdc
HS
928 err = iommu_bus_init(bus, ops);
929 if (err)
930 bus->iommu_ops = NULL;
931
932 return err;
fc2100eb 933}
ff21776d 934EXPORT_SYMBOL_GPL(bus_set_iommu);
fc2100eb 935
a1b60c1c 936bool iommu_present(struct bus_type *bus)
fc2100eb 937{
94441c3b 938 return bus->iommu_ops != NULL;
fc2100eb 939}
a1b60c1c 940EXPORT_SYMBOL_GPL(iommu_present);
fc2100eb 941
3c0e0ca0
JR
942bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
943{
944 if (!bus->iommu_ops || !bus->iommu_ops->capable)
945 return false;
946
947 return bus->iommu_ops->capable(cap);
948}
949EXPORT_SYMBOL_GPL(iommu_capable);
950
4f3f8d9d
OBC
951/**
952 * iommu_set_fault_handler() - set a fault handler for an iommu domain
953 * @domain: iommu domain
954 * @handler: fault handler
77ca2332 955 * @token: user data, will be passed back to the fault handler
0ed6d2d2
OBC
956 *
957 * This function should be used by IOMMU users which want to be notified
958 * whenever an IOMMU fault happens.
959 *
960 * The fault handler itself should return 0 on success, and an appropriate
961 * error code otherwise.
4f3f8d9d
OBC
962 */
963void iommu_set_fault_handler(struct iommu_domain *domain,
77ca2332
OBC
964 iommu_fault_handler_t handler,
965 void *token)
4f3f8d9d
OBC
966{
967 BUG_ON(!domain);
968
969 domain->handler = handler;
77ca2332 970 domain->handler_token = token;
4f3f8d9d 971}
30bd918c 972EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
4f3f8d9d 973
53723dc5
JR
974static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
975 unsigned type)
fc2100eb
JR
976{
977 struct iommu_domain *domain;
fc2100eb 978
94441c3b 979 if (bus == NULL || bus->iommu_ops == NULL)
905d66c1
JR
980 return NULL;
981
53723dc5 982 domain = bus->iommu_ops->domain_alloc(type);
fc2100eb
JR
983 if (!domain)
984 return NULL;
985
8539c7c1 986 domain->ops = bus->iommu_ops;
53723dc5 987 domain->type = type;
905d66c1 988
fc2100eb 989 return domain;
fc2100eb 990}
53723dc5
JR
991
992struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
993{
994 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
995}
fc2100eb
JR
996EXPORT_SYMBOL_GPL(iommu_domain_alloc);
997
998void iommu_domain_free(struct iommu_domain *domain)
999{
89be34a1 1000 domain->ops->domain_free(domain);
fc2100eb
JR
1001}
1002EXPORT_SYMBOL_GPL(iommu_domain_free);
1003
426a2738
JR
1004static int __iommu_attach_device(struct iommu_domain *domain,
1005 struct device *dev)
fc2100eb 1006{
b54db778 1007 int ret;
e5aa7f00
JR
1008 if (unlikely(domain->ops->attach_dev == NULL))
1009 return -ENODEV;
1010
b54db778
SK
1011 ret = domain->ops->attach_dev(domain, dev);
1012 if (!ret)
1013 trace_attach_device_to_domain(dev);
1014 return ret;
fc2100eb 1015}
426a2738
JR
1016
1017int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1018{
1019 struct iommu_group *group;
1020 int ret;
1021
1022 group = iommu_group_get(dev);
1023 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1024 if (group == NULL)
1025 return __iommu_attach_device(domain, dev);
1026
1027 /*
1028 * We have a group - lock it to make sure the device-count doesn't
1029 * change while we are attaching
1030 */
1031 mutex_lock(&group->mutex);
1032 ret = -EINVAL;
1033 if (iommu_group_device_count(group) != 1)
1034 goto out_unlock;
1035
e39cb8a3 1036 ret = __iommu_attach_group(domain, group);
426a2738
JR
1037
1038out_unlock:
1039 mutex_unlock(&group->mutex);
1040 iommu_group_put(group);
1041
1042 return ret;
1043}
fc2100eb
JR
1044EXPORT_SYMBOL_GPL(iommu_attach_device);
1045
426a2738
JR
1046static void __iommu_detach_device(struct iommu_domain *domain,
1047 struct device *dev)
fc2100eb 1048{
e5aa7f00
JR
1049 if (unlikely(domain->ops->detach_dev == NULL))
1050 return;
1051
1052 domain->ops->detach_dev(domain, dev);
69980630 1053 trace_detach_device_from_domain(dev);
fc2100eb 1054}
426a2738
JR
1055
1056void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1057{
1058 struct iommu_group *group;
1059
1060 group = iommu_group_get(dev);
1061 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1062 if (group == NULL)
1063 return __iommu_detach_device(domain, dev);
1064
1065 mutex_lock(&group->mutex);
1066 if (iommu_group_device_count(group) != 1) {
1067 WARN_ON(1);
1068 goto out_unlock;
1069 }
1070
e39cb8a3 1071 __iommu_detach_group(domain, group);
426a2738
JR
1072
1073out_unlock:
1074 mutex_unlock(&group->mutex);
1075 iommu_group_put(group);
1076}
fc2100eb
JR
1077EXPORT_SYMBOL_GPL(iommu_detach_device);
1078
d72e31c9
AW
1079/*
1080 * IOMMU groups are really the natrual working unit of the IOMMU, but
1081 * the IOMMU API works on domains and devices. Bridge that gap by
1082 * iterating over the devices in a group. Ideally we'd have a single
1083 * device which represents the requestor ID of the group, but we also
1084 * allow IOMMU drivers to create policy defined minimum sets, where
1085 * the physical hardware may be able to distiguish members, but we
1086 * wish to group them at a higher level (ex. untrusted multi-function
1087 * PCI devices). Thus we attach each device.
1088 */
1089static int iommu_group_do_attach_device(struct device *dev, void *data)
1090{
1091 struct iommu_domain *domain = data;
1092
426a2738 1093 return __iommu_attach_device(domain, dev);
d72e31c9
AW
1094}
1095
e39cb8a3
JR
1096static int __iommu_attach_group(struct iommu_domain *domain,
1097 struct iommu_group *group)
1098{
1099 int ret;
1100
1101 if (group->default_domain && group->domain != group->default_domain)
1102 return -EBUSY;
1103
1104 ret = __iommu_group_for_each_dev(group, domain,
1105 iommu_group_do_attach_device);
1106 if (ret == 0)
1107 group->domain = domain;
1108
1109 return ret;
1110}
1111
d72e31c9
AW
1112int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1113{
e39cb8a3
JR
1114 int ret;
1115
1116 mutex_lock(&group->mutex);
1117 ret = __iommu_attach_group(domain, group);
1118 mutex_unlock(&group->mutex);
1119
1120 return ret;
d72e31c9
AW
1121}
1122EXPORT_SYMBOL_GPL(iommu_attach_group);
1123
1124static int iommu_group_do_detach_device(struct device *dev, void *data)
1125{
1126 struct iommu_domain *domain = data;
1127
426a2738 1128 __iommu_detach_device(domain, dev);
d72e31c9
AW
1129
1130 return 0;
1131}
1132
e39cb8a3
JR
1133static void __iommu_detach_group(struct iommu_domain *domain,
1134 struct iommu_group *group)
1135{
1136 int ret;
1137
1138 if (!group->default_domain) {
1139 __iommu_group_for_each_dev(group, domain,
1140 iommu_group_do_detach_device);
1141 group->domain = NULL;
1142 return;
1143 }
1144
1145 if (group->domain == group->default_domain)
1146 return;
1147
1148 /* Detach by re-attaching to the default domain */
1149 ret = __iommu_group_for_each_dev(group, group->default_domain,
1150 iommu_group_do_attach_device);
1151 if (ret != 0)
1152 WARN_ON(1);
1153 else
1154 group->domain = group->default_domain;
1155}
1156
d72e31c9
AW
1157void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1158{
e39cb8a3
JR
1159 mutex_lock(&group->mutex);
1160 __iommu_detach_group(domain, group);
1161 mutex_unlock(&group->mutex);
d72e31c9
AW
1162}
1163EXPORT_SYMBOL_GPL(iommu_detach_group);
1164
bb5547ac 1165phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
fc2100eb 1166{
e5aa7f00
JR
1167 if (unlikely(domain->ops->iova_to_phys == NULL))
1168 return 0;
1169
1170 return domain->ops->iova_to_phys(domain, iova);
fc2100eb
JR
1171}
1172EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
dbb9fd86 1173
bd13969b
AW
1174static size_t iommu_pgsize(struct iommu_domain *domain,
1175 unsigned long addr_merge, size_t size)
1176{
1177 unsigned int pgsize_idx;
1178 size_t pgsize;
1179
1180 /* Max page size that still fits into 'size' */
1181 pgsize_idx = __fls(size);
1182
1183 /* need to consider alignment requirements ? */
1184 if (likely(addr_merge)) {
1185 /* Max page size allowed by address */
1186 unsigned int align_pgsize_idx = __ffs(addr_merge);
1187 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1188 }
1189
1190 /* build a mask of acceptable page sizes */
1191 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1192
1193 /* throw away page sizes not supported by the hardware */
1194 pgsize &= domain->ops->pgsize_bitmap;
1195
1196 /* make sure we're still sane */
1197 BUG_ON(!pgsize);
1198
1199 /* pick the biggest page */
1200 pgsize_idx = __fls(pgsize);
1201 pgsize = 1UL << pgsize_idx;
1202
1203 return pgsize;
1204}
1205
cefc53c7 1206int iommu_map(struct iommu_domain *domain, unsigned long iova,
7d3002cc 1207 phys_addr_t paddr, size_t size, int prot)
cefc53c7 1208{
7d3002cc
OBC
1209 unsigned long orig_iova = iova;
1210 unsigned int min_pagesz;
1211 size_t orig_size = size;
1212 int ret = 0;
cefc53c7 1213
9db4ad91 1214 if (unlikely(domain->ops->map == NULL ||
57886518 1215 domain->ops->pgsize_bitmap == 0UL))
e5aa7f00 1216 return -ENODEV;
cefc53c7 1217
a10315e5
JR
1218 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1219 return -EINVAL;
1220
7d3002cc
OBC
1221 /* find out the minimum page size supported */
1222 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
1223
1224 /*
1225 * both the virtual address and the physical one, as well as
1226 * the size of the mapping, must be aligned (at least) to the
1227 * size of the smallest page supported by the hardware
1228 */
1229 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
abedb049 1230 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
6197ca82 1231 iova, &paddr, size, min_pagesz);
7d3002cc
OBC
1232 return -EINVAL;
1233 }
1234
abedb049 1235 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
7d3002cc
OBC
1236
1237 while (size) {
bd13969b 1238 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
7d3002cc 1239
abedb049 1240 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
6197ca82 1241 iova, &paddr, pgsize);
7d3002cc
OBC
1242
1243 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1244 if (ret)
1245 break;
1246
1247 iova += pgsize;
1248 paddr += pgsize;
1249 size -= pgsize;
1250 }
1251
1252 /* unroll mapping in case something went wrong */
1253 if (ret)
1254 iommu_unmap(domain, orig_iova, orig_size - size);
e0be7c86 1255 else
860cd64d 1256 trace_map(orig_iova, paddr, orig_size);
7d3002cc
OBC
1257
1258 return ret;
cefc53c7
JR
1259}
1260EXPORT_SYMBOL_GPL(iommu_map);
1261
7d3002cc 1262size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
cefc53c7 1263{
7d3002cc
OBC
1264 size_t unmapped_page, unmapped = 0;
1265 unsigned int min_pagesz;
6fd492fd 1266 unsigned long orig_iova = iova;
cefc53c7 1267
57886518
JR
1268 if (unlikely(domain->ops->unmap == NULL ||
1269 domain->ops->pgsize_bitmap == 0UL))
e5aa7f00
JR
1270 return -ENODEV;
1271
a10315e5
JR
1272 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1273 return -EINVAL;
1274
7d3002cc
OBC
1275 /* find out the minimum page size supported */
1276 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
1277
1278 /*
1279 * The virtual address, as well as the size of the mapping, must be
1280 * aligned (at least) to the size of the smallest page supported
1281 * by the hardware
1282 */
1283 if (!IS_ALIGNED(iova | size, min_pagesz)) {
6197ca82
JP
1284 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1285 iova, size, min_pagesz);
7d3002cc
OBC
1286 return -EINVAL;
1287 }
1288
6197ca82 1289 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
7d3002cc
OBC
1290
1291 /*
1292 * Keep iterating until we either unmap 'size' bytes (or more)
1293 * or we hit an area that isn't mapped.
1294 */
1295 while (unmapped < size) {
bd13969b 1296 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
7d3002cc 1297
bd13969b 1298 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
7d3002cc
OBC
1299 if (!unmapped_page)
1300 break;
1301
6197ca82
JP
1302 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1303 iova, unmapped_page);
7d3002cc
OBC
1304
1305 iova += unmapped_page;
1306 unmapped += unmapped_page;
1307 }
1308
db8614d3 1309 trace_unmap(orig_iova, size, unmapped);
7d3002cc 1310 return unmapped;
cefc53c7
JR
1311}
1312EXPORT_SYMBOL_GPL(iommu_unmap);
1460432c 1313
315786eb
OH
1314size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1315 struct scatterlist *sg, unsigned int nents, int prot)
1316{
38ec010d 1317 struct scatterlist *s;
315786eb 1318 size_t mapped = 0;
18f23409 1319 unsigned int i, min_pagesz;
38ec010d 1320 int ret;
315786eb 1321
18f23409
RM
1322 if (unlikely(domain->ops->pgsize_bitmap == 0UL))
1323 return 0;
315786eb 1324
18f23409
RM
1325 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
1326
1327 for_each_sg(sg, s, nents, i) {
1328 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
1329
1330 /*
1331 * We are mapping on IOMMU page boundaries, so offset within
1332 * the page must be 0. However, the IOMMU may support pages
1333 * smaller than PAGE_SIZE, so s->offset may still represent
1334 * an offset of that boundary within the CPU page.
1335 */
1336 if (!IS_ALIGNED(s->offset, min_pagesz))
38ec010d
JR
1337 goto out_err;
1338
1339 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1340 if (ret)
1341 goto out_err;
1342
1343 mapped += s->length;
315786eb
OH
1344 }
1345
1346 return mapped;
38ec010d
JR
1347
1348out_err:
1349 /* undo mappings already done */
1350 iommu_unmap(domain, iova, mapped);
1351
1352 return 0;
1353
315786eb
OH
1354}
1355EXPORT_SYMBOL_GPL(default_iommu_map_sg);
d7787d57
JR
1356
1357int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
80f97f0f 1358 phys_addr_t paddr, u64 size, int prot)
d7787d57
JR
1359{
1360 if (unlikely(domain->ops->domain_window_enable == NULL))
1361 return -ENODEV;
1362
80f97f0f
VS
1363 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1364 prot);
d7787d57
JR
1365}
1366EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1367
1368void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1369{
1370 if (unlikely(domain->ops->domain_window_disable == NULL))
1371 return;
1372
1373 return domain->ops->domain_window_disable(domain, wnd_nr);
1374}
1375EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1376
d72e31c9 1377static int __init iommu_init(void)
1460432c 1378{
d72e31c9
AW
1379 iommu_group_kset = kset_create_and_add("iommu_groups",
1380 NULL, kernel_kobj);
1381 ida_init(&iommu_group_ida);
1382 mutex_init(&iommu_group_mutex);
1460432c 1383
d72e31c9
AW
1384 BUG_ON(!iommu_group_kset);
1385
1386 return 0;
1460432c 1387}
097e3635 1388arch_initcall(iommu_init);
0cd76dd1
JR
1389
1390int iommu_domain_get_attr(struct iommu_domain *domain,
1391 enum iommu_attr attr, void *data)
1392{
0ff64f80 1393 struct iommu_domain_geometry *geometry;
d2e12160 1394 bool *paging;
0ff64f80 1395 int ret = 0;
69356712 1396 u32 *count;
0ff64f80
JR
1397
1398 switch (attr) {
1399 case DOMAIN_ATTR_GEOMETRY:
1400 geometry = data;
1401 *geometry = domain->geometry;
1402
d2e12160
JR
1403 break;
1404 case DOMAIN_ATTR_PAGING:
1405 paging = data;
1406 *paging = (domain->ops->pgsize_bitmap != 0UL);
69356712
JR
1407 break;
1408 case DOMAIN_ATTR_WINDOWS:
1409 count = data;
1410
1411 if (domain->ops->domain_get_windows != NULL)
1412 *count = domain->ops->domain_get_windows(domain);
1413 else
1414 ret = -ENODEV;
1415
0ff64f80
JR
1416 break;
1417 default:
1418 if (!domain->ops->domain_get_attr)
1419 return -EINVAL;
0cd76dd1 1420
0ff64f80
JR
1421 ret = domain->ops->domain_get_attr(domain, attr, data);
1422 }
1423
1424 return ret;
0cd76dd1
JR
1425}
1426EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1427
1428int iommu_domain_set_attr(struct iommu_domain *domain,
1429 enum iommu_attr attr, void *data)
1430{
69356712
JR
1431 int ret = 0;
1432 u32 *count;
1433
1434 switch (attr) {
1435 case DOMAIN_ATTR_WINDOWS:
1436 count = data;
1437
1438 if (domain->ops->domain_set_windows != NULL)
1439 ret = domain->ops->domain_set_windows(domain, *count);
1440 else
1441 ret = -ENODEV;
1460432c 1442
69356712
JR
1443 break;
1444 default:
1445 if (domain->ops->domain_set_attr == NULL)
1446 return -EINVAL;
1447
1448 ret = domain->ops->domain_set_attr(domain, attr, data);
1449 }
1450
1451 return ret;
1460432c 1452}
0cd76dd1 1453EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
This page took 0.459818 seconds and 5 git commands to generate.