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