Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
| 3 | * Author: Joerg Roedel <joerg.roedel@amd.com> |
| 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 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 19 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 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> |
Shuah Khan | 7f6db17 | 2013-08-15 11:59:23 -0600 | [diff] [blame] | 33 | #include <trace/events/iommu.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 34 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 35 | static struct kset *iommu_group_kset; |
| 36 | static struct ida iommu_group_ida; |
| 37 | static struct mutex iommu_group_mutex; |
| 38 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 39 | struct iommu_callback_data { |
| 40 | const struct iommu_ops *ops; |
| 41 | }; |
| 42 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 43 | struct iommu_group { |
| 44 | struct kobject kobj; |
| 45 | struct kobject *devices_kobj; |
| 46 | struct list_head devices; |
| 47 | struct mutex mutex; |
| 48 | struct blocking_notifier_head notifier; |
| 49 | void *iommu_data; |
| 50 | void (*iommu_data_release)(void *iommu_data); |
| 51 | char *name; |
| 52 | int id; |
| 53 | }; |
| 54 | |
| 55 | struct iommu_device { |
| 56 | struct list_head list; |
| 57 | struct device *dev; |
| 58 | char *name; |
| 59 | }; |
| 60 | |
| 61 | struct iommu_group_attribute { |
| 62 | struct attribute attr; |
| 63 | ssize_t (*show)(struct iommu_group *group, char *buf); |
| 64 | ssize_t (*store)(struct iommu_group *group, |
| 65 | const char *buf, size_t count); |
| 66 | }; |
| 67 | |
| 68 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
| 69 | struct iommu_group_attribute iommu_group_attr_##_name = \ |
| 70 | __ATTR(_name, _mode, _show, _store) |
| 71 | |
| 72 | #define to_iommu_group_attr(_attr) \ |
| 73 | container_of(_attr, struct iommu_group_attribute, attr) |
| 74 | #define to_iommu_group(_kobj) \ |
| 75 | container_of(_kobj, struct iommu_group, kobj) |
| 76 | |
| 77 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
| 78 | struct attribute *__attr, char *buf) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 79 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 80 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 81 | struct iommu_group *group = to_iommu_group(kobj); |
| 82 | ssize_t ret = -EIO; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 83 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 84 | if (attr->show) |
| 85 | ret = attr->show(group, buf); |
| 86 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 87 | } |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 88 | |
| 89 | static ssize_t iommu_group_attr_store(struct kobject *kobj, |
| 90 | struct attribute *__attr, |
| 91 | const char *buf, size_t count) |
| 92 | { |
| 93 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 94 | struct iommu_group *group = to_iommu_group(kobj); |
| 95 | ssize_t ret = -EIO; |
| 96 | |
| 97 | if (attr->store) |
| 98 | ret = attr->store(group, buf, count); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
| 103 | .show = iommu_group_attr_show, |
| 104 | .store = iommu_group_attr_store, |
| 105 | }; |
| 106 | |
| 107 | static int iommu_group_create_file(struct iommu_group *group, |
| 108 | struct iommu_group_attribute *attr) |
| 109 | { |
| 110 | return sysfs_create_file(&group->kobj, &attr->attr); |
| 111 | } |
| 112 | |
| 113 | static void iommu_group_remove_file(struct iommu_group *group, |
| 114 | struct iommu_group_attribute *attr) |
| 115 | { |
| 116 | sysfs_remove_file(&group->kobj, &attr->attr); |
| 117 | } |
| 118 | |
| 119 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) |
| 120 | { |
| 121 | return sprintf(buf, "%s\n", group->name); |
| 122 | } |
| 123 | |
| 124 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
| 125 | |
| 126 | static void iommu_group_release(struct kobject *kobj) |
| 127 | { |
| 128 | struct iommu_group *group = to_iommu_group(kobj); |
| 129 | |
| 130 | if (group->iommu_data_release) |
| 131 | group->iommu_data_release(group->iommu_data); |
| 132 | |
| 133 | mutex_lock(&iommu_group_mutex); |
| 134 | ida_remove(&iommu_group_ida, group->id); |
| 135 | mutex_unlock(&iommu_group_mutex); |
| 136 | |
| 137 | kfree(group->name); |
| 138 | kfree(group); |
| 139 | } |
| 140 | |
| 141 | static struct kobj_type iommu_group_ktype = { |
| 142 | .sysfs_ops = &iommu_group_sysfs_ops, |
| 143 | .release = iommu_group_release, |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * iommu_group_alloc - Allocate a new group |
| 148 | * @name: Optional name to associate with group, visible in sysfs |
| 149 | * |
| 150 | * This function is called by an iommu driver to allocate a new iommu |
| 151 | * group. The iommu group represents the minimum granularity of the iommu. |
| 152 | * Upon successful return, the caller holds a reference to the supplied |
| 153 | * group in order to hold the group until devices are added. Use |
| 154 | * iommu_group_put() to release this extra reference count, allowing the |
| 155 | * group to be automatically reclaimed once it has no devices or external |
| 156 | * references. |
| 157 | */ |
| 158 | struct iommu_group *iommu_group_alloc(void) |
| 159 | { |
| 160 | struct iommu_group *group; |
| 161 | int ret; |
| 162 | |
| 163 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 164 | if (!group) |
| 165 | return ERR_PTR(-ENOMEM); |
| 166 | |
| 167 | group->kobj.kset = iommu_group_kset; |
| 168 | mutex_init(&group->mutex); |
| 169 | INIT_LIST_HEAD(&group->devices); |
| 170 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); |
| 171 | |
| 172 | mutex_lock(&iommu_group_mutex); |
| 173 | |
| 174 | again: |
| 175 | if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) { |
| 176 | kfree(group); |
| 177 | mutex_unlock(&iommu_group_mutex); |
| 178 | return ERR_PTR(-ENOMEM); |
| 179 | } |
| 180 | |
| 181 | if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id)) |
| 182 | goto again; |
| 183 | |
| 184 | mutex_unlock(&iommu_group_mutex); |
| 185 | |
| 186 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
| 187 | NULL, "%d", group->id); |
| 188 | if (ret) { |
| 189 | mutex_lock(&iommu_group_mutex); |
| 190 | ida_remove(&iommu_group_ida, group->id); |
| 191 | mutex_unlock(&iommu_group_mutex); |
| 192 | kfree(group); |
| 193 | return ERR_PTR(ret); |
| 194 | } |
| 195 | |
| 196 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); |
| 197 | if (!group->devices_kobj) { |
| 198 | kobject_put(&group->kobj); /* triggers .release & free */ |
| 199 | return ERR_PTR(-ENOMEM); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * The devices_kobj holds a reference on the group kobject, so |
| 204 | * as long as that exists so will the group. We can therefore |
| 205 | * use the devices_kobj for reference counting. |
| 206 | */ |
| 207 | kobject_put(&group->kobj); |
| 208 | |
| 209 | return group; |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(iommu_group_alloc); |
| 212 | |
Alexey Kardashevskiy | aa16bea | 2013-03-25 10:23:49 +1100 | [diff] [blame] | 213 | struct iommu_group *iommu_group_get_by_id(int id) |
| 214 | { |
| 215 | struct kobject *group_kobj; |
| 216 | struct iommu_group *group; |
| 217 | const char *name; |
| 218 | |
| 219 | if (!iommu_group_kset) |
| 220 | return NULL; |
| 221 | |
| 222 | name = kasprintf(GFP_KERNEL, "%d", id); |
| 223 | if (!name) |
| 224 | return NULL; |
| 225 | |
| 226 | group_kobj = kset_find_obj(iommu_group_kset, name); |
| 227 | kfree(name); |
| 228 | |
| 229 | if (!group_kobj) |
| 230 | return NULL; |
| 231 | |
| 232 | group = container_of(group_kobj, struct iommu_group, kobj); |
| 233 | BUG_ON(group->id != id); |
| 234 | |
| 235 | kobject_get(group->devices_kobj); |
| 236 | kobject_put(&group->kobj); |
| 237 | |
| 238 | return group; |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); |
| 241 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 242 | /** |
| 243 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group |
| 244 | * @group: the group |
| 245 | * |
| 246 | * iommu drivers can store data in the group for use when doing iommu |
| 247 | * operations. This function provides a way to retrieve it. Caller |
| 248 | * should hold a group reference. |
| 249 | */ |
| 250 | void *iommu_group_get_iommudata(struct iommu_group *group) |
| 251 | { |
| 252 | return group->iommu_data; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); |
| 255 | |
| 256 | /** |
| 257 | * iommu_group_set_iommudata - set iommu_data for a group |
| 258 | * @group: the group |
| 259 | * @iommu_data: new data |
| 260 | * @release: release function for iommu_data |
| 261 | * |
| 262 | * iommu drivers can store data in the group for use when doing iommu |
| 263 | * operations. This function provides a way to set the data after |
| 264 | * the group has been allocated. Caller should hold a group reference. |
| 265 | */ |
| 266 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, |
| 267 | void (*release)(void *iommu_data)) |
| 268 | { |
| 269 | group->iommu_data = iommu_data; |
| 270 | group->iommu_data_release = release; |
| 271 | } |
| 272 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); |
| 273 | |
| 274 | /** |
| 275 | * iommu_group_set_name - set name for a group |
| 276 | * @group: the group |
| 277 | * @name: name |
| 278 | * |
| 279 | * Allow iommu driver to set a name for a group. When set it will |
| 280 | * appear in a name attribute file under the group in sysfs. |
| 281 | */ |
| 282 | int iommu_group_set_name(struct iommu_group *group, const char *name) |
| 283 | { |
| 284 | int ret; |
| 285 | |
| 286 | if (group->name) { |
| 287 | iommu_group_remove_file(group, &iommu_group_attr_name); |
| 288 | kfree(group->name); |
| 289 | group->name = NULL; |
| 290 | if (!name) |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | group->name = kstrdup(name, GFP_KERNEL); |
| 295 | if (!group->name) |
| 296 | return -ENOMEM; |
| 297 | |
| 298 | ret = iommu_group_create_file(group, &iommu_group_attr_name); |
| 299 | if (ret) { |
| 300 | kfree(group->name); |
| 301 | group->name = NULL; |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
| 308 | |
| 309 | /** |
| 310 | * iommu_group_add_device - add a device to an iommu group |
| 311 | * @group: the group into which to add the device (reference should be held) |
| 312 | * @dev: the device |
| 313 | * |
| 314 | * This function is called by an iommu driver to add a device into a |
| 315 | * group. Adding a device increments the group reference count. |
| 316 | */ |
| 317 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) |
| 318 | { |
| 319 | int ret, i = 0; |
| 320 | struct iommu_device *device; |
| 321 | |
| 322 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 323 | if (!device) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | device->dev = dev; |
| 327 | |
| 328 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
| 329 | if (ret) { |
| 330 | kfree(device); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); |
| 335 | rename: |
| 336 | if (!device->name) { |
| 337 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 338 | kfree(device); |
| 339 | return -ENOMEM; |
| 340 | } |
| 341 | |
| 342 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
| 343 | &dev->kobj, device->name); |
| 344 | if (ret) { |
| 345 | kfree(device->name); |
| 346 | if (ret == -EEXIST && i >= 0) { |
| 347 | /* |
| 348 | * Account for the slim chance of collision |
| 349 | * and append an instance to the name. |
| 350 | */ |
| 351 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
| 352 | kobject_name(&dev->kobj), i++); |
| 353 | goto rename; |
| 354 | } |
| 355 | |
| 356 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 357 | kfree(device); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | kobject_get(group->devices_kobj); |
| 362 | |
| 363 | dev->iommu_group = group; |
| 364 | |
| 365 | mutex_lock(&group->mutex); |
| 366 | list_add_tail(&device->list, &group->devices); |
| 367 | mutex_unlock(&group->mutex); |
| 368 | |
| 369 | /* Notify any listeners about change to group. */ |
| 370 | blocking_notifier_call_chain(&group->notifier, |
| 371 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); |
Shuah Khan | d1cf7e8 | 2013-08-15 11:59:24 -0600 | [diff] [blame] | 372 | |
| 373 | trace_add_device_to_group(group->id, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
| 377 | |
| 378 | /** |
| 379 | * iommu_group_remove_device - remove a device from it's current group |
| 380 | * @dev: device to be removed |
| 381 | * |
| 382 | * This function is called by an iommu driver to remove the device from |
| 383 | * it's current group. This decrements the iommu group reference count. |
| 384 | */ |
| 385 | void iommu_group_remove_device(struct device *dev) |
| 386 | { |
| 387 | struct iommu_group *group = dev->iommu_group; |
| 388 | struct iommu_device *tmp_device, *device = NULL; |
| 389 | |
| 390 | /* Pre-notify listeners that a device is being removed. */ |
| 391 | blocking_notifier_call_chain(&group->notifier, |
| 392 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); |
| 393 | |
| 394 | mutex_lock(&group->mutex); |
| 395 | list_for_each_entry(tmp_device, &group->devices, list) { |
| 396 | if (tmp_device->dev == dev) { |
| 397 | device = tmp_device; |
| 398 | list_del(&device->list); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | mutex_unlock(&group->mutex); |
| 403 | |
| 404 | if (!device) |
| 405 | return; |
| 406 | |
| 407 | sysfs_remove_link(group->devices_kobj, device->name); |
| 408 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 409 | |
Shuah Khan | 2e75708 | 2013-08-15 11:59:25 -0600 | [diff] [blame] | 410 | trace_remove_device_from_group(group->id, dev); |
| 411 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 412 | kfree(device->name); |
| 413 | kfree(device); |
| 414 | dev->iommu_group = NULL; |
| 415 | kobject_put(group->devices_kobj); |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); |
| 418 | |
| 419 | /** |
| 420 | * iommu_group_for_each_dev - iterate over each device in the group |
| 421 | * @group: the group |
| 422 | * @data: caller opaque data to be passed to callback function |
| 423 | * @fn: caller supplied callback function |
| 424 | * |
| 425 | * This function is called by group users to iterate over group devices. |
| 426 | * Callers should hold a reference count to the group during callback. |
| 427 | * The group->mutex is held across callbacks, which will block calls to |
| 428 | * iommu_group_add/remove_device. |
| 429 | */ |
| 430 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 431 | int (*fn)(struct device *, void *)) |
| 432 | { |
| 433 | struct iommu_device *device; |
| 434 | int ret = 0; |
| 435 | |
| 436 | mutex_lock(&group->mutex); |
| 437 | list_for_each_entry(device, &group->devices, list) { |
| 438 | ret = fn(device->dev, data); |
| 439 | if (ret) |
| 440 | break; |
| 441 | } |
| 442 | mutex_unlock(&group->mutex); |
| 443 | return ret; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); |
| 446 | |
| 447 | /** |
| 448 | * iommu_group_get - Return the group for a device and increment reference |
| 449 | * @dev: get the group that this device belongs to |
| 450 | * |
| 451 | * This function is called by iommu drivers and users to get the group |
| 452 | * for the specified device. If found, the group is returned and the group |
| 453 | * reference in incremented, else NULL. |
| 454 | */ |
| 455 | struct iommu_group *iommu_group_get(struct device *dev) |
| 456 | { |
| 457 | struct iommu_group *group = dev->iommu_group; |
| 458 | |
| 459 | if (group) |
| 460 | kobject_get(group->devices_kobj); |
| 461 | |
| 462 | return group; |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(iommu_group_get); |
| 465 | |
| 466 | /** |
| 467 | * iommu_group_put - Decrement group reference |
| 468 | * @group: the group to use |
| 469 | * |
| 470 | * This function is called by iommu drivers and users to release the |
| 471 | * iommu group. Once the reference count is zero, the group is released. |
| 472 | */ |
| 473 | void iommu_group_put(struct iommu_group *group) |
| 474 | { |
| 475 | if (group) |
| 476 | kobject_put(group->devices_kobj); |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(iommu_group_put); |
| 479 | |
| 480 | /** |
| 481 | * iommu_group_register_notifier - Register a notifier for group changes |
| 482 | * @group: the group to watch |
| 483 | * @nb: notifier block to signal |
| 484 | * |
| 485 | * This function allows iommu group users to track changes in a group. |
| 486 | * See include/linux/iommu.h for actions sent via this notifier. Caller |
| 487 | * should hold a reference to the group throughout notifier registration. |
| 488 | */ |
| 489 | int iommu_group_register_notifier(struct iommu_group *group, |
| 490 | struct notifier_block *nb) |
| 491 | { |
| 492 | return blocking_notifier_chain_register(&group->notifier, nb); |
| 493 | } |
| 494 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); |
| 495 | |
| 496 | /** |
| 497 | * iommu_group_unregister_notifier - Unregister a notifier |
| 498 | * @group: the group to watch |
| 499 | * @nb: notifier block to signal |
| 500 | * |
| 501 | * Unregister a previously registered group notifier block. |
| 502 | */ |
| 503 | int iommu_group_unregister_notifier(struct iommu_group *group, |
| 504 | struct notifier_block *nb) |
| 505 | { |
| 506 | return blocking_notifier_chain_unregister(&group->notifier, nb); |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); |
| 509 | |
| 510 | /** |
| 511 | * iommu_group_id - Return ID for a group |
| 512 | * @group: the group to ID |
| 513 | * |
| 514 | * Return the unique ID for the group matching the sysfs group number. |
| 515 | */ |
| 516 | int iommu_group_id(struct iommu_group *group) |
| 517 | { |
| 518 | return group->id; |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(iommu_group_id); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 521 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 522 | /* |
| 523 | * To consider a PCI device isolated, we require ACS to support Source |
| 524 | * Validation, Request Redirection, Completer Redirection, and Upstream |
| 525 | * Forwarding. This effectively means that devices cannot spoof their |
| 526 | * requester ID, requests and completions cannot be redirected, and all |
| 527 | * transactions are forwarded upstream, even as it passes through a |
| 528 | * bridge where the target device is downstream. |
| 529 | */ |
| 530 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) |
| 531 | |
| 532 | struct group_for_pci_data { |
| 533 | struct pci_dev *pdev; |
| 534 | struct iommu_group *group; |
| 535 | }; |
| 536 | |
| 537 | /* |
| 538 | * DMA alias iterator callback, return the last seen device. Stop and return |
| 539 | * the IOMMU group if we find one along the way. |
| 540 | */ |
| 541 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) |
| 542 | { |
| 543 | struct group_for_pci_data *data = opaque; |
| 544 | |
| 545 | data->pdev = pdev; |
| 546 | data->group = iommu_group_get(&pdev->dev); |
| 547 | |
| 548 | return data->group != NULL; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Use standard PCI bus topology, isolation features, and DMA alias quirks |
| 553 | * to find or create an IOMMU group for a device. |
| 554 | */ |
| 555 | static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev) |
| 556 | { |
| 557 | struct group_for_pci_data data; |
| 558 | struct pci_bus *bus; |
| 559 | struct iommu_group *group = NULL; |
| 560 | struct pci_dev *tmp; |
| 561 | |
| 562 | /* |
| 563 | * Find the upstream DMA alias for the device. A device must not |
| 564 | * be aliased due to topology in order to have its own IOMMU group. |
| 565 | * If we find an alias along the way that already belongs to a |
| 566 | * group, use it. |
| 567 | */ |
| 568 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) |
| 569 | return data.group; |
| 570 | |
| 571 | pdev = data.pdev; |
| 572 | |
| 573 | /* |
| 574 | * Continue upstream from the point of minimum IOMMU granularity |
| 575 | * due to aliases to the point where devices are protected from |
| 576 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing |
| 577 | * group, use it. |
| 578 | */ |
| 579 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { |
| 580 | if (!bus->self) |
| 581 | continue; |
| 582 | |
| 583 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) |
| 584 | break; |
| 585 | |
| 586 | pdev = bus->self; |
| 587 | |
| 588 | group = iommu_group_get(&pdev->dev); |
| 589 | if (group) |
| 590 | return group; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Next we need to consider DMA alias quirks. If one device aliases |
| 595 | * to another, they should be grouped together. It's theoretically |
| 596 | * possible that aliases could create chains of devices where each |
| 597 | * device aliases another device. If we then factor in multifunction |
| 598 | * ACS grouping requirements, each alias could incorporate a new slot |
| 599 | * with multiple functions, each with aliases. This is all extremely |
| 600 | * unlikely as DMA alias quirks are typically only used for PCIe |
| 601 | * devices where we usually have a single slot per bus. Furthermore, |
| 602 | * the alias quirk is usually to another function within the slot |
| 603 | * (and ACS multifunction is not supported) or to a different slot |
| 604 | * that doesn't physically exist. The likely scenario is therefore |
| 605 | * that everything on the bus gets grouped together. To reduce the |
| 606 | * problem space, share the IOMMU group for all devices on the bus |
| 607 | * if a DMA alias quirk is present on the bus. |
| 608 | */ |
| 609 | tmp = NULL; |
| 610 | for_each_pci_dev(tmp) { |
| 611 | if (tmp->bus != pdev->bus || |
| 612 | !(tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN)) |
| 613 | continue; |
| 614 | |
| 615 | pci_dev_put(tmp); |
| 616 | tmp = NULL; |
| 617 | |
| 618 | /* We have an alias quirk, search for an existing group */ |
| 619 | for_each_pci_dev(tmp) { |
| 620 | struct iommu_group *group_tmp; |
| 621 | |
| 622 | if (tmp->bus != pdev->bus) |
| 623 | continue; |
| 624 | |
| 625 | group_tmp = iommu_group_get(&tmp->dev); |
| 626 | if (!group) { |
| 627 | group = group_tmp; |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | if (group_tmp) { |
| 632 | WARN_ON(group != group_tmp); |
| 633 | iommu_group_put(group_tmp); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return group ? group : iommu_group_alloc(); |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Non-multifunction devices or multifunction devices supporting |
| 642 | * ACS get their own group. |
| 643 | */ |
| 644 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) |
| 645 | return iommu_group_alloc(); |
| 646 | |
| 647 | /* |
| 648 | * Multifunction devices not supporting ACS share a group with other |
| 649 | * similar devices in the same slot. |
| 650 | */ |
| 651 | tmp = NULL; |
| 652 | for_each_pci_dev(tmp) { |
| 653 | if (tmp == pdev || tmp->bus != pdev->bus || |
| 654 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || |
| 655 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) |
| 656 | continue; |
| 657 | |
| 658 | group = iommu_group_get(&tmp->dev); |
| 659 | if (group) { |
| 660 | pci_dev_put(tmp); |
| 661 | return group; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* No shared group found, allocate new */ |
| 666 | return iommu_group_alloc(); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device |
| 671 | * @dev: target device |
| 672 | * |
| 673 | * This function is intended to be called by IOMMU drivers and extended to |
| 674 | * support common, bus-defined algorithms when determining or creating the |
| 675 | * IOMMU group for a device. On success, the caller will hold a reference |
| 676 | * to the returned IOMMU group, which will already include the provided |
| 677 | * device. The reference should be released with iommu_group_put(). |
| 678 | */ |
| 679 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) |
| 680 | { |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 681 | struct iommu_group *group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 682 | int ret; |
| 683 | |
| 684 | group = iommu_group_get(dev); |
| 685 | if (group) |
| 686 | return group; |
| 687 | |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 688 | if (!dev_is_pci(dev)) |
| 689 | return ERR_PTR(-EINVAL); |
| 690 | |
| 691 | group = iommu_group_get_for_pci_dev(to_pci_dev(dev)); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 692 | |
| 693 | if (IS_ERR(group)) |
| 694 | return group; |
| 695 | |
| 696 | ret = iommu_group_add_device(group, dev); |
| 697 | if (ret) { |
| 698 | iommu_group_put(group); |
| 699 | return ERR_PTR(ret); |
| 700 | } |
| 701 | |
| 702 | return group; |
| 703 | } |
| 704 | |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 705 | static int add_iommu_group(struct device *dev, void *data) |
| 706 | { |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 707 | struct iommu_callback_data *cb = data; |
| 708 | const struct iommu_ops *ops = cb->ops; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 709 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 710 | if (!ops->add_device) |
| 711 | return -ENODEV; |
| 712 | |
| 713 | WARN_ON(dev->iommu_group); |
| 714 | |
| 715 | ops->add_device(dev); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 720 | static int iommu_bus_notifier(struct notifier_block *nb, |
| 721 | unsigned long action, void *data) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 722 | { |
| 723 | struct device *dev = data; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 724 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 725 | struct iommu_group *group; |
| 726 | unsigned long group_action = 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 727 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 728 | /* |
| 729 | * ADD/DEL call into iommu driver ops if provided, which may |
| 730 | * result in ADD/DEL notifiers to group->notifier |
| 731 | */ |
| 732 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
| 733 | if (ops->add_device) |
| 734 | return ops->add_device(dev); |
| 735 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { |
| 736 | if (ops->remove_device && dev->iommu_group) { |
| 737 | ops->remove_device(dev); |
| 738 | return 0; |
| 739 | } |
| 740 | } |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 741 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 742 | /* |
| 743 | * Remaining BUS_NOTIFYs get filtered and republished to the |
| 744 | * group, if anyone is listening |
| 745 | */ |
| 746 | group = iommu_group_get(dev); |
| 747 | if (!group) |
| 748 | return 0; |
| 749 | |
| 750 | switch (action) { |
| 751 | case BUS_NOTIFY_BIND_DRIVER: |
| 752 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; |
| 753 | break; |
| 754 | case BUS_NOTIFY_BOUND_DRIVER: |
| 755 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; |
| 756 | break; |
| 757 | case BUS_NOTIFY_UNBIND_DRIVER: |
| 758 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; |
| 759 | break; |
| 760 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 761 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | if (group_action) |
| 766 | blocking_notifier_call_chain(&group->notifier, |
| 767 | group_action, dev); |
| 768 | |
| 769 | iommu_group_put(group); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 773 | static struct notifier_block iommu_bus_nb = { |
| 774 | .notifier_call = iommu_bus_notifier, |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 775 | }; |
| 776 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 777 | static void iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 778 | { |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 779 | struct iommu_callback_data cb = { |
| 780 | .ops = ops, |
| 781 | }; |
| 782 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 783 | bus_register_notifier(bus, &iommu_bus_nb); |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 784 | bus_for_each_dev(bus, NULL, &cb, add_iommu_group); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 787 | /** |
| 788 | * bus_set_iommu - set iommu-callbacks for the bus |
| 789 | * @bus: bus. |
| 790 | * @ops: the callbacks provided by the iommu-driver |
| 791 | * |
| 792 | * This function is called by an iommu driver to set the iommu methods |
| 793 | * used for a particular bus. Drivers for devices on that bus can use |
| 794 | * the iommu-api after these ops are registered. |
| 795 | * This special function is needed because IOMMUs are usually devices on |
| 796 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 797 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 798 | * afterwards. |
| 799 | */ |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 800 | 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] | 801 | { |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 802 | if (bus->iommu_ops != NULL) |
| 803 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 804 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 805 | bus->iommu_ops = ops; |
| 806 | |
| 807 | /* Do IOMMU specific setup for this bus-type */ |
| 808 | iommu_bus_init(bus, ops); |
| 809 | |
| 810 | return 0; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 811 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 812 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 813 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 814 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 815 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 816 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 817 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 818 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 819 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 820 | /** |
| 821 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 822 | * @domain: iommu domain |
| 823 | * @handler: fault handler |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 824 | * @token: user data, will be passed back to the fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 825 | * |
| 826 | * This function should be used by IOMMU users which want to be notified |
| 827 | * whenever an IOMMU fault happens. |
| 828 | * |
| 829 | * The fault handler itself should return 0 on success, and an appropriate |
| 830 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 831 | */ |
| 832 | void iommu_set_fault_handler(struct iommu_domain *domain, |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 833 | iommu_fault_handler_t handler, |
| 834 | void *token) |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 835 | { |
| 836 | BUG_ON(!domain); |
| 837 | |
| 838 | domain->handler = handler; |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 839 | domain->handler_token = token; |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 840 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 841 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 842 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 843 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 844 | { |
| 845 | struct iommu_domain *domain; |
| 846 | int ret; |
| 847 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 848 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 849 | return NULL; |
| 850 | |
KyongHo Cho | 8bd6960 | 2011-12-16 21:38:25 +0900 | [diff] [blame] | 851 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 852 | if (!domain) |
| 853 | return NULL; |
| 854 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 855 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 856 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 857 | ret = domain->ops->domain_init(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 858 | if (ret) |
| 859 | goto out_free; |
| 860 | |
| 861 | return domain; |
| 862 | |
| 863 | out_free: |
| 864 | kfree(domain); |
| 865 | |
| 866 | return NULL; |
| 867 | } |
| 868 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 869 | |
| 870 | void iommu_domain_free(struct iommu_domain *domain) |
| 871 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 872 | if (likely(domain->ops->domain_destroy != NULL)) |
| 873 | domain->ops->domain_destroy(domain); |
| 874 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 875 | kfree(domain); |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 878 | |
| 879 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 880 | { |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 881 | int ret; |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 882 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 883 | return -ENODEV; |
| 884 | |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 885 | ret = domain->ops->attach_dev(domain, dev); |
| 886 | if (!ret) |
| 887 | trace_attach_device_to_domain(dev); |
| 888 | return ret; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 889 | } |
| 890 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 891 | |
| 892 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 893 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 894 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 895 | return; |
| 896 | |
| 897 | domain->ops->detach_dev(domain, dev); |
Shuah Khan | 6998063 | 2013-08-15 11:59:27 -0600 | [diff] [blame] | 898 | trace_detach_device_from_domain(dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 899 | } |
| 900 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 901 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 902 | /* |
| 903 | * IOMMU groups are really the natrual working unit of the IOMMU, but |
| 904 | * the IOMMU API works on domains and devices. Bridge that gap by |
| 905 | * iterating over the devices in a group. Ideally we'd have a single |
| 906 | * device which represents the requestor ID of the group, but we also |
| 907 | * allow IOMMU drivers to create policy defined minimum sets, where |
| 908 | * the physical hardware may be able to distiguish members, but we |
| 909 | * wish to group them at a higher level (ex. untrusted multi-function |
| 910 | * PCI devices). Thus we attach each device. |
| 911 | */ |
| 912 | static int iommu_group_do_attach_device(struct device *dev, void *data) |
| 913 | { |
| 914 | struct iommu_domain *domain = data; |
| 915 | |
| 916 | return iommu_attach_device(domain, dev); |
| 917 | } |
| 918 | |
| 919 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 920 | { |
| 921 | return iommu_group_for_each_dev(group, domain, |
| 922 | iommu_group_do_attach_device); |
| 923 | } |
| 924 | EXPORT_SYMBOL_GPL(iommu_attach_group); |
| 925 | |
| 926 | static int iommu_group_do_detach_device(struct device *dev, void *data) |
| 927 | { |
| 928 | struct iommu_domain *domain = data; |
| 929 | |
| 930 | iommu_detach_device(domain, dev); |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 936 | { |
| 937 | iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device); |
| 938 | } |
| 939 | EXPORT_SYMBOL_GPL(iommu_detach_group); |
| 940 | |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 941 | 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] | 942 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 943 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 944 | return 0; |
| 945 | |
| 946 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 947 | } |
| 948 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 949 | |
| 950 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 951 | unsigned long cap) |
| 952 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 953 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
| 954 | return 0; |
| 955 | |
| 956 | return domain->ops->domain_has_cap(domain, cap); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 957 | } |
| 958 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 959 | |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 960 | static size_t iommu_pgsize(struct iommu_domain *domain, |
| 961 | unsigned long addr_merge, size_t size) |
| 962 | { |
| 963 | unsigned int pgsize_idx; |
| 964 | size_t pgsize; |
| 965 | |
| 966 | /* Max page size that still fits into 'size' */ |
| 967 | pgsize_idx = __fls(size); |
| 968 | |
| 969 | /* need to consider alignment requirements ? */ |
| 970 | if (likely(addr_merge)) { |
| 971 | /* Max page size allowed by address */ |
| 972 | unsigned int align_pgsize_idx = __ffs(addr_merge); |
| 973 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); |
| 974 | } |
| 975 | |
| 976 | /* build a mask of acceptable page sizes */ |
| 977 | pgsize = (1UL << (pgsize_idx + 1)) - 1; |
| 978 | |
| 979 | /* throw away page sizes not supported by the hardware */ |
| 980 | pgsize &= domain->ops->pgsize_bitmap; |
| 981 | |
| 982 | /* make sure we're still sane */ |
| 983 | BUG_ON(!pgsize); |
| 984 | |
| 985 | /* pick the biggest page */ |
| 986 | pgsize_idx = __fls(pgsize); |
| 987 | pgsize = 1UL << pgsize_idx; |
| 988 | |
| 989 | return pgsize; |
| 990 | } |
| 991 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 992 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 993 | phys_addr_t paddr, size_t size, int prot) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 994 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 995 | unsigned long orig_iova = iova; |
| 996 | unsigned int min_pagesz; |
| 997 | size_t orig_size = size; |
| 998 | int ret = 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 999 | |
Joerg Roedel | 9db4ad9 | 2014-08-19 00:19:26 +0200 | [diff] [blame] | 1000 | if (unlikely(domain->ops->map == NULL || |
Joerg Roedel | 5788651 | 2013-01-29 13:41:09 +0100 | [diff] [blame] | 1001 | domain->ops->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1002 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1003 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1004 | /* find out the minimum page size supported */ |
| 1005 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1006 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1007 | /* |
| 1008 | * both the virtual address and the physical one, as well as |
| 1009 | * the size of the mapping, must be aligned (at least) to the |
| 1010 | * size of the smallest page supported by the hardware |
| 1011 | */ |
| 1012 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1013 | 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] | 1014 | iova, &paddr, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1015 | return -EINVAL; |
| 1016 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1017 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1018 | 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] | 1019 | |
| 1020 | while (size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1021 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1022 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1023 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1024 | iova, &paddr, pgsize); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1025 | |
| 1026 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); |
| 1027 | if (ret) |
| 1028 | break; |
| 1029 | |
| 1030 | iova += pgsize; |
| 1031 | paddr += pgsize; |
| 1032 | size -= pgsize; |
| 1033 | } |
| 1034 | |
| 1035 | /* unroll mapping in case something went wrong */ |
| 1036 | if (ret) |
| 1037 | iommu_unmap(domain, orig_iova, orig_size - size); |
Shuah Khan | e0be7c8 | 2013-08-15 11:59:28 -0600 | [diff] [blame] | 1038 | else |
| 1039 | trace_map(iova, paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1040 | |
| 1041 | return ret; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1042 | } |
| 1043 | EXPORT_SYMBOL_GPL(iommu_map); |
| 1044 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1045 | 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] | 1046 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1047 | size_t unmapped_page, unmapped = 0; |
| 1048 | unsigned int min_pagesz; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1049 | |
Joerg Roedel | 5788651 | 2013-01-29 13:41:09 +0100 | [diff] [blame] | 1050 | if (unlikely(domain->ops->unmap == NULL || |
| 1051 | domain->ops->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1052 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1053 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1054 | /* find out the minimum page size supported */ |
| 1055 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1056 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1057 | /* |
| 1058 | * The virtual address, as well as the size of the mapping, must be |
| 1059 | * aligned (at least) to the size of the smallest page supported |
| 1060 | * by the hardware |
| 1061 | */ |
| 1062 | if (!IS_ALIGNED(iova | size, min_pagesz)) { |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1063 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
| 1064 | iova, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1065 | return -EINVAL; |
| 1066 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1067 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1068 | 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] | 1069 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1070 | /* |
| 1071 | * Keep iterating until we either unmap 'size' bytes (or more) |
| 1072 | * or we hit an area that isn't mapped. |
| 1073 | */ |
| 1074 | while (unmapped < size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1075 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1076 | |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1077 | unmapped_page = domain->ops->unmap(domain, iova, pgsize); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1078 | if (!unmapped_page) |
| 1079 | break; |
| 1080 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1081 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
| 1082 | iova, unmapped_page); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1083 | |
| 1084 | iova += unmapped_page; |
| 1085 | unmapped += unmapped_page; |
| 1086 | } |
| 1087 | |
Shuah Khan | 3a50639 | 2013-08-15 11:59:29 -0600 | [diff] [blame] | 1088 | trace_unmap(iova, 0, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1089 | return unmapped; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1092 | |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1093 | |
| 1094 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1095 | phys_addr_t paddr, u64 size, int prot) |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1096 | { |
| 1097 | if (unlikely(domain->ops->domain_window_enable == NULL)) |
| 1098 | return -ENODEV; |
| 1099 | |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 1100 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
| 1101 | prot); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 1102 | } |
| 1103 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); |
| 1104 | |
| 1105 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) |
| 1106 | { |
| 1107 | if (unlikely(domain->ops->domain_window_disable == NULL)) |
| 1108 | return; |
| 1109 | |
| 1110 | return domain->ops->domain_window_disable(domain, wnd_nr); |
| 1111 | } |
| 1112 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); |
| 1113 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1114 | static int __init iommu_init(void) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1115 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1116 | iommu_group_kset = kset_create_and_add("iommu_groups", |
| 1117 | NULL, kernel_kobj); |
| 1118 | ida_init(&iommu_group_ida); |
| 1119 | mutex_init(&iommu_group_mutex); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1120 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1121 | BUG_ON(!iommu_group_kset); |
| 1122 | |
| 1123 | return 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1124 | } |
Alexey Kardashevskiy | 097e363 | 2013-01-07 18:51:52 +1100 | [diff] [blame] | 1125 | arch_initcall(iommu_init); |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1126 | |
| 1127 | int iommu_domain_get_attr(struct iommu_domain *domain, |
| 1128 | enum iommu_attr attr, void *data) |
| 1129 | { |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1130 | struct iommu_domain_geometry *geometry; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1131 | bool *paging; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1132 | int ret = 0; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1133 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1134 | |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1135 | switch (attr) { |
| 1136 | case DOMAIN_ATTR_GEOMETRY: |
| 1137 | geometry = data; |
| 1138 | *geometry = domain->geometry; |
| 1139 | |
| 1140 | break; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 1141 | case DOMAIN_ATTR_PAGING: |
| 1142 | paging = data; |
| 1143 | *paging = (domain->ops->pgsize_bitmap != 0UL); |
| 1144 | break; |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1145 | case DOMAIN_ATTR_WINDOWS: |
| 1146 | count = data; |
| 1147 | |
| 1148 | if (domain->ops->domain_get_windows != NULL) |
| 1149 | *count = domain->ops->domain_get_windows(domain); |
| 1150 | else |
| 1151 | ret = -ENODEV; |
| 1152 | |
| 1153 | break; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 1154 | default: |
| 1155 | if (!domain->ops->domain_get_attr) |
| 1156 | return -EINVAL; |
| 1157 | |
| 1158 | ret = domain->ops->domain_get_attr(domain, attr, data); |
| 1159 | } |
| 1160 | |
| 1161 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1162 | } |
| 1163 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); |
| 1164 | |
| 1165 | int iommu_domain_set_attr(struct iommu_domain *domain, |
| 1166 | enum iommu_attr attr, void *data) |
| 1167 | { |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1168 | int ret = 0; |
| 1169 | u32 *count; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1170 | |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 1171 | switch (attr) { |
| 1172 | case DOMAIN_ATTR_WINDOWS: |
| 1173 | count = data; |
| 1174 | |
| 1175 | if (domain->ops->domain_set_windows != NULL) |
| 1176 | ret = domain->ops->domain_set_windows(domain, *count); |
| 1177 | else |
| 1178 | ret = -ENODEV; |
| 1179 | |
| 1180 | break; |
| 1181 | default: |
| 1182 | if (domain->ops->domain_set_attr == NULL) |
| 1183 | return -EINVAL; |
| 1184 | |
| 1185 | ret = domain->ops->domain_set_attr(domain, attr, data); |
| 1186 | } |
| 1187 | |
| 1188 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 1189 | } |
| 1190 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |