blob: d69e0ca77f82e2773efe72a7aac7ff5c85b358a8 [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
Joerg Roedel19762d72015-05-28 18:41:26 +0200753 return ops->add_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400754}
755
Joerg Roedel8da30142015-05-28 18:41:27 +0200756static int remove_iommu_group(struct device *dev, void *data)
757{
758 struct iommu_callback_data *cb = data;
759 const struct iommu_ops *ops = cb->ops;
760
761 if (ops->remove_device && dev->iommu_group)
762 ops->remove_device(dev);
763
764 return 0;
765}
766
Alex Williamsond72e31c2012-05-30 14:18:53 -0600767static int iommu_bus_notifier(struct notifier_block *nb,
768 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400769{
770 struct device *dev = data;
Thierry Redingb22f6432014-06-27 09:03:12 +0200771 const struct iommu_ops *ops = dev->bus->iommu_ops;
Alex Williamsond72e31c2012-05-30 14:18:53 -0600772 struct iommu_group *group;
773 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400774
Alex Williamsond72e31c2012-05-30 14:18:53 -0600775 /*
776 * ADD/DEL call into iommu driver ops if provided, which may
777 * result in ADD/DEL notifiers to group->notifier
778 */
779 if (action == BUS_NOTIFY_ADD_DEVICE) {
780 if (ops->add_device)
781 return ops->add_device(dev);
Joerg Roedel843cb6d2015-05-28 18:41:28 +0200782 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
Alex Williamsond72e31c2012-05-30 14:18:53 -0600783 if (ops->remove_device && dev->iommu_group) {
784 ops->remove_device(dev);
785 return 0;
786 }
787 }
Alex Williamson14604322011-10-21 15:56:05 -0400788
Alex Williamsond72e31c2012-05-30 14:18:53 -0600789 /*
790 * Remaining BUS_NOTIFYs get filtered and republished to the
791 * group, if anyone is listening
792 */
793 group = iommu_group_get(dev);
794 if (!group)
795 return 0;
796
797 switch (action) {
798 case BUS_NOTIFY_BIND_DRIVER:
799 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
800 break;
801 case BUS_NOTIFY_BOUND_DRIVER:
802 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
803 break;
804 case BUS_NOTIFY_UNBIND_DRIVER:
805 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
806 break;
807 case BUS_NOTIFY_UNBOUND_DRIVER:
808 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
809 break;
810 }
811
812 if (group_action)
813 blocking_notifier_call_chain(&group->notifier,
814 group_action, dev);
815
816 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400817 return 0;
818}
819
Mark Salterfb3e3062014-09-21 13:58:24 -0400820static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100821{
Mark Salterfb3e3062014-09-21 13:58:24 -0400822 int err;
823 struct notifier_block *nb;
Thierry Redingb22f6432014-06-27 09:03:12 +0200824 struct iommu_callback_data cb = {
825 .ops = ops,
826 };
827
Mark Salterfb3e3062014-09-21 13:58:24 -0400828 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
829 if (!nb)
830 return -ENOMEM;
831
832 nb->notifier_call = iommu_bus_notifier;
833
834 err = bus_register_notifier(bus, nb);
Joerg Roedel8da30142015-05-28 18:41:27 +0200835 if (err)
836 goto out_free;
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100837
838 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
Joerg Roedel8da30142015-05-28 18:41:27 +0200839 if (err)
840 goto out_err;
841
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100842
843 return 0;
Joerg Roedel8da30142015-05-28 18:41:27 +0200844
845out_err:
846 /* Clean up */
847 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
848 bus_unregister_notifier(bus, nb);
849
850out_free:
851 kfree(nb);
852
853 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100854}
855
Joerg Roedelff217762011-08-26 16:48:26 +0200856/**
857 * bus_set_iommu - set iommu-callbacks for the bus
858 * @bus: bus.
859 * @ops: the callbacks provided by the iommu-driver
860 *
861 * This function is called by an iommu driver to set the iommu methods
862 * used for a particular bus. Drivers for devices on that bus can use
863 * the iommu-api after these ops are registered.
864 * This special function is needed because IOMMUs are usually devices on
865 * the bus itself, so the iommu drivers are not initialized when the bus
866 * is set up. With this function the iommu-driver can set the iommu-ops
867 * afterwards.
868 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200869int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100870{
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100871 int err;
872
Joerg Roedelff217762011-08-26 16:48:26 +0200873 if (bus->iommu_ops != NULL)
874 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100875
Joerg Roedelff217762011-08-26 16:48:26 +0200876 bus->iommu_ops = ops;
877
878 /* Do IOMMU specific setup for this bus-type */
Heiko Stübnerd7da6bd2014-10-29 01:22:56 +0100879 err = iommu_bus_init(bus, ops);
880 if (err)
881 bus->iommu_ops = NULL;
882
883 return err;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100884}
Joerg Roedelff217762011-08-26 16:48:26 +0200885EXPORT_SYMBOL_GPL(bus_set_iommu);
886
Joerg Roedela1b60c12011-09-06 18:46:34 +0200887bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100888{
Joerg Roedel94441c32011-09-06 18:58:54 +0200889 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100890}
Joerg Roedela1b60c12011-09-06 18:46:34 +0200891EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100892
Joerg Roedel3c0e0ca2014-09-03 18:47:25 +0200893bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
894{
895 if (!bus->iommu_ops || !bus->iommu_ops->capable)
896 return false;
897
898 return bus->iommu_ops->capable(cap);
899}
900EXPORT_SYMBOL_GPL(iommu_capable);
901
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400902/**
903 * iommu_set_fault_handler() - set a fault handler for an iommu domain
904 * @domain: iommu domain
905 * @handler: fault handler
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300906 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400907 *
908 * This function should be used by IOMMU users which want to be notified
909 * whenever an IOMMU fault happens.
910 *
911 * The fault handler itself should return 0 on success, and an appropriate
912 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400913 */
914void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300915 iommu_fault_handler_t handler,
916 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400917{
918 BUG_ON(!domain);
919
920 domain->handler = handler;
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300921 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400922}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -0400923EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400924
Joerg Roedel905d66c2011-09-06 16:03:26 +0200925struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100926{
927 struct iommu_domain *domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100928
Joerg Roedel94441c32011-09-06 18:58:54 +0200929 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +0200930 return NULL;
931
Joerg Roedel89be34a2015-03-26 13:43:19 +0100932 domain = bus->iommu_ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100933 if (!domain)
934 return NULL;
935
Joerg Roedel8539c7c2015-03-26 13:43:05 +0100936 domain->ops = bus->iommu_ops;
937 domain->type = IOMMU_DOMAIN_UNMANAGED;
Joerg Roedel905d66c2011-09-06 16:03:26 +0200938
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100939 return domain;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100940}
941EXPORT_SYMBOL_GPL(iommu_domain_alloc);
942
943void iommu_domain_free(struct iommu_domain *domain)
944{
Joerg Roedel89be34a2015-03-26 13:43:19 +0100945 domain->ops->domain_free(domain);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100946}
947EXPORT_SYMBOL_GPL(iommu_domain_free);
948
949int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
950{
Shuah Khanb54db772013-08-15 11:59:26 -0600951 int ret;
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200952 if (unlikely(domain->ops->attach_dev == NULL))
953 return -ENODEV;
954
Shuah Khanb54db772013-08-15 11:59:26 -0600955 ret = domain->ops->attach_dev(domain, dev);
956 if (!ret)
957 trace_attach_device_to_domain(dev);
958 return ret;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100959}
960EXPORT_SYMBOL_GPL(iommu_attach_device);
961
962void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
963{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200964 if (unlikely(domain->ops->detach_dev == NULL))
965 return;
966
967 domain->ops->detach_dev(domain, dev);
Shuah Khan69980632013-08-15 11:59:27 -0600968 trace_detach_device_from_domain(dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100969}
970EXPORT_SYMBOL_GPL(iommu_detach_device);
971
Alex Williamsond72e31c2012-05-30 14:18:53 -0600972/*
973 * IOMMU groups are really the natrual working unit of the IOMMU, but
974 * the IOMMU API works on domains and devices. Bridge that gap by
975 * iterating over the devices in a group. Ideally we'd have a single
976 * device which represents the requestor ID of the group, but we also
977 * allow IOMMU drivers to create policy defined minimum sets, where
978 * the physical hardware may be able to distiguish members, but we
979 * wish to group them at a higher level (ex. untrusted multi-function
980 * PCI devices). Thus we attach each device.
981 */
982static int iommu_group_do_attach_device(struct device *dev, void *data)
983{
984 struct iommu_domain *domain = data;
985
986 return iommu_attach_device(domain, dev);
987}
988
989int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
990{
991 return iommu_group_for_each_dev(group, domain,
992 iommu_group_do_attach_device);
993}
994EXPORT_SYMBOL_GPL(iommu_attach_group);
995
996static int iommu_group_do_detach_device(struct device *dev, void *data)
997{
998 struct iommu_domain *domain = data;
999
1000 iommu_detach_device(domain, dev);
1001
1002 return 0;
1003}
1004
1005void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1006{
1007 iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
1008}
1009EXPORT_SYMBOL_GPL(iommu_detach_group);
1010
Varun Sethibb5547ac2013-03-29 01:23:58 +05301011phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001012{
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001013 if (unlikely(domain->ops->iova_to_phys == NULL))
1014 return 0;
1015
1016 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001017}
1018EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +08001019
Alex Williamsonbd139692013-06-17 19:57:34 -06001020static size_t iommu_pgsize(struct iommu_domain *domain,
1021 unsigned long addr_merge, size_t size)
1022{
1023 unsigned int pgsize_idx;
1024 size_t pgsize;
1025
1026 /* Max page size that still fits into 'size' */
1027 pgsize_idx = __fls(size);
1028
1029 /* need to consider alignment requirements ? */
1030 if (likely(addr_merge)) {
1031 /* Max page size allowed by address */
1032 unsigned int align_pgsize_idx = __ffs(addr_merge);
1033 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1034 }
1035
1036 /* build a mask of acceptable page sizes */
1037 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1038
1039 /* throw away page sizes not supported by the hardware */
1040 pgsize &= domain->ops->pgsize_bitmap;
1041
1042 /* make sure we're still sane */
1043 BUG_ON(!pgsize);
1044
1045 /* pick the biggest page */
1046 pgsize_idx = __fls(pgsize);
1047 pgsize = 1UL << pgsize_idx;
1048
1049 return pgsize;
1050}
1051
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001052int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001053 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001054{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001055 unsigned long orig_iova = iova;
1056 unsigned int min_pagesz;
1057 size_t orig_size = size;
1058 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001059
Joerg Roedel9db4ad92014-08-19 00:19:26 +02001060 if (unlikely(domain->ops->map == NULL ||
Joerg Roedel57886512013-01-29 13:41:09 +01001061 domain->ops->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001062 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001063
Joerg Roedela10315e2015-03-26 13:43:06 +01001064 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1065 return -EINVAL;
1066
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001067 /* find out the minimum page size supported */
1068 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001069
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001070 /*
1071 * both the virtual address and the physical one, as well as
1072 * the size of the mapping, must be aligned (at least) to the
1073 * size of the smallest page supported by the hardware
1074 */
1075 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
Fabio Estevamabedb042013-08-22 10:25:42 -03001076 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001077 iova, &paddr, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001078 return -EINVAL;
1079 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001080
Fabio Estevamabedb042013-08-22 10:25:42 -03001081 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001082
1083 while (size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001084 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001085
Fabio Estevamabedb042013-08-22 10:25:42 -03001086 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
Joe Perches6197ca82013-06-23 12:29:04 -07001087 iova, &paddr, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001088
1089 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1090 if (ret)
1091 break;
1092
1093 iova += pgsize;
1094 paddr += pgsize;
1095 size -= pgsize;
1096 }
1097
1098 /* unroll mapping in case something went wrong */
1099 if (ret)
1100 iommu_unmap(domain, orig_iova, orig_size - size);
Shuah Khane0be7c82013-08-15 11:59:28 -06001101 else
Shuah Khan860cd642015-01-15 19:29:43 -07001102 trace_map(orig_iova, paddr, orig_size);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001103
1104 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001105}
1106EXPORT_SYMBOL_GPL(iommu_map);
1107
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001108size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001109{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001110 size_t unmapped_page, unmapped = 0;
1111 unsigned int min_pagesz;
Shuah Khan6fd492f2015-01-16 16:47:19 -07001112 unsigned long orig_iova = iova;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001113
Joerg Roedel57886512013-01-29 13:41:09 +01001114 if (unlikely(domain->ops->unmap == NULL ||
1115 domain->ops->pgsize_bitmap == 0UL))
Joerg Roedele5aa7f02011-09-06 16:44:29 +02001116 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001117
Joerg Roedela10315e2015-03-26 13:43:06 +01001118 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1119 return -EINVAL;
1120
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001121 /* find out the minimum page size supported */
1122 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001123
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001124 /*
1125 * The virtual address, as well as the size of the mapping, must be
1126 * aligned (at least) to the size of the smallest page supported
1127 * by the hardware
1128 */
1129 if (!IS_ALIGNED(iova | size, min_pagesz)) {
Joe Perches6197ca82013-06-23 12:29:04 -07001130 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1131 iova, size, min_pagesz);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001132 return -EINVAL;
1133 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001134
Joe Perches6197ca82013-06-23 12:29:04 -07001135 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02001136
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001137 /*
1138 * Keep iterating until we either unmap 'size' bytes (or more)
1139 * or we hit an area that isn't mapped.
1140 */
1141 while (unmapped < size) {
Alex Williamsonbd139692013-06-17 19:57:34 -06001142 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001143
Alex Williamsonbd139692013-06-17 19:57:34 -06001144 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001145 if (!unmapped_page)
1146 break;
1147
Joe Perches6197ca82013-06-23 12:29:04 -07001148 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1149 iova, unmapped_page);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001150
1151 iova += unmapped_page;
1152 unmapped += unmapped_page;
1153 }
1154
Shuah Khandb8614d2015-01-16 20:53:17 -07001155 trace_unmap(orig_iova, size, unmapped);
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +02001156 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +01001157}
1158EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -04001159
Olav Haugan315786e2014-10-25 09:55:16 -07001160size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1161 struct scatterlist *sg, unsigned int nents, int prot)
1162{
Joerg Roedel38ec0102014-11-04 14:53:51 +01001163 struct scatterlist *s;
Olav Haugan315786e2014-10-25 09:55:16 -07001164 size_t mapped = 0;
Robin Murphy18f23402014-11-25 17:50:55 +00001165 unsigned int i, min_pagesz;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001166 int ret;
Olav Haugan315786e2014-10-25 09:55:16 -07001167
Robin Murphy18f23402014-11-25 17:50:55 +00001168 if (unlikely(domain->ops->pgsize_bitmap == 0UL))
1169 return 0;
Olav Haugan315786e2014-10-25 09:55:16 -07001170
Robin Murphy18f23402014-11-25 17:50:55 +00001171 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
1172
1173 for_each_sg(sg, s, nents, i) {
1174 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
1175
1176 /*
1177 * We are mapping on IOMMU page boundaries, so offset within
1178 * the page must be 0. However, the IOMMU may support pages
1179 * smaller than PAGE_SIZE, so s->offset may still represent
1180 * an offset of that boundary within the CPU page.
1181 */
1182 if (!IS_ALIGNED(s->offset, min_pagesz))
Joerg Roedel38ec0102014-11-04 14:53:51 +01001183 goto out_err;
1184
1185 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1186 if (ret)
1187 goto out_err;
1188
1189 mapped += s->length;
Olav Haugan315786e2014-10-25 09:55:16 -07001190 }
1191
1192 return mapped;
Joerg Roedel38ec0102014-11-04 14:53:51 +01001193
1194out_err:
1195 /* undo mappings already done */
1196 iommu_unmap(domain, iova, mapped);
1197
1198 return 0;
1199
Olav Haugan315786e2014-10-25 09:55:16 -07001200}
1201EXPORT_SYMBOL_GPL(default_iommu_map_sg);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001202
1203int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
Varun Sethi80f97f02013-03-29 01:24:00 +05301204 phys_addr_t paddr, u64 size, int prot)
Joerg Roedeld7787d52013-01-29 14:26:20 +01001205{
1206 if (unlikely(domain->ops->domain_window_enable == NULL))
1207 return -ENODEV;
1208
Varun Sethi80f97f02013-03-29 01:24:00 +05301209 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1210 prot);
Joerg Roedeld7787d52013-01-29 14:26:20 +01001211}
1212EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1213
1214void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1215{
1216 if (unlikely(domain->ops->domain_window_disable == NULL))
1217 return;
1218
1219 return domain->ops->domain_window_disable(domain, wnd_nr);
1220}
1221EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1222
Alex Williamsond72e31c2012-05-30 14:18:53 -06001223static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -04001224{
Alex Williamsond72e31c2012-05-30 14:18:53 -06001225 iommu_group_kset = kset_create_and_add("iommu_groups",
1226 NULL, kernel_kobj);
1227 ida_init(&iommu_group_ida);
1228 mutex_init(&iommu_group_mutex);
Alex Williamson14604322011-10-21 15:56:05 -04001229
Alex Williamsond72e31c2012-05-30 14:18:53 -06001230 BUG_ON(!iommu_group_kset);
1231
1232 return 0;
Alex Williamson14604322011-10-21 15:56:05 -04001233}
Alexey Kardashevskiy097e3632013-01-07 18:51:52 +11001234arch_initcall(iommu_init);
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001235
1236int iommu_domain_get_attr(struct iommu_domain *domain,
1237 enum iommu_attr attr, void *data)
1238{
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001239 struct iommu_domain_geometry *geometry;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001240 bool *paging;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001241 int ret = 0;
Joerg Roedel69356712013-02-04 14:00:01 +01001242 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001243
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001244 switch (attr) {
1245 case DOMAIN_ATTR_GEOMETRY:
1246 geometry = data;
1247 *geometry = domain->geometry;
1248
1249 break;
Joerg Roedeld2e12162013-01-29 13:49:04 +01001250 case DOMAIN_ATTR_PAGING:
1251 paging = data;
1252 *paging = (domain->ops->pgsize_bitmap != 0UL);
1253 break;
Joerg Roedel69356712013-02-04 14:00:01 +01001254 case DOMAIN_ATTR_WINDOWS:
1255 count = data;
1256
1257 if (domain->ops->domain_get_windows != NULL)
1258 *count = domain->ops->domain_get_windows(domain);
1259 else
1260 ret = -ENODEV;
1261
1262 break;
Joerg Roedel0ff64f82012-01-26 19:40:53 +01001263 default:
1264 if (!domain->ops->domain_get_attr)
1265 return -EINVAL;
1266
1267 ret = domain->ops->domain_get_attr(domain, attr, data);
1268 }
1269
1270 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001271}
1272EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1273
1274int iommu_domain_set_attr(struct iommu_domain *domain,
1275 enum iommu_attr attr, void *data)
1276{
Joerg Roedel69356712013-02-04 14:00:01 +01001277 int ret = 0;
1278 u32 *count;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001279
Joerg Roedel69356712013-02-04 14:00:01 +01001280 switch (attr) {
1281 case DOMAIN_ATTR_WINDOWS:
1282 count = data;
1283
1284 if (domain->ops->domain_set_windows != NULL)
1285 ret = domain->ops->domain_set_windows(domain, *count);
1286 else
1287 ret = -ENODEV;
1288
1289 break;
1290 default:
1291 if (domain->ops->domain_set_attr == NULL)
1292 return -EINVAL;
1293
1294 ret = domain->ops->domain_set_attr(domain, attr, data);
1295 }
1296
1297 return ret;
Joerg Roedel0cd76dd2012-01-26 19:40:52 +01001298}
1299EXPORT_SYMBOL_GPL(iommu_domain_set_attr);