]>
Commit | Line | Data |
---|---|---|
45051539 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fc2100eb JR |
2 | /* |
3 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. | |
63ce3ae8 | 4 | * Author: Joerg Roedel <jroedel@suse.de> |
fc2100eb JR |
5 | */ |
6 | ||
92e7066f | 7 | #define pr_fmt(fmt) "iommu: " fmt |
7d3002cc | 8 | |
905d66c1 | 9 | #include <linux/device.h> |
40998188 | 10 | #include <linux/kernel.h> |
fc2100eb JR |
11 | #include <linux/bug.h> |
12 | #include <linux/types.h> | |
c1af7b40 PG |
13 | #include <linux/init.h> |
14 | #include <linux/export.h> | |
60db4027 | 15 | #include <linux/slab.h> |
fc2100eb JR |
16 | #include <linux/errno.h> |
17 | #include <linux/iommu.h> | |
d72e31c9 AW |
18 | #include <linux/idr.h> |
19 | #include <linux/notifier.h> | |
20 | #include <linux/err.h> | |
104a1c13 | 21 | #include <linux/pci.h> |
f096c061 | 22 | #include <linux/bitops.h> |
57f98d2f | 23 | #include <linux/property.h> |
eab03e2a | 24 | #include <linux/fsl/mc.h> |
7f6db171 | 25 | #include <trace/events/iommu.h> |
d72e31c9 AW |
26 | |
27 | static struct kset *iommu_group_kset; | |
e38d1f13 | 28 | static DEFINE_IDA(iommu_group_ida); |
22bb182c JR |
29 | |
30 | static unsigned int iommu_def_domain_type __read_mostly; | |
68a6efe8 | 31 | static bool iommu_dma_strict __read_mostly = true; |
faf14989 | 32 | static u32 iommu_cmd_line __read_mostly; |
d72e31c9 AW |
33 | |
34 | struct iommu_group { | |
35 | struct kobject kobj; | |
36 | struct kobject *devices_kobj; | |
37 | struct list_head devices; | |
38 | struct mutex mutex; | |
39 | struct blocking_notifier_head notifier; | |
40 | void *iommu_data; | |
41 | void (*iommu_data_release)(void *iommu_data); | |
42 | char *name; | |
43 | int id; | |
53723dc5 | 44 | struct iommu_domain *default_domain; |
e39cb8a3 | 45 | struct iommu_domain *domain; |
d72e31c9 AW |
46 | }; |
47 | ||
c09e22d5 | 48 | struct group_device { |
d72e31c9 AW |
49 | struct list_head list; |
50 | struct device *dev; | |
51 | char *name; | |
52 | }; | |
53 | ||
54 | struct iommu_group_attribute { | |
55 | struct attribute attr; | |
56 | ssize_t (*show)(struct iommu_group *group, char *buf); | |
57 | ssize_t (*store)(struct iommu_group *group, | |
58 | const char *buf, size_t count); | |
59 | }; | |
60 | ||
bc7d12b9 | 61 | static const char * const iommu_group_resv_type_string[] = { |
adfd3738 EA |
62 | [IOMMU_RESV_DIRECT] = "direct", |
63 | [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable", | |
64 | [IOMMU_RESV_RESERVED] = "reserved", | |
65 | [IOMMU_RESV_MSI] = "msi", | |
66 | [IOMMU_RESV_SW_MSI] = "msi", | |
bc7d12b9 EA |
67 | }; |
68 | ||
faf14989 JR |
69 | #define IOMMU_CMD_LINE_DMA_API BIT(0) |
70 | ||
71 | static void iommu_set_cmd_line_dma_api(void) | |
72 | { | |
73 | iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; | |
74 | } | |
75 | ||
22bb182c | 76 | static bool iommu_cmd_line_dma_api(void) |
faf14989 JR |
77 | { |
78 | return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API); | |
79 | } | |
80 | ||
d72e31c9 AW |
81 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
82 | struct iommu_group_attribute iommu_group_attr_##_name = \ | |
83 | __ATTR(_name, _mode, _show, _store) | |
fc2100eb | 84 | |
d72e31c9 AW |
85 | #define to_iommu_group_attr(_attr) \ |
86 | container_of(_attr, struct iommu_group_attribute, attr) | |
87 | #define to_iommu_group(_kobj) \ | |
88 | container_of(_kobj, struct iommu_group, kobj) | |
fc2100eb | 89 | |
b0119e87 JR |
90 | static LIST_HEAD(iommu_device_list); |
91 | static DEFINE_SPINLOCK(iommu_device_lock); | |
92 | ||
5fa9e7c5 JR |
93 | /* |
94 | * Use a function instead of an array here because the domain-type is a | |
95 | * bit-field, so an array would waste memory. | |
96 | */ | |
97 | static const char *iommu_domain_type_str(unsigned int t) | |
98 | { | |
99 | switch (t) { | |
100 | case IOMMU_DOMAIN_BLOCKED: | |
101 | return "Blocked"; | |
102 | case IOMMU_DOMAIN_IDENTITY: | |
103 | return "Passthrough"; | |
104 | case IOMMU_DOMAIN_UNMANAGED: | |
105 | return "Unmanaged"; | |
106 | case IOMMU_DOMAIN_DMA: | |
107 | return "Translated"; | |
108 | default: | |
109 | return "Unknown"; | |
110 | } | |
111 | } | |
112 | ||
113 | static int __init iommu_subsys_init(void) | |
114 | { | |
22bb182c JR |
115 | bool cmd_line = iommu_cmd_line_dma_api(); |
116 | ||
117 | if (!cmd_line) { | |
118 | if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) | |
119 | iommu_set_default_passthrough(false); | |
120 | else | |
121 | iommu_set_default_translated(false); | |
122 | } | |
123 | ||
124 | pr_info("Default domain type: %s %s\n", | |
125 | iommu_domain_type_str(iommu_def_domain_type), | |
126 | cmd_line ? "(set via kernel command line)" : ""); | |
5fa9e7c5 JR |
127 | |
128 | return 0; | |
129 | } | |
130 | subsys_initcall(iommu_subsys_init); | |
131 | ||
b0119e87 JR |
132 | int iommu_device_register(struct iommu_device *iommu) |
133 | { | |
134 | spin_lock(&iommu_device_lock); | |
135 | list_add_tail(&iommu->list, &iommu_device_list); | |
136 | spin_unlock(&iommu_device_lock); | |
b0119e87 JR |
137 | return 0; |
138 | } | |
139 | ||
140 | void iommu_device_unregister(struct iommu_device *iommu) | |
141 | { | |
142 | spin_lock(&iommu_device_lock); | |
143 | list_del(&iommu->list); | |
144 | spin_unlock(&iommu_device_lock); | |
145 | } | |
146 | ||
0c830e6b JP |
147 | static struct iommu_param *iommu_get_dev_param(struct device *dev) |
148 | { | |
149 | struct iommu_param *param = dev->iommu_param; | |
150 | ||
151 | if (param) | |
152 | return param; | |
153 | ||
154 | param = kzalloc(sizeof(*param), GFP_KERNEL); | |
155 | if (!param) | |
156 | return NULL; | |
157 | ||
158 | mutex_init(¶m->lock); | |
159 | dev->iommu_param = param; | |
160 | return param; | |
161 | } | |
162 | ||
163 | static void iommu_free_dev_param(struct device *dev) | |
164 | { | |
165 | kfree(dev->iommu_param); | |
166 | dev->iommu_param = NULL; | |
167 | } | |
168 | ||
cc5aed44 JR |
169 | int iommu_probe_device(struct device *dev) |
170 | { | |
171 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
0c830e6b | 172 | int ret; |
cc5aed44 JR |
173 | |
174 | WARN_ON(dev->iommu_group); | |
0c830e6b JP |
175 | if (!ops) |
176 | return -EINVAL; | |
cc5aed44 | 177 | |
0c830e6b JP |
178 | if (!iommu_get_dev_param(dev)) |
179 | return -ENOMEM; | |
cc5aed44 | 180 | |
0c830e6b JP |
181 | ret = ops->add_device(dev); |
182 | if (ret) | |
183 | iommu_free_dev_param(dev); | |
dc9de8a2 JR |
184 | |
185 | return ret; | |
cc5aed44 JR |
186 | } |
187 | ||
188 | void iommu_release_device(struct device *dev) | |
189 | { | |
190 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
191 | ||
192 | if (dev->iommu_group) | |
193 | ops->remove_device(dev); | |
0c830e6b JP |
194 | |
195 | iommu_free_dev_param(dev); | |
cc5aed44 JR |
196 | } |
197 | ||
53723dc5 JR |
198 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
199 | unsigned type); | |
e39cb8a3 JR |
200 | static int __iommu_attach_device(struct iommu_domain *domain, |
201 | struct device *dev); | |
202 | static int __iommu_attach_group(struct iommu_domain *domain, | |
203 | struct iommu_group *group); | |
204 | static void __iommu_detach_group(struct iommu_domain *domain, | |
205 | struct iommu_group *group); | |
53723dc5 | 206 | |
fccb4e3b WD |
207 | static int __init iommu_set_def_domain_type(char *str) |
208 | { | |
209 | bool pt; | |
7f9584df | 210 | int ret; |
fccb4e3b | 211 | |
7f9584df AS |
212 | ret = kstrtobool(str, &pt); |
213 | if (ret) | |
214 | return ret; | |
fccb4e3b | 215 | |
adab0b07 JR |
216 | if (pt) |
217 | iommu_set_default_passthrough(true); | |
218 | else | |
219 | iommu_set_default_translated(true); | |
faf14989 | 220 | |
fccb4e3b WD |
221 | return 0; |
222 | } | |
223 | early_param("iommu.passthrough", iommu_set_def_domain_type); | |
224 | ||
68a6efe8 ZL |
225 | static int __init iommu_dma_setup(char *str) |
226 | { | |
227 | return kstrtobool(str, &iommu_dma_strict); | |
228 | } | |
229 | early_param("iommu.strict", iommu_dma_setup); | |
230 | ||
d72e31c9 AW |
231 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
232 | struct attribute *__attr, char *buf) | |
1460432c | 233 | { |
d72e31c9 AW |
234 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
235 | struct iommu_group *group = to_iommu_group(kobj); | |
236 | ssize_t ret = -EIO; | |
1460432c | 237 | |
d72e31c9 AW |
238 | if (attr->show) |
239 | ret = attr->show(group, buf); | |
240 | return ret; | |
241 | } | |
242 | ||
243 | static ssize_t iommu_group_attr_store(struct kobject *kobj, | |
244 | struct attribute *__attr, | |
245 | const char *buf, size_t count) | |
246 | { | |
247 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); | |
248 | struct iommu_group *group = to_iommu_group(kobj); | |
249 | ssize_t ret = -EIO; | |
1460432c | 250 | |
d72e31c9 AW |
251 | if (attr->store) |
252 | ret = attr->store(group, buf, count); | |
253 | return ret; | |
1460432c | 254 | } |
1460432c | 255 | |
d72e31c9 AW |
256 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
257 | .show = iommu_group_attr_show, | |
258 | .store = iommu_group_attr_store, | |
259 | }; | |
1460432c | 260 | |
d72e31c9 AW |
261 | static int iommu_group_create_file(struct iommu_group *group, |
262 | struct iommu_group_attribute *attr) | |
263 | { | |
264 | return sysfs_create_file(&group->kobj, &attr->attr); | |
1460432c | 265 | } |
1460432c | 266 | |
d72e31c9 AW |
267 | static void iommu_group_remove_file(struct iommu_group *group, |
268 | struct iommu_group_attribute *attr) | |
269 | { | |
270 | sysfs_remove_file(&group->kobj, &attr->attr); | |
271 | } | |
272 | ||
273 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) | |
274 | { | |
275 | return sprintf(buf, "%s\n", group->name); | |
276 | } | |
277 | ||
6c65fb31 EA |
278 | /** |
279 | * iommu_insert_resv_region - Insert a new region in the | |
280 | * list of reserved regions. | |
281 | * @new: new region to insert | |
282 | * @regions: list of regions | |
283 | * | |
284 | * The new element is sorted by address with respect to the other | |
285 | * regions of the same type. In case it overlaps with another | |
286 | * region of the same type, regions are merged. In case it | |
287 | * overlaps with another region of different type, regions are | |
288 | * not merged. | |
289 | */ | |
290 | static int iommu_insert_resv_region(struct iommu_resv_region *new, | |
291 | struct list_head *regions) | |
292 | { | |
293 | struct iommu_resv_region *region; | |
294 | phys_addr_t start = new->start; | |
295 | phys_addr_t end = new->start + new->length - 1; | |
296 | struct list_head *pos = regions->next; | |
297 | ||
298 | while (pos != regions) { | |
299 | struct iommu_resv_region *entry = | |
300 | list_entry(pos, struct iommu_resv_region, list); | |
301 | phys_addr_t a = entry->start; | |
302 | phys_addr_t b = entry->start + entry->length - 1; | |
303 | int type = entry->type; | |
304 | ||
305 | if (end < a) { | |
306 | goto insert; | |
307 | } else if (start > b) { | |
308 | pos = pos->next; | |
309 | } else if ((start >= a) && (end <= b)) { | |
310 | if (new->type == type) | |
ad0834de | 311 | return 0; |
6c65fb31 EA |
312 | else |
313 | pos = pos->next; | |
314 | } else { | |
315 | if (new->type == type) { | |
316 | phys_addr_t new_start = min(a, start); | |
317 | phys_addr_t new_end = max(b, end); | |
ad0834de | 318 | int ret; |
6c65fb31 EA |
319 | |
320 | list_del(&entry->list); | |
321 | entry->start = new_start; | |
322 | entry->length = new_end - new_start + 1; | |
ad0834de EA |
323 | ret = iommu_insert_resv_region(entry, regions); |
324 | kfree(entry); | |
325 | return ret; | |
6c65fb31 EA |
326 | } else { |
327 | pos = pos->next; | |
328 | } | |
329 | } | |
330 | } | |
331 | insert: | |
332 | region = iommu_alloc_resv_region(new->start, new->length, | |
333 | new->prot, new->type); | |
334 | if (!region) | |
335 | return -ENOMEM; | |
336 | ||
337 | list_add_tail(®ion->list, pos); | |
6c65fb31 EA |
338 | return 0; |
339 | } | |
340 | ||
341 | static int | |
342 | iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, | |
343 | struct list_head *group_resv_regions) | |
344 | { | |
345 | struct iommu_resv_region *entry; | |
a514a6e2 | 346 | int ret = 0; |
6c65fb31 EA |
347 | |
348 | list_for_each_entry(entry, dev_resv_regions, list) { | |
349 | ret = iommu_insert_resv_region(entry, group_resv_regions); | |
350 | if (ret) | |
351 | break; | |
352 | } | |
353 | return ret; | |
354 | } | |
355 | ||
356 | int iommu_get_group_resv_regions(struct iommu_group *group, | |
357 | struct list_head *head) | |
358 | { | |
8d2932dd | 359 | struct group_device *device; |
6c65fb31 EA |
360 | int ret = 0; |
361 | ||
362 | mutex_lock(&group->mutex); | |
363 | list_for_each_entry(device, &group->devices, list) { | |
364 | struct list_head dev_resv_regions; | |
365 | ||
366 | INIT_LIST_HEAD(&dev_resv_regions); | |
367 | iommu_get_resv_regions(device->dev, &dev_resv_regions); | |
368 | ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); | |
369 | iommu_put_resv_regions(device->dev, &dev_resv_regions); | |
370 | if (ret) | |
371 | break; | |
372 | } | |
373 | mutex_unlock(&group->mutex); | |
374 | return ret; | |
375 | } | |
376 | EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); | |
377 | ||
bc7d12b9 EA |
378 | static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, |
379 | char *buf) | |
380 | { | |
381 | struct iommu_resv_region *region, *next; | |
382 | struct list_head group_resv_regions; | |
383 | char *str = buf; | |
384 | ||
385 | INIT_LIST_HEAD(&group_resv_regions); | |
386 | iommu_get_group_resv_regions(group, &group_resv_regions); | |
387 | ||
388 | list_for_each_entry_safe(region, next, &group_resv_regions, list) { | |
389 | str += sprintf(str, "0x%016llx 0x%016llx %s\n", | |
390 | (long long int)region->start, | |
391 | (long long int)(region->start + | |
392 | region->length - 1), | |
393 | iommu_group_resv_type_string[region->type]); | |
394 | kfree(region); | |
395 | } | |
396 | ||
397 | return (str - buf); | |
398 | } | |
399 | ||
c52c72d3 OJ |
400 | static ssize_t iommu_group_show_type(struct iommu_group *group, |
401 | char *buf) | |
402 | { | |
403 | char *type = "unknown\n"; | |
404 | ||
405 | if (group->default_domain) { | |
406 | switch (group->default_domain->type) { | |
407 | case IOMMU_DOMAIN_BLOCKED: | |
408 | type = "blocked\n"; | |
409 | break; | |
410 | case IOMMU_DOMAIN_IDENTITY: | |
411 | type = "identity\n"; | |
412 | break; | |
413 | case IOMMU_DOMAIN_UNMANAGED: | |
414 | type = "unmanaged\n"; | |
415 | break; | |
416 | case IOMMU_DOMAIN_DMA: | |
24f307d8 | 417 | type = "DMA\n"; |
c52c72d3 OJ |
418 | break; |
419 | } | |
420 | } | |
421 | strcpy(buf, type); | |
422 | ||
423 | return strlen(type); | |
424 | } | |
425 | ||
d72e31c9 AW |
426 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
427 | ||
bc7d12b9 EA |
428 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
429 | iommu_group_show_resv_regions, NULL); | |
430 | ||
c52c72d3 OJ |
431 | static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL); |
432 | ||
d72e31c9 AW |
433 | static void iommu_group_release(struct kobject *kobj) |
434 | { | |
435 | struct iommu_group *group = to_iommu_group(kobj); | |
436 | ||
269aa808 JR |
437 | pr_debug("Releasing group %d\n", group->id); |
438 | ||
d72e31c9 AW |
439 | if (group->iommu_data_release) |
440 | group->iommu_data_release(group->iommu_data); | |
441 | ||
feccf398 | 442 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 | 443 | |
53723dc5 JR |
444 | if (group->default_domain) |
445 | iommu_domain_free(group->default_domain); | |
446 | ||
d72e31c9 AW |
447 | kfree(group->name); |
448 | kfree(group); | |
449 | } | |
450 | ||
451 | static struct kobj_type iommu_group_ktype = { | |
452 | .sysfs_ops = &iommu_group_sysfs_ops, | |
453 | .release = iommu_group_release, | |
454 | }; | |
455 | ||
456 | /** | |
457 | * iommu_group_alloc - Allocate a new group | |
d72e31c9 AW |
458 | * |
459 | * This function is called by an iommu driver to allocate a new iommu | |
460 | * group. The iommu group represents the minimum granularity of the iommu. | |
461 | * Upon successful return, the caller holds a reference to the supplied | |
462 | * group in order to hold the group until devices are added. Use | |
463 | * iommu_group_put() to release this extra reference count, allowing the | |
464 | * group to be automatically reclaimed once it has no devices or external | |
465 | * references. | |
466 | */ | |
467 | struct iommu_group *iommu_group_alloc(void) | |
1460432c | 468 | { |
d72e31c9 AW |
469 | struct iommu_group *group; |
470 | int ret; | |
471 | ||
472 | group = kzalloc(sizeof(*group), GFP_KERNEL); | |
473 | if (!group) | |
474 | return ERR_PTR(-ENOMEM); | |
475 | ||
476 | group->kobj.kset = iommu_group_kset; | |
477 | mutex_init(&group->mutex); | |
478 | INIT_LIST_HEAD(&group->devices); | |
479 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); | |
480 | ||
feccf398 HK |
481 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
482 | if (ret < 0) { | |
d72e31c9 | 483 | kfree(group); |
feccf398 | 484 | return ERR_PTR(ret); |
d72e31c9 | 485 | } |
feccf398 | 486 | group->id = ret; |
1460432c | 487 | |
d72e31c9 AW |
488 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
489 | NULL, "%d", group->id); | |
490 | if (ret) { | |
feccf398 | 491 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 AW |
492 | kfree(group); |
493 | return ERR_PTR(ret); | |
494 | } | |
495 | ||
496 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); | |
497 | if (!group->devices_kobj) { | |
498 | kobject_put(&group->kobj); /* triggers .release & free */ | |
499 | return ERR_PTR(-ENOMEM); | |
500 | } | |
501 | ||
502 | /* | |
503 | * The devices_kobj holds a reference on the group kobject, so | |
504 | * as long as that exists so will the group. We can therefore | |
505 | * use the devices_kobj for reference counting. | |
506 | */ | |
507 | kobject_put(&group->kobj); | |
508 | ||
bc7d12b9 EA |
509 | ret = iommu_group_create_file(group, |
510 | &iommu_group_attr_reserved_regions); | |
511 | if (ret) | |
512 | return ERR_PTR(ret); | |
513 | ||
c52c72d3 OJ |
514 | ret = iommu_group_create_file(group, &iommu_group_attr_type); |
515 | if (ret) | |
516 | return ERR_PTR(ret); | |
517 | ||
269aa808 JR |
518 | pr_debug("Allocated group %d\n", group->id); |
519 | ||
d72e31c9 AW |
520 | return group; |
521 | } | |
522 | EXPORT_SYMBOL_GPL(iommu_group_alloc); | |
523 | ||
aa16bea9 AK |
524 | struct iommu_group *iommu_group_get_by_id(int id) |
525 | { | |
526 | struct kobject *group_kobj; | |
527 | struct iommu_group *group; | |
528 | const char *name; | |
529 | ||
530 | if (!iommu_group_kset) | |
531 | return NULL; | |
532 | ||
533 | name = kasprintf(GFP_KERNEL, "%d", id); | |
534 | if (!name) | |
535 | return NULL; | |
536 | ||
537 | group_kobj = kset_find_obj(iommu_group_kset, name); | |
538 | kfree(name); | |
539 | ||
540 | if (!group_kobj) | |
541 | return NULL; | |
542 | ||
543 | group = container_of(group_kobj, struct iommu_group, kobj); | |
544 | BUG_ON(group->id != id); | |
545 | ||
546 | kobject_get(group->devices_kobj); | |
547 | kobject_put(&group->kobj); | |
548 | ||
549 | return group; | |
550 | } | |
551 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); | |
552 | ||
d72e31c9 AW |
553 | /** |
554 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group | |
555 | * @group: the group | |
556 | * | |
557 | * iommu drivers can store data in the group for use when doing iommu | |
558 | * operations. This function provides a way to retrieve it. Caller | |
559 | * should hold a group reference. | |
560 | */ | |
561 | void *iommu_group_get_iommudata(struct iommu_group *group) | |
562 | { | |
563 | return group->iommu_data; | |
564 | } | |
565 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); | |
566 | ||
567 | /** | |
568 | * iommu_group_set_iommudata - set iommu_data for a group | |
569 | * @group: the group | |
570 | * @iommu_data: new data | |
571 | * @release: release function for iommu_data | |
572 | * | |
573 | * iommu drivers can store data in the group for use when doing iommu | |
574 | * operations. This function provides a way to set the data after | |
575 | * the group has been allocated. Caller should hold a group reference. | |
576 | */ | |
577 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, | |
578 | void (*release)(void *iommu_data)) | |
1460432c | 579 | { |
d72e31c9 AW |
580 | group->iommu_data = iommu_data; |
581 | group->iommu_data_release = release; | |
582 | } | |
583 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); | |
1460432c | 584 | |
d72e31c9 AW |
585 | /** |
586 | * iommu_group_set_name - set name for a group | |
587 | * @group: the group | |
588 | * @name: name | |
589 | * | |
590 | * Allow iommu driver to set a name for a group. When set it will | |
591 | * appear in a name attribute file under the group in sysfs. | |
592 | */ | |
593 | int iommu_group_set_name(struct iommu_group *group, const char *name) | |
594 | { | |
595 | int ret; | |
596 | ||
597 | if (group->name) { | |
598 | iommu_group_remove_file(group, &iommu_group_attr_name); | |
599 | kfree(group->name); | |
600 | group->name = NULL; | |
601 | if (!name) | |
602 | return 0; | |
603 | } | |
604 | ||
605 | group->name = kstrdup(name, GFP_KERNEL); | |
606 | if (!group->name) | |
607 | return -ENOMEM; | |
608 | ||
609 | ret = iommu_group_create_file(group, &iommu_group_attr_name); | |
610 | if (ret) { | |
611 | kfree(group->name); | |
612 | group->name = NULL; | |
613 | return ret; | |
614 | } | |
1460432c AW |
615 | |
616 | return 0; | |
617 | } | |
d72e31c9 | 618 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
1460432c | 619 | |
beed2821 JR |
620 | static int iommu_group_create_direct_mappings(struct iommu_group *group, |
621 | struct device *dev) | |
622 | { | |
623 | struct iommu_domain *domain = group->default_domain; | |
e5b5234a | 624 | struct iommu_resv_region *entry; |
beed2821 JR |
625 | struct list_head mappings; |
626 | unsigned long pg_size; | |
627 | int ret = 0; | |
628 | ||
629 | if (!domain || domain->type != IOMMU_DOMAIN_DMA) | |
630 | return 0; | |
631 | ||
d16e0faa | 632 | BUG_ON(!domain->pgsize_bitmap); |
beed2821 | 633 | |
d16e0faa | 634 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
beed2821 JR |
635 | INIT_LIST_HEAD(&mappings); |
636 | ||
e5b5234a | 637 | iommu_get_resv_regions(dev, &mappings); |
beed2821 JR |
638 | |
639 | /* We need to consider overlapping regions for different devices */ | |
640 | list_for_each_entry(entry, &mappings, list) { | |
641 | dma_addr_t start, end, addr; | |
642 | ||
e5b5234a EA |
643 | if (domain->ops->apply_resv_region) |
644 | domain->ops->apply_resv_region(dev, domain, entry); | |
33b21a6b | 645 | |
beed2821 JR |
646 | start = ALIGN(entry->start, pg_size); |
647 | end = ALIGN(entry->start + entry->length, pg_size); | |
648 | ||
adfd3738 EA |
649 | if (entry->type != IOMMU_RESV_DIRECT && |
650 | entry->type != IOMMU_RESV_DIRECT_RELAXABLE) | |
544a25d9 EA |
651 | continue; |
652 | ||
beed2821 JR |
653 | for (addr = start; addr < end; addr += pg_size) { |
654 | phys_addr_t phys_addr; | |
655 | ||
656 | phys_addr = iommu_iova_to_phys(domain, addr); | |
657 | if (phys_addr) | |
658 | continue; | |
659 | ||
660 | ret = iommu_map(domain, addr, addr, pg_size, entry->prot); | |
661 | if (ret) | |
662 | goto out; | |
663 | } | |
664 | ||
665 | } | |
666 | ||
add02cfd JR |
667 | iommu_flush_tlb_all(domain); |
668 | ||
beed2821 | 669 | out: |
e5b5234a | 670 | iommu_put_resv_regions(dev, &mappings); |
beed2821 JR |
671 | |
672 | return ret; | |
673 | } | |
674 | ||
d72e31c9 AW |
675 | /** |
676 | * iommu_group_add_device - add a device to an iommu group | |
677 | * @group: the group into which to add the device (reference should be held) | |
678 | * @dev: the device | |
679 | * | |
680 | * This function is called by an iommu driver to add a device into a | |
681 | * group. Adding a device increments the group reference count. | |
682 | */ | |
683 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) | |
1460432c | 684 | { |
d72e31c9 | 685 | int ret, i = 0; |
c09e22d5 | 686 | struct group_device *device; |
d72e31c9 AW |
687 | |
688 | device = kzalloc(sizeof(*device), GFP_KERNEL); | |
689 | if (!device) | |
690 | return -ENOMEM; | |
691 | ||
692 | device->dev = dev; | |
1460432c | 693 | |
d72e31c9 | 694 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
797a8b4d RM |
695 | if (ret) |
696 | goto err_free_device; | |
d72e31c9 AW |
697 | |
698 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); | |
699 | rename: | |
700 | if (!device->name) { | |
797a8b4d RM |
701 | ret = -ENOMEM; |
702 | goto err_remove_link; | |
d72e31c9 | 703 | } |
1460432c | 704 | |
d72e31c9 AW |
705 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
706 | &dev->kobj, device->name); | |
707 | if (ret) { | |
d72e31c9 AW |
708 | if (ret == -EEXIST && i >= 0) { |
709 | /* | |
710 | * Account for the slim chance of collision | |
711 | * and append an instance to the name. | |
712 | */ | |
797a8b4d | 713 | kfree(device->name); |
d72e31c9 AW |
714 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
715 | kobject_name(&dev->kobj), i++); | |
716 | goto rename; | |
717 | } | |
797a8b4d | 718 | goto err_free_name; |
d72e31c9 AW |
719 | } |
720 | ||
721 | kobject_get(group->devices_kobj); | |
722 | ||
723 | dev->iommu_group = group; | |
724 | ||
beed2821 JR |
725 | iommu_group_create_direct_mappings(group, dev); |
726 | ||
d72e31c9 AW |
727 | mutex_lock(&group->mutex); |
728 | list_add_tail(&device->list, &group->devices); | |
e39cb8a3 | 729 | if (group->domain) |
797a8b4d | 730 | ret = __iommu_attach_device(group->domain, dev); |
d72e31c9 | 731 | mutex_unlock(&group->mutex); |
797a8b4d RM |
732 | if (ret) |
733 | goto err_put_group; | |
d72e31c9 AW |
734 | |
735 | /* Notify any listeners about change to group. */ | |
736 | blocking_notifier_call_chain(&group->notifier, | |
737 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); | |
d1cf7e82 SK |
738 | |
739 | trace_add_device_to_group(group->id, dev); | |
269aa808 | 740 | |
780da9e4 | 741 | dev_info(dev, "Adding to iommu group %d\n", group->id); |
269aa808 | 742 | |
1460432c | 743 | return 0; |
797a8b4d RM |
744 | |
745 | err_put_group: | |
746 | mutex_lock(&group->mutex); | |
747 | list_del(&device->list); | |
748 | mutex_unlock(&group->mutex); | |
749 | dev->iommu_group = NULL; | |
750 | kobject_put(group->devices_kobj); | |
751 | err_free_name: | |
752 | kfree(device->name); | |
753 | err_remove_link: | |
754 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
755 | err_free_device: | |
756 | kfree(device); | |
780da9e4 | 757 | dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); |
797a8b4d | 758 | return ret; |
1460432c | 759 | } |
d72e31c9 | 760 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
1460432c | 761 | |
d72e31c9 AW |
762 | /** |
763 | * iommu_group_remove_device - remove a device from it's current group | |
764 | * @dev: device to be removed | |
765 | * | |
766 | * This function is called by an iommu driver to remove the device from | |
767 | * it's current group. This decrements the iommu group reference count. | |
768 | */ | |
769 | void iommu_group_remove_device(struct device *dev) | |
770 | { | |
771 | struct iommu_group *group = dev->iommu_group; | |
c09e22d5 | 772 | struct group_device *tmp_device, *device = NULL; |
d72e31c9 | 773 | |
780da9e4 | 774 | dev_info(dev, "Removing from iommu group %d\n", group->id); |
269aa808 | 775 | |
d72e31c9 AW |
776 | /* Pre-notify listeners that a device is being removed. */ |
777 | blocking_notifier_call_chain(&group->notifier, | |
778 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); | |
779 | ||
780 | mutex_lock(&group->mutex); | |
781 | list_for_each_entry(tmp_device, &group->devices, list) { | |
782 | if (tmp_device->dev == dev) { | |
783 | device = tmp_device; | |
784 | list_del(&device->list); | |
785 | break; | |
786 | } | |
787 | } | |
788 | mutex_unlock(&group->mutex); | |
789 | ||
790 | if (!device) | |
791 | return; | |
792 | ||
793 | sysfs_remove_link(group->devices_kobj, device->name); | |
794 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
795 | ||
2e757086 SK |
796 | trace_remove_device_from_group(group->id, dev); |
797 | ||
d72e31c9 AW |
798 | kfree(device->name); |
799 | kfree(device); | |
800 | dev->iommu_group = NULL; | |
801 | kobject_put(group->devices_kobj); | |
802 | } | |
803 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); | |
804 | ||
426a2738 JR |
805 | static int iommu_group_device_count(struct iommu_group *group) |
806 | { | |
c09e22d5 | 807 | struct group_device *entry; |
426a2738 JR |
808 | int ret = 0; |
809 | ||
810 | list_for_each_entry(entry, &group->devices, list) | |
811 | ret++; | |
812 | ||
813 | return ret; | |
814 | } | |
815 | ||
d72e31c9 AW |
816 | /** |
817 | * iommu_group_for_each_dev - iterate over each device in the group | |
818 | * @group: the group | |
819 | * @data: caller opaque data to be passed to callback function | |
820 | * @fn: caller supplied callback function | |
821 | * | |
822 | * This function is called by group users to iterate over group devices. | |
823 | * Callers should hold a reference count to the group during callback. | |
824 | * The group->mutex is held across callbacks, which will block calls to | |
825 | * iommu_group_add/remove_device. | |
826 | */ | |
e39cb8a3 JR |
827 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
828 | int (*fn)(struct device *, void *)) | |
d72e31c9 | 829 | { |
c09e22d5 | 830 | struct group_device *device; |
d72e31c9 AW |
831 | int ret = 0; |
832 | ||
d72e31c9 AW |
833 | list_for_each_entry(device, &group->devices, list) { |
834 | ret = fn(device->dev, data); | |
835 | if (ret) | |
836 | break; | |
837 | } | |
e39cb8a3 JR |
838 | return ret; |
839 | } | |
840 | ||
841 | ||
842 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, | |
843 | int (*fn)(struct device *, void *)) | |
844 | { | |
845 | int ret; | |
846 | ||
847 | mutex_lock(&group->mutex); | |
848 | ret = __iommu_group_for_each_dev(group, data, fn); | |
d72e31c9 | 849 | mutex_unlock(&group->mutex); |
e39cb8a3 | 850 | |
d72e31c9 AW |
851 | return ret; |
852 | } | |
853 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); | |
854 | ||
855 | /** | |
856 | * iommu_group_get - Return the group for a device and increment reference | |
857 | * @dev: get the group that this device belongs to | |
858 | * | |
859 | * This function is called by iommu drivers and users to get the group | |
860 | * for the specified device. If found, the group is returned and the group | |
861 | * reference in incremented, else NULL. | |
862 | */ | |
863 | struct iommu_group *iommu_group_get(struct device *dev) | |
864 | { | |
865 | struct iommu_group *group = dev->iommu_group; | |
866 | ||
867 | if (group) | |
868 | kobject_get(group->devices_kobj); | |
869 | ||
870 | return group; | |
871 | } | |
872 | EXPORT_SYMBOL_GPL(iommu_group_get); | |
873 | ||
13f59a78 RM |
874 | /** |
875 | * iommu_group_ref_get - Increment reference on a group | |
876 | * @group: the group to use, must not be NULL | |
877 | * | |
878 | * This function is called by iommu drivers to take additional references on an | |
879 | * existing group. Returns the given group for convenience. | |
880 | */ | |
881 | struct iommu_group *iommu_group_ref_get(struct iommu_group *group) | |
882 | { | |
883 | kobject_get(group->devices_kobj); | |
884 | return group; | |
885 | } | |
886 | ||
d72e31c9 AW |
887 | /** |
888 | * iommu_group_put - Decrement group reference | |
889 | * @group: the group to use | |
890 | * | |
891 | * This function is called by iommu drivers and users to release the | |
892 | * iommu group. Once the reference count is zero, the group is released. | |
893 | */ | |
894 | void iommu_group_put(struct iommu_group *group) | |
895 | { | |
896 | if (group) | |
897 | kobject_put(group->devices_kobj); | |
898 | } | |
899 | EXPORT_SYMBOL_GPL(iommu_group_put); | |
900 | ||
901 | /** | |
902 | * iommu_group_register_notifier - Register a notifier for group changes | |
903 | * @group: the group to watch | |
904 | * @nb: notifier block to signal | |
905 | * | |
906 | * This function allows iommu group users to track changes in a group. | |
907 | * See include/linux/iommu.h for actions sent via this notifier. Caller | |
908 | * should hold a reference to the group throughout notifier registration. | |
909 | */ | |
910 | int iommu_group_register_notifier(struct iommu_group *group, | |
911 | struct notifier_block *nb) | |
912 | { | |
913 | return blocking_notifier_chain_register(&group->notifier, nb); | |
914 | } | |
915 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); | |
916 | ||
917 | /** | |
918 | * iommu_group_unregister_notifier - Unregister a notifier | |
919 | * @group: the group to watch | |
920 | * @nb: notifier block to signal | |
921 | * | |
922 | * Unregister a previously registered group notifier block. | |
923 | */ | |
924 | int iommu_group_unregister_notifier(struct iommu_group *group, | |
925 | struct notifier_block *nb) | |
926 | { | |
927 | return blocking_notifier_chain_unregister(&group->notifier, nb); | |
928 | } | |
929 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); | |
930 | ||
0c830e6b JP |
931 | /** |
932 | * iommu_register_device_fault_handler() - Register a device fault handler | |
933 | * @dev: the device | |
934 | * @handler: the fault handler | |
935 | * @data: private data passed as argument to the handler | |
936 | * | |
937 | * When an IOMMU fault event is received, this handler gets called with the | |
bf3255b3 JPB |
938 | * fault event and data as argument. The handler should return 0 on success. If |
939 | * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also | |
940 | * complete the fault by calling iommu_page_response() with one of the following | |
941 | * response code: | |
942 | * - IOMMU_PAGE_RESP_SUCCESS: retry the translation | |
943 | * - IOMMU_PAGE_RESP_INVALID: terminate the fault | |
944 | * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting | |
945 | * page faults if possible. | |
0c830e6b JP |
946 | * |
947 | * Return 0 if the fault handler was installed successfully, or an error. | |
948 | */ | |
949 | int iommu_register_device_fault_handler(struct device *dev, | |
950 | iommu_dev_fault_handler_t handler, | |
951 | void *data) | |
952 | { | |
953 | struct iommu_param *param = dev->iommu_param; | |
954 | int ret = 0; | |
955 | ||
956 | if (!param) | |
957 | return -EINVAL; | |
958 | ||
959 | mutex_lock(¶m->lock); | |
960 | /* Only allow one fault handler registered for each device */ | |
961 | if (param->fault_param) { | |
962 | ret = -EBUSY; | |
963 | goto done_unlock; | |
964 | } | |
965 | ||
966 | get_device(dev); | |
967 | param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL); | |
968 | if (!param->fault_param) { | |
969 | put_device(dev); | |
970 | ret = -ENOMEM; | |
971 | goto done_unlock; | |
972 | } | |
973 | param->fault_param->handler = handler; | |
974 | param->fault_param->data = data; | |
bf3255b3 JPB |
975 | mutex_init(¶m->fault_param->lock); |
976 | INIT_LIST_HEAD(¶m->fault_param->faults); | |
0c830e6b JP |
977 | |
978 | done_unlock: | |
979 | mutex_unlock(¶m->lock); | |
980 | ||
981 | return ret; | |
982 | } | |
983 | EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler); | |
984 | ||
985 | /** | |
986 | * iommu_unregister_device_fault_handler() - Unregister the device fault handler | |
987 | * @dev: the device | |
988 | * | |
989 | * Remove the device fault handler installed with | |
990 | * iommu_register_device_fault_handler(). | |
991 | * | |
992 | * Return 0 on success, or an error. | |
993 | */ | |
994 | int iommu_unregister_device_fault_handler(struct device *dev) | |
995 | { | |
996 | struct iommu_param *param = dev->iommu_param; | |
997 | int ret = 0; | |
998 | ||
999 | if (!param) | |
1000 | return -EINVAL; | |
1001 | ||
1002 | mutex_lock(¶m->lock); | |
1003 | ||
1004 | if (!param->fault_param) | |
1005 | goto unlock; | |
1006 | ||
bf3255b3 JPB |
1007 | /* we cannot unregister handler if there are pending faults */ |
1008 | if (!list_empty(¶m->fault_param->faults)) { | |
1009 | ret = -EBUSY; | |
1010 | goto unlock; | |
1011 | } | |
1012 | ||
0c830e6b JP |
1013 | kfree(param->fault_param); |
1014 | param->fault_param = NULL; | |
1015 | put_device(dev); | |
1016 | unlock: | |
1017 | mutex_unlock(¶m->lock); | |
1018 | ||
1019 | return ret; | |
1020 | } | |
1021 | EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler); | |
1022 | ||
1023 | /** | |
1024 | * iommu_report_device_fault() - Report fault event to device driver | |
1025 | * @dev: the device | |
1026 | * @evt: fault event data | |
1027 | * | |
1028 | * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ | |
bf3255b3 JPB |
1029 | * handler. When this function fails and the fault is recoverable, it is the |
1030 | * caller's responsibility to complete the fault. | |
0c830e6b JP |
1031 | * |
1032 | * Return 0 on success, or an error. | |
1033 | */ | |
1034 | int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt) | |
1035 | { | |
1036 | struct iommu_param *param = dev->iommu_param; | |
bf3255b3 | 1037 | struct iommu_fault_event *evt_pending = NULL; |
0c830e6b JP |
1038 | struct iommu_fault_param *fparam; |
1039 | int ret = 0; | |
1040 | ||
1041 | if (!param || !evt) | |
1042 | return -EINVAL; | |
1043 | ||
1044 | /* we only report device fault if there is a handler registered */ | |
1045 | mutex_lock(¶m->lock); | |
1046 | fparam = param->fault_param; | |
1047 | if (!fparam || !fparam->handler) { | |
1048 | ret = -EINVAL; | |
1049 | goto done_unlock; | |
1050 | } | |
bf3255b3 JPB |
1051 | |
1052 | if (evt->fault.type == IOMMU_FAULT_PAGE_REQ && | |
1053 | (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) { | |
1054 | evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event), | |
1055 | GFP_KERNEL); | |
1056 | if (!evt_pending) { | |
1057 | ret = -ENOMEM; | |
1058 | goto done_unlock; | |
1059 | } | |
1060 | mutex_lock(&fparam->lock); | |
1061 | list_add_tail(&evt_pending->list, &fparam->faults); | |
1062 | mutex_unlock(&fparam->lock); | |
1063 | } | |
1064 | ||
0c830e6b | 1065 | ret = fparam->handler(&evt->fault, fparam->data); |
bf3255b3 JPB |
1066 | if (ret && evt_pending) { |
1067 | mutex_lock(&fparam->lock); | |
1068 | list_del(&evt_pending->list); | |
1069 | mutex_unlock(&fparam->lock); | |
1070 | kfree(evt_pending); | |
1071 | } | |
0c830e6b JP |
1072 | done_unlock: |
1073 | mutex_unlock(¶m->lock); | |
1074 | return ret; | |
1075 | } | |
1076 | EXPORT_SYMBOL_GPL(iommu_report_device_fault); | |
1077 | ||
bf3255b3 JPB |
1078 | int iommu_page_response(struct device *dev, |
1079 | struct iommu_page_response *msg) | |
1080 | { | |
1081 | bool pasid_valid; | |
1082 | int ret = -EINVAL; | |
1083 | struct iommu_fault_event *evt; | |
1084 | struct iommu_fault_page_request *prm; | |
1085 | struct iommu_param *param = dev->iommu_param; | |
1086 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); | |
1087 | ||
1088 | if (!domain || !domain->ops->page_response) | |
1089 | return -ENODEV; | |
1090 | ||
1091 | if (!param || !param->fault_param) | |
1092 | return -EINVAL; | |
1093 | ||
1094 | if (msg->version != IOMMU_PAGE_RESP_VERSION_1 || | |
1095 | msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID) | |
1096 | return -EINVAL; | |
1097 | ||
1098 | /* Only send response if there is a fault report pending */ | |
1099 | mutex_lock(¶m->fault_param->lock); | |
1100 | if (list_empty(¶m->fault_param->faults)) { | |
1101 | dev_warn_ratelimited(dev, "no pending PRQ, drop response\n"); | |
1102 | goto done_unlock; | |
1103 | } | |
1104 | /* | |
1105 | * Check if we have a matching page request pending to respond, | |
1106 | * otherwise return -EINVAL | |
1107 | */ | |
1108 | list_for_each_entry(evt, ¶m->fault_param->faults, list) { | |
1109 | prm = &evt->fault.prm; | |
1110 | pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; | |
1111 | ||
1112 | if ((pasid_valid && prm->pasid != msg->pasid) || | |
1113 | prm->grpid != msg->grpid) | |
1114 | continue; | |
1115 | ||
1116 | /* Sanitize the reply */ | |
1117 | msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0; | |
1118 | ||
1119 | ret = domain->ops->page_response(dev, evt, msg); | |
1120 | list_del(&evt->list); | |
1121 | kfree(evt); | |
1122 | break; | |
1123 | } | |
1124 | ||
1125 | done_unlock: | |
1126 | mutex_unlock(¶m->fault_param->lock); | |
1127 | return ret; | |
1128 | } | |
1129 | EXPORT_SYMBOL_GPL(iommu_page_response); | |
1130 | ||
d72e31c9 AW |
1131 | /** |
1132 | * iommu_group_id - Return ID for a group | |
1133 | * @group: the group to ID | |
1134 | * | |
1135 | * Return the unique ID for the group matching the sysfs group number. | |
1136 | */ | |
1137 | int iommu_group_id(struct iommu_group *group) | |
1138 | { | |
1139 | return group->id; | |
1140 | } | |
1141 | EXPORT_SYMBOL_GPL(iommu_group_id); | |
1460432c | 1142 | |
f096c061 AW |
1143 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
1144 | unsigned long *devfns); | |
1145 | ||
104a1c13 AW |
1146 | /* |
1147 | * To consider a PCI device isolated, we require ACS to support Source | |
1148 | * Validation, Request Redirection, Completer Redirection, and Upstream | |
1149 | * Forwarding. This effectively means that devices cannot spoof their | |
1150 | * requester ID, requests and completions cannot be redirected, and all | |
1151 | * transactions are forwarded upstream, even as it passes through a | |
1152 | * bridge where the target device is downstream. | |
1153 | */ | |
1154 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) | |
1155 | ||
f096c061 AW |
1156 | /* |
1157 | * For multifunction devices which are not isolated from each other, find | |
1158 | * all the other non-isolated functions and look for existing groups. For | |
1159 | * each function, we also need to look for aliases to or from other devices | |
1160 | * that may already have a group. | |
1161 | */ | |
1162 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, | |
1163 | unsigned long *devfns) | |
1164 | { | |
1165 | struct pci_dev *tmp = NULL; | |
1166 | struct iommu_group *group; | |
1167 | ||
1168 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) | |
1169 | return NULL; | |
1170 | ||
1171 | for_each_pci_dev(tmp) { | |
1172 | if (tmp == pdev || tmp->bus != pdev->bus || | |
1173 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || | |
1174 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) | |
1175 | continue; | |
1176 | ||
1177 | group = get_pci_alias_group(tmp, devfns); | |
1178 | if (group) { | |
1179 | pci_dev_put(tmp); | |
1180 | return group; | |
1181 | } | |
1182 | } | |
1183 | ||
1184 | return NULL; | |
1185 | } | |
1186 | ||
1187 | /* | |
338c3149 JL |
1188 | * Look for aliases to or from the given device for existing groups. DMA |
1189 | * aliases are only supported on the same bus, therefore the search | |
f096c061 AW |
1190 | * space is quite small (especially since we're really only looking at pcie |
1191 | * device, and therefore only expect multiple slots on the root complex or | |
1192 | * downstream switch ports). It's conceivable though that a pair of | |
1193 | * multifunction devices could have aliases between them that would cause a | |
1194 | * loop. To prevent this, we use a bitmap to track where we've been. | |
1195 | */ | |
1196 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, | |
1197 | unsigned long *devfns) | |
1198 | { | |
1199 | struct pci_dev *tmp = NULL; | |
1200 | struct iommu_group *group; | |
1201 | ||
1202 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) | |
1203 | return NULL; | |
1204 | ||
1205 | group = iommu_group_get(&pdev->dev); | |
1206 | if (group) | |
1207 | return group; | |
1208 | ||
1209 | for_each_pci_dev(tmp) { | |
1210 | if (tmp == pdev || tmp->bus != pdev->bus) | |
1211 | continue; | |
1212 | ||
1213 | /* We alias them or they alias us */ | |
338c3149 | 1214 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
f096c061 AW |
1215 | group = get_pci_alias_group(tmp, devfns); |
1216 | if (group) { | |
1217 | pci_dev_put(tmp); | |
1218 | return group; | |
1219 | } | |
1220 | ||
1221 | group = get_pci_function_alias_group(tmp, devfns); | |
1222 | if (group) { | |
1223 | pci_dev_put(tmp); | |
1224 | return group; | |
1225 | } | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | return NULL; | |
1230 | } | |
1231 | ||
104a1c13 AW |
1232 | struct group_for_pci_data { |
1233 | struct pci_dev *pdev; | |
1234 | struct iommu_group *group; | |
1235 | }; | |
1236 | ||
1237 | /* | |
1238 | * DMA alias iterator callback, return the last seen device. Stop and return | |
1239 | * the IOMMU group if we find one along the way. | |
1240 | */ | |
1241 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) | |
1242 | { | |
1243 | struct group_for_pci_data *data = opaque; | |
1244 | ||
1245 | data->pdev = pdev; | |
1246 | data->group = iommu_group_get(&pdev->dev); | |
1247 | ||
1248 | return data->group != NULL; | |
1249 | } | |
1250 | ||
6eab556a JR |
1251 | /* |
1252 | * Generic device_group call-back function. It just allocates one | |
1253 | * iommu-group per device. | |
1254 | */ | |
1255 | struct iommu_group *generic_device_group(struct device *dev) | |
1256 | { | |
7f7a2304 | 1257 | return iommu_group_alloc(); |
6eab556a JR |
1258 | } |
1259 | ||
104a1c13 AW |
1260 | /* |
1261 | * Use standard PCI bus topology, isolation features, and DMA alias quirks | |
1262 | * to find or create an IOMMU group for a device. | |
1263 | */ | |
5e62292b | 1264 | struct iommu_group *pci_device_group(struct device *dev) |
104a1c13 | 1265 | { |
5e62292b | 1266 | struct pci_dev *pdev = to_pci_dev(dev); |
104a1c13 AW |
1267 | struct group_for_pci_data data; |
1268 | struct pci_bus *bus; | |
1269 | struct iommu_group *group = NULL; | |
f096c061 | 1270 | u64 devfns[4] = { 0 }; |
104a1c13 | 1271 | |
5e62292b JR |
1272 | if (WARN_ON(!dev_is_pci(dev))) |
1273 | return ERR_PTR(-EINVAL); | |
1274 | ||
104a1c13 AW |
1275 | /* |
1276 | * Find the upstream DMA alias for the device. A device must not | |
1277 | * be aliased due to topology in order to have its own IOMMU group. | |
1278 | * If we find an alias along the way that already belongs to a | |
1279 | * group, use it. | |
1280 | */ | |
1281 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) | |
1282 | return data.group; | |
1283 | ||
1284 | pdev = data.pdev; | |
1285 | ||
1286 | /* | |
1287 | * Continue upstream from the point of minimum IOMMU granularity | |
1288 | * due to aliases to the point where devices are protected from | |
1289 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing | |
1290 | * group, use it. | |
1291 | */ | |
1292 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { | |
1293 | if (!bus->self) | |
1294 | continue; | |
1295 | ||
1296 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) | |
1297 | break; | |
1298 | ||
1299 | pdev = bus->self; | |
1300 | ||
1301 | group = iommu_group_get(&pdev->dev); | |
1302 | if (group) | |
1303 | return group; | |
1304 | } | |
1305 | ||
1306 | /* | |
f096c061 AW |
1307 | * Look for existing groups on device aliases. If we alias another |
1308 | * device or another device aliases us, use the same group. | |
104a1c13 | 1309 | */ |
f096c061 AW |
1310 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
1311 | if (group) | |
1312 | return group; | |
104a1c13 AW |
1313 | |
1314 | /* | |
f096c061 AW |
1315 | * Look for existing groups on non-isolated functions on the same |
1316 | * slot and aliases of those funcions, if any. No need to clear | |
1317 | * the search bitmap, the tested devfns are still valid. | |
104a1c13 | 1318 | */ |
f096c061 AW |
1319 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
1320 | if (group) | |
1321 | return group; | |
104a1c13 AW |
1322 | |
1323 | /* No shared group found, allocate new */ | |
7f7a2304 | 1324 | return iommu_group_alloc(); |
104a1c13 AW |
1325 | } |
1326 | ||
eab03e2a NG |
1327 | /* Get the IOMMU group for device on fsl-mc bus */ |
1328 | struct iommu_group *fsl_mc_device_group(struct device *dev) | |
1329 | { | |
1330 | struct device *cont_dev = fsl_mc_cont_dev(dev); | |
1331 | struct iommu_group *group; | |
1332 | ||
1333 | group = iommu_group_get(cont_dev); | |
1334 | if (!group) | |
1335 | group = iommu_group_alloc(); | |
1336 | return group; | |
1337 | } | |
1338 | ||
104a1c13 AW |
1339 | /** |
1340 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device | |
1341 | * @dev: target device | |
1342 | * | |
1343 | * This function is intended to be called by IOMMU drivers and extended to | |
1344 | * support common, bus-defined algorithms when determining or creating the | |
1345 | * IOMMU group for a device. On success, the caller will hold a reference | |
1346 | * to the returned IOMMU group, which will already include the provided | |
1347 | * device. The reference should be released with iommu_group_put(). | |
1348 | */ | |
1349 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) | |
1350 | { | |
46c6b2bc | 1351 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
c4a783b8 | 1352 | struct iommu_group *group; |
104a1c13 AW |
1353 | int ret; |
1354 | ||
1355 | group = iommu_group_get(dev); | |
1356 | if (group) | |
1357 | return group; | |
1358 | ||
05f80300 RM |
1359 | if (!ops) |
1360 | return ERR_PTR(-EINVAL); | |
104a1c13 | 1361 | |
05f80300 | 1362 | group = ops->device_group(dev); |
72dcac63 JR |
1363 | if (WARN_ON_ONCE(group == NULL)) |
1364 | return ERR_PTR(-EINVAL); | |
1365 | ||
104a1c13 AW |
1366 | if (IS_ERR(group)) |
1367 | return group; | |
1368 | ||
1228236d JR |
1369 | /* |
1370 | * Try to allocate a default domain - needs support from the | |
1371 | * IOMMU driver. | |
1372 | */ | |
1373 | if (!group->default_domain) { | |
fccb4e3b WD |
1374 | struct iommu_domain *dom; |
1375 | ||
1376 | dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); | |
1377 | if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { | |
fccb4e3b | 1378 | dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); |
8bc32a28 JR |
1379 | if (dom) { |
1380 | dev_warn(dev, | |
1381 | "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", | |
1382 | iommu_def_domain_type); | |
1383 | } | |
fccb4e3b WD |
1384 | } |
1385 | ||
1386 | group->default_domain = dom; | |
eebb8034 | 1387 | if (!group->domain) |
fccb4e3b | 1388 | group->domain = dom; |
68a6efe8 ZL |
1389 | |
1390 | if (dom && !iommu_dma_strict) { | |
1391 | int attr = 1; | |
1392 | iommu_domain_set_attr(dom, | |
1393 | DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, | |
1394 | &attr); | |
1395 | } | |
1228236d JR |
1396 | } |
1397 | ||
104a1c13 AW |
1398 | ret = iommu_group_add_device(group, dev); |
1399 | if (ret) { | |
1400 | iommu_group_put(group); | |
1401 | return ERR_PTR(ret); | |
1402 | } | |
1403 | ||
1404 | return group; | |
1405 | } | |
1406 | ||
6827ca83 JR |
1407 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
1408 | { | |
1409 | return group->default_domain; | |
1410 | } | |
1411 | ||
d72e31c9 | 1412 | static int add_iommu_group(struct device *dev, void *data) |
1460432c | 1413 | { |
cc5aed44 | 1414 | int ret = iommu_probe_device(dev); |
38667f18 JR |
1415 | |
1416 | /* | |
1417 | * We ignore -ENODEV errors for now, as they just mean that the | |
1418 | * device is not translated by an IOMMU. We still care about | |
1419 | * other errors and fail to initialize when they happen. | |
1420 | */ | |
1421 | if (ret == -ENODEV) | |
1422 | ret = 0; | |
1423 | ||
1424 | return ret; | |
1460432c AW |
1425 | } |
1426 | ||
8da30142 JR |
1427 | static int remove_iommu_group(struct device *dev, void *data) |
1428 | { | |
cc5aed44 | 1429 | iommu_release_device(dev); |
1460432c AW |
1430 | |
1431 | return 0; | |
1432 | } | |
1433 | ||
d72e31c9 AW |
1434 | static int iommu_bus_notifier(struct notifier_block *nb, |
1435 | unsigned long action, void *data) | |
1460432c | 1436 | { |
cc5aed44 | 1437 | unsigned long group_action = 0; |
1460432c | 1438 | struct device *dev = data; |
d72e31c9 | 1439 | struct iommu_group *group; |
d72e31c9 AW |
1440 | |
1441 | /* | |
1442 | * ADD/DEL call into iommu driver ops if provided, which may | |
1443 | * result in ADD/DEL notifiers to group->notifier | |
1444 | */ | |
1445 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
cc5aed44 | 1446 | int ret; |
3ba8775f | 1447 | |
cc5aed44 JR |
1448 | ret = iommu_probe_device(dev); |
1449 | return (ret) ? NOTIFY_DONE : NOTIFY_OK; | |
843cb6dc | 1450 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
cc5aed44 JR |
1451 | iommu_release_device(dev); |
1452 | return NOTIFY_OK; | |
d72e31c9 | 1453 | } |
1460432c | 1454 | |
d72e31c9 AW |
1455 | /* |
1456 | * Remaining BUS_NOTIFYs get filtered and republished to the | |
1457 | * group, if anyone is listening | |
1458 | */ | |
1459 | group = iommu_group_get(dev); | |
1460 | if (!group) | |
1461 | return 0; | |
1460432c | 1462 | |
d72e31c9 AW |
1463 | switch (action) { |
1464 | case BUS_NOTIFY_BIND_DRIVER: | |
1465 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; | |
1466 | break; | |
1467 | case BUS_NOTIFY_BOUND_DRIVER: | |
1468 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; | |
1469 | break; | |
1470 | case BUS_NOTIFY_UNBIND_DRIVER: | |
1471 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; | |
1472 | break; | |
1473 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
1474 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; | |
1475 | break; | |
1476 | } | |
1460432c | 1477 | |
d72e31c9 AW |
1478 | if (group_action) |
1479 | blocking_notifier_call_chain(&group->notifier, | |
1480 | group_action, dev); | |
1460432c | 1481 | |
d72e31c9 | 1482 | iommu_group_put(group); |
1460432c AW |
1483 | return 0; |
1484 | } | |
1485 | ||
fb3e3065 | 1486 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
ff21776d | 1487 | { |
fb3e3065 MS |
1488 | int err; |
1489 | struct notifier_block *nb; | |
b22f6434 | 1490 | |
fb3e3065 MS |
1491 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
1492 | if (!nb) | |
1493 | return -ENOMEM; | |
1494 | ||
1495 | nb->notifier_call = iommu_bus_notifier; | |
1496 | ||
1497 | err = bus_register_notifier(bus, nb); | |
8da30142 JR |
1498 | if (err) |
1499 | goto out_free; | |
d7da6bdc | 1500 | |
8cec63e5 | 1501 | err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group); |
8da30142 JR |
1502 | if (err) |
1503 | goto out_err; | |
1504 | ||
d7da6bdc HS |
1505 | |
1506 | return 0; | |
8da30142 JR |
1507 | |
1508 | out_err: | |
1509 | /* Clean up */ | |
8cec63e5 | 1510 | bus_for_each_dev(bus, NULL, NULL, remove_iommu_group); |
8da30142 JR |
1511 | bus_unregister_notifier(bus, nb); |
1512 | ||
1513 | out_free: | |
1514 | kfree(nb); | |
1515 | ||
1516 | return err; | |
ff21776d | 1517 | } |
fc2100eb | 1518 | |
ff21776d JR |
1519 | /** |
1520 | * bus_set_iommu - set iommu-callbacks for the bus | |
1521 | * @bus: bus. | |
1522 | * @ops: the callbacks provided by the iommu-driver | |
1523 | * | |
1524 | * This function is called by an iommu driver to set the iommu methods | |
1525 | * used for a particular bus. Drivers for devices on that bus can use | |
1526 | * the iommu-api after these ops are registered. | |
1527 | * This special function is needed because IOMMUs are usually devices on | |
1528 | * the bus itself, so the iommu drivers are not initialized when the bus | |
1529 | * is set up. With this function the iommu-driver can set the iommu-ops | |
1530 | * afterwards. | |
1531 | */ | |
b22f6434 | 1532 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
fc2100eb | 1533 | { |
d7da6bdc HS |
1534 | int err; |
1535 | ||
ff21776d JR |
1536 | if (bus->iommu_ops != NULL) |
1537 | return -EBUSY; | |
fc2100eb | 1538 | |
ff21776d JR |
1539 | bus->iommu_ops = ops; |
1540 | ||
1541 | /* Do IOMMU specific setup for this bus-type */ | |
d7da6bdc HS |
1542 | err = iommu_bus_init(bus, ops); |
1543 | if (err) | |
1544 | bus->iommu_ops = NULL; | |
1545 | ||
1546 | return err; | |
fc2100eb | 1547 | } |
ff21776d | 1548 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 1549 | |
a1b60c1c | 1550 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 1551 | { |
94441c3b | 1552 | return bus->iommu_ops != NULL; |
fc2100eb | 1553 | } |
a1b60c1c | 1554 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 1555 | |
3c0e0ca0 JR |
1556 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
1557 | { | |
1558 | if (!bus->iommu_ops || !bus->iommu_ops->capable) | |
1559 | return false; | |
1560 | ||
1561 | return bus->iommu_ops->capable(cap); | |
1562 | } | |
1563 | EXPORT_SYMBOL_GPL(iommu_capable); | |
1564 | ||
4f3f8d9d OBC |
1565 | /** |
1566 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
1567 | * @domain: iommu domain | |
1568 | * @handler: fault handler | |
77ca2332 | 1569 | * @token: user data, will be passed back to the fault handler |
0ed6d2d2 OBC |
1570 | * |
1571 | * This function should be used by IOMMU users which want to be notified | |
1572 | * whenever an IOMMU fault happens. | |
1573 | * | |
1574 | * The fault handler itself should return 0 on success, and an appropriate | |
1575 | * error code otherwise. | |
4f3f8d9d OBC |
1576 | */ |
1577 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77ca2332 OBC |
1578 | iommu_fault_handler_t handler, |
1579 | void *token) | |
4f3f8d9d OBC |
1580 | { |
1581 | BUG_ON(!domain); | |
1582 | ||
1583 | domain->handler = handler; | |
77ca2332 | 1584 | domain->handler_token = token; |
4f3f8d9d | 1585 | } |
30bd918c | 1586 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 1587 | |
53723dc5 JR |
1588 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
1589 | unsigned type) | |
fc2100eb JR |
1590 | { |
1591 | struct iommu_domain *domain; | |
fc2100eb | 1592 | |
94441c3b | 1593 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
1594 | return NULL; |
1595 | ||
53723dc5 | 1596 | domain = bus->iommu_ops->domain_alloc(type); |
fc2100eb JR |
1597 | if (!domain) |
1598 | return NULL; | |
1599 | ||
8539c7c1 | 1600 | domain->ops = bus->iommu_ops; |
53723dc5 | 1601 | domain->type = type; |
d16e0faa RM |
1602 | /* Assume all sizes by default; the driver may override this later */ |
1603 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; | |
905d66c1 | 1604 | |
fc2100eb | 1605 | return domain; |
fc2100eb | 1606 | } |
fc2100eb | 1607 | |
53723dc5 JR |
1608 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
1609 | { | |
1610 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); | |
fc2100eb JR |
1611 | } |
1612 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
1613 | ||
1614 | void iommu_domain_free(struct iommu_domain *domain) | |
1615 | { | |
89be34a1 | 1616 | domain->ops->domain_free(domain); |
fc2100eb JR |
1617 | } |
1618 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
1619 | ||
426a2738 JR |
1620 | static int __iommu_attach_device(struct iommu_domain *domain, |
1621 | struct device *dev) | |
fc2100eb | 1622 | { |
b54db778 | 1623 | int ret; |
e01d1913 BH |
1624 | if ((domain->ops->is_attach_deferred != NULL) && |
1625 | domain->ops->is_attach_deferred(domain, dev)) | |
1626 | return 0; | |
1627 | ||
e5aa7f00 JR |
1628 | if (unlikely(domain->ops->attach_dev == NULL)) |
1629 | return -ENODEV; | |
1630 | ||
b54db778 SK |
1631 | ret = domain->ops->attach_dev(domain, dev); |
1632 | if (!ret) | |
1633 | trace_attach_device_to_domain(dev); | |
1634 | return ret; | |
fc2100eb | 1635 | } |
426a2738 JR |
1636 | |
1637 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
1638 | { | |
1639 | struct iommu_group *group; | |
1640 | int ret; | |
1641 | ||
1642 | group = iommu_group_get(dev); | |
9ae9df03 JC |
1643 | if (!group) |
1644 | return -ENODEV; | |
1645 | ||
426a2738 | 1646 | /* |
05f80300 | 1647 | * Lock the group to make sure the device-count doesn't |
426a2738 JR |
1648 | * change while we are attaching |
1649 | */ | |
1650 | mutex_lock(&group->mutex); | |
1651 | ret = -EINVAL; | |
1652 | if (iommu_group_device_count(group) != 1) | |
1653 | goto out_unlock; | |
1654 | ||
e39cb8a3 | 1655 | ret = __iommu_attach_group(domain, group); |
426a2738 JR |
1656 | |
1657 | out_unlock: | |
1658 | mutex_unlock(&group->mutex); | |
1659 | iommu_group_put(group); | |
1660 | ||
1661 | return ret; | |
1662 | } | |
fc2100eb JR |
1663 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
1664 | ||
426a2738 JR |
1665 | static void __iommu_detach_device(struct iommu_domain *domain, |
1666 | struct device *dev) | |
fc2100eb | 1667 | { |
e01d1913 BH |
1668 | if ((domain->ops->is_attach_deferred != NULL) && |
1669 | domain->ops->is_attach_deferred(domain, dev)) | |
1670 | return; | |
1671 | ||
e5aa7f00 JR |
1672 | if (unlikely(domain->ops->detach_dev == NULL)) |
1673 | return; | |
1674 | ||
1675 | domain->ops->detach_dev(domain, dev); | |
69980630 | 1676 | trace_detach_device_from_domain(dev); |
fc2100eb | 1677 | } |
426a2738 JR |
1678 | |
1679 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
1680 | { | |
1681 | struct iommu_group *group; | |
1682 | ||
1683 | group = iommu_group_get(dev); | |
9ae9df03 JC |
1684 | if (!group) |
1685 | return; | |
426a2738 JR |
1686 | |
1687 | mutex_lock(&group->mutex); | |
1688 | if (iommu_group_device_count(group) != 1) { | |
1689 | WARN_ON(1); | |
1690 | goto out_unlock; | |
1691 | } | |
1692 | ||
e39cb8a3 | 1693 | __iommu_detach_group(domain, group); |
426a2738 JR |
1694 | |
1695 | out_unlock: | |
1696 | mutex_unlock(&group->mutex); | |
1697 | iommu_group_put(group); | |
1698 | } | |
fc2100eb JR |
1699 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
1700 | ||
2c1296d9 JR |
1701 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
1702 | { | |
1703 | struct iommu_domain *domain; | |
1704 | struct iommu_group *group; | |
1705 | ||
1706 | group = iommu_group_get(dev); | |
1464d0b1 | 1707 | if (!group) |
2c1296d9 JR |
1708 | return NULL; |
1709 | ||
1710 | domain = group->domain; | |
1711 | ||
1712 | iommu_group_put(group); | |
1713 | ||
1714 | return domain; | |
1715 | } | |
1716 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); | |
fc2100eb | 1717 | |
d72e31c9 | 1718 | /* |
6af588fe RM |
1719 | * For IOMMU_DOMAIN_DMA implementations which already provide their own |
1720 | * guarantees that the group and its default domain are valid and correct. | |
1721 | */ | |
1722 | struct iommu_domain *iommu_get_dma_domain(struct device *dev) | |
1723 | { | |
1724 | return dev->iommu_group->default_domain; | |
1725 | } | |
1726 | ||
d72e31c9 | 1727 | /* |
35449adc | 1728 | * IOMMU groups are really the natural working unit of the IOMMU, but |
d72e31c9 AW |
1729 | * the IOMMU API works on domains and devices. Bridge that gap by |
1730 | * iterating over the devices in a group. Ideally we'd have a single | |
1731 | * device which represents the requestor ID of the group, but we also | |
1732 | * allow IOMMU drivers to create policy defined minimum sets, where | |
1733 | * the physical hardware may be able to distiguish members, but we | |
1734 | * wish to group them at a higher level (ex. untrusted multi-function | |
1735 | * PCI devices). Thus we attach each device. | |
1736 | */ | |
1737 | static int iommu_group_do_attach_device(struct device *dev, void *data) | |
1738 | { | |
1739 | struct iommu_domain *domain = data; | |
1740 | ||
426a2738 | 1741 | return __iommu_attach_device(domain, dev); |
d72e31c9 AW |
1742 | } |
1743 | ||
e39cb8a3 JR |
1744 | static int __iommu_attach_group(struct iommu_domain *domain, |
1745 | struct iommu_group *group) | |
1746 | { | |
1747 | int ret; | |
1748 | ||
1749 | if (group->default_domain && group->domain != group->default_domain) | |
1750 | return -EBUSY; | |
1751 | ||
1752 | ret = __iommu_group_for_each_dev(group, domain, | |
1753 | iommu_group_do_attach_device); | |
1754 | if (ret == 0) | |
1755 | group->domain = domain; | |
1756 | ||
1757 | return ret; | |
d72e31c9 AW |
1758 | } |
1759 | ||
1760 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) | |
1761 | { | |
e39cb8a3 JR |
1762 | int ret; |
1763 | ||
1764 | mutex_lock(&group->mutex); | |
1765 | ret = __iommu_attach_group(domain, group); | |
1766 | mutex_unlock(&group->mutex); | |
1767 | ||
1768 | return ret; | |
d72e31c9 AW |
1769 | } |
1770 | EXPORT_SYMBOL_GPL(iommu_attach_group); | |
1771 | ||
1772 | static int iommu_group_do_detach_device(struct device *dev, void *data) | |
1773 | { | |
1774 | struct iommu_domain *domain = data; | |
1775 | ||
426a2738 | 1776 | __iommu_detach_device(domain, dev); |
d72e31c9 AW |
1777 | |
1778 | return 0; | |
1779 | } | |
1780 | ||
e39cb8a3 JR |
1781 | static void __iommu_detach_group(struct iommu_domain *domain, |
1782 | struct iommu_group *group) | |
1783 | { | |
1784 | int ret; | |
1785 | ||
1786 | if (!group->default_domain) { | |
1787 | __iommu_group_for_each_dev(group, domain, | |
1788 | iommu_group_do_detach_device); | |
1789 | group->domain = NULL; | |
1790 | return; | |
1791 | } | |
1792 | ||
1793 | if (group->domain == group->default_domain) | |
1794 | return; | |
1795 | ||
1796 | /* Detach by re-attaching to the default domain */ | |
1797 | ret = __iommu_group_for_each_dev(group, group->default_domain, | |
1798 | iommu_group_do_attach_device); | |
1799 | if (ret != 0) | |
1800 | WARN_ON(1); | |
1801 | else | |
1802 | group->domain = group->default_domain; | |
1803 | } | |
1804 | ||
d72e31c9 AW |
1805 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
1806 | { | |
e39cb8a3 JR |
1807 | mutex_lock(&group->mutex); |
1808 | __iommu_detach_group(domain, group); | |
1809 | mutex_unlock(&group->mutex); | |
d72e31c9 AW |
1810 | } |
1811 | EXPORT_SYMBOL_GPL(iommu_detach_group); | |
1812 | ||
bb5547ac | 1813 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
fc2100eb | 1814 | { |
e5aa7f00 JR |
1815 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
1816 | return 0; | |
1817 | ||
1818 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
1819 | } |
1820 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 | 1821 | |
bd13969b AW |
1822 | static size_t iommu_pgsize(struct iommu_domain *domain, |
1823 | unsigned long addr_merge, size_t size) | |
1824 | { | |
1825 | unsigned int pgsize_idx; | |
1826 | size_t pgsize; | |
1827 | ||
1828 | /* Max page size that still fits into 'size' */ | |
1829 | pgsize_idx = __fls(size); | |
1830 | ||
1831 | /* need to consider alignment requirements ? */ | |
1832 | if (likely(addr_merge)) { | |
1833 | /* Max page size allowed by address */ | |
1834 | unsigned int align_pgsize_idx = __ffs(addr_merge); | |
1835 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); | |
1836 | } | |
1837 | ||
1838 | /* build a mask of acceptable page sizes */ | |
1839 | pgsize = (1UL << (pgsize_idx + 1)) - 1; | |
1840 | ||
1841 | /* throw away page sizes not supported by the hardware */ | |
d16e0faa | 1842 | pgsize &= domain->pgsize_bitmap; |
bd13969b AW |
1843 | |
1844 | /* make sure we're still sane */ | |
1845 | BUG_ON(!pgsize); | |
1846 | ||
1847 | /* pick the biggest page */ | |
1848 | pgsize_idx = __fls(pgsize); | |
1849 | pgsize = 1UL << pgsize_idx; | |
1850 | ||
1851 | return pgsize; | |
1852 | } | |
1853 | ||
cefc53c7 | 1854 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
7d3002cc | 1855 | phys_addr_t paddr, size_t size, int prot) |
cefc53c7 | 1856 | { |
1d7ae53b | 1857 | const struct iommu_ops *ops = domain->ops; |
7d3002cc OBC |
1858 | unsigned long orig_iova = iova; |
1859 | unsigned int min_pagesz; | |
1860 | size_t orig_size = size; | |
06bfcaa9 | 1861 | phys_addr_t orig_paddr = paddr; |
7d3002cc | 1862 | int ret = 0; |
cefc53c7 | 1863 | |
1d7ae53b | 1864 | if (unlikely(ops->map == NULL || |
d16e0faa | 1865 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 | 1866 | return -ENODEV; |
cefc53c7 | 1867 | |
a10315e5 JR |
1868 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
1869 | return -EINVAL; | |
1870 | ||
7d3002cc | 1871 | /* find out the minimum page size supported */ |
d16e0faa | 1872 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1873 | |
1874 | /* | |
1875 | * both the virtual address and the physical one, as well as | |
1876 | * the size of the mapping, must be aligned (at least) to the | |
1877 | * size of the smallest page supported by the hardware | |
1878 | */ | |
1879 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { | |
abedb049 | 1880 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
6197ca82 | 1881 | iova, &paddr, size, min_pagesz); |
7d3002cc OBC |
1882 | return -EINVAL; |
1883 | } | |
1884 | ||
abedb049 | 1885 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
7d3002cc OBC |
1886 | |
1887 | while (size) { | |
bd13969b | 1888 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
7d3002cc | 1889 | |
abedb049 | 1890 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
6197ca82 | 1891 | iova, &paddr, pgsize); |
7d3002cc | 1892 | |
1d7ae53b | 1893 | ret = ops->map(domain, iova, paddr, pgsize, prot); |
7d3002cc OBC |
1894 | if (ret) |
1895 | break; | |
1896 | ||
1897 | iova += pgsize; | |
1898 | paddr += pgsize; | |
1899 | size -= pgsize; | |
1900 | } | |
1901 | ||
1d7ae53b DO |
1902 | if (ops->iotlb_sync_map) |
1903 | ops->iotlb_sync_map(domain); | |
1904 | ||
7d3002cc OBC |
1905 | /* unroll mapping in case something went wrong */ |
1906 | if (ret) | |
1907 | iommu_unmap(domain, orig_iova, orig_size - size); | |
e0be7c86 | 1908 | else |
06bfcaa9 | 1909 | trace_map(orig_iova, orig_paddr, orig_size); |
7d3002cc OBC |
1910 | |
1911 | return ret; | |
cefc53c7 JR |
1912 | } |
1913 | EXPORT_SYMBOL_GPL(iommu_map); | |
1914 | ||
add02cfd JR |
1915 | static size_t __iommu_unmap(struct iommu_domain *domain, |
1916 | unsigned long iova, size_t size, | |
a7d20dc1 | 1917 | struct iommu_iotlb_gather *iotlb_gather) |
cefc53c7 | 1918 | { |
add02cfd | 1919 | const struct iommu_ops *ops = domain->ops; |
7d3002cc | 1920 | size_t unmapped_page, unmapped = 0; |
6fd492fd | 1921 | unsigned long orig_iova = iova; |
add02cfd | 1922 | unsigned int min_pagesz; |
cefc53c7 | 1923 | |
add02cfd | 1924 | if (unlikely(ops->unmap == NULL || |
d16e0faa | 1925 | domain->pgsize_bitmap == 0UL)) |
c5611a87 | 1926 | return 0; |
e5aa7f00 | 1927 | |
a10315e5 | 1928 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
c5611a87 | 1929 | return 0; |
a10315e5 | 1930 | |
7d3002cc | 1931 | /* find out the minimum page size supported */ |
d16e0faa | 1932 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
1933 | |
1934 | /* | |
1935 | * The virtual address, as well as the size of the mapping, must be | |
1936 | * aligned (at least) to the size of the smallest page supported | |
1937 | * by the hardware | |
1938 | */ | |
1939 | if (!IS_ALIGNED(iova | size, min_pagesz)) { | |
6197ca82 JP |
1940 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
1941 | iova, size, min_pagesz); | |
c5611a87 | 1942 | return 0; |
7d3002cc OBC |
1943 | } |
1944 | ||
6197ca82 | 1945 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
7d3002cc OBC |
1946 | |
1947 | /* | |
1948 | * Keep iterating until we either unmap 'size' bytes (or more) | |
1949 | * or we hit an area that isn't mapped. | |
1950 | */ | |
1951 | while (unmapped < size) { | |
bd13969b | 1952 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
7d3002cc | 1953 | |
56f8af5e | 1954 | unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather); |
7d3002cc OBC |
1955 | if (!unmapped_page) |
1956 | break; | |
1957 | ||
6197ca82 JP |
1958 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
1959 | iova, unmapped_page); | |
7d3002cc OBC |
1960 | |
1961 | iova += unmapped_page; | |
1962 | unmapped += unmapped_page; | |
1963 | } | |
1964 | ||
db8614d3 | 1965 | trace_unmap(orig_iova, size, unmapped); |
7d3002cc | 1966 | return unmapped; |
cefc53c7 | 1967 | } |
add02cfd JR |
1968 | |
1969 | size_t iommu_unmap(struct iommu_domain *domain, | |
1970 | unsigned long iova, size_t size) | |
1971 | { | |
a7d20dc1 WD |
1972 | struct iommu_iotlb_gather iotlb_gather; |
1973 | size_t ret; | |
1974 | ||
1975 | iommu_iotlb_gather_init(&iotlb_gather); | |
1976 | ret = __iommu_unmap(domain, iova, size, &iotlb_gather); | |
1977 | iommu_tlb_sync(domain, &iotlb_gather); | |
1978 | ||
1979 | return ret; | |
add02cfd | 1980 | } |
cefc53c7 | 1981 | EXPORT_SYMBOL_GPL(iommu_unmap); |
1460432c | 1982 | |
add02cfd | 1983 | size_t iommu_unmap_fast(struct iommu_domain *domain, |
a7d20dc1 WD |
1984 | unsigned long iova, size_t size, |
1985 | struct iommu_iotlb_gather *iotlb_gather) | |
add02cfd | 1986 | { |
a7d20dc1 | 1987 | return __iommu_unmap(domain, iova, size, iotlb_gather); |
add02cfd JR |
1988 | } |
1989 | EXPORT_SYMBOL_GPL(iommu_unmap_fast); | |
1990 | ||
d88e61fa CH |
1991 | size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
1992 | struct scatterlist *sg, unsigned int nents, int prot) | |
315786eb | 1993 | { |
5d95f40e RM |
1994 | size_t len = 0, mapped = 0; |
1995 | phys_addr_t start; | |
1996 | unsigned int i = 0; | |
38ec010d | 1997 | int ret; |
315786eb | 1998 | |
5d95f40e RM |
1999 | while (i <= nents) { |
2000 | phys_addr_t s_phys = sg_phys(sg); | |
18f23409 | 2001 | |
5d95f40e RM |
2002 | if (len && s_phys != start + len) { |
2003 | ret = iommu_map(domain, iova + mapped, start, len, prot); | |
2004 | if (ret) | |
2005 | goto out_err; | |
18f23409 | 2006 | |
5d95f40e RM |
2007 | mapped += len; |
2008 | len = 0; | |
2009 | } | |
38ec010d | 2010 | |
5d95f40e RM |
2011 | if (len) { |
2012 | len += sg->length; | |
2013 | } else { | |
2014 | len = sg->length; | |
2015 | start = s_phys; | |
2016 | } | |
38ec010d | 2017 | |
5d95f40e RM |
2018 | if (++i < nents) |
2019 | sg = sg_next(sg); | |
315786eb OH |
2020 | } |
2021 | ||
2022 | return mapped; | |
38ec010d JR |
2023 | |
2024 | out_err: | |
2025 | /* undo mappings already done */ | |
2026 | iommu_unmap(domain, iova, mapped); | |
2027 | ||
2028 | return 0; | |
2029 | ||
315786eb | 2030 | } |
d88e61fa | 2031 | EXPORT_SYMBOL_GPL(iommu_map_sg); |
d7787d57 JR |
2032 | |
2033 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, | |
80f97f0f | 2034 | phys_addr_t paddr, u64 size, int prot) |
d7787d57 JR |
2035 | { |
2036 | if (unlikely(domain->ops->domain_window_enable == NULL)) | |
2037 | return -ENODEV; | |
2038 | ||
80f97f0f VS |
2039 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
2040 | prot); | |
d7787d57 JR |
2041 | } |
2042 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); | |
2043 | ||
2044 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) | |
2045 | { | |
2046 | if (unlikely(domain->ops->domain_window_disable == NULL)) | |
2047 | return; | |
2048 | ||
2049 | return domain->ops->domain_window_disable(domain, wnd_nr); | |
2050 | } | |
2051 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); | |
2052 | ||
207c6e36 JR |
2053 | /** |
2054 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework | |
2055 | * @domain: the iommu domain where the fault has happened | |
2056 | * @dev: the device where the fault has happened | |
2057 | * @iova: the faulting address | |
2058 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) | |
2059 | * | |
2060 | * This function should be called by the low-level IOMMU implementations | |
2061 | * whenever IOMMU faults happen, to allow high-level users, that are | |
2062 | * interested in such events, to know about them. | |
2063 | * | |
2064 | * This event may be useful for several possible use cases: | |
2065 | * - mere logging of the event | |
2066 | * - dynamic TLB/PTE loading | |
2067 | * - if restarting of the faulting device is required | |
2068 | * | |
2069 | * Returns 0 on success and an appropriate error code otherwise (if dynamic | |
2070 | * PTE/TLB loading will one day be supported, implementations will be able | |
2071 | * to tell whether it succeeded or not according to this return value). | |
2072 | * | |
2073 | * Specifically, -ENOSYS is returned if a fault handler isn't installed | |
2074 | * (though fault handlers can also return -ENOSYS, in case they want to | |
2075 | * elicit the default behavior of the IOMMU drivers). | |
2076 | */ | |
2077 | int report_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
2078 | unsigned long iova, int flags) | |
2079 | { | |
2080 | int ret = -ENOSYS; | |
2081 | ||
2082 | /* | |
2083 | * if upper layers showed interest and installed a fault handler, | |
2084 | * invoke it. | |
2085 | */ | |
2086 | if (domain->handler) | |
2087 | ret = domain->handler(domain, dev, iova, flags, | |
2088 | domain->handler_token); | |
2089 | ||
2090 | trace_io_page_fault(dev, iova, flags); | |
2091 | return ret; | |
2092 | } | |
2093 | EXPORT_SYMBOL_GPL(report_iommu_fault); | |
2094 | ||
d72e31c9 | 2095 | static int __init iommu_init(void) |
1460432c | 2096 | { |
d72e31c9 AW |
2097 | iommu_group_kset = kset_create_and_add("iommu_groups", |
2098 | NULL, kernel_kobj); | |
d72e31c9 AW |
2099 | BUG_ON(!iommu_group_kset); |
2100 | ||
bad614b2 GH |
2101 | iommu_debugfs_setup(); |
2102 | ||
d72e31c9 | 2103 | return 0; |
1460432c | 2104 | } |
d7ef9995 | 2105 | core_initcall(iommu_init); |
0cd76dd1 JR |
2106 | |
2107 | int iommu_domain_get_attr(struct iommu_domain *domain, | |
2108 | enum iommu_attr attr, void *data) | |
2109 | { | |
0ff64f80 | 2110 | struct iommu_domain_geometry *geometry; |
d2e12160 | 2111 | bool *paging; |
0ff64f80 JR |
2112 | int ret = 0; |
2113 | ||
2114 | switch (attr) { | |
2115 | case DOMAIN_ATTR_GEOMETRY: | |
2116 | geometry = data; | |
2117 | *geometry = domain->geometry; | |
2118 | ||
d2e12160 JR |
2119 | break; |
2120 | case DOMAIN_ATTR_PAGING: | |
2121 | paging = data; | |
d16e0faa | 2122 | *paging = (domain->pgsize_bitmap != 0UL); |
0ff64f80 JR |
2123 | break; |
2124 | default: | |
2125 | if (!domain->ops->domain_get_attr) | |
2126 | return -EINVAL; | |
0cd76dd1 | 2127 | |
0ff64f80 JR |
2128 | ret = domain->ops->domain_get_attr(domain, attr, data); |
2129 | } | |
2130 | ||
2131 | return ret; | |
0cd76dd1 JR |
2132 | } |
2133 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); | |
2134 | ||
2135 | int iommu_domain_set_attr(struct iommu_domain *domain, | |
2136 | enum iommu_attr attr, void *data) | |
2137 | { | |
69356712 | 2138 | int ret = 0; |
69356712 JR |
2139 | |
2140 | switch (attr) { | |
69356712 JR |
2141 | default: |
2142 | if (domain->ops->domain_set_attr == NULL) | |
2143 | return -EINVAL; | |
2144 | ||
2145 | ret = domain->ops->domain_set_attr(domain, attr, data); | |
2146 | } | |
2147 | ||
2148 | return ret; | |
1460432c | 2149 | } |
0cd76dd1 | 2150 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
a1015c2b | 2151 | |
e5b5234a | 2152 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
2153 | { |
2154 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2155 | ||
e5b5234a EA |
2156 | if (ops && ops->get_resv_regions) |
2157 | ops->get_resv_regions(dev, list); | |
a1015c2b JR |
2158 | } |
2159 | ||
e5b5234a | 2160 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
2161 | { |
2162 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2163 | ||
e5b5234a EA |
2164 | if (ops && ops->put_resv_regions) |
2165 | ops->put_resv_regions(dev, list); | |
a1015c2b | 2166 | } |
d290f1e7 | 2167 | |
2b20cbba | 2168 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
9d3a4de4 RM |
2169 | size_t length, int prot, |
2170 | enum iommu_resv_type type) | |
2b20cbba EA |
2171 | { |
2172 | struct iommu_resv_region *region; | |
2173 | ||
2174 | region = kzalloc(sizeof(*region), GFP_KERNEL); | |
2175 | if (!region) | |
2176 | return NULL; | |
2177 | ||
2178 | INIT_LIST_HEAD(®ion->list); | |
2179 | region->start = start; | |
2180 | region->length = length; | |
2181 | region->prot = prot; | |
2182 | region->type = type; | |
2183 | return region; | |
a1015c2b | 2184 | } |
d290f1e7 | 2185 | |
7423e017 LB |
2186 | static int |
2187 | request_default_domain_for_dev(struct device *dev, unsigned long type) | |
d290f1e7 | 2188 | { |
7423e017 | 2189 | struct iommu_domain *domain; |
d290f1e7 JR |
2190 | struct iommu_group *group; |
2191 | int ret; | |
2192 | ||
2193 | /* Device must already be in a group before calling this function */ | |
57274ea2 LB |
2194 | group = iommu_group_get(dev); |
2195 | if (!group) | |
2196 | return -EINVAL; | |
d290f1e7 JR |
2197 | |
2198 | mutex_lock(&group->mutex); | |
2199 | ||
2200 | /* Check if the default domain is already direct mapped */ | |
2201 | ret = 0; | |
7423e017 | 2202 | if (group->default_domain && group->default_domain->type == type) |
d290f1e7 JR |
2203 | goto out; |
2204 | ||
2205 | /* Don't change mappings of existing devices */ | |
2206 | ret = -EBUSY; | |
2207 | if (iommu_group_device_count(group) != 1) | |
2208 | goto out; | |
2209 | ||
2210 | /* Allocate a direct mapped domain */ | |
2211 | ret = -ENOMEM; | |
7423e017 LB |
2212 | domain = __iommu_domain_alloc(dev->bus, type); |
2213 | if (!domain) | |
d290f1e7 JR |
2214 | goto out; |
2215 | ||
2216 | /* Attach the device to the domain */ | |
7423e017 | 2217 | ret = __iommu_attach_group(domain, group); |
d290f1e7 | 2218 | if (ret) { |
7423e017 | 2219 | iommu_domain_free(domain); |
d290f1e7 JR |
2220 | goto out; |
2221 | } | |
2222 | ||
7423e017 LB |
2223 | iommu_group_create_direct_mappings(group, dev); |
2224 | ||
d290f1e7 JR |
2225 | /* Make the direct mapped domain the default for this group */ |
2226 | if (group->default_domain) | |
2227 | iommu_domain_free(group->default_domain); | |
7423e017 | 2228 | group->default_domain = domain; |
d290f1e7 | 2229 | |
7423e017 LB |
2230 | dev_info(dev, "Using iommu %s mapping\n", |
2231 | type == IOMMU_DOMAIN_DMA ? "dma" : "direct"); | |
d290f1e7 JR |
2232 | |
2233 | ret = 0; | |
2234 | out: | |
2235 | mutex_unlock(&group->mutex); | |
2236 | iommu_group_put(group); | |
2237 | ||
2238 | return ret; | |
2239 | } | |
57f98d2f | 2240 | |
7423e017 LB |
2241 | /* Request that a device is direct mapped by the IOMMU */ |
2242 | int iommu_request_dm_for_dev(struct device *dev) | |
2243 | { | |
2244 | return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY); | |
2245 | } | |
2246 | ||
2247 | /* Request that a device can't be direct mapped by the IOMMU */ | |
2248 | int iommu_request_dma_domain_for_dev(struct device *dev) | |
2249 | { | |
2250 | return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA); | |
2251 | } | |
2252 | ||
8a69961c JR |
2253 | void iommu_set_default_passthrough(bool cmd_line) |
2254 | { | |
2255 | if (cmd_line) | |
2256 | iommu_set_cmd_line_dma_api(); | |
2257 | ||
2258 | iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY; | |
2259 | } | |
2260 | ||
2261 | void iommu_set_default_translated(bool cmd_line) | |
2262 | { | |
2263 | if (cmd_line) | |
2264 | iommu_set_cmd_line_dma_api(); | |
2265 | ||
2266 | iommu_def_domain_type = IOMMU_DOMAIN_DMA; | |
2267 | } | |
2268 | ||
2269 | bool iommu_default_passthrough(void) | |
2270 | { | |
2271 | return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY; | |
2272 | } | |
2273 | EXPORT_SYMBOL_GPL(iommu_default_passthrough); | |
2274 | ||
534766df | 2275 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
e4f10ffe | 2276 | { |
e4f10ffe | 2277 | const struct iommu_ops *ops = NULL; |
d0f6f583 | 2278 | struct iommu_device *iommu; |
e4f10ffe | 2279 | |
d0f6f583 JR |
2280 | spin_lock(&iommu_device_lock); |
2281 | list_for_each_entry(iommu, &iommu_device_list, list) | |
2282 | if (iommu->fwnode == fwnode) { | |
2283 | ops = iommu->ops; | |
e4f10ffe LP |
2284 | break; |
2285 | } | |
d0f6f583 | 2286 | spin_unlock(&iommu_device_lock); |
e4f10ffe LP |
2287 | return ops; |
2288 | } | |
2289 | ||
57f98d2f RM |
2290 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
2291 | const struct iommu_ops *ops) | |
2292 | { | |
b4ef725e | 2293 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
57f98d2f RM |
2294 | |
2295 | if (fwspec) | |
2296 | return ops == fwspec->ops ? 0 : -EINVAL; | |
2297 | ||
2298 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); | |
2299 | if (!fwspec) | |
2300 | return -ENOMEM; | |
2301 | ||
2302 | of_node_get(to_of_node(iommu_fwnode)); | |
2303 | fwspec->iommu_fwnode = iommu_fwnode; | |
2304 | fwspec->ops = ops; | |
b4ef725e | 2305 | dev_iommu_fwspec_set(dev, fwspec); |
57f98d2f RM |
2306 | return 0; |
2307 | } | |
2308 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); | |
2309 | ||
2310 | void iommu_fwspec_free(struct device *dev) | |
2311 | { | |
b4ef725e | 2312 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
57f98d2f RM |
2313 | |
2314 | if (fwspec) { | |
2315 | fwnode_handle_put(fwspec->iommu_fwnode); | |
2316 | kfree(fwspec); | |
b4ef725e | 2317 | dev_iommu_fwspec_set(dev, NULL); |
57f98d2f RM |
2318 | } |
2319 | } | |
2320 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); | |
2321 | ||
2322 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) | |
2323 | { | |
b4ef725e | 2324 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
57f98d2f RM |
2325 | size_t size; |
2326 | int i; | |
2327 | ||
2328 | if (!fwspec) | |
2329 | return -EINVAL; | |
2330 | ||
2331 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); | |
2332 | if (size > sizeof(*fwspec)) { | |
b4ef725e | 2333 | fwspec = krealloc(fwspec, size, GFP_KERNEL); |
57f98d2f RM |
2334 | if (!fwspec) |
2335 | return -ENOMEM; | |
909111ba | 2336 | |
b4ef725e | 2337 | dev_iommu_fwspec_set(dev, fwspec); |
57f98d2f RM |
2338 | } |
2339 | ||
2340 | for (i = 0; i < num_ids; i++) | |
2341 | fwspec->ids[fwspec->num_ids + i] = ids[i]; | |
2342 | ||
2343 | fwspec->num_ids += num_ids; | |
57f98d2f RM |
2344 | return 0; |
2345 | } | |
2346 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); | |
a3a19592 LB |
2347 | |
2348 | /* | |
2349 | * Per device IOMMU features. | |
2350 | */ | |
2351 | bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat) | |
2352 | { | |
2353 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2354 | ||
2355 | if (ops && ops->dev_has_feat) | |
2356 | return ops->dev_has_feat(dev, feat); | |
2357 | ||
2358 | return false; | |
2359 | } | |
2360 | EXPORT_SYMBOL_GPL(iommu_dev_has_feature); | |
2361 | ||
2362 | int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) | |
2363 | { | |
2364 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2365 | ||
2366 | if (ops && ops->dev_enable_feat) | |
2367 | return ops->dev_enable_feat(dev, feat); | |
2368 | ||
2369 | return -ENODEV; | |
2370 | } | |
2371 | EXPORT_SYMBOL_GPL(iommu_dev_enable_feature); | |
2372 | ||
2373 | /* | |
2374 | * The device drivers should do the necessary cleanups before calling this. | |
2375 | * For example, before disabling the aux-domain feature, the device driver | |
2376 | * should detach all aux-domains. Otherwise, this will return -EBUSY. | |
2377 | */ | |
2378 | int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) | |
2379 | { | |
2380 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2381 | ||
2382 | if (ops && ops->dev_disable_feat) | |
2383 | return ops->dev_disable_feat(dev, feat); | |
2384 | ||
2385 | return -EBUSY; | |
2386 | } | |
2387 | EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); | |
2388 | ||
2389 | bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat) | |
2390 | { | |
2391 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2392 | ||
2393 | if (ops && ops->dev_feat_enabled) | |
2394 | return ops->dev_feat_enabled(dev, feat); | |
2395 | ||
2396 | return false; | |
2397 | } | |
2398 | EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled); | |
2399 | ||
2400 | /* | |
2401 | * Aux-domain specific attach/detach. | |
2402 | * | |
2403 | * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns | |
2404 | * true. Also, as long as domains are attached to a device through this | |
2405 | * interface, any tries to call iommu_attach_device() should fail | |
2406 | * (iommu_detach_device() can't fail, so we fail when trying to re-attach). | |
2407 | * This should make us safe against a device being attached to a guest as a | |
2408 | * whole while there are still pasid users on it (aux and sva). | |
2409 | */ | |
2410 | int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev) | |
2411 | { | |
2412 | int ret = -ENODEV; | |
2413 | ||
2414 | if (domain->ops->aux_attach_dev) | |
2415 | ret = domain->ops->aux_attach_dev(domain, dev); | |
2416 | ||
2417 | if (!ret) | |
2418 | trace_attach_device_to_domain(dev); | |
2419 | ||
2420 | return ret; | |
2421 | } | |
2422 | EXPORT_SYMBOL_GPL(iommu_aux_attach_device); | |
2423 | ||
2424 | void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev) | |
2425 | { | |
2426 | if (domain->ops->aux_detach_dev) { | |
2427 | domain->ops->aux_detach_dev(domain, dev); | |
2428 | trace_detach_device_from_domain(dev); | |
2429 | } | |
2430 | } | |
2431 | EXPORT_SYMBOL_GPL(iommu_aux_detach_device); | |
2432 | ||
2433 | int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) | |
2434 | { | |
2435 | int ret = -ENODEV; | |
2436 | ||
2437 | if (domain->ops->aux_get_pasid) | |
2438 | ret = domain->ops->aux_get_pasid(domain, dev); | |
2439 | ||
2440 | return ret; | |
2441 | } | |
2442 | EXPORT_SYMBOL_GPL(iommu_aux_get_pasid); | |
26b25a2b JPB |
2443 | |
2444 | /** | |
2445 | * iommu_sva_bind_device() - Bind a process address space to a device | |
2446 | * @dev: the device | |
2447 | * @mm: the mm to bind, caller must hold a reference to it | |
2448 | * | |
2449 | * Create a bond between device and address space, allowing the device to access | |
2450 | * the mm using the returned PASID. If a bond already exists between @device and | |
2451 | * @mm, it is returned and an additional reference is taken. Caller must call | |
2452 | * iommu_sva_unbind_device() to release each reference. | |
2453 | * | |
2454 | * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to | |
2455 | * initialize the required SVA features. | |
2456 | * | |
2457 | * On error, returns an ERR_PTR value. | |
2458 | */ | |
2459 | struct iommu_sva * | |
2460 | iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) | |
2461 | { | |
2462 | struct iommu_group *group; | |
2463 | struct iommu_sva *handle = ERR_PTR(-EINVAL); | |
2464 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2465 | ||
2466 | if (!ops || !ops->sva_bind) | |
2467 | return ERR_PTR(-ENODEV); | |
2468 | ||
2469 | group = iommu_group_get(dev); | |
2470 | if (!group) | |
2471 | return ERR_PTR(-ENODEV); | |
2472 | ||
2473 | /* Ensure device count and domain don't change while we're binding */ | |
2474 | mutex_lock(&group->mutex); | |
2475 | ||
2476 | /* | |
2477 | * To keep things simple, SVA currently doesn't support IOMMU groups | |
2478 | * with more than one device. Existing SVA-capable systems are not | |
2479 | * affected by the problems that required IOMMU groups (lack of ACS | |
2480 | * isolation, device ID aliasing and other hardware issues). | |
2481 | */ | |
2482 | if (iommu_group_device_count(group) != 1) | |
2483 | goto out_unlock; | |
2484 | ||
2485 | handle = ops->sva_bind(dev, mm, drvdata); | |
2486 | ||
2487 | out_unlock: | |
2488 | mutex_unlock(&group->mutex); | |
2489 | iommu_group_put(group); | |
2490 | ||
2491 | return handle; | |
2492 | } | |
2493 | EXPORT_SYMBOL_GPL(iommu_sva_bind_device); | |
2494 | ||
2495 | /** | |
2496 | * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device | |
2497 | * @handle: the handle returned by iommu_sva_bind_device() | |
2498 | * | |
2499 | * Put reference to a bond between device and address space. The device should | |
2500 | * not be issuing any more transaction for this PASID. All outstanding page | |
2501 | * requests for this PASID must have been flushed to the IOMMU. | |
2502 | * | |
2503 | * Returns 0 on success, or an error value | |
2504 | */ | |
2505 | void iommu_sva_unbind_device(struct iommu_sva *handle) | |
2506 | { | |
2507 | struct iommu_group *group; | |
2508 | struct device *dev = handle->dev; | |
2509 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2510 | ||
2511 | if (!ops || !ops->sva_unbind) | |
2512 | return; | |
2513 | ||
2514 | group = iommu_group_get(dev); | |
2515 | if (!group) | |
2516 | return; | |
2517 | ||
2518 | mutex_lock(&group->mutex); | |
2519 | ops->sva_unbind(handle); | |
2520 | mutex_unlock(&group->mutex); | |
2521 | ||
2522 | iommu_group_put(group); | |
2523 | } | |
2524 | EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); | |
2525 | ||
2526 | int iommu_sva_set_ops(struct iommu_sva *handle, | |
2527 | const struct iommu_sva_ops *sva_ops) | |
2528 | { | |
2529 | if (handle->ops && handle->ops != sva_ops) | |
2530 | return -EEXIST; | |
2531 | ||
2532 | handle->ops = sva_ops; | |
2533 | return 0; | |
2534 | } | |
2535 | EXPORT_SYMBOL_GPL(iommu_sva_set_ops); | |
2536 | ||
2537 | int iommu_sva_get_pasid(struct iommu_sva *handle) | |
2538 | { | |
2539 | const struct iommu_ops *ops = handle->dev->bus->iommu_ops; | |
2540 | ||
2541 | if (!ops || !ops->sva_get_pasid) | |
2542 | return IOMMU_PASID_INVALID; | |
2543 | ||
2544 | return ops->sva_get_pasid(handle); | |
2545 | } | |
2546 | EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); |