blob: f7ca009bda670709ef629a1a92a420ca8ac7b480 [file] [log] [blame]
Joerg Roedele3c495c2011-11-09 12:31:15 +01001/*
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 Roedel8736b2c2011-11-24 16:21:52 +010019#include <linux/mmu_notifier.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010020#include <linux/amd-iommu.h>
21#include <linux/mm_types.h>
Joerg Roedel8736b2c2011-11-24 16:21:52 +010022#include <linux/profile.h>
Joerg Roedele3c495c2011-11-09 12:31:15 +010023#include <linux/module.h>
Joerg Roedel2d5503b2011-11-24 10:41:57 +010024#include <linux/sched.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010025#include <linux/iommu.h>
Joerg Roedel028eeac2011-11-24 12:48:13 +010026#include <linux/wait.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010027#include <linux/pci.h>
28#include <linux/gfp.h>
29
Joerg Roedel028eeac2011-11-24 12:48:13 +010030#include "amd_iommu_types.h"
Joerg Roedeled96f222011-11-23 17:30:39 +010031#include "amd_iommu_proto.h"
Joerg Roedele3c495c2011-11-09 12:31:15 +010032
33MODULE_LICENSE("GPL v2");
34MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>");
35
Joerg Roedeled96f222011-11-23 17:30:39 +010036#define MAX_DEVICES 0x10000
37#define PRI_QUEUE_SIZE 512
38
39struct pri_queue {
40 atomic_t inflight;
41 bool finish;
Joerg Roedel028eeac2011-11-24 12:48:13 +010042 int status;
Joerg Roedeled96f222011-11-23 17:30:39 +010043};
44
45struct pasid_state {
46 struct list_head list; /* For global state-list */
47 atomic_t count; /* Reference count */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020048 unsigned mmu_notifier_count; /* Counting nested mmu_notifier
Joerg Roedele79df312014-05-20 23:18:26 +020049 calls */
Joerg Roedeled96f222011-11-23 17:30:39 +010050 struct mm_struct *mm; /* mm_struct for the faults */
Joerg Roedelff6d0cc2014-07-08 12:49:50 +020051 struct mmu_notifier mn; /* mmu_notifier handle */
Joerg Roedeled96f222011-11-23 17:30:39 +010052 struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
53 struct device_state *device_state; /* Link to our device_state */
54 int pasid; /* PASID index */
Joerg Roedel53d340e2014-07-08 15:01:43 +020055 bool invalid; /* Used during teardown */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020056 spinlock_t lock; /* Protect pri_queues and
57 mmu_notifer_count */
Joerg Roedel028eeac2011-11-24 12:48:13 +010058 wait_queue_head_t wq; /* To wait for count == 0 */
Joerg Roedeled96f222011-11-23 17:30:39 +010059};
60
61struct device_state {
Joerg Roedel741669c2014-05-20 23:18:23 +020062 struct list_head list;
63 u16 devid;
Joerg Roedeled96f222011-11-23 17:30:39 +010064 atomic_t count;
65 struct pci_dev *pdev;
66 struct pasid_state **states;
67 struct iommu_domain *domain;
68 int pasid_levels;
69 int max_pasids;
Joerg Roedel175d6142011-11-28 14:36:36 +010070 amd_iommu_invalid_ppr_cb inv_ppr_cb;
Joerg Roedelbc216622011-12-07 12:24:42 +010071 amd_iommu_invalidate_ctx inv_ctx_cb;
Joerg Roedeled96f222011-11-23 17:30:39 +010072 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010073 wait_queue_head_t wq;
74};
75
76struct fault {
77 struct work_struct work;
78 struct device_state *dev_state;
79 struct pasid_state *state;
80 struct mm_struct *mm;
81 u64 address;
82 u16 devid;
83 u16 pasid;
84 u16 tag;
85 u16 finish;
86 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010087};
88
Joerg Roedel741669c2014-05-20 23:18:23 +020089static LIST_HEAD(state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +010090static spinlock_t state_lock;
91
Joerg Roedel028eeac2011-11-24 12:48:13 +010092static struct workqueue_struct *iommu_wq;
93
Joerg Roedel8736b2c2011-11-24 16:21:52 +010094/*
95 * Empty page table - Used between
96 * mmu_notifier_invalidate_range_start and
97 * mmu_notifier_invalidate_range_end
98 */
99static u64 *empty_page_table;
100
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100101static void free_pasid_states(struct device_state *dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100102
103static u16 device_id(struct pci_dev *pdev)
104{
105 u16 devid;
106
107 devid = pdev->bus->number;
108 devid = (devid << 8) | pdev->devfn;
109
110 return devid;
111}
112
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200113static struct device_state *__get_device_state(u16 devid)
114{
Joerg Roedel741669c2014-05-20 23:18:23 +0200115 struct device_state *dev_state;
116
117 list_for_each_entry(dev_state, &state_list, list) {
118 if (dev_state->devid == devid)
119 return dev_state;
120 }
121
122 return NULL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200123}
124
Joerg Roedeled96f222011-11-23 17:30:39 +0100125static struct device_state *get_device_state(u16 devid)
126{
127 struct device_state *dev_state;
128 unsigned long flags;
129
130 spin_lock_irqsave(&state_lock, flags);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200131 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100132 if (dev_state != NULL)
133 atomic_inc(&dev_state->count);
134 spin_unlock_irqrestore(&state_lock, flags);
135
136 return dev_state;
137}
138
139static void free_device_state(struct device_state *dev_state)
140{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100141 /*
142 * First detach device from domain - No more PRI requests will arrive
143 * from that device after it is unbound from the IOMMUv2 domain.
144 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100145 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100146
147 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100148 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100149
150 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100151 kfree(dev_state);
152}
153
154static void put_device_state(struct device_state *dev_state)
155{
156 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100157 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100158}
159
Joerg Roedel028eeac2011-11-24 12:48:13 +0100160static void put_device_state_wait(struct device_state *dev_state)
161{
162 DEFINE_WAIT(wait);
163
164 prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
165 if (!atomic_dec_and_test(&dev_state->count))
166 schedule();
167 finish_wait(&dev_state->wq, &wait);
168
169 free_device_state(dev_state);
170}
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100171
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100172/* Must be called under dev_state->lock */
173static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
174 int pasid, bool alloc)
175{
176 struct pasid_state **root, **ptr;
177 int level, index;
178
179 level = dev_state->pasid_levels;
180 root = dev_state->states;
181
182 while (true) {
183
184 index = (pasid >> (9 * level)) & 0x1ff;
185 ptr = &root[index];
186
187 if (level == 0)
188 break;
189
190 if (*ptr == NULL) {
191 if (!alloc)
192 return NULL;
193
194 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
195 if (*ptr == NULL)
196 return NULL;
197 }
198
199 root = (struct pasid_state **)*ptr;
200 level -= 1;
201 }
202
203 return ptr;
204}
205
206static int set_pasid_state(struct device_state *dev_state,
207 struct pasid_state *pasid_state,
208 int pasid)
209{
210 struct pasid_state **ptr;
211 unsigned long flags;
212 int ret;
213
214 spin_lock_irqsave(&dev_state->lock, flags);
215 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
216
217 ret = -ENOMEM;
218 if (ptr == NULL)
219 goto out_unlock;
220
221 ret = -ENOMEM;
222 if (*ptr != NULL)
223 goto out_unlock;
224
225 *ptr = pasid_state;
226
227 ret = 0;
228
229out_unlock:
230 spin_unlock_irqrestore(&dev_state->lock, flags);
231
232 return ret;
233}
234
235static void clear_pasid_state(struct device_state *dev_state, int pasid)
236{
237 struct pasid_state **ptr;
238 unsigned long flags;
239
240 spin_lock_irqsave(&dev_state->lock, flags);
241 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
242
243 if (ptr == NULL)
244 goto out_unlock;
245
246 *ptr = NULL;
247
248out_unlock:
249 spin_unlock_irqrestore(&dev_state->lock, flags);
250}
251
252static struct pasid_state *get_pasid_state(struct device_state *dev_state,
253 int pasid)
254{
255 struct pasid_state **ptr, *ret = NULL;
256 unsigned long flags;
257
258 spin_lock_irqsave(&dev_state->lock, flags);
259 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
260
261 if (ptr == NULL)
262 goto out_unlock;
263
264 ret = *ptr;
265 if (ret)
266 atomic_inc(&ret->count);
267
268out_unlock:
269 spin_unlock_irqrestore(&dev_state->lock, flags);
270
271 return ret;
272}
273
274static void free_pasid_state(struct pasid_state *pasid_state)
275{
276 kfree(pasid_state);
277}
278
279static void put_pasid_state(struct pasid_state *pasid_state)
280{
281 if (atomic_dec_and_test(&pasid_state->count)) {
282 put_device_state(pasid_state->device_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100283 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100284 }
285}
286
Joerg Roedel028eeac2011-11-24 12:48:13 +0100287static void put_pasid_state_wait(struct pasid_state *pasid_state)
288{
289 DEFINE_WAIT(wait);
290
291 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
292
293 if (atomic_dec_and_test(&pasid_state->count))
294 put_device_state(pasid_state->device_state);
295 else
296 schedule();
297
298 finish_wait(&pasid_state->wq, &wait);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100299 free_pasid_state(pasid_state);
300}
301
Joerg Roedel61feb432014-07-08 14:19:35 +0200302static void unbind_pasid(struct pasid_state *pasid_state)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100303{
304 struct iommu_domain *domain;
305
306 domain = pasid_state->device_state->domain;
307
Joerg Roedel53d340e2014-07-08 15:01:43 +0200308 /*
309 * Mark pasid_state as invalid, no more faults will we added to the
310 * work queue after this is visible everywhere.
311 */
312 pasid_state->invalid = true;
313
314 /* Make sure this is visible */
315 smp_wmb();
316
317 /* After this the device/pasid can't access the mm anymore */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100318 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100319
320 /* Make sure no more pending faults are in the queue */
321 flush_workqueue(iommu_wq);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200322
323 /*
324 * No more faults are in the work queue and no new faults will be queued
325 * from here on. We can safely set pasid_state->mm to NULL now as the
326 * mm_struct might go away after we return.
327 */
328 pasid_state->mm = NULL;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100329}
330
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100331static void free_pasid_states_level1(struct pasid_state **tbl)
332{
333 int i;
334
335 for (i = 0; i < 512; ++i) {
336 if (tbl[i] == NULL)
337 continue;
338
339 free_page((unsigned long)tbl[i]);
340 }
341}
342
343static void free_pasid_states_level2(struct pasid_state **tbl)
344{
345 struct pasid_state **ptr;
346 int i;
347
348 for (i = 0; i < 512; ++i) {
349 if (tbl[i] == NULL)
350 continue;
351
352 ptr = (struct pasid_state **)tbl[i];
353 free_pasid_states_level1(ptr);
354 }
355}
356
357static void free_pasid_states(struct device_state *dev_state)
358{
359 struct pasid_state *pasid_state;
360 int i;
361
362 for (i = 0; i < dev_state->max_pasids; ++i) {
363 pasid_state = get_pasid_state(dev_state, i);
364 if (pasid_state == NULL)
365 continue;
366
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100367 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200368
369 /*
370 * This will call the mn_release function and
371 * unbind the PASID
372 */
373 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200374
375 put_pasid_state_wait(pasid_state); /* Reference taken in
376 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100377 }
378
379 if (dev_state->pasid_levels == 2)
380 free_pasid_states_level2(dev_state->states);
381 else if (dev_state->pasid_levels == 1)
382 free_pasid_states_level1(dev_state->states);
383 else if (dev_state->pasid_levels != 0)
384 BUG();
385
386 free_page((unsigned long)dev_state->states);
387}
388
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100389static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
390{
391 return container_of(mn, struct pasid_state, mn);
392}
393
394static void __mn_flush_page(struct mmu_notifier *mn,
395 unsigned long address)
396{
397 struct pasid_state *pasid_state;
398 struct device_state *dev_state;
399
400 pasid_state = mn_to_state(mn);
401 dev_state = pasid_state->device_state;
402
403 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
404}
405
406static int mn_clear_flush_young(struct mmu_notifier *mn,
407 struct mm_struct *mm,
408 unsigned long address)
409{
410 __mn_flush_page(mn, address);
411
412 return 0;
413}
414
415static void mn_change_pte(struct mmu_notifier *mn,
416 struct mm_struct *mm,
417 unsigned long address,
418 pte_t pte)
419{
420 __mn_flush_page(mn, address);
421}
422
423static void mn_invalidate_page(struct mmu_notifier *mn,
424 struct mm_struct *mm,
425 unsigned long address)
426{
427 __mn_flush_page(mn, address);
428}
429
430static void mn_invalidate_range_start(struct mmu_notifier *mn,
431 struct mm_struct *mm,
432 unsigned long start, unsigned long end)
433{
434 struct pasid_state *pasid_state;
435 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200436 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100437
438 pasid_state = mn_to_state(mn);
439 dev_state = pasid_state->device_state;
440
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200441 spin_lock_irqsave(&pasid_state->lock, flags);
442 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200443 amd_iommu_domain_set_gcr3(dev_state->domain,
444 pasid_state->pasid,
445 __pa(empty_page_table));
446 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200447 pasid_state->mmu_notifier_count += 1;
448 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100449}
450
451static void mn_invalidate_range_end(struct mmu_notifier *mn,
452 struct mm_struct *mm,
453 unsigned long start, unsigned long end)
454{
455 struct pasid_state *pasid_state;
456 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200457 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100458
459 pasid_state = mn_to_state(mn);
460 dev_state = pasid_state->device_state;
461
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200462 spin_lock_irqsave(&pasid_state->lock, flags);
463 pasid_state->mmu_notifier_count -= 1;
464 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200465 amd_iommu_domain_set_gcr3(dev_state->domain,
466 pasid_state->pasid,
467 __pa(pasid_state->mm->pgd));
468 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200469 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100470}
471
Joerg Roedela40d4c62014-05-20 23:18:24 +0200472static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
473{
474 struct pasid_state *pasid_state;
475 struct device_state *dev_state;
476
477 might_sleep();
478
479 pasid_state = mn_to_state(mn);
480 dev_state = pasid_state->device_state;
481
482 if (pasid_state->device_state->inv_ctx_cb)
483 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
484
Joerg Roedel61feb432014-07-08 14:19:35 +0200485 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200486}
487
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100488static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200489 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100490 .clear_flush_young = mn_clear_flush_young,
491 .change_pte = mn_change_pte,
492 .invalidate_page = mn_invalidate_page,
493 .invalidate_range_start = mn_invalidate_range_start,
494 .invalidate_range_end = mn_invalidate_range_end,
495};
496
Joerg Roedel028eeac2011-11-24 12:48:13 +0100497static void set_pri_tag_status(struct pasid_state *pasid_state,
498 u16 tag, int status)
499{
500 unsigned long flags;
501
502 spin_lock_irqsave(&pasid_state->lock, flags);
503 pasid_state->pri[tag].status = status;
504 spin_unlock_irqrestore(&pasid_state->lock, flags);
505}
506
507static void finish_pri_tag(struct device_state *dev_state,
508 struct pasid_state *pasid_state,
509 u16 tag)
510{
511 unsigned long flags;
512
513 spin_lock_irqsave(&pasid_state->lock, flags);
514 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
515 pasid_state->pri[tag].finish) {
516 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
517 pasid_state->pri[tag].status, tag);
518 pasid_state->pri[tag].finish = false;
519 pasid_state->pri[tag].status = PPR_SUCCESS;
520 }
521 spin_unlock_irqrestore(&pasid_state->lock, flags);
522}
523
524static void do_fault(struct work_struct *work)
525{
526 struct fault *fault = container_of(work, struct fault, work);
527 int npages, write;
528 struct page *page;
529
530 write = !!(fault->flags & PPR_FAULT_WRITE);
531
Jay Cornwall4378d992014-04-28 17:27:46 -0500532 down_read(&fault->state->mm->mmap_sem);
Joerg Roedeldba38382014-07-09 17:56:43 +0200533 npages = get_user_pages(NULL, fault->state->mm,
Joerg Roedel028eeac2011-11-24 12:48:13 +0100534 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500535 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100536
Joerg Roedel175d6142011-11-28 14:36:36 +0100537 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100538 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100539 } else if (fault->dev_state->inv_ppr_cb) {
540 int status;
541
542 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
543 fault->pasid,
544 fault->address,
545 fault->flags);
546 switch (status) {
547 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
548 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
549 break;
550 case AMD_IOMMU_INV_PRI_RSP_INVALID:
551 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
552 break;
553 case AMD_IOMMU_INV_PRI_RSP_FAIL:
554 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
555 break;
556 default:
557 BUG();
558 }
559 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100560 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100561 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100562
563 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
564
565 put_pasid_state(fault->state);
566
567 kfree(fault);
568}
569
570static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
571{
572 struct amd_iommu_fault *iommu_fault;
573 struct pasid_state *pasid_state;
574 struct device_state *dev_state;
575 unsigned long flags;
576 struct fault *fault;
577 bool finish;
578 u16 tag;
579 int ret;
580
581 iommu_fault = data;
582 tag = iommu_fault->tag & 0x1ff;
583 finish = (iommu_fault->tag >> 9) & 1;
584
585 ret = NOTIFY_DONE;
586 dev_state = get_device_state(iommu_fault->device_id);
587 if (dev_state == NULL)
588 goto out;
589
590 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200591 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100592 /* We know the device but not the PASID -> send INVALID */
593 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
594 PPR_INVALID, tag);
595 goto out_drop_state;
596 }
597
598 spin_lock_irqsave(&pasid_state->lock, flags);
599 atomic_inc(&pasid_state->pri[tag].inflight);
600 if (finish)
601 pasid_state->pri[tag].finish = true;
602 spin_unlock_irqrestore(&pasid_state->lock, flags);
603
604 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
605 if (fault == NULL) {
606 /* We are OOM - send success and let the device re-fault */
607 finish_pri_tag(dev_state, pasid_state, tag);
608 goto out_drop_state;
609 }
610
611 fault->dev_state = dev_state;
612 fault->address = iommu_fault->address;
613 fault->state = pasid_state;
614 fault->tag = tag;
615 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300616 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100617 fault->flags = iommu_fault->flags;
618 INIT_WORK(&fault->work, do_fault);
619
620 queue_work(iommu_wq, &fault->work);
621
622 ret = NOTIFY_OK;
623
624out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200625
626 if (ret != NOTIFY_OK && pasid_state)
627 put_pasid_state(pasid_state);
628
Joerg Roedel028eeac2011-11-24 12:48:13 +0100629 put_device_state(dev_state);
630
631out:
632 return ret;
633}
634
635static struct notifier_block ppr_nb = {
636 .notifier_call = ppr_notifier,
637};
638
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100639int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
640 struct task_struct *task)
641{
642 struct pasid_state *pasid_state;
643 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200644 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100645 u16 devid;
646 int ret;
647
648 might_sleep();
649
650 if (!amd_iommu_v2_supported())
651 return -ENODEV;
652
653 devid = device_id(pdev);
654 dev_state = get_device_state(devid);
655
656 if (dev_state == NULL)
657 return -EINVAL;
658
659 ret = -EINVAL;
660 if (pasid < 0 || pasid >= dev_state->max_pasids)
661 goto out;
662
663 ret = -ENOMEM;
664 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
665 if (pasid_state == NULL)
666 goto out;
667
Joerg Roedelf0aac632014-07-08 15:15:07 +0200668
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100669 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100670 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200671 spin_lock_init(&pasid_state->lock);
672
Joerg Roedelf0aac632014-07-08 15:15:07 +0200673 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200674 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100675 pasid_state->device_state = dev_state;
676 pasid_state->pasid = pasid;
Joerg Roedel53d340e2014-07-08 15:01:43 +0200677 pasid_state->invalid = false;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100678 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100679
680 if (pasid_state->mm == NULL)
681 goto out_free;
682
Joerg Roedelf0aac632014-07-08 15:15:07 +0200683 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100684
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100685 ret = set_pasid_state(dev_state, pasid_state, pasid);
686 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100687 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100688
689 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
690 __pa(pasid_state->mm->pgd));
691 if (ret)
692 goto out_clear_state;
693
Joerg Roedelf0aac632014-07-08 15:15:07 +0200694 /*
695 * Drop the reference to the mm_struct here. We rely on the
696 * mmu_notifier release call-back to inform us when the mm
697 * is going away.
698 */
699 mmput(mm);
700
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100701 return 0;
702
703out_clear_state:
704 clear_pasid_state(dev_state, pasid);
705
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100706out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200707 mmu_notifier_unregister(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100708
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100709out_free:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200710 mmput(mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100711 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100712
713out:
714 put_device_state(dev_state);
715
716 return ret;
717}
718EXPORT_SYMBOL(amd_iommu_bind_pasid);
719
720void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
721{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200722 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100723 struct device_state *dev_state;
724 u16 devid;
725
726 might_sleep();
727
728 if (!amd_iommu_v2_supported())
729 return;
730
731 devid = device_id(pdev);
732 dev_state = get_device_state(devid);
733 if (dev_state == NULL)
734 return;
735
736 if (pasid < 0 || pasid >= dev_state->max_pasids)
737 goto out;
738
Joerg Roedela40d4c62014-05-20 23:18:24 +0200739 pasid_state = get_pasid_state(dev_state, pasid);
740 if (pasid_state == NULL)
741 goto out;
742 /*
743 * Drop reference taken here. We are safe because we still hold
744 * the reference taken in the amd_iommu_bind_pasid function.
745 */
746 put_pasid_state(pasid_state);
747
Joerg Roedel53d340e2014-07-08 15:01:43 +0200748 /* Clear the pasid state so that the pasid can be re-used */
749 clear_pasid_state(dev_state, pasid_state->pasid);
750
Joerg Roedelf0aac632014-07-08 15:15:07 +0200751 /*
752 * Check if pasid_state->mm is still valid. If mn_release has already
753 * run it will be NULL and we can't (and don't need to) call
754 * mmu_notifier_unregister() on it anymore.
755 */
756 if (pasid_state->mm) {
757 /*
758 * This will call the mn_release function and unbind
759 * the PASID.
760 */
761 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
762 }
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100763
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200764 put_pasid_state_wait(pasid_state); /* Reference taken in
765 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100766out:
767 put_device_state(dev_state);
768}
769EXPORT_SYMBOL(amd_iommu_unbind_pasid);
770
Joerg Roedeled96f222011-11-23 17:30:39 +0100771int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
772{
773 struct device_state *dev_state;
774 unsigned long flags;
775 int ret, tmp;
776 u16 devid;
777
778 might_sleep();
779
780 if (!amd_iommu_v2_supported())
781 return -ENODEV;
782
783 if (pasids <= 0 || pasids > (PASID_MASK + 1))
784 return -EINVAL;
785
786 devid = device_id(pdev);
787
788 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
789 if (dev_state == NULL)
790 return -ENOMEM;
791
792 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100793 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200794 dev_state->pdev = pdev;
795 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100796
797 tmp = pasids;
798 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
799 dev_state->pasid_levels += 1;
800
801 atomic_set(&dev_state->count, 1);
802 dev_state->max_pasids = pasids;
803
804 ret = -ENOMEM;
805 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
806 if (dev_state->states == NULL)
807 goto out_free_dev_state;
808
809 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
810 if (dev_state->domain == NULL)
811 goto out_free_states;
812
813 amd_iommu_domain_direct_map(dev_state->domain);
814
815 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
816 if (ret)
817 goto out_free_domain;
818
819 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
820 if (ret != 0)
821 goto out_free_domain;
822
823 spin_lock_irqsave(&state_lock, flags);
824
Joerg Roedel741669c2014-05-20 23:18:23 +0200825 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100826 spin_unlock_irqrestore(&state_lock, flags);
827 ret = -EBUSY;
828 goto out_free_domain;
829 }
830
Joerg Roedel741669c2014-05-20 23:18:23 +0200831 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100832
833 spin_unlock_irqrestore(&state_lock, flags);
834
835 return 0;
836
837out_free_domain:
838 iommu_domain_free(dev_state->domain);
839
840out_free_states:
841 free_page((unsigned long)dev_state->states);
842
843out_free_dev_state:
844 kfree(dev_state);
845
846 return ret;
847}
848EXPORT_SYMBOL(amd_iommu_init_device);
849
850void amd_iommu_free_device(struct pci_dev *pdev)
851{
852 struct device_state *dev_state;
853 unsigned long flags;
854 u16 devid;
855
856 if (!amd_iommu_v2_supported())
857 return;
858
859 devid = device_id(pdev);
860
861 spin_lock_irqsave(&state_lock, flags);
862
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200863 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100864 if (dev_state == NULL) {
865 spin_unlock_irqrestore(&state_lock, flags);
866 return;
867 }
868
Joerg Roedel741669c2014-05-20 23:18:23 +0200869 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100870
871 spin_unlock_irqrestore(&state_lock, flags);
872
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100873 /* Get rid of any remaining pasid states */
874 free_pasid_states(dev_state);
875
Joerg Roedel028eeac2011-11-24 12:48:13 +0100876 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100877}
878EXPORT_SYMBOL(amd_iommu_free_device);
879
Joerg Roedel175d6142011-11-28 14:36:36 +0100880int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
881 amd_iommu_invalid_ppr_cb cb)
882{
883 struct device_state *dev_state;
884 unsigned long flags;
885 u16 devid;
886 int ret;
887
888 if (!amd_iommu_v2_supported())
889 return -ENODEV;
890
891 devid = device_id(pdev);
892
893 spin_lock_irqsave(&state_lock, flags);
894
895 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200896 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100897 if (dev_state == NULL)
898 goto out_unlock;
899
900 dev_state->inv_ppr_cb = cb;
901
902 ret = 0;
903
904out_unlock:
905 spin_unlock_irqrestore(&state_lock, flags);
906
907 return ret;
908}
909EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
910
Joerg Roedelbc216622011-12-07 12:24:42 +0100911int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
912 amd_iommu_invalidate_ctx cb)
913{
914 struct device_state *dev_state;
915 unsigned long flags;
916 u16 devid;
917 int ret;
918
919 if (!amd_iommu_v2_supported())
920 return -ENODEV;
921
922 devid = device_id(pdev);
923
924 spin_lock_irqsave(&state_lock, flags);
925
926 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200927 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100928 if (dev_state == NULL)
929 goto out_unlock;
930
931 dev_state->inv_ctx_cb = cb;
932
933 ret = 0;
934
935out_unlock:
936 spin_unlock_irqrestore(&state_lock, flags);
937
938 return ret;
939}
940EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
941
Joerg Roedele3c495c2011-11-09 12:31:15 +0100942static int __init amd_iommu_v2_init(void)
943{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100944 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100945
Joerg Roedel474d567d2012-03-15 12:46:40 +0100946 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
947
948 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900949 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100950 /*
951 * Load anyway to provide the symbols to other modules
952 * which may use AMD IOMMUv2 optionally.
953 */
954 return 0;
955 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100956
Joerg Roedeled96f222011-11-23 17:30:39 +0100957 spin_lock_init(&state_lock);
958
Joerg Roedel028eeac2011-11-24 12:48:13 +0100959 ret = -ENOMEM;
960 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100961 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200962 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100963
964 ret = -ENOMEM;
965 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
966 if (empty_page_table == NULL)
967 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100968
969 amd_iommu_register_ppr_notifier(&ppr_nb);
970
Joerg Roedele3c495c2011-11-09 12:31:15 +0100971 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100972
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100973out_destroy_wq:
974 destroy_workqueue(iommu_wq);
975
Joerg Roedel741669c2014-05-20 23:18:23 +0200976out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100977 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100978}
979
980static void __exit amd_iommu_v2_exit(void)
981{
Joerg Roedeled96f222011-11-23 17:30:39 +0100982 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100983 int i;
984
Joerg Roedel474d567d2012-03-15 12:46:40 +0100985 if (!amd_iommu_v2_supported())
986 return;
987
Joerg Roedel028eeac2011-11-24 12:48:13 +0100988 amd_iommu_unregister_ppr_notifier(&ppr_nb);
989
990 flush_workqueue(iommu_wq);
991
992 /*
993 * The loop below might call flush_workqueue(), so call
994 * destroy_workqueue() after it
995 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100996 for (i = 0; i < MAX_DEVICES; ++i) {
997 dev_state = get_device_state(i);
998
999 if (dev_state == NULL)
1000 continue;
1001
1002 WARN_ON_ONCE(1);
1003
Joerg Roedeled96f222011-11-23 17:30:39 +01001004 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +01001005 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +01001006 }
1007
Joerg Roedel028eeac2011-11-24 12:48:13 +01001008 destroy_workqueue(iommu_wq);
1009
Joerg Roedel8736b2c2011-11-24 16:21:52 +01001010 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +01001011}
1012
1013module_init(amd_iommu_v2_init);
1014module_exit(amd_iommu_v2_exit);