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"); |
| 389 | if (ret) { |
| 390 | kfree(device); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); |
| 395 | rename: |
| 396 | if (!device->name) { |
| 397 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 398 | kfree(device); |
| 399 | return -ENOMEM; |
| 400 | } |
| 401 | |
| 402 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
| 403 | &dev->kobj, device->name); |
| 404 | if (ret) { |
| 405 | kfree(device->name); |
| 406 | if (ret == -EEXIST && i >= 0) { |
| 407 | /* |
| 408 | * Account for the slim chance of collision |
| 409 | * and append an instance to the name. |
| 410 | */ |
| 411 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
| 412 | kobject_name(&dev->kobj), i++); |
| 413 | goto rename; |
| 414 | } |
| 415 | |
| 416 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 417 | kfree(device); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | kobject_get(group->devices_kobj); |
| 422 | |
| 423 | dev->iommu_group = group; |
| 424 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 425 | iommu_group_create_direct_mappings(group, dev); |
| 426 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 427 | mutex_lock(&group->mutex); |
| 428 | list_add_tail(&device->list, &group->devices); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 429 | if (group->domain) |
| 430 | __iommu_attach_device(group->domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 431 | mutex_unlock(&group->mutex); |
| 432 | |
| 433 | /* Notify any listeners about change to group. */ |
| 434 | blocking_notifier_call_chain(&group->notifier, |
| 435 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); |
Shuah Khan | d1cf7e8 | 2013-08-15 11:59:24 -0600 | [diff] [blame] | 436 | |
| 437 | trace_add_device_to_group(group->id, dev); |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 438 | |
| 439 | pr_info("Adding device %s to group %d\n", dev_name(dev), group->id); |
| 440 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
| 444 | |
| 445 | /** |
| 446 | * iommu_group_remove_device - remove a device from it's current group |
| 447 | * @dev: device to be removed |
| 448 | * |
| 449 | * This function is called by an iommu driver to remove the device from |
| 450 | * it's current group. This decrements the iommu group reference count. |
| 451 | */ |
| 452 | void iommu_group_remove_device(struct device *dev) |
| 453 | { |
| 454 | struct iommu_group *group = dev->iommu_group; |
| 455 | struct iommu_device *tmp_device, *device = NULL; |
| 456 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 457 | pr_info("Removing device %s from group %d\n", dev_name(dev), group->id); |
| 458 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 459 | /* Pre-notify listeners that a device is being removed. */ |
| 460 | blocking_notifier_call_chain(&group->notifier, |
| 461 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); |
| 462 | |
| 463 | mutex_lock(&group->mutex); |
| 464 | list_for_each_entry(tmp_device, &group->devices, list) { |
| 465 | if (tmp_device->dev == dev) { |
| 466 | device = tmp_device; |
| 467 | list_del(&device->list); |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | mutex_unlock(&group->mutex); |
| 472 | |
| 473 | if (!device) |
| 474 | return; |
| 475 | |
| 476 | sysfs_remove_link(group->devices_kobj, device->name); |
| 477 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 478 | |
Shuah Khan | 2e75708 | 2013-08-15 11:59:25 -0600 | [diff] [blame] | 479 | trace_remove_device_from_group(group->id, dev); |
| 480 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 481 | kfree(device->name); |
| 482 | kfree(device); |
| 483 | dev->iommu_group = NULL; |
| 484 | kobject_put(group->devices_kobj); |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); |
| 487 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 488 | static int iommu_group_device_count(struct iommu_group *group) |
| 489 | { |
| 490 | struct iommu_device *entry; |
| 491 | int ret = 0; |
| 492 | |
| 493 | list_for_each_entry(entry, &group->devices, list) |
| 494 | ret++; |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 499 | /** |
| 500 | * iommu_group_for_each_dev - iterate over each device in the group |
| 501 | * @group: the group |
| 502 | * @data: caller opaque data to be passed to callback function |
| 503 | * @fn: caller supplied callback function |
| 504 | * |
| 505 | * This function is called by group users to iterate over group devices. |
| 506 | * Callers should hold a reference count to the group during callback. |
| 507 | * The group->mutex is held across callbacks, which will block calls to |
| 508 | * iommu_group_add/remove_device. |
| 509 | */ |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 510 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 511 | int (*fn)(struct device *, void *)) |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 512 | { |
| 513 | struct iommu_device *device; |
| 514 | int ret = 0; |
| 515 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 516 | list_for_each_entry(device, &group->devices, list) { |
| 517 | ret = fn(device->dev, data); |
| 518 | if (ret) |
| 519 | break; |
| 520 | } |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 526 | int (*fn)(struct device *, void *)) |
| 527 | { |
| 528 | int ret; |
| 529 | |
| 530 | mutex_lock(&group->mutex); |
| 531 | ret = __iommu_group_for_each_dev(group, data, fn); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 532 | mutex_unlock(&group->mutex); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 533 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 534 | return ret; |
| 535 | } |
| 536 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); |
| 537 | |
| 538 | /** |
| 539 | * iommu_group_get - Return the group for a device and increment reference |
| 540 | * @dev: get the group that this device belongs to |
| 541 | * |
| 542 | * This function is called by iommu drivers and users to get the group |
| 543 | * for the specified device. If found, the group is returned and the group |
| 544 | * reference in incremented, else NULL. |
| 545 | */ |
| 546 | struct iommu_group *iommu_group_get(struct device *dev) |
| 547 | { |
| 548 | struct iommu_group *group = dev->iommu_group; |
| 549 | |
| 550 | if (group) |
| 551 | kobject_get(group->devices_kobj); |
| 552 | |
| 553 | return group; |
| 554 | } |
| 555 | EXPORT_SYMBOL_GPL(iommu_group_get); |
| 556 | |
| 557 | /** |
| 558 | * iommu_group_put - Decrement group reference |
| 559 | * @group: the group to use |
| 560 | * |
| 561 | * This function is called by iommu drivers and users to release the |
| 562 | * iommu group. Once the reference count is zero, the group is released. |
| 563 | */ |
| 564 | void iommu_group_put(struct iommu_group *group) |
| 565 | { |
| 566 | if (group) |
| 567 | kobject_put(group->devices_kobj); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(iommu_group_put); |
| 570 | |
| 571 | /** |
| 572 | * iommu_group_register_notifier - Register a notifier for group changes |
| 573 | * @group: the group to watch |
| 574 | * @nb: notifier block to signal |
| 575 | * |
| 576 | * This function allows iommu group users to track changes in a group. |
| 577 | * See include/linux/iommu.h for actions sent via this notifier. Caller |
| 578 | * should hold a reference to the group throughout notifier registration. |
| 579 | */ |
| 580 | int iommu_group_register_notifier(struct iommu_group *group, |
| 581 | struct notifier_block *nb) |
| 582 | { |
| 583 | return blocking_notifier_chain_register(&group->notifier, nb); |
| 584 | } |
| 585 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); |
| 586 | |
| 587 | /** |
| 588 | * iommu_group_unregister_notifier - Unregister a notifier |
| 589 | * @group: the group to watch |
| 590 | * @nb: notifier block to signal |
| 591 | * |
| 592 | * Unregister a previously registered group notifier block. |
| 593 | */ |
| 594 | int iommu_group_unregister_notifier(struct iommu_group *group, |
| 595 | struct notifier_block *nb) |
| 596 | { |
| 597 | return blocking_notifier_chain_unregister(&group->notifier, nb); |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); |
| 600 | |
| 601 | /** |
| 602 | * iommu_group_id - Return ID for a group |
| 603 | * @group: the group to ID |
| 604 | * |
| 605 | * Return the unique ID for the group matching the sysfs group number. |
| 606 | */ |
| 607 | int iommu_group_id(struct iommu_group *group) |
| 608 | { |
| 609 | return group->id; |
| 610 | } |
| 611 | EXPORT_SYMBOL_GPL(iommu_group_id); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 612 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 613 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
| 614 | unsigned long *devfns); |
| 615 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 616 | /* |
| 617 | * To consider a PCI device isolated, we require ACS to support Source |
| 618 | * Validation, Request Redirection, Completer Redirection, and Upstream |
| 619 | * Forwarding. This effectively means that devices cannot spoof their |
| 620 | * requester ID, requests and completions cannot be redirected, and all |
| 621 | * transactions are forwarded upstream, even as it passes through a |
| 622 | * bridge where the target device is downstream. |
| 623 | */ |
| 624 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) |
| 625 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 626 | /* |
| 627 | * For multifunction devices which are not isolated from each other, find |
| 628 | * all the other non-isolated functions and look for existing groups. For |
| 629 | * each function, we also need to look for aliases to or from other devices |
| 630 | * that may already have a group. |
| 631 | */ |
| 632 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, |
| 633 | unsigned long *devfns) |
| 634 | { |
| 635 | struct pci_dev *tmp = NULL; |
| 636 | struct iommu_group *group; |
| 637 | |
| 638 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) |
| 639 | return NULL; |
| 640 | |
| 641 | for_each_pci_dev(tmp) { |
| 642 | if (tmp == pdev || tmp->bus != pdev->bus || |
| 643 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || |
| 644 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) |
| 645 | continue; |
| 646 | |
| 647 | group = get_pci_alias_group(tmp, devfns); |
| 648 | if (group) { |
| 649 | pci_dev_put(tmp); |
| 650 | return group; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | /* |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 658 | * Look for aliases to or from the given device for existing groups. DMA |
| 659 | * aliases are only supported on the same bus, therefore the search |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 660 | * space is quite small (especially since we're really only looking at pcie |
| 661 | * device, and therefore only expect multiple slots on the root complex or |
| 662 | * downstream switch ports). It's conceivable though that a pair of |
| 663 | * multifunction devices could have aliases between them that would cause a |
| 664 | * loop. To prevent this, we use a bitmap to track where we've been. |
| 665 | */ |
| 666 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
| 667 | unsigned long *devfns) |
| 668 | { |
| 669 | struct pci_dev *tmp = NULL; |
| 670 | struct iommu_group *group; |
| 671 | |
| 672 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) |
| 673 | return NULL; |
| 674 | |
| 675 | group = iommu_group_get(&pdev->dev); |
| 676 | if (group) |
| 677 | return group; |
| 678 | |
| 679 | for_each_pci_dev(tmp) { |
| 680 | if (tmp == pdev || tmp->bus != pdev->bus) |
| 681 | continue; |
| 682 | |
| 683 | /* We alias them or they alias us */ |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 684 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 685 | group = get_pci_alias_group(tmp, devfns); |
| 686 | if (group) { |
| 687 | pci_dev_put(tmp); |
| 688 | return group; |
| 689 | } |
| 690 | |
| 691 | group = get_pci_function_alias_group(tmp, devfns); |
| 692 | if (group) { |
| 693 | pci_dev_put(tmp); |
| 694 | return group; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return NULL; |
| 700 | } |
| 701 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 702 | struct group_for_pci_data { |
| 703 | struct pci_dev *pdev; |
| 704 | struct iommu_group *group; |
| 705 | }; |
| 706 | |
| 707 | /* |
| 708 | * DMA alias iterator callback, return the last seen device. Stop and return |
| 709 | * the IOMMU group if we find one along the way. |
| 710 | */ |
| 711 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) |
| 712 | { |
| 713 | struct group_for_pci_data *data = opaque; |
| 714 | |
| 715 | data->pdev = pdev; |
| 716 | data->group = iommu_group_get(&pdev->dev); |
| 717 | |
| 718 | return data->group != NULL; |
| 719 | } |
| 720 | |
| 721 | /* |
Joerg Roedel | 6eab556 | 2015-10-21 23:51:38 +0200 | [diff] [blame] | 722 | * Generic device_group call-back function. It just allocates one |
| 723 | * iommu-group per device. |
| 724 | */ |
| 725 | struct iommu_group *generic_device_group(struct device *dev) |
| 726 | { |
| 727 | struct iommu_group *group; |
| 728 | |
| 729 | group = iommu_group_alloc(); |
| 730 | if (IS_ERR(group)) |
| 731 | return NULL; |
| 732 | |
| 733 | return group; |
| 734 | } |
| 735 | |
| 736 | /* |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 737 | * Use standard PCI bus topology, isolation features, and DMA alias quirks |
| 738 | * to find or create an IOMMU group for a device. |
| 739 | */ |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 740 | struct iommu_group *pci_device_group(struct device *dev) |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 741 | { |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 742 | struct pci_dev *pdev = to_pci_dev(dev); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 743 | struct group_for_pci_data data; |
| 744 | struct pci_bus *bus; |
| 745 | struct iommu_group *group = NULL; |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 746 | u64 devfns[4] = { 0 }; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 747 | |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 748 | if (WARN_ON(!dev_is_pci(dev))) |
| 749 | return ERR_PTR(-EINVAL); |
| 750 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 751 | /* |
| 752 | * Find the upstream DMA alias for the device. A device must not |
| 753 | * be aliased due to topology in order to have its own IOMMU group. |
| 754 | * If we find an alias along the way that already belongs to a |
| 755 | * group, use it. |
| 756 | */ |
| 757 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) |
| 758 | return data.group; |
| 759 | |
| 760 | pdev = data.pdev; |
| 761 | |
| 762 | /* |
| 763 | * Continue upstream from the point of minimum IOMMU granularity |
| 764 | * due to aliases to the point where devices are protected from |
| 765 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing |
| 766 | * group, use it. |
| 767 | */ |
| 768 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { |
| 769 | if (!bus->self) |
| 770 | continue; |
| 771 | |
| 772 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) |
| 773 | break; |
| 774 | |
| 775 | pdev = bus->self; |
| 776 | |
| 777 | group = iommu_group_get(&pdev->dev); |
| 778 | if (group) |
| 779 | return group; |
| 780 | } |
| 781 | |
| 782 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 783 | * Look for existing groups on device aliases. If we alias another |
| 784 | * device or another device aliases us, use the same group. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 785 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 786 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
| 787 | if (group) |
| 788 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 789 | |
| 790 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 791 | * Look for existing groups on non-isolated functions on the same |
| 792 | * slot and aliases of those funcions, if any. No need to clear |
| 793 | * the search bitmap, the tested devfns are still valid. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 794 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 795 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
| 796 | if (group) |
| 797 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 798 | |
| 799 | /* No shared group found, allocate new */ |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 800 | group = iommu_group_alloc(); |
Dan Carpenter | 409e553 | 2015-06-10 13:59:27 +0300 | [diff] [blame] | 801 | if (IS_ERR(group)) |
| 802 | return NULL; |
| 803 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 804 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** |
| 808 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device |
| 809 | * @dev: target device |
| 810 | * |
| 811 | * This function is intended to be called by IOMMU drivers and extended to |
| 812 | * support common, bus-defined algorithms when determining or creating the |
| 813 | * IOMMU group for a device. On success, the caller will hold a reference |
| 814 | * to the returned IOMMU group, which will already include the provided |
| 815 | * device. The reference should be released with iommu_group_put(). |
| 816 | */ |
| 817 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) |
| 818 | { |
Joerg Roedel | 46c6b2b | 2015-10-21 23:51:36 +0200 | [diff] [blame] | 819 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 820 | struct iommu_group *group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 821 | int ret; |
| 822 | |
| 823 | group = iommu_group_get(dev); |
| 824 | if (group) |
| 825 | return group; |
| 826 | |
Joerg Roedel | 46c6b2b | 2015-10-21 23:51:36 +0200 | [diff] [blame] | 827 | group = ERR_PTR(-EINVAL); |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 828 | |
Joerg Roedel | 46c6b2b | 2015-10-21 23:51:36 +0200 | [diff] [blame] | 829 | if (ops && ops->device_group) |
| 830 | group = ops->device_group(dev); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 831 | |
| 832 | if (IS_ERR(group)) |
| 833 | return group; |
| 834 | |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 835 | /* |
| 836 | * Try to allocate a default domain - needs support from the |
| 837 | * IOMMU driver. |
| 838 | */ |
| 839 | if (!group->default_domain) { |
| 840 | group->default_domain = __iommu_domain_alloc(dev->bus, |
| 841 | IOMMU_DOMAIN_DMA); |
Joerg Roedel | eebb803 | 2016-04-04 15:47:48 +0200 | [diff] [blame] | 842 | if (!group->domain) |
| 843 | group->domain = group->default_domain; |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 846 | ret = iommu_group_add_device(group, dev); |
| 847 | if (ret) { |
| 848 | iommu_group_put(group); |
| 849 | return ERR_PTR(ret); |
| 850 | } |
| 851 | |
| 852 | return group; |
| 853 | } |
| 854 | |
Joerg Roedel | 6827ca8 | 2015-05-28 18:41:35 +0200 | [diff] [blame] | 855 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
| 856 | { |
| 857 | return group->default_domain; |
| 858 | } |
| 859 | |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 860 | static int add_iommu_group(struct device *dev, void *data) |
| 861 | { |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 862 | struct iommu_callback_data *cb = data; |
| 863 | const struct iommu_ops *ops = cb->ops; |
Joerg Roedel | 38667f1 | 2015-06-29 10:16:08 +0200 | [diff] [blame] | 864 | int ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 865 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 866 | if (!ops->add_device) |
Marek Szyprowski | 461bfb3f | 2014-11-19 11:15:31 +0000 | [diff] [blame] | 867 | return 0; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 868 | |
| 869 | WARN_ON(dev->iommu_group); |
| 870 | |
Joerg Roedel | 38667f1 | 2015-06-29 10:16:08 +0200 | [diff] [blame] | 871 | ret = ops->add_device(dev); |
| 872 | |
| 873 | /* |
| 874 | * We ignore -ENODEV errors for now, as they just mean that the |
| 875 | * device is not translated by an IOMMU. We still care about |
| 876 | * other errors and fail to initialize when they happen. |
| 877 | */ |
| 878 | if (ret == -ENODEV) |
| 879 | ret = 0; |
| 880 | |
| 881 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 882 | } |
| 883 | |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 884 | static int remove_iommu_group(struct device *dev, void *data) |
| 885 | { |
| 886 | struct iommu_callback_data *cb = data; |
| 887 | const struct iommu_ops *ops = cb->ops; |
| 888 | |
| 889 | if (ops->remove_device && dev->iommu_group) |
| 890 | ops->remove_device(dev); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 895 | static int iommu_bus_notifier(struct notifier_block *nb, |
| 896 | unsigned long action, void *data) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 897 | { |
| 898 | struct device *dev = data; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 899 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 900 | struct iommu_group *group; |
| 901 | unsigned long group_action = 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 902 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 903 | /* |
| 904 | * ADD/DEL call into iommu driver ops if provided, which may |
| 905 | * result in ADD/DEL notifiers to group->notifier |
| 906 | */ |
| 907 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
| 908 | if (ops->add_device) |
| 909 | return ops->add_device(dev); |
Joerg Roedel | 843cb6d | 2015-05-28 18:41:28 +0200 | [diff] [blame] | 910 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 911 | if (ops->remove_device && dev->iommu_group) { |
| 912 | ops->remove_device(dev); |
| 913 | return 0; |
| 914 | } |
| 915 | } |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 916 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 917 | /* |
| 918 | * Remaining BUS_NOTIFYs get filtered and republished to the |
| 919 | * group, if anyone is listening |
| 920 | */ |
| 921 | group = iommu_group_get(dev); |
| 922 | if (!group) |
| 923 | return 0; |
| 924 | |
| 925 | switch (action) { |
| 926 | case BUS_NOTIFY_BIND_DRIVER: |
| 927 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; |
| 928 | break; |
| 929 | case BUS_NOTIFY_BOUND_DRIVER: |
| 930 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; |
| 931 | break; |
| 932 | case BUS_NOTIFY_UNBIND_DRIVER: |
| 933 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; |
| 934 | break; |
| 935 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 936 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | if (group_action) |
| 941 | blocking_notifier_call_chain(&group->notifier, |
| 942 | group_action, dev); |
| 943 | |
| 944 | iommu_group_put(group); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 948 | 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] | 949 | { |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 950 | int err; |
| 951 | struct notifier_block *nb; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 952 | struct iommu_callback_data cb = { |
| 953 | .ops = ops, |
| 954 | }; |
| 955 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 956 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
| 957 | if (!nb) |
| 958 | return -ENOMEM; |
| 959 | |
| 960 | nb->notifier_call = iommu_bus_notifier; |
| 961 | |
| 962 | err = bus_register_notifier(bus, nb); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 963 | if (err) |
| 964 | goto out_free; |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 965 | |
| 966 | err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 967 | if (err) |
| 968 | goto out_err; |
| 969 | |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 970 | |
| 971 | return 0; |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 972 | |
| 973 | out_err: |
| 974 | /* Clean up */ |
| 975 | bus_for_each_dev(bus, NULL, &cb, remove_iommu_group); |
| 976 | bus_unregister_notifier(bus, nb); |
| 977 | |
| 978 | out_free: |
| 979 | kfree(nb); |
| 980 | |
| 981 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 984 | /** |
| 985 | * bus_set_iommu - set iommu-callbacks for the bus |
| 986 | * @bus: bus. |
| 987 | * @ops: the callbacks provided by the iommu-driver |
| 988 | * |
| 989 | * This function is called by an iommu driver to set the iommu methods |
| 990 | * used for a particular bus. Drivers for devices on that bus can use |
| 991 | * the iommu-api after these ops are registered. |
| 992 | * This special function is needed because IOMMUs are usually devices on |
| 993 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 994 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 995 | * afterwards. |
| 996 | */ |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 997 | 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] | 998 | { |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 999 | int err; |
| 1000 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1001 | if (bus->iommu_ops != NULL) |
| 1002 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1003 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1004 | bus->iommu_ops = ops; |
| 1005 | |
| 1006 | /* Do IOMMU specific setup for this bus-type */ |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1007 | err = iommu_bus_init(bus, ops); |
| 1008 | if (err) |
| 1009 | bus->iommu_ops = NULL; |
| 1010 | |
| 1011 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1012 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1013 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 1014 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1015 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1016 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1017 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1018 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1019 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1020 | |
Joerg Roedel | 3c0e0ca | 2014-09-03 18:47:25 +0200 | [diff] [blame] | 1021 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
| 1022 | { |
| 1023 | if (!bus->iommu_ops || !bus->iommu_ops->capable) |
| 1024 | return false; |
| 1025 | |
| 1026 | return bus->iommu_ops->capable(cap); |
| 1027 | } |
| 1028 | EXPORT_SYMBOL_GPL(iommu_capable); |
| 1029 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1030 | /** |
| 1031 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 1032 | * @domain: iommu domain |
| 1033 | * @handler: fault handler |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1034 | * @token: user data, will be passed back to the fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 1035 | * |
| 1036 | * This function should be used by IOMMU users which want to be notified |
| 1037 | * whenever an IOMMU fault happens. |
| 1038 | * |
| 1039 | * The fault handler itself should return 0 on success, and an appropriate |
| 1040 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1041 | */ |
| 1042 | void iommu_set_fault_handler(struct iommu_domain *domain, |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1043 | iommu_fault_handler_t handler, |
| 1044 | void *token) |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1045 | { |
| 1046 | BUG_ON(!domain); |
| 1047 | |
| 1048 | domain->handler = handler; |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1049 | domain->handler_token = token; |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1050 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 1051 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1052 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1053 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
| 1054 | unsigned type) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1055 | { |
| 1056 | struct iommu_domain *domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1057 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1058 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1059 | return NULL; |
| 1060 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1061 | domain = bus->iommu_ops->domain_alloc(type); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1062 | if (!domain) |
| 1063 | return NULL; |
| 1064 | |
Joerg Roedel | 8539c7c | 2015-03-26 13:43:05 +0100 | [diff] [blame] | 1065 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1066 | domain->type = type; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1067 | /* Assume all sizes by default; the driver may override this later */ |
| 1068 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1069 | |
Susheel Khiani | e66aa5b | 2015-08-25 17:25:42 +0530 | [diff] [blame] | 1070 | iommu_debug_domain_add(domain); |
| 1071 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1072 | return domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1073 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1074 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1075 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
| 1076 | { |
| 1077 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1078 | } |
| 1079 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 1080 | |
| 1081 | void iommu_domain_free(struct iommu_domain *domain) |
| 1082 | { |
Susheel Khiani | e66aa5b | 2015-08-25 17:25:42 +0530 | [diff] [blame] | 1083 | iommu_debug_domain_remove(domain); |
Joerg Roedel | 89be34a | 2015-03-26 13:43:19 +0100 | [diff] [blame] | 1084 | domain->ops->domain_free(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 1087 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1088 | static int __iommu_attach_device(struct iommu_domain *domain, |
| 1089 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1090 | { |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1091 | int ret; |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1092 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 1093 | return -ENODEV; |
| 1094 | |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1095 | ret = domain->ops->attach_dev(domain, dev); |
Mitchel Humpherys | 42296fb | 2015-06-23 16:29:16 -0700 | [diff] [blame] | 1096 | if (!ret) { |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1097 | trace_attach_device_to_domain(dev); |
Mitchel Humpherys | 42296fb | 2015-06-23 16:29:16 -0700 | [diff] [blame] | 1098 | iommu_debug_attach_device(domain, dev); |
| 1099 | } |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1100 | return ret; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1101 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1102 | |
| 1103 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 1104 | { |
| 1105 | struct iommu_group *group; |
| 1106 | int ret; |
| 1107 | |
| 1108 | group = iommu_group_get(dev); |
| 1109 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ |
| 1110 | if (group == NULL) |
| 1111 | return __iommu_attach_device(domain, dev); |
| 1112 | |
| 1113 | /* |
| 1114 | * We have a group - lock it to make sure the device-count doesn't |
| 1115 | * change while we are attaching |
| 1116 | */ |
| 1117 | mutex_lock(&group->mutex); |
| 1118 | ret = -EINVAL; |
| 1119 | if (iommu_group_device_count(group) != 1) |
| 1120 | goto out_unlock; |
| 1121 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1122 | ret = __iommu_attach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1123 | |
| 1124 | out_unlock: |
| 1125 | mutex_unlock(&group->mutex); |
| 1126 | iommu_group_put(group); |
| 1127 | |
| 1128 | return ret; |
| 1129 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1130 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 1131 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1132 | static void __iommu_detach_device(struct iommu_domain *domain, |
| 1133 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1134 | { |
Mitchel Humpherys | 42296fb | 2015-06-23 16:29:16 -0700 | [diff] [blame] | 1135 | iommu_debug_detach_device(domain, dev); |
| 1136 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1137 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 1138 | return; |
| 1139 | |
| 1140 | domain->ops->detach_dev(domain, dev); |
Shuah Khan | 6998063 | 2013-08-15 11:59:27 -0600 | [diff] [blame] | 1141 | trace_detach_device_from_domain(dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1142 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1143 | |
| 1144 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 1145 | { |
| 1146 | struct iommu_group *group; |
| 1147 | |
| 1148 | group = iommu_group_get(dev); |
| 1149 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ |
| 1150 | if (group == NULL) |
| 1151 | return __iommu_detach_device(domain, dev); |
| 1152 | |
| 1153 | mutex_lock(&group->mutex); |
| 1154 | if (iommu_group_device_count(group) != 1) { |
| 1155 | WARN_ON(1); |
| 1156 | goto out_unlock; |
| 1157 | } |
| 1158 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1159 | __iommu_detach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1160 | |
| 1161 | out_unlock: |
| 1162 | mutex_unlock(&group->mutex); |
| 1163 | iommu_group_put(group); |
| 1164 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1165 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 1166 | |
Joerg Roedel | 2c1296d | 2015-05-28 18:41:32 +0200 | [diff] [blame] | 1167 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
| 1168 | { |
| 1169 | struct iommu_domain *domain; |
| 1170 | struct iommu_group *group; |
| 1171 | |
| 1172 | group = iommu_group_get(dev); |
| 1173 | /* FIXME: Remove this when groups a mandatory for iommu drivers */ |
| 1174 | if (group == NULL) |
| 1175 | return NULL; |
| 1176 | |
| 1177 | domain = group->domain; |
| 1178 | |
| 1179 | iommu_group_put(group); |
| 1180 | |
| 1181 | return domain; |
| 1182 | } |
| 1183 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); |
| 1184 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1185 | /* |
| 1186 | * IOMMU groups are really the natrual working unit of the IOMMU, but |
| 1187 | * the IOMMU API works on domains and devices. Bridge that gap by |
| 1188 | * iterating over the devices in a group. Ideally we'd have a single |
| 1189 | * device which represents the requestor ID of the group, but we also |
| 1190 | * allow IOMMU drivers to create policy defined minimum sets, where |
| 1191 | * the physical hardware may be able to distiguish members, but we |
| 1192 | * wish to group them at a higher level (ex. untrusted multi-function |
| 1193 | * PCI devices). Thus we attach each device. |
| 1194 | */ |
| 1195 | static int iommu_group_do_attach_device(struct device *dev, void *data) |
| 1196 | { |
| 1197 | struct iommu_domain *domain = data; |
| 1198 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1199 | return __iommu_attach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1200 | } |
| 1201 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1202 | static int __iommu_attach_group(struct iommu_domain *domain, |
| 1203 | struct iommu_group *group) |
| 1204 | { |
| 1205 | int ret; |
| 1206 | |
| 1207 | if (group->default_domain && group->domain != group->default_domain) |
| 1208 | return -EBUSY; |
| 1209 | |
| 1210 | ret = __iommu_group_for_each_dev(group, domain, |
| 1211 | iommu_group_do_attach_device); |
| 1212 | if (ret == 0) |
| 1213 | group->domain = domain; |
| 1214 | |
| 1215 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1219 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1220 | int ret; |
| 1221 | |
| 1222 | mutex_lock(&group->mutex); |
| 1223 | ret = __iommu_attach_group(domain, group); |
| 1224 | mutex_unlock(&group->mutex); |
| 1225 | |
| 1226 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1227 | } |
| 1228 | EXPORT_SYMBOL_GPL(iommu_attach_group); |
| 1229 | |
| 1230 | static int iommu_group_do_detach_device(struct device *dev, void *data) |
| 1231 | { |
| 1232 | struct iommu_domain *domain = data; |
| 1233 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1234 | __iommu_detach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1239 | static void __iommu_detach_group(struct iommu_domain *domain, |
| 1240 | struct iommu_group *group) |
| 1241 | { |
| 1242 | int ret; |
| 1243 | |
| 1244 | if (!group->default_domain) { |
| 1245 | __iommu_group_for_each_dev(group, domain, |
| 1246 | iommu_group_do_detach_device); |
| 1247 | group->domain = NULL; |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | if (group->domain == group->default_domain) |
| 1252 | return; |
| 1253 | |
| 1254 | /* Detach by re-attaching to the default domain */ |
| 1255 | ret = __iommu_group_for_each_dev(group, group->default_domain, |
| 1256 | iommu_group_do_attach_device); |
| 1257 | if (ret != 0) |
| 1258 | WARN_ON(1); |
| 1259 | else |
| 1260 | group->domain = group->default_domain; |
| 1261 | } |
| 1262 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1263 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1264 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1265 | mutex_lock(&group->mutex); |
| 1266 | __iommu_detach_group(domain, group); |
| 1267 | mutex_unlock(&group->mutex); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1268 | } |
| 1269 | EXPORT_SYMBOL_GPL(iommu_detach_group); |
| 1270 | |
Varun Sethi | bb5547ac | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 1271 | 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] | 1272 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1273 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 1274 | return 0; |
| 1275 | |
| 1276 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1277 | } |
| 1278 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 1279 | |
Mitchel Humpherys | 36b8c32 | 2015-07-06 15:24:22 -0700 | [diff] [blame] | 1280 | phys_addr_t iommu_iova_to_phys_hard(struct iommu_domain *domain, |
| 1281 | dma_addr_t iova) |
| 1282 | { |
| 1283 | if (unlikely(domain->ops->iova_to_phys_hard == NULL)) |
| 1284 | return 0; |
| 1285 | |
| 1286 | return domain->ops->iova_to_phys_hard(domain, iova); |
| 1287 | } |
| 1288 | |
Liam Mark | 5bb6422 | 2016-12-09 14:36:07 -0800 | [diff] [blame] | 1289 | bool iommu_is_iova_coherent(struct iommu_domain *domain, dma_addr_t iova) |
| 1290 | { |
| 1291 | if (unlikely(domain->ops->is_iova_coherent == NULL)) |
| 1292 | return 0; |
| 1293 | |
| 1294 | return domain->ops->is_iova_coherent(domain, iova); |
| 1295 | } |
| 1296 | |
Mitchel Humpherys | fad2592 | 2015-05-01 17:13:21 -0700 | [diff] [blame] | 1297 | size_t iommu_pgsize(unsigned long pgsize_bitmap, |
| 1298 | unsigned long addr_merge, size_t size) |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1299 | { |
| 1300 | unsigned int pgsize_idx; |
| 1301 | size_t pgsize; |
| 1302 | |
| 1303 | /* Max page size that still fits into 'size' */ |
| 1304 | pgsize_idx = __fls(size); |
| 1305 | |
| 1306 | /* need to consider alignment requirements ? */ |
| 1307 | if (likely(addr_merge)) { |
| 1308 | /* Max page size allowed by address */ |
| 1309 | unsigned int align_pgsize_idx = __ffs(addr_merge); |
| 1310 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); |
| 1311 | } |
| 1312 | |
| 1313 | /* build a mask of acceptable page sizes */ |
| 1314 | pgsize = (1UL << (pgsize_idx + 1)) - 1; |
| 1315 | |
| 1316 | /* throw away page sizes not supported by the hardware */ |
Mitchel Humpherys | fad2592 | 2015-05-01 17:13:21 -0700 | [diff] [blame] | 1317 | pgsize &= pgsize_bitmap; |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1318 | |
| 1319 | /* make sure we're still sane */ |
Mitchel Humpherys | 76e77fb | 2015-05-12 09:44:32 -0700 | [diff] [blame] | 1320 | if (!pgsize) { |
| 1321 | pr_err("invalid pgsize/addr/size! 0x%lx 0x%lx 0x%zx\n", |
| 1322 | pgsize_bitmap, addr_merge, size); |
| 1323 | BUG(); |
| 1324 | } |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1325 | |
| 1326 | /* pick the biggest page */ |
| 1327 | pgsize_idx = __fls(pgsize); |
| 1328 | pgsize = 1UL << pgsize_idx; |
| 1329 | |
| 1330 | return pgsize; |
| 1331 | } |
| 1332 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1333 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1334 | phys_addr_t paddr, size_t size, int prot) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1335 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1336 | unsigned long orig_iova = iova; |
| 1337 | unsigned int min_pagesz; |
| 1338 | size_t orig_size = size; |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1339 | phys_addr_t orig_paddr = paddr; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1340 | int ret = 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1341 | |
Joerg Roedel | 9db4ad9 | 2014-08-19 00:19:26 +0200 | [diff] [blame] | 1342 | if (unlikely(domain->ops->map == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1343 | domain->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1344 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1345 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1346 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
| 1347 | return -EINVAL; |
| 1348 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1349 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1350 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1351 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1352 | /* |
| 1353 | * both the virtual address and the physical one, as well as |
| 1354 | * the size of the mapping, must be aligned (at least) to the |
| 1355 | * size of the smallest page supported by the hardware |
| 1356 | */ |
| 1357 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1358 | 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] | 1359 | iova, &paddr, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1360 | return -EINVAL; |
| 1361 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1362 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1363 | 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] | 1364 | |
| 1365 | while (size) { |
Mitchel Humpherys | fad2592 | 2015-05-01 17:13:21 -0700 | [diff] [blame] | 1366 | size_t pgsize = iommu_pgsize(domain->pgsize_bitmap, |
| 1367 | iova | paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1368 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1369 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1370 | iova, &paddr, pgsize); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1371 | |
| 1372 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); |
| 1373 | if (ret) |
| 1374 | break; |
| 1375 | |
| 1376 | iova += pgsize; |
| 1377 | paddr += pgsize; |
| 1378 | size -= pgsize; |
| 1379 | } |
| 1380 | |
| 1381 | /* unroll mapping in case something went wrong */ |
| 1382 | if (ret) |
| 1383 | iommu_unmap(domain, orig_iova, orig_size - size); |
Shuah Khan | e0be7c8 | 2013-08-15 11:59:28 -0600 | [diff] [blame] | 1384 | else |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1385 | trace_map(orig_iova, orig_paddr, orig_size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1386 | |
| 1387 | return ret; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1388 | } |
| 1389 | EXPORT_SYMBOL_GPL(iommu_map); |
| 1390 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1391 | 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] | 1392 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1393 | size_t unmapped_page, unmapped = 0; |
| 1394 | unsigned int min_pagesz; |
Shuah Khan | 6fd492f | 2015-01-16 16:47:19 -0700 | [diff] [blame] | 1395 | unsigned long orig_iova = iova; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1396 | |
Joerg Roedel | 5788651 | 2013-01-29 13:41:09 +0100 | [diff] [blame] | 1397 | if (unlikely(domain->ops->unmap == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1398 | domain->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1399 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1400 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1401 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
| 1402 | return -EINVAL; |
| 1403 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1404 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1405 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1406 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1407 | /* |
| 1408 | * The virtual address, as well as the size of the mapping, must be |
| 1409 | * aligned (at least) to the size of the smallest page supported |
| 1410 | * by the hardware |
| 1411 | */ |
| 1412 | if (!IS_ALIGNED(iova | size, min_pagesz)) { |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1413 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
| 1414 | iova, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1415 | return -EINVAL; |
| 1416 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1417 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1418 | 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] | 1419 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1420 | /* |
| 1421 | * Keep iterating until we either unmap 'size' bytes (or more) |
| 1422 | * or we hit an area that isn't mapped. |
| 1423 | */ |
| 1424 | while (unmapped < size) { |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 1425 | size_t left = size - unmapped; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1426 | |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 1427 | unmapped_page = domain->ops->unmap(domain, iova, left); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1428 | if (!unmapped_page) |
| 1429 | break; |
| 1430 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1431 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
| 1432 | iova, unmapped_page); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1433 | |
| 1434 | iova += unmapped_page; |
| 1435 | unmapped += unmapped_page; |
| 1436 | } |
| 1437 | |
Shuah Khan | db8614d | 2015-01-16 20:53:17 -0700 | [diff] [blame] | 1438 | trace_unmap(orig_iova, size, unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1439 | return unmapped; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1440 | } |
| 1441 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1442 | |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1443 | size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
| 1444 | struct scatterlist *sg, unsigned int nents, int prot) |
| 1445 | { |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1446 | struct scatterlist *s; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1447 | size_t mapped = 0; |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1448 | unsigned int i, min_pagesz; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1449 | int ret; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1450 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1451 | if (unlikely(domain->pgsize_bitmap == 0UL)) |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1452 | return 0; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1453 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1454 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1455 | |
| 1456 | for_each_sg(sg, s, nents, i) { |
Dan Williams | 3e6110f | 2015-12-15 12:54:06 -0800 | [diff] [blame] | 1457 | phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset; |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 1458 | |
| 1459 | /* |
| 1460 | * We are mapping on IOMMU page boundaries, so offset within |
| 1461 | * the page must be 0. However, the IOMMU may support pages |
| 1462 | * smaller than PAGE_SIZE, so s->offset may still represent |
| 1463 | * an offset of that boundary within the CPU page. |
| 1464 | */ |
| 1465 | if (!IS_ALIGNED(s->offset, min_pagesz)) |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1466 | goto out_err; |
| 1467 | |
| 1468 | ret = iommu_map(domain, iova + mapped, phys, s->length, prot); |
| 1469 | if (ret) |
| 1470 | goto out_err; |
| 1471 | |
| 1472 | mapped += s->length; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | return mapped; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 1476 | |
| 1477 | out_err: |
| 1478 | /* undo mappings already done */ |
| 1479 | iommu_unmap(domain, iova, mapped); |
| 1480 | |
| 1481 | return 0; |
| 1482 | |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 1483 | } |
| 1484 | EXPORT_SYMBOL_GPL(default_iommu_map_sg); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1485 | |
| 1486 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1487 | phys_addr_t paddr, u64 size, int prot) |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1488 | { |
| 1489 | if (unlikely(domain->ops->domain_window_enable == NULL)) |
| 1490 | return -ENODEV; |
| 1491 | |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1492 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
| 1493 | prot); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1494 | } |
| 1495 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); |
| 1496 | |
| 1497 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) |
| 1498 | { |
| 1499 | if (unlikely(domain->ops->domain_window_disable == NULL)) |
| 1500 | return; |
| 1501 | |
| 1502 | return domain->ops->domain_window_disable(domain, wnd_nr); |
| 1503 | } |
| 1504 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); |
| 1505 | |
Mitchel Humpherys | c75ae49 | 2015-07-15 18:27:36 -0700 | [diff] [blame] | 1506 | struct dentry *iommu_debugfs_top; |
| 1507 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1508 | static int __init iommu_init(void) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1509 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1510 | iommu_group_kset = kset_create_and_add("iommu_groups", |
| 1511 | NULL, kernel_kobj); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1512 | BUG_ON(!iommu_group_kset); |
| 1513 | |
Mitchel Humpherys | c75ae49 | 2015-07-15 18:27:36 -0700 | [diff] [blame] | 1514 | iommu_debugfs_top = debugfs_create_dir("iommu", NULL); |
| 1515 | if (!iommu_debugfs_top) { |
| 1516 | pr_err("Couldn't create iommu debugfs directory\n"); |
| 1517 | return -ENODEV; |
| 1518 | } |
| 1519 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1520 | return 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1521 | } |
Marek Szyprowski | d7ef999 | 2015-05-19 15:20:23 +0200 | [diff] [blame] | 1522 | core_initcall(iommu_init); |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1523 | |
| 1524 | int iommu_domain_get_attr(struct iommu_domain *domain, |
| 1525 | enum iommu_attr attr, void *data) |
| 1526 | { |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1527 | struct iommu_domain_geometry *geometry; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1528 | bool *paging; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1529 | int ret = 0; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1530 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1531 | |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1532 | switch (attr) { |
| 1533 | case DOMAIN_ATTR_GEOMETRY: |
| 1534 | geometry = data; |
| 1535 | *geometry = domain->geometry; |
| 1536 | |
| 1537 | break; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1538 | case DOMAIN_ATTR_PAGING: |
| 1539 | paging = data; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1540 | *paging = (domain->pgsize_bitmap != 0UL); |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1541 | break; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1542 | case DOMAIN_ATTR_WINDOWS: |
| 1543 | count = data; |
| 1544 | |
| 1545 | if (domain->ops->domain_get_windows != NULL) |
| 1546 | *count = domain->ops->domain_get_windows(domain); |
| 1547 | else |
| 1548 | ret = -ENODEV; |
| 1549 | |
| 1550 | break; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1551 | default: |
| 1552 | if (!domain->ops->domain_get_attr) |
| 1553 | return -EINVAL; |
| 1554 | |
| 1555 | ret = domain->ops->domain_get_attr(domain, attr, data); |
| 1556 | } |
| 1557 | |
| 1558 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1559 | } |
| 1560 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); |
| 1561 | |
| 1562 | int iommu_domain_set_attr(struct iommu_domain *domain, |
| 1563 | enum iommu_attr attr, void *data) |
| 1564 | { |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1565 | int ret = 0; |
| 1566 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1567 | |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1568 | switch (attr) { |
| 1569 | case DOMAIN_ATTR_WINDOWS: |
| 1570 | count = data; |
| 1571 | |
| 1572 | if (domain->ops->domain_set_windows != NULL) |
| 1573 | ret = domain->ops->domain_set_windows(domain, *count); |
| 1574 | else |
| 1575 | ret = -ENODEV; |
| 1576 | |
| 1577 | break; |
| 1578 | default: |
| 1579 | if (domain->ops->domain_set_attr == NULL) |
| 1580 | return -EINVAL; |
| 1581 | |
| 1582 | ret = domain->ops->domain_set_attr(domain, attr, data); |
| 1583 | } |
| 1584 | |
| 1585 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1586 | } |
| 1587 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1588 | |
Mitchel Humpherys | 8488df2 | 2015-07-09 16:59:02 -0700 | [diff] [blame] | 1589 | /** |
| 1590 | * iommu_trigger_fault() - trigger an IOMMU fault |
| 1591 | * @domain: iommu domain |
| 1592 | * |
| 1593 | * Triggers a fault on the device to which this domain is attached. |
| 1594 | * |
| 1595 | * This function should only be used for debugging purposes, for obvious |
| 1596 | * reasons. |
| 1597 | */ |
| 1598 | void iommu_trigger_fault(struct iommu_domain *domain, unsigned long flags) |
| 1599 | { |
| 1600 | if (domain->ops->trigger_fault) |
| 1601 | domain->ops->trigger_fault(domain, flags); |
| 1602 | } |
| 1603 | |
Mitchel Humpherys | 3ede5d9 | 2015-08-21 14:06:14 -0700 | [diff] [blame] | 1604 | /** |
| 1605 | * iommu_reg_read() - read an IOMMU register |
| 1606 | * |
| 1607 | * Reads the IOMMU register at the given offset. |
| 1608 | */ |
| 1609 | unsigned long iommu_reg_read(struct iommu_domain *domain, unsigned long offset) |
| 1610 | { |
| 1611 | if (domain->ops->reg_read) |
| 1612 | return domain->ops->reg_read(domain, offset); |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | /** |
| 1617 | * iommu_reg_write() - write an IOMMU register |
| 1618 | * |
| 1619 | * Writes the given value to the IOMMU register at the given offset. |
| 1620 | */ |
| 1621 | void iommu_reg_write(struct iommu_domain *domain, unsigned long offset, |
| 1622 | unsigned long val) |
| 1623 | { |
| 1624 | if (domain->ops->reg_write) |
| 1625 | domain->ops->reg_write(domain, offset, val); |
| 1626 | } |
| 1627 | |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 1628 | void iommu_get_dm_regions(struct device *dev, struct list_head *list) |
| 1629 | { |
| 1630 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 1631 | |
| 1632 | if (ops && ops->get_dm_regions) |
| 1633 | ops->get_dm_regions(dev, list); |
| 1634 | } |
| 1635 | |
| 1636 | void iommu_put_dm_regions(struct device *dev, struct list_head *list) |
| 1637 | { |
| 1638 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 1639 | |
| 1640 | if (ops && ops->put_dm_regions) |
| 1641 | ops->put_dm_regions(dev, list); |
| 1642 | } |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 1643 | |
| 1644 | /* Request that a device is direct mapped by the IOMMU */ |
| 1645 | int iommu_request_dm_for_dev(struct device *dev) |
| 1646 | { |
| 1647 | struct iommu_domain *dm_domain; |
| 1648 | struct iommu_group *group; |
| 1649 | int ret; |
| 1650 | |
| 1651 | /* Device must already be in a group before calling this function */ |
| 1652 | group = iommu_group_get_for_dev(dev); |
Dan Carpenter | 409e553 | 2015-06-10 13:59:27 +0300 | [diff] [blame] | 1653 | if (IS_ERR(group)) |
| 1654 | return PTR_ERR(group); |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 1655 | |
| 1656 | mutex_lock(&group->mutex); |
| 1657 | |
| 1658 | /* Check if the default domain is already direct mapped */ |
| 1659 | ret = 0; |
| 1660 | if (group->default_domain && |
| 1661 | group->default_domain->type == IOMMU_DOMAIN_IDENTITY) |
| 1662 | goto out; |
| 1663 | |
| 1664 | /* Don't change mappings of existing devices */ |
| 1665 | ret = -EBUSY; |
| 1666 | if (iommu_group_device_count(group) != 1) |
| 1667 | goto out; |
| 1668 | |
| 1669 | /* Allocate a direct mapped domain */ |
| 1670 | ret = -ENOMEM; |
| 1671 | dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); |
| 1672 | if (!dm_domain) |
| 1673 | goto out; |
| 1674 | |
| 1675 | /* Attach the device to the domain */ |
| 1676 | ret = __iommu_attach_group(dm_domain, group); |
| 1677 | if (ret) { |
| 1678 | iommu_domain_free(dm_domain); |
| 1679 | goto out; |
| 1680 | } |
| 1681 | |
| 1682 | /* Make the direct mapped domain the default for this group */ |
| 1683 | if (group->default_domain) |
| 1684 | iommu_domain_free(group->default_domain); |
| 1685 | group->default_domain = dm_domain; |
| 1686 | |
| 1687 | pr_info("Using direct mapping for device %s\n", dev_name(dev)); |
| 1688 | |
| 1689 | ret = 0; |
| 1690 | out: |
| 1691 | mutex_unlock(&group->mutex); |
| 1692 | iommu_group_put(group); |
| 1693 | |
| 1694 | return ret; |
| 1695 | } |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 1696 | |
| 1697 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
| 1698 | const struct iommu_ops *ops) |
| 1699 | { |
| 1700 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1701 | |
| 1702 | if (fwspec) |
| 1703 | return ops == fwspec->ops ? 0 : -EINVAL; |
| 1704 | |
| 1705 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); |
| 1706 | if (!fwspec) |
| 1707 | return -ENOMEM; |
| 1708 | |
| 1709 | of_node_get(to_of_node(iommu_fwnode)); |
| 1710 | fwspec->iommu_fwnode = iommu_fwnode; |
| 1711 | fwspec->ops = ops; |
| 1712 | dev->iommu_fwspec = fwspec; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); |
| 1716 | |
| 1717 | void iommu_fwspec_free(struct device *dev) |
| 1718 | { |
| 1719 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1720 | |
| 1721 | if (fwspec) { |
| 1722 | fwnode_handle_put(fwspec->iommu_fwnode); |
| 1723 | kfree(fwspec); |
| 1724 | dev->iommu_fwspec = NULL; |
| 1725 | } |
| 1726 | } |
| 1727 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); |
| 1728 | |
| 1729 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) |
| 1730 | { |
| 1731 | struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
| 1732 | size_t size; |
| 1733 | int i; |
| 1734 | |
| 1735 | if (!fwspec) |
| 1736 | return -EINVAL; |
| 1737 | |
| 1738 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); |
| 1739 | if (size > sizeof(*fwspec)) { |
| 1740 | fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL); |
| 1741 | if (!fwspec) |
| 1742 | return -ENOMEM; |
| 1743 | } |
| 1744 | |
| 1745 | for (i = 0; i < num_ids; i++) |
| 1746 | fwspec->ids[fwspec->num_ids + i] = ids[i]; |
| 1747 | |
| 1748 | fwspec->num_ids += num_ids; |
| 1749 | dev->iommu_fwspec = fwspec; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); |