]>
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> |
46983fcd | 10 | #include <linux/dma-iommu.h> |
40998188 | 11 | #include <linux/kernel.h> |
e7d6fff6 | 12 | #include <linux/bits.h> |
fc2100eb JR |
13 | #include <linux/bug.h> |
14 | #include <linux/types.h> | |
c1af7b40 PG |
15 | #include <linux/init.h> |
16 | #include <linux/export.h> | |
60db4027 | 17 | #include <linux/slab.h> |
fc2100eb JR |
18 | #include <linux/errno.h> |
19 | #include <linux/iommu.h> | |
d72e31c9 AW |
20 | #include <linux/idr.h> |
21 | #include <linux/notifier.h> | |
22 | #include <linux/err.h> | |
104a1c13 | 23 | #include <linux/pci.h> |
f096c061 | 24 | #include <linux/bitops.h> |
57f98d2f | 25 | #include <linux/property.h> |
eab03e2a | 26 | #include <linux/fsl/mc.h> |
25f003de | 27 | #include <linux/module.h> |
7f6db171 | 28 | #include <trace/events/iommu.h> |
d72e31c9 AW |
29 | |
30 | static struct kset *iommu_group_kset; | |
e38d1f13 | 31 | static DEFINE_IDA(iommu_group_ida); |
22bb182c JR |
32 | |
33 | static unsigned int iommu_def_domain_type __read_mostly; | |
e96763ec | 34 | static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT); |
faf14989 | 35 | static u32 iommu_cmd_line __read_mostly; |
d72e31c9 AW |
36 | |
37 | struct iommu_group { | |
38 | struct kobject kobj; | |
39 | struct kobject *devices_kobj; | |
40 | struct list_head devices; | |
41 | struct mutex mutex; | |
42 | struct blocking_notifier_head notifier; | |
43 | void *iommu_data; | |
44 | void (*iommu_data_release)(void *iommu_data); | |
45 | char *name; | |
46 | int id; | |
53723dc5 | 47 | struct iommu_domain *default_domain; |
e39cb8a3 | 48 | struct iommu_domain *domain; |
41df6dcc | 49 | struct list_head entry; |
d72e31c9 AW |
50 | }; |
51 | ||
c09e22d5 | 52 | struct group_device { |
d72e31c9 AW |
53 | struct list_head list; |
54 | struct device *dev; | |
55 | char *name; | |
56 | }; | |
57 | ||
58 | struct iommu_group_attribute { | |
59 | struct attribute attr; | |
60 | ssize_t (*show)(struct iommu_group *group, char *buf); | |
61 | ssize_t (*store)(struct iommu_group *group, | |
62 | const char *buf, size_t count); | |
63 | }; | |
64 | ||
bc7d12b9 | 65 | static const char * const iommu_group_resv_type_string[] = { |
adfd3738 EA |
66 | [IOMMU_RESV_DIRECT] = "direct", |
67 | [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable", | |
68 | [IOMMU_RESV_RESERVED] = "reserved", | |
69 | [IOMMU_RESV_MSI] = "msi", | |
70 | [IOMMU_RESV_SW_MSI] = "msi", | |
bc7d12b9 EA |
71 | }; |
72 | ||
faf14989 | 73 | #define IOMMU_CMD_LINE_DMA_API BIT(0) |
a250c23f | 74 | #define IOMMU_CMD_LINE_STRICT BIT(1) |
faf14989 | 75 | |
79659190 JR |
76 | static int iommu_alloc_default_domain(struct iommu_group *group, |
77 | struct device *dev); | |
6e1aa204 JR |
78 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
79 | unsigned type); | |
80 | static int __iommu_attach_device(struct iommu_domain *domain, | |
81 | struct device *dev); | |
82 | static int __iommu_attach_group(struct iommu_domain *domain, | |
83 | struct iommu_group *group); | |
84 | static void __iommu_detach_group(struct iommu_domain *domain, | |
85 | struct iommu_group *group); | |
ce574c27 JR |
86 | static int iommu_create_device_direct_mappings(struct iommu_group *group, |
87 | struct device *dev); | |
1b032ec1 | 88 | static struct iommu_group *iommu_group_get_for_dev(struct device *dev); |
08a27c1c SPP |
89 | static ssize_t iommu_group_store_type(struct iommu_group *group, |
90 | const char *buf, size_t count); | |
6e1aa204 | 91 | |
d72e31c9 AW |
92 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
93 | struct iommu_group_attribute iommu_group_attr_##_name = \ | |
94 | __ATTR(_name, _mode, _show, _store) | |
fc2100eb | 95 | |
d72e31c9 AW |
96 | #define to_iommu_group_attr(_attr) \ |
97 | container_of(_attr, struct iommu_group_attribute, attr) | |
98 | #define to_iommu_group(_kobj) \ | |
99 | container_of(_kobj, struct iommu_group, kobj) | |
fc2100eb | 100 | |
b0119e87 JR |
101 | static LIST_HEAD(iommu_device_list); |
102 | static DEFINE_SPINLOCK(iommu_device_lock); | |
103 | ||
5fa9e7c5 JR |
104 | /* |
105 | * Use a function instead of an array here because the domain-type is a | |
106 | * bit-field, so an array would waste memory. | |
107 | */ | |
108 | static const char *iommu_domain_type_str(unsigned int t) | |
109 | { | |
110 | switch (t) { | |
111 | case IOMMU_DOMAIN_BLOCKED: | |
112 | return "Blocked"; | |
113 | case IOMMU_DOMAIN_IDENTITY: | |
114 | return "Passthrough"; | |
115 | case IOMMU_DOMAIN_UNMANAGED: | |
116 | return "Unmanaged"; | |
117 | case IOMMU_DOMAIN_DMA: | |
bf3aed46 | 118 | case IOMMU_DOMAIN_DMA_FQ: |
5fa9e7c5 JR |
119 | return "Translated"; |
120 | default: | |
121 | return "Unknown"; | |
122 | } | |
123 | } | |
124 | ||
125 | static int __init iommu_subsys_init(void) | |
126 | { | |
3189713a | 127 | if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) { |
22bb182c JR |
128 | if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) |
129 | iommu_set_default_passthrough(false); | |
130 | else | |
131 | iommu_set_default_translated(false); | |
2cc13bb4 | 132 | |
2896ba40 JR |
133 | if (iommu_default_passthrough() && mem_encrypt_active()) { |
134 | pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n"); | |
2cc13bb4 JR |
135 | iommu_set_default_translated(false); |
136 | } | |
22bb182c JR |
137 | } |
138 | ||
c208916f RM |
139 | if (!iommu_default_passthrough() && !iommu_dma_strict) |
140 | iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ; | |
141 | ||
22bb182c JR |
142 | pr_info("Default domain type: %s %s\n", |
143 | iommu_domain_type_str(iommu_def_domain_type), | |
3189713a CH |
144 | (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ? |
145 | "(set via kernel command line)" : ""); | |
5fa9e7c5 | 146 | |
7cf8a638 RM |
147 | if (!iommu_default_passthrough()) |
148 | pr_info("DMA domain TLB invalidation policy: %s mode %s\n", | |
149 | iommu_dma_strict ? "strict" : "lazy", | |
150 | (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ? | |
151 | "(set via kernel command line)" : ""); | |
d8577d2e | 152 | |
5fa9e7c5 JR |
153 | return 0; |
154 | } | |
155 | subsys_initcall(iommu_subsys_init); | |
156 | ||
2d471b20 RM |
157 | /** |
158 | * iommu_device_register() - Register an IOMMU hardware instance | |
159 | * @iommu: IOMMU handle for the instance | |
160 | * @ops: IOMMU ops to associate with the instance | |
161 | * @hwdev: (optional) actual instance device, used for fwnode lookup | |
162 | * | |
163 | * Return: 0 on success, or an error. | |
164 | */ | |
165 | int iommu_device_register(struct iommu_device *iommu, | |
166 | const struct iommu_ops *ops, struct device *hwdev) | |
b0119e87 | 167 | { |
2d471b20 RM |
168 | /* We need to be able to take module references appropriately */ |
169 | if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) | |
170 | return -EINVAL; | |
171 | ||
172 | iommu->ops = ops; | |
173 | if (hwdev) | |
174 | iommu->fwnode = hwdev->fwnode; | |
175 | ||
b0119e87 JR |
176 | spin_lock(&iommu_device_lock); |
177 | list_add_tail(&iommu->list, &iommu_device_list); | |
178 | spin_unlock(&iommu_device_lock); | |
b0119e87 JR |
179 | return 0; |
180 | } | |
a7ba5c3d | 181 | EXPORT_SYMBOL_GPL(iommu_device_register); |
b0119e87 JR |
182 | |
183 | void iommu_device_unregister(struct iommu_device *iommu) | |
184 | { | |
185 | spin_lock(&iommu_device_lock); | |
186 | list_del(&iommu->list); | |
187 | spin_unlock(&iommu_device_lock); | |
188 | } | |
a7ba5c3d | 189 | EXPORT_SYMBOL_GPL(iommu_device_unregister); |
b0119e87 | 190 | |
045a7042 | 191 | static struct dev_iommu *dev_iommu_get(struct device *dev) |
0c830e6b | 192 | { |
045a7042 | 193 | struct dev_iommu *param = dev->iommu; |
0c830e6b JP |
194 | |
195 | if (param) | |
196 | return param; | |
197 | ||
198 | param = kzalloc(sizeof(*param), GFP_KERNEL); | |
199 | if (!param) | |
200 | return NULL; | |
201 | ||
202 | mutex_init(¶m->lock); | |
045a7042 | 203 | dev->iommu = param; |
0c830e6b JP |
204 | return param; |
205 | } | |
206 | ||
045a7042 | 207 | static void dev_iommu_free(struct device *dev) |
0c830e6b | 208 | { |
6e04703c VJ |
209 | struct dev_iommu *param = dev->iommu; |
210 | ||
045a7042 | 211 | dev->iommu = NULL; |
6e04703c VJ |
212 | if (param->fwspec) { |
213 | fwnode_handle_put(param->fwspec->iommu_fwnode); | |
214 | kfree(param->fwspec); | |
215 | } | |
216 | kfree(param); | |
0c830e6b JP |
217 | } |
218 | ||
41df6dcc | 219 | static int __iommu_probe_device(struct device *dev, struct list_head *group_list) |
cc5aed44 JR |
220 | { |
221 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
a6a4c7e2 JR |
222 | struct iommu_device *iommu_dev; |
223 | struct iommu_group *group; | |
0c830e6b | 224 | int ret; |
cc5aed44 | 225 | |
0c830e6b | 226 | if (!ops) |
f38338cf | 227 | return -ENODEV; |
cc5aed44 | 228 | |
045a7042 | 229 | if (!dev_iommu_get(dev)) |
0c830e6b | 230 | return -ENOMEM; |
cc5aed44 | 231 | |
25f003de WD |
232 | if (!try_module_get(ops->owner)) { |
233 | ret = -EINVAL; | |
4e8906f0 | 234 | goto err_free; |
25f003de WD |
235 | } |
236 | ||
a6a4c7e2 | 237 | iommu_dev = ops->probe_device(dev); |
4e8906f0 JR |
238 | if (IS_ERR(iommu_dev)) { |
239 | ret = PTR_ERR(iommu_dev); | |
240 | goto out_module_put; | |
241 | } | |
a6a4c7e2 JR |
242 | |
243 | dev->iommu->iommu_dev = iommu_dev; | |
244 | ||
245 | group = iommu_group_get_for_dev(dev); | |
deac0b3b | 246 | if (IS_ERR(group)) { |
a6a4c7e2 JR |
247 | ret = PTR_ERR(group); |
248 | goto out_release; | |
249 | } | |
250 | iommu_group_put(group); | |
251 | ||
41df6dcc JR |
252 | if (group_list && !group->default_domain && list_empty(&group->entry)) |
253 | list_add_tail(&group->entry, group_list); | |
254 | ||
a6a4c7e2 | 255 | iommu_device_link(iommu_dev, dev); |
25f003de WD |
256 | |
257 | return 0; | |
dc9de8a2 | 258 | |
a6a4c7e2 JR |
259 | out_release: |
260 | ops->release_device(dev); | |
261 | ||
4e8906f0 | 262 | out_module_put: |
25f003de | 263 | module_put(ops->owner); |
4e8906f0 JR |
264 | |
265 | err_free: | |
045a7042 | 266 | dev_iommu_free(dev); |
4e8906f0 | 267 | |
dc9de8a2 | 268 | return ret; |
cc5aed44 JR |
269 | } |
270 | ||
3eeeb45c | 271 | int iommu_probe_device(struct device *dev) |
cc5aed44 JR |
272 | { |
273 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
cf193888 JR |
274 | struct iommu_group *group; |
275 | int ret; | |
cc5aed44 | 276 | |
cf193888 JR |
277 | ret = __iommu_probe_device(dev, NULL); |
278 | if (ret) | |
279 | goto err_out; | |
280 | ||
79659190 | 281 | group = iommu_group_get(dev); |
058236ee YY |
282 | if (!group) { |
283 | ret = -ENODEV; | |
79659190 | 284 | goto err_release; |
058236ee | 285 | } |
79659190 | 286 | |
cf193888 JR |
287 | /* |
288 | * Try to allocate a default domain - needs support from the | |
289 | * IOMMU driver. There are still some drivers which don't | |
290 | * support default domains, so the return value is not yet | |
291 | * checked. | |
292 | */ | |
211ff31b | 293 | mutex_lock(&group->mutex); |
79659190 | 294 | iommu_alloc_default_domain(group, dev); |
cf193888 | 295 | |
77c38c8c | 296 | if (group->default_domain) { |
cf193888 | 297 | ret = __iommu_attach_device(group->default_domain, dev); |
77c38c8c | 298 | if (ret) { |
e9b4115a | 299 | mutex_unlock(&group->mutex); |
77c38c8c SK |
300 | iommu_group_put(group); |
301 | goto err_release; | |
302 | } | |
303 | } | |
cf193888 | 304 | |
ce574c27 JR |
305 | iommu_create_device_direct_mappings(group, dev); |
306 | ||
e9b4115a | 307 | mutex_unlock(&group->mutex); |
cf193888 JR |
308 | iommu_group_put(group); |
309 | ||
cf193888 JR |
310 | if (ops->probe_finalize) |
311 | ops->probe_finalize(dev); | |
312 | ||
313 | return 0; | |
314 | ||
315 | err_release: | |
316 | iommu_release_device(dev); | |
3eeeb45c | 317 | |
cf193888 JR |
318 | err_out: |
319 | return ret; | |
0c830e6b | 320 | |
cc5aed44 JR |
321 | } |
322 | ||
3eeeb45c | 323 | void iommu_release_device(struct device *dev) |
cc5aed44 JR |
324 | { |
325 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
25f003de | 326 | |
3eeeb45c JR |
327 | if (!dev->iommu) |
328 | return; | |
a6a4c7e2 JR |
329 | |
330 | iommu_device_unlink(dev->iommu->iommu_dev, dev); | |
a6a4c7e2 JR |
331 | |
332 | ops->release_device(dev); | |
0c830e6b | 333 | |
9ac85451 | 334 | iommu_group_remove_device(dev); |
a6a4c7e2 JR |
335 | module_put(ops->owner); |
336 | dev_iommu_free(dev); | |
cc5aed44 | 337 | } |
53723dc5 | 338 | |
fccb4e3b WD |
339 | static int __init iommu_set_def_domain_type(char *str) |
340 | { | |
341 | bool pt; | |
7f9584df | 342 | int ret; |
fccb4e3b | 343 | |
7f9584df AS |
344 | ret = kstrtobool(str, &pt); |
345 | if (ret) | |
346 | return ret; | |
fccb4e3b | 347 | |
adab0b07 JR |
348 | if (pt) |
349 | iommu_set_default_passthrough(true); | |
350 | else | |
351 | iommu_set_default_translated(true); | |
faf14989 | 352 | |
fccb4e3b WD |
353 | return 0; |
354 | } | |
355 | early_param("iommu.passthrough", iommu_set_def_domain_type); | |
356 | ||
68a6efe8 ZL |
357 | static int __init iommu_dma_setup(char *str) |
358 | { | |
a250c23f RM |
359 | int ret = kstrtobool(str, &iommu_dma_strict); |
360 | ||
361 | if (!ret) | |
362 | iommu_cmd_line |= IOMMU_CMD_LINE_STRICT; | |
363 | return ret; | |
68a6efe8 ZL |
364 | } |
365 | early_param("iommu.strict", iommu_dma_setup); | |
366 | ||
308723e3 | 367 | void iommu_set_dma_strict(void) |
a250c23f | 368 | { |
308723e3 | 369 | iommu_dma_strict = true; |
c208916f RM |
370 | if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ) |
371 | iommu_def_domain_type = IOMMU_DOMAIN_DMA; | |
a250c23f RM |
372 | } |
373 | ||
d72e31c9 AW |
374 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
375 | struct attribute *__attr, char *buf) | |
1460432c | 376 | { |
d72e31c9 AW |
377 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
378 | struct iommu_group *group = to_iommu_group(kobj); | |
379 | ssize_t ret = -EIO; | |
1460432c | 380 | |
d72e31c9 AW |
381 | if (attr->show) |
382 | ret = attr->show(group, buf); | |
383 | return ret; | |
384 | } | |
385 | ||
386 | static ssize_t iommu_group_attr_store(struct kobject *kobj, | |
387 | struct attribute *__attr, | |
388 | const char *buf, size_t count) | |
389 | { | |
390 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); | |
391 | struct iommu_group *group = to_iommu_group(kobj); | |
392 | ssize_t ret = -EIO; | |
1460432c | 393 | |
d72e31c9 AW |
394 | if (attr->store) |
395 | ret = attr->store(group, buf, count); | |
396 | return ret; | |
1460432c | 397 | } |
1460432c | 398 | |
d72e31c9 AW |
399 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
400 | .show = iommu_group_attr_show, | |
401 | .store = iommu_group_attr_store, | |
402 | }; | |
1460432c | 403 | |
d72e31c9 AW |
404 | static int iommu_group_create_file(struct iommu_group *group, |
405 | struct iommu_group_attribute *attr) | |
406 | { | |
407 | return sysfs_create_file(&group->kobj, &attr->attr); | |
1460432c | 408 | } |
1460432c | 409 | |
d72e31c9 AW |
410 | static void iommu_group_remove_file(struct iommu_group *group, |
411 | struct iommu_group_attribute *attr) | |
412 | { | |
413 | sysfs_remove_file(&group->kobj, &attr->attr); | |
414 | } | |
415 | ||
416 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) | |
417 | { | |
418 | return sprintf(buf, "%s\n", group->name); | |
419 | } | |
420 | ||
6c65fb31 EA |
421 | /** |
422 | * iommu_insert_resv_region - Insert a new region in the | |
423 | * list of reserved regions. | |
424 | * @new: new region to insert | |
425 | * @regions: list of regions | |
426 | * | |
4dbd258f EA |
427 | * Elements are sorted by start address and overlapping segments |
428 | * of the same type are merged. | |
6c65fb31 | 429 | */ |
1b0b2a84 WY |
430 | static int iommu_insert_resv_region(struct iommu_resv_region *new, |
431 | struct list_head *regions) | |
6c65fb31 | 432 | { |
4dbd258f EA |
433 | struct iommu_resv_region *iter, *tmp, *nr, *top; |
434 | LIST_HEAD(stack); | |
435 | ||
436 | nr = iommu_alloc_resv_region(new->start, new->length, | |
437 | new->prot, new->type); | |
438 | if (!nr) | |
439 | return -ENOMEM; | |
440 | ||
441 | /* First add the new element based on start address sorting */ | |
442 | list_for_each_entry(iter, regions, list) { | |
443 | if (nr->start < iter->start || | |
444 | (nr->start == iter->start && nr->type <= iter->type)) | |
445 | break; | |
446 | } | |
447 | list_add_tail(&nr->list, &iter->list); | |
448 | ||
449 | /* Merge overlapping segments of type nr->type in @regions, if any */ | |
450 | list_for_each_entry_safe(iter, tmp, regions, list) { | |
451 | phys_addr_t top_end, iter_end = iter->start + iter->length - 1; | |
452 | ||
4c80ba39 EA |
453 | /* no merge needed on elements of different types than @new */ |
454 | if (iter->type != new->type) { | |
4dbd258f EA |
455 | list_move_tail(&iter->list, &stack); |
456 | continue; | |
457 | } | |
458 | ||
459 | /* look for the last stack element of same type as @iter */ | |
460 | list_for_each_entry_reverse(top, &stack, list) | |
461 | if (top->type == iter->type) | |
462 | goto check_overlap; | |
463 | ||
464 | list_move_tail(&iter->list, &stack); | |
465 | continue; | |
466 | ||
467 | check_overlap: | |
468 | top_end = top->start + top->length - 1; | |
469 | ||
470 | if (iter->start > top_end + 1) { | |
471 | list_move_tail(&iter->list, &stack); | |
6c65fb31 | 472 | } else { |
4dbd258f EA |
473 | top->length = max(top_end, iter_end) - top->start + 1; |
474 | list_del(&iter->list); | |
475 | kfree(iter); | |
6c65fb31 EA |
476 | } |
477 | } | |
4dbd258f | 478 | list_splice(&stack, regions); |
6c65fb31 EA |
479 | return 0; |
480 | } | |
481 | ||
482 | static int | |
483 | iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, | |
484 | struct list_head *group_resv_regions) | |
485 | { | |
486 | struct iommu_resv_region *entry; | |
a514a6e2 | 487 | int ret = 0; |
6c65fb31 EA |
488 | |
489 | list_for_each_entry(entry, dev_resv_regions, list) { | |
490 | ret = iommu_insert_resv_region(entry, group_resv_regions); | |
491 | if (ret) | |
492 | break; | |
493 | } | |
494 | return ret; | |
495 | } | |
496 | ||
497 | int iommu_get_group_resv_regions(struct iommu_group *group, | |
498 | struct list_head *head) | |
499 | { | |
8d2932dd | 500 | struct group_device *device; |
6c65fb31 EA |
501 | int ret = 0; |
502 | ||
503 | mutex_lock(&group->mutex); | |
504 | list_for_each_entry(device, &group->devices, list) { | |
505 | struct list_head dev_resv_regions; | |
506 | ||
507 | INIT_LIST_HEAD(&dev_resv_regions); | |
508 | iommu_get_resv_regions(device->dev, &dev_resv_regions); | |
509 | ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); | |
510 | iommu_put_resv_regions(device->dev, &dev_resv_regions); | |
511 | if (ret) | |
512 | break; | |
513 | } | |
514 | mutex_unlock(&group->mutex); | |
515 | return ret; | |
516 | } | |
517 | EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); | |
518 | ||
bc7d12b9 EA |
519 | static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, |
520 | char *buf) | |
521 | { | |
522 | struct iommu_resv_region *region, *next; | |
523 | struct list_head group_resv_regions; | |
524 | char *str = buf; | |
525 | ||
526 | INIT_LIST_HEAD(&group_resv_regions); | |
527 | iommu_get_group_resv_regions(group, &group_resv_regions); | |
528 | ||
529 | list_for_each_entry_safe(region, next, &group_resv_regions, list) { | |
530 | str += sprintf(str, "0x%016llx 0x%016llx %s\n", | |
531 | (long long int)region->start, | |
532 | (long long int)(region->start + | |
533 | region->length - 1), | |
534 | iommu_group_resv_type_string[region->type]); | |
535 | kfree(region); | |
536 | } | |
537 | ||
538 | return (str - buf); | |
539 | } | |
540 | ||
c52c72d3 OJ |
541 | static ssize_t iommu_group_show_type(struct iommu_group *group, |
542 | char *buf) | |
543 | { | |
544 | char *type = "unknown\n"; | |
545 | ||
0b8a96a3 | 546 | mutex_lock(&group->mutex); |
c52c72d3 OJ |
547 | if (group->default_domain) { |
548 | switch (group->default_domain->type) { | |
549 | case IOMMU_DOMAIN_BLOCKED: | |
550 | type = "blocked\n"; | |
551 | break; | |
552 | case IOMMU_DOMAIN_IDENTITY: | |
553 | type = "identity\n"; | |
554 | break; | |
555 | case IOMMU_DOMAIN_UNMANAGED: | |
556 | type = "unmanaged\n"; | |
557 | break; | |
558 | case IOMMU_DOMAIN_DMA: | |
24f307d8 | 559 | type = "DMA\n"; |
c52c72d3 | 560 | break; |
bf3aed46 RM |
561 | case IOMMU_DOMAIN_DMA_FQ: |
562 | type = "DMA-FQ\n"; | |
563 | break; | |
c52c72d3 OJ |
564 | } |
565 | } | |
0b8a96a3 | 566 | mutex_unlock(&group->mutex); |
c52c72d3 OJ |
567 | strcpy(buf, type); |
568 | ||
569 | return strlen(type); | |
570 | } | |
571 | ||
d72e31c9 AW |
572 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
573 | ||
bc7d12b9 EA |
574 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
575 | iommu_group_show_resv_regions, NULL); | |
576 | ||
08a27c1c SPP |
577 | static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type, |
578 | iommu_group_store_type); | |
c52c72d3 | 579 | |
d72e31c9 AW |
580 | static void iommu_group_release(struct kobject *kobj) |
581 | { | |
582 | struct iommu_group *group = to_iommu_group(kobj); | |
583 | ||
269aa808 JR |
584 | pr_debug("Releasing group %d\n", group->id); |
585 | ||
d72e31c9 AW |
586 | if (group->iommu_data_release) |
587 | group->iommu_data_release(group->iommu_data); | |
588 | ||
feccf398 | 589 | ida_simple_remove(&iommu_group_ida, group->id); |
d72e31c9 | 590 | |
53723dc5 JR |
591 | if (group->default_domain) |
592 | iommu_domain_free(group->default_domain); | |
593 | ||
d72e31c9 AW |
594 | kfree(group->name); |
595 | kfree(group); | |
596 | } | |
597 | ||
598 | static struct kobj_type iommu_group_ktype = { | |
599 | .sysfs_ops = &iommu_group_sysfs_ops, | |
600 | .release = iommu_group_release, | |
601 | }; | |
602 | ||
603 | /** | |
604 | * iommu_group_alloc - Allocate a new group | |
d72e31c9 AW |
605 | * |
606 | * This function is called by an iommu driver to allocate a new iommu | |
607 | * group. The iommu group represents the minimum granularity of the iommu. | |
608 | * Upon successful return, the caller holds a reference to the supplied | |
609 | * group in order to hold the group until devices are added. Use | |
610 | * iommu_group_put() to release this extra reference count, allowing the | |
611 | * group to be automatically reclaimed once it has no devices or external | |
612 | * references. | |
613 | */ | |
614 | struct iommu_group *iommu_group_alloc(void) | |
1460432c | 615 | { |
d72e31c9 AW |
616 | struct iommu_group *group; |
617 | int ret; | |
618 | ||
619 | group = kzalloc(sizeof(*group), GFP_KERNEL); | |
620 | if (!group) | |
621 | return ERR_PTR(-ENOMEM); | |
622 | ||
623 | group->kobj.kset = iommu_group_kset; | |
624 | mutex_init(&group->mutex); | |
625 | INIT_LIST_HEAD(&group->devices); | |
41df6dcc | 626 | INIT_LIST_HEAD(&group->entry); |
d72e31c9 AW |
627 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); |
628 | ||
feccf398 HK |
629 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
630 | if (ret < 0) { | |
d72e31c9 | 631 | kfree(group); |
feccf398 | 632 | return ERR_PTR(ret); |
d72e31c9 | 633 | } |
feccf398 | 634 | group->id = ret; |
1460432c | 635 | |
d72e31c9 AW |
636 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
637 | NULL, "%d", group->id); | |
638 | if (ret) { | |
feccf398 | 639 | ida_simple_remove(&iommu_group_ida, group->id); |
7cc31613 | 640 | kobject_put(&group->kobj); |
d72e31c9 AW |
641 | return ERR_PTR(ret); |
642 | } | |
643 | ||
644 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); | |
645 | if (!group->devices_kobj) { | |
646 | kobject_put(&group->kobj); /* triggers .release & free */ | |
647 | return ERR_PTR(-ENOMEM); | |
648 | } | |
649 | ||
650 | /* | |
651 | * The devices_kobj holds a reference on the group kobject, so | |
652 | * as long as that exists so will the group. We can therefore | |
653 | * use the devices_kobj for reference counting. | |
654 | */ | |
655 | kobject_put(&group->kobj); | |
656 | ||
bc7d12b9 EA |
657 | ret = iommu_group_create_file(group, |
658 | &iommu_group_attr_reserved_regions); | |
659 | if (ret) | |
660 | return ERR_PTR(ret); | |
661 | ||
c52c72d3 OJ |
662 | ret = iommu_group_create_file(group, &iommu_group_attr_type); |
663 | if (ret) | |
664 | return ERR_PTR(ret); | |
665 | ||
269aa808 JR |
666 | pr_debug("Allocated group %d\n", group->id); |
667 | ||
d72e31c9 AW |
668 | return group; |
669 | } | |
670 | EXPORT_SYMBOL_GPL(iommu_group_alloc); | |
671 | ||
aa16bea9 AK |
672 | struct iommu_group *iommu_group_get_by_id(int id) |
673 | { | |
674 | struct kobject *group_kobj; | |
675 | struct iommu_group *group; | |
676 | const char *name; | |
677 | ||
678 | if (!iommu_group_kset) | |
679 | return NULL; | |
680 | ||
681 | name = kasprintf(GFP_KERNEL, "%d", id); | |
682 | if (!name) | |
683 | return NULL; | |
684 | ||
685 | group_kobj = kset_find_obj(iommu_group_kset, name); | |
686 | kfree(name); | |
687 | ||
688 | if (!group_kobj) | |
689 | return NULL; | |
690 | ||
691 | group = container_of(group_kobj, struct iommu_group, kobj); | |
692 | BUG_ON(group->id != id); | |
693 | ||
694 | kobject_get(group->devices_kobj); | |
695 | kobject_put(&group->kobj); | |
696 | ||
697 | return group; | |
698 | } | |
699 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); | |
700 | ||
d72e31c9 AW |
701 | /** |
702 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group | |
703 | * @group: the group | |
704 | * | |
705 | * iommu drivers can store data in the group for use when doing iommu | |
706 | * operations. This function provides a way to retrieve it. Caller | |
707 | * should hold a group reference. | |
708 | */ | |
709 | void *iommu_group_get_iommudata(struct iommu_group *group) | |
710 | { | |
711 | return group->iommu_data; | |
712 | } | |
713 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); | |
714 | ||
715 | /** | |
716 | * iommu_group_set_iommudata - set iommu_data for a group | |
717 | * @group: the group | |
718 | * @iommu_data: new data | |
719 | * @release: release function for iommu_data | |
720 | * | |
721 | * iommu drivers can store data in the group for use when doing iommu | |
722 | * operations. This function provides a way to set the data after | |
723 | * the group has been allocated. Caller should hold a group reference. | |
724 | */ | |
725 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, | |
726 | void (*release)(void *iommu_data)) | |
1460432c | 727 | { |
d72e31c9 AW |
728 | group->iommu_data = iommu_data; |
729 | group->iommu_data_release = release; | |
730 | } | |
731 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); | |
1460432c | 732 | |
d72e31c9 AW |
733 | /** |
734 | * iommu_group_set_name - set name for a group | |
735 | * @group: the group | |
736 | * @name: name | |
737 | * | |
738 | * Allow iommu driver to set a name for a group. When set it will | |
739 | * appear in a name attribute file under the group in sysfs. | |
740 | */ | |
741 | int iommu_group_set_name(struct iommu_group *group, const char *name) | |
742 | { | |
743 | int ret; | |
744 | ||
745 | if (group->name) { | |
746 | iommu_group_remove_file(group, &iommu_group_attr_name); | |
747 | kfree(group->name); | |
748 | group->name = NULL; | |
749 | if (!name) | |
750 | return 0; | |
751 | } | |
752 | ||
753 | group->name = kstrdup(name, GFP_KERNEL); | |
754 | if (!group->name) | |
755 | return -ENOMEM; | |
756 | ||
757 | ret = iommu_group_create_file(group, &iommu_group_attr_name); | |
758 | if (ret) { | |
759 | kfree(group->name); | |
760 | group->name = NULL; | |
761 | return ret; | |
762 | } | |
1460432c AW |
763 | |
764 | return 0; | |
765 | } | |
d72e31c9 | 766 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
1460432c | 767 | |
ce574c27 JR |
768 | static int iommu_create_device_direct_mappings(struct iommu_group *group, |
769 | struct device *dev) | |
beed2821 JR |
770 | { |
771 | struct iommu_domain *domain = group->default_domain; | |
e5b5234a | 772 | struct iommu_resv_region *entry; |
beed2821 JR |
773 | struct list_head mappings; |
774 | unsigned long pg_size; | |
775 | int ret = 0; | |
776 | ||
bf3aed46 | 777 | if (!domain || !iommu_is_dma_domain(domain)) |
beed2821 JR |
778 | return 0; |
779 | ||
d16e0faa | 780 | BUG_ON(!domain->pgsize_bitmap); |
beed2821 | 781 | |
d16e0faa | 782 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
beed2821 JR |
783 | INIT_LIST_HEAD(&mappings); |
784 | ||
e5b5234a | 785 | iommu_get_resv_regions(dev, &mappings); |
beed2821 JR |
786 | |
787 | /* We need to consider overlapping regions for different devices */ | |
788 | list_for_each_entry(entry, &mappings, list) { | |
789 | dma_addr_t start, end, addr; | |
093b32a8 | 790 | size_t map_size = 0; |
beed2821 | 791 | |
e5b5234a EA |
792 | if (domain->ops->apply_resv_region) |
793 | domain->ops->apply_resv_region(dev, domain, entry); | |
33b21a6b | 794 | |
beed2821 JR |
795 | start = ALIGN(entry->start, pg_size); |
796 | end = ALIGN(entry->start + entry->length, pg_size); | |
797 | ||
adfd3738 EA |
798 | if (entry->type != IOMMU_RESV_DIRECT && |
799 | entry->type != IOMMU_RESV_DIRECT_RELAXABLE) | |
544a25d9 EA |
800 | continue; |
801 | ||
093b32a8 | 802 | for (addr = start; addr <= end; addr += pg_size) { |
beed2821 JR |
803 | phys_addr_t phys_addr; |
804 | ||
093b32a8 YW |
805 | if (addr == end) |
806 | goto map_end; | |
807 | ||
beed2821 | 808 | phys_addr = iommu_iova_to_phys(domain, addr); |
093b32a8 YW |
809 | if (!phys_addr) { |
810 | map_size += pg_size; | |
beed2821 | 811 | continue; |
093b32a8 | 812 | } |
beed2821 | 813 | |
093b32a8 YW |
814 | map_end: |
815 | if (map_size) { | |
816 | ret = iommu_map(domain, addr - map_size, | |
817 | addr - map_size, map_size, | |
818 | entry->prot); | |
819 | if (ret) | |
820 | goto out; | |
821 | map_size = 0; | |
822 | } | |
beed2821 JR |
823 | } |
824 | ||
825 | } | |
826 | ||
aae4c8e2 | 827 | iommu_flush_iotlb_all(domain); |
add02cfd | 828 | |
beed2821 | 829 | out: |
e5b5234a | 830 | iommu_put_resv_regions(dev, &mappings); |
beed2821 JR |
831 | |
832 | return ret; | |
833 | } | |
834 | ||
bd421264 JR |
835 | static bool iommu_is_attach_deferred(struct iommu_domain *domain, |
836 | struct device *dev) | |
837 | { | |
838 | if (domain->ops->is_attach_deferred) | |
839 | return domain->ops->is_attach_deferred(domain, dev); | |
840 | ||
841 | return false; | |
842 | } | |
843 | ||
d72e31c9 AW |
844 | /** |
845 | * iommu_group_add_device - add a device to an iommu group | |
846 | * @group: the group into which to add the device (reference should be held) | |
847 | * @dev: the device | |
848 | * | |
849 | * This function is called by an iommu driver to add a device into a | |
850 | * group. Adding a device increments the group reference count. | |
851 | */ | |
852 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) | |
1460432c | 853 | { |
d72e31c9 | 854 | int ret, i = 0; |
c09e22d5 | 855 | struct group_device *device; |
d72e31c9 AW |
856 | |
857 | device = kzalloc(sizeof(*device), GFP_KERNEL); | |
858 | if (!device) | |
859 | return -ENOMEM; | |
860 | ||
861 | device->dev = dev; | |
1460432c | 862 | |
d72e31c9 | 863 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
797a8b4d RM |
864 | if (ret) |
865 | goto err_free_device; | |
d72e31c9 AW |
866 | |
867 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); | |
868 | rename: | |
869 | if (!device->name) { | |
797a8b4d RM |
870 | ret = -ENOMEM; |
871 | goto err_remove_link; | |
d72e31c9 | 872 | } |
1460432c | 873 | |
d72e31c9 AW |
874 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
875 | &dev->kobj, device->name); | |
876 | if (ret) { | |
d72e31c9 AW |
877 | if (ret == -EEXIST && i >= 0) { |
878 | /* | |
879 | * Account for the slim chance of collision | |
880 | * and append an instance to the name. | |
881 | */ | |
797a8b4d | 882 | kfree(device->name); |
d72e31c9 AW |
883 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
884 | kobject_name(&dev->kobj), i++); | |
885 | goto rename; | |
886 | } | |
797a8b4d | 887 | goto err_free_name; |
d72e31c9 AW |
888 | } |
889 | ||
890 | kobject_get(group->devices_kobj); | |
891 | ||
892 | dev->iommu_group = group; | |
893 | ||
894 | mutex_lock(&group->mutex); | |
895 | list_add_tail(&device->list, &group->devices); | |
bd421264 | 896 | if (group->domain && !iommu_is_attach_deferred(group->domain, dev)) |
797a8b4d | 897 | ret = __iommu_attach_device(group->domain, dev); |
d72e31c9 | 898 | mutex_unlock(&group->mutex); |
797a8b4d RM |
899 | if (ret) |
900 | goto err_put_group; | |
d72e31c9 AW |
901 | |
902 | /* Notify any listeners about change to group. */ | |
903 | blocking_notifier_call_chain(&group->notifier, | |
904 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); | |
d1cf7e82 SK |
905 | |
906 | trace_add_device_to_group(group->id, dev); | |
269aa808 | 907 | |
780da9e4 | 908 | dev_info(dev, "Adding to iommu group %d\n", group->id); |
269aa808 | 909 | |
1460432c | 910 | return 0; |
797a8b4d RM |
911 | |
912 | err_put_group: | |
913 | mutex_lock(&group->mutex); | |
914 | list_del(&device->list); | |
915 | mutex_unlock(&group->mutex); | |
916 | dev->iommu_group = NULL; | |
917 | kobject_put(group->devices_kobj); | |
7d4e6ccd | 918 | sysfs_remove_link(group->devices_kobj, device->name); |
797a8b4d RM |
919 | err_free_name: |
920 | kfree(device->name); | |
921 | err_remove_link: | |
922 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
923 | err_free_device: | |
924 | kfree(device); | |
780da9e4 | 925 | dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); |
797a8b4d | 926 | return ret; |
1460432c | 927 | } |
d72e31c9 | 928 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
1460432c | 929 | |
d72e31c9 AW |
930 | /** |
931 | * iommu_group_remove_device - remove a device from it's current group | |
932 | * @dev: device to be removed | |
933 | * | |
934 | * This function is called by an iommu driver to remove the device from | |
935 | * it's current group. This decrements the iommu group reference count. | |
936 | */ | |
937 | void iommu_group_remove_device(struct device *dev) | |
938 | { | |
939 | struct iommu_group *group = dev->iommu_group; | |
c09e22d5 | 940 | struct group_device *tmp_device, *device = NULL; |
d72e31c9 | 941 | |
5aa95d88 FW |
942 | if (!group) |
943 | return; | |
944 | ||
780da9e4 | 945 | dev_info(dev, "Removing from iommu group %d\n", group->id); |
269aa808 | 946 | |
d72e31c9 AW |
947 | /* Pre-notify listeners that a device is being removed. */ |
948 | blocking_notifier_call_chain(&group->notifier, | |
949 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); | |
950 | ||
951 | mutex_lock(&group->mutex); | |
952 | list_for_each_entry(tmp_device, &group->devices, list) { | |
953 | if (tmp_device->dev == dev) { | |
954 | device = tmp_device; | |
955 | list_del(&device->list); | |
956 | break; | |
957 | } | |
958 | } | |
959 | mutex_unlock(&group->mutex); | |
960 | ||
961 | if (!device) | |
962 | return; | |
963 | ||
964 | sysfs_remove_link(group->devices_kobj, device->name); | |
965 | sysfs_remove_link(&dev->kobj, "iommu_group"); | |
966 | ||
2e757086 SK |
967 | trace_remove_device_from_group(group->id, dev); |
968 | ||
d72e31c9 AW |
969 | kfree(device->name); |
970 | kfree(device); | |
971 | dev->iommu_group = NULL; | |
972 | kobject_put(group->devices_kobj); | |
973 | } | |
974 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); | |
975 | ||
426a2738 JR |
976 | static int iommu_group_device_count(struct iommu_group *group) |
977 | { | |
c09e22d5 | 978 | struct group_device *entry; |
426a2738 JR |
979 | int ret = 0; |
980 | ||
981 | list_for_each_entry(entry, &group->devices, list) | |
982 | ret++; | |
983 | ||
984 | return ret; | |
985 | } | |
986 | ||
d72e31c9 AW |
987 | /** |
988 | * iommu_group_for_each_dev - iterate over each device in the group | |
989 | * @group: the group | |
990 | * @data: caller opaque data to be passed to callback function | |
991 | * @fn: caller supplied callback function | |
992 | * | |
993 | * This function is called by group users to iterate over group devices. | |
994 | * Callers should hold a reference count to the group during callback. | |
995 | * The group->mutex is held across callbacks, which will block calls to | |
996 | * iommu_group_add/remove_device. | |
997 | */ | |
e39cb8a3 JR |
998 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
999 | int (*fn)(struct device *, void *)) | |
d72e31c9 | 1000 | { |
c09e22d5 | 1001 | struct group_device *device; |
d72e31c9 AW |
1002 | int ret = 0; |
1003 | ||
d72e31c9 AW |
1004 | list_for_each_entry(device, &group->devices, list) { |
1005 | ret = fn(device->dev, data); | |
1006 | if (ret) | |
1007 | break; | |
1008 | } | |
e39cb8a3 JR |
1009 | return ret; |
1010 | } | |
1011 | ||
1012 | ||
1013 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, | |
1014 | int (*fn)(struct device *, void *)) | |
1015 | { | |
1016 | int ret; | |
1017 | ||
1018 | mutex_lock(&group->mutex); | |
1019 | ret = __iommu_group_for_each_dev(group, data, fn); | |
d72e31c9 | 1020 | mutex_unlock(&group->mutex); |
e39cb8a3 | 1021 | |
d72e31c9 AW |
1022 | return ret; |
1023 | } | |
1024 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); | |
1025 | ||
1026 | /** | |
1027 | * iommu_group_get - Return the group for a device and increment reference | |
1028 | * @dev: get the group that this device belongs to | |
1029 | * | |
1030 | * This function is called by iommu drivers and users to get the group | |
1031 | * for the specified device. If found, the group is returned and the group | |
1032 | * reference in incremented, else NULL. | |
1033 | */ | |
1034 | struct iommu_group *iommu_group_get(struct device *dev) | |
1035 | { | |
1036 | struct iommu_group *group = dev->iommu_group; | |
1037 | ||
1038 | if (group) | |
1039 | kobject_get(group->devices_kobj); | |
1040 | ||
1041 | return group; | |
1042 | } | |
1043 | EXPORT_SYMBOL_GPL(iommu_group_get); | |
1044 | ||
13f59a78 RM |
1045 | /** |
1046 | * iommu_group_ref_get - Increment reference on a group | |
1047 | * @group: the group to use, must not be NULL | |
1048 | * | |
1049 | * This function is called by iommu drivers to take additional references on an | |
1050 | * existing group. Returns the given group for convenience. | |
1051 | */ | |
1052 | struct iommu_group *iommu_group_ref_get(struct iommu_group *group) | |
1053 | { | |
1054 | kobject_get(group->devices_kobj); | |
1055 | return group; | |
1056 | } | |
a7ba5c3d | 1057 | EXPORT_SYMBOL_GPL(iommu_group_ref_get); |
13f59a78 | 1058 | |
d72e31c9 AW |
1059 | /** |
1060 | * iommu_group_put - Decrement group reference | |
1061 | * @group: the group to use | |
1062 | * | |
1063 | * This function is called by iommu drivers and users to release the | |
1064 | * iommu group. Once the reference count is zero, the group is released. | |
1065 | */ | |
1066 | void iommu_group_put(struct iommu_group *group) | |
1067 | { | |
1068 | if (group) | |
1069 | kobject_put(group->devices_kobj); | |
1070 | } | |
1071 | EXPORT_SYMBOL_GPL(iommu_group_put); | |
1072 | ||
1073 | /** | |
1074 | * iommu_group_register_notifier - Register a notifier for group changes | |
1075 | * @group: the group to watch | |
1076 | * @nb: notifier block to signal | |
1077 | * | |
1078 | * This function allows iommu group users to track changes in a group. | |
1079 | * See include/linux/iommu.h for actions sent via this notifier. Caller | |
1080 | * should hold a reference to the group throughout notifier registration. | |
1081 | */ | |
1082 | int iommu_group_register_notifier(struct iommu_group *group, | |
1083 | struct notifier_block *nb) | |
1084 | { | |
1085 | return blocking_notifier_chain_register(&group->notifier, nb); | |
1086 | } | |
1087 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); | |
1088 | ||
1089 | /** | |
1090 | * iommu_group_unregister_notifier - Unregister a notifier | |
1091 | * @group: the group to watch | |
1092 | * @nb: notifier block to signal | |
1093 | * | |
1094 | * Unregister a previously registered group notifier block. | |
1095 | */ | |
1096 | int iommu_group_unregister_notifier(struct iommu_group *group, | |
1097 | struct notifier_block *nb) | |
1098 | { | |
1099 | return blocking_notifier_chain_unregister(&group->notifier, nb); | |
1100 | } | |
1101 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); | |
1102 | ||
0c830e6b JP |
1103 | /** |
1104 | * iommu_register_device_fault_handler() - Register a device fault handler | |
1105 | * @dev: the device | |
1106 | * @handler: the fault handler | |
1107 | * @data: private data passed as argument to the handler | |
1108 | * | |
1109 | * When an IOMMU fault event is received, this handler gets called with the | |
bf3255b3 JPB |
1110 | * fault event and data as argument. The handler should return 0 on success. If |
1111 | * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also | |
1112 | * complete the fault by calling iommu_page_response() with one of the following | |
1113 | * response code: | |
1114 | * - IOMMU_PAGE_RESP_SUCCESS: retry the translation | |
1115 | * - IOMMU_PAGE_RESP_INVALID: terminate the fault | |
1116 | * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting | |
1117 | * page faults if possible. | |
0c830e6b JP |
1118 | * |
1119 | * Return 0 if the fault handler was installed successfully, or an error. | |
1120 | */ | |
1121 | int iommu_register_device_fault_handler(struct device *dev, | |
1122 | iommu_dev_fault_handler_t handler, | |
1123 | void *data) | |
1124 | { | |
045a7042 | 1125 | struct dev_iommu *param = dev->iommu; |
0c830e6b JP |
1126 | int ret = 0; |
1127 | ||
1128 | if (!param) | |
1129 | return -EINVAL; | |
1130 | ||
1131 | mutex_lock(¶m->lock); | |
1132 | /* Only allow one fault handler registered for each device */ | |
1133 | if (param->fault_param) { | |
1134 | ret = -EBUSY; | |
1135 | goto done_unlock; | |
1136 | } | |
1137 | ||
1138 | get_device(dev); | |
1139 | param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL); | |
1140 | if (!param->fault_param) { | |
1141 | put_device(dev); | |
1142 | ret = -ENOMEM; | |
1143 | goto done_unlock; | |
1144 | } | |
1145 | param->fault_param->handler = handler; | |
1146 | param->fault_param->data = data; | |
bf3255b3 JPB |
1147 | mutex_init(¶m->fault_param->lock); |
1148 | INIT_LIST_HEAD(¶m->fault_param->faults); | |
0c830e6b JP |
1149 | |
1150 | done_unlock: | |
1151 | mutex_unlock(¶m->lock); | |
1152 | ||
1153 | return ret; | |
1154 | } | |
1155 | EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler); | |
1156 | ||
1157 | /** | |
1158 | * iommu_unregister_device_fault_handler() - Unregister the device fault handler | |
1159 | * @dev: the device | |
1160 | * | |
1161 | * Remove the device fault handler installed with | |
1162 | * iommu_register_device_fault_handler(). | |
1163 | * | |
1164 | * Return 0 on success, or an error. | |
1165 | */ | |
1166 | int iommu_unregister_device_fault_handler(struct device *dev) | |
1167 | { | |
045a7042 | 1168 | struct dev_iommu *param = dev->iommu; |
0c830e6b JP |
1169 | int ret = 0; |
1170 | ||
1171 | if (!param) | |
1172 | return -EINVAL; | |
1173 | ||
1174 | mutex_lock(¶m->lock); | |
1175 | ||
1176 | if (!param->fault_param) | |
1177 | goto unlock; | |
1178 | ||
bf3255b3 JPB |
1179 | /* we cannot unregister handler if there are pending faults */ |
1180 | if (!list_empty(¶m->fault_param->faults)) { | |
1181 | ret = -EBUSY; | |
1182 | goto unlock; | |
1183 | } | |
1184 | ||
0c830e6b JP |
1185 | kfree(param->fault_param); |
1186 | param->fault_param = NULL; | |
1187 | put_device(dev); | |
1188 | unlock: | |
1189 | mutex_unlock(¶m->lock); | |
1190 | ||
1191 | return ret; | |
1192 | } | |
1193 | EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler); | |
1194 | ||
1195 | /** | |
1196 | * iommu_report_device_fault() - Report fault event to device driver | |
1197 | * @dev: the device | |
1198 | * @evt: fault event data | |
1199 | * | |
1200 | * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ | |
bf3255b3 JPB |
1201 | * handler. When this function fails and the fault is recoverable, it is the |
1202 | * caller's responsibility to complete the fault. | |
0c830e6b JP |
1203 | * |
1204 | * Return 0 on success, or an error. | |
1205 | */ | |
1206 | int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt) | |
1207 | { | |
045a7042 | 1208 | struct dev_iommu *param = dev->iommu; |
bf3255b3 | 1209 | struct iommu_fault_event *evt_pending = NULL; |
0c830e6b JP |
1210 | struct iommu_fault_param *fparam; |
1211 | int ret = 0; | |
1212 | ||
1213 | if (!param || !evt) | |
1214 | return -EINVAL; | |
1215 | ||
1216 | /* we only report device fault if there is a handler registered */ | |
1217 | mutex_lock(¶m->lock); | |
1218 | fparam = param->fault_param; | |
1219 | if (!fparam || !fparam->handler) { | |
1220 | ret = -EINVAL; | |
1221 | goto done_unlock; | |
1222 | } | |
bf3255b3 JPB |
1223 | |
1224 | if (evt->fault.type == IOMMU_FAULT_PAGE_REQ && | |
1225 | (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) { | |
1226 | evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event), | |
1227 | GFP_KERNEL); | |
1228 | if (!evt_pending) { | |
1229 | ret = -ENOMEM; | |
1230 | goto done_unlock; | |
1231 | } | |
1232 | mutex_lock(&fparam->lock); | |
1233 | list_add_tail(&evt_pending->list, &fparam->faults); | |
1234 | mutex_unlock(&fparam->lock); | |
1235 | } | |
1236 | ||
0c830e6b | 1237 | ret = fparam->handler(&evt->fault, fparam->data); |
bf3255b3 JPB |
1238 | if (ret && evt_pending) { |
1239 | mutex_lock(&fparam->lock); | |
1240 | list_del(&evt_pending->list); | |
1241 | mutex_unlock(&fparam->lock); | |
1242 | kfree(evt_pending); | |
1243 | } | |
0c830e6b JP |
1244 | done_unlock: |
1245 | mutex_unlock(¶m->lock); | |
1246 | return ret; | |
1247 | } | |
1248 | EXPORT_SYMBOL_GPL(iommu_report_device_fault); | |
1249 | ||
bf3255b3 JPB |
1250 | int iommu_page_response(struct device *dev, |
1251 | struct iommu_page_response *msg) | |
1252 | { | |
97047191 | 1253 | bool needs_pasid; |
bf3255b3 JPB |
1254 | int ret = -EINVAL; |
1255 | struct iommu_fault_event *evt; | |
1256 | struct iommu_fault_page_request *prm; | |
045a7042 | 1257 | struct dev_iommu *param = dev->iommu; |
97047191 | 1258 | bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID; |
bf3255b3 JPB |
1259 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
1260 | ||
1261 | if (!domain || !domain->ops->page_response) | |
1262 | return -ENODEV; | |
1263 | ||
1264 | if (!param || !param->fault_param) | |
1265 | return -EINVAL; | |
1266 | ||
1267 | if (msg->version != IOMMU_PAGE_RESP_VERSION_1 || | |
1268 | msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID) | |
1269 | return -EINVAL; | |
1270 | ||
1271 | /* Only send response if there is a fault report pending */ | |
1272 | mutex_lock(¶m->fault_param->lock); | |
1273 | if (list_empty(¶m->fault_param->faults)) { | |
1274 | dev_warn_ratelimited(dev, "no pending PRQ, drop response\n"); | |
1275 | goto done_unlock; | |
1276 | } | |
1277 | /* | |
1278 | * Check if we have a matching page request pending to respond, | |
1279 | * otherwise return -EINVAL | |
1280 | */ | |
1281 | list_for_each_entry(evt, ¶m->fault_param->faults, list) { | |
1282 | prm = &evt->fault.prm; | |
97047191 JPB |
1283 | if (prm->grpid != msg->grpid) |
1284 | continue; | |
bf3255b3 | 1285 | |
97047191 JPB |
1286 | /* |
1287 | * If the PASID is required, the corresponding request is | |
1288 | * matched using the group ID, the PASID valid bit and the PASID | |
1289 | * value. Otherwise only the group ID matches request and | |
1290 | * response. | |
1291 | */ | |
1292 | needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID; | |
1293 | if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid)) | |
bf3255b3 JPB |
1294 | continue; |
1295 | ||
97047191 JPB |
1296 | if (!needs_pasid && has_pasid) { |
1297 | /* No big deal, just clear it. */ | |
1298 | msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID; | |
1299 | msg->pasid = 0; | |
1300 | } | |
bf3255b3 JPB |
1301 | |
1302 | ret = domain->ops->page_response(dev, evt, msg); | |
1303 | list_del(&evt->list); | |
1304 | kfree(evt); | |
1305 | break; | |
1306 | } | |
1307 | ||
1308 | done_unlock: | |
1309 | mutex_unlock(¶m->fault_param->lock); | |
1310 | return ret; | |
1311 | } | |
1312 | EXPORT_SYMBOL_GPL(iommu_page_response); | |
1313 | ||
d72e31c9 AW |
1314 | /** |
1315 | * iommu_group_id - Return ID for a group | |
1316 | * @group: the group to ID | |
1317 | * | |
1318 | * Return the unique ID for the group matching the sysfs group number. | |
1319 | */ | |
1320 | int iommu_group_id(struct iommu_group *group) | |
1321 | { | |
1322 | return group->id; | |
1323 | } | |
1324 | EXPORT_SYMBOL_GPL(iommu_group_id); | |
1460432c | 1325 | |
f096c061 AW |
1326 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
1327 | unsigned long *devfns); | |
1328 | ||
104a1c13 AW |
1329 | /* |
1330 | * To consider a PCI device isolated, we require ACS to support Source | |
1331 | * Validation, Request Redirection, Completer Redirection, and Upstream | |
1332 | * Forwarding. This effectively means that devices cannot spoof their | |
1333 | * requester ID, requests and completions cannot be redirected, and all | |
1334 | * transactions are forwarded upstream, even as it passes through a | |
1335 | * bridge where the target device is downstream. | |
1336 | */ | |
1337 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) | |
1338 | ||
f096c061 AW |
1339 | /* |
1340 | * For multifunction devices which are not isolated from each other, find | |
1341 | * all the other non-isolated functions and look for existing groups. For | |
1342 | * each function, we also need to look for aliases to or from other devices | |
1343 | * that may already have a group. | |
1344 | */ | |
1345 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, | |
1346 | unsigned long *devfns) | |
1347 | { | |
1348 | struct pci_dev *tmp = NULL; | |
1349 | struct iommu_group *group; | |
1350 | ||
1351 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) | |
1352 | return NULL; | |
1353 | ||
1354 | for_each_pci_dev(tmp) { | |
1355 | if (tmp == pdev || tmp->bus != pdev->bus || | |
1356 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || | |
1357 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) | |
1358 | continue; | |
1359 | ||
1360 | group = get_pci_alias_group(tmp, devfns); | |
1361 | if (group) { | |
1362 | pci_dev_put(tmp); | |
1363 | return group; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | return NULL; | |
1368 | } | |
1369 | ||
1370 | /* | |
338c3149 JL |
1371 | * Look for aliases to or from the given device for existing groups. DMA |
1372 | * aliases are only supported on the same bus, therefore the search | |
f096c061 AW |
1373 | * space is quite small (especially since we're really only looking at pcie |
1374 | * device, and therefore only expect multiple slots on the root complex or | |
1375 | * downstream switch ports). It's conceivable though that a pair of | |
1376 | * multifunction devices could have aliases between them that would cause a | |
1377 | * loop. To prevent this, we use a bitmap to track where we've been. | |
1378 | */ | |
1379 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, | |
1380 | unsigned long *devfns) | |
1381 | { | |
1382 | struct pci_dev *tmp = NULL; | |
1383 | struct iommu_group *group; | |
1384 | ||
1385 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) | |
1386 | return NULL; | |
1387 | ||
1388 | group = iommu_group_get(&pdev->dev); | |
1389 | if (group) | |
1390 | return group; | |
1391 | ||
1392 | for_each_pci_dev(tmp) { | |
1393 | if (tmp == pdev || tmp->bus != pdev->bus) | |
1394 | continue; | |
1395 | ||
1396 | /* We alias them or they alias us */ | |
338c3149 | 1397 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
f096c061 AW |
1398 | group = get_pci_alias_group(tmp, devfns); |
1399 | if (group) { | |
1400 | pci_dev_put(tmp); | |
1401 | return group; | |
1402 | } | |
1403 | ||
1404 | group = get_pci_function_alias_group(tmp, devfns); | |
1405 | if (group) { | |
1406 | pci_dev_put(tmp); | |
1407 | return group; | |
1408 | } | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | return NULL; | |
1413 | } | |
1414 | ||
104a1c13 AW |
1415 | struct group_for_pci_data { |
1416 | struct pci_dev *pdev; | |
1417 | struct iommu_group *group; | |
1418 | }; | |
1419 | ||
1420 | /* | |
1421 | * DMA alias iterator callback, return the last seen device. Stop and return | |
1422 | * the IOMMU group if we find one along the way. | |
1423 | */ | |
1424 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) | |
1425 | { | |
1426 | struct group_for_pci_data *data = opaque; | |
1427 | ||
1428 | data->pdev = pdev; | |
1429 | data->group = iommu_group_get(&pdev->dev); | |
1430 | ||
1431 | return data->group != NULL; | |
1432 | } | |
1433 | ||
6eab556a JR |
1434 | /* |
1435 | * Generic device_group call-back function. It just allocates one | |
1436 | * iommu-group per device. | |
1437 | */ | |
1438 | struct iommu_group *generic_device_group(struct device *dev) | |
1439 | { | |
7f7a2304 | 1440 | return iommu_group_alloc(); |
6eab556a | 1441 | } |
a7ba5c3d | 1442 | EXPORT_SYMBOL_GPL(generic_device_group); |
6eab556a | 1443 | |
104a1c13 AW |
1444 | /* |
1445 | * Use standard PCI bus topology, isolation features, and DMA alias quirks | |
1446 | * to find or create an IOMMU group for a device. | |
1447 | */ | |
5e62292b | 1448 | struct iommu_group *pci_device_group(struct device *dev) |
104a1c13 | 1449 | { |
5e62292b | 1450 | struct pci_dev *pdev = to_pci_dev(dev); |
104a1c13 AW |
1451 | struct group_for_pci_data data; |
1452 | struct pci_bus *bus; | |
1453 | struct iommu_group *group = NULL; | |
f096c061 | 1454 | u64 devfns[4] = { 0 }; |
104a1c13 | 1455 | |
5e62292b JR |
1456 | if (WARN_ON(!dev_is_pci(dev))) |
1457 | return ERR_PTR(-EINVAL); | |
1458 | ||
104a1c13 AW |
1459 | /* |
1460 | * Find the upstream DMA alias for the device. A device must not | |
1461 | * be aliased due to topology in order to have its own IOMMU group. | |
1462 | * If we find an alias along the way that already belongs to a | |
1463 | * group, use it. | |
1464 | */ | |
1465 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) | |
1466 | return data.group; | |
1467 | ||
1468 | pdev = data.pdev; | |
1469 | ||
1470 | /* | |
1471 | * Continue upstream from the point of minimum IOMMU granularity | |
1472 | * due to aliases to the point where devices are protected from | |
1473 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing | |
1474 | * group, use it. | |
1475 | */ | |
1476 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { | |
1477 | if (!bus->self) | |
1478 | continue; | |
1479 | ||
1480 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) | |
1481 | break; | |
1482 | ||
1483 | pdev = bus->self; | |
1484 | ||
1485 | group = iommu_group_get(&pdev->dev); | |
1486 | if (group) | |
1487 | return group; | |
1488 | } | |
1489 | ||
1490 | /* | |
f096c061 AW |
1491 | * Look for existing groups on device aliases. If we alias another |
1492 | * device or another device aliases us, use the same group. | |
104a1c13 | 1493 | */ |
f096c061 AW |
1494 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
1495 | if (group) | |
1496 | return group; | |
104a1c13 AW |
1497 | |
1498 | /* | |
f096c061 AW |
1499 | * Look for existing groups on non-isolated functions on the same |
1500 | * slot and aliases of those funcions, if any. No need to clear | |
1501 | * the search bitmap, the tested devfns are still valid. | |
104a1c13 | 1502 | */ |
f096c061 AW |
1503 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
1504 | if (group) | |
1505 | return group; | |
104a1c13 AW |
1506 | |
1507 | /* No shared group found, allocate new */ | |
7f7a2304 | 1508 | return iommu_group_alloc(); |
104a1c13 | 1509 | } |
a7ba5c3d | 1510 | EXPORT_SYMBOL_GPL(pci_device_group); |
104a1c13 | 1511 | |
eab03e2a NG |
1512 | /* Get the IOMMU group for device on fsl-mc bus */ |
1513 | struct iommu_group *fsl_mc_device_group(struct device *dev) | |
1514 | { | |
1515 | struct device *cont_dev = fsl_mc_cont_dev(dev); | |
1516 | struct iommu_group *group; | |
1517 | ||
1518 | group = iommu_group_get(cont_dev); | |
1519 | if (!group) | |
1520 | group = iommu_group_alloc(); | |
1521 | return group; | |
1522 | } | |
a7ba5c3d | 1523 | EXPORT_SYMBOL_GPL(fsl_mc_device_group); |
eab03e2a | 1524 | |
4cbf3851 SPP |
1525 | static int iommu_get_def_domain_type(struct device *dev) |
1526 | { | |
1527 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
28b41e2c LB |
1528 | |
1529 | if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted) | |
1530 | return IOMMU_DOMAIN_DMA; | |
4cbf3851 SPP |
1531 | |
1532 | if (ops->def_domain_type) | |
28b41e2c | 1533 | return ops->def_domain_type(dev); |
4cbf3851 | 1534 | |
28b41e2c | 1535 | return 0; |
4cbf3851 SPP |
1536 | } |
1537 | ||
6e1aa204 JR |
1538 | static int iommu_group_alloc_default_domain(struct bus_type *bus, |
1539 | struct iommu_group *group, | |
1540 | unsigned int type) | |
ff2a08b3 JR |
1541 | { |
1542 | struct iommu_domain *dom; | |
ff2a08b3 | 1543 | |
6e1aa204 | 1544 | dom = __iommu_domain_alloc(bus, type); |
4cbf3851 | 1545 | if (!dom && type != IOMMU_DOMAIN_DMA) { |
6e1aa204 JR |
1546 | dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA); |
1547 | if (dom) | |
1548 | pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA", | |
1549 | type, group->name); | |
ff2a08b3 JR |
1550 | } |
1551 | ||
1552 | if (!dom) | |
1553 | return -ENOMEM; | |
1554 | ||
1555 | group->default_domain = dom; | |
1556 | if (!group->domain) | |
1557 | group->domain = dom; | |
ff2a08b3 JR |
1558 | return 0; |
1559 | } | |
1560 | ||
79659190 JR |
1561 | static int iommu_alloc_default_domain(struct iommu_group *group, |
1562 | struct device *dev) | |
6e1aa204 | 1563 | { |
6e1aa204 JR |
1564 | unsigned int type; |
1565 | ||
6e1aa204 JR |
1566 | if (group->default_domain) |
1567 | return 0; | |
1568 | ||
28b41e2c | 1569 | type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type; |
6e1aa204 JR |
1570 | |
1571 | return iommu_group_alloc_default_domain(dev->bus, group, type); | |
1572 | } | |
1573 | ||
104a1c13 AW |
1574 | /** |
1575 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device | |
1576 | * @dev: target device | |
1577 | * | |
1578 | * This function is intended to be called by IOMMU drivers and extended to | |
1579 | * support common, bus-defined algorithms when determining or creating the | |
1580 | * IOMMU group for a device. On success, the caller will hold a reference | |
1581 | * to the returned IOMMU group, which will already include the provided | |
1582 | * device. The reference should be released with iommu_group_put(). | |
1583 | */ | |
1b032ec1 | 1584 | static struct iommu_group *iommu_group_get_for_dev(struct device *dev) |
104a1c13 | 1585 | { |
46c6b2bc | 1586 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
c4a783b8 | 1587 | struct iommu_group *group; |
104a1c13 AW |
1588 | int ret; |
1589 | ||
1590 | group = iommu_group_get(dev); | |
1591 | if (group) | |
1592 | return group; | |
1593 | ||
05f80300 RM |
1594 | if (!ops) |
1595 | return ERR_PTR(-EINVAL); | |
104a1c13 | 1596 | |
05f80300 | 1597 | group = ops->device_group(dev); |
72dcac63 JR |
1598 | if (WARN_ON_ONCE(group == NULL)) |
1599 | return ERR_PTR(-EINVAL); | |
1600 | ||
104a1c13 AW |
1601 | if (IS_ERR(group)) |
1602 | return group; | |
1603 | ||
1604 | ret = iommu_group_add_device(group, dev); | |
6e1aa204 JR |
1605 | if (ret) |
1606 | goto out_put_group; | |
104a1c13 AW |
1607 | |
1608 | return group; | |
ff2a08b3 JR |
1609 | |
1610 | out_put_group: | |
1611 | iommu_group_put(group); | |
1612 | ||
1613 | return ERR_PTR(ret); | |
104a1c13 AW |
1614 | } |
1615 | ||
6827ca83 JR |
1616 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
1617 | { | |
1618 | return group->default_domain; | |
1619 | } | |
1620 | ||
deac0b3b | 1621 | static int probe_iommu_group(struct device *dev, void *data) |
1460432c | 1622 | { |
deac0b3b | 1623 | struct list_head *group_list = data; |
5012c396 | 1624 | struct iommu_group *group; |
deac0b3b | 1625 | int ret; |
38667f18 | 1626 | |
5012c396 JR |
1627 | /* Device is probed already if in a group */ |
1628 | group = iommu_group_get(dev); | |
1629 | if (group) { | |
1630 | iommu_group_put(group); | |
1631 | return 0; | |
1632 | } | |
1633 | ||
deac0b3b | 1634 | ret = __iommu_probe_device(dev, group_list); |
38667f18 JR |
1635 | if (ret == -ENODEV) |
1636 | ret = 0; | |
1637 | ||
1638 | return ret; | |
1460432c AW |
1639 | } |
1640 | ||
8da30142 JR |
1641 | static int remove_iommu_group(struct device *dev, void *data) |
1642 | { | |
cc5aed44 | 1643 | iommu_release_device(dev); |
1460432c AW |
1644 | |
1645 | return 0; | |
1646 | } | |
1647 | ||
d72e31c9 AW |
1648 | static int iommu_bus_notifier(struct notifier_block *nb, |
1649 | unsigned long action, void *data) | |
1460432c | 1650 | { |
cc5aed44 | 1651 | unsigned long group_action = 0; |
1460432c | 1652 | struct device *dev = data; |
d72e31c9 | 1653 | struct iommu_group *group; |
d72e31c9 AW |
1654 | |
1655 | /* | |
1656 | * ADD/DEL call into iommu driver ops if provided, which may | |
1657 | * result in ADD/DEL notifiers to group->notifier | |
1658 | */ | |
1659 | if (action == BUS_NOTIFY_ADD_DEVICE) { | |
cc5aed44 | 1660 | int ret; |
3ba8775f | 1661 | |
cc5aed44 JR |
1662 | ret = iommu_probe_device(dev); |
1663 | return (ret) ? NOTIFY_DONE : NOTIFY_OK; | |
843cb6dc | 1664 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
cc5aed44 JR |
1665 | iommu_release_device(dev); |
1666 | return NOTIFY_OK; | |
d72e31c9 | 1667 | } |
1460432c | 1668 | |
d72e31c9 AW |
1669 | /* |
1670 | * Remaining BUS_NOTIFYs get filtered and republished to the | |
1671 | * group, if anyone is listening | |
1672 | */ | |
1673 | group = iommu_group_get(dev); | |
1674 | if (!group) | |
1675 | return 0; | |
1460432c | 1676 | |
d72e31c9 AW |
1677 | switch (action) { |
1678 | case BUS_NOTIFY_BIND_DRIVER: | |
1679 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; | |
1680 | break; | |
1681 | case BUS_NOTIFY_BOUND_DRIVER: | |
1682 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; | |
1683 | break; | |
1684 | case BUS_NOTIFY_UNBIND_DRIVER: | |
1685 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; | |
1686 | break; | |
1687 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
1688 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; | |
1689 | break; | |
1690 | } | |
1460432c | 1691 | |
d72e31c9 AW |
1692 | if (group_action) |
1693 | blocking_notifier_call_chain(&group->notifier, | |
1694 | group_action, dev); | |
1460432c | 1695 | |
d72e31c9 | 1696 | iommu_group_put(group); |
1460432c AW |
1697 | return 0; |
1698 | } | |
1699 | ||
deac0b3b JR |
1700 | struct __group_domain_type { |
1701 | struct device *dev; | |
1702 | unsigned int type; | |
1703 | }; | |
1704 | ||
1705 | static int probe_get_default_domain_type(struct device *dev, void *data) | |
1706 | { | |
deac0b3b | 1707 | struct __group_domain_type *gtype = data; |
28b41e2c | 1708 | unsigned int type = iommu_get_def_domain_type(dev); |
deac0b3b JR |
1709 | |
1710 | if (type) { | |
1711 | if (gtype->type && gtype->type != type) { | |
1712 | dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n", | |
1713 | iommu_domain_type_str(type), | |
1714 | dev_name(gtype->dev), | |
1715 | iommu_domain_type_str(gtype->type)); | |
1716 | gtype->type = 0; | |
1717 | } | |
1718 | ||
1719 | if (!gtype->dev) { | |
1720 | gtype->dev = dev; | |
1721 | gtype->type = type; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | static void probe_alloc_default_domain(struct bus_type *bus, | |
1729 | struct iommu_group *group) | |
1730 | { | |
1731 | struct __group_domain_type gtype; | |
1732 | ||
1733 | memset(>ype, 0, sizeof(gtype)); | |
1734 | ||
1735 | /* Ask for default domain requirements of all devices in the group */ | |
1736 | __iommu_group_for_each_dev(group, >ype, | |
1737 | probe_get_default_domain_type); | |
1738 | ||
1739 | if (!gtype.type) | |
1740 | gtype.type = iommu_def_domain_type; | |
1741 | ||
1742 | iommu_group_alloc_default_domain(bus, group, gtype.type); | |
ce574c27 | 1743 | |
deac0b3b JR |
1744 | } |
1745 | ||
1746 | static int iommu_group_do_dma_attach(struct device *dev, void *data) | |
1747 | { | |
1748 | struct iommu_domain *domain = data; | |
431275af JR |
1749 | int ret = 0; |
1750 | ||
1751 | if (!iommu_is_attach_deferred(domain, dev)) | |
1752 | ret = __iommu_attach_device(domain, dev); | |
deac0b3b | 1753 | |
431275af | 1754 | return ret; |
deac0b3b JR |
1755 | } |
1756 | ||
1757 | static int __iommu_group_dma_attach(struct iommu_group *group) | |
1758 | { | |
1759 | return __iommu_group_for_each_dev(group, group->default_domain, | |
1760 | iommu_group_do_dma_attach); | |
1761 | } | |
1762 | ||
70b8170e JR |
1763 | static int iommu_group_do_probe_finalize(struct device *dev, void *data) |
1764 | { | |
1765 | struct iommu_domain *domain = data; | |
1766 | ||
1767 | if (domain->ops->probe_finalize) | |
1768 | domain->ops->probe_finalize(dev); | |
1769 | ||
1770 | return 0; | |
1771 | } | |
1772 | ||
1773 | static void __iommu_group_dma_finalize(struct iommu_group *group) | |
1774 | { | |
1775 | __iommu_group_for_each_dev(group, group->default_domain, | |
1776 | iommu_group_do_probe_finalize); | |
1777 | } | |
cc69fc48 | 1778 | |
ce574c27 JR |
1779 | static int iommu_do_create_direct_mappings(struct device *dev, void *data) |
1780 | { | |
1781 | struct iommu_group *group = data; | |
1782 | ||
1783 | iommu_create_device_direct_mappings(group, dev); | |
1784 | ||
1785 | return 0; | |
1786 | } | |
1787 | ||
1788 | static int iommu_group_create_direct_mappings(struct iommu_group *group) | |
1789 | { | |
1790 | return __iommu_group_for_each_dev(group, group, | |
1791 | iommu_do_create_direct_mappings); | |
1792 | } | |
1793 | ||
5012c396 | 1794 | int bus_iommu_probe(struct bus_type *bus) |
deac0b3b | 1795 | { |
3eeeb45c JR |
1796 | struct iommu_group *group, *next; |
1797 | LIST_HEAD(group_list); | |
deac0b3b JR |
1798 | int ret; |
1799 | ||
3eeeb45c JR |
1800 | /* |
1801 | * This code-path does not allocate the default domain when | |
1802 | * creating the iommu group, so do it after the groups are | |
1803 | * created. | |
1804 | */ | |
1805 | ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group); | |
1806 | if (ret) | |
1807 | return ret; | |
deac0b3b | 1808 | |
3eeeb45c JR |
1809 | list_for_each_entry_safe(group, next, &group_list, entry) { |
1810 | /* Remove item from the list */ | |
1811 | list_del_init(&group->entry); | |
deac0b3b | 1812 | |
3eeeb45c | 1813 | mutex_lock(&group->mutex); |
deac0b3b | 1814 | |
3eeeb45c JR |
1815 | /* Try to allocate default domain */ |
1816 | probe_alloc_default_domain(bus, group); | |
deac0b3b | 1817 | |
3eeeb45c JR |
1818 | if (!group->default_domain) { |
1819 | mutex_unlock(&group->mutex); | |
1820 | continue; | |
1821 | } | |
deac0b3b | 1822 | |
3eeeb45c | 1823 | iommu_group_create_direct_mappings(group); |
ce574c27 | 1824 | |
3eeeb45c | 1825 | ret = __iommu_group_dma_attach(group); |
deac0b3b | 1826 | |
3eeeb45c | 1827 | mutex_unlock(&group->mutex); |
deac0b3b | 1828 | |
3eeeb45c JR |
1829 | if (ret) |
1830 | break; | |
70b8170e JR |
1831 | |
1832 | __iommu_group_dma_finalize(group); | |
deac0b3b JR |
1833 | } |
1834 | ||
1835 | return ret; | |
1836 | } | |
1837 | ||
fb3e3065 | 1838 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
ff21776d | 1839 | { |
fb3e3065 | 1840 | struct notifier_block *nb; |
deac0b3b | 1841 | int err; |
b22f6434 | 1842 | |
fb3e3065 MS |
1843 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
1844 | if (!nb) | |
1845 | return -ENOMEM; | |
1846 | ||
1847 | nb->notifier_call = iommu_bus_notifier; | |
1848 | ||
1849 | err = bus_register_notifier(bus, nb); | |
8da30142 JR |
1850 | if (err) |
1851 | goto out_free; | |
d7da6bdc | 1852 | |
deac0b3b | 1853 | err = bus_iommu_probe(bus); |
8da30142 JR |
1854 | if (err) |
1855 | goto out_err; | |
1856 | ||
d7da6bdc HS |
1857 | |
1858 | return 0; | |
8da30142 JR |
1859 | |
1860 | out_err: | |
1861 | /* Clean up */ | |
8cec63e5 | 1862 | bus_for_each_dev(bus, NULL, NULL, remove_iommu_group); |
8da30142 JR |
1863 | bus_unregister_notifier(bus, nb); |
1864 | ||
1865 | out_free: | |
1866 | kfree(nb); | |
1867 | ||
1868 | return err; | |
ff21776d | 1869 | } |
fc2100eb | 1870 | |
ff21776d JR |
1871 | /** |
1872 | * bus_set_iommu - set iommu-callbacks for the bus | |
1873 | * @bus: bus. | |
1874 | * @ops: the callbacks provided by the iommu-driver | |
1875 | * | |
1876 | * This function is called by an iommu driver to set the iommu methods | |
1877 | * used for a particular bus. Drivers for devices on that bus can use | |
1878 | * the iommu-api after these ops are registered. | |
1879 | * This special function is needed because IOMMUs are usually devices on | |
1880 | * the bus itself, so the iommu drivers are not initialized when the bus | |
1881 | * is set up. With this function the iommu-driver can set the iommu-ops | |
1882 | * afterwards. | |
1883 | */ | |
b22f6434 | 1884 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
fc2100eb | 1885 | { |
d7da6bdc HS |
1886 | int err; |
1887 | ||
4312cf7f WD |
1888 | if (ops == NULL) { |
1889 | bus->iommu_ops = NULL; | |
1890 | return 0; | |
1891 | } | |
1892 | ||
ff21776d JR |
1893 | if (bus->iommu_ops != NULL) |
1894 | return -EBUSY; | |
fc2100eb | 1895 | |
ff21776d JR |
1896 | bus->iommu_ops = ops; |
1897 | ||
1898 | /* Do IOMMU specific setup for this bus-type */ | |
d7da6bdc HS |
1899 | err = iommu_bus_init(bus, ops); |
1900 | if (err) | |
1901 | bus->iommu_ops = NULL; | |
1902 | ||
1903 | return err; | |
fc2100eb | 1904 | } |
ff21776d | 1905 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
fc2100eb | 1906 | |
a1b60c1c | 1907 | bool iommu_present(struct bus_type *bus) |
fc2100eb | 1908 | { |
94441c3b | 1909 | return bus->iommu_ops != NULL; |
fc2100eb | 1910 | } |
a1b60c1c | 1911 | EXPORT_SYMBOL_GPL(iommu_present); |
fc2100eb | 1912 | |
3c0e0ca0 JR |
1913 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
1914 | { | |
1915 | if (!bus->iommu_ops || !bus->iommu_ops->capable) | |
1916 | return false; | |
1917 | ||
1918 | return bus->iommu_ops->capable(cap); | |
1919 | } | |
1920 | EXPORT_SYMBOL_GPL(iommu_capable); | |
1921 | ||
4f3f8d9d OBC |
1922 | /** |
1923 | * iommu_set_fault_handler() - set a fault handler for an iommu domain | |
1924 | * @domain: iommu domain | |
1925 | * @handler: fault handler | |
77ca2332 | 1926 | * @token: user data, will be passed back to the fault handler |
0ed6d2d2 OBC |
1927 | * |
1928 | * This function should be used by IOMMU users which want to be notified | |
1929 | * whenever an IOMMU fault happens. | |
1930 | * | |
1931 | * The fault handler itself should return 0 on success, and an appropriate | |
1932 | * error code otherwise. | |
4f3f8d9d OBC |
1933 | */ |
1934 | void iommu_set_fault_handler(struct iommu_domain *domain, | |
77ca2332 OBC |
1935 | iommu_fault_handler_t handler, |
1936 | void *token) | |
4f3f8d9d OBC |
1937 | { |
1938 | BUG_ON(!domain); | |
1939 | ||
1940 | domain->handler = handler; | |
77ca2332 | 1941 | domain->handler_token = token; |
4f3f8d9d | 1942 | } |
30bd918c | 1943 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
4f3f8d9d | 1944 | |
53723dc5 JR |
1945 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
1946 | unsigned type) | |
fc2100eb JR |
1947 | { |
1948 | struct iommu_domain *domain; | |
fc2100eb | 1949 | |
94441c3b | 1950 | if (bus == NULL || bus->iommu_ops == NULL) |
905d66c1 JR |
1951 | return NULL; |
1952 | ||
53723dc5 | 1953 | domain = bus->iommu_ops->domain_alloc(type); |
fc2100eb JR |
1954 | if (!domain) |
1955 | return NULL; | |
1956 | ||
8539c7c1 | 1957 | domain->ops = bus->iommu_ops; |
53723dc5 | 1958 | domain->type = type; |
d16e0faa RM |
1959 | /* Assume all sizes by default; the driver may override this later */ |
1960 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; | |
905d66c1 | 1961 | |
46983fcd | 1962 | /* Temporarily avoid -EEXIST while drivers still get their own cookies */ |
bf3aed46 | 1963 | if (iommu_is_dma_domain(domain) && !domain->iova_cookie && iommu_get_dma_cookie(domain)) { |
46983fcd RM |
1964 | iommu_domain_free(domain); |
1965 | domain = NULL; | |
1966 | } | |
fc2100eb | 1967 | return domain; |
fc2100eb | 1968 | } |
fc2100eb | 1969 | |
53723dc5 JR |
1970 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
1971 | { | |
1972 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); | |
fc2100eb JR |
1973 | } |
1974 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); | |
1975 | ||
1976 | void iommu_domain_free(struct iommu_domain *domain) | |
1977 | { | |
46983fcd | 1978 | iommu_put_dma_cookie(domain); |
89be34a1 | 1979 | domain->ops->domain_free(domain); |
fc2100eb JR |
1980 | } |
1981 | EXPORT_SYMBOL_GPL(iommu_domain_free); | |
1982 | ||
426a2738 JR |
1983 | static int __iommu_attach_device(struct iommu_domain *domain, |
1984 | struct device *dev) | |
fc2100eb | 1985 | { |
b54db778 | 1986 | int ret; |
e01d1913 | 1987 | |
e5aa7f00 JR |
1988 | if (unlikely(domain->ops->attach_dev == NULL)) |
1989 | return -ENODEV; | |
1990 | ||
b54db778 SK |
1991 | ret = domain->ops->attach_dev(domain, dev); |
1992 | if (!ret) | |
1993 | trace_attach_device_to_domain(dev); | |
1994 | return ret; | |
fc2100eb | 1995 | } |
426a2738 JR |
1996 | |
1997 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) | |
1998 | { | |
1999 | struct iommu_group *group; | |
2000 | int ret; | |
2001 | ||
2002 | group = iommu_group_get(dev); | |
9ae9df03 JC |
2003 | if (!group) |
2004 | return -ENODEV; | |
2005 | ||
426a2738 | 2006 | /* |
05f80300 | 2007 | * Lock the group to make sure the device-count doesn't |
426a2738 JR |
2008 | * change while we are attaching |
2009 | */ | |
2010 | mutex_lock(&group->mutex); | |
2011 | ret = -EINVAL; | |
2012 | if (iommu_group_device_count(group) != 1) | |
2013 | goto out_unlock; | |
2014 | ||
e39cb8a3 | 2015 | ret = __iommu_attach_group(domain, group); |
426a2738 JR |
2016 | |
2017 | out_unlock: | |
2018 | mutex_unlock(&group->mutex); | |
2019 | iommu_group_put(group); | |
2020 | ||
2021 | return ret; | |
2022 | } | |
fc2100eb JR |
2023 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
2024 | ||
3ab65729 LJ |
2025 | int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain) |
2026 | { | |
2027 | const struct iommu_ops *ops = domain->ops; | |
2028 | ||
2029 | if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev)) | |
2030 | return __iommu_attach_device(domain, dev); | |
2031 | ||
2032 | return 0; | |
2033 | } | |
2034 | ||
d9057381 JP |
2035 | /* |
2036 | * Check flags and other user provided data for valid combinations. We also | |
2037 | * make sure no reserved fields or unused flags are set. This is to ensure | |
2038 | * not breaking userspace in the future when these fields or flags are used. | |
2039 | */ | |
2040 | static int iommu_check_cache_invl_data(struct iommu_cache_invalidate_info *info) | |
2041 | { | |
2042 | u32 mask; | |
2043 | int i; | |
2044 | ||
2045 | if (info->version != IOMMU_CACHE_INVALIDATE_INFO_VERSION_1) | |
2046 | return -EINVAL; | |
2047 | ||
2048 | mask = (1 << IOMMU_CACHE_INV_TYPE_NR) - 1; | |
2049 | if (info->cache & ~mask) | |
2050 | return -EINVAL; | |
2051 | ||
2052 | if (info->granularity >= IOMMU_INV_GRANU_NR) | |
2053 | return -EINVAL; | |
2054 | ||
2055 | switch (info->granularity) { | |
2056 | case IOMMU_INV_GRANU_ADDR: | |
2057 | if (info->cache & IOMMU_CACHE_INV_TYPE_PASID) | |
2058 | return -EINVAL; | |
2059 | ||
2060 | mask = IOMMU_INV_ADDR_FLAGS_PASID | | |
2061 | IOMMU_INV_ADDR_FLAGS_ARCHID | | |
2062 | IOMMU_INV_ADDR_FLAGS_LEAF; | |
2063 | ||
2064 | if (info->granu.addr_info.flags & ~mask) | |
2065 | return -EINVAL; | |
2066 | break; | |
2067 | case IOMMU_INV_GRANU_PASID: | |
2068 | mask = IOMMU_INV_PASID_FLAGS_PASID | | |
2069 | IOMMU_INV_PASID_FLAGS_ARCHID; | |
2070 | if (info->granu.pasid_info.flags & ~mask) | |
2071 | return -EINVAL; | |
2072 | ||
2073 | break; | |
2074 | case IOMMU_INV_GRANU_DOMAIN: | |
2075 | if (info->cache & IOMMU_CACHE_INV_TYPE_DEV_IOTLB) | |
2076 | return -EINVAL; | |
2077 | break; | |
2078 | default: | |
2079 | return -EINVAL; | |
2080 | } | |
2081 | ||
2082 | /* Check reserved padding fields */ | |
2083 | for (i = 0; i < sizeof(info->padding); i++) { | |
2084 | if (info->padding[i]) | |
2085 | return -EINVAL; | |
2086 | } | |
2087 | ||
2088 | return 0; | |
2089 | } | |
2090 | ||
23cc3493 | 2091 | int iommu_uapi_cache_invalidate(struct iommu_domain *domain, struct device *dev, |
d9057381 | 2092 | void __user *uinfo) |
4c7c171f | 2093 | { |
d9057381 JP |
2094 | struct iommu_cache_invalidate_info inv_info = { 0 }; |
2095 | u32 minsz; | |
2096 | int ret; | |
2097 | ||
4c7c171f YL |
2098 | if (unlikely(!domain->ops->cache_invalidate)) |
2099 | return -ENODEV; | |
2100 | ||
d9057381 JP |
2101 | /* |
2102 | * No new spaces can be added before the variable sized union, the | |
2103 | * minimum size is the offset to the union. | |
2104 | */ | |
2105 | minsz = offsetof(struct iommu_cache_invalidate_info, granu); | |
2106 | ||
2107 | /* Copy minsz from user to get flags and argsz */ | |
2108 | if (copy_from_user(&inv_info, uinfo, minsz)) | |
2109 | return -EFAULT; | |
2110 | ||
2111 | /* Fields before the variable size union are mandatory */ | |
2112 | if (inv_info.argsz < minsz) | |
2113 | return -EINVAL; | |
2114 | ||
2115 | /* PASID and address granu require additional info beyond minsz */ | |
2116 | if (inv_info.granularity == IOMMU_INV_GRANU_PASID && | |
2117 | inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.pasid_info)) | |
2118 | return -EINVAL; | |
2119 | ||
2120 | if (inv_info.granularity == IOMMU_INV_GRANU_ADDR && | |
2121 | inv_info.argsz < offsetofend(struct iommu_cache_invalidate_info, granu.addr_info)) | |
2122 | return -EINVAL; | |
2123 | ||
2124 | /* | |
2125 | * User might be using a newer UAPI header which has a larger data | |
2126 | * size, we shall support the existing flags within the current | |
2127 | * size. Copy the remaining user data _after_ minsz but not more | |
2128 | * than the current kernel supported size. | |
2129 | */ | |
2130 | if (copy_from_user((void *)&inv_info + minsz, uinfo + minsz, | |
2131 | min_t(u32, inv_info.argsz, sizeof(inv_info)) - minsz)) | |
2132 | return -EFAULT; | |
2133 | ||
2134 | /* Now the argsz is validated, check the content */ | |
2135 | ret = iommu_check_cache_invl_data(&inv_info); | |
2136 | if (ret) | |
2137 | return ret; | |
2138 | ||
2139 | return domain->ops->cache_invalidate(domain, dev, &inv_info); | |
4c7c171f | 2140 | } |
23cc3493 | 2141 | EXPORT_SYMBOL_GPL(iommu_uapi_cache_invalidate); |
4c7c171f | 2142 | |
d9057381 | 2143 | static int iommu_check_bind_data(struct iommu_gpasid_bind_data *data) |
808be0aa | 2144 | { |
4dd6ce47 | 2145 | u64 mask; |
d9057381 JP |
2146 | int i; |
2147 | ||
2148 | if (data->version != IOMMU_GPASID_BIND_VERSION_1) | |
2149 | return -EINVAL; | |
2150 | ||
2151 | /* Check the range of supported formats */ | |
2152 | if (data->format >= IOMMU_PASID_FORMAT_LAST) | |
2153 | return -EINVAL; | |
2154 | ||
2155 | /* Check all flags */ | |
2156 | mask = IOMMU_SVA_GPASID_VAL; | |
2157 | if (data->flags & ~mask) | |
2158 | return -EINVAL; | |
2159 | ||
2160 | /* Check reserved padding fields */ | |
2161 | for (i = 0; i < sizeof(data->padding); i++) { | |
2162 | if (data->padding[i]) | |
2163 | return -EINVAL; | |
2164 | } | |
2165 | ||
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | static int iommu_sva_prepare_bind_data(void __user *udata, | |
2170 | struct iommu_gpasid_bind_data *data) | |
2171 | { | |
2172 | u32 minsz; | |
2173 | ||
2174 | /* | |
2175 | * No new spaces can be added before the variable sized union, the | |
2176 | * minimum size is the offset to the union. | |
2177 | */ | |
2178 | minsz = offsetof(struct iommu_gpasid_bind_data, vendor); | |
2179 | ||
2180 | /* Copy minsz from user to get flags and argsz */ | |
2181 | if (copy_from_user(data, udata, minsz)) | |
2182 | return -EFAULT; | |
2183 | ||
2184 | /* Fields before the variable size union are mandatory */ | |
2185 | if (data->argsz < minsz) | |
2186 | return -EINVAL; | |
2187 | /* | |
2188 | * User might be using a newer UAPI header, we shall let IOMMU vendor | |
2189 | * driver decide on what size it needs. Since the guest PASID bind data | |
2190 | * can be vendor specific, larger argsz could be the result of extension | |
2191 | * for one vendor but it should not affect another vendor. | |
2192 | * Copy the remaining user data _after_ minsz | |
2193 | */ | |
2194 | if (copy_from_user((void *)data + minsz, udata + minsz, | |
2195 | min_t(u32, data->argsz, sizeof(*data)) - minsz)) | |
2196 | return -EFAULT; | |
2197 | ||
2198 | return iommu_check_bind_data(data); | |
4c7c171f | 2199 | } |
4c7c171f | 2200 | |
d9057381 JP |
2201 | int iommu_uapi_sva_bind_gpasid(struct iommu_domain *domain, struct device *dev, |
2202 | void __user *udata) | |
808be0aa | 2203 | { |
d9057381 JP |
2204 | struct iommu_gpasid_bind_data data = { 0 }; |
2205 | int ret; | |
2206 | ||
808be0aa JP |
2207 | if (unlikely(!domain->ops->sva_bind_gpasid)) |
2208 | return -ENODEV; | |
2209 | ||
d9057381 JP |
2210 | ret = iommu_sva_prepare_bind_data(udata, &data); |
2211 | if (ret) | |
2212 | return ret; | |
2213 | ||
2214 | return domain->ops->sva_bind_gpasid(domain, dev, &data); | |
808be0aa | 2215 | } |
23cc3493 | 2216 | EXPORT_SYMBOL_GPL(iommu_uapi_sva_bind_gpasid); |
808be0aa JP |
2217 | |
2218 | int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev, | |
2219 | ioasid_t pasid) | |
2220 | { | |
2221 | if (unlikely(!domain->ops->sva_unbind_gpasid)) | |
2222 | return -ENODEV; | |
2223 | ||
2224 | return domain->ops->sva_unbind_gpasid(dev, pasid); | |
2225 | } | |
2226 | EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid); | |
2227 | ||
d9057381 JP |
2228 | int iommu_uapi_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev, |
2229 | void __user *udata) | |
2230 | { | |
2231 | struct iommu_gpasid_bind_data data = { 0 }; | |
2232 | int ret; | |
2233 | ||
2234 | if (unlikely(!domain->ops->sva_bind_gpasid)) | |
2235 | return -ENODEV; | |
2236 | ||
2237 | ret = iommu_sva_prepare_bind_data(udata, &data); | |
2238 | if (ret) | |
2239 | return ret; | |
2240 | ||
2241 | return iommu_sva_unbind_gpasid(domain, dev, data.hpasid); | |
2242 | } | |
23cc3493 | 2243 | EXPORT_SYMBOL_GPL(iommu_uapi_sva_unbind_gpasid); |
808be0aa | 2244 | |
426a2738 JR |
2245 | static void __iommu_detach_device(struct iommu_domain *domain, |
2246 | struct device *dev) | |
fc2100eb | 2247 | { |
bd421264 | 2248 | if (iommu_is_attach_deferred(domain, dev)) |
e01d1913 BH |
2249 | return; |
2250 | ||
e5aa7f00 JR |
2251 | if (unlikely(domain->ops->detach_dev == NULL)) |
2252 | return; | |
2253 | ||
2254 | domain->ops->detach_dev(domain, dev); | |
69980630 | 2255 | trace_detach_device_from_domain(dev); |
fc2100eb | 2256 | } |
426a2738 JR |
2257 | |
2258 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) | |
2259 | { | |
2260 | struct iommu_group *group; | |
2261 | ||
2262 | group = iommu_group_get(dev); | |
9ae9df03 JC |
2263 | if (!group) |
2264 | return; | |
426a2738 JR |
2265 | |
2266 | mutex_lock(&group->mutex); | |
2267 | if (iommu_group_device_count(group) != 1) { | |
2268 | WARN_ON(1); | |
2269 | goto out_unlock; | |
2270 | } | |
2271 | ||
e39cb8a3 | 2272 | __iommu_detach_group(domain, group); |
426a2738 JR |
2273 | |
2274 | out_unlock: | |
2275 | mutex_unlock(&group->mutex); | |
2276 | iommu_group_put(group); | |
2277 | } | |
fc2100eb JR |
2278 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
2279 | ||
2c1296d9 JR |
2280 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
2281 | { | |
2282 | struct iommu_domain *domain; | |
2283 | struct iommu_group *group; | |
2284 | ||
2285 | group = iommu_group_get(dev); | |
1464d0b1 | 2286 | if (!group) |
2c1296d9 JR |
2287 | return NULL; |
2288 | ||
2289 | domain = group->domain; | |
2290 | ||
2291 | iommu_group_put(group); | |
2292 | ||
2293 | return domain; | |
2294 | } | |
2295 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); | |
fc2100eb | 2296 | |
d72e31c9 | 2297 | /* |
6af588fe RM |
2298 | * For IOMMU_DOMAIN_DMA implementations which already provide their own |
2299 | * guarantees that the group and its default domain are valid and correct. | |
2300 | */ | |
2301 | struct iommu_domain *iommu_get_dma_domain(struct device *dev) | |
2302 | { | |
2303 | return dev->iommu_group->default_domain; | |
2304 | } | |
2305 | ||
d72e31c9 | 2306 | /* |
35449adc | 2307 | * IOMMU groups are really the natural working unit of the IOMMU, but |
d72e31c9 AW |
2308 | * the IOMMU API works on domains and devices. Bridge that gap by |
2309 | * iterating over the devices in a group. Ideally we'd have a single | |
2310 | * device which represents the requestor ID of the group, but we also | |
2311 | * allow IOMMU drivers to create policy defined minimum sets, where | |
2312 | * the physical hardware may be able to distiguish members, but we | |
2313 | * wish to group them at a higher level (ex. untrusted multi-function | |
2314 | * PCI devices). Thus we attach each device. | |
2315 | */ | |
2316 | static int iommu_group_do_attach_device(struct device *dev, void *data) | |
2317 | { | |
2318 | struct iommu_domain *domain = data; | |
2319 | ||
426a2738 | 2320 | return __iommu_attach_device(domain, dev); |
d72e31c9 AW |
2321 | } |
2322 | ||
e39cb8a3 JR |
2323 | static int __iommu_attach_group(struct iommu_domain *domain, |
2324 | struct iommu_group *group) | |
2325 | { | |
2326 | int ret; | |
2327 | ||
2328 | if (group->default_domain && group->domain != group->default_domain) | |
2329 | return -EBUSY; | |
2330 | ||
2331 | ret = __iommu_group_for_each_dev(group, domain, | |
2332 | iommu_group_do_attach_device); | |
2333 | if (ret == 0) | |
2334 | group->domain = domain; | |
2335 | ||
2336 | return ret; | |
d72e31c9 AW |
2337 | } |
2338 | ||
2339 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) | |
2340 | { | |
e39cb8a3 JR |
2341 | int ret; |
2342 | ||
2343 | mutex_lock(&group->mutex); | |
2344 | ret = __iommu_attach_group(domain, group); | |
2345 | mutex_unlock(&group->mutex); | |
2346 | ||
2347 | return ret; | |
d72e31c9 AW |
2348 | } |
2349 | EXPORT_SYMBOL_GPL(iommu_attach_group); | |
2350 | ||
2351 | static int iommu_group_do_detach_device(struct device *dev, void *data) | |
2352 | { | |
2353 | struct iommu_domain *domain = data; | |
2354 | ||
426a2738 | 2355 | __iommu_detach_device(domain, dev); |
d72e31c9 AW |
2356 | |
2357 | return 0; | |
2358 | } | |
2359 | ||
e39cb8a3 JR |
2360 | static void __iommu_detach_group(struct iommu_domain *domain, |
2361 | struct iommu_group *group) | |
2362 | { | |
2363 | int ret; | |
2364 | ||
2365 | if (!group->default_domain) { | |
2366 | __iommu_group_for_each_dev(group, domain, | |
2367 | iommu_group_do_detach_device); | |
2368 | group->domain = NULL; | |
2369 | return; | |
2370 | } | |
2371 | ||
2372 | if (group->domain == group->default_domain) | |
2373 | return; | |
2374 | ||
2375 | /* Detach by re-attaching to the default domain */ | |
2376 | ret = __iommu_group_for_each_dev(group, group->default_domain, | |
2377 | iommu_group_do_attach_device); | |
2378 | if (ret != 0) | |
2379 | WARN_ON(1); | |
2380 | else | |
2381 | group->domain = group->default_domain; | |
2382 | } | |
2383 | ||
d72e31c9 AW |
2384 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
2385 | { | |
e39cb8a3 JR |
2386 | mutex_lock(&group->mutex); |
2387 | __iommu_detach_group(domain, group); | |
2388 | mutex_unlock(&group->mutex); | |
d72e31c9 AW |
2389 | } |
2390 | EXPORT_SYMBOL_GPL(iommu_detach_group); | |
2391 | ||
bb5547ac | 2392 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
fc2100eb | 2393 | { |
13b6eb6e RM |
2394 | if (domain->type == IOMMU_DOMAIN_IDENTITY) |
2395 | return iova; | |
2396 | ||
2397 | if (domain->type == IOMMU_DOMAIN_BLOCKED) | |
e5aa7f00 JR |
2398 | return 0; |
2399 | ||
2400 | return domain->ops->iova_to_phys(domain, iova); | |
fc2100eb JR |
2401 | } |
2402 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); | |
dbb9fd86 | 2403 | |
89d5b960 | 2404 | static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova, |
b1d99dc5 | 2405 | phys_addr_t paddr, size_t size, size_t *count) |
bd13969b | 2406 | { |
b1d99dc5 | 2407 | unsigned int pgsize_idx, pgsize_idx_next; |
e7d6fff6 | 2408 | unsigned long pgsizes; |
b1d99dc5 | 2409 | size_t offset, pgsize, pgsize_next; |
89d5b960 | 2410 | unsigned long addr_merge = paddr | iova; |
bd13969b | 2411 | |
e7d6fff6 WD |
2412 | /* Page sizes supported by the hardware and small enough for @size */ |
2413 | pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0); | |
bd13969b | 2414 | |
e7d6fff6 WD |
2415 | /* Constrain the page sizes further based on the maximum alignment */ |
2416 | if (likely(addr_merge)) | |
2417 | pgsizes &= GENMASK(__ffs(addr_merge), 0); | |
bd13969b | 2418 | |
e7d6fff6 WD |
2419 | /* Make sure we have at least one suitable page size */ |
2420 | BUG_ON(!pgsizes); | |
bd13969b | 2421 | |
e7d6fff6 WD |
2422 | /* Pick the biggest page size remaining */ |
2423 | pgsize_idx = __fls(pgsizes); | |
2424 | pgsize = BIT(pgsize_idx); | |
b1d99dc5 WD |
2425 | if (!count) |
2426 | return pgsize; | |
bd13969b | 2427 | |
b1d99dc5 WD |
2428 | /* Find the next biggest support page size, if it exists */ |
2429 | pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0); | |
2430 | if (!pgsizes) | |
2431 | goto out_set_count; | |
bd13969b | 2432 | |
b1d99dc5 WD |
2433 | pgsize_idx_next = __ffs(pgsizes); |
2434 | pgsize_next = BIT(pgsize_idx_next); | |
bd13969b | 2435 | |
b1d99dc5 WD |
2436 | /* |
2437 | * There's no point trying a bigger page size unless the virtual | |
2438 | * and physical addresses are similarly offset within the larger page. | |
2439 | */ | |
2440 | if ((iova ^ paddr) & (pgsize_next - 1)) | |
2441 | goto out_set_count; | |
bd13969b | 2442 | |
b1d99dc5 WD |
2443 | /* Calculate the offset to the next page size alignment boundary */ |
2444 | offset = pgsize_next - (addr_merge & (pgsize_next - 1)); | |
bd13969b | 2445 | |
b1d99dc5 WD |
2446 | /* |
2447 | * If size is big enough to accommodate the larger page, reduce | |
2448 | * the number of smaller pages. | |
2449 | */ | |
2450 | if (offset + pgsize_next <= size) | |
2451 | size = offset; | |
2452 | ||
2453 | out_set_count: | |
2454 | *count = size >> pgsize_idx; | |
bd13969b AW |
2455 | return pgsize; |
2456 | } | |
2457 | ||
647c5776 IM |
2458 | static int __iommu_map_pages(struct iommu_domain *domain, unsigned long iova, |
2459 | phys_addr_t paddr, size_t size, int prot, | |
2460 | gfp_t gfp, size_t *mapped) | |
2461 | { | |
2462 | const struct iommu_ops *ops = domain->ops; | |
2463 | size_t pgsize, count; | |
2464 | int ret; | |
2465 | ||
2466 | pgsize = iommu_pgsize(domain, iova, paddr, size, &count); | |
2467 | ||
2468 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n", | |
2469 | iova, &paddr, pgsize, count); | |
2470 | ||
2471 | if (ops->map_pages) { | |
2472 | ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot, | |
2473 | gfp, mapped); | |
2474 | } else { | |
2475 | ret = ops->map(domain, iova, paddr, pgsize, prot, gfp); | |
2476 | *mapped = ret ? 0 : pgsize; | |
2477 | } | |
2478 | ||
2479 | return ret; | |
2480 | } | |
2481 | ||
1b0b2a84 WY |
2482 | static int __iommu_map(struct iommu_domain *domain, unsigned long iova, |
2483 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) | |
cefc53c7 | 2484 | { |
1d7ae53b | 2485 | const struct iommu_ops *ops = domain->ops; |
7d3002cc OBC |
2486 | unsigned long orig_iova = iova; |
2487 | unsigned int min_pagesz; | |
2488 | size_t orig_size = size; | |
06bfcaa9 | 2489 | phys_addr_t orig_paddr = paddr; |
7d3002cc | 2490 | int ret = 0; |
cefc53c7 | 2491 | |
647c5776 | 2492 | if (unlikely(!(ops->map || ops->map_pages) || |
d16e0faa | 2493 | domain->pgsize_bitmap == 0UL)) |
e5aa7f00 | 2494 | return -ENODEV; |
cefc53c7 | 2495 | |
a10315e5 JR |
2496 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
2497 | return -EINVAL; | |
2498 | ||
7d3002cc | 2499 | /* find out the minimum page size supported */ |
d16e0faa | 2500 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
2501 | |
2502 | /* | |
2503 | * both the virtual address and the physical one, as well as | |
2504 | * the size of the mapping, must be aligned (at least) to the | |
2505 | * size of the smallest page supported by the hardware | |
2506 | */ | |
2507 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { | |
abedb049 | 2508 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
6197ca82 | 2509 | iova, &paddr, size, min_pagesz); |
7d3002cc OBC |
2510 | return -EINVAL; |
2511 | } | |
2512 | ||
abedb049 | 2513 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
7d3002cc OBC |
2514 | |
2515 | while (size) { | |
647c5776 | 2516 | size_t mapped = 0; |
7d3002cc | 2517 | |
647c5776 IM |
2518 | ret = __iommu_map_pages(domain, iova, paddr, size, prot, gfp, |
2519 | &mapped); | |
2520 | /* | |
2521 | * Some pages may have been mapped, even if an error occurred, | |
2522 | * so we should account for those so they can be unmapped. | |
2523 | */ | |
2524 | size -= mapped; | |
7d3002cc | 2525 | |
7d3002cc OBC |
2526 | if (ret) |
2527 | break; | |
2528 | ||
647c5776 IM |
2529 | iova += mapped; |
2530 | paddr += mapped; | |
7d3002cc OBC |
2531 | } |
2532 | ||
2533 | /* unroll mapping in case something went wrong */ | |
2534 | if (ret) | |
2535 | iommu_unmap(domain, orig_iova, orig_size - size); | |
e0be7c86 | 2536 | else |
06bfcaa9 | 2537 | trace_map(orig_iova, orig_paddr, orig_size); |
7d3002cc OBC |
2538 | |
2539 | return ret; | |
cefc53c7 | 2540 | } |
781ca2de | 2541 | |
d8c1df02 YW |
2542 | static int _iommu_map(struct iommu_domain *domain, unsigned long iova, |
2543 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) | |
2544 | { | |
2545 | const struct iommu_ops *ops = domain->ops; | |
2546 | int ret; | |
2547 | ||
b8437a3e | 2548 | ret = __iommu_map(domain, iova, paddr, size, prot, gfp); |
d8c1df02 | 2549 | if (ret == 0 && ops->iotlb_sync_map) |
2ebbd258 | 2550 | ops->iotlb_sync_map(domain, iova, size); |
d8c1df02 YW |
2551 | |
2552 | return ret; | |
2553 | } | |
2554 | ||
781ca2de TM |
2555 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
2556 | phys_addr_t paddr, size_t size, int prot) | |
2557 | { | |
2558 | might_sleep(); | |
d8c1df02 | 2559 | return _iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL); |
781ca2de | 2560 | } |
cefc53c7 JR |
2561 | EXPORT_SYMBOL_GPL(iommu_map); |
2562 | ||
781ca2de TM |
2563 | int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova, |
2564 | phys_addr_t paddr, size_t size, int prot) | |
2565 | { | |
d8c1df02 | 2566 | return _iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC); |
781ca2de TM |
2567 | } |
2568 | EXPORT_SYMBOL_GPL(iommu_map_atomic); | |
2569 | ||
b1d99dc5 WD |
2570 | static size_t __iommu_unmap_pages(struct iommu_domain *domain, |
2571 | unsigned long iova, size_t size, | |
2572 | struct iommu_iotlb_gather *iotlb_gather) | |
2573 | { | |
2574 | const struct iommu_ops *ops = domain->ops; | |
2575 | size_t pgsize, count; | |
2576 | ||
2577 | pgsize = iommu_pgsize(domain, iova, iova, size, &count); | |
2578 | return ops->unmap_pages ? | |
2579 | ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather) : | |
2580 | ops->unmap(domain, iova, pgsize, iotlb_gather); | |
2581 | } | |
2582 | ||
add02cfd JR |
2583 | static size_t __iommu_unmap(struct iommu_domain *domain, |
2584 | unsigned long iova, size_t size, | |
a7d20dc1 | 2585 | struct iommu_iotlb_gather *iotlb_gather) |
cefc53c7 | 2586 | { |
add02cfd | 2587 | const struct iommu_ops *ops = domain->ops; |
7d3002cc | 2588 | size_t unmapped_page, unmapped = 0; |
6fd492fd | 2589 | unsigned long orig_iova = iova; |
add02cfd | 2590 | unsigned int min_pagesz; |
cefc53c7 | 2591 | |
b1d99dc5 | 2592 | if (unlikely(!(ops->unmap || ops->unmap_pages) || |
d16e0faa | 2593 | domain->pgsize_bitmap == 0UL)) |
c5611a87 | 2594 | return 0; |
e5aa7f00 | 2595 | |
a10315e5 | 2596 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
c5611a87 | 2597 | return 0; |
a10315e5 | 2598 | |
7d3002cc | 2599 | /* find out the minimum page size supported */ |
d16e0faa | 2600 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
7d3002cc OBC |
2601 | |
2602 | /* | |
2603 | * The virtual address, as well as the size of the mapping, must be | |
2604 | * aligned (at least) to the size of the smallest page supported | |
2605 | * by the hardware | |
2606 | */ | |
2607 | if (!IS_ALIGNED(iova | size, min_pagesz)) { | |
6197ca82 JP |
2608 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
2609 | iova, size, min_pagesz); | |
c5611a87 | 2610 | return 0; |
7d3002cc OBC |
2611 | } |
2612 | ||
6197ca82 | 2613 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
7d3002cc OBC |
2614 | |
2615 | /* | |
2616 | * Keep iterating until we either unmap 'size' bytes (or more) | |
2617 | * or we hit an area that isn't mapped. | |
2618 | */ | |
2619 | while (unmapped < size) { | |
b1d99dc5 WD |
2620 | unmapped_page = __iommu_unmap_pages(domain, iova, |
2621 | size - unmapped, | |
2622 | iotlb_gather); | |
7d3002cc OBC |
2623 | if (!unmapped_page) |
2624 | break; | |
2625 | ||
6197ca82 JP |
2626 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
2627 | iova, unmapped_page); | |
7d3002cc OBC |
2628 | |
2629 | iova += unmapped_page; | |
2630 | unmapped += unmapped_page; | |
2631 | } | |
2632 | ||
db8614d3 | 2633 | trace_unmap(orig_iova, size, unmapped); |
7d3002cc | 2634 | return unmapped; |
cefc53c7 | 2635 | } |
add02cfd JR |
2636 | |
2637 | size_t iommu_unmap(struct iommu_domain *domain, | |
2638 | unsigned long iova, size_t size) | |
2639 | { | |
a7d20dc1 WD |
2640 | struct iommu_iotlb_gather iotlb_gather; |
2641 | size_t ret; | |
2642 | ||
2643 | iommu_iotlb_gather_init(&iotlb_gather); | |
2644 | ret = __iommu_unmap(domain, iova, size, &iotlb_gather); | |
aae4c8e2 | 2645 | iommu_iotlb_sync(domain, &iotlb_gather); |
a7d20dc1 WD |
2646 | |
2647 | return ret; | |
add02cfd | 2648 | } |
cefc53c7 | 2649 | EXPORT_SYMBOL_GPL(iommu_unmap); |
1460432c | 2650 | |
add02cfd | 2651 | size_t iommu_unmap_fast(struct iommu_domain *domain, |
a7d20dc1 WD |
2652 | unsigned long iova, size_t size, |
2653 | struct iommu_iotlb_gather *iotlb_gather) | |
add02cfd | 2654 | { |
a7d20dc1 | 2655 | return __iommu_unmap(domain, iova, size, iotlb_gather); |
add02cfd JR |
2656 | } |
2657 | EXPORT_SYMBOL_GPL(iommu_unmap_fast); | |
2658 | ||
ad8f36e4 LG |
2659 | static ssize_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
2660 | struct scatterlist *sg, unsigned int nents, int prot, | |
2661 | gfp_t gfp) | |
315786eb | 2662 | { |
d8c1df02 | 2663 | const struct iommu_ops *ops = domain->ops; |
5d95f40e RM |
2664 | size_t len = 0, mapped = 0; |
2665 | phys_addr_t start; | |
2666 | unsigned int i = 0; | |
38ec010d | 2667 | int ret; |
315786eb | 2668 | |
5d95f40e RM |
2669 | while (i <= nents) { |
2670 | phys_addr_t s_phys = sg_phys(sg); | |
18f23409 | 2671 | |
5d95f40e | 2672 | if (len && s_phys != start + len) { |
781ca2de TM |
2673 | ret = __iommu_map(domain, iova + mapped, start, |
2674 | len, prot, gfp); | |
2675 | ||
5d95f40e RM |
2676 | if (ret) |
2677 | goto out_err; | |
18f23409 | 2678 | |
5d95f40e RM |
2679 | mapped += len; |
2680 | len = 0; | |
2681 | } | |
38ec010d | 2682 | |
5d95f40e RM |
2683 | if (len) { |
2684 | len += sg->length; | |
2685 | } else { | |
2686 | len = sg->length; | |
2687 | start = s_phys; | |
2688 | } | |
38ec010d | 2689 | |
5d95f40e RM |
2690 | if (++i < nents) |
2691 | sg = sg_next(sg); | |
315786eb OH |
2692 | } |
2693 | ||
d8c1df02 | 2694 | if (ops->iotlb_sync_map) |
2ebbd258 | 2695 | ops->iotlb_sync_map(domain, iova, mapped); |
315786eb | 2696 | return mapped; |
38ec010d JR |
2697 | |
2698 | out_err: | |
2699 | /* undo mappings already done */ | |
2700 | iommu_unmap(domain, iova, mapped); | |
2701 | ||
ad8f36e4 | 2702 | return ret; |
315786eb | 2703 | } |
781ca2de | 2704 | |
ad8f36e4 LG |
2705 | ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
2706 | struct scatterlist *sg, unsigned int nents, int prot) | |
781ca2de TM |
2707 | { |
2708 | might_sleep(); | |
2709 | return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL); | |
2710 | } | |
d88e61fa | 2711 | EXPORT_SYMBOL_GPL(iommu_map_sg); |
d7787d57 | 2712 | |
ad8f36e4 | 2713 | ssize_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova, |
781ca2de TM |
2714 | struct scatterlist *sg, unsigned int nents, int prot) |
2715 | { | |
2716 | return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC); | |
2717 | } | |
781ca2de | 2718 | |
207c6e36 JR |
2719 | /** |
2720 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework | |
2721 | * @domain: the iommu domain where the fault has happened | |
2722 | * @dev: the device where the fault has happened | |
2723 | * @iova: the faulting address | |
2724 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) | |
2725 | * | |
2726 | * This function should be called by the low-level IOMMU implementations | |
2727 | * whenever IOMMU faults happen, to allow high-level users, that are | |
2728 | * interested in such events, to know about them. | |
2729 | * | |
2730 | * This event may be useful for several possible use cases: | |
2731 | * - mere logging of the event | |
2732 | * - dynamic TLB/PTE loading | |
2733 | * - if restarting of the faulting device is required | |
2734 | * | |
2735 | * Returns 0 on success and an appropriate error code otherwise (if dynamic | |
2736 | * PTE/TLB loading will one day be supported, implementations will be able | |
2737 | * to tell whether it succeeded or not according to this return value). | |
2738 | * | |
2739 | * Specifically, -ENOSYS is returned if a fault handler isn't installed | |
2740 | * (though fault handlers can also return -ENOSYS, in case they want to | |
2741 | * elicit the default behavior of the IOMMU drivers). | |
2742 | */ | |
2743 | int report_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
2744 | unsigned long iova, int flags) | |
2745 | { | |
2746 | int ret = -ENOSYS; | |
2747 | ||
2748 | /* | |
2749 | * if upper layers showed interest and installed a fault handler, | |
2750 | * invoke it. | |
2751 | */ | |
2752 | if (domain->handler) | |
2753 | ret = domain->handler(domain, dev, iova, flags, | |
2754 | domain->handler_token); | |
2755 | ||
2756 | trace_io_page_fault(dev, iova, flags); | |
2757 | return ret; | |
2758 | } | |
2759 | EXPORT_SYMBOL_GPL(report_iommu_fault); | |
2760 | ||
d72e31c9 | 2761 | static int __init iommu_init(void) |
1460432c | 2762 | { |
d72e31c9 AW |
2763 | iommu_group_kset = kset_create_and_add("iommu_groups", |
2764 | NULL, kernel_kobj); | |
d72e31c9 AW |
2765 | BUG_ON(!iommu_group_kset); |
2766 | ||
bad614b2 GH |
2767 | iommu_debugfs_setup(); |
2768 | ||
d72e31c9 | 2769 | return 0; |
1460432c | 2770 | } |
d7ef9995 | 2771 | core_initcall(iommu_init); |
0cd76dd1 | 2772 | |
7e147547 CH |
2773 | int iommu_enable_nesting(struct iommu_domain *domain) |
2774 | { | |
2775 | if (domain->type != IOMMU_DOMAIN_UNMANAGED) | |
2776 | return -EINVAL; | |
2777 | if (!domain->ops->enable_nesting) | |
2778 | return -EINVAL; | |
2779 | return domain->ops->enable_nesting(domain); | |
2780 | } | |
2781 | EXPORT_SYMBOL_GPL(iommu_enable_nesting); | |
2782 | ||
4fc52b81 CH |
2783 | int iommu_set_pgtable_quirks(struct iommu_domain *domain, |
2784 | unsigned long quirk) | |
2785 | { | |
2786 | if (domain->type != IOMMU_DOMAIN_UNMANAGED) | |
2787 | return -EINVAL; | |
2788 | if (!domain->ops->set_pgtable_quirks) | |
2789 | return -EINVAL; | |
2790 | return domain->ops->set_pgtable_quirks(domain, quirk); | |
2791 | } | |
2792 | EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); | |
2793 | ||
e5b5234a | 2794 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
2795 | { |
2796 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2797 | ||
e5b5234a EA |
2798 | if (ops && ops->get_resv_regions) |
2799 | ops->get_resv_regions(dev, list); | |
a1015c2b JR |
2800 | } |
2801 | ||
e5b5234a | 2802 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
a1015c2b JR |
2803 | { |
2804 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
2805 | ||
e5b5234a EA |
2806 | if (ops && ops->put_resv_regions) |
2807 | ops->put_resv_regions(dev, list); | |
a1015c2b | 2808 | } |
d290f1e7 | 2809 | |
f9f6971e TR |
2810 | /** |
2811 | * generic_iommu_put_resv_regions - Reserved region driver helper | |
2812 | * @dev: device for which to free reserved regions | |
2813 | * @list: reserved region list for device | |
2814 | * | |
2815 | * IOMMU drivers can use this to implement their .put_resv_regions() callback | |
2816 | * for simple reservations. Memory allocated for each reserved region will be | |
2817 | * freed. If an IOMMU driver allocates additional resources per region, it is | |
2818 | * going to have to implement a custom callback. | |
2819 | */ | |
2820 | void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list) | |
2821 | { | |
2822 | struct iommu_resv_region *entry, *next; | |
2823 | ||
2824 | list_for_each_entry_safe(entry, next, list, list) | |
2825 | kfree(entry); | |
2826 | } | |
2827 | EXPORT_SYMBOL(generic_iommu_put_resv_regions); | |
2828 | ||
2b20cbba | 2829 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
9d3a4de4 RM |
2830 | size_t length, int prot, |
2831 | enum iommu_resv_type type) | |
2b20cbba EA |
2832 | { |
2833 | struct iommu_resv_region *region; | |
2834 | ||
2835 | region = kzalloc(sizeof(*region), GFP_KERNEL); | |
2836 | if (!region) | |
2837 | return NULL; | |
2838 | ||
2839 | INIT_LIST_HEAD(®ion->list); | |
2840 | region->start = start; | |
2841 | region->length = length; | |
2842 | region->prot = prot; | |
2843 | region->type = type; | |
2844 | return region; | |
a1015c2b | 2845 | } |
a7ba5c3d | 2846 | EXPORT_SYMBOL_GPL(iommu_alloc_resv_region); |
d290f1e7 | 2847 | |
8a69961c JR |
2848 | void iommu_set_default_passthrough(bool cmd_line) |
2849 | { | |
2850 | if (cmd_line) | |
3189713a | 2851 | iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; |
8a69961c JR |
2852 | iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY; |
2853 | } | |
2854 | ||
2855 | void iommu_set_default_translated(bool cmd_line) | |
2856 | { | |
2857 | if (cmd_line) | |
3189713a | 2858 | iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; |
8a69961c JR |
2859 | iommu_def_domain_type = IOMMU_DOMAIN_DMA; |
2860 | } | |
2861 | ||
2862 | bool iommu_default_passthrough(void) | |
2863 | { | |
2864 | return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY; | |
2865 | } | |
2866 | EXPORT_SYMBOL_GPL(iommu_default_passthrough); | |
2867 | ||
534766df | 2868 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
e4f10ffe | 2869 | { |
e4f10ffe | 2870 | const struct iommu_ops *ops = NULL; |
d0f6f583 | 2871 | struct iommu_device *iommu; |
e4f10ffe | 2872 | |
d0f6f583 JR |
2873 | spin_lock(&iommu_device_lock); |
2874 | list_for_each_entry(iommu, &iommu_device_list, list) | |
2875 | if (iommu->fwnode == fwnode) { | |
2876 | ops = iommu->ops; | |
e4f10ffe LP |
2877 | break; |
2878 | } | |
d0f6f583 | 2879 | spin_unlock(&iommu_device_lock); |
e4f10ffe LP |
2880 | return ops; |
2881 | } | |
2882 | ||
57f98d2f RM |
2883 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
2884 | const struct iommu_ops *ops) | |
2885 | { | |
b4ef725e | 2886 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
57f98d2f RM |
2887 | |
2888 | if (fwspec) | |
2889 | return ops == fwspec->ops ? 0 : -EINVAL; | |
2890 | ||
72acd9df JR |
2891 | if (!dev_iommu_get(dev)) |
2892 | return -ENOMEM; | |
2893 | ||
098accf2 RM |
2894 | /* Preallocate for the overwhelmingly common case of 1 ID */ |
2895 | fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL); | |
57f98d2f RM |
2896 | if (!fwspec) |
2897 | return -ENOMEM; | |
2898 | ||
2899 | of_node_get(to_of_node(iommu_fwnode)); | |
2900 | fwspec->iommu_fwnode = iommu_fwnode; | |
2901 | fwspec->ops = ops; | |
b4ef725e | 2902 | dev_iommu_fwspec_set(dev, fwspec); |
57f98d2f RM |
2903 | return 0; |
2904 | } | |
2905 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); | |
2906 | ||
2907 | void iommu_fwspec_free(struct device *dev) | |
2908 | { | |
b4ef725e | 2909 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
57f98d2f RM |
2910 | |
2911 | if (fwspec) { | |
2912 | fwnode_handle_put(fwspec->iommu_fwnode); | |
2913 | kfree(fwspec); | |
b4ef725e | 2914 | dev_iommu_fwspec_set(dev, NULL); |
57f98d2f RM |
2915 | } |
2916 | } | |
2917 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); | |
2918 | ||
2919 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) | |
2920 | { | |
b4ef725e | 2921 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
098accf2 | 2922 | int i, new_num; |
57f98d2f RM |
2923 | |
2924 | if (!fwspec) | |
2925 | return -EINVAL; | |
2926 | ||
098accf2 RM |
2927 | new_num = fwspec->num_ids + num_ids; |
2928 | if (new_num > 1) { | |
2929 | fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num), | |
2930 | GFP_KERNEL); | |
57f98d2f RM |
2931 | if (!fwspec) |
2932 | return -ENOMEM; | |
909111ba | 2933 | |
b4ef725e | 2934 | dev_iommu_fwspec_set(dev, fwspec); |
57f98d2f RM |
2935 | } |
2936 | ||
2937 | for (i = 0; i < num_ids; i++) | |
2938 | fwspec->ids[fwspec->num_ids + i] = ids[i]; | |
2939 | ||
098accf2 | 2940 | fwspec->num_ids = new_num; |
57f98d2f RM |
2941 | return 0; |
2942 | } | |
2943 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); | |
a3a19592 LB |
2944 | |
2945 | /* | |
2946 | * Per device IOMMU features. | |
2947 | */ | |
a3a19592 LB |
2948 | int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) |
2949 | { | |
b9abb19f SK |
2950 | if (dev->iommu && dev->iommu->iommu_dev) { |
2951 | const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; | |
a3a19592 | 2952 | |
b9abb19f SK |
2953 | if (ops->dev_enable_feat) |
2954 | return ops->dev_enable_feat(dev, feat); | |
2955 | } | |
a3a19592 LB |
2956 | |
2957 | return -ENODEV; | |
2958 | } | |
2959 | EXPORT_SYMBOL_GPL(iommu_dev_enable_feature); | |
2960 | ||
2961 | /* | |
2962 | * The device drivers should do the necessary cleanups before calling this. | |
2963 | * For example, before disabling the aux-domain feature, the device driver | |
2964 | * should detach all aux-domains. Otherwise, this will return -EBUSY. | |
2965 | */ | |
2966 | int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) | |
2967 | { | |
b9abb19f SK |
2968 | if (dev->iommu && dev->iommu->iommu_dev) { |
2969 | const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; | |
a3a19592 | 2970 | |
b9abb19f SK |
2971 | if (ops->dev_disable_feat) |
2972 | return ops->dev_disable_feat(dev, feat); | |
2973 | } | |
a3a19592 LB |
2974 | |
2975 | return -EBUSY; | |
2976 | } | |
2977 | EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); | |
2978 | ||
2979 | bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat) | |
2980 | { | |
b9abb19f SK |
2981 | if (dev->iommu && dev->iommu->iommu_dev) { |
2982 | const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; | |
a3a19592 | 2983 | |
b9abb19f SK |
2984 | if (ops->dev_feat_enabled) |
2985 | return ops->dev_feat_enabled(dev, feat); | |
2986 | } | |
a3a19592 LB |
2987 | |
2988 | return false; | |
2989 | } | |
2990 | EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled); | |
2991 | ||
2992 | /* | |
2993 | * Aux-domain specific attach/detach. | |
2994 | * | |
2995 | * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns | |
2996 | * true. Also, as long as domains are attached to a device through this | |
2997 | * interface, any tries to call iommu_attach_device() should fail | |
2998 | * (iommu_detach_device() can't fail, so we fail when trying to re-attach). | |
2999 | * This should make us safe against a device being attached to a guest as a | |
3000 | * whole while there are still pasid users on it (aux and sva). | |
3001 | */ | |
3002 | int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev) | |
3003 | { | |
3004 | int ret = -ENODEV; | |
3005 | ||
3006 | if (domain->ops->aux_attach_dev) | |
3007 | ret = domain->ops->aux_attach_dev(domain, dev); | |
3008 | ||
3009 | if (!ret) | |
3010 | trace_attach_device_to_domain(dev); | |
3011 | ||
3012 | return ret; | |
3013 | } | |
3014 | EXPORT_SYMBOL_GPL(iommu_aux_attach_device); | |
3015 | ||
3016 | void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev) | |
3017 | { | |
3018 | if (domain->ops->aux_detach_dev) { | |
3019 | domain->ops->aux_detach_dev(domain, dev); | |
3020 | trace_detach_device_from_domain(dev); | |
3021 | } | |
3022 | } | |
3023 | EXPORT_SYMBOL_GPL(iommu_aux_detach_device); | |
3024 | ||
3025 | int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) | |
3026 | { | |
3027 | int ret = -ENODEV; | |
3028 | ||
3029 | if (domain->ops->aux_get_pasid) | |
3030 | ret = domain->ops->aux_get_pasid(domain, dev); | |
3031 | ||
3032 | return ret; | |
3033 | } | |
3034 | EXPORT_SYMBOL_GPL(iommu_aux_get_pasid); | |
26b25a2b JPB |
3035 | |
3036 | /** | |
3037 | * iommu_sva_bind_device() - Bind a process address space to a device | |
3038 | * @dev: the device | |
3039 | * @mm: the mm to bind, caller must hold a reference to it | |
3040 | * | |
3041 | * Create a bond between device and address space, allowing the device to access | |
3042 | * the mm using the returned PASID. If a bond already exists between @device and | |
3043 | * @mm, it is returned and an additional reference is taken. Caller must call | |
3044 | * iommu_sva_unbind_device() to release each reference. | |
3045 | * | |
3046 | * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to | |
3047 | * initialize the required SVA features. | |
3048 | * | |
3049 | * On error, returns an ERR_PTR value. | |
3050 | */ | |
3051 | struct iommu_sva * | |
3052 | iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) | |
3053 | { | |
3054 | struct iommu_group *group; | |
3055 | struct iommu_sva *handle = ERR_PTR(-EINVAL); | |
3056 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
3057 | ||
3058 | if (!ops || !ops->sva_bind) | |
3059 | return ERR_PTR(-ENODEV); | |
3060 | ||
3061 | group = iommu_group_get(dev); | |
3062 | if (!group) | |
3063 | return ERR_PTR(-ENODEV); | |
3064 | ||
3065 | /* Ensure device count and domain don't change while we're binding */ | |
3066 | mutex_lock(&group->mutex); | |
3067 | ||
3068 | /* | |
3069 | * To keep things simple, SVA currently doesn't support IOMMU groups | |
3070 | * with more than one device. Existing SVA-capable systems are not | |
3071 | * affected by the problems that required IOMMU groups (lack of ACS | |
3072 | * isolation, device ID aliasing and other hardware issues). | |
3073 | */ | |
3074 | if (iommu_group_device_count(group) != 1) | |
3075 | goto out_unlock; | |
3076 | ||
3077 | handle = ops->sva_bind(dev, mm, drvdata); | |
3078 | ||
3079 | out_unlock: | |
3080 | mutex_unlock(&group->mutex); | |
3081 | iommu_group_put(group); | |
3082 | ||
3083 | return handle; | |
3084 | } | |
3085 | EXPORT_SYMBOL_GPL(iommu_sva_bind_device); | |
3086 | ||
3087 | /** | |
3088 | * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device | |
3089 | * @handle: the handle returned by iommu_sva_bind_device() | |
3090 | * | |
3091 | * Put reference to a bond between device and address space. The device should | |
3092 | * not be issuing any more transaction for this PASID. All outstanding page | |
3093 | * requests for this PASID must have been flushed to the IOMMU. | |
26b25a2b JPB |
3094 | */ |
3095 | void iommu_sva_unbind_device(struct iommu_sva *handle) | |
3096 | { | |
3097 | struct iommu_group *group; | |
3098 | struct device *dev = handle->dev; | |
3099 | const struct iommu_ops *ops = dev->bus->iommu_ops; | |
3100 | ||
3101 | if (!ops || !ops->sva_unbind) | |
3102 | return; | |
3103 | ||
3104 | group = iommu_group_get(dev); | |
3105 | if (!group) | |
3106 | return; | |
3107 | ||
3108 | mutex_lock(&group->mutex); | |
3109 | ops->sva_unbind(handle); | |
3110 | mutex_unlock(&group->mutex); | |
3111 | ||
3112 | iommu_group_put(group); | |
3113 | } | |
3114 | EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); | |
3115 | ||
c7b6bac9 | 3116 | u32 iommu_sva_get_pasid(struct iommu_sva *handle) |
26b25a2b JPB |
3117 | { |
3118 | const struct iommu_ops *ops = handle->dev->bus->iommu_ops; | |
3119 | ||
3120 | if (!ops || !ops->sva_get_pasid) | |
3121 | return IOMMU_PASID_INVALID; | |
3122 | ||
3123 | return ops->sva_get_pasid(handle); | |
3124 | } | |
3125 | EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); | |
08a27c1c SPP |
3126 | |
3127 | /* | |
3128 | * Changes the default domain of an iommu group that has *only* one device | |
3129 | * | |
3130 | * @group: The group for which the default domain should be changed | |
3131 | * @prev_dev: The device in the group (this is used to make sure that the device | |
3132 | * hasn't changed after the caller has called this function) | |
3133 | * @type: The type of the new default domain that gets associated with the group | |
3134 | * | |
3135 | * Returns 0 on success and error code on failure | |
3136 | * | |
3137 | * Note: | |
3138 | * 1. Presently, this function is called only when user requests to change the | |
3139 | * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type | |
3140 | * Please take a closer look if intended to use for other purposes. | |
3141 | */ | |
3142 | static int iommu_change_dev_def_domain(struct iommu_group *group, | |
3143 | struct device *prev_dev, int type) | |
3144 | { | |
3145 | struct iommu_domain *prev_dom; | |
3146 | struct group_device *grp_dev; | |
3147 | int ret, dev_def_dom; | |
3148 | struct device *dev; | |
3149 | ||
08a27c1c SPP |
3150 | mutex_lock(&group->mutex); |
3151 | ||
3152 | if (group->default_domain != group->domain) { | |
3153 | dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n"); | |
3154 | ret = -EBUSY; | |
3155 | goto out; | |
3156 | } | |
3157 | ||
3158 | /* | |
3159 | * iommu group wasn't locked while acquiring device lock in | |
3160 | * iommu_group_store_type(). So, make sure that the device count hasn't | |
3161 | * changed while acquiring device lock. | |
3162 | * | |
3163 | * Changing default domain of an iommu group with two or more devices | |
3164 | * isn't supported because there could be a potential deadlock. Consider | |
3165 | * the following scenario. T1 is trying to acquire device locks of all | |
3166 | * the devices in the group and before it could acquire all of them, | |
3167 | * there could be another thread T2 (from different sub-system and use | |
3168 | * case) that has already acquired some of the device locks and might be | |
3169 | * waiting for T1 to release other device locks. | |
3170 | */ | |
3171 | if (iommu_group_device_count(group) != 1) { | |
3172 | dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n"); | |
3173 | ret = -EINVAL; | |
3174 | goto out; | |
3175 | } | |
3176 | ||
3177 | /* Since group has only one device */ | |
3178 | grp_dev = list_first_entry(&group->devices, struct group_device, list); | |
3179 | dev = grp_dev->dev; | |
3180 | ||
3181 | if (prev_dev != dev) { | |
3182 | dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n"); | |
3183 | ret = -EBUSY; | |
3184 | goto out; | |
3185 | } | |
3186 | ||
3187 | prev_dom = group->default_domain; | |
3188 | if (!prev_dom) { | |
3189 | ret = -EINVAL; | |
3190 | goto out; | |
3191 | } | |
3192 | ||
3193 | dev_def_dom = iommu_get_def_domain_type(dev); | |
3194 | if (!type) { | |
3195 | /* | |
3196 | * If the user hasn't requested any specific type of domain and | |
3197 | * if the device supports both the domains, then default to the | |
3198 | * domain the device was booted with | |
3199 | */ | |
3200 | type = dev_def_dom ? : iommu_def_domain_type; | |
3201 | } else if (dev_def_dom && type != dev_def_dom) { | |
3202 | dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n", | |
3203 | iommu_domain_type_str(type)); | |
3204 | ret = -EINVAL; | |
3205 | goto out; | |
3206 | } | |
3207 | ||
3208 | /* | |
3209 | * Switch to a new domain only if the requested domain type is different | |
3210 | * from the existing default domain type | |
3211 | */ | |
3212 | if (prev_dom->type == type) { | |
3213 | ret = 0; | |
3214 | goto out; | |
3215 | } | |
3216 | ||
452e69b5 RM |
3217 | /* We can bring up a flush queue without tearing down the domain */ |
3218 | if (type == IOMMU_DOMAIN_DMA_FQ && prev_dom->type == IOMMU_DOMAIN_DMA) { | |
3219 | ret = iommu_dma_init_fq(prev_dom); | |
3220 | if (!ret) | |
3221 | prev_dom->type = IOMMU_DOMAIN_DMA_FQ; | |
3222 | goto out; | |
3223 | } | |
3224 | ||
08a27c1c SPP |
3225 | /* Sets group->default_domain to the newly allocated domain */ |
3226 | ret = iommu_group_alloc_default_domain(dev->bus, group, type); | |
3227 | if (ret) | |
3228 | goto out; | |
3229 | ||
3230 | ret = iommu_create_device_direct_mappings(group, dev); | |
3231 | if (ret) | |
3232 | goto free_new_domain; | |
3233 | ||
3234 | ret = __iommu_attach_device(group->default_domain, dev); | |
3235 | if (ret) | |
3236 | goto free_new_domain; | |
3237 | ||
3238 | group->domain = group->default_domain; | |
3239 | ||
3240 | /* | |
3241 | * Release the mutex here because ops->probe_finalize() call-back of | |
3242 | * some vendor IOMMU drivers calls arm_iommu_attach_device() which | |
3243 | * in-turn might call back into IOMMU core code, where it tries to take | |
3244 | * group->mutex, resulting in a deadlock. | |
3245 | */ | |
3246 | mutex_unlock(&group->mutex); | |
3247 | ||
3248 | /* Make sure dma_ops is appropriatley set */ | |
3249 | iommu_group_do_probe_finalize(dev, group->default_domain); | |
3250 | iommu_domain_free(prev_dom); | |
3251 | return 0; | |
3252 | ||
3253 | free_new_domain: | |
3254 | iommu_domain_free(group->default_domain); | |
3255 | group->default_domain = prev_dom; | |
3256 | group->domain = prev_dom; | |
3257 | ||
3258 | out: | |
3259 | mutex_unlock(&group->mutex); | |
3260 | ||
3261 | return ret; | |
3262 | } | |
3263 | ||
3264 | /* | |
452e69b5 RM |
3265 | * Changing the default domain through sysfs requires the users to unbind the |
3266 | * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ | |
3267 | * transition. Return failure if this isn't met. | |
08a27c1c SPP |
3268 | * |
3269 | * We need to consider the race between this and the device release path. | |
3270 | * device_lock(dev) is used here to guarantee that the device release path | |
3271 | * will not be entered at the same time. | |
3272 | */ | |
3273 | static ssize_t iommu_group_store_type(struct iommu_group *group, | |
3274 | const char *buf, size_t count) | |
3275 | { | |
3276 | struct group_device *grp_dev; | |
3277 | struct device *dev; | |
3278 | int ret, req_type; | |
3279 | ||
3280 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | |
3281 | return -EACCES; | |
3282 | ||
3283 | if (WARN_ON(!group)) | |
3284 | return -EINVAL; | |
3285 | ||
3286 | if (sysfs_streq(buf, "identity")) | |
3287 | req_type = IOMMU_DOMAIN_IDENTITY; | |
3288 | else if (sysfs_streq(buf, "DMA")) | |
3289 | req_type = IOMMU_DOMAIN_DMA; | |
26225bea RM |
3290 | else if (sysfs_streq(buf, "DMA-FQ")) |
3291 | req_type = IOMMU_DOMAIN_DMA_FQ; | |
08a27c1c SPP |
3292 | else if (sysfs_streq(buf, "auto")) |
3293 | req_type = 0; | |
3294 | else | |
3295 | return -EINVAL; | |
3296 | ||
3297 | /* | |
3298 | * Lock/Unlock the group mutex here before device lock to | |
3299 | * 1. Make sure that the iommu group has only one device (this is a | |
3300 | * prerequisite for step 2) | |
3301 | * 2. Get struct *dev which is needed to lock device | |
3302 | */ | |
3303 | mutex_lock(&group->mutex); | |
3304 | if (iommu_group_device_count(group) != 1) { | |
3305 | mutex_unlock(&group->mutex); | |
3306 | pr_err_ratelimited("Cannot change default domain: Group has more than one device\n"); | |
3307 | return -EINVAL; | |
3308 | } | |
3309 | ||
3310 | /* Since group has only one device */ | |
3311 | grp_dev = list_first_entry(&group->devices, struct group_device, list); | |
3312 | dev = grp_dev->dev; | |
3313 | get_device(dev); | |
3314 | ||
3315 | /* | |
3316 | * Don't hold the group mutex because taking group mutex first and then | |
3317 | * the device lock could potentially cause a deadlock as below. Assume | |
3318 | * two threads T1 and T2. T1 is trying to change default domain of an | |
3319 | * iommu group and T2 is trying to hot unplug a device or release [1] VF | |
3320 | * of a PCIe device which is in the same iommu group. T1 takes group | |
3321 | * mutex and before it could take device lock assume T2 has taken device | |
3322 | * lock and is yet to take group mutex. Now, both the threads will be | |
3323 | * waiting for the other thread to release lock. Below, lock order was | |
3324 | * suggested. | |
3325 | * device_lock(dev); | |
3326 | * mutex_lock(&group->mutex); | |
3327 | * iommu_change_dev_def_domain(); | |
3328 | * mutex_unlock(&group->mutex); | |
3329 | * device_unlock(dev); | |
3330 | * | |
3331 | * [1] Typical device release path | |
3332 | * device_lock() from device/driver core code | |
3333 | * -> bus_notifier() | |
3334 | * -> iommu_bus_notifier() | |
3335 | * -> iommu_release_device() | |
3336 | * -> ops->release_device() vendor driver calls back iommu core code | |
3337 | * -> mutex_lock() from iommu core code | |
3338 | */ | |
3339 | mutex_unlock(&group->mutex); | |
3340 | ||
3341 | /* Check if the device in the group still has a driver bound to it */ | |
3342 | device_lock(dev); | |
452e69b5 RM |
3343 | if (device_is_bound(dev) && !(req_type == IOMMU_DOMAIN_DMA_FQ && |
3344 | group->default_domain->type == IOMMU_DOMAIN_DMA)) { | |
08a27c1c SPP |
3345 | pr_err_ratelimited("Device is still bound to driver\n"); |
3346 | ret = -EBUSY; | |
3347 | goto out; | |
3348 | } | |
3349 | ||
3350 | ret = iommu_change_dev_def_domain(group, dev, req_type); | |
3351 | ret = ret ?: count; | |
3352 | ||
3353 | out: | |
3354 | device_unlock(dev); | |
3355 | put_device(dev); | |
3356 | ||
3357 | return ret; | |
3358 | } |