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