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