]>
Commit | Line | Data |
---|---|---|
fc2100eb JR |
1 | /* |
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 | ||
7d3002cc OBC |
19 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
20 | ||
905d66c1 | 21 | #include <linux/device.h> |
40998188 | 22 | #include <linux/kernel.h> |
fc2100eb JR |
23 | #include <linux/bug.h> |
24 | #include <linux/types.h> | |
60db4027 AM |
25 | #include <linux/module.h> |
26 | #include <linux/slab.h> | |
fc2100eb JR |
27 | #include <linux/errno.h> |
28 | #include <linux/iommu.h> | |
d72e31c9 AW |
29 | #include <linux/idr.h> |
30 | #include <linux/notifier.h> | |
31 | #include <linux/err.h> | |
7f6db171 | 32 | #include <trace/events/iommu.h> |
d72e31c9 AW |
33 | |
34 | static struct kset *iommu_group_kset; | |
35 | static struct ida iommu_group_ida; | |
36 | static struct mutex iommu_group_mutex; | |
37 | ||
38 | struct 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 | ||
50 | struct iommu_device { | |
51 | struct list_head list; | |
52 | struct device *dev; | |
53 | char *name; | |
54 | }; | |
55 | ||
56 | struct 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) \ | |
64 | struct iommu_group_attribute iommu_group_attr_##_name = \ | |
65 | __ATTR(_name, _mode, _show, _store) | |
fc2100eb | 66 | |
d72e31c9 AW |
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) | |
fc2100eb | 71 | |
d72e31c9 AW |
72 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
73 | struct attribute *__attr, char *buf) | |
1460432c | 74 | { |
d72e31c9 AW |
75 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
76 | struct iommu_group *group = to_iommu_group(kobj); | |
77 | ssize_t ret = -EIO; | |
1460432c | 78 | |
d72e31c9 AW |
79 | if (attr->show) |
80 | ret = attr->show(group, buf); | |
81 | return ret; | |
82 | } | |
83 | ||
84 | static 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; | |
1460432c | 91 | |
d72e31c9 AW |
92 | if (attr->store) |
93 | ret = attr->store(group, buf, count); | |
94 | return ret; | |
1460432c | 95 | } |
1460432c | 96 | |
d72e31c9 AW |
97 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
98 | .show = iommu_group_attr_show, | |
99 | .store = iommu_group_attr_store, | |
100 | }; | |
1460432c | 101 | |
d72e31c9 AW |
102 | static 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); | |
1460432c | 106 | } |
1460432c | 107 | |
d72e31c9 AW |
108 | static 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 | ||
114 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) | |
115 | { | |
116 | return sprintf(buf, "%s\n", group->name); | |
117 | } | |
118 | ||
119 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); | |
120 | ||
121 | static 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); | |
129 | ida_remove(&iommu_group_ida, group->id); | |
130 | mutex_unlock(&iommu_group_mutex); | |
131 | ||
132 | kfree(group->name); | |
133 | kfree(group); | |
134 | } | |
135 | ||
136 | static 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 | */ | |
153 | struct iommu_group *iommu_group_alloc(void) | |
1460432c | 154 | { |
d72e31c9 AW |
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 | ||
169 | again: | |
170 | if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) { | |
171 | kfree(group); | |
172 | mutex_unlock(&iommu_group_mutex); | |
173 | return ERR_PTR(-ENOMEM); | |
174 | } | |
175 | ||
176 | if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id)) | |
177 | goto again; | |
178 | ||
179 | mutex_unlock(&iommu_group_mutex); | |
1460432c | 180 | |
d72e31c9 AW |
181 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
182 | NULL, "%d", group->id); | |
183 | if (ret) { | |
184 | mutex_lock(&iommu_group_mutex); | |
185 | ida_remove(&iommu_group_ida, group->id); | |
186 | mutex_unlock(&iommu_group_mutex); | |
187 | kfree(group); | |
188 | return ERR_PTR(ret); | |
189 | } | |
190 | ||
191 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); | |
192 | if (!group->devices_kobj) { | |
193 | kobject_put(&group->kobj); /* triggers .release & free */ | |
194 | return ERR_PTR(-ENOMEM); | |
195 | } | |
196 | ||
197 | /* | |
198 | * The devices_kobj holds a reference on the group kobject, so | |
199 | * as long as that exists so will the group. We can therefore | |
200 | * use the devices_kobj for reference counting. | |
201 | */ | |
202 | kobject_put(&group->kobj); | |
203 | ||
204 | return group; | |
205 | } | |
206 | EXPORT_SYMBOL_GPL(iommu_group_alloc); | |
207 | ||
aa16bea9 AK |
208 | struct iommu_group *iommu_group_get_by_id(int id) |
209 | { | |
210 | struct kobject *group_kobj; | |
211 | struct iommu_group *group; | |
212 | const char *name; | |
213 | ||
214 | if (!iommu_group_kset) | |
215 | return NULL; | |
216 | ||
217 | name = kasprintf(GFP_KERNEL, "%d", id); | |
218 | if (!name) | |
219 | return NULL; | |
220 | ||
221 | group_kobj = kset_find_obj(iommu_group_kset, name); | |
222 | kfree(name); | |
223 | ||
224 | if (!group_kobj) | |
225 | return NULL; | |
226 | ||
227 | group = container_of(group_kobj, struct iommu_group, kobj); | |
228 | BUG_ON(group->id != id); | |
229 | ||
230 | kobject_get(group->devices_kobj); | |
231 | kobject_put(&group->kobj); | |
232 | ||
233 | return group; | |
234 | } | |
235 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); | |
236 | ||
d72e31c9 AW |
237 | /** |
238 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group | |
239 | * @group: the group | |
240 | * | |
241 | * iommu drivers can store data in the group for use when doing iommu | |
242 | * operations. This function provides a way to retrieve it. Caller | |
243 | * should hold a group reference. | |
244 | */ | |
245 | void *iommu_group_get_iommudata(struct iommu_group *group) | |
246 | { | |
247 | return group->iommu_data; | |
248 | } | |
249 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); | |
250 | ||
251 | /** | |
252 | * iommu_group_set_iommudata - set iommu_data for a group | |
253 | * @group: the group | |
254 | * @iommu_data: new data | |
255 | * @release: release function for iommu_data | |
256 | * | |
257 | * iommu drivers can store data in the group for use when doing iommu | |
258 | * operations. This function provides a way to set the data after | |
259 | * the group has been allocated. Caller should hold a group reference. | |
260 | */ | |
261 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, | |
262 | void (*release)(void *iommu_data)) | |
1460432c | 263 | { |
d72e31c9 AW |
264 | group->iommu_data = iommu_data; |
265 | group->iommu_data_release = release; | |
266 | } | |
267 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); | |
1460432c | 268 | |
d72e31c9 AW |
269 | /** |
270 | * iommu_group_set_name - set name for a group | |
271 | * @group: the group | |
272 | * @name: name | |
273 | * | |
274 | * Allow iommu driver to set a name for a group. When set it will | |
275 | * appear in a name attribute file under the group in sysfs. | |
276 | */ | |
277 | int iommu_group_set_name(struct iommu_group *group, const char *name) | |
278 | { | |
279 | int ret; | |
280 | ||
281 | if (group->name) { | |
282 | iommu_group_remove_file(group, &iommu_group_attr_name); | |
283 | kfree(group->name); | |
284 | group->name = NULL; | |
285 | if (!name) | |
286 | return 0; | |
287 | } | |
288 | ||
289 | group->name = kstrdup(name, GFP_KERNEL); | |
290 | if (!group->name) | |
291 | return -ENOMEM; | |
292 | ||
293 | ret = iommu_group_create_file(group, &iommu_group_attr_name); | |
294 | if (ret) { | |
295 | kfree(group->name); | |
296 | group->name = NULL; | |
297 | return ret; | |
298 | } | |
1460432c AW |
299 | |
300 | return 0; | |
301 | } | |
d72e31c9 | 302 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
1460432c | 303 | |
d72e31c9 AW |
304 | /** |
305 | * iommu_group_add_device - add a device to an iommu group | |
306 | * @group: the group into which to add the device (reference should be held) | |
307 | * @dev: the device | |
308 | * | |
309 | * This function is called by an iommu driver to add a device into a | |
310 | * group. Adding a device increments the group reference count. | |
311 | */ | |
312 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) | |
1460432c | 313 | { |
d72e31c9 AW |
314 | int ret, i = 0; |
315 | struct iommu_device *device; | |
316 | ||
317 | device = kzalloc(sizeof(*device), GFP_KERNEL); | |
318 | if (!device) | |
319 | return -ENOMEM; | |
320 | ||
321 | device->dev = dev; | |
1460432c | 322 | |
d72e31c9 AW |
323 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
324 | if (ret) { | |
325 | kfree(device); | |
326 | return ret; | |
327 | } | |
328 | ||
329 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); | |
330 | rename: | |
331 | if (!device->name) { | |
332 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
333 | kfree(device); | |
334 | return -ENOMEM; | |
335 | } | |
1460432c | 336 | |
d72e31c9 AW |
337 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
338 | &dev->kobj, device->name); | |
339 | if (ret) { | |
340 | kfree(device->name); | |
341 | if (ret == -EEXIST && i >= 0) { | |
342 | /* | |
343 | * Account for the slim chance of collision | |
344 | * and append an instance to the name. | |
345 | */ | |
346 | device->name = kasprintf(GFP_KERNEL, "%s.%d", | |
347 | kobject_name(&dev->kobj), i++); | |
348 | goto rename; | |
349 | } | |
350 | ||
351 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
352 | kfree(device); | |
353 | return ret; | |
354 | } | |
355 | ||
356 | kobject_get(group->devices_kobj); | |
357 | ||
358 | dev->iommu_group = group; | |
359 | ||
360 | mutex_lock(&group->mutex); | |
361 | list_add_tail(&device->list, &group->devices); | |
362 | mutex_unlock(&group->mutex); | |
363 | ||
364 | /* Notify any listeners about change to group. */ | |
365 | blocking_notifier_call_chain(&group->notifier, | |
366 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); | |
d1cf7e82 SK |
367 | |
368 | trace_add_device_to_group(group->id, dev); | |
1460432c AW |
369 | return 0; |
370 | } | |
d72e31c9 | 371 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
1460432c | 372 | |
d72e31c9 AW |
373 | /** |
374 | * iommu_group_remove_device - remove a device from it's current group | |
375 | * @dev: device to be removed | |
376 | * | |
377 | * This function is called by an iommu driver to remove the device from | |
378 | * it's current group. This decrements the iommu group reference count. | |
379 | */ | |
380 | void iommu_group_remove_device(struct device *dev) | |
381 | { | |
382 | struct iommu_group *group = dev->iommu_group; | |
383 | struct iommu_device *tmp_device, *device = NULL; | |
384 | ||
385 | /* Pre-notify listeners that a device is being removed. */ | |
386 | blocking_notifier_call_chain(&group->notifier, | |
387 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); | |
388 | ||
389 | mutex_lock(&group->mutex); | |
390 | list_for_each_entry(tmp_device, &group->devices, list) { | |
391 | if (tmp_device->dev == dev) { | |
392 | device = tmp_device; | |
393 | list_del(&device->list); | |
394 | break; | |
395 | } | |
396 | } | |
397 | mutex_unlock(&group->mutex); | |
398 | ||
399 | if (!device) | |
400 | return; | |
401 | ||
402 | sysfs_remove_link(group->devices_kobj, device->name); | |
403 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
404 | ||
405 | kfree(device->name); | |
406 | kfree(device); | |
407 | dev->iommu_group = NULL; | |
408 | kobject_put(group->devices_kobj); | |
409 | } | |
410 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); | |
411 | ||
412 | /** | |
413 | * iommu_group_for_each_dev - iterate over each device in the group | |
414 | * @group: the group | |
415 | * @data: caller opaque data to be passed to callback function | |
416 | * @fn: caller supplied callback function | |
417 | * | |
418 | * This function is called by group users to iterate over group devices. | |
419 | * Callers should hold a reference count to the group during callback. | |
420 | * The group->mutex is held across callbacks, which will block calls to | |
421 | * iommu_group_add/remove_device. | |
422 | */ | |
423 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, | |
424 | int (*fn)(struct device *, void *)) | |
425 | { | |
426 | struct iommu_device *device; | |
427 | int ret = 0; | |
428 | ||
429 | mutex_lock(&group->mutex); | |
430 | list_for_each_entry(device, &group->devices, list) { | |
431 | ret = fn(device->dev, data); | |
432 | if (ret) | |
433 | break; | |
434 | } | |
435 | mutex_unlock(&group->mutex); | |
436 | return ret; | |
437 | } | |
438 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); | |
439 | ||
440 | /** | |
441 | * iommu_group_get - Return the group for a device and increment reference | |
442 | * @dev: get the group that this device belongs to | |
443 | * | |
444 | * This function is called by iommu drivers and users to get the group | |
445 | * for the specified device. If found, the group is returned and the group | |
446 | * reference in incremented, else NULL. | |
447 | */ | |
448 | struct iommu_group *iommu_group_get(struct device *dev) | |
449 | { | |
450 | struct iommu_group *group = dev->iommu_group; | |
451 | ||
452 | if (group) | |
453 | kobject_get(group->devices_kobj); | |
454 | ||
455 | return group; | |
456 | } | |
457 | EXPORT_SYMBOL_GPL(iommu_group_get); | |
458 | ||
459 | /** | |
460 | * iommu_group_put - Decrement group reference | |
461 | * @group: the group to use | |
462 | * | |
463 | * This function is called by iommu drivers and users to release the | |
464 | * iommu group. Once the reference count is zero, the group is released. | |
465 | */ | |
466 | void iommu_group_put(struct iommu_group *group) | |
467 | { | |
468 | if (group) | |
469 | kobject_put(group->devices_kobj); | |
470 | } | |
471 | EXPORT_SYMBOL_GPL(iommu_group_put); | |
472 | ||
473 | /** | |
474 | * iommu_group_register_notifier - Register a notifier for group changes | |
475 | * @group: the group to watch | |
476 | * @nb: notifier block to signal | |
477 | * | |
478 | * This function allows iommu group users to track changes in a group. | |
479 | * See include/linux/iommu.h for actions sent via this notifier. Caller | |
480 | * should hold a reference to the group throughout notifier registration. | |
481 | */ | |
482 | int iommu_group_register_notifier(struct iommu_group *group, | |
483 | struct notifier_block *nb) | |
484 | { | |
485 | return blocking_notifier_chain_register(&group->notifier, nb); | |
486 | } | |
487 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); | |
488 | ||
489 | /** | |
490 | * iommu_group_unregister_notifier - Unregister a notifier | |
491 | * @group: the group to watch | |
492 | * @nb: notifier block to signal | |
493 | * | |
494 | * Unregister a previously registered group notifier block. | |
495 | */ | |
496 | int iommu_group_unregister_notifier(struct iommu_group *group, | |
497 | struct notifier_block *nb) | |
498 | { | |
499 | return blocking_notifier_chain_unregister(&group->notifier, nb); | |
500 | } | |
501 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); | |
502 | ||
503 | /** | |
504 | * iommu_group_id - Return ID for a group | |
505 | * @group: the group to ID | |
506 | * | |
507 | * Return the unique ID for the group matching the sysfs group number. | |
508 | */ | |
509 | int iommu_group_id(struct iommu_group *group) | |
510 | { | |
511 | return group->id; | |
512 | } | |
513 | EXPORT_SYMBOL_GPL(iommu_group_id); | |
1460432c | 514 | |
d72e31c9 | 515 | static int add_iommu_group(struct device *dev, void *data) |
1460432c | 516 | { |
d72e31c9 AW |
517 | struct iommu_ops *ops = data; |
518 | ||
519 | if (!ops->add_device) | |
520 | return -ENODEV; | |
1460432c | 521 | |
d72e31c9 AW |
522 | WARN_ON(dev->iommu_group); |
523 | ||
524 | ops->add_device(dev); | |
1460432c AW |
525 | |
526 | return 0; | |
527 | } | |
528 | ||
d72e31c9 AW |
529 | static int iommu_bus_notifier(struct notifier_block *nb, |
530 | unsigned long action, void *data) | |
1460432c AW |
531 | { |
532 | struct device *dev = data; | |
d72e31c9 AW |
533 | struct iommu_ops *ops = dev->bus->iommu_ops; |
534 | struct iommu_group *group; | |
535 | unsigned long group_action = 0; | |
536 | ||
537 | /* | |
538 | * ADD/DEL call into iommu driver ops if provided, which may | |
539 | * result in ADD/DEL notifiers to group->notifier | |
540 | */ | |
541 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
542 | if (ops->add_device) | |
543 | return ops->add_device(dev); | |
544 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { | |
545 | if (ops->remove_device && dev->iommu_group) { | |
546 | ops->remove_device(dev); | |
547 | return 0; | |
548 | } | |
549 | } | |
1460432c | 550 | |
d72e31c9 AW |
551 | /* |
552 | * Remaining BUS_NOTIFYs get filtered and republished to the | |
553 | * group, if anyone is listening | |
554 | */ | |
555 | group = iommu_group_get(dev); | |
556 | if (!group) | |
557 | return 0; | |
1460432c | 558 | |
d72e31c9 AW |
559 | switch (action) { |
560 | case BUS_NOTIFY_BIND_DRIVER: | |
561 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; | |
562 | break; | |
563 | case BUS_NOTIFY_BOUND_DRIVER: | |
564 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; | |
565 | break; | |
566 | case BUS_NOTIFY_UNBIND_DRIVER: | |
567 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; | |
568 | break; | |
569 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
570 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; | |
571 | break; | |
572 | } | |
1460432c | 573 | |
d72e31c9 AW |
574 | if (group_action) |
575 | blocking_notifier_call_chain(&group->notifier, | |
576 | group_action, dev); | |
1460432c | 577 | |
d72e31c9 | 578 | iommu_group_put(group); |
1460432c AW |
579 | return 0; |
580 | } | |
581 | ||
d72e31c9 AW |
582 | static struct notifier_block iommu_bus_nb = { |
583 | .notifier_call = iommu_bus_notifier, | |
1460432c AW |
584 | }; |
585 | ||
ff21776d JR |
586 | static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops) |
587 | { | |
d72e31c9 AW |
588 | bus_register_notifier(bus, &iommu_bus_nb); |
589 | bus_for_each_dev(bus, NULL, ops, add_iommu_group); | |
ff21776d | 590 | } |
fc2100eb | 591 | |
ff21776d JR |
592 | /** |
593 | * bus_set_iommu - set iommu-callbacks for the bus | |
594 | * @bus: bus. | |
595 | * @ops: the callbacks provided by the iommu-driver | |
596 | * | |
597 | * This function is called by an iommu driver to set the iommu methods | |
598 | * used for a particular bus. Drivers for devices on that bus can use | |
599 | * the iommu-api after these ops are registered. | |
600 | * This special function is needed because IOMMUs are usually devices on | |
601 | * the bus itself, so the iommu drivers are not initialized when the bus | |
602 | * is set up. With this function the iommu-driver can set the iommu-ops | |
603 | * afterwards. | |
604 | */ | |
605 | int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) | |
fc2100eb | 606 | { |
ff21776d JR |
607 | if (bus->iommu_ops != NULL) |
608 | return -EBUSY; | |
fc2100eb | 609 | |
ff21776d JR |
610 | bus->iommu_ops = ops; |
611 | ||
612 | /* Do IOMMU specific setup for this bus-type */ | |
613 | iommu_bus_init(bus, ops); | |
fc2100eb | 614 | |
ff21776d | 615 | return 0; |
fc2100eb | 616 | } |
ff21776d | 617 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 618 | |
a1b60c1c | 619 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 620 | { |
94441c3b | 621 | return bus->iommu_ops != NULL; |
fc2100eb | 622 | } |
a1b60c1c | 623 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 624 | |
4f3f8d9d OBC |
625 | /** |
626 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
627 | * @domain: iommu domain | |
628 | * @handler: fault handler | |
77ca2332 | 629 | * @token: user data, will be passed back to the fault handler |
0ed6d2d2 OBC |
630 | * |
631 | * This function should be used by IOMMU users which want to be notified | |
632 | * whenever an IOMMU fault happens. | |
633 | * | |
634 | * The fault handler itself should return 0 on success, and an appropriate | |
635 | * error code otherwise. | |
4f3f8d9d OBC |
636 | */ |
637 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77ca2332 OBC |
638 | iommu_fault_handler_t handler, |
639 | void *token) | |
4f3f8d9d OBC |
640 | { |
641 | BUG_ON(!domain); | |
642 | ||
643 | domain->handler = handler; | |
77ca2332 | 644 | domain->handler_token = token; |
4f3f8d9d | 645 | } |
30bd918c | 646 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 647 | |
905d66c1 | 648 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
fc2100eb JR |
649 | { |
650 | struct iommu_domain *domain; | |
651 | int ret; | |
652 | ||
94441c3b | 653 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
654 | return NULL; |
655 | ||
8bd6960c | 656 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
fc2100eb JR |
657 | if (!domain) |
658 | return NULL; | |
659 | ||
94441c3b | 660 | domain->ops = bus->iommu_ops; |
905d66c1 | 661 | |
94441c3b | 662 | ret = domain->ops->domain_init(domain); |
fc2100eb JR |
663 | if (ret) |
664 | goto out_free; | |
665 | ||
666 | return domain; | |
667 | ||
668 | out_free: | |
669 | kfree(domain); | |
670 | ||
671 | return NULL; | |
672 | } | |
673 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
674 | ||
675 | void iommu_domain_free(struct iommu_domain *domain) | |
676 | { | |
e5aa7f00 JR |
677 | if (likely(domain->ops->domain_destroy != NULL)) |
678 | domain->ops->domain_destroy(domain); | |
679 | ||
fc2100eb JR |
680 | kfree(domain); |
681 | } | |
682 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
683 | ||
684 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
685 | { | |
e5aa7f00 JR |
686 | if (unlikely(domain->ops->attach_dev == NULL)) |
687 | return -ENODEV; | |
688 | ||
689 | return domain->ops->attach_dev(domain, dev); | |
fc2100eb JR |
690 | } |
691 | EXPORT_SYMBOL_GPL(iommu_attach_device); | |
692 | ||
693 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
694 | { | |
e5aa7f00 JR |
695 | if (unlikely(domain->ops->detach_dev == NULL)) |
696 | return; | |
697 | ||
698 | domain->ops->detach_dev(domain, dev); | |
fc2100eb JR |
699 | } |
700 | EXPORT_SYMBOL_GPL(iommu_detach_device); | |
701 | ||
d72e31c9 AW |
702 | /* |
703 | * IOMMU groups are really the natrual working unit of the IOMMU, but | |
704 | * the IOMMU API works on domains and devices. Bridge that gap by | |
705 | * iterating over the devices in a group. Ideally we'd have a single | |
706 | * device which represents the requestor ID of the group, but we also | |
707 | * allow IOMMU drivers to create policy defined minimum sets, where | |
708 | * the physical hardware may be able to distiguish members, but we | |
709 | * wish to group them at a higher level (ex. untrusted multi-function | |
710 | * PCI devices). Thus we attach each device. | |
711 | */ | |
712 | static int iommu_group_do_attach_device(struct device *dev, void *data) | |
713 | { | |
714 | struct iommu_domain *domain = data; | |
715 | ||
716 | return iommu_attach_device(domain, dev); | |
717 | } | |
718 | ||
719 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) | |
720 | { | |
721 | return iommu_group_for_each_dev(group, domain, | |
722 | iommu_group_do_attach_device); | |
723 | } | |
724 | EXPORT_SYMBOL_GPL(iommu_attach_group); | |
725 | ||
726 | static int iommu_group_do_detach_device(struct device *dev, void *data) | |
727 | { | |
728 | struct iommu_domain *domain = data; | |
729 | ||
730 | iommu_detach_device(domain, dev); | |
731 | ||
732 | return 0; | |
733 | } | |
734 | ||
735 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) | |
736 | { | |
737 | iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device); | |
738 | } | |
739 | EXPORT_SYMBOL_GPL(iommu_detach_group); | |
740 | ||
bb5547ac | 741 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
fc2100eb | 742 | { |
e5aa7f00 JR |
743 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
744 | return 0; | |
745 | ||
746 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
747 | } |
748 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 SY |
749 | |
750 | int iommu_domain_has_cap(struct iommu_domain *domain, | |
751 | unsigned long cap) | |
752 | { | |
e5aa7f00 JR |
753 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
754 | return 0; | |
755 | ||
756 | return domain->ops->domain_has_cap(domain, cap); | |
dbb9fd86 SY |
757 | } |
758 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); | |
cefc53c7 | 759 | |
bd13969b AW |
760 | static size_t iommu_pgsize(struct iommu_domain *domain, |
761 | unsigned long addr_merge, size_t size) | |
762 | { | |
763 | unsigned int pgsize_idx; | |
764 | size_t pgsize; | |
765 | ||
766 | /* Max page size that still fits into 'size' */ | |
767 | pgsize_idx = __fls(size); | |
768 | ||
769 | /* need to consider alignment requirements ? */ | |
770 | if (likely(addr_merge)) { | |
771 | /* Max page size allowed by address */ | |
772 | unsigned int align_pgsize_idx = __ffs(addr_merge); | |
773 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); | |
774 | } | |
775 | ||
776 | /* build a mask of acceptable page sizes */ | |
777 | pgsize = (1UL << (pgsize_idx + 1)) - 1; | |
778 | ||
779 | /* throw away page sizes not supported by the hardware */ | |
780 | pgsize &= domain->ops->pgsize_bitmap; | |
781 | ||
782 | /* make sure we're still sane */ | |
783 | BUG_ON(!pgsize); | |
784 | ||
785 | /* pick the biggest page */ | |
786 | pgsize_idx = __fls(pgsize); | |
787 | pgsize = 1UL << pgsize_idx; | |
788 | ||
789 | return pgsize; | |
790 | } | |
791 | ||
cefc53c7 | 792 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
7d3002cc | 793 | phys_addr_t paddr, size_t size, int prot) |
cefc53c7 | 794 | { |
7d3002cc OBC |
795 | unsigned long orig_iova = iova; |
796 | unsigned int min_pagesz; | |
797 | size_t orig_size = size; | |
798 | int ret = 0; | |
cefc53c7 | 799 | |
57886518 JR |
800 | if (unlikely(domain->ops->unmap == NULL || |
801 | domain->ops->pgsize_bitmap == 0UL)) | |
e5aa7f00 | 802 | return -ENODEV; |
cefc53c7 | 803 | |
7d3002cc OBC |
804 | /* find out the minimum page size supported */ |
805 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); | |
806 | ||
807 | /* | |
808 | * both the virtual address and the physical one, as well as | |
809 | * the size of the mapping, must be aligned (at least) to the | |
810 | * size of the smallest page supported by the hardware | |
811 | */ | |
812 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { | |
6197ca82 JP |
813 | pr_err("unaligned: iova 0x%lx pa 0x%pa size 0x%zx min_pagesz 0x%x\n", |
814 | iova, &paddr, size, min_pagesz); | |
7d3002cc OBC |
815 | return -EINVAL; |
816 | } | |
817 | ||
6197ca82 | 818 | pr_debug("map: iova 0x%lx pa 0x%pa size 0x%zx\n", iova, &paddr, size); |
7d3002cc OBC |
819 | |
820 | while (size) { | |
bd13969b | 821 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
7d3002cc | 822 | |
6197ca82 JP |
823 | pr_debug("mapping: iova 0x%lx pa 0x%pa pgsize 0x%zx\n", |
824 | iova, &paddr, pgsize); | |
7d3002cc OBC |
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; | |
cefc53c7 JR |
840 | } |
841 | EXPORT_SYMBOL_GPL(iommu_map); | |
842 | ||
7d3002cc | 843 | size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size) |
cefc53c7 | 844 | { |
7d3002cc OBC |
845 | size_t unmapped_page, unmapped = 0; |
846 | unsigned int min_pagesz; | |
cefc53c7 | 847 | |
57886518 JR |
848 | if (unlikely(domain->ops->unmap == NULL || |
849 | domain->ops->pgsize_bitmap == 0UL)) | |
e5aa7f00 JR |
850 | return -ENODEV; |
851 | ||
7d3002cc OBC |
852 | /* find out the minimum page size supported */ |
853 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); | |
854 | ||
855 | /* | |
856 | * The virtual address, as well as the size of the mapping, must be | |
857 | * aligned (at least) to the size of the smallest page supported | |
858 | * by the hardware | |
859 | */ | |
860 | if (!IS_ALIGNED(iova | size, min_pagesz)) { | |
6197ca82 JP |
861 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
862 | iova, size, min_pagesz); | |
7d3002cc OBC |
863 | return -EINVAL; |
864 | } | |
865 | ||
6197ca82 | 866 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
7d3002cc OBC |
867 | |
868 | /* | |
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) { | |
bd13969b | 873 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
7d3002cc | 874 | |
bd13969b | 875 | unmapped_page = domain->ops->unmap(domain, iova, pgsize); |
7d3002cc OBC |
876 | if (!unmapped_page) |
877 | break; | |
878 | ||
6197ca82 JP |
879 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
880 | iova, unmapped_page); | |
7d3002cc OBC |
881 | |
882 | iova += unmapped_page; | |
883 | unmapped += unmapped_page; | |
884 | } | |
885 | ||
886 | return unmapped; | |
cefc53c7 JR |
887 | } |
888 | EXPORT_SYMBOL_GPL(iommu_unmap); | |
1460432c | 889 | |
d7787d57 JR |
890 | |
891 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, | |
80f97f0f | 892 | phys_addr_t paddr, u64 size, int prot) |
d7787d57 JR |
893 | { |
894 | if (unlikely(domain->ops->domain_window_enable == NULL)) | |
895 | return -ENODEV; | |
896 | ||
80f97f0f VS |
897 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
898 | prot); | |
d7787d57 JR |
899 | } |
900 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); | |
901 | ||
902 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) | |
903 | { | |
904 | if (unlikely(domain->ops->domain_window_disable == NULL)) | |
905 | return; | |
906 | ||
907 | return domain->ops->domain_window_disable(domain, wnd_nr); | |
908 | } | |
909 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); | |
910 | ||
d72e31c9 | 911 | static int __init iommu_init(void) |
1460432c | 912 | { |
d72e31c9 AW |
913 | iommu_group_kset = kset_create_and_add("iommu_groups", |
914 | NULL, kernel_kobj); | |
915 | ida_init(&iommu_group_ida); | |
916 | mutex_init(&iommu_group_mutex); | |
1460432c | 917 | |
d72e31c9 AW |
918 | BUG_ON(!iommu_group_kset); |
919 | ||
920 | return 0; | |
1460432c | 921 | } |
097e3635 | 922 | arch_initcall(iommu_init); |
0cd76dd1 JR |
923 | |
924 | int iommu_domain_get_attr(struct iommu_domain *domain, | |
925 | enum iommu_attr attr, void *data) | |
926 | { | |
0ff64f80 | 927 | struct iommu_domain_geometry *geometry; |
d2e12160 | 928 | bool *paging; |
0ff64f80 | 929 | int ret = 0; |
69356712 | 930 | u32 *count; |
0ff64f80 JR |
931 | |
932 | switch (attr) { | |
933 | case DOMAIN_ATTR_GEOMETRY: | |
934 | geometry = data; | |
935 | *geometry = domain->geometry; | |
936 | ||
d2e12160 JR |
937 | break; |
938 | case DOMAIN_ATTR_PAGING: | |
939 | paging = data; | |
940 | *paging = (domain->ops->pgsize_bitmap != 0UL); | |
69356712 JR |
941 | break; |
942 | case DOMAIN_ATTR_WINDOWS: | |
943 | count = data; | |
944 | ||
945 | if (domain->ops->domain_get_windows != NULL) | |
946 | *count = domain->ops->domain_get_windows(domain); | |
947 | else | |
948 | ret = -ENODEV; | |
949 | ||
0ff64f80 JR |
950 | break; |
951 | default: | |
952 | if (!domain->ops->domain_get_attr) | |
953 | return -EINVAL; | |
0cd76dd1 | 954 | |
0ff64f80 JR |
955 | ret = domain->ops->domain_get_attr(domain, attr, data); |
956 | } | |
957 | ||
958 | return ret; | |
0cd76dd1 JR |
959 | } |
960 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); | |
961 | ||
962 | int iommu_domain_set_attr(struct iommu_domain *domain, | |
963 | enum iommu_attr attr, void *data) | |
964 | { | |
69356712 JR |
965 | int ret = 0; |
966 | u32 *count; | |
967 | ||
968 | switch (attr) { | |
969 | case DOMAIN_ATTR_WINDOWS: | |
970 | count = data; | |
971 | ||
972 | if (domain->ops->domain_set_windows != NULL) | |
973 | ret = domain->ops->domain_set_windows(domain, *count); | |
974 | else | |
975 | ret = -ENODEV; | |
1460432c | 976 | |
69356712 JR |
977 | break; |
978 | default: | |
979 | if (domain->ops->domain_set_attr == NULL) | |
980 | return -EINVAL; | |
981 | ||
982 | ret = domain->ops->domain_set_attr(domain, attr, data); | |
983 | } | |
984 | ||
985 | return ret; | |
1460432c | 986 | } |
0cd76dd1 | 987 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |