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