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