blob: 755e4889046a7a5884b7171595ac320d2c0adcff [file] [log] [blame]
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelfc2100e2008-11-26 17:21:24 +01004 *
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
Joerg Roedel92e70662015-05-28 18:41:24 +020019#define pr_fmt(fmt) "iommu: " fmt
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020020
Joerg Roedel905d66c2011-09-06 16:03:26 +020021#include <linux/device.h>
Ohad Ben-Cohen40998182011-09-02 13:32:32 -040022#include <linux/kernel.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010023#include <linux/bug.h>
24#include <linux/types.h>
Andrew Morton60db4022009-05-06 16:03:07 -070025#include <linux/module.h>
26#include <linux/slab.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010027#include <linux/errno.h>
28#include <linux/iommu.h>
Alex Williamsond72e31c2012-05-30 14:18:53 -060029#include <linux/idr.h>
30#include <linux/notifier.h>
31#include <linux/err.h>
Alex Williamson104a1c12014-07-03 09:51:18 -060032#include <linux/pci.h>
Alex Williamsonf096c062014-09-19 10:03:06 -060033#include <linux/bitops.h>
Shuah Khan7f6db172013-08-15 11:59:23 -060034#include <trace/events/iommu.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010035
Alex Williamsond72e31c2012-05-30 14:18:53 -060036static struct kset *iommu_group_kset;
37static struct ida iommu_group_ida;
38static struct mutex iommu_group_mutex;
39
Thierry Redingb22f6432014-06-27 09:03:12 +020040struct iommu_callback_data {
41 const struct iommu_ops *ops;
42};
43
Alex Williamsond72e31c2012-05-30 14:18:53 -060044struct 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};
55
56struct iommu_device {
57 struct list_head list;
58 struct device *dev;
59 char *name;
60};
61
62struct iommu_group_attribute {
63 struct attribute attr;
64 ssize_t (*show)(struct iommu_group *group, char *buf);
65 ssize_t (*store)(struct iommu_group *group,
66 const char *buf, size_t count);
67};
68
69#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
70struct iommu_group_attribute iommu_group_attr_##_name = \
71 __ATTR(_name, _mode, _show, _store)
72
73#define to_iommu_group_attr(_attr) \
74 container_of(_attr, struct iommu_group_attribute, attr)
75#define to_iommu_group(_kobj) \
76 container_of(_kobj, struct iommu_group, kobj)
77
78static ssize_t iommu_group_attr_show(struct kobject *kobj,
79 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -040080{
Alex Williamsond72e31c2012-05-30 14:18:53 -060081 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
82 struct iommu_group *group = to_iommu_group(kobj);
83 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -040084
Alex Williamsond72e31c2012-05-30 14:18:53 -060085 if (attr->show)
86 ret = attr->show(group, buf);
87 return ret;
Alex Williamson14604322011-10-21 15:56:05 -040088}
Alex Williamsond72e31c2012-05-30 14:18:53 -060089
90static ssize_t iommu_group_attr_store(struct kobject *kobj,
91 struct attribute *__attr,
92 const char *buf, size_t count)
93{
94 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
95 struct iommu_group *group = to_iommu_group(kobj);
96 ssize_t ret = -EIO;
97
98 if (attr->store)
99 ret = attr->store(group, buf, count);
100 return ret;
101}
102
103static const struct sysfs_ops iommu_group_sysfs_ops = {
104 .show = iommu_group_attr_show,
105 .store = iommu_group_attr_store,
106};
107
108static int iommu_group_create_file(struct iommu_group *group,
109 struct iommu_group_attribute *attr)
110{
111 return sysfs_create_file(&group->kobj, &attr->attr);
112}
113
114static void iommu_group_remove_file(struct iommu_group *group,
115 struct iommu_group_attribute *attr)
116{
117 sysfs_remove_file(&group->kobj, &attr->attr);
118}
119
120static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
121{
122 return sprintf(buf, "%s\n", group->name);
123}
124
125static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
126
127static void iommu_group_release(struct kobject *kobj)
128{
129 struct iommu_group *group = to_iommu_group(kobj);
130
Joerg Roedel269aa802015-05-28 18:41:25 +0200131 pr_debug("Releasing group %d\n", group->id);
132
Alex Williamsond72e31c2012-05-30 14:18:53 -0600133 if (group->iommu_data_release)
134 group->iommu_data_release(group->iommu_data);
135
136 mutex_lock(&iommu_group_mutex);
137 ida_remove(&iommu_group_ida, group->id);
138 mutex_unlock(&iommu_group_mutex);
139
140 kfree(group->name);
141 kfree(group);
142}
143
144static struct kobj_type iommu_group_ktype = {
145 .sysfs_ops = &iommu_group_sysfs_ops,
146 .release = iommu_group_release,
147};
148
149/**
150 * iommu_group_alloc - Allocate a new group
151 * @name: Optional name to associate with group, visible in sysfs
152 *
153 * This function is called by an iommu driver to allocate a new iommu
154 * group. The iommu group represents the minimum granularity of the iommu.
155 * Upon successful return, the caller holds a reference to the supplied
156 * group in order to hold the group until devices are added. Use
157 * iommu_group_put() to release this extra reference count, allowing the
158 * group to be automatically reclaimed once it has no devices or external
159 * references.
160 */
161struct iommu_group *iommu_group_alloc(void)
162{
163 struct iommu_group *group;
164 int ret;
165
166 group = kzalloc(sizeof(*group), GFP_KERNEL);
167 if (!group)
168 return ERR_PTR(-ENOMEM);
169
170 group->kobj.kset = iommu_group_kset;
171 mutex_init(&group->mutex);
172 INIT_LIST_HEAD(&group->devices);
173 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
174
175 mutex_lock(&iommu_group_mutex);
176
177again:
178 if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) {
179 kfree(group);
180 mutex_unlock(&iommu_group_mutex);
181 return ERR_PTR(-ENOMEM);
182 }
183
184 if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id))
185 goto again;
186
187 mutex_unlock(&iommu_group_mutex);
188
189 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
190 NULL, "%d", group->id);
191 if (ret) {
192 mutex_lock(&iommu_group_mutex);
193 ida_remove(&iommu_group_ida, group->id);
194 mutex_unlock(&iommu_group_mutex);
195 kfree(group);
196 return ERR_PTR(ret);
197 }
198
199 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
200 if (!group->devices_kobj) {
201 kobject_put(&group->kobj); /* triggers .release & free */
202 return ERR_PTR(-ENOMEM);
203 }
204
205 /*
206 * The devices_kobj holds a reference on the group kobject, so
207 * as long as that exists so will the group. We can therefore
208 * use the devices_kobj for reference counting.
209 */
210 kobject_put(&group->kobj);
211
Joerg Roedel269aa802015-05-28 18:41:25 +0200212 pr_debug("Allocated group %d\n", group->id);
213
Alex Williamsond72e31c2012-05-30 14:18:53 -0600214 return group;
215}
216EXPORT_SYMBOL_GPL(iommu_group_alloc);
217
Alexey Kardashevskiyaa16bea2013-03-25 10:23:49 +1100218struct iommu_group *iommu_group_get_by_id(int id)
219{
220 struct kobject *group_kobj;
221 struct iommu_group *group;
222 const char *name;
223
224 if (!iommu_group_kset)
225 return NULL;
226
227 name = kasprintf(GFP_KERNEL, "%d", id);
228 if (!name)
229 return NULL;
230
231 group_kobj = kset_find_obj(iommu_group_kset, name);
232 kfree(name);
233
234 if (!group_kobj)
235 return NULL;
236
237 group = container_of(group_kobj, struct iommu_group, kobj);
238 BUG_ON(group->id != id);
239
240 kobject_get(group->devices_kobj);
241 kobject_put(&group->kobj);
242
243 return group;
244}
245EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
246
Alex Williamsond72e31c2012-05-30 14:18:53 -0600247/**
248 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
249 * @group: the group
250 *
251 * iommu drivers can store data in the group for use when doing iommu
252 * operations. This function provides a way to retrieve it. Caller
253 * should hold a group reference.
254 */
255void *iommu_group_get_iommudata(struct iommu_group *group)
256{
257 return group->iommu_data;
258}
259EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
260
261/**
262 * iommu_group_set_iommudata - set iommu_data for a group
263 * @group: the group
264 * @iommu_data: new data
265 * @release: release function for iommu_data
266 *
267 * iommu drivers can store data in the group for use when doing iommu
268 * operations. This function provides a way to set the data after
269 * the group has been allocated. Caller should hold a group reference.
270 */
271void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
272 void (*release)(void *iommu_data))
273{
274 group->iommu_data = iommu_data;
275 group->iommu_data_release = release;
276}
277EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
278
279/**
280 * iommu_group_set_name - set name for a group
281 * @group: the group
282 * @name: name
283 *
284 * Allow iommu driver to set a name for a group. When set it will
285 * appear in a name attribute file under the group in sysfs.
286 */
287int iommu_group_set_name(struct iommu_group *group, const char *name)
288{
289 int ret;
290
291 if (group->name) {
292 iommu_group_remove_file(group, &iommu_group_attr_name);
293 kfree(group->name);
294 group->name = NULL;
295 if (!name)
296 return 0;
297 }
298
299 group->name = kstrdup(name, GFP_KERNEL);
300 if (!group->name)
301 return -ENOMEM;
302
303 ret = iommu_group_create_file(group, &iommu_group_attr_name);
304 if (ret) {
305 kfree(group->name);
306 group->name = NULL;
307 return ret;
308 }
309
310 return 0;
311}
312EXPORT_SYMBOL_GPL(iommu_group_set_name);
313
314/**
315 * iommu_group_add_device - add a device to an iommu group
316 * @group: the group into which to add the device (reference should be held)
317 * @dev: the device
318 *
319 * This function is called by an iommu driver to add a device into a
320 * group. Adding a device increments the group reference count.
321 */
322int iommu_group_add_device(struct iommu_group *group, struct device *dev)
323{
324 int ret, i = 0;
325 struct iommu_device *device;
326
327 device = kzalloc(sizeof(*device), GFP_KERNEL);
328 if (!device)
329 return -ENOMEM;
330
331 device->dev = dev;
332
333 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
334 if (ret) {
335 kfree(device);
336 return ret;
337 }
338
339 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
340rename:
341 if (!device->name) {
342 sysfs_remove_link(&dev->kobj, "iommu_group");
343 kfree(device);
344 return -ENOMEM;
345 }
346
347 ret = sysfs_create_link_nowarn(group->devices_kobj,
348 &dev->kobj, device->name);
349 if (ret) {
350 kfree(device->name);
351 if (ret == -EEXIST && i >= 0) {
352 /*
353 * Account for the slim chance of collision
354 * and append an instance to the name.
355 */
356 device->name = kasprintf(GFP_KERNEL, "%s.%d",
357 kobject_name(&dev->kobj), i++);
358 goto rename;
359 }
360
361 sysfs_remove_link(&dev->kobj, "iommu_group");
362 kfree(device);
363 return ret;
364 }
365
366 kobject_get(group->devices_kobj);
367
368 dev->iommu_group = group;
369
370 mutex_lock(&group->mutex);
371 list_add_tail(&device->list, &group->devices);
372 mutex_unlock(&group->mutex);
373
374 /* Notify any listeners about change to group. */
375 blocking_notifier_call_chain(&group->notifier,
376 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
Shuah Khand1cf7e82013-08-15 11:59:24 -0600377
378 trace_add_device_to_group(group->id, dev);
Joerg Roedel269aa802015-05-28 18:41:25 +0200379
380 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
381
Alex Williamsond72e31c2012-05-30 14:18:53 -0600382 return 0;
383}
384EXPORT_SYMBOL_GPL(iommu_group_add_device);
385
386/**
387 * iommu_group_remove_device - remove a device from it's current group
388 * @dev: device to be removed
389 *
390 * This function is called by an iommu driver to remove the device from
391 * it's current group. This decrements the iommu group reference count.
392 */
393void iommu_group_remove_device(struct device *dev)
394{
395 struct iommu_group *group = dev->iommu_group;
396 struct iommu_device *tmp_device, *device = NULL;
397
Joerg Roedel269aa802015-05-28 18:41:25 +0200398 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
399
Alex Williamsond72e31c2012-05-30 14:18:53 -0600400 /* Pre-notify listeners that a device is being removed. */
401 blocking_notifier_call_chain(&group->notifier,
402 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
403
404 mutex_lock(&group->mutex);
405 list_for_each_entry(tmp_device, &group->devices, list) {
406 if (tmp_device->dev == dev) {
407 device = tmp_device;
408 list_del(&device->list);
409 break;
410 }
411 }
412 mutex_unlock(&group->mutex);
413
414 if (!device)
415 return;
416
417 sysfs_remove_link(group->devices_kobj, device->name);
418 sysfs_remove_link(&dev->kobj, "iommu_group");
419
Shuah Khan2e757082013-08-15 11:59:25 -0600420 trace_remove_device_from_group(group->id, dev);
421
Alex Williamsond72e31c2012-05-30 14:18:53 -0600422 kfree(device->name);
423 kfree(device);
424 dev->iommu_group = NULL;
425 kobject_put(group->devices_kobj);
426}
427EXPORT_SYMBOL_GPL(iommu_group_remove_device);
428
429/**
430 * iommu_group_for_each_dev - iterate over each device in the group
431 * @group: the group
432 * @data: caller opaque data to be passed to callback function
433 * @fn: caller supplied callback function
434 *
435 * This function is called by group users to iterate over group devices.
436 * Callers should hold a reference count to the group during callback.
437 * The group->mutex is held across callbacks, which will block calls to
438 * iommu_group_add/remove_device.
439 */
440int iommu_group_for_each_dev(struct iommu_group *group, void *data,
441 int (*fn)(struct device *, void *))
442{
443 struct iommu_device *device;
444 int ret = 0;
445
446 mutex_lock(&group->mutex);
447 list_for_each_entry(device, &group->devices, list) {
448 ret = fn(device->dev, data);
449 if (ret)
450 break;
451 }
452 mutex_unlock(&group->mutex);
453 return ret;
454}
455EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
456
457/**
458 * iommu_group_get - Return the group for a device and increment reference
459 * @dev: get the group that this device belongs to
460 *
461 * This function is called by iommu drivers and users to get the group
462 * for the specified device. If found, the group is returned and the group
463 * reference in incremented, else NULL.
464 */
465struct iommu_group *iommu_group_get(struct device *dev)
466{
467 struct iommu_group *group = dev->iommu_group;
468
469 if (group)
470 kobject_get(group->devices_kobj);
471
472 return group;
473}
474EXPORT_SYMBOL_GPL(iommu_group_get);
475
476/**
477 * iommu_group_put - Decrement group reference
478 * @group: the group to use
479 *
480 * This function is called by iommu drivers and users to release the
481 * iommu group. Once the reference count is zero, the group is released.
482 */
483void iommu_group_put(struct iommu_group *group)
484{
485 if (group)
486 kobject_put(group->devices_kobj);
487}
488EXPORT_SYMBOL_GPL(iommu_group_put);
489
490/**
491 * iommu_group_register_notifier - Register a notifier for group changes
492 * @group: the group to watch
493 * @nb: notifier block to signal
494 *
495 * This function allows iommu group users to track changes in a group.
496 * See include/linux/iommu.h for actions sent via this notifier. Caller
497 * should hold a reference to the group throughout notifier registration.
498 */
499int iommu_group_register_notifier(struct iommu_group *group,
500 struct notifier_block *nb)
501{
502 return blocking_notifier_chain_register(&group->notifier, nb);
503}
504EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
505
506/**
507 * iommu_group_unregister_notifier - Unregister a notifier
508 * @group: the group to watch
509 * @nb: notifier block to signal
510 *
511 * Unregister a previously registered group notifier block.
512 */
513int iommu_group_unregister_notifier(struct iommu_group *group,
514 struct notifier_block *nb)
515{
516 return blocking_notifier_chain_unregister(&group->notifier, nb);
517}
518EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
519
520/**
521 * iommu_group_id - Return ID for a group
522 * @group: the group to ID
523 *
524 * Return the unique ID for the group matching the sysfs group number.
525 */
526int iommu_group_id(struct iommu_group *group)
527{
528 return group->id;
529}
530EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400531
Alex Williamsonf096c062014-09-19 10:03:06 -0600532static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
533 unsigned long *devfns);
534
Alex Williamson104a1c12014-07-03 09:51:18 -0600535/*
536 * To consider a PCI device isolated, we require ACS to support Source
537 * Validation, Request Redirection, Completer Redirection, and Upstream
538 * Forwarding. This effectively means that devices cannot spoof their
539 * requester ID, requests and completions cannot be redirected, and all
540 * transactions are forwarded upstream, even as it passes through a
541 * bridge where the target device is downstream.
542 */
543#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
544
Alex Williamsonf096c062014-09-19 10:03:06 -0600545/*
546 * For multifunction devices which are not isolated from each other, find
547 * all the other non-isolated functions and look for existing groups. For
548 * each function, we also need to look for aliases to or from other devices
549 * that may already have a group.
550 */
551static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
552 unsigned long *devfns)
553{
554 struct pci_dev *tmp = NULL;
555 struct iommu_group *group;
556
557 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
558 return NULL;
559
560 for_each_pci_dev(tmp) {
561 if (tmp == pdev || tmp->bus != pdev->bus ||
562 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
563 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
564 continue;
565
566 group = get_pci_alias_group(tmp, devfns);
567 if (group) {
568 pci_dev_put(tmp);
569 return group;
570 }
571 }
572
573 return NULL;
574}
575
576/*
577 * Look for aliases to or from the given device for exisiting groups. The
578 * dma_alias_devfn only supports aliases on the same bus, therefore the search
579 * space is quite small (especially since we're really only looking at pcie
580 * device, and therefore only expect multiple slots on the root complex or
581 * downstream switch ports). It's conceivable though that a pair of
582 * multifunction devices could have aliases between them that would cause a
583 * loop. To prevent this, we use a bitmap to track where we've been.
584 */
585static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
586 unsigned long *devfns)
587{
588 struct pci_dev *tmp = NULL;
589 struct iommu_group *group;
590
591 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
592 return NULL;
593
594 group = iommu_group_get(&pdev->dev);
595 if (group)
596 return group;
597
598 for_each_pci_dev(tmp) {
599 if (tmp == pdev || tmp->bus != pdev->bus)
600 continue;
601
602 /* We alias them or they alias us */
603 if (((pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
604 pdev->dma_alias_devfn == tmp->devfn) ||
605 ((tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
606 tmp->dma_alias_devfn == pdev->devfn)) {
607
608 group = get_pci_alias_group(tmp, devfns);
609 if (group) {
610 pci_dev_put(tmp);
611 return group;
612 }
613
614 group = get_pci_function_alias_group(tmp, devfns);
615 if (group) {
616 pci_dev_put(tmp);
617 return group;
618 }
619 }
620 }
621
622 return NULL;
623}
624
Alex Williamson104a1c12014-07-03 09:51:18 -0600625struct group_for_pci_data {
626 struct pci_dev *pdev;
627 struct iommu_group *group;
628};
629
630/*
631 * DMA alias iterator callback, return the last seen device. Stop and return
632 * the IOMMU group if we find one along the way.
633 */
634static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
635{
636 struct group_for_pci_data *data = opaque;
637
638 data->pdev = pdev;
639 data->group = iommu_group_get(&pdev->dev);
640
641 return data->group != NULL;
642}
643
644/*
645 * Use standard PCI bus topology, isolation features, and DMA alias quirks
646 * to find or create an IOMMU group for a device.
647 */
648static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
649{
650 struct group_for_pci_data data;
651 struct pci_bus *bus;
652 struct iommu_group *group = NULL;
Alex Williamsonf096c062014-09-19 10:03:06 -0600653 u64 devfns[4] = { 0 };
Alex Williamson104a1c12014-07-03 09:51:18 -0600654
655 /*
656 * Find the upstream DMA alias for the device. A device must not
657 * be aliased due to topology in order to have its own IOMMU group.
658 * If we find an alias along the way that already belongs to a
659 * group, use it.
660 */
661 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
662 return data.group;
663
664 pdev = data.pdev;
665
666 /*
667 * Continue upstream from the point of minimum IOMMU granularity
668 * due to aliases to the point where devices are protected from
669 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
670 * group, use it.
671 */
672 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
673 if (!bus->self)
674 continue;
675
676 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
677 break;
678
679 pdev = bus->self;
680
681 group = iommu_group_get(&pdev->dev);
682 if (group)
683 return group;
684 }
685
686 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600687 * Look for existing groups on device aliases. If we alias another
688 * device or another device aliases us, use the same group.
Alex Williamson104a1c12014-07-03 09:51:18 -0600689 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600690 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
691 if (group)
692 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600693
694 /*
Alex Williamsonf096c062014-09-19 10:03:06 -0600695 * Look for existing groups on non-isolated functions on the same
696 * slot and aliases of those funcions, if any. No need to clear
697 * the search bitmap, the tested devfns are still valid.
Alex Williamson104a1c12014-07-03 09:51:18 -0600698 */
Alex Williamsonf096c062014-09-19 10:03:06 -0600699 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
700 if (group)
701 return group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600702
703 /* No shared group found, allocate new */
704 return iommu_group_alloc();
705}
706
707/**
708 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
709 * @dev: target device
710 *
711 * This function is intended to be called by IOMMU drivers and extended to
712 * support common, bus-defined algorithms when determining or creating the
713 * IOMMU group for a device. On success, the caller will hold a reference
714 * to the returned IOMMU group, which will already include the provided
715 * device. The reference should be released with iommu_group_put().
716 */
717struct iommu_group *iommu_group_get_for_dev(struct device *dev)
718{
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200719 struct iommu_group *group;
Alex Williamson104a1c12014-07-03 09:51:18 -0600720 int ret;
721
722 group = iommu_group_get(dev);
723 if (group)
724 return group;
725
Joerg Roedelc4a783b2014-08-21 22:32:08 +0200726 if (!dev_is_pci(dev))
727 return ERR_PTR(-EINVAL);
728
729 group = iommu_group_get_for_pci_dev(to_pci_dev(dev));
Alex Williamson104a1c12014-07-03 09:51:18 -0600730
731 if (IS_ERR(group))
732 return group;
733
734 ret = iommu_group_add_device(group, dev);
735 if (ret) {
736 iommu_group_put(group);
737 return ERR_PTR(ret);
738 }
739
740 return group;
741}
742
Alex Williamson14604322011-10-21 15:56:05 -0400743static int add_iommu_group(struct device *dev, void *data)
744{
Thierry Redingb22f6432014-06-27 09:03:12 +0200745 struct iommu_callback_data *cb = data;
746 const struct iommu_ops *ops = cb->ops;
Alex Williamson14604322011-10-21 15:56:05 -0400747
Alex Williamsond72e31c2012-05-30 14:18:53 -0600748 if (!ops->add_device)
Marek Szyprowski461bfb3f2014-11-19 11:15:31 +0000749 return 0;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600750
751 WARN_ON(dev->iommu_group);
752
753 ops->add_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400754
755 return 0;
756}
757
Alex Williamsond72e31c2012-05-30 14:18:53 -0600758static int iommu_bus_notifier(struct notifier_block *nb,
759 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400760{
761 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +0200762 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600763 struct iommu_group *group;
764 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400765
Alex Williamsond72e31c2012-05-30 14:18:53 -0600766 /*
767 * ADD/DEL call into iommu driver ops if provided, which may
768 * result in ADD/DEL notifiers to group->notifier
769 */
770 if (action == BUS_NOTIFY_ADD_DEVICE) {
771 if (ops->add_device)
772 return ops->add_device(dev);
773 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
774 if (ops->remove_device && dev->iommu_group) {
775 ops->remove_device(dev);
776 return 0;
777 }
778 }
Alex Williamson14604322011-10-21 15:56:05 -0400779
Alex Williamsond72e31c2012-05-30 14:18:53 -0600780 /*
781 * Remaining BUS_NOTIFYs get filtered and republished to the
782 * group, if anyone is listening
783 */
784 group = iommu_group_get(dev);
785 if (!group)
786 return 0;
787
788 switch (action) {
789 case BUS_NOTIFY_BIND_DRIVER:
790 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
791 break;
792 case BUS_NOTIFY_BOUND_DRIVER:
793 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
794 break;
795 case BUS_NOTIFY_UNBIND_DRIVER:
796 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
797 break;
798 case BUS_NOTIFY_UNBOUND_DRIVER:
799 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
800 break;
801 }
802
803 if (group_action)
804 blocking_notifier_call_chain(&group->notifier,
805 group_action, dev);
806
807 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400808 return 0;
809}
810
Mark Salterfb3e3062014-09-21 13:58:24 -0400811static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100812{
Mark Salterfb3e3062014-09-21 13:58:24 -0400813 int err;
814 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +0200815 struct iommu_callback_data cb = {
816 .ops = ops,
817 };
818
Mark Salterfb3e3062014-09-21 13:58:24 -0400819 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
820 if (!nb)
821 return -ENOMEM;
822
823 nb->notifier_call = iommu_bus_notifier;
824
825 err = bus_register_notifier(bus, nb);
826 if (err) {
827 kfree(nb);
828 return err;
829 }
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100830
831 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
832 if (err) {
833 bus_unregister_notifier(bus, nb);
834 kfree(nb);
835 return err;
836 }
837
838 return 0;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100839}
840
Joerg Roedelff217762011-08-26 16:48:26 +0200841/**
842 * bus_set_iommu - set iommu-callbacks for the bus
843 * @bus: bus.
844 * @ops: the callbacks provided by the iommu-driver
845 *
846 * This function is called by an iommu driver to set the iommu methods
847 * used for a particular bus. Drivers for devices on that bus can use
848 * the iommu-api after these ops are registered.
849 * This special function is needed because IOMMUs are usually devices on
850 * the bus itself, so the iommu drivers are not initialized when the bus
851 * is set up. With this function the iommu-driver can set the iommu-ops
852 * afterwards.
853 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200854int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100855{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100856 int err;
857
Joerg Roedelff217762011-08-26 16:48:26 +0200858 if (bus->iommu_ops != NULL)
859 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100860
Joerg Roedelff217762011-08-26 16:48:26 +0200861 bus->iommu_ops = ops;
862
863 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100864 err = iommu_bus_init(bus, ops);
865 if (err)
866 bus->iommu_ops = NULL;
867
868 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100869}
Joerg Roedelff217762011-08-26 16:48:26 +0200870EXPORT_SYMBOL_GPL(bus_set_iommu);
871
Joerg Roedela1b60c12011-09-06 18:46:34 +0200872bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100873{
Joerg Roedel94441c32011-09-06 18:58:54 +0200874 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100875}
Joerg Roedela1b60c12011-09-06 18:46:34 +0200876EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100877
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200878bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
879{
880 if (!bus->iommu_ops || !bus->iommu_ops->capable)
881 return false;
882
883 return bus->iommu_ops->capable(cap);
884}
885EXPORT_SYMBOL_GPL(iommu_capable);
886
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400887/**
888 * iommu_set_fault_handler() - set a fault handler for an iommu domain
889 * @domain: iommu domain
890 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300891 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400892 *
893 * This function should be used by IOMMU users which want to be notified
894 * whenever an IOMMU fault happens.
895 *
896 * The fault handler itself should return 0 on success, and an appropriate
897 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400898 */
899void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300900 iommu_fault_handler_t handler,
901 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400902{
903 BUG_ON(!domain);
904
905 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300906 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400907}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -0400908EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400909
Joerg Roedel905d66c2011-09-06 16:03:26 +0200910struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100911{
912 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100913
Joerg Roedel94441c32011-09-06 18:58:54 +0200914 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +0200915 return NULL;
916
Joerg Roedel89be34a2015-03-26 13:43:19 +0100917 domain = bus->iommu_ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100918 if (!domain)
919 return NULL;
920
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100921 domain->ops = bus->iommu_ops;
922 domain->type = IOMMU_DOMAIN_UNMANAGED;
Joerg Roedel905d66c2011-09-06 16:03:26 +0200923
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100924 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100925}
926EXPORT_SYMBOL_GPL(iommu_domain_alloc);
927
928void iommu_domain_free(struct iommu_domain *domain)
929{
Joerg Roedel89be34a2015-03-26 13:43:19 +0100930 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100931}
932EXPORT_SYMBOL_GPL(iommu_domain_free);
933
934int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
935{
Shuah Khanb54db772013-08-15 11:59:26 -0600936 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200937 if (unlikely(domain->ops->attach_dev == NULL))
938 return -ENODEV;
939
Shuah Khanb54db772013-08-15 11:59:26 -0600940 ret = domain->ops->attach_dev(domain, dev);
941 if (!ret)
942 trace_attach_device_to_domain(dev);
943 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100944}
945EXPORT_SYMBOL_GPL(iommu_attach_device);
946
947void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
948{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200949 if (unlikely(domain->ops->detach_dev == NULL))
950 return;
951
952 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -0600953 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100954}
955EXPORT_SYMBOL_GPL(iommu_detach_device);
956
Alex Williamsond72e31c2012-05-30 14:18:53 -0600957/*
958 * IOMMU groups are really the natrual working unit of the IOMMU, but
959 * the IOMMU API works on domains and devices. Bridge that gap by
960 * iterating over the devices in a group. Ideally we'd have a single
961 * device which represents the requestor ID of the group, but we also
962 * allow IOMMU drivers to create policy defined minimum sets, where
963 * the physical hardware may be able to distiguish members, but we
964 * wish to group them at a higher level (ex. untrusted multi-function
965 * PCI devices). Thus we attach each device.
966 */
967static int iommu_group_do_attach_device(struct device *dev, void *data)
968{
969 struct iommu_domain *domain = data;
970
971 return iommu_attach_device(domain, dev);
972}
973
974int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
975{
976 return iommu_group_for_each_dev(group, domain,
977 iommu_group_do_attach_device);
978}
979EXPORT_SYMBOL_GPL(iommu_attach_group);
980
981static int iommu_group_do_detach_device(struct device *dev, void *data)
982{
983 struct iommu_domain *domain = data;
984
985 iommu_detach_device(domain, dev);
986
987 return 0;
988}
989
990void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
991{
992 iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
993}
994EXPORT_SYMBOL_GPL(iommu_detach_group);
995
Varun Sethibb5547ac2013-03-29 01:23:58 +0530996phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100997{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200998 if (unlikely(domain->ops->iova_to_phys == NULL))
999 return 0;
1000
1001 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001002}
1003EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001004
Alex Williamsonbd139692013-06-17 19:57:34 -06001005static size_t iommu_pgsize(struct iommu_domain *domain,
1006 unsigned long addr_merge, size_t size)
1007{
1008 unsigned int pgsize_idx;
1009 size_t pgsize;
1010
1011 /* Max page size that still fits into 'size' */
1012 pgsize_idx = __fls(size);
1013
1014 /* need to consider alignment requirements ? */
1015 if (likely(addr_merge)) {
1016 /* Max page size allowed by address */
1017 unsigned int align_pgsize_idx = __ffs(addr_merge);
1018 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1019 }
1020
1021 /* build a mask of acceptable page sizes */
1022 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1023
1024 /* throw away page sizes not supported by the hardware */
1025 pgsize &= domain->ops->pgsize_bitmap;
1026
1027 /* make sure we're still sane */
1028 BUG_ON(!pgsize);
1029
1030 /* pick the biggest page */
1031 pgsize_idx = __fls(pgsize);
1032 pgsize = 1UL << pgsize_idx;
1033
1034 return pgsize;
1035}
1036
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001037int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001038 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001039{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001040 unsigned long orig_iova = iova;
1041 unsigned int min_pagesz;
1042 size_t orig_size = size;
1043 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001044
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001045 if (unlikely(domain->ops->map == NULL ||
Joerg Roedel57886512013-01-29 13:41:09 +01001046 domain->ops->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001047 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001048
Joerg Roedela10315e2015-03-26 13:43:06 +01001049 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1050 return -EINVAL;
1051
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001052 /* find out the minimum page size supported */
1053 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001054
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001055 /*
1056 * both the virtual address and the physical one, as well as
1057 * the size of the mapping, must be aligned (at least) to the
1058 * size of the smallest page supported by the hardware
1059 */
1060 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001061 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001062 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001063 return -EINVAL;
1064 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001065
Fabio Estevamabedb042013-08-22 10:25:42 -03001066 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001067
1068 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001069 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001070
Fabio Estevamabedb042013-08-22 10:25:42 -03001071 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001072 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001073
1074 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1075 if (ret)
1076 break;
1077
1078 iova += pgsize;
1079 paddr += pgsize;
1080 size -= pgsize;
1081 }
1082
1083 /* unroll mapping in case something went wrong */
1084 if (ret)
1085 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001086 else
Shuah Khan860cd64d2015-01-15 19:29:43 -07001087 trace_map(orig_iova, paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001088
1089 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001090}
1091EXPORT_SYMBOL_GPL(iommu_map);
1092
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001093size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001094{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001095 size_t unmapped_page, unmapped = 0;
1096 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001097 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001098
Joerg Roedel57886512013-01-29 13:41:09 +01001099 if (unlikely(domain->ops->unmap == NULL ||
1100 domain->ops->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001101 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001102
Joerg Roedela10315e2015-03-26 13:43:06 +01001103 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1104 return -EINVAL;
1105
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001106 /* find out the minimum page size supported */
1107 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001108
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001109 /*
1110 * The virtual address, as well as the size of the mapping, must be
1111 * aligned (at least) to the size of the smallest page supported
1112 * by the hardware
1113 */
1114 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001115 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1116 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001117 return -EINVAL;
1118 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001119
Joe Perches6197ca82013-06-23 12:29:04 -07001120 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001121
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001122 /*
1123 * Keep iterating until we either unmap 'size' bytes (or more)
1124 * or we hit an area that isn't mapped.
1125 */
1126 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001127 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001128
Alex Williamsonbd139692013-06-17 19:57:34 -06001129 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001130 if (!unmapped_page)
1131 break;
1132
Joe Perches6197ca82013-06-23 12:29:04 -07001133 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1134 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001135
1136 iova += unmapped_page;
1137 unmapped += unmapped_page;
1138 }
1139
Shuah Khandb8614d2015-01-16 20:53:17 -07001140 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001141 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001142}
1143EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001144
Olav Haugan315786e2014-10-25 09:55:16 -07001145size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1146 struct scatterlist *sg, unsigned int nents, int prot)
1147{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001148 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001149 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001150 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001151 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001152
Robin Murphy18f23402014-11-25 17:50:55 +00001153 if (unlikely(domain->ops->pgsize_bitmap == 0UL))
1154 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001155
Robin Murphy18f23402014-11-25 17:50:55 +00001156 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
1157
1158 for_each_sg(sg, s, nents, i) {
1159 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
1160
1161 /*
1162 * We are mapping on IOMMU page boundaries, so offset within
1163 * the page must be 0. However, the IOMMU may support pages
1164 * smaller than PAGE_SIZE, so s->offset may still represent
1165 * an offset of that boundary within the CPU page.
1166 */
1167 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001168 goto out_err;
1169
1170 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1171 if (ret)
1172 goto out_err;
1173
1174 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001175 }
1176
1177 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001178
1179out_err:
1180 /* undo mappings already done */
1181 iommu_unmap(domain, iova, mapped);
1182
1183 return 0;
1184
Olav Haugan315786e2014-10-25 09:55:16 -07001185}
1186EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001187
1188int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301189 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001190{
1191 if (unlikely(domain->ops->domain_window_enable == NULL))
1192 return -ENODEV;
1193
Varun Sethi80f97f02013-03-29 01:24:00 +05301194 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1195 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001196}
1197EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1198
1199void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1200{
1201 if (unlikely(domain->ops->domain_window_disable == NULL))
1202 return;
1203
1204 return domain->ops->domain_window_disable(domain, wnd_nr);
1205}
1206EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1207
Alex Williamsond72e31c2012-05-30 14:18:53 -06001208static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001209{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001210 iommu_group_kset = kset_create_and_add("iommu_groups",
1211 NULL, kernel_kobj);
1212 ida_init(&iommu_group_ida);
1213 mutex_init(&iommu_group_mutex);
Alex Williamson14604322011-10-21 15:56:05 -04001214
Alex Williamsond72e31c2012-05-30 14:18:53 -06001215 BUG_ON(!iommu_group_kset);
1216
1217 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001218}
Alexey Kardashevskiy097e3632013-01-07 18:51:52 +11001219arch_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001220
1221int iommu_domain_get_attr(struct iommu_domain *domain,
1222 enum iommu_attr attr, void *data)
1223{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001224 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001225 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001226 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001227 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001228
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001229 switch (attr) {
1230 case DOMAIN_ATTR_GEOMETRY:
1231 geometry = data;
1232 *geometry = domain->geometry;
1233
1234 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001235 case DOMAIN_ATTR_PAGING:
1236 paging = data;
1237 *paging = (domain->ops->pgsize_bitmap != 0UL);
1238 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001239 case DOMAIN_ATTR_WINDOWS:
1240 count = data;
1241
1242 if (domain->ops->domain_get_windows != NULL)
1243 *count = domain->ops->domain_get_windows(domain);
1244 else
1245 ret = -ENODEV;
1246
1247 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001248 default:
1249 if (!domain->ops->domain_get_attr)
1250 return -EINVAL;
1251
1252 ret = domain->ops->domain_get_attr(domain, attr, data);
1253 }
1254
1255 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001256}
1257EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1258
1259int iommu_domain_set_attr(struct iommu_domain *domain,
1260 enum iommu_attr attr, void *data)
1261{
Joerg Roedel69356712013-02-04 14:00:01 +01001262 int ret = 0;
1263 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001264
Joerg Roedel69356712013-02-04 14:00:01 +01001265 switch (attr) {
1266 case DOMAIN_ATTR_WINDOWS:
1267 count = data;
1268
1269 if (domain->ops->domain_set_windows != NULL)
1270 ret = domain->ops->domain_set_windows(domain, *count);
1271 else
1272 ret = -ENODEV;
1273
1274 break;
1275 default:
1276 if (domain->ops->domain_set_attr == NULL)
1277 return -EINVAL;
1278
1279 ret = domain->ops->domain_set_attr(domain, attr, data);
1280 }
1281
1282 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001283}
1284EXPORT_SYMBOL_GPL(iommu_domain_set_attr);