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> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 30 | #include <linux/idr.h> |
| 31 | #include <linux/notifier.h> |
| 32 | #include <linux/err.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 33 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 34 | static struct kset *iommu_group_kset; |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 35 | static struct idr iommu_group_idr; |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 36 | static struct mutex iommu_group_mutex; |
| 37 | |
| 38 | struct iommu_group { |
| 39 | struct kobject kobj; |
| 40 | struct kobject *devices_kobj; |
| 41 | struct list_head devices; |
| 42 | struct mutex mutex; |
| 43 | struct blocking_notifier_head notifier; |
| 44 | void *iommu_data; |
| 45 | void (*iommu_data_release)(void *iommu_data); |
| 46 | char *name; |
| 47 | int id; |
| 48 | }; |
| 49 | |
| 50 | struct iommu_device { |
| 51 | struct list_head list; |
| 52 | struct device *dev; |
| 53 | char *name; |
| 54 | }; |
| 55 | |
| 56 | struct iommu_group_attribute { |
| 57 | struct attribute attr; |
| 58 | ssize_t (*show)(struct iommu_group *group, char *buf); |
| 59 | ssize_t (*store)(struct iommu_group *group, |
| 60 | const char *buf, size_t count); |
| 61 | }; |
| 62 | |
| 63 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
| 64 | struct iommu_group_attribute iommu_group_attr_##_name = \ |
| 65 | __ATTR(_name, _mode, _show, _store) |
| 66 | |
| 67 | #define to_iommu_group_attr(_attr) \ |
| 68 | container_of(_attr, struct iommu_group_attribute, attr) |
| 69 | #define to_iommu_group(_kobj) \ |
| 70 | container_of(_kobj, struct iommu_group, kobj) |
| 71 | |
| 72 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
| 73 | struct attribute *__attr, char *buf) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 74 | { |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 75 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 76 | struct iommu_group *group = to_iommu_group(kobj); |
| 77 | ssize_t ret = -EIO; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 78 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 79 | if (attr->show) |
| 80 | ret = attr->show(group, buf); |
| 81 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 82 | } |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 83 | |
| 84 | static ssize_t iommu_group_attr_store(struct kobject *kobj, |
| 85 | struct attribute *__attr, |
| 86 | const char *buf, size_t count) |
| 87 | { |
| 88 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 89 | struct iommu_group *group = to_iommu_group(kobj); |
| 90 | ssize_t ret = -EIO; |
| 91 | |
| 92 | if (attr->store) |
| 93 | ret = attr->store(group, buf, count); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
| 98 | .show = iommu_group_attr_show, |
| 99 | .store = iommu_group_attr_store, |
| 100 | }; |
| 101 | |
| 102 | static int iommu_group_create_file(struct iommu_group *group, |
| 103 | struct iommu_group_attribute *attr) |
| 104 | { |
| 105 | return sysfs_create_file(&group->kobj, &attr->attr); |
| 106 | } |
| 107 | |
| 108 | static void iommu_group_remove_file(struct iommu_group *group, |
| 109 | struct iommu_group_attribute *attr) |
| 110 | { |
| 111 | sysfs_remove_file(&group->kobj, &attr->attr); |
| 112 | } |
| 113 | |
| 114 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) |
| 115 | { |
| 116 | return sprintf(buf, "%s\n", group->name); |
| 117 | } |
| 118 | |
| 119 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
| 120 | |
| 121 | static void iommu_group_release(struct kobject *kobj) |
| 122 | { |
| 123 | struct iommu_group *group = to_iommu_group(kobj); |
| 124 | |
| 125 | if (group->iommu_data_release) |
| 126 | group->iommu_data_release(group->iommu_data); |
| 127 | |
| 128 | mutex_lock(&iommu_group_mutex); |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 129 | idr_remove(&iommu_group_idr, group->id); |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 130 | mutex_unlock(&iommu_group_mutex); |
| 131 | |
| 132 | kfree(group->name); |
| 133 | kfree(group); |
| 134 | } |
| 135 | |
| 136 | static struct kobj_type iommu_group_ktype = { |
| 137 | .sysfs_ops = &iommu_group_sysfs_ops, |
| 138 | .release = iommu_group_release, |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * iommu_group_alloc - Allocate a new group |
| 143 | * @name: Optional name to associate with group, visible in sysfs |
| 144 | * |
| 145 | * This function is called by an iommu driver to allocate a new iommu |
| 146 | * group. The iommu group represents the minimum granularity of the iommu. |
| 147 | * Upon successful return, the caller holds a reference to the supplied |
| 148 | * group in order to hold the group until devices are added. Use |
| 149 | * iommu_group_put() to release this extra reference count, allowing the |
| 150 | * group to be automatically reclaimed once it has no devices or external |
| 151 | * references. |
| 152 | */ |
| 153 | struct iommu_group *iommu_group_alloc(void) |
| 154 | { |
| 155 | struct iommu_group *group; |
| 156 | int ret; |
| 157 | |
| 158 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 159 | if (!group) |
| 160 | return ERR_PTR(-ENOMEM); |
| 161 | |
| 162 | group->kobj.kset = iommu_group_kset; |
| 163 | mutex_init(&group->mutex); |
| 164 | INIT_LIST_HEAD(&group->devices); |
| 165 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); |
| 166 | |
| 167 | mutex_lock(&iommu_group_mutex); |
| 168 | |
| 169 | again: |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 170 | if (unlikely(0 == idr_pre_get(&iommu_group_idr, GFP_KERNEL))) { |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 171 | kfree(group); |
| 172 | mutex_unlock(&iommu_group_mutex); |
| 173 | return ERR_PTR(-ENOMEM); |
| 174 | } |
| 175 | |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 176 | ret = idr_get_new_above(&iommu_group_idr, group, 1, &group->id); |
| 177 | if (ret == -EAGAIN) |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 178 | goto again; |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 179 | mutex_unlock(&iommu_group_mutex); |
| 180 | |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 181 | if (ret == -ENOSPC) { |
| 182 | kfree(group); |
| 183 | return ERR_PTR(ret); |
| 184 | } |
| 185 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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); |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 190 | idr_remove(&iommu_group_idr, group->id); |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 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 | |
| 213 | /** |
| 214 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group |
| 215 | * @group: the group |
| 216 | * |
| 217 | * iommu drivers can store data in the group for use when doing iommu |
| 218 | * operations. This function provides a way to retrieve it. Caller |
| 219 | * should hold a group reference. |
| 220 | */ |
| 221 | void *iommu_group_get_iommudata(struct iommu_group *group) |
| 222 | { |
| 223 | return group->iommu_data; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); |
| 226 | |
| 227 | /** |
| 228 | * iommu_group_set_iommudata - set iommu_data for a group |
| 229 | * @group: the group |
| 230 | * @iommu_data: new data |
| 231 | * @release: release function for iommu_data |
| 232 | * |
| 233 | * iommu drivers can store data in the group for use when doing iommu |
| 234 | * operations. This function provides a way to set the data after |
| 235 | * the group has been allocated. Caller should hold a group reference. |
| 236 | */ |
| 237 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, |
| 238 | void (*release)(void *iommu_data)) |
| 239 | { |
| 240 | group->iommu_data = iommu_data; |
| 241 | group->iommu_data_release = release; |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); |
| 244 | |
| 245 | /** |
| 246 | * iommu_group_set_name - set name for a group |
| 247 | * @group: the group |
| 248 | * @name: name |
| 249 | * |
| 250 | * Allow iommu driver to set a name for a group. When set it will |
| 251 | * appear in a name attribute file under the group in sysfs. |
| 252 | */ |
| 253 | int iommu_group_set_name(struct iommu_group *group, const char *name) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | if (group->name) { |
| 258 | iommu_group_remove_file(group, &iommu_group_attr_name); |
| 259 | kfree(group->name); |
| 260 | group->name = NULL; |
| 261 | if (!name) |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | group->name = kstrdup(name, GFP_KERNEL); |
| 266 | if (!group->name) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | ret = iommu_group_create_file(group, &iommu_group_attr_name); |
| 270 | if (ret) { |
| 271 | kfree(group->name); |
| 272 | group->name = NULL; |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
| 279 | |
| 280 | /** |
| 281 | * iommu_group_add_device - add a device to an iommu group |
| 282 | * @group: the group into which to add the device (reference should be held) |
| 283 | * @dev: the device |
| 284 | * |
| 285 | * This function is called by an iommu driver to add a device into a |
| 286 | * group. Adding a device increments the group reference count. |
| 287 | */ |
| 288 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) |
| 289 | { |
| 290 | int ret, i = 0; |
| 291 | struct iommu_device *device; |
| 292 | |
| 293 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 294 | if (!device) |
| 295 | return -ENOMEM; |
| 296 | |
| 297 | device->dev = dev; |
| 298 | |
| 299 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
| 300 | if (ret) { |
| 301 | kfree(device); |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); |
| 306 | rename: |
| 307 | if (!device->name) { |
| 308 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 309 | kfree(device); |
| 310 | return -ENOMEM; |
| 311 | } |
| 312 | |
| 313 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
| 314 | &dev->kobj, device->name); |
| 315 | if (ret) { |
| 316 | kfree(device->name); |
| 317 | if (ret == -EEXIST && i >= 0) { |
| 318 | /* |
| 319 | * Account for the slim chance of collision |
| 320 | * and append an instance to the name. |
| 321 | */ |
| 322 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
| 323 | kobject_name(&dev->kobj), i++); |
| 324 | goto rename; |
| 325 | } |
| 326 | |
| 327 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 328 | kfree(device); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | kobject_get(group->devices_kobj); |
| 333 | |
| 334 | dev->iommu_group = group; |
| 335 | |
| 336 | mutex_lock(&group->mutex); |
| 337 | list_add_tail(&device->list, &group->devices); |
| 338 | mutex_unlock(&group->mutex); |
| 339 | |
| 340 | /* Notify any listeners about change to group. */ |
| 341 | blocking_notifier_call_chain(&group->notifier, |
| 342 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); |
| 343 | return 0; |
| 344 | } |
| 345 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
| 346 | |
| 347 | /** |
| 348 | * iommu_group_remove_device - remove a device from it's current group |
| 349 | * @dev: device to be removed |
| 350 | * |
| 351 | * This function is called by an iommu driver to remove the device from |
| 352 | * it's current group. This decrements the iommu group reference count. |
| 353 | */ |
| 354 | void iommu_group_remove_device(struct device *dev) |
| 355 | { |
| 356 | struct iommu_group *group = dev->iommu_group; |
| 357 | struct iommu_device *tmp_device, *device = NULL; |
| 358 | |
| 359 | /* Pre-notify listeners that a device is being removed. */ |
| 360 | blocking_notifier_call_chain(&group->notifier, |
| 361 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); |
| 362 | |
| 363 | mutex_lock(&group->mutex); |
| 364 | list_for_each_entry(tmp_device, &group->devices, list) { |
| 365 | if (tmp_device->dev == dev) { |
| 366 | device = tmp_device; |
| 367 | list_del(&device->list); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | mutex_unlock(&group->mutex); |
| 372 | |
| 373 | if (!device) |
| 374 | return; |
| 375 | |
| 376 | sysfs_remove_link(group->devices_kobj, device->name); |
| 377 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 378 | |
| 379 | kfree(device->name); |
| 380 | kfree(device); |
| 381 | dev->iommu_group = NULL; |
| 382 | kobject_put(group->devices_kobj); |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); |
| 385 | |
| 386 | /** |
| 387 | * iommu_group_for_each_dev - iterate over each device in the group |
| 388 | * @group: the group |
| 389 | * @data: caller opaque data to be passed to callback function |
| 390 | * @fn: caller supplied callback function |
| 391 | * |
| 392 | * This function is called by group users to iterate over group devices. |
| 393 | * Callers should hold a reference count to the group during callback. |
| 394 | * The group->mutex is held across callbacks, which will block calls to |
| 395 | * iommu_group_add/remove_device. |
| 396 | */ |
| 397 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 398 | int (*fn)(struct device *, void *)) |
| 399 | { |
| 400 | struct iommu_device *device; |
| 401 | int ret = 0; |
| 402 | |
| 403 | mutex_lock(&group->mutex); |
| 404 | list_for_each_entry(device, &group->devices, list) { |
| 405 | ret = fn(device->dev, data); |
| 406 | if (ret) |
| 407 | break; |
| 408 | } |
| 409 | mutex_unlock(&group->mutex); |
| 410 | return ret; |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); |
| 413 | |
| 414 | /** |
| 415 | * iommu_group_get - Return the group for a device and increment reference |
| 416 | * @dev: get the group that this device belongs to |
| 417 | * |
| 418 | * This function is called by iommu drivers and users to get the group |
| 419 | * for the specified device. If found, the group is returned and the group |
| 420 | * reference in incremented, else NULL. |
| 421 | */ |
| 422 | struct iommu_group *iommu_group_get(struct device *dev) |
| 423 | { |
| 424 | struct iommu_group *group = dev->iommu_group; |
| 425 | |
| 426 | if (group) |
| 427 | kobject_get(group->devices_kobj); |
| 428 | |
| 429 | return group; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(iommu_group_get); |
| 432 | |
| 433 | /** |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 434 | * iommu_group_find - Find and return the group based on the group name. |
| 435 | * Also increment the reference count. |
| 436 | * @name: the name of the group |
| 437 | * |
| 438 | * This function is called by iommu drivers and clients to get the group |
| 439 | * by the specified name. If found, the group is returned and the group |
| 440 | * reference is incremented, else NULL. |
| 441 | */ |
| 442 | struct iommu_group *iommu_group_find(const char *name) |
| 443 | { |
| 444 | struct iommu_group *group; |
| 445 | int next = 0; |
| 446 | |
| 447 | mutex_lock(&iommu_group_mutex); |
| 448 | while ((group = idr_get_next(&iommu_group_idr, &next))) { |
| 449 | if (group->name) { |
| 450 | if (strcmp(group->name, name) == 0) |
| 451 | break; |
| 452 | } |
| 453 | ++next; |
| 454 | } |
| 455 | mutex_unlock(&iommu_group_mutex); |
| 456 | |
| 457 | if (group) |
| 458 | kobject_get(group->devices_kobj); |
| 459 | |
| 460 | return group; |
| 461 | } |
| 462 | EXPORT_SYMBOL_GPL(iommu_group_find); |
| 463 | |
| 464 | /** |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 465 | * iommu_group_put - Decrement group reference |
| 466 | * @group: the group to use |
| 467 | * |
| 468 | * This function is called by iommu drivers and users to release the |
| 469 | * iommu group. Once the reference count is zero, the group is released. |
| 470 | */ |
| 471 | void iommu_group_put(struct iommu_group *group) |
| 472 | { |
| 473 | if (group) |
| 474 | kobject_put(group->devices_kobj); |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(iommu_group_put); |
| 477 | |
| 478 | /** |
| 479 | * iommu_group_register_notifier - Register a notifier for group changes |
| 480 | * @group: the group to watch |
| 481 | * @nb: notifier block to signal |
| 482 | * |
| 483 | * This function allows iommu group users to track changes in a group. |
| 484 | * See include/linux/iommu.h for actions sent via this notifier. Caller |
| 485 | * should hold a reference to the group throughout notifier registration. |
| 486 | */ |
| 487 | int iommu_group_register_notifier(struct iommu_group *group, |
| 488 | struct notifier_block *nb) |
| 489 | { |
| 490 | return blocking_notifier_chain_register(&group->notifier, nb); |
| 491 | } |
| 492 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); |
| 493 | |
| 494 | /** |
| 495 | * iommu_group_unregister_notifier - Unregister a notifier |
| 496 | * @group: the group to watch |
| 497 | * @nb: notifier block to signal |
| 498 | * |
| 499 | * Unregister a previously registered group notifier block. |
| 500 | */ |
| 501 | int iommu_group_unregister_notifier(struct iommu_group *group, |
| 502 | struct notifier_block *nb) |
| 503 | { |
| 504 | return blocking_notifier_chain_unregister(&group->notifier, nb); |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); |
| 507 | |
| 508 | /** |
| 509 | * iommu_group_id - Return ID for a group |
| 510 | * @group: the group to ID |
| 511 | * |
| 512 | * Return the unique ID for the group matching the sysfs group number. |
| 513 | */ |
| 514 | int iommu_group_id(struct iommu_group *group) |
| 515 | { |
| 516 | return group->id; |
| 517 | } |
| 518 | EXPORT_SYMBOL_GPL(iommu_group_id); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 519 | |
| 520 | static int add_iommu_group(struct device *dev, void *data) |
| 521 | { |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 522 | struct iommu_ops *ops = data; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 523 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 524 | if (!ops->add_device) |
| 525 | return -ENODEV; |
| 526 | |
| 527 | WARN_ON(dev->iommu_group); |
| 528 | |
| 529 | ops->add_device(dev); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 534 | static int iommu_bus_notifier(struct notifier_block *nb, |
| 535 | unsigned long action, void *data) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 536 | { |
| 537 | struct device *dev = data; |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 538 | struct iommu_ops *ops = dev->bus->iommu_ops; |
| 539 | struct iommu_group *group; |
| 540 | unsigned long group_action = 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 541 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 542 | /* |
| 543 | * ADD/DEL call into iommu driver ops if provided, which may |
| 544 | * result in ADD/DEL notifiers to group->notifier |
| 545 | */ |
| 546 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
| 547 | if (ops->add_device) |
| 548 | return ops->add_device(dev); |
| 549 | } else if (action == BUS_NOTIFY_DEL_DEVICE) { |
| 550 | if (ops->remove_device && dev->iommu_group) { |
| 551 | ops->remove_device(dev); |
| 552 | return 0; |
| 553 | } |
| 554 | } |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 555 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 556 | /* |
| 557 | * Remaining BUS_NOTIFYs get filtered and republished to the |
| 558 | * group, if anyone is listening |
| 559 | */ |
| 560 | group = iommu_group_get(dev); |
| 561 | if (!group) |
| 562 | return 0; |
| 563 | |
| 564 | switch (action) { |
| 565 | case BUS_NOTIFY_BIND_DRIVER: |
| 566 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; |
| 567 | break; |
| 568 | case BUS_NOTIFY_BOUND_DRIVER: |
| 569 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; |
| 570 | break; |
| 571 | case BUS_NOTIFY_UNBIND_DRIVER: |
| 572 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; |
| 573 | break; |
| 574 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 575 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | if (group_action) |
| 580 | blocking_notifier_call_chain(&group->notifier, |
| 581 | group_action, dev); |
| 582 | |
| 583 | iommu_group_put(group); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 587 | static struct notifier_block iommu_bus_nb = { |
| 588 | .notifier_call = iommu_bus_notifier, |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 589 | }; |
| 590 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 591 | static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 592 | { |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 593 | bus_register_notifier(bus, &iommu_bus_nb); |
| 594 | bus_for_each_dev(bus, NULL, ops, add_iommu_group); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 597 | /** |
| 598 | * bus_set_iommu - set iommu-callbacks for the bus |
| 599 | * @bus: bus. |
| 600 | * @ops: the callbacks provided by the iommu-driver |
| 601 | * |
| 602 | * This function is called by an iommu driver to set the iommu methods |
| 603 | * used for a particular bus. Drivers for devices on that bus can use |
| 604 | * the iommu-api after these ops are registered. |
| 605 | * This special function is needed because IOMMUs are usually devices on |
| 606 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 607 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 608 | * afterwards. |
| 609 | */ |
| 610 | int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 611 | { |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 612 | if (bus->iommu_ops != NULL) |
| 613 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 614 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 615 | bus->iommu_ops = ops; |
| 616 | |
| 617 | /* Do IOMMU specific setup for this bus-type */ |
| 618 | iommu_bus_init(bus, ops); |
| 619 | |
| 620 | return 0; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 621 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 622 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 623 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 624 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 625 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 626 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 627 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 628 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 629 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 630 | /** |
| 631 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 632 | * @domain: iommu domain |
| 633 | * @handler: fault handler |
Ohad Ben-Cohen | d506953 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 634 | * @token: user data, will be passed back to the fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 635 | * |
| 636 | * This function should be used by IOMMU users which want to be notified |
| 637 | * whenever an IOMMU fault happens. |
| 638 | * |
| 639 | * The fault handler itself should return 0 on success, and an appropriate |
| 640 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 641 | */ |
| 642 | void iommu_set_fault_handler(struct iommu_domain *domain, |
Ohad Ben-Cohen | d506953 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 643 | iommu_fault_handler_t handler, |
| 644 | void *token) |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 645 | { |
| 646 | BUG_ON(!domain); |
| 647 | |
| 648 | domain->handler = handler; |
Ohad Ben-Cohen | d506953 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 649 | domain->handler_token = token; |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 650 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 651 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 652 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 653 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 654 | { |
| 655 | struct iommu_domain *domain; |
| 656 | int ret; |
| 657 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 658 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 659 | return NULL; |
| 660 | |
KyongHo Cho | 8bd6960 | 2011-12-16 21:38:25 +0900 | [diff] [blame] | 661 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 662 | if (!domain) |
| 663 | return NULL; |
| 664 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 665 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 666 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 667 | ret = domain->ops->domain_init(domain, flags); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 668 | if (ret) |
| 669 | goto out_free; |
| 670 | |
| 671 | return domain; |
| 672 | |
| 673 | out_free: |
| 674 | kfree(domain); |
| 675 | |
| 676 | return NULL; |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 679 | |
| 680 | void iommu_domain_free(struct iommu_domain *domain) |
| 681 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 682 | if (likely(domain->ops->domain_destroy != NULL)) |
| 683 | domain->ops->domain_destroy(domain); |
| 684 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 685 | kfree(domain); |
| 686 | } |
| 687 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 688 | |
| 689 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 690 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 691 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 692 | return -ENODEV; |
| 693 | |
| 694 | return domain->ops->attach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 695 | } |
| 696 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 697 | |
| 698 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 699 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 700 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 701 | return; |
| 702 | |
| 703 | domain->ops->detach_dev(domain, dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 704 | } |
| 705 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 706 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 707 | /* |
| 708 | * IOMMU groups are really the natrual working unit of the IOMMU, but |
| 709 | * the IOMMU API works on domains and devices. Bridge that gap by |
| 710 | * iterating over the devices in a group. Ideally we'd have a single |
| 711 | * device which represents the requestor ID of the group, but we also |
| 712 | * allow IOMMU drivers to create policy defined minimum sets, where |
| 713 | * the physical hardware may be able to distiguish members, but we |
| 714 | * wish to group them at a higher level (ex. untrusted multi-function |
| 715 | * PCI devices). Thus we attach each device. |
| 716 | */ |
| 717 | static int iommu_group_do_attach_device(struct device *dev, void *data) |
| 718 | { |
| 719 | struct iommu_domain *domain = data; |
| 720 | |
| 721 | return iommu_attach_device(domain, dev); |
| 722 | } |
| 723 | |
| 724 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 725 | { |
| 726 | return iommu_group_for_each_dev(group, domain, |
| 727 | iommu_group_do_attach_device); |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(iommu_attach_group); |
| 730 | |
| 731 | static int iommu_group_do_detach_device(struct device *dev, void *data) |
| 732 | { |
| 733 | struct iommu_domain *domain = data; |
| 734 | |
| 735 | iommu_detach_device(domain, dev); |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 741 | { |
| 742 | iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device); |
| 743 | } |
| 744 | EXPORT_SYMBOL_GPL(iommu_detach_group); |
| 745 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 746 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 747 | unsigned long iova) |
| 748 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 749 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 750 | return 0; |
| 751 | |
| 752 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 753 | } |
| 754 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 755 | |
| 756 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 757 | unsigned long cap) |
| 758 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 759 | if (unlikely(domain->ops->domain_has_cap == NULL)) |
| 760 | return 0; |
| 761 | |
| 762 | return domain->ops->domain_has_cap(domain, cap); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 763 | } |
| 764 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 765 | |
| 766 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 767 | phys_addr_t paddr, size_t size, int prot) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 768 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 769 | unsigned long orig_iova = iova; |
| 770 | unsigned int min_pagesz; |
| 771 | size_t orig_size = size; |
| 772 | int ret = 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 773 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 774 | if (unlikely(domain->ops->map == NULL)) |
| 775 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 776 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 777 | /* find out the minimum page size supported */ |
| 778 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 779 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 780 | /* |
| 781 | * both the virtual address and the physical one, as well as |
| 782 | * the size of the mapping, must be aligned (at least) to the |
| 783 | * size of the smallest page supported by the hardware |
| 784 | */ |
| 785 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { |
| 786 | pr_err("unaligned: iova 0x%lx pa 0x%lx size 0x%lx min_pagesz " |
| 787 | "0x%x\n", iova, (unsigned long)paddr, |
| 788 | (unsigned long)size, min_pagesz); |
| 789 | return -EINVAL; |
| 790 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 791 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 792 | pr_debug("map: iova 0x%lx pa 0x%lx size 0x%lx\n", iova, |
| 793 | (unsigned long)paddr, (unsigned long)size); |
| 794 | |
| 795 | while (size) { |
| 796 | unsigned long pgsize, addr_merge = iova | paddr; |
| 797 | unsigned int pgsize_idx; |
| 798 | |
| 799 | /* Max page size that still fits into 'size' */ |
| 800 | pgsize_idx = __fls(size); |
| 801 | |
| 802 | /* need to consider alignment requirements ? */ |
| 803 | if (likely(addr_merge)) { |
| 804 | /* Max page size allowed by both iova and paddr */ |
| 805 | unsigned int align_pgsize_idx = __ffs(addr_merge); |
| 806 | |
| 807 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); |
| 808 | } |
| 809 | |
| 810 | /* build a mask of acceptable page sizes */ |
| 811 | pgsize = (1UL << (pgsize_idx + 1)) - 1; |
| 812 | |
| 813 | /* throw away page sizes not supported by the hardware */ |
| 814 | pgsize &= domain->ops->pgsize_bitmap; |
| 815 | |
| 816 | /* make sure we're still sane */ |
| 817 | BUG_ON(!pgsize); |
| 818 | |
| 819 | /* pick the biggest page */ |
| 820 | pgsize_idx = __fls(pgsize); |
| 821 | pgsize = 1UL << pgsize_idx; |
| 822 | |
| 823 | pr_debug("mapping: iova 0x%lx pa 0x%lx pgsize %lu\n", iova, |
| 824 | (unsigned long)paddr, pgsize); |
| 825 | |
| 826 | ret = domain->ops->map(domain, iova, paddr, pgsize, prot); |
| 827 | if (ret) |
| 828 | break; |
| 829 | |
| 830 | iova += pgsize; |
| 831 | paddr += pgsize; |
| 832 | size -= pgsize; |
| 833 | } |
| 834 | |
| 835 | /* unroll mapping in case something went wrong */ |
| 836 | if (ret) |
| 837 | iommu_unmap(domain, orig_iova, orig_size - size); |
| 838 | |
| 839 | return ret; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 840 | } |
| 841 | EXPORT_SYMBOL_GPL(iommu_map); |
| 842 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 843 | 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] | 844 | { |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 845 | size_t unmapped_page, unmapped = 0; |
| 846 | unsigned int min_pagesz; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 847 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 848 | if (unlikely(domain->ops->unmap == NULL)) |
| 849 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 850 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 851 | /* find out the minimum page size supported */ |
| 852 | min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 853 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 854 | /* |
| 855 | * The virtual address, as well as the size of the mapping, must be |
| 856 | * aligned (at least) to the size of the smallest page supported |
| 857 | * by the hardware |
| 858 | */ |
| 859 | if (!IS_ALIGNED(iova | size, min_pagesz)) { |
| 860 | pr_err("unaligned: iova 0x%lx size 0x%lx min_pagesz 0x%x\n", |
| 861 | iova, (unsigned long)size, min_pagesz); |
| 862 | return -EINVAL; |
| 863 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 864 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 865 | pr_debug("unmap this: iova 0x%lx size 0x%lx\n", iova, |
| 866 | (unsigned long)size); |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 867 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 868 | /* |
| 869 | * Keep iterating until we either unmap 'size' bytes (or more) |
| 870 | * or we hit an area that isn't mapped. |
| 871 | */ |
| 872 | while (unmapped < size) { |
| 873 | size_t left = size - unmapped; |
| 874 | |
| 875 | unmapped_page = domain->ops->unmap(domain, iova, left); |
| 876 | if (!unmapped_page) |
| 877 | break; |
| 878 | |
| 879 | pr_debug("unmapped: iova 0x%lx size %lx\n", iova, |
| 880 | (unsigned long)unmapped_page); |
| 881 | |
| 882 | iova += unmapped_page; |
| 883 | unmapped += unmapped_page; |
| 884 | } |
| 885 | |
| 886 | return unmapped; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 887 | } |
| 888 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 889 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 890 | int iommu_map_range(struct iommu_domain *domain, unsigned int iova, |
| 891 | struct scatterlist *sg, unsigned int len, int prot) |
| 892 | { |
| 893 | if (unlikely(domain->ops->map_range == NULL)) |
| 894 | return -ENODEV; |
| 895 | |
| 896 | BUG_ON(iova & (~PAGE_MASK)); |
| 897 | |
| 898 | return domain->ops->map_range(domain, iova, sg, len, prot); |
| 899 | } |
| 900 | EXPORT_SYMBOL_GPL(iommu_map_range); |
| 901 | |
| 902 | int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 903 | unsigned int len) |
| 904 | { |
| 905 | if (unlikely(domain->ops->unmap_range == NULL)) |
| 906 | return -ENODEV; |
| 907 | |
| 908 | BUG_ON(iova & (~PAGE_MASK)); |
| 909 | |
| 910 | return domain->ops->unmap_range(domain, iova, len); |
| 911 | } |
| 912 | EXPORT_SYMBOL_GPL(iommu_unmap_range); |
| 913 | |
| 914 | phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 915 | { |
| 916 | if (unlikely(domain->ops->get_pt_base_addr == NULL)) |
| 917 | return 0; |
| 918 | |
| 919 | return domain->ops->get_pt_base_addr(domain); |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr); |
| 922 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 923 | static int __init iommu_init(void) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 924 | { |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 925 | iommu_group_kset = kset_create_and_add("iommu_groups", |
| 926 | NULL, kernel_kobj); |
Olav Haugan | 0d06337 | 2012-12-06 16:53:56 -0800 | [diff] [blame] | 927 | idr_init(&iommu_group_idr); |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 928 | mutex_init(&iommu_group_mutex); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 929 | |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 930 | BUG_ON(!iommu_group_kset); |
| 931 | |
| 932 | return 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 933 | } |
Alex Williamson | 43a0ba6 | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 934 | subsys_initcall(iommu_init); |