blob: a6eae7e41279224b97d14c22e0266732b6218997 [file] [log] [blame]
Joerg Roedelfc2100e2008-11-26 17:21:24 +01001/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +020019#define pr_fmt(fmt) "%s: " fmt, __func__
20
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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070029#include <linux/scatterlist.h>
Alex Williamson43a0ba62012-05-30 14:18:53 -060030#include <linux/idr.h>
31#include <linux/notifier.h>
32#include <linux/err.h>
Joerg Roedelfc2100e2008-11-26 17:21:24 +010033
Alex Williamson43a0ba62012-05-30 14:18:53 -060034static struct kset *iommu_group_kset;
Olav Haugan0d063372012-12-06 16:53:56 -080035static struct idr iommu_group_idr;
Alex Williamson43a0ba62012-05-30 14:18:53 -060036static struct mutex iommu_group_mutex;
37
38struct iommu_group {
39 struct kobject kobj;
40 struct kobject *devices_kobj;
41 struct list_head devices;
42 struct mutex mutex;
43 struct blocking_notifier_head notifier;
44 void *iommu_data;
45 void (*iommu_data_release)(void *iommu_data);
46 char *name;
47 int id;
48};
49
50struct iommu_device {
51 struct list_head list;
52 struct device *dev;
53 char *name;
54};
55
56struct iommu_group_attribute {
57 struct attribute attr;
58 ssize_t (*show)(struct iommu_group *group, char *buf);
59 ssize_t (*store)(struct iommu_group *group,
60 const char *buf, size_t count);
61};
62
63#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
64struct iommu_group_attribute iommu_group_attr_##_name = \
65 __ATTR(_name, _mode, _show, _store)
66
67#define to_iommu_group_attr(_attr) \
68 container_of(_attr, struct iommu_group_attribute, attr)
69#define to_iommu_group(_kobj) \
70 container_of(_kobj, struct iommu_group, kobj)
71
72static ssize_t iommu_group_attr_show(struct kobject *kobj,
73 struct attribute *__attr, char *buf)
Alex Williamson14604322011-10-21 15:56:05 -040074{
Alex Williamson43a0ba62012-05-30 14:18:53 -060075 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
76 struct iommu_group *group = to_iommu_group(kobj);
77 ssize_t ret = -EIO;
Alex Williamson14604322011-10-21 15:56:05 -040078
Alex Williamson43a0ba62012-05-30 14:18:53 -060079 if (attr->show)
80 ret = attr->show(group, buf);
81 return ret;
Alex Williamson14604322011-10-21 15:56:05 -040082}
Alex Williamson43a0ba62012-05-30 14:18:53 -060083
84static ssize_t iommu_group_attr_store(struct kobject *kobj,
85 struct attribute *__attr,
86 const char *buf, size_t count)
87{
88 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
89 struct iommu_group *group = to_iommu_group(kobj);
90 ssize_t ret = -EIO;
91
92 if (attr->store)
93 ret = attr->store(group, buf, count);
94 return ret;
95}
96
97static const struct sysfs_ops iommu_group_sysfs_ops = {
98 .show = iommu_group_attr_show,
99 .store = iommu_group_attr_store,
100};
101
102static int iommu_group_create_file(struct iommu_group *group,
103 struct iommu_group_attribute *attr)
104{
105 return sysfs_create_file(&group->kobj, &attr->attr);
106}
107
108static void iommu_group_remove_file(struct iommu_group *group,
109 struct iommu_group_attribute *attr)
110{
111 sysfs_remove_file(&group->kobj, &attr->attr);
112}
113
114static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
115{
116 return sprintf(buf, "%s\n", group->name);
117}
118
119static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
120
121static void iommu_group_release(struct kobject *kobj)
122{
123 struct iommu_group *group = to_iommu_group(kobj);
124
125 if (group->iommu_data_release)
126 group->iommu_data_release(group->iommu_data);
127
128 mutex_lock(&iommu_group_mutex);
Olav Haugan0d063372012-12-06 16:53:56 -0800129 idr_remove(&iommu_group_idr, group->id);
Alex Williamson43a0ba62012-05-30 14:18:53 -0600130 mutex_unlock(&iommu_group_mutex);
131
132 kfree(group->name);
133 kfree(group);
134}
135
136static struct kobj_type iommu_group_ktype = {
137 .sysfs_ops = &iommu_group_sysfs_ops,
138 .release = iommu_group_release,
139};
140
141/**
142 * iommu_group_alloc - Allocate a new group
143 * @name: Optional name to associate with group, visible in sysfs
144 *
145 * This function is called by an iommu driver to allocate a new iommu
146 * group. The iommu group represents the minimum granularity of the iommu.
147 * Upon successful return, the caller holds a reference to the supplied
148 * group in order to hold the group until devices are added. Use
149 * iommu_group_put() to release this extra reference count, allowing the
150 * group to be automatically reclaimed once it has no devices or external
151 * references.
152 */
153struct iommu_group *iommu_group_alloc(void)
154{
155 struct iommu_group *group;
156 int ret;
157
158 group = kzalloc(sizeof(*group), GFP_KERNEL);
159 if (!group)
160 return ERR_PTR(-ENOMEM);
161
162 group->kobj.kset = iommu_group_kset;
163 mutex_init(&group->mutex);
164 INIT_LIST_HEAD(&group->devices);
165 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
166
167 mutex_lock(&iommu_group_mutex);
168
169again:
Olav Haugan0d063372012-12-06 16:53:56 -0800170 if (unlikely(0 == idr_pre_get(&iommu_group_idr, GFP_KERNEL))) {
Alex Williamson43a0ba62012-05-30 14:18:53 -0600171 kfree(group);
172 mutex_unlock(&iommu_group_mutex);
173 return ERR_PTR(-ENOMEM);
174 }
175
Olav Haugan0d063372012-12-06 16:53:56 -0800176 ret = idr_get_new_above(&iommu_group_idr, group, 1, &group->id);
177 if (ret == -EAGAIN)
Alex Williamson43a0ba62012-05-30 14:18:53 -0600178 goto again;
Alex Williamson43a0ba62012-05-30 14:18:53 -0600179 mutex_unlock(&iommu_group_mutex);
180
Olav Haugan0d063372012-12-06 16:53:56 -0800181 if (ret == -ENOSPC) {
182 kfree(group);
183 return ERR_PTR(ret);
184 }
185
Alex Williamson43a0ba62012-05-30 14:18:53 -0600186 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
187 NULL, "%d", group->id);
188 if (ret) {
189 mutex_lock(&iommu_group_mutex);
Olav Haugan0d063372012-12-06 16:53:56 -0800190 idr_remove(&iommu_group_idr, group->id);
Alex Williamson43a0ba62012-05-30 14:18:53 -0600191 mutex_unlock(&iommu_group_mutex);
192 kfree(group);
193 return ERR_PTR(ret);
194 }
195
196 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
197 if (!group->devices_kobj) {
198 kobject_put(&group->kobj); /* triggers .release & free */
199 return ERR_PTR(-ENOMEM);
200 }
201
202 /*
203 * The devices_kobj holds a reference on the group kobject, so
204 * as long as that exists so will the group. We can therefore
205 * use the devices_kobj for reference counting.
206 */
207 kobject_put(&group->kobj);
208
209 return group;
210}
211EXPORT_SYMBOL_GPL(iommu_group_alloc);
212
213/**
214 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
215 * @group: the group
216 *
217 * iommu drivers can store data in the group for use when doing iommu
218 * operations. This function provides a way to retrieve it. Caller
219 * should hold a group reference.
220 */
221void *iommu_group_get_iommudata(struct iommu_group *group)
222{
223 return group->iommu_data;
224}
225EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
226
227/**
228 * iommu_group_set_iommudata - set iommu_data for a group
229 * @group: the group
230 * @iommu_data: new data
231 * @release: release function for iommu_data
232 *
233 * iommu drivers can store data in the group for use when doing iommu
234 * operations. This function provides a way to set the data after
235 * the group has been allocated. Caller should hold a group reference.
236 */
237void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
238 void (*release)(void *iommu_data))
239{
240 group->iommu_data = iommu_data;
241 group->iommu_data_release = release;
242}
243EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
244
245/**
246 * iommu_group_set_name - set name for a group
247 * @group: the group
248 * @name: name
249 *
250 * Allow iommu driver to set a name for a group. When set it will
251 * appear in a name attribute file under the group in sysfs.
252 */
253int iommu_group_set_name(struct iommu_group *group, const char *name)
254{
255 int ret;
256
257 if (group->name) {
258 iommu_group_remove_file(group, &iommu_group_attr_name);
259 kfree(group->name);
260 group->name = NULL;
261 if (!name)
262 return 0;
263 }
264
265 group->name = kstrdup(name, GFP_KERNEL);
266 if (!group->name)
267 return -ENOMEM;
268
269 ret = iommu_group_create_file(group, &iommu_group_attr_name);
270 if (ret) {
271 kfree(group->name);
272 group->name = NULL;
273 return ret;
274 }
275
276 return 0;
277}
278EXPORT_SYMBOL_GPL(iommu_group_set_name);
279
280/**
281 * iommu_group_add_device - add a device to an iommu group
282 * @group: the group into which to add the device (reference should be held)
283 * @dev: the device
284 *
285 * This function is called by an iommu driver to add a device into a
286 * group. Adding a device increments the group reference count.
287 */
288int iommu_group_add_device(struct iommu_group *group, struct device *dev)
289{
290 int ret, i = 0;
291 struct iommu_device *device;
292
293 device = kzalloc(sizeof(*device), GFP_KERNEL);
294 if (!device)
295 return -ENOMEM;
296
297 device->dev = dev;
298
299 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
300 if (ret) {
301 kfree(device);
302 return ret;
303 }
304
305 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
306rename:
307 if (!device->name) {
308 sysfs_remove_link(&dev->kobj, "iommu_group");
309 kfree(device);
310 return -ENOMEM;
311 }
312
313 ret = sysfs_create_link_nowarn(group->devices_kobj,
314 &dev->kobj, device->name);
315 if (ret) {
316 kfree(device->name);
317 if (ret == -EEXIST && i >= 0) {
318 /*
319 * Account for the slim chance of collision
320 * and append an instance to the name.
321 */
322 device->name = kasprintf(GFP_KERNEL, "%s.%d",
323 kobject_name(&dev->kobj), i++);
324 goto rename;
325 }
326
327 sysfs_remove_link(&dev->kobj, "iommu_group");
328 kfree(device);
329 return ret;
330 }
331
332 kobject_get(group->devices_kobj);
333
334 dev->iommu_group = group;
335
336 mutex_lock(&group->mutex);
337 list_add_tail(&device->list, &group->devices);
338 mutex_unlock(&group->mutex);
339
340 /* Notify any listeners about change to group. */
341 blocking_notifier_call_chain(&group->notifier,
342 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
343 return 0;
344}
345EXPORT_SYMBOL_GPL(iommu_group_add_device);
346
347/**
348 * iommu_group_remove_device - remove a device from it's current group
349 * @dev: device to be removed
350 *
351 * This function is called by an iommu driver to remove the device from
352 * it's current group. This decrements the iommu group reference count.
353 */
354void iommu_group_remove_device(struct device *dev)
355{
356 struct iommu_group *group = dev->iommu_group;
357 struct iommu_device *tmp_device, *device = NULL;
358
359 /* Pre-notify listeners that a device is being removed. */
360 blocking_notifier_call_chain(&group->notifier,
361 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
362
363 mutex_lock(&group->mutex);
364 list_for_each_entry(tmp_device, &group->devices, list) {
365 if (tmp_device->dev == dev) {
366 device = tmp_device;
367 list_del(&device->list);
368 break;
369 }
370 }
371 mutex_unlock(&group->mutex);
372
373 if (!device)
374 return;
375
376 sysfs_remove_link(group->devices_kobj, device->name);
377 sysfs_remove_link(&dev->kobj, "iommu_group");
378
379 kfree(device->name);
380 kfree(device);
381 dev->iommu_group = NULL;
382 kobject_put(group->devices_kobj);
383}
384EXPORT_SYMBOL_GPL(iommu_group_remove_device);
385
386/**
387 * iommu_group_for_each_dev - iterate over each device in the group
388 * @group: the group
389 * @data: caller opaque data to be passed to callback function
390 * @fn: caller supplied callback function
391 *
392 * This function is called by group users to iterate over group devices.
393 * Callers should hold a reference count to the group during callback.
394 * The group->mutex is held across callbacks, which will block calls to
395 * iommu_group_add/remove_device.
396 */
397int iommu_group_for_each_dev(struct iommu_group *group, void *data,
398 int (*fn)(struct device *, void *))
399{
400 struct iommu_device *device;
401 int ret = 0;
402
403 mutex_lock(&group->mutex);
404 list_for_each_entry(device, &group->devices, list) {
405 ret = fn(device->dev, data);
406 if (ret)
407 break;
408 }
409 mutex_unlock(&group->mutex);
410 return ret;
411}
412EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
413
414/**
415 * iommu_group_get - Return the group for a device and increment reference
416 * @dev: get the group that this device belongs to
417 *
418 * This function is called by iommu drivers and users to get the group
419 * for the specified device. If found, the group is returned and the group
420 * reference in incremented, else NULL.
421 */
422struct iommu_group *iommu_group_get(struct device *dev)
423{
424 struct iommu_group *group = dev->iommu_group;
425
426 if (group)
427 kobject_get(group->devices_kobj);
428
429 return group;
430}
431EXPORT_SYMBOL_GPL(iommu_group_get);
432
433/**
Olav Haugan0d063372012-12-06 16:53:56 -0800434 * iommu_group_find - Find and return the group based on the group name.
435 * Also increment the reference count.
436 * @name: the name of the group
437 *
438 * This function is called by iommu drivers and clients to get the group
439 * by the specified name. If found, the group is returned and the group
440 * reference is incremented, else NULL.
441 */
442struct iommu_group *iommu_group_find(const char *name)
443{
444 struct iommu_group *group;
445 int next = 0;
446
447 mutex_lock(&iommu_group_mutex);
448 while ((group = idr_get_next(&iommu_group_idr, &next))) {
449 if (group->name) {
450 if (strcmp(group->name, name) == 0)
451 break;
452 }
453 ++next;
454 }
455 mutex_unlock(&iommu_group_mutex);
456
457 if (group)
458 kobject_get(group->devices_kobj);
459
460 return group;
461}
462EXPORT_SYMBOL_GPL(iommu_group_find);
463
464/**
Alex Williamson43a0ba62012-05-30 14:18:53 -0600465 * iommu_group_put - Decrement group reference
466 * @group: the group to use
467 *
468 * This function is called by iommu drivers and users to release the
469 * iommu group. Once the reference count is zero, the group is released.
470 */
471void iommu_group_put(struct iommu_group *group)
472{
473 if (group)
474 kobject_put(group->devices_kobj);
475}
476EXPORT_SYMBOL_GPL(iommu_group_put);
477
478/**
479 * iommu_group_register_notifier - Register a notifier for group changes
480 * @group: the group to watch
481 * @nb: notifier block to signal
482 *
483 * This function allows iommu group users to track changes in a group.
484 * See include/linux/iommu.h for actions sent via this notifier. Caller
485 * should hold a reference to the group throughout notifier registration.
486 */
487int iommu_group_register_notifier(struct iommu_group *group,
488 struct notifier_block *nb)
489{
490 return blocking_notifier_chain_register(&group->notifier, nb);
491}
492EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
493
494/**
495 * iommu_group_unregister_notifier - Unregister a notifier
496 * @group: the group to watch
497 * @nb: notifier block to signal
498 *
499 * Unregister a previously registered group notifier block.
500 */
501int iommu_group_unregister_notifier(struct iommu_group *group,
502 struct notifier_block *nb)
503{
504 return blocking_notifier_chain_unregister(&group->notifier, nb);
505}
506EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
507
508/**
509 * iommu_group_id - Return ID for a group
510 * @group: the group to ID
511 *
512 * Return the unique ID for the group matching the sysfs group number.
513 */
514int iommu_group_id(struct iommu_group *group)
515{
516 return group->id;
517}
518EXPORT_SYMBOL_GPL(iommu_group_id);
Alex Williamson14604322011-10-21 15:56:05 -0400519
520static int add_iommu_group(struct device *dev, void *data)
521{
Alex Williamson43a0ba62012-05-30 14:18:53 -0600522 struct iommu_ops *ops = data;
Alex Williamson14604322011-10-21 15:56:05 -0400523
Alex Williamson43a0ba62012-05-30 14:18:53 -0600524 if (!ops->add_device)
525 return -ENODEV;
526
527 WARN_ON(dev->iommu_group);
528
529 ops->add_device(dev);
Alex Williamson14604322011-10-21 15:56:05 -0400530
531 return 0;
532}
533
Alex Williamson43a0ba62012-05-30 14:18:53 -0600534static int iommu_bus_notifier(struct notifier_block *nb,
535 unsigned long action, void *data)
Alex Williamson14604322011-10-21 15:56:05 -0400536{
537 struct device *dev = data;
Alex Williamson43a0ba62012-05-30 14:18:53 -0600538 struct iommu_ops *ops = dev->bus->iommu_ops;
539 struct iommu_group *group;
540 unsigned long group_action = 0;
Alex Williamson14604322011-10-21 15:56:05 -0400541
Alex Williamson43a0ba62012-05-30 14:18:53 -0600542 /*
543 * ADD/DEL call into iommu driver ops if provided, which may
544 * result in ADD/DEL notifiers to group->notifier
545 */
546 if (action == BUS_NOTIFY_ADD_DEVICE) {
547 if (ops->add_device)
548 return ops->add_device(dev);
549 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
550 if (ops->remove_device && dev->iommu_group) {
551 ops->remove_device(dev);
552 return 0;
553 }
554 }
Alex Williamson14604322011-10-21 15:56:05 -0400555
Alex Williamson43a0ba62012-05-30 14:18:53 -0600556 /*
557 * Remaining BUS_NOTIFYs get filtered and republished to the
558 * group, if anyone is listening
559 */
560 group = iommu_group_get(dev);
561 if (!group)
562 return 0;
563
564 switch (action) {
565 case BUS_NOTIFY_BIND_DRIVER:
566 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
567 break;
568 case BUS_NOTIFY_BOUND_DRIVER:
569 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
570 break;
571 case BUS_NOTIFY_UNBIND_DRIVER:
572 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
573 break;
574 case BUS_NOTIFY_UNBOUND_DRIVER:
575 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
576 break;
577 }
578
579 if (group_action)
580 blocking_notifier_call_chain(&group->notifier,
581 group_action, dev);
582
583 iommu_group_put(group);
Alex Williamson14604322011-10-21 15:56:05 -0400584 return 0;
585}
586
Alex Williamson43a0ba62012-05-30 14:18:53 -0600587static struct notifier_block iommu_bus_nb = {
588 .notifier_call = iommu_bus_notifier,
Alex Williamson14604322011-10-21 15:56:05 -0400589};
590
Joerg Roedelff217762011-08-26 16:48:26 +0200591static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100592{
Alex Williamson43a0ba62012-05-30 14:18:53 -0600593 bus_register_notifier(bus, &iommu_bus_nb);
594 bus_for_each_dev(bus, NULL, ops, add_iommu_group);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100595}
596
Joerg Roedelff217762011-08-26 16:48:26 +0200597/**
598 * bus_set_iommu - set iommu-callbacks for the bus
599 * @bus: bus.
600 * @ops: the callbacks provided by the iommu-driver
601 *
602 * This function is called by an iommu driver to set the iommu methods
603 * used for a particular bus. Drivers for devices on that bus can use
604 * the iommu-api after these ops are registered.
605 * This special function is needed because IOMMUs are usually devices on
606 * the bus itself, so the iommu drivers are not initialized when the bus
607 * is set up. With this function the iommu-driver can set the iommu-ops
608 * afterwards.
609 */
610int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100611{
Joerg Roedelff217762011-08-26 16:48:26 +0200612 if (bus->iommu_ops != NULL)
613 return -EBUSY;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100614
Joerg Roedelff217762011-08-26 16:48:26 +0200615 bus->iommu_ops = ops;
616
617 /* Do IOMMU specific setup for this bus-type */
618 iommu_bus_init(bus, ops);
619
620 return 0;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100621}
Joerg Roedelff217762011-08-26 16:48:26 +0200622EXPORT_SYMBOL_GPL(bus_set_iommu);
623
Joerg Roedela1b60c12011-09-06 18:46:34 +0200624bool iommu_present(struct bus_type *bus)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100625{
Joerg Roedel94441c32011-09-06 18:58:54 +0200626 return bus->iommu_ops != NULL;
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100627}
Joerg Roedela1b60c12011-09-06 18:46:34 +0200628EXPORT_SYMBOL_GPL(iommu_present);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100629
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400630/**
631 * iommu_set_fault_handler() - set a fault handler for an iommu domain
632 * @domain: iommu domain
633 * @handler: fault handler
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300634 * @token: user data, will be passed back to the fault handler
Ohad Ben-Cohen0ed6d2d2011-09-27 07:36:40 -0400635 *
636 * This function should be used by IOMMU users which want to be notified
637 * whenever an IOMMU fault happens.
638 *
639 * The fault handler itself should return 0 on success, and an appropriate
640 * error code otherwise.
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400641 */
642void iommu_set_fault_handler(struct iommu_domain *domain,
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300643 iommu_fault_handler_t handler,
644 void *token)
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400645{
646 BUG_ON(!domain);
647
648 domain->handler = handler;
Ohad Ben-Cohend5069532012-05-21 20:20:05 +0300649 domain->handler_token = token;
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400650}
Ohad Ben-Cohen30bd9182011-09-26 09:11:46 -0400651EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
Ohad Ben-Cohen4f3f8d92011-09-13 15:25:23 -0400652
Steve Mucklef132c6c2012-06-06 18:30:57 -0700653struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags)
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100654{
655 struct iommu_domain *domain;
656 int ret;
657
Joerg Roedel94441c32011-09-06 18:58:54 +0200658 if (bus == NULL || bus->iommu_ops == NULL)
Joerg Roedel905d66c2011-09-06 16:03:26 +0200659 return NULL;
660
KyongHo Cho8bd69602011-12-16 21:38:25 +0900661 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100662 if (!domain)
663 return NULL;
664
Joerg Roedel94441c32011-09-06 18:58:54 +0200665 domain->ops = bus->iommu_ops;
Joerg Roedel905d66c2011-09-06 16:03:26 +0200666
Steve Mucklef132c6c2012-06-06 18:30:57 -0700667 ret = domain->ops->domain_init(domain, flags);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100668 if (ret)
669 goto out_free;
670
671 return domain;
672
673out_free:
674 kfree(domain);
675
676 return NULL;
677}
678EXPORT_SYMBOL_GPL(iommu_domain_alloc);
679
680void iommu_domain_free(struct iommu_domain *domain)
681{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200682 if (likely(domain->ops->domain_destroy != NULL))
683 domain->ops->domain_destroy(domain);
684
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100685 kfree(domain);
686}
687EXPORT_SYMBOL_GPL(iommu_domain_free);
688
689int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
690{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200691 if (unlikely(domain->ops->attach_dev == NULL))
692 return -ENODEV;
693
694 return domain->ops->attach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100695}
696EXPORT_SYMBOL_GPL(iommu_attach_device);
697
698void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
699{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200700 if (unlikely(domain->ops->detach_dev == NULL))
701 return;
702
703 domain->ops->detach_dev(domain, dev);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100704}
705EXPORT_SYMBOL_GPL(iommu_detach_device);
706
Alex Williamson43a0ba62012-05-30 14:18:53 -0600707/*
708 * IOMMU groups are really the natrual working unit of the IOMMU, but
709 * the IOMMU API works on domains and devices. Bridge that gap by
710 * iterating over the devices in a group. Ideally we'd have a single
711 * device which represents the requestor ID of the group, but we also
712 * allow IOMMU drivers to create policy defined minimum sets, where
713 * the physical hardware may be able to distiguish members, but we
714 * wish to group them at a higher level (ex. untrusted multi-function
715 * PCI devices). Thus we attach each device.
716 */
717static int iommu_group_do_attach_device(struct device *dev, void *data)
718{
719 struct iommu_domain *domain = data;
720
721 return iommu_attach_device(domain, dev);
722}
723
724int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
725{
726 return iommu_group_for_each_dev(group, domain,
727 iommu_group_do_attach_device);
728}
729EXPORT_SYMBOL_GPL(iommu_attach_group);
730
731static int iommu_group_do_detach_device(struct device *dev, void *data)
732{
733 struct iommu_domain *domain = data;
734
735 iommu_detach_device(domain, dev);
736
737 return 0;
738}
739
740void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
741{
742 iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
743}
744EXPORT_SYMBOL_GPL(iommu_detach_group);
745
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100746phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
747 unsigned long iova)
748{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200749 if (unlikely(domain->ops->iova_to_phys == NULL))
750 return 0;
751
752 return domain->ops->iova_to_phys(domain, iova);
Joerg Roedelfc2100e2008-11-26 17:21:24 +0100753}
754EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800755
756int iommu_domain_has_cap(struct iommu_domain *domain,
757 unsigned long cap)
758{
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200759 if (unlikely(domain->ops->domain_has_cap == NULL))
760 return 0;
761
762 return domain->ops->domain_has_cap(domain, cap);
Sheng Yangdbb9fd82009-03-18 15:33:06 +0800763}
764EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100765
766int iommu_map(struct iommu_domain *domain, unsigned long iova,
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200767 phys_addr_t paddr, size_t size, int prot)
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100768{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200769 unsigned long orig_iova = iova;
770 unsigned int min_pagesz;
771 size_t orig_size = size;
772 int ret = 0;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100773
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200774 if (unlikely(domain->ops->map == NULL))
775 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100776
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200777 /* find out the minimum page size supported */
778 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100779
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200780 /*
781 * both the virtual address and the physical one, as well as
782 * the size of the mapping, must be aligned (at least) to the
783 * size of the smallest page supported by the hardware
784 */
785 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
786 pr_err("unaligned: iova 0x%lx pa 0x%lx size 0x%lx min_pagesz "
787 "0x%x\n", iova, (unsigned long)paddr,
788 (unsigned long)size, min_pagesz);
789 return -EINVAL;
790 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100791
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200792 pr_debug("map: iova 0x%lx pa 0x%lx size 0x%lx\n", iova,
793 (unsigned long)paddr, (unsigned long)size);
794
795 while (size) {
796 unsigned long pgsize, addr_merge = iova | paddr;
797 unsigned int pgsize_idx;
798
799 /* Max page size that still fits into 'size' */
800 pgsize_idx = __fls(size);
801
802 /* need to consider alignment requirements ? */
803 if (likely(addr_merge)) {
804 /* Max page size allowed by both iova and paddr */
805 unsigned int align_pgsize_idx = __ffs(addr_merge);
806
807 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
808 }
809
810 /* build a mask of acceptable page sizes */
811 pgsize = (1UL << (pgsize_idx + 1)) - 1;
812
813 /* throw away page sizes not supported by the hardware */
814 pgsize &= domain->ops->pgsize_bitmap;
815
816 /* make sure we're still sane */
817 BUG_ON(!pgsize);
818
819 /* pick the biggest page */
820 pgsize_idx = __fls(pgsize);
821 pgsize = 1UL << pgsize_idx;
822
823 pr_debug("mapping: iova 0x%lx pa 0x%lx pgsize %lu\n", iova,
824 (unsigned long)paddr, pgsize);
825
826 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
827 if (ret)
828 break;
829
830 iova += pgsize;
831 paddr += pgsize;
832 size -= pgsize;
833 }
834
835 /* unroll mapping in case something went wrong */
836 if (ret)
837 iommu_unmap(domain, orig_iova, orig_size - size);
838
839 return ret;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100840}
841EXPORT_SYMBOL_GPL(iommu_map);
842
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200843size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100844{
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200845 size_t unmapped_page, unmapped = 0;
846 unsigned int min_pagesz;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100847
Joerg Roedele5aa7f02011-09-06 16:44:29 +0200848 if (unlikely(domain->ops->unmap == NULL))
849 return -ENODEV;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100850
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200851 /* find out the minimum page size supported */
852 min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100853
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200854 /*
855 * The virtual address, as well as the size of the mapping, must be
856 * aligned (at least) to the size of the smallest page supported
857 * by the hardware
858 */
859 if (!IS_ALIGNED(iova | size, min_pagesz)) {
860 pr_err("unaligned: iova 0x%lx size 0x%lx min_pagesz 0x%x\n",
861 iova, (unsigned long)size, min_pagesz);
862 return -EINVAL;
863 }
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100864
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200865 pr_debug("unmap this: iova 0x%lx size 0x%lx\n", iova,
866 (unsigned long)size);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +0200867
Ohad Ben-Cohen7d3002c2011-11-10 11:32:26 +0200868 /*
869 * Keep iterating until we either unmap 'size' bytes (or more)
870 * or we hit an area that isn't mapped.
871 */
872 while (unmapped < size) {
873 size_t left = size - unmapped;
874
875 unmapped_page = domain->ops->unmap(domain, iova, left);
876 if (!unmapped_page)
877 break;
878
879 pr_debug("unmapped: iova 0x%lx size %lx\n", iova,
880 (unsigned long)unmapped_page);
881
882 iova += unmapped_page;
883 unmapped += unmapped_page;
884 }
885
886 return unmapped;
Joerg Roedelcefc53c2010-01-08 13:35:09 +0100887}
888EXPORT_SYMBOL_GPL(iommu_unmap);
Alex Williamson14604322011-10-21 15:56:05 -0400889
Steve Mucklef132c6c2012-06-06 18:30:57 -0700890int iommu_map_range(struct iommu_domain *domain, unsigned int iova,
891 struct scatterlist *sg, unsigned int len, int prot)
892{
893 if (unlikely(domain->ops->map_range == NULL))
894 return -ENODEV;
895
896 BUG_ON(iova & (~PAGE_MASK));
897
898 return domain->ops->map_range(domain, iova, sg, len, prot);
899}
900EXPORT_SYMBOL_GPL(iommu_map_range);
901
902int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova,
903 unsigned int len)
904{
905 if (unlikely(domain->ops->unmap_range == NULL))
906 return -ENODEV;
907
908 BUG_ON(iova & (~PAGE_MASK));
909
910 return domain->ops->unmap_range(domain, iova, len);
911}
912EXPORT_SYMBOL_GPL(iommu_unmap_range);
913
914phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain)
915{
916 if (unlikely(domain->ops->get_pt_base_addr == NULL))
917 return 0;
918
919 return domain->ops->get_pt_base_addr(domain);
920}
921EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr);
922
Alex Williamson43a0ba62012-05-30 14:18:53 -0600923static int __init iommu_init(void)
Alex Williamson14604322011-10-21 15:56:05 -0400924{
Alex Williamson43a0ba62012-05-30 14:18:53 -0600925 iommu_group_kset = kset_create_and_add("iommu_groups",
926 NULL, kernel_kobj);
Olav Haugan0d063372012-12-06 16:53:56 -0800927 idr_init(&iommu_group_idr);
Alex Williamson43a0ba62012-05-30 14:18:53 -0600928 mutex_init(&iommu_group_mutex);
Alex Williamson14604322011-10-21 15:56:05 -0400929
Alex Williamson43a0ba62012-05-30 14:18:53 -0600930 BUG_ON(!iommu_group_kset);
931
932 return 0;
Alex Williamson14604322011-10-21 15:56:05 -0400933}
Alex Williamson43a0ba62012-05-30 14:18:53 -0600934subsys_initcall(iommu_init);