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