]>
Commit | Line | Data |
---|---|---|
fc2100eb JR |
1 | /* |
2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. | |
63ce3ae8 | 3 | * Author: Joerg Roedel <jroedel@suse.de> |
fc2100eb JR |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published | |
7 | * by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
92e7066f | 19 | #define pr_fmt(fmt) "iommu: " fmt |
7d3002cc | 20 | |
905d66c1 | 21 | #include <linux/device.h> |
40998188 | 22 | #include <linux/kernel.h> |
fc2100eb JR |
23 | #include <linux/bug.h> |
24 | #include <linux/types.h> | |
60db4027 AM |
25 | #include <linux/module.h> |
26 | #include <linux/slab.h> | |
fc2100eb JR |
27 | #include <linux/errno.h> |
28 | #include <linux/iommu.h> | |
d72e31c9 AW |
29 | #include <linux/idr.h> |
30 | #include <linux/notifier.h> | |
31 | #include <linux/err.h> | |
104a1c13 | 32 | #include <linux/pci.h> |
f096c061 | 33 | #include <linux/bitops.h> |
57f98d2f | 34 | #include <linux/property.h> |
7f6db171 | 35 | #include <trace/events/iommu.h> |
d72e31c9 AW |
36 | |
37 | static struct kset *iommu_group_kset; | |
e38d1f13 | 38 | static DEFINE_IDA(iommu_group_ida); |
fccb4e3b | 39 | static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA; |
3f11a03b | 40 | static bool iommu_dma_strict __read_mostly = true; |
d72e31c9 | 41 | |
b22f6434 TR |
42 | struct iommu_callback_data { |
43 | const struct iommu_ops *ops; | |
44 | }; | |
45 | ||
d72e31c9 AW |
46 | struct iommu_group { |
47 | struct kobject kobj; | |
48 | struct kobject *devices_kobj; | |
49 | struct list_head devices; | |
50 | struct mutex mutex; | |
51 | struct blocking_notifier_head notifier; | |
52 | void *iommu_data; | |
53 | void (*iommu_data_release)(void *iommu_data); | |
54 | char *name; | |
55 | int id; | |
53723dc5 | 56 | struct iommu_domain *default_domain; |
e39cb8a3 | 57 | struct iommu_domain *domain; |
d72e31c9 AW |
58 | }; |
59 | ||
c09e22d5 | 60 | struct group_device { |
d72e31c9 AW |
61 | struct list_head list; |
62 | struct device *dev; | |
63 | char *name; | |
64 | }; | |
65 | ||
66 | struct iommu_group_attribute { | |
67 | struct attribute attr; | |
68 | ssize_t (*show)(struct iommu_group *group, char *buf); | |
69 | ssize_t (*store)(struct iommu_group *group, | |
70 | const char *buf, size_t count); | |
71 | }; | |
72 | ||
bc7d12b9 EA |
73 | static const char * const iommu_group_resv_type_string[] = { |
74 | [IOMMU_RESV_DIRECT] = "direct", | |
75 | [IOMMU_RESV_RESERVED] = "reserved", | |
76 | [IOMMU_RESV_MSI] = "msi", | |
9d3a4de4 | 77 | [IOMMU_RESV_SW_MSI] = "msi", |
bc7d12b9 EA |
78 | }; |
79 | ||
d72e31c9 AW |
80 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
81 | struct iommu_group_attribute iommu_group_attr_##_name = \ | |
82 | __ATTR(_name, _mode, _show, _store) | |
fc2100eb | 83 | |
d72e31c9 AW |
84 | #define to_iommu_group_attr(_attr) \ |
85 | container_of(_attr, struct iommu_group_attribute, attr) | |
86 | #define to_iommu_group(_kobj) \ | |
87 | container_of(_kobj, struct iommu_group, kobj) | |
fc2100eb | 88 | |
b0119e87 JR |
89 | static LIST_HEAD(iommu_device_list); |
90 | static DEFINE_SPINLOCK(iommu_device_lock); | |
91 | ||
92 | int iommu_device_register(struct iommu_device *iommu) | |
93 | { | |
94 | spin_lock(&iommu_device_lock); | |
95 | list_add_tail(&iommu->list, &iommu_device_list); | |
96 | spin_unlock(&iommu_device_lock); | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
101 | void iommu_device_unregister(struct iommu_device *iommu) | |
102 | { | |
103 | spin_lock(&iommu_device_lock); | |
104 | list_del(&iommu->list); | |
105 | spin_unlock(&iommu_device_lock); | |
106 | } | |
107 | ||
53723dc5 JR |
108 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
109 | unsigned type); | |
e39cb8a3 JR |
110 | static int __iommu_attach_device(struct iommu_domain *domain, |
111 | struct device *dev); | |
112 | static int __iommu_attach_group(struct iommu_domain *domain, | |
113 | struct iommu_group *group); | |
114 | static void __iommu_detach_group(struct iommu_domain *domain, | |
115 | struct iommu_group *group); | |
53723dc5 | 116 | |
fccb4e3b WD |
117 | static int __init iommu_set_def_domain_type(char *str) |
118 | { | |
119 | bool pt; | |
120 | ||
121 | if (!str || strtobool(str, &pt)) | |
122 | return -EINVAL; | |
123 | ||
124 | iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA; | |
125 | return 0; | |
126 | } | |
127 | early_param("iommu.passthrough", iommu_set_def_domain_type); | |
128 | ||
3f11a03b ZL |
129 | static int __init iommu_dma_setup(char *str) |
130 | { | |
131 | return kstrtobool(str, &iommu_dma_strict); | |
132 | } | |
133 | early_param("iommu.strict", iommu_dma_setup); | |
134 | ||
d72e31c9 AW |
135 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
136 | struct attribute *__attr, char *buf) | |
1460432c | 137 | { |
d72e31c9 AW |
138 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
139 | struct iommu_group *group = to_iommu_group(kobj); | |
140 | ssize_t ret = -EIO; | |
1460432c | 141 | |
d72e31c9 AW |
142 | if (attr->show) |
143 | ret = attr->show(group, buf); | |
144 | return ret; | |
145 | } | |
146 | ||
147 | static ssize_t iommu_group_attr_store(struct kobject *kobj, | |
148 | struct attribute *__attr, | |
149 | const char *buf, size_t count) | |
150 | { | |
151 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); | |
152 | struct iommu_group *group = to_iommu_group(kobj); | |
153 | ssize_t ret = -EIO; | |
1460432c | 154 | |
d72e31c9 AW |
155 | if (attr->store) |
156 | ret = attr->store(group, buf, count); | |
157 | return ret; | |
1460432c | 158 | } |
1460432c | 159 | |
d72e31c9 AW |
160 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
161 | .show = iommu_group_attr_show, | |
162 | .store = iommu_group_attr_store, | |
163 | }; | |
1460432c | 164 | |
d72e31c9 AW |
165 | static int iommu_group_create_file(struct iommu_group *group, |
166 | struct iommu_group_attribute *attr) | |
167 | { | |
168 | return sysfs_create_file(&group->kobj, &attr->attr); | |
1460432c | 169 | } |
1460432c | 170 | |
d72e31c9 AW |
171 | static void iommu_group_remove_file(struct iommu_group *group, |
172 | struct iommu_group_attribute *attr) | |
173 | { | |
174 | sysfs_remove_file(&group->kobj, &attr->attr); | |
175 | } | |
176 | ||
177 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) | |
178 | { | |
179 | return sprintf(buf, "%s\n", group->name); | |
180 | } | |
181 | ||
6c65fb31 EA |
182 | /** |
183 | * iommu_insert_resv_region - Insert a new region in the | |
184 | * list of reserved regions. | |
185 | * @new: new region to insert | |
186 | * @regions: list of regions | |
187 | * | |
188 | * The new element is sorted by address with respect to the other | |
189 | * regions of the same type. In case it overlaps with another | |
190 | * region of the same type, regions are merged. In case it | |
191 | * overlaps with another region of different type, regions are | |
192 | * not merged. | |
193 | */ | |
194 | static int iommu_insert_resv_region(struct iommu_resv_region *new, | |
195 | struct list_head *regions) | |
196 | { | |
197 | struct iommu_resv_region *region; | |
198 | phys_addr_t start = new->start; | |
199 | phys_addr_t end = new->start + new->length - 1; | |
200 | struct list_head *pos = regions->next; | |
201 | ||
202 | while (pos != regions) { | |
203 | struct iommu_resv_region *entry = | |
204 | list_entry(pos, struct iommu_resv_region, list); | |
205 | phys_addr_t a = entry->start; | |
206 | phys_addr_t b = entry->start + entry->length - 1; | |
207 | int type = entry->type; | |
208 | ||
209 | if (end < a) { | |
210 | goto insert; | |
211 | } else if (start > b) { | |
212 | pos = pos->next; | |
213 | } else if ((start >= a) && (end <= b)) { | |
214 | if (new->type == type) | |
215 | goto done; | |
216 | else | |
217 | pos = pos->next; | |
218 | } else { | |
219 | if (new->type == type) { | |
220 | phys_addr_t new_start = min(a, start); | |
221 | phys_addr_t new_end = max(b, end); | |
222 | ||
223 | list_del(&entry->list); | |
224 | entry->start = new_start; | |
225 | entry->length = new_end - new_start + 1; | |
226 | iommu_insert_resv_region(entry, regions); | |
227 | } else { | |
228 | pos = pos->next; | |
229 | } | |
230 | } | |
231 | } | |
232 | insert: | |
233 | region = iommu_alloc_resv_region(new->start, new->length, | |
234 | new->prot, new->type); | |
235 | if (!region) | |
236 | return -ENOMEM; | |
237 | ||
238 | list_add_tail(®ion->list, pos); | |
239 | done: | |
240 | return 0; | |
241 | } | |
242 | ||
243 | static int | |
244 | iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, | |
245 | struct list_head *group_resv_regions) | |
246 | { | |
247 | struct iommu_resv_region *entry; | |
a514a6e2 | 248 | int ret = 0; |
6c65fb31 EA |
249 | |
250 | list_for_each_entry(entry, dev_resv_regions, list) { | |
251 | ret = iommu_insert_resv_region(entry, group_resv_regions); | |
252 | if (ret) | |
253 | break; | |
254 | } | |
255 | return ret; | |
256 | } | |
257 | ||
258 | int iommu_get_group_resv_regions(struct iommu_group *group, | |
259 | struct list_head *head) | |
260 | { | |
8d2932dd | 261 | struct group_device *device; |
6c65fb31 EA |
262 | int ret = 0; |
263 | ||
264 | mutex_lock(&group->mutex); | |
265 | list_for_each_entry(device, &group->devices, list) { | |
266 | struct list_head dev_resv_regions; | |
267 | ||
268 | INIT_LIST_HEAD(&dev_resv_regions); | |
269 | iommu_get_resv_regions(device->dev, &dev_resv_regions); | |
270 | ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); | |
271 | iommu_put_resv_regions(device->dev, &dev_resv_regions); | |
272 | if (ret) | |
273 | break; | |
274 | } | |
275 | mutex_unlock(&group->mutex); | |
276 | return ret; | |
277 | } | |
278 | EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); | |
279 | ||
bc7d12b9 EA |
280 | static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, |
281 | char *buf) | |
282 | { | |
283 | struct iommu_resv_region *region, *next; | |
284 | struct list_head group_resv_regions; | |
285 | char *str = buf; | |
286 | ||
287 | INIT_LIST_HEAD(&group_resv_regions); | |
288 | iommu_get_group_resv_regions(group, &group_resv_regions); | |
289 | ||
290 | list_for_each_entry_safe(region, next, &group_resv_regions, list) { | |
291 | str += sprintf(str, "0x%016llx 0x%016llx %s\n", | |
292 | (long long int)region->start, | |
293 | (long long int)(region->start + | |
294 | region->length - 1), | |
295 | iommu_group_resv_type_string[region->type]); | |
296 | kfree(region); | |
297 | } | |
298 | ||
299 | return (str - buf); | |
300 | } | |
301 | ||
d72e31c9 AW |
302 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
303 | ||
bc7d12b9 EA |
304 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
305 | iommu_group_show_resv_regions, NULL); | |
306 | ||
d72e31c9 AW |
307 | static void iommu_group_release(struct kobject *kobj) |
308 | { | |
309 | struct iommu_group *group = to_iommu_group(kobj); | |
310 | ||
269aa808 JR |
311 | pr_debug("Releasing group %d\n", group->id); |
312 | ||
d72e31c9 AW |
313 | if (group->iommu_data_release) |
314 | group->iommu_data_release(group->iommu_data); | |
315 | ||
feccf398 | 316 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 | 317 | |
53723dc5 JR |
318 | if (group->default_domain) |
319 | iommu_domain_free(group->default_domain); | |
320 | ||
d72e31c9 AW |
321 | kfree(group->name); |
322 | kfree(group); | |
323 | } | |
324 | ||
325 | static struct kobj_type iommu_group_ktype = { | |
326 | .sysfs_ops = &iommu_group_sysfs_ops, | |
327 | .release = iommu_group_release, | |
328 | }; | |
329 | ||
330 | /** | |
331 | * iommu_group_alloc - Allocate a new group | |
332 | * @name: Optional name to associate with group, visible in sysfs | |
333 | * | |
334 | * This function is called by an iommu driver to allocate a new iommu | |
335 | * group. The iommu group represents the minimum granularity of the iommu. | |
336 | * Upon successful return, the caller holds a reference to the supplied | |
337 | * group in order to hold the group until devices are added. Use | |
338 | * iommu_group_put() to release this extra reference count, allowing the | |
339 | * group to be automatically reclaimed once it has no devices or external | |
340 | * references. | |
341 | */ | |
342 | struct iommu_group *iommu_group_alloc(void) | |
1460432c | 343 | { |
d72e31c9 AW |
344 | struct iommu_group *group; |
345 | int ret; | |
346 | ||
347 | group = kzalloc(sizeof(*group), GFP_KERNEL); | |
348 | if (!group) | |
349 | return ERR_PTR(-ENOMEM); | |
350 | ||
351 | group->kobj.kset = iommu_group_kset; | |
352 | mutex_init(&group->mutex); | |
353 | INIT_LIST_HEAD(&group->devices); | |
354 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); | |
355 | ||
feccf398 HK |
356 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
357 | if (ret < 0) { | |
d72e31c9 | 358 | kfree(group); |
feccf398 | 359 | return ERR_PTR(ret); |
d72e31c9 | 360 | } |
feccf398 | 361 | group->id = ret; |
1460432c | 362 | |
d72e31c9 AW |
363 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
364 | NULL, "%d", group->id); | |
365 | if (ret) { | |
feccf398 | 366 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 AW |
367 | kfree(group); |
368 | return ERR_PTR(ret); | |
369 | } | |
370 | ||
371 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); | |
372 | if (!group->devices_kobj) { | |
373 | kobject_put(&group->kobj); /* triggers .release & free */ | |
374 | return ERR_PTR(-ENOMEM); | |
375 | } | |
376 | ||
377 | /* | |
378 | * The devices_kobj holds a reference on the group kobject, so | |
379 | * as long as that exists so will the group. We can therefore | |
380 | * use the devices_kobj for reference counting. | |
381 | */ | |
382 | kobject_put(&group->kobj); | |
383 | ||
bc7d12b9 EA |
384 | ret = iommu_group_create_file(group, |
385 | &iommu_group_attr_reserved_regions); | |
386 | if (ret) | |
387 | return ERR_PTR(ret); | |
388 | ||
269aa808 JR |
389 | pr_debug("Allocated group %d\n", group->id); |
390 | ||
d72e31c9 AW |
391 | return group; |
392 | } | |
393 | EXPORT_SYMBOL_GPL(iommu_group_alloc); | |
394 | ||
aa16bea9 AK |
395 | struct iommu_group *iommu_group_get_by_id(int id) |
396 | { | |
397 | struct kobject *group_kobj; | |
398 | struct iommu_group *group; | |
399 | const char *name; | |
400 | ||
401 | if (!iommu_group_kset) | |
402 | return NULL; | |
403 | ||
404 | name = kasprintf(GFP_KERNEL, "%d", id); | |
405 | if (!name) | |
406 | return NULL; | |
407 | ||
408 | group_kobj = kset_find_obj(iommu_group_kset, name); | |
409 | kfree(name); | |
410 | ||
411 | if (!group_kobj) | |
412 | return NULL; | |
413 | ||
414 | group = container_of(group_kobj, struct iommu_group, kobj); | |
415 | BUG_ON(group->id != id); | |
416 | ||
417 | kobject_get(group->devices_kobj); | |
418 | kobject_put(&group->kobj); | |
419 | ||
420 | return group; | |
421 | } | |
422 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); | |
423 | ||
d72e31c9 AW |
424 | /** |
425 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group | |
426 | * @group: the group | |
427 | * | |
428 | * iommu drivers can store data in the group for use when doing iommu | |
429 | * operations. This function provides a way to retrieve it. Caller | |
430 | * should hold a group reference. | |
431 | */ | |
432 | void *iommu_group_get_iommudata(struct iommu_group *group) | |
433 | { | |
434 | return group->iommu_data; | |
435 | } | |
436 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); | |
437 | ||
438 | /** | |
439 | * iommu_group_set_iommudata - set iommu_data for a group | |
440 | * @group: the group | |
441 | * @iommu_data: new data | |
442 | * @release: release function for iommu_data | |
443 | * | |
444 | * iommu drivers can store data in the group for use when doing iommu | |
445 | * operations. This function provides a way to set the data after | |
446 | * the group has been allocated. Caller should hold a group reference. | |
447 | */ | |
448 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, | |
449 | void (*release)(void *iommu_data)) | |
1460432c | 450 | { |
d72e31c9 AW |
451 | group->iommu_data = iommu_data; |
452 | group->iommu_data_release = release; | |
453 | } | |
454 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); | |
1460432c | 455 | |
d72e31c9 AW |
456 | /** |
457 | * iommu_group_set_name - set name for a group | |
458 | * @group: the group | |
459 | * @name: name | |
460 | * | |
461 | * Allow iommu driver to set a name for a group. When set it will | |
462 | * appear in a name attribute file under the group in sysfs. | |
463 | */ | |
464 | int iommu_group_set_name(struct iommu_group *group, const char *name) | |
465 | { | |
466 | int ret; | |
467 | ||
468 | if (group->name) { | |
469 | iommu_group_remove_file(group, &iommu_group_attr_name); | |
470 | kfree(group->name); | |
471 | group->name = NULL; | |
472 | if (!name) | |
473 | return 0; | |
474 | } | |
475 | ||
476 | group->name = kstrdup(name, GFP_KERNEL); | |
477 | if (!group->name) | |
478 | return -ENOMEM; | |
479 | ||
480 | ret = iommu_group_create_file(group, &iommu_group_attr_name); | |
481 | if (ret) { | |
482 | kfree(group->name); | |
483 | group->name = NULL; | |
484 | return ret; | |
485 | } | |
1460432c AW |
486 | |
487 | return 0; | |
488 | } | |
d72e31c9 | 489 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
1460432c | 490 | |
beed2821 JR |
491 | static int iommu_group_create_direct_mappings(struct iommu_group *group, |
492 | struct device *dev) | |
493 | { | |
494 | struct iommu_domain *domain = group->default_domain; | |
e5b5234a | 495 | struct iommu_resv_region *entry; |
beed2821 JR |
496 | struct list_head mappings; |
497 | unsigned long pg_size; | |
498 | int ret = 0; | |
499 | ||
500 | if (!domain || domain->type != IOMMU_DOMAIN_DMA) | |
501 | return 0; | |
502 | ||
d16e0faa | 503 | BUG_ON(!domain->pgsize_bitmap); |
beed2821 | 504 | |
d16e0faa | 505 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
beed2821 JR |
506 | INIT_LIST_HEAD(&mappings); |
507 | ||
e5b5234a | 508 | iommu_get_resv_regions(dev, &mappings); |
beed2821 JR |
509 | |
510 | /* We need to consider overlapping regions for different devices */ | |
511 | list_for_each_entry(entry, &mappings, list) { | |
512 | dma_addr_t start, end, addr; | |
513 | ||
e5b5234a EA |
514 | if (domain->ops->apply_resv_region) |
515 | domain->ops->apply_resv_region(dev, domain, entry); | |
33b21a6b | 516 | |
beed2821 JR |
517 | start = ALIGN(entry->start, pg_size); |
518 | end = ALIGN(entry->start + entry->length, pg_size); | |
519 | ||
544a25d9 EA |
520 | if (entry->type != IOMMU_RESV_DIRECT) |
521 | continue; | |
522 | ||
beed2821 JR |
523 | for (addr = start; addr < end; addr += pg_size) { |
524 | phys_addr_t phys_addr; | |
525 | ||
526 | phys_addr = iommu_iova_to_phys(domain, addr); | |
527 | if (phys_addr) | |
528 | continue; | |
529 | ||
530 | ret = iommu_map(domain, addr, addr, pg_size, entry->prot); | |
531 | if (ret) | |
532 | goto out; | |
533 | } | |
534 | ||
535 | } | |
536 | ||
add02cfd JR |
537 | iommu_flush_tlb_all(domain); |
538 | ||
beed2821 | 539 | out: |
e5b5234a | 540 | iommu_put_resv_regions(dev, &mappings); |
beed2821 JR |
541 | |
542 | return ret; | |
543 | } | |
544 | ||
d72e31c9 AW |
545 | /** |
546 | * iommu_group_add_device - add a device to an iommu group | |
547 | * @group: the group into which to add the device (reference should be held) | |
548 | * @dev: the device | |
549 | * | |
550 | * This function is called by an iommu driver to add a device into a | |
551 | * group. Adding a device increments the group reference count. | |
552 | */ | |
553 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) | |
1460432c | 554 | { |
d72e31c9 | 555 | int ret, i = 0; |
c09e22d5 | 556 | struct group_device *device; |
d72e31c9 AW |
557 | |
558 | device = kzalloc(sizeof(*device), GFP_KERNEL); | |
559 | if (!device) | |
560 | return -ENOMEM; | |
561 | ||
562 | device->dev = dev; | |
1460432c | 563 | |
d72e31c9 | 564 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
797a8b4d RM |
565 | if (ret) |
566 | goto err_free_device; | |
d72e31c9 AW |
567 | |
568 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); | |
569 | rename: | |
570 | if (!device->name) { | |
797a8b4d RM |
571 | ret = -ENOMEM; |
572 | goto err_remove_link; | |
d72e31c9 | 573 | } |
1460432c | 574 | |
d72e31c9 AW |
575 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
576 | &dev->kobj, device->name); | |
577 | if (ret) { | |
d72e31c9 AW |
578 | if (ret == -EEXIST && i >= 0) { |
579 | /* | |
580 | * Account for the slim chance of collision | |
581 | * and append an instance to the name. | |
582 | */ | |
797a8b4d | 583 | kfree(device->name); |
d72e31c9 AW |
584 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
585 | kobject_name(&dev->kobj), i++); | |
586 | goto rename; | |
587 | } | |
797a8b4d | 588 | goto err_free_name; |
d72e31c9 AW |
589 | } |
590 | ||
591 | kobject_get(group->devices_kobj); | |
592 | ||
593 | dev->iommu_group = group; | |
594 | ||
beed2821 JR |
595 | iommu_group_create_direct_mappings(group, dev); |
596 | ||
d72e31c9 AW |
597 | mutex_lock(&group->mutex); |
598 | list_add_tail(&device->list, &group->devices); | |
e39cb8a3 | 599 | if (group->domain) |
797a8b4d | 600 | ret = __iommu_attach_device(group->domain, dev); |
d72e31c9 | 601 | mutex_unlock(&group->mutex); |
797a8b4d RM |
602 | if (ret) |
603 | goto err_put_group; | |
d72e31c9 AW |
604 | |
605 | /* Notify any listeners about change to group. */ | |
606 | blocking_notifier_call_chain(&group->notifier, | |
607 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); | |
d1cf7e82 SK |
608 | |
609 | trace_add_device_to_group(group->id, dev); | |
269aa808 JR |
610 | |
611 | pr_info("Adding device %s to group %d\n", dev_name(dev), group->id); | |
612 | ||
1460432c | 613 | return 0; |
797a8b4d RM |
614 | |
615 | err_put_group: | |
616 | mutex_lock(&group->mutex); | |
617 | list_del(&device->list); | |
618 | mutex_unlock(&group->mutex); | |
619 | dev->iommu_group = NULL; | |
620 | kobject_put(group->devices_kobj); | |
621 | err_free_name: | |
622 | kfree(device->name); | |
623 | err_remove_link: | |
624 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
625 | err_free_device: | |
626 | kfree(device); | |
627 | pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret); | |
628 | return ret; | |
1460432c | 629 | } |
d72e31c9 | 630 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
1460432c | 631 | |
d72e31c9 AW |
632 | /** |
633 | * iommu_group_remove_device - remove a device from it's current group | |
634 | * @dev: device to be removed | |
635 | * | |
636 | * This function is called by an iommu driver to remove the device from | |
637 | * it's current group. This decrements the iommu group reference count. | |
638 | */ | |
639 | void iommu_group_remove_device(struct device *dev) | |
640 | { | |
641 | struct iommu_group *group = dev->iommu_group; | |
c09e22d5 | 642 | struct group_device *tmp_device, *device = NULL; |
d72e31c9 | 643 | |
269aa808 JR |
644 | pr_info("Removing device %s from group %d\n", dev_name(dev), group->id); |
645 | ||
d72e31c9 AW |
646 | /* Pre-notify listeners that a device is being removed. */ |
647 | blocking_notifier_call_chain(&group->notifier, | |
648 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); | |
649 | ||
650 | mutex_lock(&group->mutex); | |
651 | list_for_each_entry(tmp_device, &group->devices, list) { | |
652 | if (tmp_device->dev == dev) { | |
653 | device = tmp_device; | |
654 | list_del(&device->list); | |
655 | break; | |
656 | } | |
657 | } | |
658 | mutex_unlock(&group->mutex); | |
659 | ||
660 | if (!device) | |
661 | return; | |
662 | ||
663 | sysfs_remove_link(group->devices_kobj, device->name); | |
664 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
665 | ||
2e757086 SK |
666 | trace_remove_device_from_group(group->id, dev); |
667 | ||
d72e31c9 AW |
668 | kfree(device->name); |
669 | kfree(device); | |
670 | dev->iommu_group = NULL; | |
671 | kobject_put(group->devices_kobj); | |
672 | } | |
673 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); | |
674 | ||
426a2738 JR |
675 | static int iommu_group_device_count(struct iommu_group *group) |
676 | { | |
c09e22d5 | 677 | struct group_device *entry; |
426a2738 JR |
678 | int ret = 0; |
679 | ||
680 | list_for_each_entry(entry, &group->devices, list) | |
681 | ret++; | |
682 | ||
683 | return ret; | |
684 | } | |
685 | ||
d72e31c9 AW |
686 | /** |
687 | * iommu_group_for_each_dev - iterate over each device in the group | |
688 | * @group: the group | |
689 | * @data: caller opaque data to be passed to callback function | |
690 | * @fn: caller supplied callback function | |
691 | * | |
692 | * This function is called by group users to iterate over group devices. | |
693 | * Callers should hold a reference count to the group during callback. | |
694 | * The group->mutex is held across callbacks, which will block calls to | |
695 | * iommu_group_add/remove_device. | |
696 | */ | |
e39cb8a3 JR |
697 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
698 | int (*fn)(struct device *, void *)) | |
d72e31c9 | 699 | { |
c09e22d5 | 700 | struct group_device *device; |
d72e31c9 AW |
701 | int ret = 0; |
702 | ||
d72e31c9 AW |
703 | list_for_each_entry(device, &group->devices, list) { |
704 | ret = fn(device->dev, data); | |
705 | if (ret) | |
706 | break; | |
707 | } | |
e39cb8a3 JR |
708 | return ret; |
709 | } | |
710 | ||
711 | ||
712 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, | |
713 | int (*fn)(struct device *, void *)) | |
714 | { | |
715 | int ret; | |
716 | ||
717 | mutex_lock(&group->mutex); | |
718 | ret = __iommu_group_for_each_dev(group, data, fn); | |
d72e31c9 | 719 | mutex_unlock(&group->mutex); |
e39cb8a3 | 720 | |
d72e31c9 AW |
721 | return ret; |
722 | } | |
723 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); | |
724 | ||
725 | /** | |
726 | * iommu_group_get - Return the group for a device and increment reference | |
727 | * @dev: get the group that this device belongs to | |
728 | * | |
729 | * This function is called by iommu drivers and users to get the group | |
730 | * for the specified device. If found, the group is returned and the group | |
731 | * reference in incremented, else NULL. | |
732 | */ | |
733 | struct iommu_group *iommu_group_get(struct device *dev) | |
734 | { | |
735 | struct iommu_group *group = dev->iommu_group; | |
736 | ||
737 | if (group) | |
738 | kobject_get(group->devices_kobj); | |
739 | ||
740 | return group; | |
741 | } | |
742 | EXPORT_SYMBOL_GPL(iommu_group_get); | |
743 | ||
13f59a78 RM |
744 | /** |
745 | * iommu_group_ref_get - Increment reference on a group | |
746 | * @group: the group to use, must not be NULL | |
747 | * | |
748 | * This function is called by iommu drivers to take additional references on an | |
749 | * existing group. Returns the given group for convenience. | |
750 | */ | |
751 | struct iommu_group *iommu_group_ref_get(struct iommu_group *group) | |
752 | { | |
753 | kobject_get(group->devices_kobj); | |
754 | return group; | |
755 | } | |
756 | ||
d72e31c9 AW |
757 | /** |
758 | * iommu_group_put - Decrement group reference | |
759 | * @group: the group to use | |
760 | * | |
761 | * This function is called by iommu drivers and users to release the | |
762 | * iommu group. Once the reference count is zero, the group is released. | |
763 | */ | |
764 | void iommu_group_put(struct iommu_group *group) | |
765 | { | |
766 | if (group) | |
767 | kobject_put(group->devices_kobj); | |
768 | } | |
769 | EXPORT_SYMBOL_GPL(iommu_group_put); | |
770 | ||
771 | /** | |
772 | * iommu_group_register_notifier - Register a notifier for group changes | |
773 | * @group: the group to watch | |
774 | * @nb: notifier block to signal | |
775 | * | |
776 | * This function allows iommu group users to track changes in a group. | |
777 | * See include/linux/iommu.h for actions sent via this notifier. Caller | |
778 | * should hold a reference to the group throughout notifier registration. | |
779 | */ | |
780 | int iommu_group_register_notifier(struct iommu_group *group, | |
781 | struct notifier_block *nb) | |
782 | { | |
783 | return blocking_notifier_chain_register(&group->notifier, nb); | |
784 | } | |
785 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); | |
786 | ||
787 | /** | |
788 | * iommu_group_unregister_notifier - Unregister a notifier | |
789 | * @group: the group to watch | |
790 | * @nb: notifier block to signal | |
791 | * | |
792 | * Unregister a previously registered group notifier block. | |
793 | */ | |
794 | int iommu_group_unregister_notifier(struct iommu_group *group, | |
795 | struct notifier_block *nb) | |
796 | { | |
797 | return blocking_notifier_chain_unregister(&group->notifier, nb); | |
798 | } | |
799 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); | |
800 | ||
801 | /** | |
802 | * iommu_group_id - Return ID for a group | |
803 | * @group: the group to ID | |
804 | * | |
805 | * Return the unique ID for the group matching the sysfs group number. | |
806 | */ | |
807 | int iommu_group_id(struct iommu_group *group) | |
808 | { | |
809 | return group->id; | |
810 | } | |
811 | EXPORT_SYMBOL_GPL(iommu_group_id); | |
1460432c | 812 | |
f096c061 AW |
813 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
814 | unsigned long *devfns); | |
815 | ||
104a1c13 AW |
816 | /* |
817 | * To consider a PCI device isolated, we require ACS to support Source | |
818 | * Validation, Request Redirection, Completer Redirection, and Upstream | |
819 | * Forwarding. This effectively means that devices cannot spoof their | |
820 | * requester ID, requests and completions cannot be redirected, and all | |
821 | * transactions are forwarded upstream, even as it passes through a | |
822 | * bridge where the target device is downstream. | |
823 | */ | |
824 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) | |
825 | ||
f096c061 AW |
826 | /* |
827 | * For multifunction devices which are not isolated from each other, find | |
828 | * all the other non-isolated functions and look for existing groups. For | |
829 | * each function, we also need to look for aliases to or from other devices | |
830 | * that may already have a group. | |
831 | */ | |
832 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, | |
833 | unsigned long *devfns) | |
834 | { | |
835 | struct pci_dev *tmp = NULL; | |
836 | struct iommu_group *group; | |
837 | ||
838 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) | |
839 | return NULL; | |
840 | ||
841 | for_each_pci_dev(tmp) { | |
842 | if (tmp == pdev || tmp->bus != pdev->bus || | |
843 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || | |
844 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) | |
845 | continue; | |
846 | ||
847 | group = get_pci_alias_group(tmp, devfns); | |
848 | if (group) { | |
849 | pci_dev_put(tmp); | |
850 | return group; | |
851 | } | |
852 | } | |
853 | ||
854 | return NULL; | |
855 | } | |
856 | ||
857 | /* | |
338c3149 JL |
858 | * Look for aliases to or from the given device for existing groups. DMA |
859 | * aliases are only supported on the same bus, therefore the search | |
f096c061 AW |
860 | * space is quite small (especially since we're really only looking at pcie |
861 | * device, and therefore only expect multiple slots on the root complex or | |
862 | * downstream switch ports). It's conceivable though that a pair of | |
863 | * multifunction devices could have aliases between them that would cause a | |
864 | * loop. To prevent this, we use a bitmap to track where we've been. | |
865 | */ | |
866 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, | |
867 | unsigned long *devfns) | |
868 | { | |
869 | struct pci_dev *tmp = NULL; | |
870 | struct iommu_group *group; | |
871 | ||
872 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) | |
873 | return NULL; | |
874 | ||
875 | group = iommu_group_get(&pdev->dev); | |
876 | if (group) | |
877 | return group; | |
878 | ||
879 | for_each_pci_dev(tmp) { | |
880 | if (tmp == pdev || tmp->bus != pdev->bus) | |
881 | continue; | |
882 | ||
883 | /* We alias them or they alias us */ | |
338c3149 | 884 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
f096c061 AW |
885 | group = get_pci_alias_group(tmp, devfns); |
886 | if (group) { | |
887 | pci_dev_put(tmp); | |
888 | return group; | |
889 | } | |
890 | ||
891 | group = get_pci_function_alias_group(tmp, devfns); | |
892 | if (group) { | |
893 | pci_dev_put(tmp); | |
894 | return group; | |
895 | } | |
896 | } | |
897 | } | |
898 | ||
899 | return NULL; | |
900 | } | |
901 | ||
104a1c13 AW |
902 | struct group_for_pci_data { |
903 | struct pci_dev *pdev; | |
904 | struct iommu_group *group; | |
905 | }; | |
906 | ||
907 | /* | |
908 | * DMA alias iterator callback, return the last seen device. Stop and return | |
909 | * the IOMMU group if we find one along the way. | |
910 | */ | |
911 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) | |
912 | { | |
913 | struct group_for_pci_data *data = opaque; | |
914 | ||
915 | data->pdev = pdev; | |
916 | data->group = iommu_group_get(&pdev->dev); | |
917 | ||
918 | return data->group != NULL; | |
919 | } | |
920 | ||
6eab556a JR |
921 | /* |
922 | * Generic device_group call-back function. It just allocates one | |
923 | * iommu-group per device. | |
924 | */ | |
925 | struct iommu_group *generic_device_group(struct device *dev) | |
926 | { | |
7f7a2304 | 927 | return iommu_group_alloc(); |
6eab556a JR |
928 | } |
929 | ||
104a1c13 AW |
930 | /* |
931 | * Use standard PCI bus topology, isolation features, and DMA alias quirks | |
932 | * to find or create an IOMMU group for a device. | |
933 | */ | |
5e62292b | 934 | struct iommu_group *pci_device_group(struct device *dev) |
104a1c13 | 935 | { |
5e62292b | 936 | struct pci_dev *pdev = to_pci_dev(dev); |
104a1c13 AW |
937 | struct group_for_pci_data data; |
938 | struct pci_bus *bus; | |
939 | struct iommu_group *group = NULL; | |
f096c061 | 940 | u64 devfns[4] = { 0 }; |
104a1c13 | 941 | |
5e62292b JR |
942 | if (WARN_ON(!dev_is_pci(dev))) |
943 | return ERR_PTR(-EINVAL); | |
944 | ||
104a1c13 AW |
945 | /* |
946 | * Find the upstream DMA alias for the device. A device must not | |
947 | * be aliased due to topology in order to have its own IOMMU group. | |
948 | * If we find an alias along the way that already belongs to a | |
949 | * group, use it. | |
950 | */ | |
951 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) | |
952 | return data.group; | |
953 | ||
954 | pdev = data.pdev; | |
955 | ||
956 | /* | |
957 | * Continue upstream from the point of minimum IOMMU granularity | |
958 | * due to aliases to the point where devices are protected from | |
959 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing | |
960 | * group, use it. | |
961 | */ | |
962 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { | |
963 | if (!bus->self) | |
964 | continue; | |
965 | ||
966 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) | |
967 | break; | |
968 | ||
969 | pdev = bus->self; | |
970 | ||
971 | group = iommu_group_get(&pdev->dev); | |
972 | if (group) | |
973 | return group; | |
974 | } | |
975 | ||
976 | /* | |
f096c061 AW |
977 | * Look for existing groups on device aliases. If we alias another |
978 | * device or another device aliases us, use the same group. | |
104a1c13 | 979 | */ |
f096c061 AW |
980 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
981 | if (group) | |
982 | return group; | |
104a1c13 AW |
983 | |
984 | /* | |
f096c061 AW |
985 | * Look for existing groups on non-isolated functions on the same |
986 | * slot and aliases of those funcions, if any. No need to clear | |
987 | * the search bitmap, the tested devfns are still valid. | |
104a1c13 | 988 | */ |
f096c061 AW |
989 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
990 | if (group) | |
991 | return group; | |
104a1c13 AW |
992 | |
993 | /* No shared group found, allocate new */ | |
7f7a2304 | 994 | return iommu_group_alloc(); |
104a1c13 AW |
995 | } |
996 | ||
997 | /** | |
998 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device | |
999 | * @dev: target device | |
1000 | * | |
1001 | * This function is intended to be called by IOMMU drivers and extended to | |
1002 | * support common, bus-defined algorithms when determining or creating the | |
1003 | * IOMMU group for a device. On success, the caller will hold a reference | |
1004 | * to the returned IOMMU group, which will already include the provided | |
1005 | * device. The reference should be released with iommu_group_put(). | |
1006 | */ | |
1007 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) | |
1008 | { | |
46c6b2bc | 1009 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
c4a783b8 | 1010 | struct iommu_group *group; |
104a1c13 AW |
1011 | int ret; |
1012 | ||
1013 | group = iommu_group_get(dev); | |
1014 | if (group) | |
1015 | return group; | |
1016 | ||
05f80300 RM |
1017 | if (!ops) |
1018 | return ERR_PTR(-EINVAL); | |
104a1c13 | 1019 | |
05f80300 | 1020 | group = ops->device_group(dev); |
72dcac63 JR |
1021 | if (WARN_ON_ONCE(group == NULL)) |
1022 | return ERR_PTR(-EINVAL); | |
1023 | ||
104a1c13 AW |
1024 | if (IS_ERR(group)) |
1025 | return group; | |
1026 | ||
1228236d JR |
1027 | /* |
1028 | * Try to allocate a default domain - needs support from the | |
1029 | * IOMMU driver. | |
1030 | */ | |
1031 | if (!group->default_domain) { | |
fccb4e3b WD |
1032 | struct iommu_domain *dom; |
1033 | ||
1034 | dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); | |
1035 | if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { | |
1036 | dev_warn(dev, | |
1037 | "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", | |
1038 | iommu_def_domain_type); | |
1039 | dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); | |
1040 | } | |
1041 | ||
1042 | group->default_domain = dom; | |
eebb8034 | 1043 | if (!group->domain) |
fccb4e3b | 1044 | group->domain = dom; |
3f11a03b ZL |
1045 | |
1046 | if (dom && !iommu_dma_strict) { | |
1047 | int attr = 1; | |
1048 | iommu_domain_set_attr(dom, | |
1049 | DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, | |
1050 | &attr); | |
1051 | } | |
1228236d JR |
1052 | } |
1053 | ||
104a1c13 AW |
1054 | ret = iommu_group_add_device(group, dev); |
1055 | if (ret) { | |
1056 | iommu_group_put(group); | |
1057 | return ERR_PTR(ret); | |
1058 | } | |
1059 | ||
1060 | return group; | |
1061 | } | |
1062 | ||
6827ca83 JR |
1063 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
1064 | { | |
1065 | return group->default_domain; | |
1066 | } | |
1067 | ||
d72e31c9 | 1068 | static int add_iommu_group(struct device *dev, void *data) |
1460432c | 1069 | { |
b22f6434 TR |
1070 | struct iommu_callback_data *cb = data; |
1071 | const struct iommu_ops *ops = cb->ops; | |
38667f18 | 1072 | int ret; |
d72e31c9 AW |
1073 | |
1074 | if (!ops->add_device) | |
461bfb3f | 1075 | return 0; |
1460432c | 1076 | |
d72e31c9 AW |
1077 | WARN_ON(dev->iommu_group); |
1078 | ||
38667f18 JR |
1079 | ret = ops->add_device(dev); |
1080 | ||
1081 | /* | |
1082 | * We ignore -ENODEV errors for now, as they just mean that the | |
1083 | * device is not translated by an IOMMU. We still care about | |
1084 | * other errors and fail to initialize when they happen. | |
1085 | */ | |
1086 | if (ret == -ENODEV) | |
1087 | ret = 0; | |
1088 | ||
1089 | return ret; | |
1460432c AW |
1090 | } |
1091 | ||
8da30142 JR |
1092 | static int remove_iommu_group(struct device *dev, void *data) |
1093 | { | |
1094 | struct iommu_callback_data *cb = data; | |
1095 | const struct iommu_ops *ops = cb->ops; | |
1096 | ||
1097 | if (ops->remove_device && dev->iommu_group) | |
1098 | ops->remove_device(dev); | |
1460432c AW |
1099 | |
1100 | return 0; | |
1101 | } | |
1102 | ||
d72e31c9 AW |
1103 | static int iommu_bus_notifier(struct notifier_block *nb, |
1104 | unsigned long action, void *data) | |
1460432c AW |
1105 | { |
1106 | struct device *dev = data; | |
b22f6434 | 1107 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
d72e31c9 AW |
1108 | struct iommu_group *group; |
1109 | unsigned long group_action = 0; | |
1110 | ||
1111 | /* | |
1112 | * ADD/DEL call into iommu driver ops if provided, which may | |
1113 | * result in ADD/DEL notifiers to group->notifier | |
1114 | */ | |
1115 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
3ba8775f | 1116 | if (ops->add_device) { |
1117 | int ret; | |
1118 | ||
1119 | ret = ops->add_device(dev); | |
1120 | return (ret) ? NOTIFY_DONE : NOTIFY_OK; | |
1121 | } | |
843cb6dc | 1122 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
d72e31c9 AW |
1123 | if (ops->remove_device && dev->iommu_group) { |
1124 | ops->remove_device(dev); | |
1125 | return 0; | |
1126 | } | |
1127 | } | |
1460432c | 1128 | |
d72e31c9 AW |
1129 | /* |
1130 | * Remaining BUS_NOTIFYs get filtered and republished to the | |
1131 | * group, if anyone is listening | |
1132 | */ | |
1133 | group = iommu_group_get(dev); | |
1134 | if (!group) | |
1135 | return 0; | |
1460432c | 1136 | |
d72e31c9 AW |
1137 | switch (action) { |
1138 | case BUS_NOTIFY_BIND_DRIVER: | |
1139 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; | |
1140 | break; | |
1141 | case BUS_NOTIFY_BOUND_DRIVER: | |
1142 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; | |
1143 | break; | |
1144 | case BUS_NOTIFY_UNBIND_DRIVER: | |
1145 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; | |
1146 | break; | |
1147 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
1148 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; | |
1149 | break; | |
1150 | } | |
1460432c | 1151 | |
d72e31c9 AW |
1152 | if (group_action) |
1153 | blocking_notifier_call_chain(&group->notifier, | |
1154 | group_action, dev); | |
1460432c | 1155 | |
d72e31c9 | 1156 | iommu_group_put(group); |
1460432c AW |
1157 | return 0; |
1158 | } | |
1159 | ||
fb3e3065 | 1160 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
ff21776d | 1161 | { |
fb3e3065 MS |
1162 | int err; |
1163 | struct notifier_block *nb; | |
b22f6434 TR |
1164 | struct iommu_callback_data cb = { |
1165 | .ops = ops, | |
1166 | }; | |
1167 | ||
fb3e3065 MS |
1168 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
1169 | if (!nb) | |
1170 | return -ENOMEM; | |
1171 | ||
1172 | nb->notifier_call = iommu_bus_notifier; | |
1173 | ||
1174 | err = bus_register_notifier(bus, nb); | |
8da30142 JR |
1175 | if (err) |
1176 | goto out_free; | |
d7da6bdc HS |
1177 | |
1178 | err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group); | |
8da30142 JR |
1179 | if (err) |
1180 | goto out_err; | |
1181 | ||
d7da6bdc HS |
1182 | |
1183 | return 0; | |
8da30142 JR |
1184 | |
1185 | out_err: | |
1186 | /* Clean up */ | |
1187 | bus_for_each_dev(bus, NULL, &cb, remove_iommu_group); | |
1188 | bus_unregister_notifier(bus, nb); | |
1189 | ||
1190 | out_free: | |
1191 | kfree(nb); | |
1192 | ||
1193 | return err; | |
ff21776d | 1194 | } |
fc2100eb | 1195 | |
ff21776d JR |
1196 | /** |
1197 | * bus_set_iommu - set iommu-callbacks for the bus | |
1198 | * @bus: bus. | |
1199 | * @ops: the callbacks provided by the iommu-driver | |
1200 | * | |
1201 | * This function is called by an iommu driver to set the iommu methods | |
1202 | * used for a particular bus. Drivers for devices on that bus can use | |
1203 | * the iommu-api after these ops are registered. | |
1204 | * This special function is needed because IOMMUs are usually devices on | |
1205 | * the bus itself, so the iommu drivers are not initialized when the bus | |
1206 | * is set up. With this function the iommu-driver can set the iommu-ops | |
1207 | * afterwards. | |
1208 | */ | |
b22f6434 | 1209 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
fc2100eb | 1210 | { |
d7da6bdc HS |
1211 | int err; |
1212 | ||
ff21776d JR |
1213 | if (bus->iommu_ops != NULL) |
1214 | return -EBUSY; | |
fc2100eb | 1215 | |
ff21776d JR |
1216 | bus->iommu_ops = ops; |
1217 | ||
1218 | /* Do IOMMU specific setup for this bus-type */ | |
d7da6bdc HS |
1219 | err = iommu_bus_init(bus, ops); |
1220 | if (err) | |
1221 | bus->iommu_ops = NULL; | |
1222 | ||
1223 | return err; | |
fc2100eb | 1224 | } |
ff21776d | 1225 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 1226 | |
a1b60c1c | 1227 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 1228 | { |
94441c3b | 1229 | return bus->iommu_ops != NULL; |
fc2100eb | 1230 | } |
a1b60c1c | 1231 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 1232 | |
3c0e0ca0 JR |
1233 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
1234 | { | |
1235 | if (!bus->iommu_ops || !bus->iommu_ops->capable) | |
1236 | return false; | |
1237 | ||
1238 | return bus->iommu_ops->capable(cap); | |
1239 | } | |
1240 | EXPORT_SYMBOL_GPL(iommu_capable); | |
1241 | ||
4f3f8d9d OBC |
1242 | /** |
1243 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
1244 | * @domain: iommu domain | |
1245 | * @handler: fault handler | |
77ca2332 | 1246 | * @token: user data, will be passed back to the fault handler |
0ed6d2d2 OBC |
1247 | * |
1248 | * This function should be used by IOMMU users which want to be notified | |
1249 | * whenever an IOMMU fault happens. | |
1250 | * | |
1251 | * The fault handler itself should return 0 on success, and an appropriate | |
1252 | * error code otherwise. | |
4f3f8d9d OBC |
1253 | */ |
1254 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77ca2332 OBC |
1255 | iommu_fault_handler_t handler, |
1256 | void *token) | |
4f3f8d9d OBC |
1257 | { |
1258 | BUG_ON(!domain); | |
1259 | ||
1260 | domain->handler = handler; | |
77ca2332 | 1261 | domain->handler_token = token; |
4f3f8d9d | 1262 | } |
30bd918c | 1263 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 1264 | |
53723dc5 JR |
1265 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
1266 | unsigned type) | |
fc2100eb JR |
1267 | { |
1268 | struct iommu_domain *domain; | |
fc2100eb | 1269 | |
94441c3b | 1270 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
1271 | return NULL; |
1272 | ||
53723dc5 | 1273 | domain = bus->iommu_ops->domain_alloc(type); |
fc2100eb JR |
1274 | if (!domain) |
1275 | return NULL; | |
1276 | ||
8539c7c1 | 1277 | domain->ops = bus->iommu_ops; |
53723dc5 | 1278 | domain->type = type; |
d16e0faa RM |
1279 | /* Assume all sizes by default; the driver may override this later */ |
1280 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; | |
905d66c1 | 1281 | |
fc2100eb | 1282 | return domain; |
fc2100eb | 1283 | } |
fc2100eb | 1284 | |
53723dc5 JR |
1285 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
1286 | { | |
1287 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); | |
fc2100eb JR |
1288 | } |
1289 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
1290 | ||
1291 | void iommu_domain_free(struct iommu_domain *domain) | |
1292 | { | |
89be34a1 | 1293 | domain->ops->domain_free(domain); |
fc2100eb JR |
1294 | } |
1295 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
1296 | ||
426a2738 JR |
1297 | static int __iommu_attach_device(struct iommu_domain *domain, |
1298 | struct device *dev) | |
fc2100eb | 1299 | { |
b54db778 | 1300 | int ret; |
e01d1913 BH |
1301 | if ((domain->ops->is_attach_deferred != NULL) && |
1302 | domain->ops->is_attach_deferred(domain, dev)) | |
1303 | return 0; | |
1304 | ||
e5aa7f00 JR |
1305 | if (unlikely(domain->ops->attach_dev == NULL)) |
1306 | return -ENODEV; | |
1307 | ||
b54db778 SK |
1308 | ret = domain->ops->attach_dev(domain, dev); |
1309 | if (!ret) | |
1310 | trace_attach_device_to_domain(dev); | |
1311 | return ret; | |
fc2100eb | 1312 | } |
426a2738 JR |
1313 | |
1314 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
1315 | { | |
1316 | struct iommu_group *group; | |
1317 | int ret; | |
1318 | ||
1319 | group = iommu_group_get(dev); | |
426a2738 | 1320 | /* |
05f80300 | 1321 | * Lock the group to make sure the device-count doesn't |
426a2738 JR |
1322 | * change while we are attaching |
1323 | */ | |
1324 | mutex_lock(&group->mutex); | |
1325 | ret = -EINVAL; | |
1326 | if (iommu_group_device_count(group) != 1) | |
1327 | goto out_unlock; | |
1328 | ||
e39cb8a3 | 1329 | ret = __iommu_attach_group(domain, group); |
426a2738 JR |
1330 | |
1331 | out_unlock: | |
1332 | mutex_unlock(&group->mutex); | |
1333 | iommu_group_put(group); | |
1334 | ||
1335 | return ret; | |
1336 | } | |
fc2100eb JR |
1337 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
1338 | ||
426a2738 JR |
1339 | static void __iommu_detach_device(struct iommu_domain *domain, |
1340 | struct device *dev) | |
fc2100eb | 1341 | { |
e01d1913 BH |
1342 | if ((domain->ops->is_attach_deferred != NULL) && |
1343 | domain->ops->is_attach_deferred(domain, dev)) | |
1344 | return; | |
1345 | ||
e5aa7f00 JR |
1346 | if (unlikely(domain->ops->detach_dev == NULL)) |
1347 | return; | |
1348 | ||
1349 | domain->ops->detach_dev(domain, dev); | |
69980630 | 1350 | trace_detach_device_from_domain(dev); |
fc2100eb | 1351 | } |
426a2738 JR |
1352 | |
1353 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
1354 | { | |
1355 | struct iommu_group *group; | |
1356 | ||
1357 | group = iommu_group_get(dev); | |
426a2738 JR |
1358 | |
1359 | mutex_lock(&group->mutex); | |
1360 | if (iommu_group_device_count(group) != 1) { | |
1361 | WARN_ON(1); | |
1362 | goto out_unlock; | |
1363 | } | |
1364 | ||
e39cb8a3 | 1365 | __iommu_detach_group(domain, group); |
426a2738 JR |
1366 | |
1367 | out_unlock: | |
1368 | mutex_unlock(&group->mutex); | |
1369 | iommu_group_put(group); | |
1370 | } | |
fc2100eb JR |
1371 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
1372 | ||
2c1296d9 JR |
1373 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
1374 | { | |
1375 | struct iommu_domain *domain; | |
1376 | struct iommu_group *group; | |
1377 | ||
1378 | group = iommu_group_get(dev); | |
1464d0b1 | 1379 | if (!group) |
2c1296d9 JR |
1380 | return NULL; |
1381 | ||
1382 | domain = group->domain; | |
1383 | ||
1384 | iommu_group_put(group); | |
1385 | ||
1386 | return domain; | |
1387 | } | |
1388 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); | |
fc2100eb | 1389 | |
d72e31c9 AW |
1390 | /* |
1391 | * IOMMU groups are really the natrual working unit of the IOMMU, but | |
1392 | * the IOMMU API works on domains and devices. Bridge that gap by | |
1393 | * iterating over the devices in a group. Ideally we'd have a single | |
1394 | * device which represents the requestor ID of the group, but we also | |
1395 | * allow IOMMU drivers to create policy defined minimum sets, where | |
1396 | * the physical hardware may be able to distiguish members, but we | |
1397 | * wish to group them at a higher level (ex. untrusted multi-function | |
1398 | * PCI devices). Thus we attach each device. | |
1399 | */ | |
1400 | static int iommu_group_do_attach_device(struct device *dev, void *data) | |
1401 | { | |
1402 | struct iommu_domain *domain = data; | |
1403 | ||
426a2738 | 1404 | return __iommu_attach_device(domain, dev); |
d72e31c9 AW |
1405 | } |
1406 | ||
e39cb8a3 JR |
1407 | static int __iommu_attach_group(struct iommu_domain *domain, |
1408 | struct iommu_group *group) | |
1409 | { | |
1410 | int ret; | |
1411 | ||
1412 | if (group->default_domain && group->domain != group->default_domain) | |
1413 | return -EBUSY; | |
1414 | ||
1415 | ret = __iommu_group_for_each_dev(group, domain, | |
1416 | iommu_group_do_attach_device); | |
1417 | if (ret == 0) | |
1418 | group->domain = domain; | |
1419 | ||
1420 | return ret; | |
d72e31c9 AW |
1421 | } |
1422 | ||
1423 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) | |
1424 | { | |
e39cb8a3 JR |
1425 | int ret; |
1426 | ||
1427 | mutex_lock(&group->mutex); | |
1428 | ret = __iommu_attach_group(domain, group); | |
1429 | mutex_unlock(&group->mutex); | |
1430 | ||
1431 | return ret; | |
d72e31c9 AW |
1432 | } |
1433 | EXPORT_SYMBOL_GPL(iommu_attach_group); | |
1434 | ||
1435 | static int iommu_group_do_detach_device(struct device *dev, void *data) | |
1436 | { | |
1437 | struct iommu_domain *domain = data; | |
1438 | ||
426a2738 | 1439 | __iommu_detach_device(domain, dev); |
d72e31c9 AW |
1440 | |
1441 | return 0; | |
1442 | } | |
1443 | ||
e39cb8a3 JR |
1444 | static void __iommu_detach_group(struct iommu_domain *domain, |
1445 | struct iommu_group *group) | |
1446 | { | |
1447 | int ret; | |
1448 | ||
1449 | if (!group->default_domain) { | |
1450 | __iommu_group_for_each_dev(group, domain, | |
1451 | iommu_group_do_detach_device); | |
1452 | group->domain = NULL; | |
1453 | return; | |
1454 | } | |
1455 | ||
1456 | if (group->domain == group->default_domain) | |
1457 | return; | |
1458 | ||
1459 | /* Detach by re-attaching to the default domain */ | |
1460 | ret = __iommu_group_for_each_dev(group, group->default_domain, | |
1461 | iommu_group_do_attach_device); | |
1462 | if (ret != 0) | |
1463 | WARN_ON(1); | |
1464 | else | |
1465 | group->domain = group->default_domain; | |
1466 | } | |
1467 | ||
d72e31c9 AW |
1468 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
1469 | { | |
e39cb8a3 JR |
1470 | mutex_lock(&group->mutex); |
1471 | __iommu_detach_group(domain, group); | |
1472 | mutex_unlock(&group->mutex); | |
d72e31c9 AW |
1473 | } |
1474 | EXPORT_SYMBOL_GPL(iommu_detach_group); | |
1475 | ||
bb5547ac | 1476 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
fc2100eb | 1477 | { |
e5aa7f00 JR |
1478 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
1479 | return 0; | |
1480 | ||
1481 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
1482 | } |
1483 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 | 1484 | |
bd13969b AW |
1485 | static size_t iommu_pgsize(struct iommu_domain *domain, |
1486 | unsigned long addr_merge, size_t size) | |
1487 | { | |
1488 | unsigned int pgsize_idx; | |
1489 | size_t pgsize; | |
1490 | ||
1491 | /* Max page size that still fits into 'size' */ | |
1492 | pgsize_idx = __fls(size); | |
1493 | ||
1494 | /* need to consider alignment requirements ? */ | |
1495 | if (likely(addr_merge)) { | |
1496 | /* Max page size allowed by address */ | |
1497 | unsigned int align_pgsize_idx = __ffs(addr_merge); | |
1498 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); | |
1499 | } | |
1500 | ||
1501 | /* build a mask of acceptable page sizes */ | |
1502 | pgsize = (1UL << (pgsize_idx + 1)) - 1; | |
1503 | ||
1504 | /* throw away page sizes not supported by the hardware */ | |
d16e0faa | 1505 | pgsize &= domain->pgsize_bitmap; |
bd13969b AW |
1506 | |
1507 | /* make sure we're still sane */ | |
1508 | BUG_ON(!pgsize); | |
1509 | ||
1510 | /* pick the biggest page */ | |
1511 | pgsize_idx = __fls(pgsize); | |
1512 | pgsize = 1UL << pgsize_idx; | |
1513 | ||
1514 | return pgsize; | |
1515 | } | |
1516 | ||
cefc53c7 | 1517 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
7d3002cc | 1518 | phys_addr_t paddr, size_t size, int prot) |
cefc53c7 | 1519 | { |
7d3002cc OBC |
1520 | unsigned long orig_iova = iova; |
1521 | unsigned int min_pagesz; | |
1522 | size_t orig_size = size; | |
06bfcaa9 | 1523 | phys_addr_t orig_paddr = paddr; |
7d3002cc | 1524 | int ret = 0; |
cefc53c7 | 1525 | |
9db4ad91 | 1526 | if (unlikely(domain->ops->map == NULL || |
d16e0faa | 1527 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 | 1528 | return -ENODEV; |
cefc53c7 | 1529 | |
a10315e5 JR |
1530 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
1531 | return -EINVAL; | |
1532 | ||
7d3002cc | 1533 | /* find out the minimum page size supported */ |
d16e0faa | 1534 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1535 | |
1536 | /* | |
1537 | * both the virtual address and the physical one, as well as | |
1538 | * the size of the mapping, must be aligned (at least) to the | |
1539 | * size of the smallest page supported by the hardware | |
1540 | */ | |
1541 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { | |
abedb049 | 1542 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
6197ca82 | 1543 | iova, &paddr, size, min_pagesz); |
7d3002cc OBC |
1544 | return -EINVAL; |
1545 | } | |
1546 | ||
abedb049 | 1547 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
7d3002cc OBC |
1548 | |
1549 | while (size) { | |
bd13969b | 1550 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
7d3002cc | 1551 | |
abedb049 | 1552 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
6197ca82 | 1553 | iova, &paddr, pgsize); |
7d3002cc OBC |
1554 | |
1555 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); | |
1556 | if (ret) | |
1557 | break; | |
1558 | ||
1559 | iova += pgsize; | |
1560 | paddr += pgsize; | |
1561 | size -= pgsize; | |
1562 | } | |
1563 | ||
1564 | /* unroll mapping in case something went wrong */ | |
1565 | if (ret) | |
1566 | iommu_unmap(domain, orig_iova, orig_size - size); | |
e0be7c86 | 1567 | else |
06bfcaa9 | 1568 | trace_map(orig_iova, orig_paddr, orig_size); |
7d3002cc OBC |
1569 | |
1570 | return ret; | |
cefc53c7 JR |
1571 | } |
1572 | EXPORT_SYMBOL_GPL(iommu_map); | |
1573 | ||
add02cfd JR |
1574 | static size_t __iommu_unmap(struct iommu_domain *domain, |
1575 | unsigned long iova, size_t size, | |
1576 | bool sync) | |
cefc53c7 | 1577 | { |
add02cfd | 1578 | const struct iommu_ops *ops = domain->ops; |
7d3002cc | 1579 | size_t unmapped_page, unmapped = 0; |
6fd492fd | 1580 | unsigned long orig_iova = iova; |
add02cfd | 1581 | unsigned int min_pagesz; |
cefc53c7 | 1582 | |
add02cfd | 1583 | if (unlikely(ops->unmap == NULL || |
d16e0faa | 1584 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 JR |
1585 | return -ENODEV; |
1586 | ||
a10315e5 JR |
1587 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
1588 | return -EINVAL; | |
1589 | ||
7d3002cc | 1590 | /* find out the minimum page size supported */ |
d16e0faa | 1591 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1592 | |
1593 | /* | |
1594 | * The virtual address, as well as the size of the mapping, must be | |
1595 | * aligned (at least) to the size of the smallest page supported | |
1596 | * by the hardware | |
1597 | */ | |
1598 | if (!IS_ALIGNED(iova | size, min_pagesz)) { | |
6197ca82 JP |
1599 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
1600 | iova, size, min_pagesz); | |
7d3002cc OBC |
1601 | return -EINVAL; |
1602 | } | |
1603 | ||
6197ca82 | 1604 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
7d3002cc OBC |
1605 | |
1606 | /* | |
1607 | * Keep iterating until we either unmap 'size' bytes (or more) | |
1608 | * or we hit an area that isn't mapped. | |
1609 | */ | |
1610 | while (unmapped < size) { | |
bd13969b | 1611 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
7d3002cc | 1612 | |
add02cfd | 1613 | unmapped_page = ops->unmap(domain, iova, pgsize); |
7d3002cc OBC |
1614 | if (!unmapped_page) |
1615 | break; | |
1616 | ||
add02cfd JR |
1617 | if (sync && ops->iotlb_range_add) |
1618 | ops->iotlb_range_add(domain, iova, pgsize); | |
1619 | ||
6197ca82 JP |
1620 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
1621 | iova, unmapped_page); | |
7d3002cc OBC |
1622 | |
1623 | iova += unmapped_page; | |
1624 | unmapped += unmapped_page; | |
1625 | } | |
1626 | ||
add02cfd JR |
1627 | if (sync && ops->iotlb_sync) |
1628 | ops->iotlb_sync(domain); | |
1629 | ||
db8614d3 | 1630 | trace_unmap(orig_iova, size, unmapped); |
7d3002cc | 1631 | return unmapped; |
cefc53c7 | 1632 | } |
add02cfd JR |
1633 | |
1634 | size_t iommu_unmap(struct iommu_domain *domain, | |
1635 | unsigned long iova, size_t size) | |
1636 | { | |
1637 | return __iommu_unmap(domain, iova, size, true); | |
1638 | } | |
cefc53c7 | 1639 | EXPORT_SYMBOL_GPL(iommu_unmap); |
1460432c | 1640 | |
add02cfd JR |
1641 | size_t iommu_unmap_fast(struct iommu_domain *domain, |
1642 | unsigned long iova, size_t size) | |
1643 | { | |
1644 | return __iommu_unmap(domain, iova, size, false); | |
1645 | } | |
1646 | EXPORT_SYMBOL_GPL(iommu_unmap_fast); | |
1647 | ||
315786eb OH |
1648 | size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
1649 | struct scatterlist *sg, unsigned int nents, int prot) | |
1650 | { | |
38ec010d | 1651 | struct scatterlist *s; |
315786eb | 1652 | size_t mapped = 0; |
18f23409 | 1653 | unsigned int i, min_pagesz; |
38ec010d | 1654 | int ret; |
315786eb | 1655 | |
d16e0faa | 1656 | if (unlikely(domain->pgsize_bitmap == 0UL)) |
18f23409 | 1657 | return 0; |
315786eb | 1658 | |
d16e0faa | 1659 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
18f23409 RM |
1660 | |
1661 | for_each_sg(sg, s, nents, i) { | |
3e6110fd | 1662 | phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset; |
18f23409 RM |
1663 | |
1664 | /* | |
1665 | * We are mapping on IOMMU page boundaries, so offset within | |
1666 | * the page must be 0. However, the IOMMU may support pages | |
1667 | * smaller than PAGE_SIZE, so s->offset may still represent | |
1668 | * an offset of that boundary within the CPU page. | |
1669 | */ | |
1670 | if (!IS_ALIGNED(s->offset, min_pagesz)) | |
38ec010d JR |
1671 | goto out_err; |
1672 | ||
1673 | ret = iommu_map(domain, iova + mapped, phys, s->length, prot); | |
1674 | if (ret) | |
1675 | goto out_err; | |
1676 | ||
1677 | mapped += s->length; | |
315786eb OH |
1678 | } |
1679 | ||
1680 | return mapped; | |
38ec010d JR |
1681 | |
1682 | out_err: | |
1683 | /* undo mappings already done */ | |
1684 | iommu_unmap(domain, iova, mapped); | |
1685 | ||
1686 | return 0; | |
1687 | ||
315786eb OH |
1688 | } |
1689 | EXPORT_SYMBOL_GPL(default_iommu_map_sg); | |
d7787d57 JR |
1690 | |
1691 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, | |
80f97f0f | 1692 | phys_addr_t paddr, u64 size, int prot) |
d7787d57 JR |
1693 | { |
1694 | if (unlikely(domain->ops->domain_window_enable == NULL)) | |
1695 | return -ENODEV; | |
1696 | ||
80f97f0f VS |
1697 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
1698 | prot); | |
d7787d57 JR |
1699 | } |
1700 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); | |
1701 | ||
1702 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) | |
1703 | { | |
1704 | if (unlikely(domain->ops->domain_window_disable == NULL)) | |
1705 | return; | |
1706 | ||
1707 | return domain->ops->domain_window_disable(domain, wnd_nr); | |
1708 | } | |
1709 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); | |
1710 | ||
207c6e36 JR |
1711 | /** |
1712 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework | |
1713 | * @domain: the iommu domain where the fault has happened | |
1714 | * @dev: the device where the fault has happened | |
1715 | * @iova: the faulting address | |
1716 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) | |
1717 | * | |
1718 | * This function should be called by the low-level IOMMU implementations | |
1719 | * whenever IOMMU faults happen, to allow high-level users, that are | |
1720 | * interested in such events, to know about them. | |
1721 | * | |
1722 | * This event may be useful for several possible use cases: | |
1723 | * - mere logging of the event | |
1724 | * - dynamic TLB/PTE loading | |
1725 | * - if restarting of the faulting device is required | |
1726 | * | |
1727 | * Returns 0 on success and an appropriate error code otherwise (if dynamic | |
1728 | * PTE/TLB loading will one day be supported, implementations will be able | |
1729 | * to tell whether it succeeded or not according to this return value). | |
1730 | * | |
1731 | * Specifically, -ENOSYS is returned if a fault handler isn't installed | |
1732 | * (though fault handlers can also return -ENOSYS, in case they want to | |
1733 | * elicit the default behavior of the IOMMU drivers). | |
1734 | */ | |
1735 | int report_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
1736 | unsigned long iova, int flags) | |
1737 | { | |
1738 | int ret = -ENOSYS; | |
1739 | ||
1740 | /* | |
1741 | * if upper layers showed interest and installed a fault handler, | |
1742 | * invoke it. | |
1743 | */ | |
1744 | if (domain->handler) | |
1745 | ret = domain->handler(domain, dev, iova, flags, | |
1746 | domain->handler_token); | |
1747 | ||
1748 | trace_io_page_fault(dev, iova, flags); | |
1749 | return ret; | |
1750 | } | |
1751 | EXPORT_SYMBOL_GPL(report_iommu_fault); | |
1752 | ||
d72e31c9 | 1753 | static int __init iommu_init(void) |
1460432c | 1754 | { |
d72e31c9 AW |
1755 | iommu_group_kset = kset_create_and_add("iommu_groups", |
1756 | NULL, kernel_kobj); | |
d72e31c9 AW |
1757 | BUG_ON(!iommu_group_kset); |
1758 | ||
1759 | return 0; | |
1460432c | 1760 | } |
d7ef9995 | 1761 | core_initcall(iommu_init); |
0cd76dd1 JR |
1762 | |
1763 | int iommu_domain_get_attr(struct iommu_domain *domain, | |
1764 | enum iommu_attr attr, void *data) | |
1765 | { | |
0ff64f80 | 1766 | struct iommu_domain_geometry *geometry; |
d2e12160 | 1767 | bool *paging; |
0ff64f80 | 1768 | int ret = 0; |
69356712 | 1769 | u32 *count; |
0ff64f80 JR |
1770 | |
1771 | switch (attr) { | |
1772 | case DOMAIN_ATTR_GEOMETRY: | |
1773 | geometry = data; | |
1774 | *geometry = domain->geometry; | |
1775 | ||
d2e12160 JR |
1776 | break; |
1777 | case DOMAIN_ATTR_PAGING: | |
1778 | paging = data; | |
d16e0faa | 1779 | *paging = (domain->pgsize_bitmap != 0UL); |
69356712 JR |
1780 | break; |
1781 | case DOMAIN_ATTR_WINDOWS: | |
1782 | count = data; | |
1783 | ||
1784 | if (domain->ops->domain_get_windows != NULL) | |
1785 | *count = domain->ops->domain_get_windows(domain); | |
1786 | else | |
1787 | ret = -ENODEV; | |
1788 | ||
0ff64f80 JR |
1789 | break; |
1790 | default: | |
1791 | if (!domain->ops->domain_get_attr) | |
1792 | return -EINVAL; | |
0cd76dd1 | 1793 | |
0ff64f80 JR |
1794 | ret = domain->ops->domain_get_attr(domain, attr, data); |
1795 | } | |
1796 | ||
1797 | return ret; | |
0cd76dd1 JR |
1798 | } |
1799 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); | |
1800 | ||
1801 | int iommu_domain_set_attr(struct iommu_domain *domain, | |
1802 | enum iommu_attr attr, void *data) | |
1803 | { | |
69356712 JR |
1804 | int ret = 0; |
1805 | u32 *count; | |
1806 | ||
1807 | switch (attr) { | |
1808 | case DOMAIN_ATTR_WINDOWS: | |
1809 | count = data; | |
1810 | ||
1811 | if (domain->ops->domain_set_windows != NULL) | |
1812 | ret = domain->ops->domain_set_windows(domain, *count); | |
1813 | else | |
1814 | ret = -ENODEV; | |
1460432c | 1815 | |
69356712 JR |
1816 | break; |
1817 | default: | |
1818 | if (domain->ops->domain_set_attr == NULL) | |
1819 | return -EINVAL; | |
1820 | ||
1821 | ret = domain->ops->domain_set_attr(domain, attr, data); | |
1822 | } | |
1823 | ||
1824 | return ret; | |
1460432c | 1825 | } |
0cd76dd1 | 1826 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
a1015c2b | 1827 | |
e5b5234a | 1828 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
1829 | { |
1830 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
1831 | ||
e5b5234a EA |
1832 | if (ops && ops->get_resv_regions) |
1833 | ops->get_resv_regions(dev, list); | |
a1015c2b JR |
1834 | } |
1835 | ||
e5b5234a | 1836 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
1837 | { |
1838 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
1839 | ||
e5b5234a EA |
1840 | if (ops && ops->put_resv_regions) |
1841 | ops->put_resv_regions(dev, list); | |
a1015c2b | 1842 | } |
d290f1e7 | 1843 | |
2b20cbba | 1844 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
9d3a4de4 RM |
1845 | size_t length, int prot, |
1846 | enum iommu_resv_type type) | |
2b20cbba EA |
1847 | { |
1848 | struct iommu_resv_region *region; | |
1849 | ||
1850 | region = kzalloc(sizeof(*region), GFP_KERNEL); | |
1851 | if (!region) | |
1852 | return NULL; | |
1853 | ||
1854 | INIT_LIST_HEAD(®ion->list); | |
1855 | region->start = start; | |
1856 | region->length = length; | |
1857 | region->prot = prot; | |
1858 | region->type = type; | |
1859 | return region; | |
a1015c2b | 1860 | } |
d290f1e7 JR |
1861 | |
1862 | /* Request that a device is direct mapped by the IOMMU */ | |
1863 | int iommu_request_dm_for_dev(struct device *dev) | |
1864 | { | |
1865 | struct iommu_domain *dm_domain; | |
1866 | struct iommu_group *group; | |
1867 | int ret; | |
1868 | ||
1869 | /* Device must already be in a group before calling this function */ | |
1870 | group = iommu_group_get_for_dev(dev); | |
409e553d DC |
1871 | if (IS_ERR(group)) |
1872 | return PTR_ERR(group); | |
d290f1e7 JR |
1873 | |
1874 | mutex_lock(&group->mutex); | |
1875 | ||
1876 | /* Check if the default domain is already direct mapped */ | |
1877 | ret = 0; | |
1878 | if (group->default_domain && | |
1879 | group->default_domain->type == IOMMU_DOMAIN_IDENTITY) | |
1880 | goto out; | |
1881 | ||
1882 | /* Don't change mappings of existing devices */ | |
1883 | ret = -EBUSY; | |
1884 | if (iommu_group_device_count(group) != 1) | |
1885 | goto out; | |
1886 | ||
1887 | /* Allocate a direct mapped domain */ | |
1888 | ret = -ENOMEM; | |
1889 | dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); | |
1890 | if (!dm_domain) | |
1891 | goto out; | |
1892 | ||
1893 | /* Attach the device to the domain */ | |
1894 | ret = __iommu_attach_group(dm_domain, group); | |
1895 | if (ret) { | |
1896 | iommu_domain_free(dm_domain); | |
1897 | goto out; | |
1898 | } | |
1899 | ||
1900 | /* Make the direct mapped domain the default for this group */ | |
1901 | if (group->default_domain) | |
1902 | iommu_domain_free(group->default_domain); | |
1903 | group->default_domain = dm_domain; | |
1904 | ||
1905 | pr_info("Using direct mapping for device %s\n", dev_name(dev)); | |
1906 | ||
1907 | ret = 0; | |
1908 | out: | |
1909 | mutex_unlock(&group->mutex); | |
1910 | iommu_group_put(group); | |
1911 | ||
1912 | return ret; | |
1913 | } | |
57f98d2f | 1914 | |
534766df | 1915 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
e4f10ffe | 1916 | { |
e4f10ffe | 1917 | const struct iommu_ops *ops = NULL; |
d0f6f583 | 1918 | struct iommu_device *iommu; |
e4f10ffe | 1919 | |
d0f6f583 JR |
1920 | spin_lock(&iommu_device_lock); |
1921 | list_for_each_entry(iommu, &iommu_device_list, list) | |
1922 | if (iommu->fwnode == fwnode) { | |
1923 | ops = iommu->ops; | |
e4f10ffe LP |
1924 | break; |
1925 | } | |
d0f6f583 | 1926 | spin_unlock(&iommu_device_lock); |
e4f10ffe LP |
1927 | return ops; |
1928 | } | |
1929 | ||
57f98d2f RM |
1930 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
1931 | const struct iommu_ops *ops) | |
1932 | { | |
1933 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1934 | ||
1935 | if (fwspec) | |
1936 | return ops == fwspec->ops ? 0 : -EINVAL; | |
1937 | ||
1938 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); | |
1939 | if (!fwspec) | |
1940 | return -ENOMEM; | |
1941 | ||
1942 | of_node_get(to_of_node(iommu_fwnode)); | |
1943 | fwspec->iommu_fwnode = iommu_fwnode; | |
1944 | fwspec->ops = ops; | |
1945 | dev->iommu_fwspec = fwspec; | |
1946 | return 0; | |
1947 | } | |
1948 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); | |
1949 | ||
1950 | void iommu_fwspec_free(struct device *dev) | |
1951 | { | |
1952 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1953 | ||
1954 | if (fwspec) { | |
1955 | fwnode_handle_put(fwspec->iommu_fwnode); | |
1956 | kfree(fwspec); | |
1957 | dev->iommu_fwspec = NULL; | |
1958 | } | |
1959 | } | |
1960 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); | |
1961 | ||
1962 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) | |
1963 | { | |
1964 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; | |
1965 | size_t size; | |
1966 | int i; | |
1967 | ||
1968 | if (!fwspec) | |
1969 | return -EINVAL; | |
1970 | ||
1971 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); | |
1972 | if (size > sizeof(*fwspec)) { | |
1973 | fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); | |
1974 | if (!fwspec) | |
1975 | return -ENOMEM; | |
909111ba ZL |
1976 | |
1977 | dev->iommu_fwspec = fwspec; | |
57f98d2f RM |
1978 | } |
1979 | ||
1980 | for (i = 0; i < num_ids; i++) | |
1981 | fwspec->ids[fwspec->num_ids + i] = ids[i]; | |
1982 | ||
1983 | fwspec->num_ids += num_ids; | |
57f98d2f RM |
1984 | return 0; |
1985 | } | |
1986 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); |