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