Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2012 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 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 19 | #include <linux/mmu_notifier.h> |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 20 | #include <linux/amd-iommu.h> |
| 21 | #include <linux/mm_types.h> |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 22 | #include <linux/profile.h> |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 23 | #include <linux/module.h> |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 24 | #include <linux/sched.h> |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 25 | #include <linux/iommu.h> |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 26 | #include <linux/wait.h> |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 27 | #include <linux/pci.h> |
| 28 | #include <linux/gfp.h> |
| 29 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 30 | #include "amd_iommu_types.h" |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 31 | #include "amd_iommu_proto.h" |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 32 | |
| 33 | MODULE_LICENSE("GPL v2"); |
| 34 | MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>"); |
| 35 | |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 36 | #define MAX_DEVICES 0x10000 |
| 37 | #define PRI_QUEUE_SIZE 512 |
| 38 | |
| 39 | struct pri_queue { |
| 40 | atomic_t inflight; |
| 41 | bool finish; |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 42 | int status; |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct pasid_state { |
| 46 | struct list_head list; /* For global state-list */ |
| 47 | atomic_t count; /* Reference count */ |
| 48 | struct task_struct *task; /* Task bound to this PASID */ |
| 49 | struct mm_struct *mm; /* mm_struct for the faults */ |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 50 | struct mmu_notifier mn; /* mmu_otifier handle */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 51 | struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */ |
| 52 | struct device_state *device_state; /* Link to our device_state */ |
| 53 | int pasid; /* PASID index */ |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 54 | spinlock_t lock; /* Protect pri_queues */ |
| 55 | wait_queue_head_t wq; /* To wait for count == 0 */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct device_state { |
| 59 | atomic_t count; |
| 60 | struct pci_dev *pdev; |
| 61 | struct pasid_state **states; |
| 62 | struct iommu_domain *domain; |
| 63 | int pasid_levels; |
| 64 | int max_pasids; |
Joerg Roedel | 175d614 | 2011-11-28 14:36:36 +0100 | [diff] [blame] | 65 | amd_iommu_invalid_ppr_cb inv_ppr_cb; |
Joerg Roedel | bc21662 | 2011-12-07 12:24:42 +0100 | [diff] [blame] | 66 | amd_iommu_invalidate_ctx inv_ctx_cb; |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 67 | spinlock_t lock; |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 68 | wait_queue_head_t wq; |
| 69 | }; |
| 70 | |
| 71 | struct fault { |
| 72 | struct work_struct work; |
| 73 | struct device_state *dev_state; |
| 74 | struct pasid_state *state; |
| 75 | struct mm_struct *mm; |
| 76 | u64 address; |
| 77 | u16 devid; |
| 78 | u16 pasid; |
| 79 | u16 tag; |
| 80 | u16 finish; |
| 81 | u16 flags; |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 84 | static struct device_state **state_table; |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 85 | static spinlock_t state_lock; |
| 86 | |
| 87 | /* List and lock for all pasid_states */ |
| 88 | static LIST_HEAD(pasid_state_list); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 89 | static DEFINE_SPINLOCK(ps_lock); |
| 90 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 91 | static struct workqueue_struct *iommu_wq; |
| 92 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Empty page table - Used between |
| 95 | * mmu_notifier_invalidate_range_start and |
| 96 | * mmu_notifier_invalidate_range_end |
| 97 | */ |
| 98 | static u64 *empty_page_table; |
| 99 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 100 | static void free_pasid_states(struct device_state *dev_state); |
| 101 | static void unbind_pasid(struct device_state *dev_state, int pasid); |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 102 | static int task_exit(struct notifier_block *nb, unsigned long e, void *data); |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 103 | |
| 104 | static u16 device_id(struct pci_dev *pdev) |
| 105 | { |
| 106 | u16 devid; |
| 107 | |
| 108 | devid = pdev->bus->number; |
| 109 | devid = (devid << 8) | pdev->devfn; |
| 110 | |
| 111 | return devid; |
| 112 | } |
| 113 | |
| 114 | static struct device_state *get_device_state(u16 devid) |
| 115 | { |
| 116 | struct device_state *dev_state; |
| 117 | unsigned long flags; |
| 118 | |
| 119 | spin_lock_irqsave(&state_lock, flags); |
| 120 | dev_state = state_table[devid]; |
| 121 | if (dev_state != NULL) |
| 122 | atomic_inc(&dev_state->count); |
| 123 | spin_unlock_irqrestore(&state_lock, flags); |
| 124 | |
| 125 | return dev_state; |
| 126 | } |
| 127 | |
| 128 | static void free_device_state(struct device_state *dev_state) |
| 129 | { |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 130 | /* |
| 131 | * First detach device from domain - No more PRI requests will arrive |
| 132 | * from that device after it is unbound from the IOMMUv2 domain. |
| 133 | */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 134 | iommu_detach_device(dev_state->domain, &dev_state->pdev->dev); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 135 | |
| 136 | /* Everything is down now, free the IOMMUv2 domain */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 137 | iommu_domain_free(dev_state->domain); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 138 | |
| 139 | /* Finally get rid of the device-state */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 140 | kfree(dev_state); |
| 141 | } |
| 142 | |
| 143 | static void put_device_state(struct device_state *dev_state) |
| 144 | { |
| 145 | if (atomic_dec_and_test(&dev_state->count)) |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 146 | wake_up(&dev_state->wq); |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 149 | static void put_device_state_wait(struct device_state *dev_state) |
| 150 | { |
| 151 | DEFINE_WAIT(wait); |
| 152 | |
| 153 | prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE); |
| 154 | if (!atomic_dec_and_test(&dev_state->count)) |
| 155 | schedule(); |
| 156 | finish_wait(&dev_state->wq, &wait); |
| 157 | |
| 158 | free_device_state(dev_state); |
| 159 | } |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 160 | |
| 161 | static struct notifier_block profile_nb = { |
| 162 | .notifier_call = task_exit, |
| 163 | }; |
| 164 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 165 | static void link_pasid_state(struct pasid_state *pasid_state) |
| 166 | { |
| 167 | spin_lock(&ps_lock); |
| 168 | list_add_tail(&pasid_state->list, &pasid_state_list); |
| 169 | spin_unlock(&ps_lock); |
| 170 | } |
| 171 | |
| 172 | static void __unlink_pasid_state(struct pasid_state *pasid_state) |
| 173 | { |
| 174 | list_del(&pasid_state->list); |
| 175 | } |
| 176 | |
| 177 | static void unlink_pasid_state(struct pasid_state *pasid_state) |
| 178 | { |
| 179 | spin_lock(&ps_lock); |
| 180 | __unlink_pasid_state(pasid_state); |
| 181 | spin_unlock(&ps_lock); |
| 182 | } |
| 183 | |
| 184 | /* Must be called under dev_state->lock */ |
| 185 | static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state, |
| 186 | int pasid, bool alloc) |
| 187 | { |
| 188 | struct pasid_state **root, **ptr; |
| 189 | int level, index; |
| 190 | |
| 191 | level = dev_state->pasid_levels; |
| 192 | root = dev_state->states; |
| 193 | |
| 194 | while (true) { |
| 195 | |
| 196 | index = (pasid >> (9 * level)) & 0x1ff; |
| 197 | ptr = &root[index]; |
| 198 | |
| 199 | if (level == 0) |
| 200 | break; |
| 201 | |
| 202 | if (*ptr == NULL) { |
| 203 | if (!alloc) |
| 204 | return NULL; |
| 205 | |
| 206 | *ptr = (void *)get_zeroed_page(GFP_ATOMIC); |
| 207 | if (*ptr == NULL) |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | root = (struct pasid_state **)*ptr; |
| 212 | level -= 1; |
| 213 | } |
| 214 | |
| 215 | return ptr; |
| 216 | } |
| 217 | |
| 218 | static int set_pasid_state(struct device_state *dev_state, |
| 219 | struct pasid_state *pasid_state, |
| 220 | int pasid) |
| 221 | { |
| 222 | struct pasid_state **ptr; |
| 223 | unsigned long flags; |
| 224 | int ret; |
| 225 | |
| 226 | spin_lock_irqsave(&dev_state->lock, flags); |
| 227 | ptr = __get_pasid_state_ptr(dev_state, pasid, true); |
| 228 | |
| 229 | ret = -ENOMEM; |
| 230 | if (ptr == NULL) |
| 231 | goto out_unlock; |
| 232 | |
| 233 | ret = -ENOMEM; |
| 234 | if (*ptr != NULL) |
| 235 | goto out_unlock; |
| 236 | |
| 237 | *ptr = pasid_state; |
| 238 | |
| 239 | ret = 0; |
| 240 | |
| 241 | out_unlock: |
| 242 | spin_unlock_irqrestore(&dev_state->lock, flags); |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static void clear_pasid_state(struct device_state *dev_state, int pasid) |
| 248 | { |
| 249 | struct pasid_state **ptr; |
| 250 | unsigned long flags; |
| 251 | |
| 252 | spin_lock_irqsave(&dev_state->lock, flags); |
| 253 | ptr = __get_pasid_state_ptr(dev_state, pasid, true); |
| 254 | |
| 255 | if (ptr == NULL) |
| 256 | goto out_unlock; |
| 257 | |
| 258 | *ptr = NULL; |
| 259 | |
| 260 | out_unlock: |
| 261 | spin_unlock_irqrestore(&dev_state->lock, flags); |
| 262 | } |
| 263 | |
| 264 | static struct pasid_state *get_pasid_state(struct device_state *dev_state, |
| 265 | int pasid) |
| 266 | { |
| 267 | struct pasid_state **ptr, *ret = NULL; |
| 268 | unsigned long flags; |
| 269 | |
| 270 | spin_lock_irqsave(&dev_state->lock, flags); |
| 271 | ptr = __get_pasid_state_ptr(dev_state, pasid, false); |
| 272 | |
| 273 | if (ptr == NULL) |
| 274 | goto out_unlock; |
| 275 | |
| 276 | ret = *ptr; |
| 277 | if (ret) |
| 278 | atomic_inc(&ret->count); |
| 279 | |
| 280 | out_unlock: |
| 281 | spin_unlock_irqrestore(&dev_state->lock, flags); |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static void free_pasid_state(struct pasid_state *pasid_state) |
| 287 | { |
| 288 | kfree(pasid_state); |
| 289 | } |
| 290 | |
| 291 | static void put_pasid_state(struct pasid_state *pasid_state) |
| 292 | { |
| 293 | if (atomic_dec_and_test(&pasid_state->count)) { |
| 294 | put_device_state(pasid_state->device_state); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 295 | wake_up(&pasid_state->wq); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 299 | static void put_pasid_state_wait(struct pasid_state *pasid_state) |
| 300 | { |
| 301 | DEFINE_WAIT(wait); |
| 302 | |
| 303 | prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE); |
| 304 | |
| 305 | if (atomic_dec_and_test(&pasid_state->count)) |
| 306 | put_device_state(pasid_state->device_state); |
| 307 | else |
| 308 | schedule(); |
| 309 | |
| 310 | finish_wait(&pasid_state->wq, &wait); |
| 311 | mmput(pasid_state->mm); |
| 312 | free_pasid_state(pasid_state); |
| 313 | } |
| 314 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 315 | static void __unbind_pasid(struct pasid_state *pasid_state) |
| 316 | { |
| 317 | struct iommu_domain *domain; |
| 318 | |
| 319 | domain = pasid_state->device_state->domain; |
| 320 | |
| 321 | amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid); |
| 322 | clear_pasid_state(pasid_state->device_state, pasid_state->pasid); |
| 323 | |
| 324 | /* Make sure no more pending faults are in the queue */ |
| 325 | flush_workqueue(iommu_wq); |
| 326 | |
| 327 | mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm); |
| 328 | |
| 329 | put_pasid_state(pasid_state); /* Reference taken in bind() function */ |
| 330 | } |
| 331 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 332 | static void unbind_pasid(struct device_state *dev_state, int pasid) |
| 333 | { |
| 334 | struct pasid_state *pasid_state; |
| 335 | |
| 336 | pasid_state = get_pasid_state(dev_state, pasid); |
| 337 | if (pasid_state == NULL) |
| 338 | return; |
| 339 | |
| 340 | unlink_pasid_state(pasid_state); |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 341 | __unbind_pasid(pasid_state); |
| 342 | put_pasid_state_wait(pasid_state); /* Reference taken in this function */ |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void free_pasid_states_level1(struct pasid_state **tbl) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < 512; ++i) { |
| 350 | if (tbl[i] == NULL) |
| 351 | continue; |
| 352 | |
| 353 | free_page((unsigned long)tbl[i]); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static void free_pasid_states_level2(struct pasid_state **tbl) |
| 358 | { |
| 359 | struct pasid_state **ptr; |
| 360 | int i; |
| 361 | |
| 362 | for (i = 0; i < 512; ++i) { |
| 363 | if (tbl[i] == NULL) |
| 364 | continue; |
| 365 | |
| 366 | ptr = (struct pasid_state **)tbl[i]; |
| 367 | free_pasid_states_level1(ptr); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void free_pasid_states(struct device_state *dev_state) |
| 372 | { |
| 373 | struct pasid_state *pasid_state; |
| 374 | int i; |
| 375 | |
| 376 | for (i = 0; i < dev_state->max_pasids; ++i) { |
| 377 | pasid_state = get_pasid_state(dev_state, i); |
| 378 | if (pasid_state == NULL) |
| 379 | continue; |
| 380 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 381 | put_pasid_state(pasid_state); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 382 | unbind_pasid(dev_state, i); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (dev_state->pasid_levels == 2) |
| 386 | free_pasid_states_level2(dev_state->states); |
| 387 | else if (dev_state->pasid_levels == 1) |
| 388 | free_pasid_states_level1(dev_state->states); |
| 389 | else if (dev_state->pasid_levels != 0) |
| 390 | BUG(); |
| 391 | |
| 392 | free_page((unsigned long)dev_state->states); |
| 393 | } |
| 394 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 395 | static struct pasid_state *mn_to_state(struct mmu_notifier *mn) |
| 396 | { |
| 397 | return container_of(mn, struct pasid_state, mn); |
| 398 | } |
| 399 | |
| 400 | static void __mn_flush_page(struct mmu_notifier *mn, |
| 401 | unsigned long address) |
| 402 | { |
| 403 | struct pasid_state *pasid_state; |
| 404 | struct device_state *dev_state; |
| 405 | |
| 406 | pasid_state = mn_to_state(mn); |
| 407 | dev_state = pasid_state->device_state; |
| 408 | |
| 409 | amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address); |
| 410 | } |
| 411 | |
| 412 | static int mn_clear_flush_young(struct mmu_notifier *mn, |
| 413 | struct mm_struct *mm, |
| 414 | unsigned long address) |
| 415 | { |
| 416 | __mn_flush_page(mn, address); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void mn_change_pte(struct mmu_notifier *mn, |
| 422 | struct mm_struct *mm, |
| 423 | unsigned long address, |
| 424 | pte_t pte) |
| 425 | { |
| 426 | __mn_flush_page(mn, address); |
| 427 | } |
| 428 | |
| 429 | static void mn_invalidate_page(struct mmu_notifier *mn, |
| 430 | struct mm_struct *mm, |
| 431 | unsigned long address) |
| 432 | { |
| 433 | __mn_flush_page(mn, address); |
| 434 | } |
| 435 | |
| 436 | static void mn_invalidate_range_start(struct mmu_notifier *mn, |
| 437 | struct mm_struct *mm, |
| 438 | unsigned long start, unsigned long end) |
| 439 | { |
| 440 | struct pasid_state *pasid_state; |
| 441 | struct device_state *dev_state; |
| 442 | |
| 443 | pasid_state = mn_to_state(mn); |
| 444 | dev_state = pasid_state->device_state; |
| 445 | |
| 446 | amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid, |
| 447 | __pa(empty_page_table)); |
| 448 | } |
| 449 | |
| 450 | static void mn_invalidate_range_end(struct mmu_notifier *mn, |
| 451 | struct mm_struct *mm, |
| 452 | unsigned long start, unsigned long end) |
| 453 | { |
| 454 | struct pasid_state *pasid_state; |
| 455 | struct device_state *dev_state; |
| 456 | |
| 457 | pasid_state = mn_to_state(mn); |
| 458 | dev_state = pasid_state->device_state; |
| 459 | |
| 460 | amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid, |
| 461 | __pa(pasid_state->mm->pgd)); |
| 462 | } |
| 463 | |
| 464 | static struct mmu_notifier_ops iommu_mn = { |
| 465 | .clear_flush_young = mn_clear_flush_young, |
| 466 | .change_pte = mn_change_pte, |
| 467 | .invalidate_page = mn_invalidate_page, |
| 468 | .invalidate_range_start = mn_invalidate_range_start, |
| 469 | .invalidate_range_end = mn_invalidate_range_end, |
| 470 | }; |
| 471 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 472 | static void set_pri_tag_status(struct pasid_state *pasid_state, |
| 473 | u16 tag, int status) |
| 474 | { |
| 475 | unsigned long flags; |
| 476 | |
| 477 | spin_lock_irqsave(&pasid_state->lock, flags); |
| 478 | pasid_state->pri[tag].status = status; |
| 479 | spin_unlock_irqrestore(&pasid_state->lock, flags); |
| 480 | } |
| 481 | |
| 482 | static void finish_pri_tag(struct device_state *dev_state, |
| 483 | struct pasid_state *pasid_state, |
| 484 | u16 tag) |
| 485 | { |
| 486 | unsigned long flags; |
| 487 | |
| 488 | spin_lock_irqsave(&pasid_state->lock, flags); |
| 489 | if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) && |
| 490 | pasid_state->pri[tag].finish) { |
| 491 | amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid, |
| 492 | pasid_state->pri[tag].status, tag); |
| 493 | pasid_state->pri[tag].finish = false; |
| 494 | pasid_state->pri[tag].status = PPR_SUCCESS; |
| 495 | } |
| 496 | spin_unlock_irqrestore(&pasid_state->lock, flags); |
| 497 | } |
| 498 | |
| 499 | static void do_fault(struct work_struct *work) |
| 500 | { |
| 501 | struct fault *fault = container_of(work, struct fault, work); |
| 502 | int npages, write; |
| 503 | struct page *page; |
| 504 | |
| 505 | write = !!(fault->flags & PPR_FAULT_WRITE); |
| 506 | |
Jay Cornwall | 4378d99 | 2014-04-28 17:27:46 -0500 | [diff] [blame] | 507 | down_read(&fault->state->mm->mmap_sem); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 508 | npages = get_user_pages(fault->state->task, fault->state->mm, |
| 509 | fault->address, 1, write, 0, &page, NULL); |
Jay Cornwall | 4378d99 | 2014-04-28 17:27:46 -0500 | [diff] [blame] | 510 | up_read(&fault->state->mm->mmap_sem); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 511 | |
Joerg Roedel | 175d614 | 2011-11-28 14:36:36 +0100 | [diff] [blame] | 512 | if (npages == 1) { |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 513 | put_page(page); |
Joerg Roedel | 175d614 | 2011-11-28 14:36:36 +0100 | [diff] [blame] | 514 | } else if (fault->dev_state->inv_ppr_cb) { |
| 515 | int status; |
| 516 | |
| 517 | status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev, |
| 518 | fault->pasid, |
| 519 | fault->address, |
| 520 | fault->flags); |
| 521 | switch (status) { |
| 522 | case AMD_IOMMU_INV_PRI_RSP_SUCCESS: |
| 523 | set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS); |
| 524 | break; |
| 525 | case AMD_IOMMU_INV_PRI_RSP_INVALID: |
| 526 | set_pri_tag_status(fault->state, fault->tag, PPR_INVALID); |
| 527 | break; |
| 528 | case AMD_IOMMU_INV_PRI_RSP_FAIL: |
| 529 | set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE); |
| 530 | break; |
| 531 | default: |
| 532 | BUG(); |
| 533 | } |
| 534 | } else { |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 535 | set_pri_tag_status(fault->state, fault->tag, PPR_INVALID); |
Joerg Roedel | 175d614 | 2011-11-28 14:36:36 +0100 | [diff] [blame] | 536 | } |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 537 | |
| 538 | finish_pri_tag(fault->dev_state, fault->state, fault->tag); |
| 539 | |
| 540 | put_pasid_state(fault->state); |
| 541 | |
| 542 | kfree(fault); |
| 543 | } |
| 544 | |
| 545 | static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data) |
| 546 | { |
| 547 | struct amd_iommu_fault *iommu_fault; |
| 548 | struct pasid_state *pasid_state; |
| 549 | struct device_state *dev_state; |
| 550 | unsigned long flags; |
| 551 | struct fault *fault; |
| 552 | bool finish; |
| 553 | u16 tag; |
| 554 | int ret; |
| 555 | |
| 556 | iommu_fault = data; |
| 557 | tag = iommu_fault->tag & 0x1ff; |
| 558 | finish = (iommu_fault->tag >> 9) & 1; |
| 559 | |
| 560 | ret = NOTIFY_DONE; |
| 561 | dev_state = get_device_state(iommu_fault->device_id); |
| 562 | if (dev_state == NULL) |
| 563 | goto out; |
| 564 | |
| 565 | pasid_state = get_pasid_state(dev_state, iommu_fault->pasid); |
| 566 | if (pasid_state == NULL) { |
| 567 | /* We know the device but not the PASID -> send INVALID */ |
| 568 | amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid, |
| 569 | PPR_INVALID, tag); |
| 570 | goto out_drop_state; |
| 571 | } |
| 572 | |
| 573 | spin_lock_irqsave(&pasid_state->lock, flags); |
| 574 | atomic_inc(&pasid_state->pri[tag].inflight); |
| 575 | if (finish) |
| 576 | pasid_state->pri[tag].finish = true; |
| 577 | spin_unlock_irqrestore(&pasid_state->lock, flags); |
| 578 | |
| 579 | fault = kzalloc(sizeof(*fault), GFP_ATOMIC); |
| 580 | if (fault == NULL) { |
| 581 | /* We are OOM - send success and let the device re-fault */ |
| 582 | finish_pri_tag(dev_state, pasid_state, tag); |
| 583 | goto out_drop_state; |
| 584 | } |
| 585 | |
| 586 | fault->dev_state = dev_state; |
| 587 | fault->address = iommu_fault->address; |
| 588 | fault->state = pasid_state; |
| 589 | fault->tag = tag; |
| 590 | fault->finish = finish; |
| 591 | fault->flags = iommu_fault->flags; |
| 592 | INIT_WORK(&fault->work, do_fault); |
| 593 | |
| 594 | queue_work(iommu_wq, &fault->work); |
| 595 | |
| 596 | ret = NOTIFY_OK; |
| 597 | |
| 598 | out_drop_state: |
| 599 | put_device_state(dev_state); |
| 600 | |
| 601 | out: |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static struct notifier_block ppr_nb = { |
| 606 | .notifier_call = ppr_notifier, |
| 607 | }; |
| 608 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 609 | static int task_exit(struct notifier_block *nb, unsigned long e, void *data) |
| 610 | { |
| 611 | struct pasid_state *pasid_state; |
| 612 | struct task_struct *task; |
| 613 | |
| 614 | task = data; |
| 615 | |
| 616 | /* |
| 617 | * Using this notifier is a hack - but there is no other choice |
| 618 | * at the moment. What I really want is a sleeping notifier that |
| 619 | * is called when an MM goes down. But such a notifier doesn't |
| 620 | * exist yet. The notifier needs to sleep because it has to make |
| 621 | * sure that the device does not use the PASID and the address |
| 622 | * space anymore before it is destroyed. This includes waiting |
| 623 | * for pending PRI requests to pass the workqueue. The |
| 624 | * MMU-Notifiers would be a good fit, but they use RCU and so |
| 625 | * they are not allowed to sleep. Lets see how we can solve this |
| 626 | * in a more intelligent way in the future. |
| 627 | */ |
| 628 | again: |
| 629 | spin_lock(&ps_lock); |
| 630 | list_for_each_entry(pasid_state, &pasid_state_list, list) { |
| 631 | struct device_state *dev_state; |
| 632 | int pasid; |
| 633 | |
| 634 | if (pasid_state->task != task) |
| 635 | continue; |
| 636 | |
| 637 | /* Drop Lock and unbind */ |
| 638 | spin_unlock(&ps_lock); |
| 639 | |
| 640 | dev_state = pasid_state->device_state; |
| 641 | pasid = pasid_state->pasid; |
| 642 | |
Joerg Roedel | bc21662 | 2011-12-07 12:24:42 +0100 | [diff] [blame] | 643 | if (pasid_state->device_state->inv_ctx_cb) |
| 644 | dev_state->inv_ctx_cb(dev_state->pdev, pasid); |
| 645 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 646 | unbind_pasid(dev_state, pasid); |
| 647 | |
| 648 | /* Task may be in the list multiple times */ |
| 649 | goto again; |
| 650 | } |
| 651 | spin_unlock(&ps_lock); |
| 652 | |
| 653 | return NOTIFY_OK; |
| 654 | } |
| 655 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 656 | int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid, |
| 657 | struct task_struct *task) |
| 658 | { |
| 659 | struct pasid_state *pasid_state; |
| 660 | struct device_state *dev_state; |
| 661 | u16 devid; |
| 662 | int ret; |
| 663 | |
| 664 | might_sleep(); |
| 665 | |
| 666 | if (!amd_iommu_v2_supported()) |
| 667 | return -ENODEV; |
| 668 | |
| 669 | devid = device_id(pdev); |
| 670 | dev_state = get_device_state(devid); |
| 671 | |
| 672 | if (dev_state == NULL) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | ret = -EINVAL; |
| 676 | if (pasid < 0 || pasid >= dev_state->max_pasids) |
| 677 | goto out; |
| 678 | |
| 679 | ret = -ENOMEM; |
| 680 | pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL); |
| 681 | if (pasid_state == NULL) |
| 682 | goto out; |
| 683 | |
| 684 | atomic_set(&pasid_state->count, 1); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 685 | init_waitqueue_head(&pasid_state->wq); |
Joerg Roedel | 2c13d47 | 2012-07-19 10:56:10 +0200 | [diff] [blame] | 686 | spin_lock_init(&pasid_state->lock); |
| 687 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 688 | pasid_state->task = task; |
| 689 | pasid_state->mm = get_task_mm(task); |
| 690 | pasid_state->device_state = dev_state; |
| 691 | pasid_state->pasid = pasid; |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 692 | pasid_state->mn.ops = &iommu_mn; |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 693 | |
| 694 | if (pasid_state->mm == NULL) |
| 695 | goto out_free; |
| 696 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 697 | mmu_notifier_register(&pasid_state->mn, pasid_state->mm); |
| 698 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 699 | ret = set_pasid_state(dev_state, pasid_state, pasid); |
| 700 | if (ret) |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 701 | goto out_unregister; |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 702 | |
| 703 | ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid, |
| 704 | __pa(pasid_state->mm->pgd)); |
| 705 | if (ret) |
| 706 | goto out_clear_state; |
| 707 | |
| 708 | link_pasid_state(pasid_state); |
| 709 | |
| 710 | return 0; |
| 711 | |
| 712 | out_clear_state: |
| 713 | clear_pasid_state(dev_state, pasid); |
| 714 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 715 | out_unregister: |
| 716 | mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm); |
| 717 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 718 | out_free: |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 719 | free_pasid_state(pasid_state); |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 720 | |
| 721 | out: |
| 722 | put_device_state(dev_state); |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | EXPORT_SYMBOL(amd_iommu_bind_pasid); |
| 727 | |
| 728 | void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid) |
| 729 | { |
| 730 | struct device_state *dev_state; |
| 731 | u16 devid; |
| 732 | |
| 733 | might_sleep(); |
| 734 | |
| 735 | if (!amd_iommu_v2_supported()) |
| 736 | return; |
| 737 | |
| 738 | devid = device_id(pdev); |
| 739 | dev_state = get_device_state(devid); |
| 740 | if (dev_state == NULL) |
| 741 | return; |
| 742 | |
| 743 | if (pasid < 0 || pasid >= dev_state->max_pasids) |
| 744 | goto out; |
| 745 | |
| 746 | unbind_pasid(dev_state, pasid); |
| 747 | |
| 748 | out: |
| 749 | put_device_state(dev_state); |
| 750 | } |
| 751 | EXPORT_SYMBOL(amd_iommu_unbind_pasid); |
| 752 | |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 753 | int amd_iommu_init_device(struct pci_dev *pdev, int pasids) |
| 754 | { |
| 755 | struct device_state *dev_state; |
| 756 | unsigned long flags; |
| 757 | int ret, tmp; |
| 758 | u16 devid; |
| 759 | |
| 760 | might_sleep(); |
| 761 | |
| 762 | if (!amd_iommu_v2_supported()) |
| 763 | return -ENODEV; |
| 764 | |
| 765 | if (pasids <= 0 || pasids > (PASID_MASK + 1)) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | devid = device_id(pdev); |
| 769 | |
| 770 | dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL); |
| 771 | if (dev_state == NULL) |
| 772 | return -ENOMEM; |
| 773 | |
| 774 | spin_lock_init(&dev_state->lock); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 775 | init_waitqueue_head(&dev_state->wq); |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 776 | dev_state->pdev = pdev; |
| 777 | |
| 778 | tmp = pasids; |
| 779 | for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9) |
| 780 | dev_state->pasid_levels += 1; |
| 781 | |
| 782 | atomic_set(&dev_state->count, 1); |
| 783 | dev_state->max_pasids = pasids; |
| 784 | |
| 785 | ret = -ENOMEM; |
| 786 | dev_state->states = (void *)get_zeroed_page(GFP_KERNEL); |
| 787 | if (dev_state->states == NULL) |
| 788 | goto out_free_dev_state; |
| 789 | |
| 790 | dev_state->domain = iommu_domain_alloc(&pci_bus_type); |
| 791 | if (dev_state->domain == NULL) |
| 792 | goto out_free_states; |
| 793 | |
| 794 | amd_iommu_domain_direct_map(dev_state->domain); |
| 795 | |
| 796 | ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids); |
| 797 | if (ret) |
| 798 | goto out_free_domain; |
| 799 | |
| 800 | ret = iommu_attach_device(dev_state->domain, &pdev->dev); |
| 801 | if (ret != 0) |
| 802 | goto out_free_domain; |
| 803 | |
| 804 | spin_lock_irqsave(&state_lock, flags); |
| 805 | |
| 806 | if (state_table[devid] != NULL) { |
| 807 | spin_unlock_irqrestore(&state_lock, flags); |
| 808 | ret = -EBUSY; |
| 809 | goto out_free_domain; |
| 810 | } |
| 811 | |
| 812 | state_table[devid] = dev_state; |
| 813 | |
| 814 | spin_unlock_irqrestore(&state_lock, flags); |
| 815 | |
| 816 | return 0; |
| 817 | |
| 818 | out_free_domain: |
| 819 | iommu_domain_free(dev_state->domain); |
| 820 | |
| 821 | out_free_states: |
| 822 | free_page((unsigned long)dev_state->states); |
| 823 | |
| 824 | out_free_dev_state: |
| 825 | kfree(dev_state); |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | EXPORT_SYMBOL(amd_iommu_init_device); |
| 830 | |
| 831 | void amd_iommu_free_device(struct pci_dev *pdev) |
| 832 | { |
| 833 | struct device_state *dev_state; |
| 834 | unsigned long flags; |
| 835 | u16 devid; |
| 836 | |
| 837 | if (!amd_iommu_v2_supported()) |
| 838 | return; |
| 839 | |
| 840 | devid = device_id(pdev); |
| 841 | |
| 842 | spin_lock_irqsave(&state_lock, flags); |
| 843 | |
| 844 | dev_state = state_table[devid]; |
| 845 | if (dev_state == NULL) { |
| 846 | spin_unlock_irqrestore(&state_lock, flags); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | state_table[devid] = NULL; |
| 851 | |
| 852 | spin_unlock_irqrestore(&state_lock, flags); |
| 853 | |
Joerg Roedel | 2d5503b | 2011-11-24 10:41:57 +0100 | [diff] [blame] | 854 | /* Get rid of any remaining pasid states */ |
| 855 | free_pasid_states(dev_state); |
| 856 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 857 | put_device_state_wait(dev_state); |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 858 | } |
| 859 | EXPORT_SYMBOL(amd_iommu_free_device); |
| 860 | |
Joerg Roedel | 175d614 | 2011-11-28 14:36:36 +0100 | [diff] [blame] | 861 | int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev, |
| 862 | amd_iommu_invalid_ppr_cb cb) |
| 863 | { |
| 864 | struct device_state *dev_state; |
| 865 | unsigned long flags; |
| 866 | u16 devid; |
| 867 | int ret; |
| 868 | |
| 869 | if (!amd_iommu_v2_supported()) |
| 870 | return -ENODEV; |
| 871 | |
| 872 | devid = device_id(pdev); |
| 873 | |
| 874 | spin_lock_irqsave(&state_lock, flags); |
| 875 | |
| 876 | ret = -EINVAL; |
| 877 | dev_state = state_table[devid]; |
| 878 | if (dev_state == NULL) |
| 879 | goto out_unlock; |
| 880 | |
| 881 | dev_state->inv_ppr_cb = cb; |
| 882 | |
| 883 | ret = 0; |
| 884 | |
| 885 | out_unlock: |
| 886 | spin_unlock_irqrestore(&state_lock, flags); |
| 887 | |
| 888 | return ret; |
| 889 | } |
| 890 | EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb); |
| 891 | |
Joerg Roedel | bc21662 | 2011-12-07 12:24:42 +0100 | [diff] [blame] | 892 | int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev, |
| 893 | amd_iommu_invalidate_ctx cb) |
| 894 | { |
| 895 | struct device_state *dev_state; |
| 896 | unsigned long flags; |
| 897 | u16 devid; |
| 898 | int ret; |
| 899 | |
| 900 | if (!amd_iommu_v2_supported()) |
| 901 | return -ENODEV; |
| 902 | |
| 903 | devid = device_id(pdev); |
| 904 | |
| 905 | spin_lock_irqsave(&state_lock, flags); |
| 906 | |
| 907 | ret = -EINVAL; |
| 908 | dev_state = state_table[devid]; |
| 909 | if (dev_state == NULL) |
| 910 | goto out_unlock; |
| 911 | |
| 912 | dev_state->inv_ctx_cb = cb; |
| 913 | |
| 914 | ret = 0; |
| 915 | |
| 916 | out_unlock: |
| 917 | spin_unlock_irqrestore(&state_lock, flags); |
| 918 | |
| 919 | return ret; |
| 920 | } |
| 921 | EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb); |
| 922 | |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 923 | static int __init amd_iommu_v2_init(void) |
| 924 | { |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 925 | size_t state_table_size; |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 926 | int ret; |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 927 | |
Joerg Roedel | 474d567d | 2012-03-15 12:46:40 +0100 | [diff] [blame] | 928 | pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n"); |
| 929 | |
| 930 | if (!amd_iommu_v2_supported()) { |
Masanari Iida | 07db040 | 2012-07-22 02:21:32 +0900 | [diff] [blame] | 931 | pr_info("AMD IOMMUv2 functionality not available on this system\n"); |
Joerg Roedel | 474d567d | 2012-03-15 12:46:40 +0100 | [diff] [blame] | 932 | /* |
| 933 | * Load anyway to provide the symbols to other modules |
| 934 | * which may use AMD IOMMUv2 optionally. |
| 935 | */ |
| 936 | return 0; |
| 937 | } |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 938 | |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 939 | spin_lock_init(&state_lock); |
| 940 | |
| 941 | state_table_size = MAX_DEVICES * sizeof(struct device_state *); |
| 942 | state_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 943 | get_order(state_table_size)); |
| 944 | if (state_table == NULL) |
| 945 | return -ENOMEM; |
| 946 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 947 | ret = -ENOMEM; |
| 948 | iommu_wq = create_workqueue("amd_iommu_v2"); |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 949 | if (iommu_wq == NULL) |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 950 | goto out_free; |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 951 | |
| 952 | ret = -ENOMEM; |
| 953 | empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL); |
| 954 | if (empty_page_table == NULL) |
| 955 | goto out_destroy_wq; |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 956 | |
| 957 | amd_iommu_register_ppr_notifier(&ppr_nb); |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 958 | profile_event_register(PROFILE_TASK_EXIT, &profile_nb); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 959 | |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 960 | return 0; |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 961 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 962 | out_destroy_wq: |
| 963 | destroy_workqueue(iommu_wq); |
| 964 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 965 | out_free: |
| 966 | free_pages((unsigned long)state_table, get_order(state_table_size)); |
| 967 | |
| 968 | return ret; |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static void __exit amd_iommu_v2_exit(void) |
| 972 | { |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 973 | struct device_state *dev_state; |
| 974 | size_t state_table_size; |
| 975 | int i; |
| 976 | |
Joerg Roedel | 474d567d | 2012-03-15 12:46:40 +0100 | [diff] [blame] | 977 | if (!amd_iommu_v2_supported()) |
| 978 | return; |
| 979 | |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 980 | profile_event_unregister(PROFILE_TASK_EXIT, &profile_nb); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 981 | amd_iommu_unregister_ppr_notifier(&ppr_nb); |
| 982 | |
| 983 | flush_workqueue(iommu_wq); |
| 984 | |
| 985 | /* |
| 986 | * The loop below might call flush_workqueue(), so call |
| 987 | * destroy_workqueue() after it |
| 988 | */ |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 989 | for (i = 0; i < MAX_DEVICES; ++i) { |
| 990 | dev_state = get_device_state(i); |
| 991 | |
| 992 | if (dev_state == NULL) |
| 993 | continue; |
| 994 | |
| 995 | WARN_ON_ONCE(1); |
| 996 | |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 997 | put_device_state(dev_state); |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 998 | amd_iommu_free_device(dev_state->pdev); |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Joerg Roedel | 028eeac | 2011-11-24 12:48:13 +0100 | [diff] [blame] | 1001 | destroy_workqueue(iommu_wq); |
| 1002 | |
Joerg Roedel | ed96f22 | 2011-11-23 17:30:39 +0100 | [diff] [blame] | 1003 | state_table_size = MAX_DEVICES * sizeof(struct device_state *); |
| 1004 | free_pages((unsigned long)state_table, get_order(state_table_size)); |
Joerg Roedel | 8736b2c | 2011-11-24 16:21:52 +0100 | [diff] [blame] | 1005 | |
| 1006 | free_page((unsigned long)empty_page_table); |
Joerg Roedel | e3c495c | 2011-11-09 12:31:15 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | module_init(amd_iommu_v2_init); |
| 1010 | module_exit(amd_iommu_v2_exit); |