Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
Joerg Roedel | 63ce3ae | 2015-02-04 16:12:55 +0100 | [diff] [blame] | 3 | * Author: Joerg Roedel <jroedel@suse.de> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 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 | |
Joerg Roedel | 92e7066 | 2015-05-28 18:41:24 +0200 | [diff] [blame] | 19 | #define pr_fmt(fmt) "iommu: " fmt |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 20 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 21 | #include <linux/device.h> |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 23 | #include <linux/bug.h> |
| 24 | #include <linux/types.h> |
Andrew Morton | 60db402 | 2009-05-06 16:03:07 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/slab.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 27 | #include <linux/errno.h> |
| 28 | #include <linux/iommu.h> |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 29 | #include <linux/idr.h> |
| 30 | #include <linux/notifier.h> |
| 31 | #include <linux/err.h> |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 32 | #include <linux/pci.h> |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 33 | #include <linux/bitops.h> |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 34 | #include <linux/property.h> |
Shuah Khan | 7f6db17 | 2013-08-15 11:59:23 -0600 | [diff] [blame] | 35 | #include <trace/events/iommu.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 36 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 37 | static struct kset *iommu_group_kset; |
Heiner Kallweit | e38d1f1 | 2016-06-28 20:38:36 +0200 | [diff] [blame] | 38 | static DEFINE_IDA(iommu_group_ida); |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 39 | static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 40 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 41 | struct iommu_callback_data { |
| 42 | const struct iommu_ops *ops; |
| 43 | }; |
| 44 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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; |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 55 | struct iommu_domain *default_domain; |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 56 | struct iommu_domain *domain; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 59 | struct group_device { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 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", |
Robin Murphy | 9d3a4de | 2017-03-16 17:00:16 +0000 | [diff] [blame] | 76 | [IOMMU_RESV_SW_MSI] = "msi", |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 79 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
| 80 | struct iommu_group_attribute iommu_group_attr_##_name = \ |
| 81 | __ATTR(_name, _mode, _show, _store) |
| 82 | |
| 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) |
| 87 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 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 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 107 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
| 108 | unsigned type); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 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); |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 115 | |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 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 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 128 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
| 129 | struct attribute *__attr, char *buf) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 130 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 134 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 135 | if (attr->show) |
| 136 | ret = attr->show(group, buf); |
| 137 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 138 | } |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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; |
| 147 | |
| 148 | if (attr->store) |
| 149 | ret = attr->store(group, buf, count); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
| 154 | .show = iommu_group_attr_show, |
| 155 | .store = iommu_group_attr_store, |
| 156 | }; |
| 157 | |
| 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); |
| 162 | } |
| 163 | |
| 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 | |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 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; |
Eric Auger | a514a6e | 2017-02-06 10:11:38 +0100 | [diff] [blame] | 241 | int ret = 0; |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 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 | { |
Joerg Roedel | 8d2932d | 2017-02-10 15:13:10 +0100 | [diff] [blame] | 254 | struct group_device *device; |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 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 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 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 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 295 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
| 296 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 297 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
| 298 | iommu_group_show_resv_regions, NULL); |
| 299 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 300 | static void iommu_group_release(struct kobject *kobj) |
| 301 | { |
| 302 | struct iommu_group *group = to_iommu_group(kobj); |
| 303 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 304 | pr_debug("Releasing group %d\n", group->id); |
| 305 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 306 | if (group->iommu_data_release) |
| 307 | group->iommu_data_release(group->iommu_data); |
| 308 | |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 309 | ida_simple_remove(&iommu_group_ida, group->id); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 310 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 311 | if (group->default_domain) |
| 312 | iommu_domain_free(group->default_domain); |
| 313 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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) |
| 336 | { |
| 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 | |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 349 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
| 350 | if (ret < 0) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 351 | kfree(group); |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 352 | return ERR_PTR(ret); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 353 | } |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 354 | group->id = ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 355 | |
| 356 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
| 357 | NULL, "%d", group->id); |
| 358 | if (ret) { |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 359 | ida_simple_remove(&iommu_group_ida, group->id); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 377 | ret = iommu_group_create_file(group, |
| 378 | &iommu_group_attr_reserved_regions); |
| 379 | if (ret) |
| 380 | return ERR_PTR(ret); |
| 381 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 382 | pr_debug("Allocated group %d\n", group->id); |
| 383 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 384 | return group; |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(iommu_group_alloc); |
| 387 | |
Alexey Kardashevskiy | aa16bea | 2013-03-25 10:23:49 +1100 | [diff] [blame] | 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 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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)) |
| 443 | { |
| 444 | group->iommu_data = iommu_data; |
| 445 | group->iommu_data_release = release; |
| 446 | } |
| 447 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); |
| 448 | |
| 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 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
| 483 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 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; |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 488 | struct iommu_resv_region *entry; |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 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 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 496 | BUG_ON(!domain->pgsize_bitmap); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 497 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 498 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 499 | INIT_LIST_HEAD(&mappings); |
| 500 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 501 | iommu_get_resv_regions(dev, &mappings); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 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 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 507 | if (domain->ops->apply_resv_region) |
| 508 | domain->ops->apply_resv_region(dev, domain, entry); |
Joerg Roedel | 33b21a6 | 2016-07-05 13:07:53 +0200 | [diff] [blame] | 509 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 510 | start = ALIGN(entry->start, pg_size); |
| 511 | end = ALIGN(entry->start + entry->length, pg_size); |
| 512 | |
Eric Auger | 544a25d | 2017-01-19 20:57:50 +0000 | [diff] [blame] | 513 | if (entry->type != IOMMU_RESV_DIRECT) |
| 514 | continue; |
| 515 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 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: |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 531 | iommu_put_resv_regions(dev, &mappings); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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) |
| 545 | { |
| 546 | int ret, i = 0; |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 547 | struct group_device *device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 548 | |
| 549 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 550 | if (!device) |
| 551 | return -ENOMEM; |
| 552 | |
| 553 | device->dev = dev; |
| 554 | |
| 555 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 556 | if (ret) |
| 557 | goto err_free_device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 558 | |
| 559 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); |
| 560 | rename: |
| 561 | if (!device->name) { |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 562 | ret = -ENOMEM; |
| 563 | goto err_remove_link; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
| 567 | &dev->kobj, device->name); |
| 568 | if (ret) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 569 | if (ret == -EEXIST && i >= 0) { |
| 570 | /* |
| 571 | * Account for the slim chance of collision |
| 572 | * and append an instance to the name. |
| 573 | */ |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 574 | kfree(device->name); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 575 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
| 576 | kobject_name(&dev->kobj), i++); |
| 577 | goto rename; |
| 578 | } |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 579 | goto err_free_name; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | kobject_get(group->devices_kobj); |
| 583 | |
| 584 | dev->iommu_group = group; |
| 585 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 586 | iommu_group_create_direct_mappings(group, dev); |
| 587 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 588 | mutex_lock(&group->mutex); |
| 589 | list_add_tail(&device->list, &group->devices); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 590 | if (group->domain) |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 591 | ret = __iommu_attach_device(group->domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 592 | mutex_unlock(&group->mutex); |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 593 | if (ret) |
| 594 | goto err_put_group; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 595 | |
| 596 | /* Notify any listeners about change to group. */ |
| 597 | blocking_notifier_call_chain(&group->notifier, |
| 598 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); |
Shuah Khan | d1cf7e8 | 2013-08-15 11:59:24 -0600 | [diff] [blame] | 599 | |
| 600 | trace_add_device_to_group(group->id, dev); |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 601 | |
| 602 | pr_info("Adding device %s to group %d\n", dev_name(dev), group->id); |
| 603 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 604 | return 0; |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 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; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 620 | } |
| 621 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
| 622 | |
| 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; |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 633 | struct group_device *tmp_device, *device = NULL; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 634 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 635 | pr_info("Removing device %s from group %d\n", dev_name(dev), group->id); |
| 636 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
Shuah Khan | 2e75708 | 2013-08-15 11:59:25 -0600 | [diff] [blame] | 657 | trace_remove_device_from_group(group->id, dev); |
| 658 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 666 | static int iommu_group_device_count(struct iommu_group *group) |
| 667 | { |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 668 | struct group_device *entry; |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 669 | int ret = 0; |
| 670 | |
| 671 | list_for_each_entry(entry, &group->devices, list) |
| 672 | ret++; |
| 673 | |
| 674 | return ret; |
| 675 | } |
| 676 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | */ |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 688 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 689 | int (*fn)(struct device *, void *)) |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 690 | { |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 691 | struct group_device *device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 692 | int ret = 0; |
| 693 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 694 | list_for_each_entry(device, &group->devices, list) { |
| 695 | ret = fn(device->dev, data); |
| 696 | if (ret) |
| 697 | break; |
| 698 | } |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 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); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 710 | mutex_unlock(&group->mutex); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 711 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
| 735 | /** |
Robin Murphy | 13f59a7 | 2016-11-11 17:59:21 +0000 | [diff] [blame] | 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 | |
| 748 | /** |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 803 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 804 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
| 805 | unsigned long *devfns); |
| 806 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 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 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 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 | /* |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 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 |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 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 */ |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 875 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 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 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 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 | |
| 912 | /* |
Joerg Roedel | 6eab556 | 2015-10-21 23:51:38 +0200 | [diff] [blame] | 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 | { |
Joerg Roedel | 7f7a230 | 2017-06-28 12:45:31 +0200 | [diff] [blame] | 918 | return iommu_group_alloc(); |
Joerg Roedel | 6eab556 | 2015-10-21 23:51:38 +0200 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /* |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 922 | * Use standard PCI bus topology, isolation features, and DMA alias quirks |
| 923 | * to find or create an IOMMU group for a device. |
| 924 | */ |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 925 | struct iommu_group *pci_device_group(struct device *dev) |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 926 | { |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 927 | struct pci_dev *pdev = to_pci_dev(dev); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 928 | struct group_for_pci_data data; |
| 929 | struct pci_bus *bus; |
| 930 | struct iommu_group *group = NULL; |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 931 | u64 devfns[4] = { 0 }; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 932 | |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 933 | if (WARN_ON(!dev_is_pci(dev))) |
| 934 | return ERR_PTR(-EINVAL); |
| 935 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 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 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 968 | * Look for existing groups on device aliases. If we alias another |
| 969 | * device or another device aliases us, use the same group. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 970 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 971 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
| 972 | if (group) |
| 973 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 974 | |
| 975 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 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. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 979 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 980 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
| 981 | if (group) |
| 982 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 983 | |
| 984 | /* No shared group found, allocate new */ |
Joerg Roedel | 7f7a230 | 2017-06-28 12:45:31 +0200 | [diff] [blame] | 985 | return iommu_group_alloc(); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 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 | { |
Joerg Roedel | 46c6b2b | 2015-10-21 23:51:36 +0200 | [diff] [blame] | 1000 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 1001 | struct iommu_group *group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1002 | int ret; |
| 1003 | |
| 1004 | group = iommu_group_get(dev); |
| 1005 | if (group) |
| 1006 | return group; |
| 1007 | |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame^] | 1008 | if (!ops) |
| 1009 | return ERR_PTR(-EINVAL); |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 1010 | |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame^] | 1011 | group = ops->device_group(dev); |
Joerg Roedel | 72dcac6 | 2017-06-28 12:52:48 +0200 | [diff] [blame] | 1012 | if (WARN_ON_ONCE(group == NULL)) |
| 1013 | return ERR_PTR(-EINVAL); |
| 1014 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1015 | if (IS_ERR(group)) |
| 1016 | return group; |
| 1017 | |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 1018 | /* |
| 1019 | * Try to allocate a default domain - needs support from the |
| 1020 | * IOMMU driver. |
| 1021 | */ |
| 1022 | if (!group->default_domain) { |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1023 | struct iommu_domain *dom; |
| 1024 | |
| 1025 | dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); |
| 1026 | if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { |
| 1027 | dev_warn(dev, |
| 1028 | "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", |
| 1029 | iommu_def_domain_type); |
| 1030 | dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); |
| 1031 | } |
| 1032 | |
| 1033 | group->default_domain = dom; |
Joerg Roedel | eebb803 | 2016-04-04 15:47:48 +0200 | [diff] [blame] | 1034 | if (!group->domain) |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1035 | group->domain = dom; |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1038 | ret = iommu_group_add_device(group, dev); |
| 1039 | if (ret) { |
| 1040 | iommu_group_put(group); |
| 1041 | return ERR_PTR(ret); |
| 1042 | } |
| 1043 | |
| 1044 | return group; |
| 1045 | } |
| 1046 | |
Joerg Roedel | 6827ca8 | 2015-05-28 18:41:35 +0200 | [diff] [blame] | 1047 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
| 1048 | { |
| 1049 | return group->default_domain; |
| 1050 | } |
| 1051 | |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1052 | static int add_iommu_group(struct device *dev, void *data) |
| 1053 | { |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1054 | struct iommu_callback_data *cb = data; |
| 1055 | const struct iommu_ops *ops = cb->ops; |
Joerg Roedel | 38667f1 | 2015-06-29 10:16:08 +0200 | [diff] [blame] | 1056 | int ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1057 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1058 | if (!ops->add_device) |
Marek Szyprowski | 461bfb3f | 2014-11-19 11:15:31 +0000 | [diff] [blame] | 1059 | return 0; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1060 | |
| 1061 | WARN_ON(dev->iommu_group); |
| 1062 | |
Joerg Roedel | 38667f1 | 2015-06-29 10:16:08 +0200 | [diff] [blame] | 1063 | ret = ops->add_device(dev); |
| 1064 | |
| 1065 | /* |
| 1066 | * We ignore -ENODEV errors for now, as they just mean that the |
| 1067 | * device is not translated by an IOMMU. We still care about |
| 1068 | * other errors and fail to initialize when they happen. |
| 1069 | */ |
| 1070 | if (ret == -ENODEV) |
| 1071 | ret = 0; |
| 1072 | |
| 1073 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1076 | static int remove_iommu_group(struct device *dev, void *data) |
| 1077 | { |
| 1078 | struct iommu_callback_data *cb = data; |
| 1079 | const struct iommu_ops *ops = cb->ops; |
| 1080 | |
| 1081 | if (ops->remove_device && dev->iommu_group) |
| 1082 | ops->remove_device(dev); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1087 | static int iommu_bus_notifier(struct notifier_block *nb, |
| 1088 | unsigned long action, void *data) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1089 | { |
| 1090 | struct device *dev = data; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1091 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1092 | struct iommu_group *group; |
| 1093 | unsigned long group_action = 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1094 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1095 | /* |
| 1096 | * ADD/DEL call into iommu driver ops if provided, which may |
| 1097 | * result in ADD/DEL notifiers to group->notifier |
| 1098 | */ |
| 1099 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
zhichang.yuan | 3ba8775 | 2017-04-18 20:51:48 +0800 | [diff] [blame] | 1100 | if (ops->add_device) { |
| 1101 | int ret; |
| 1102 | |
| 1103 | ret = ops->add_device(dev); |
| 1104 | return (ret) ? NOTIFY_DONE : NOTIFY_OK; |
| 1105 | } |
Joerg Roedel | 843cb6d | 2015-05-28 18:41:28 +0200 | [diff] [blame] | 1106 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1107 | if (ops->remove_device && dev->iommu_group) { |
| 1108 | ops->remove_device(dev); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | } |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1112 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1113 | /* |
| 1114 | * Remaining BUS_NOTIFYs get filtered and republished to the |
| 1115 | * group, if anyone is listening |
| 1116 | */ |
| 1117 | group = iommu_group_get(dev); |
| 1118 | if (!group) |
| 1119 | return 0; |
| 1120 | |
| 1121 | switch (action) { |
| 1122 | case BUS_NOTIFY_BIND_DRIVER: |
| 1123 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; |
| 1124 | break; |
| 1125 | case BUS_NOTIFY_BOUND_DRIVER: |
| 1126 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; |
| 1127 | break; |
| 1128 | case BUS_NOTIFY_UNBIND_DRIVER: |
| 1129 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; |
| 1130 | break; |
| 1131 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 1132 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | if (group_action) |
| 1137 | blocking_notifier_call_chain(&group->notifier, |
| 1138 | group_action, dev); |
| 1139 | |
| 1140 | iommu_group_put(group); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1144 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1145 | { |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1146 | int err; |
| 1147 | struct notifier_block *nb; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1148 | struct iommu_callback_data cb = { |
| 1149 | .ops = ops, |
| 1150 | }; |
| 1151 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1152 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
| 1153 | if (!nb) |
| 1154 | return -ENOMEM; |
| 1155 | |
| 1156 | nb->notifier_call = iommu_bus_notifier; |
| 1157 | |
| 1158 | err = bus_register_notifier(bus, nb); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1159 | if (err) |
| 1160 | goto out_free; |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1161 | |
| 1162 | err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1163 | if (err) |
| 1164 | goto out_err; |
| 1165 | |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1166 | |
| 1167 | return 0; |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1168 | |
| 1169 | out_err: |
| 1170 | /* Clean up */ |
| 1171 | bus_for_each_dev(bus, NULL, &cb, remove_iommu_group); |
| 1172 | bus_unregister_notifier(bus, nb); |
| 1173 | |
| 1174 | out_free: |
| 1175 | kfree(nb); |
| 1176 | |
| 1177 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1180 | /** |
| 1181 | * bus_set_iommu - set iommu-callbacks for the bus |
| 1182 | * @bus: bus. |
| 1183 | * @ops: the callbacks provided by the iommu-driver |
| 1184 | * |
| 1185 | * This function is called by an iommu driver to set the iommu methods |
| 1186 | * used for a particular bus. Drivers for devices on that bus can use |
| 1187 | * the iommu-api after these ops are registered. |
| 1188 | * This special function is needed because IOMMUs are usually devices on |
| 1189 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 1190 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 1191 | * afterwards. |
| 1192 | */ |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1193 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1194 | { |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1195 | int err; |
| 1196 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1197 | if (bus->iommu_ops != NULL) |
| 1198 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1199 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1200 | bus->iommu_ops = ops; |
| 1201 | |
| 1202 | /* Do IOMMU specific setup for this bus-type */ |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1203 | err = iommu_bus_init(bus, ops); |
| 1204 | if (err) |
| 1205 | bus->iommu_ops = NULL; |
| 1206 | |
| 1207 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1208 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1209 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 1210 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1211 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1212 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1213 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1214 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1215 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1216 | |
Joerg Roedel | 3c0e0ca | 2014-09-03 18:47:25 +0200 | [diff] [blame] | 1217 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
| 1218 | { |
| 1219 | if (!bus->iommu_ops || !bus->iommu_ops->capable) |
| 1220 | return false; |
| 1221 | |
| 1222 | return bus->iommu_ops->capable(cap); |
| 1223 | } |
| 1224 | EXPORT_SYMBOL_GPL(iommu_capable); |
| 1225 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1226 | /** |
| 1227 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 1228 | * @domain: iommu domain |
| 1229 | * @handler: fault handler |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1230 | * @token: user data, will be passed back to the fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 1231 | * |
| 1232 | * This function should be used by IOMMU users which want to be notified |
| 1233 | * whenever an IOMMU fault happens. |
| 1234 | * |
| 1235 | * The fault handler itself should return 0 on success, and an appropriate |
| 1236 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1237 | */ |
| 1238 | void iommu_set_fault_handler(struct iommu_domain *domain, |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1239 | iommu_fault_handler_t handler, |
| 1240 | void *token) |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1241 | { |
| 1242 | BUG_ON(!domain); |
| 1243 | |
| 1244 | domain->handler = handler; |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1245 | domain->handler_token = token; |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1246 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 1247 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1248 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1249 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
| 1250 | unsigned type) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1251 | { |
| 1252 | struct iommu_domain *domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1253 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1254 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1255 | return NULL; |
| 1256 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1257 | domain = bus->iommu_ops->domain_alloc(type); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1258 | if (!domain) |
| 1259 | return NULL; |
| 1260 | |
Joerg Roedel | 8539c7c | 2015-03-26 13:43:05 +0100 | [diff] [blame] | 1261 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1262 | domain->type = type; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1263 | /* Assume all sizes by default; the driver may override this later */ |
| 1264 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1265 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1266 | return domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1267 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1268 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1269 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
| 1270 | { |
| 1271 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 1274 | |
| 1275 | void iommu_domain_free(struct iommu_domain *domain) |
| 1276 | { |
Joerg Roedel | 89be34a | 2015-03-26 13:43:19 +0100 | [diff] [blame] | 1277 | domain->ops->domain_free(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1278 | } |
| 1279 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 1280 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1281 | static int __iommu_attach_device(struct iommu_domain *domain, |
| 1282 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1283 | { |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1284 | int ret; |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1285 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 1286 | return -ENODEV; |
| 1287 | |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1288 | ret = domain->ops->attach_dev(domain, dev); |
| 1289 | if (!ret) |
| 1290 | trace_attach_device_to_domain(dev); |
| 1291 | return ret; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1292 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1293 | |
| 1294 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 1295 | { |
| 1296 | struct iommu_group *group; |
| 1297 | int ret; |
| 1298 | |
| 1299 | group = iommu_group_get(dev); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1300 | /* |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame^] | 1301 | * Lock the group to make sure the device-count doesn't |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1302 | * change while we are attaching |
| 1303 | */ |
| 1304 | mutex_lock(&group->mutex); |
| 1305 | ret = -EINVAL; |
| 1306 | if (iommu_group_device_count(group) != 1) |
| 1307 | goto out_unlock; |
| 1308 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1309 | ret = __iommu_attach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1310 | |
| 1311 | out_unlock: |
| 1312 | mutex_unlock(&group->mutex); |
| 1313 | iommu_group_put(group); |
| 1314 | |
| 1315 | return ret; |
| 1316 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1317 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 1318 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1319 | static void __iommu_detach_device(struct iommu_domain *domain, |
| 1320 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1321 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1322 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 1323 | return; |
| 1324 | |
| 1325 | domain->ops->detach_dev(domain, dev); |
Shuah Khan | 6998063 | 2013-08-15 11:59:27 -0600 | [diff] [blame] | 1326 | trace_detach_device_from_domain(dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1327 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1328 | |
| 1329 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 1330 | { |
| 1331 | struct iommu_group *group; |
| 1332 | |
| 1333 | group = iommu_group_get(dev); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1334 | |
| 1335 | mutex_lock(&group->mutex); |
| 1336 | if (iommu_group_device_count(group) != 1) { |
| 1337 | WARN_ON(1); |
| 1338 | goto out_unlock; |
| 1339 | } |
| 1340 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1341 | __iommu_detach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1342 | |
| 1343 | out_unlock: |
| 1344 | mutex_unlock(&group->mutex); |
| 1345 | iommu_group_put(group); |
| 1346 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1347 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 1348 | |
Joerg Roedel | 2c1296d | 2015-05-28 18:41:32 +0200 | [diff] [blame] | 1349 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
| 1350 | { |
| 1351 | struct iommu_domain *domain; |
| 1352 | struct iommu_group *group; |
| 1353 | |
| 1354 | group = iommu_group_get(dev); |
Joerg Roedel | 2c1296d | 2015-05-28 18:41:32 +0200 | [diff] [blame] | 1355 | |
| 1356 | domain = group->domain; |
| 1357 | |
| 1358 | iommu_group_put(group); |
| 1359 | |
| 1360 | return domain; |
| 1361 | } |
| 1362 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); |
| 1363 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1364 | /* |
| 1365 | * IOMMU groups are really the natrual working unit of the IOMMU, but |
| 1366 | * the IOMMU API works on domains and devices. Bridge that gap by |
| 1367 | * iterating over the devices in a group. Ideally we'd have a single |
| 1368 | * device which represents the requestor ID of the group, but we also |
| 1369 | * allow IOMMU drivers to create policy defined minimum sets, where |
| 1370 | * the physical hardware may be able to distiguish members, but we |
| 1371 | * wish to group them at a higher level (ex. untrusted multi-function |
| 1372 | * PCI devices). Thus we attach each device. |
| 1373 | */ |
| 1374 | static int iommu_group_do_attach_device(struct device *dev, void *data) |
| 1375 | { |
| 1376 | struct iommu_domain *domain = data; |
| 1377 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1378 | return __iommu_attach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1379 | } |
| 1380 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1381 | static int __iommu_attach_group(struct iommu_domain *domain, |
| 1382 | struct iommu_group *group) |
| 1383 | { |
| 1384 | int ret; |
| 1385 | |
| 1386 | if (group->default_domain && group->domain != group->default_domain) |
| 1387 | return -EBUSY; |
| 1388 | |
| 1389 | ret = __iommu_group_for_each_dev(group, domain, |
| 1390 | iommu_group_do_attach_device); |
| 1391 | if (ret == 0) |
| 1392 | group->domain = domain; |
| 1393 | |
| 1394 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1398 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1399 | int ret; |
| 1400 | |
| 1401 | mutex_lock(&group->mutex); |
| 1402 | ret = __iommu_attach_group(domain, group); |
| 1403 | mutex_unlock(&group->mutex); |
| 1404 | |
| 1405 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1406 | } |
| 1407 | EXPORT_SYMBOL_GPL(iommu_attach_group); |
| 1408 | |
| 1409 | static int iommu_group_do_detach_device(struct device *dev, void *data) |
| 1410 | { |
| 1411 | struct iommu_domain *domain = data; |
| 1412 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1413 | __iommu_detach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1418 | static void __iommu_detach_group(struct iommu_domain *domain, |
| 1419 | struct iommu_group *group) |
| 1420 | { |
| 1421 | int ret; |
| 1422 | |
| 1423 | if (!group->default_domain) { |
| 1424 | __iommu_group_for_each_dev(group, domain, |
| 1425 | iommu_group_do_detach_device); |
| 1426 | group->domain = NULL; |
| 1427 | return; |
| 1428 | } |
| 1429 | |
| 1430 | if (group->domain == group->default_domain) |
| 1431 | return; |
| 1432 | |
| 1433 | /* Detach by re-attaching to the default domain */ |
| 1434 | ret = __iommu_group_for_each_dev(group, group->default_domain, |
| 1435 | iommu_group_do_attach_device); |
| 1436 | if (ret != 0) |
| 1437 | WARN_ON(1); |
| 1438 | else |
| 1439 | group->domain = group->default_domain; |
| 1440 | } |
| 1441 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1442 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1443 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1444 | mutex_lock(&group->mutex); |
| 1445 | __iommu_detach_group(domain, group); |
| 1446 | mutex_unlock(&group->mutex); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1447 | } |
| 1448 | EXPORT_SYMBOL_GPL(iommu_detach_group); |
| 1449 | |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 1450 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1451 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1452 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 1453 | return 0; |
| 1454 | |
| 1455 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1456 | } |
| 1457 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 1458 | |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1459 | static size_t iommu_pgsize(struct iommu_domain *domain, |
| 1460 | unsigned long addr_merge, size_t size) |
| 1461 | { |
| 1462 | unsigned int pgsize_idx; |
| 1463 | size_t pgsize; |
| 1464 | |
| 1465 | /* Max page size that still fits into 'size' */ |
| 1466 | pgsize_idx = __fls(size); |
| 1467 | |
| 1468 | /* need to consider alignment requirements ? */ |
| 1469 | if (likely(addr_merge)) { |
| 1470 | /* Max page size allowed by address */ |
| 1471 | unsigned int align_pgsize_idx = __ffs(addr_merge); |
| 1472 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); |
| 1473 | } |
| 1474 | |
| 1475 | /* build a mask of acceptable page sizes */ |
| 1476 | pgsize = (1UL << (pgsize_idx + 1)) - 1; |
| 1477 | |
| 1478 | /* throw away page sizes not supported by the hardware */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1479 | pgsize &= domain->pgsize_bitmap; |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1480 | |
| 1481 | /* make sure we're still sane */ |
| 1482 | BUG_ON(!pgsize); |
| 1483 | |
| 1484 | /* pick the biggest page */ |
| 1485 | pgsize_idx = __fls(pgsize); |
| 1486 | pgsize = 1UL << pgsize_idx; |
| 1487 | |
| 1488 | return pgsize; |
| 1489 | } |
| 1490 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1491 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1492 | phys_addr_t paddr, size_t size, int prot) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1493 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1494 | unsigned long orig_iova = iova; |
| 1495 | unsigned int min_pagesz; |
| 1496 | size_t orig_size = size; |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1497 | phys_addr_t orig_paddr = paddr; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1498 | int ret = 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1499 | |
Joerg Roedel | 9db4ad9 | 2014-08-19 00:19:26 +0200 | [diff] [blame] | 1500 | if (unlikely(domain->ops->map == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1501 | domain->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1502 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1503 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1504 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
| 1505 | return -EINVAL; |
| 1506 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1507 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1508 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1509 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1510 | /* |
| 1511 | * both the virtual address and the physical one, as well as |
| 1512 | * the size of the mapping, must be aligned (at least) to the |
| 1513 | * size of the smallest page supported by the hardware |
| 1514 | */ |
| 1515 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1516 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1517 | iova, &paddr, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1518 | return -EINVAL; |
| 1519 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1520 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1521 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1522 | |
| 1523 | while (size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1524 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1525 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1526 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1527 | iova, &paddr, pgsize); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1528 | |
| 1529 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); |
| 1530 | if (ret) |
| 1531 | break; |
| 1532 | |
| 1533 | iova += pgsize; |
| 1534 | paddr += pgsize; |
| 1535 | size -= pgsize; |
| 1536 | } |
| 1537 | |
| 1538 | /* unroll mapping in case something went wrong */ |
| 1539 | if (ret) |
| 1540 | iommu_unmap(domain, orig_iova, orig_size - size); |
Shuah Khan | e0be7c8 | 2013-08-15 11:59:28 -0600 | [diff] [blame] | 1541 | else |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1542 | trace_map(orig_iova, orig_paddr, orig_size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1543 | |
| 1544 | return ret; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1545 | } |
| 1546 | EXPORT_SYMBOL_GPL(iommu_map); |
| 1547 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1548 | size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1549 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1550 | size_t unmapped_page, unmapped = 0; |
| 1551 | unsigned int min_pagesz; |
Shuah Khan | 6fd492f | 2015-01-16 16:47:19 -0700 | [diff] [blame] | 1552 | unsigned long orig_iova = iova; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1553 | |
Joerg Roedel | 5788651 | 2013-01-29 13:41:09 +0100 | [diff] [blame] | 1554 | if (unlikely(domain->ops->unmap == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1555 | domain->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1556 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1557 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1558 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
| 1559 | return -EINVAL; |
| 1560 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1561 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1562 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1563 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1564 | /* |
| 1565 | * The virtual address, as well as the size of the mapping, must be |
| 1566 | * aligned (at least) to the size of the smallest page supported |
| 1567 | * by the hardware |
| 1568 | */ |
| 1569 | if (!IS_ALIGNED(iova | size, min_pagesz)) { |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1570 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
| 1571 | iova, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1572 | return -EINVAL; |
| 1573 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1574 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1575 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1576 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1577 | /* |
| 1578 | * Keep iterating until we either unmap 'size' bytes (or more) |
| 1579 | * or we hit an area that isn't mapped. |
| 1580 | */ |
| 1581 | while (unmapped < size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1582 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1583 | |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1584 | unmapped_page = domain->ops->unmap(domain, iova, pgsize); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1585 | if (!unmapped_page) |
| 1586 | break; |
| 1587 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1588 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
| 1589 | iova, unmapped_page); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1590 | |
| 1591 | iova += unmapped_page; |
| 1592 | unmapped += unmapped_page; |
| 1593 | } |
| 1594 | |
Shuah Khan | db8614d | 2015-01-16 20:53:17 -0700 | [diff] [blame] | 1595 | trace_unmap(orig_iova, size, unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1596 | return unmapped; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1597 | } |
| 1598 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1599 | |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1600 | size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
| 1601 | struct scatterlist *sg, unsigned int nents, int prot) |
| 1602 | { |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1603 | struct scatterlist *s; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1604 | size_t mapped = 0; |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1605 | unsigned int i, min_pagesz; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1606 | int ret; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1607 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1608 | if (unlikely(domain->pgsize_bitmap == 0UL)) |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1609 | return 0; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1610 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1611 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1612 | |
| 1613 | for_each_sg(sg, s, nents, i) { |
Dan Williams | 3e6110f | 2015-12-15 12:54:06 -0800 | [diff] [blame] | 1614 | phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset; |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1615 | |
| 1616 | /* |
| 1617 | * We are mapping on IOMMU page boundaries, so offset within |
| 1618 | * the page must be 0. However, the IOMMU may support pages |
| 1619 | * smaller than PAGE_SIZE, so s->offset may still represent |
| 1620 | * an offset of that boundary within the CPU page. |
| 1621 | */ |
| 1622 | if (!IS_ALIGNED(s->offset, min_pagesz)) |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1623 | goto out_err; |
| 1624 | |
| 1625 | ret = iommu_map(domain, iova + mapped, phys, s->length, prot); |
| 1626 | if (ret) |
| 1627 | goto out_err; |
| 1628 | |
| 1629 | mapped += s->length; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | return mapped; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1633 | |
| 1634 | out_err: |
| 1635 | /* undo mappings already done */ |
| 1636 | iommu_unmap(domain, iova, mapped); |
| 1637 | |
| 1638 | return 0; |
| 1639 | |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1640 | } |
| 1641 | EXPORT_SYMBOL_GPL(default_iommu_map_sg); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1642 | |
| 1643 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1644 | phys_addr_t paddr, u64 size, int prot) |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1645 | { |
| 1646 | if (unlikely(domain->ops->domain_window_enable == NULL)) |
| 1647 | return -ENODEV; |
| 1648 | |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1649 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
| 1650 | prot); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1651 | } |
| 1652 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); |
| 1653 | |
| 1654 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) |
| 1655 | { |
| 1656 | if (unlikely(domain->ops->domain_window_disable == NULL)) |
| 1657 | return; |
| 1658 | |
| 1659 | return domain->ops->domain_window_disable(domain, wnd_nr); |
| 1660 | } |
| 1661 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); |
| 1662 | |
Joerg Roedel | 207c6e3 | 2017-04-26 15:39:28 +0200 | [diff] [blame] | 1663 | /** |
| 1664 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework |
| 1665 | * @domain: the iommu domain where the fault has happened |
| 1666 | * @dev: the device where the fault has happened |
| 1667 | * @iova: the faulting address |
| 1668 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) |
| 1669 | * |
| 1670 | * This function should be called by the low-level IOMMU implementations |
| 1671 | * whenever IOMMU faults happen, to allow high-level users, that are |
| 1672 | * interested in such events, to know about them. |
| 1673 | * |
| 1674 | * This event may be useful for several possible use cases: |
| 1675 | * - mere logging of the event |
| 1676 | * - dynamic TLB/PTE loading |
| 1677 | * - if restarting of the faulting device is required |
| 1678 | * |
| 1679 | * Returns 0 on success and an appropriate error code otherwise (if dynamic |
| 1680 | * PTE/TLB loading will one day be supported, implementations will be able |
| 1681 | * to tell whether it succeeded or not according to this return value). |
| 1682 | * |
| 1683 | * Specifically, -ENOSYS is returned if a fault handler isn't installed |
| 1684 | * (though fault handlers can also return -ENOSYS, in case they want to |
| 1685 | * elicit the default behavior of the IOMMU drivers). |
| 1686 | */ |
| 1687 | int report_iommu_fault(struct iommu_domain *domain, struct device *dev, |
| 1688 | unsigned long iova, int flags) |
| 1689 | { |
| 1690 | int ret = -ENOSYS; |
| 1691 | |
| 1692 | /* |
| 1693 | * if upper layers showed interest and installed a fault handler, |
| 1694 | * invoke it. |
| 1695 | */ |
| 1696 | if (domain->handler) |
| 1697 | ret = domain->handler(domain, dev, iova, flags, |
| 1698 | domain->handler_token); |
| 1699 | |
| 1700 | trace_io_page_fault(dev, iova, flags); |
| 1701 | return ret; |
| 1702 | } |
| 1703 | EXPORT_SYMBOL_GPL(report_iommu_fault); |
| 1704 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1705 | static int __init iommu_init(void) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1706 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1707 | iommu_group_kset = kset_create_and_add("iommu_groups", |
| 1708 | NULL, kernel_kobj); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1709 | BUG_ON(!iommu_group_kset); |
| 1710 | |
| 1711 | return 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1712 | } |
Marek Szyprowski | d7ef999 | 2015-05-19 15:20:23 +0200 | [diff] [blame] | 1713 | core_initcall(iommu_init); |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1714 | |
| 1715 | int iommu_domain_get_attr(struct iommu_domain *domain, |
| 1716 | enum iommu_attr attr, void *data) |
| 1717 | { |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1718 | struct iommu_domain_geometry *geometry; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1719 | bool *paging; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1720 | int ret = 0; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1721 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1722 | |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1723 | switch (attr) { |
| 1724 | case DOMAIN_ATTR_GEOMETRY: |
| 1725 | geometry = data; |
| 1726 | *geometry = domain->geometry; |
| 1727 | |
| 1728 | break; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1729 | case DOMAIN_ATTR_PAGING: |
| 1730 | paging = data; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1731 | *paging = (domain->pgsize_bitmap != 0UL); |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1732 | break; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1733 | case DOMAIN_ATTR_WINDOWS: |
| 1734 | count = data; |
| 1735 | |
| 1736 | if (domain->ops->domain_get_windows != NULL) |
| 1737 | *count = domain->ops->domain_get_windows(domain); |
| 1738 | else |
| 1739 | ret = -ENODEV; |
| 1740 | |
| 1741 | break; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1742 | default: |
| 1743 | if (!domain->ops->domain_get_attr) |
| 1744 | return -EINVAL; |
| 1745 | |
| 1746 | ret = domain->ops->domain_get_attr(domain, attr, data); |
| 1747 | } |
| 1748 | |
| 1749 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1750 | } |
| 1751 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); |
| 1752 | |
| 1753 | int iommu_domain_set_attr(struct iommu_domain *domain, |
| 1754 | enum iommu_attr attr, void *data) |
| 1755 | { |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1756 | int ret = 0; |
| 1757 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1758 | |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1759 | switch (attr) { |
| 1760 | case DOMAIN_ATTR_WINDOWS: |
| 1761 | count = data; |
| 1762 | |
| 1763 | if (domain->ops->domain_set_windows != NULL) |
| 1764 | ret = domain->ops->domain_set_windows(domain, *count); |
| 1765 | else |
| 1766 | ret = -ENODEV; |
| 1767 | |
| 1768 | break; |
| 1769 | default: |
| 1770 | if (domain->ops->domain_set_attr == NULL) |
| 1771 | return -EINVAL; |
| 1772 | |
| 1773 | ret = domain->ops->domain_set_attr(domain, attr, data); |
| 1774 | } |
| 1775 | |
| 1776 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1777 | } |
| 1778 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1779 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 1780 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1781 | { |
| 1782 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 1783 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 1784 | if (ops && ops->get_resv_regions) |
| 1785 | ops->get_resv_regions(dev, list); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1786 | } |
| 1787 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 1788 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1789 | { |
| 1790 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 1791 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 1792 | if (ops && ops->put_resv_regions) |
| 1793 | ops->put_resv_regions(dev, list); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1794 | } |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 1795 | |
Eric Auger | 2b20cbb | 2017-01-19 20:57:49 +0000 | [diff] [blame] | 1796 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
Robin Murphy | 9d3a4de | 2017-03-16 17:00:16 +0000 | [diff] [blame] | 1797 | size_t length, int prot, |
| 1798 | enum iommu_resv_type type) |
Eric Auger | 2b20cbb | 2017-01-19 20:57:49 +0000 | [diff] [blame] | 1799 | { |
| 1800 | struct iommu_resv_region *region; |
| 1801 | |
| 1802 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 1803 | if (!region) |
| 1804 | return NULL; |
| 1805 | |
| 1806 | INIT_LIST_HEAD(®ion->list); |
| 1807 | region->start = start; |
| 1808 | region->length = length; |
| 1809 | region->prot = prot; |
| 1810 | region->type = type; |
| 1811 | return region; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1812 | } |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 1813 | |
| 1814 | /* Request that a device is direct mapped by the IOMMU */ |
| 1815 | int iommu_request_dm_for_dev(struct device *dev) |
| 1816 | { |
| 1817 | struct iommu_domain *dm_domain; |
| 1818 | struct iommu_group *group; |
| 1819 | int ret; |
| 1820 | |
| 1821 | /* Device must already be in a group before calling this function */ |
| 1822 | group = iommu_group_get_for_dev(dev); |
Dan Carpenter | 409e553 | 2015-06-10 13:59:27 +0300 | [diff] [blame] | 1823 | if (IS_ERR(group)) |
| 1824 | return PTR_ERR(group); |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 1825 | |
| 1826 | mutex_lock(&group->mutex); |
| 1827 | |
| 1828 | /* Check if the default domain is already direct mapped */ |
| 1829 | ret = 0; |
| 1830 | if (group->default_domain && |
| 1831 | group->default_domain->type == IOMMU_DOMAIN_IDENTITY) |
| 1832 | goto out; |
| 1833 | |
| 1834 | /* Don't change mappings of existing devices */ |
| 1835 | ret = -EBUSY; |
| 1836 | if (iommu_group_device_count(group) != 1) |
| 1837 | goto out; |
| 1838 | |
| 1839 | /* Allocate a direct mapped domain */ |
| 1840 | ret = -ENOMEM; |
| 1841 | dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); |
| 1842 | if (!dm_domain) |
| 1843 | goto out; |
| 1844 | |
| 1845 | /* Attach the device to the domain */ |
| 1846 | ret = __iommu_attach_group(dm_domain, group); |
| 1847 | if (ret) { |
| 1848 | iommu_domain_free(dm_domain); |
| 1849 | goto out; |
| 1850 | } |
| 1851 | |
| 1852 | /* Make the direct mapped domain the default for this group */ |
| 1853 | if (group->default_domain) |
| 1854 | iommu_domain_free(group->default_domain); |
| 1855 | group->default_domain = dm_domain; |
| 1856 | |
| 1857 | pr_info("Using direct mapping for device %s\n", dev_name(dev)); |
| 1858 | |
| 1859 | ret = 0; |
| 1860 | out: |
| 1861 | mutex_unlock(&group->mutex); |
| 1862 | iommu_group_put(group); |
| 1863 | |
| 1864 | return ret; |
| 1865 | } |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 1866 | |
Joerg Roedel | 534766d | 2017-01-31 16:58:42 +0100 | [diff] [blame] | 1867 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 1868 | { |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 1869 | const struct iommu_ops *ops = NULL; |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 1870 | struct iommu_device *iommu; |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 1871 | |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 1872 | spin_lock(&iommu_device_lock); |
| 1873 | list_for_each_entry(iommu, &iommu_device_list, list) |
| 1874 | if (iommu->fwnode == fwnode) { |
| 1875 | ops = iommu->ops; |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 1878 | spin_unlock(&iommu_device_lock); |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 1879 | return ops; |
| 1880 | } |
| 1881 | |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 1882 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
| 1883 | const struct iommu_ops *ops) |
| 1884 | { |
| 1885 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1886 | |
| 1887 | if (fwspec) |
| 1888 | return ops == fwspec->ops ? 0 : -EINVAL; |
| 1889 | |
| 1890 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); |
| 1891 | if (!fwspec) |
| 1892 | return -ENOMEM; |
| 1893 | |
| 1894 | of_node_get(to_of_node(iommu_fwnode)); |
| 1895 | fwspec->iommu_fwnode = iommu_fwnode; |
| 1896 | fwspec->ops = ops; |
| 1897 | dev->iommu_fwspec = fwspec; |
| 1898 | return 0; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); |
| 1901 | |
| 1902 | void iommu_fwspec_free(struct device *dev) |
| 1903 | { |
| 1904 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1905 | |
| 1906 | if (fwspec) { |
| 1907 | fwnode_handle_put(fwspec->iommu_fwnode); |
| 1908 | kfree(fwspec); |
| 1909 | dev->iommu_fwspec = NULL; |
| 1910 | } |
| 1911 | } |
| 1912 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); |
| 1913 | |
| 1914 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) |
| 1915 | { |
| 1916 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1917 | size_t size; |
| 1918 | int i; |
| 1919 | |
| 1920 | if (!fwspec) |
| 1921 | return -EINVAL; |
| 1922 | |
| 1923 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); |
| 1924 | if (size > sizeof(*fwspec)) { |
| 1925 | fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); |
| 1926 | if (!fwspec) |
| 1927 | return -ENOMEM; |
Zhen Lei | 909111b | 2017-02-03 17:35:02 +0800 | [diff] [blame] | 1928 | |
| 1929 | dev->iommu_fwspec = fwspec; |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | for (i = 0; i < num_ids; i++) |
| 1933 | fwspec->ids[fwspec->num_ids + i] = ids[i]; |
| 1934 | |
| 1935 | fwspec->num_ids += num_ids; |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); |