blob: abdb8396f89a1a026c4d9c85267f92b0b7114e9a [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 */
48 struct task_struct *task; /* Task bound to this PASID */
49 struct mm_struct *mm; /* mm_struct for the faults */
Joerg Roedel8736b2c2011-11-24 16:21:52 +010050 struct mmu_notifier mn; /* mmu_otifier handle */
Joerg Roedeled96f222011-11-23 17:30:39 +010051 struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
52 struct device_state *device_state; /* Link to our device_state */
53 int pasid; /* PASID index */
Joerg Roedel028eeac2011-11-24 12:48:13 +010054 spinlock_t lock; /* Protect pri_queues */
55 wait_queue_head_t wq; /* To wait for count == 0 */
Joerg Roedeled96f222011-11-23 17:30:39 +010056};
57
58struct device_state {
59 atomic_t count;
60 struct pci_dev *pdev;
61 struct pasid_state **states;
62 struct iommu_domain *domain;
63 int pasid_levels;
64 int max_pasids;
65 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010066 wait_queue_head_t wq;
67};
68
69struct fault {
70 struct work_struct work;
71 struct device_state *dev_state;
72 struct pasid_state *state;
73 struct mm_struct *mm;
74 u64 address;
75 u16 devid;
76 u16 pasid;
77 u16 tag;
78 u16 finish;
79 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010080};
81
82struct device_state **state_table;
83static spinlock_t state_lock;
84
85/* List and lock for all pasid_states */
86static LIST_HEAD(pasid_state_list);
Joerg Roedel2d5503b2011-11-24 10:41:57 +010087static DEFINE_SPINLOCK(ps_lock);
88
Joerg Roedel028eeac2011-11-24 12:48:13 +010089static struct workqueue_struct *iommu_wq;
90
Joerg Roedel8736b2c2011-11-24 16:21:52 +010091/*
92 * Empty page table - Used between
93 * mmu_notifier_invalidate_range_start and
94 * mmu_notifier_invalidate_range_end
95 */
96static u64 *empty_page_table;
97
Joerg Roedel2d5503b2011-11-24 10:41:57 +010098static void free_pasid_states(struct device_state *dev_state);
99static void unbind_pasid(struct device_state *dev_state, int pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100100static int task_exit(struct notifier_block *nb, unsigned long e, void *data);
Joerg Roedeled96f222011-11-23 17:30:39 +0100101
102static u16 device_id(struct pci_dev *pdev)
103{
104 u16 devid;
105
106 devid = pdev->bus->number;
107 devid = (devid << 8) | pdev->devfn;
108
109 return devid;
110}
111
112static struct device_state *get_device_state(u16 devid)
113{
114 struct device_state *dev_state;
115 unsigned long flags;
116
117 spin_lock_irqsave(&state_lock, flags);
118 dev_state = state_table[devid];
119 if (dev_state != NULL)
120 atomic_inc(&dev_state->count);
121 spin_unlock_irqrestore(&state_lock, flags);
122
123 return dev_state;
124}
125
126static void free_device_state(struct device_state *dev_state)
127{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100128 /*
129 * First detach device from domain - No more PRI requests will arrive
130 * from that device after it is unbound from the IOMMUv2 domain.
131 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100132 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100133
134 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100135 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100136
137 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100138 kfree(dev_state);
139}
140
141static void put_device_state(struct device_state *dev_state)
142{
143 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100144 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100145}
146
Joerg Roedel028eeac2011-11-24 12:48:13 +0100147static void put_device_state_wait(struct device_state *dev_state)
148{
149 DEFINE_WAIT(wait);
150
151 prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
152 if (!atomic_dec_and_test(&dev_state->count))
153 schedule();
154 finish_wait(&dev_state->wq, &wait);
155
156 free_device_state(dev_state);
157}
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100158
159static struct notifier_block profile_nb = {
160 .notifier_call = task_exit,
161};
162
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100163static void link_pasid_state(struct pasid_state *pasid_state)
164{
165 spin_lock(&ps_lock);
166 list_add_tail(&pasid_state->list, &pasid_state_list);
167 spin_unlock(&ps_lock);
168}
169
170static void __unlink_pasid_state(struct pasid_state *pasid_state)
171{
172 list_del(&pasid_state->list);
173}
174
175static void unlink_pasid_state(struct pasid_state *pasid_state)
176{
177 spin_lock(&ps_lock);
178 __unlink_pasid_state(pasid_state);
179 spin_unlock(&ps_lock);
180}
181
182/* Must be called under dev_state->lock */
183static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
184 int pasid, bool alloc)
185{
186 struct pasid_state **root, **ptr;
187 int level, index;
188
189 level = dev_state->pasid_levels;
190 root = dev_state->states;
191
192 while (true) {
193
194 index = (pasid >> (9 * level)) & 0x1ff;
195 ptr = &root[index];
196
197 if (level == 0)
198 break;
199
200 if (*ptr == NULL) {
201 if (!alloc)
202 return NULL;
203
204 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
205 if (*ptr == NULL)
206 return NULL;
207 }
208
209 root = (struct pasid_state **)*ptr;
210 level -= 1;
211 }
212
213 return ptr;
214}
215
216static int set_pasid_state(struct device_state *dev_state,
217 struct pasid_state *pasid_state,
218 int pasid)
219{
220 struct pasid_state **ptr;
221 unsigned long flags;
222 int ret;
223
224 spin_lock_irqsave(&dev_state->lock, flags);
225 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
226
227 ret = -ENOMEM;
228 if (ptr == NULL)
229 goto out_unlock;
230
231 ret = -ENOMEM;
232 if (*ptr != NULL)
233 goto out_unlock;
234
235 *ptr = pasid_state;
236
237 ret = 0;
238
239out_unlock:
240 spin_unlock_irqrestore(&dev_state->lock, flags);
241
242 return ret;
243}
244
245static void clear_pasid_state(struct device_state *dev_state, int pasid)
246{
247 struct pasid_state **ptr;
248 unsigned long flags;
249
250 spin_lock_irqsave(&dev_state->lock, flags);
251 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
252
253 if (ptr == NULL)
254 goto out_unlock;
255
256 *ptr = NULL;
257
258out_unlock:
259 spin_unlock_irqrestore(&dev_state->lock, flags);
260}
261
262static struct pasid_state *get_pasid_state(struct device_state *dev_state,
263 int pasid)
264{
265 struct pasid_state **ptr, *ret = NULL;
266 unsigned long flags;
267
268 spin_lock_irqsave(&dev_state->lock, flags);
269 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
270
271 if (ptr == NULL)
272 goto out_unlock;
273
274 ret = *ptr;
275 if (ret)
276 atomic_inc(&ret->count);
277
278out_unlock:
279 spin_unlock_irqrestore(&dev_state->lock, flags);
280
281 return ret;
282}
283
284static void free_pasid_state(struct pasid_state *pasid_state)
285{
286 kfree(pasid_state);
287}
288
289static void put_pasid_state(struct pasid_state *pasid_state)
290{
291 if (atomic_dec_and_test(&pasid_state->count)) {
292 put_device_state(pasid_state->device_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100293 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100294 }
295}
296
Joerg Roedel028eeac2011-11-24 12:48:13 +0100297static void put_pasid_state_wait(struct pasid_state *pasid_state)
298{
299 DEFINE_WAIT(wait);
300
301 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
302
303 if (atomic_dec_and_test(&pasid_state->count))
304 put_device_state(pasid_state->device_state);
305 else
306 schedule();
307
308 finish_wait(&pasid_state->wq, &wait);
309 mmput(pasid_state->mm);
310 free_pasid_state(pasid_state);
311}
312
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100313static void __unbind_pasid(struct pasid_state *pasid_state)
314{
315 struct iommu_domain *domain;
316
317 domain = pasid_state->device_state->domain;
318
319 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
320 clear_pasid_state(pasid_state->device_state, pasid_state->pasid);
321
322 /* Make sure no more pending faults are in the queue */
323 flush_workqueue(iommu_wq);
324
325 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
326
327 put_pasid_state(pasid_state); /* Reference taken in bind() function */
328}
329
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100330static void unbind_pasid(struct device_state *dev_state, int pasid)
331{
332 struct pasid_state *pasid_state;
333
334 pasid_state = get_pasid_state(dev_state, pasid);
335 if (pasid_state == NULL)
336 return;
337
338 unlink_pasid_state(pasid_state);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100339 __unbind_pasid(pasid_state);
340 put_pasid_state_wait(pasid_state); /* Reference taken in this function */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100341}
342
343static void free_pasid_states_level1(struct pasid_state **tbl)
344{
345 int i;
346
347 for (i = 0; i < 512; ++i) {
348 if (tbl[i] == NULL)
349 continue;
350
351 free_page((unsigned long)tbl[i]);
352 }
353}
354
355static void free_pasid_states_level2(struct pasid_state **tbl)
356{
357 struct pasid_state **ptr;
358 int i;
359
360 for (i = 0; i < 512; ++i) {
361 if (tbl[i] == NULL)
362 continue;
363
364 ptr = (struct pasid_state **)tbl[i];
365 free_pasid_states_level1(ptr);
366 }
367}
368
369static void free_pasid_states(struct device_state *dev_state)
370{
371 struct pasid_state *pasid_state;
372 int i;
373
374 for (i = 0; i < dev_state->max_pasids; ++i) {
375 pasid_state = get_pasid_state(dev_state, i);
376 if (pasid_state == NULL)
377 continue;
378
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100379 put_pasid_state(pasid_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100380 unbind_pasid(dev_state, i);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100381 }
382
383 if (dev_state->pasid_levels == 2)
384 free_pasid_states_level2(dev_state->states);
385 else if (dev_state->pasid_levels == 1)
386 free_pasid_states_level1(dev_state->states);
387 else if (dev_state->pasid_levels != 0)
388 BUG();
389
390 free_page((unsigned long)dev_state->states);
391}
392
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100393static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
394{
395 return container_of(mn, struct pasid_state, mn);
396}
397
398static void __mn_flush_page(struct mmu_notifier *mn,
399 unsigned long address)
400{
401 struct pasid_state *pasid_state;
402 struct device_state *dev_state;
403
404 pasid_state = mn_to_state(mn);
405 dev_state = pasid_state->device_state;
406
407 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
408}
409
410static int mn_clear_flush_young(struct mmu_notifier *mn,
411 struct mm_struct *mm,
412 unsigned long address)
413{
414 __mn_flush_page(mn, address);
415
416 return 0;
417}
418
419static void mn_change_pte(struct mmu_notifier *mn,
420 struct mm_struct *mm,
421 unsigned long address,
422 pte_t pte)
423{
424 __mn_flush_page(mn, address);
425}
426
427static void mn_invalidate_page(struct mmu_notifier *mn,
428 struct mm_struct *mm,
429 unsigned long address)
430{
431 __mn_flush_page(mn, address);
432}
433
434static void mn_invalidate_range_start(struct mmu_notifier *mn,
435 struct mm_struct *mm,
436 unsigned long start, unsigned long end)
437{
438 struct pasid_state *pasid_state;
439 struct device_state *dev_state;
440
441 pasid_state = mn_to_state(mn);
442 dev_state = pasid_state->device_state;
443
444 amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid,
445 __pa(empty_page_table));
446}
447
448static void mn_invalidate_range_end(struct mmu_notifier *mn,
449 struct mm_struct *mm,
450 unsigned long start, unsigned long end)
451{
452 struct pasid_state *pasid_state;
453 struct device_state *dev_state;
454
455 pasid_state = mn_to_state(mn);
456 dev_state = pasid_state->device_state;
457
458 amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid,
459 __pa(pasid_state->mm->pgd));
460}
461
462static struct mmu_notifier_ops iommu_mn = {
463 .clear_flush_young = mn_clear_flush_young,
464 .change_pte = mn_change_pte,
465 .invalidate_page = mn_invalidate_page,
466 .invalidate_range_start = mn_invalidate_range_start,
467 .invalidate_range_end = mn_invalidate_range_end,
468};
469
Joerg Roedel028eeac2011-11-24 12:48:13 +0100470static void set_pri_tag_status(struct pasid_state *pasid_state,
471 u16 tag, int status)
472{
473 unsigned long flags;
474
475 spin_lock_irqsave(&pasid_state->lock, flags);
476 pasid_state->pri[tag].status = status;
477 spin_unlock_irqrestore(&pasid_state->lock, flags);
478}
479
480static void finish_pri_tag(struct device_state *dev_state,
481 struct pasid_state *pasid_state,
482 u16 tag)
483{
484 unsigned long flags;
485
486 spin_lock_irqsave(&pasid_state->lock, flags);
487 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
488 pasid_state->pri[tag].finish) {
489 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
490 pasid_state->pri[tag].status, tag);
491 pasid_state->pri[tag].finish = false;
492 pasid_state->pri[tag].status = PPR_SUCCESS;
493 }
494 spin_unlock_irqrestore(&pasid_state->lock, flags);
495}
496
497static void do_fault(struct work_struct *work)
498{
499 struct fault *fault = container_of(work, struct fault, work);
500 int npages, write;
501 struct page *page;
502
503 write = !!(fault->flags & PPR_FAULT_WRITE);
504
505 npages = get_user_pages(fault->state->task, fault->state->mm,
506 fault->address, 1, write, 0, &page, NULL);
507
508 if (npages == 1)
509 put_page(page);
510 else
511 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
512
513 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
514
515 put_pasid_state(fault->state);
516
517 kfree(fault);
518}
519
520static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
521{
522 struct amd_iommu_fault *iommu_fault;
523 struct pasid_state *pasid_state;
524 struct device_state *dev_state;
525 unsigned long flags;
526 struct fault *fault;
527 bool finish;
528 u16 tag;
529 int ret;
530
531 iommu_fault = data;
532 tag = iommu_fault->tag & 0x1ff;
533 finish = (iommu_fault->tag >> 9) & 1;
534
535 ret = NOTIFY_DONE;
536 dev_state = get_device_state(iommu_fault->device_id);
537 if (dev_state == NULL)
538 goto out;
539
540 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
541 if (pasid_state == NULL) {
542 /* We know the device but not the PASID -> send INVALID */
543 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
544 PPR_INVALID, tag);
545 goto out_drop_state;
546 }
547
548 spin_lock_irqsave(&pasid_state->lock, flags);
549 atomic_inc(&pasid_state->pri[tag].inflight);
550 if (finish)
551 pasid_state->pri[tag].finish = true;
552 spin_unlock_irqrestore(&pasid_state->lock, flags);
553
554 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
555 if (fault == NULL) {
556 /* We are OOM - send success and let the device re-fault */
557 finish_pri_tag(dev_state, pasid_state, tag);
558 goto out_drop_state;
559 }
560
561 fault->dev_state = dev_state;
562 fault->address = iommu_fault->address;
563 fault->state = pasid_state;
564 fault->tag = tag;
565 fault->finish = finish;
566 fault->flags = iommu_fault->flags;
567 INIT_WORK(&fault->work, do_fault);
568
569 queue_work(iommu_wq, &fault->work);
570
571 ret = NOTIFY_OK;
572
573out_drop_state:
574 put_device_state(dev_state);
575
576out:
577 return ret;
578}
579
580static struct notifier_block ppr_nb = {
581 .notifier_call = ppr_notifier,
582};
583
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100584static int task_exit(struct notifier_block *nb, unsigned long e, void *data)
585{
586 struct pasid_state *pasid_state;
587 struct task_struct *task;
588
589 task = data;
590
591 /*
592 * Using this notifier is a hack - but there is no other choice
593 * at the moment. What I really want is a sleeping notifier that
594 * is called when an MM goes down. But such a notifier doesn't
595 * exist yet. The notifier needs to sleep because it has to make
596 * sure that the device does not use the PASID and the address
597 * space anymore before it is destroyed. This includes waiting
598 * for pending PRI requests to pass the workqueue. The
599 * MMU-Notifiers would be a good fit, but they use RCU and so
600 * they are not allowed to sleep. Lets see how we can solve this
601 * in a more intelligent way in the future.
602 */
603again:
604 spin_lock(&ps_lock);
605 list_for_each_entry(pasid_state, &pasid_state_list, list) {
606 struct device_state *dev_state;
607 int pasid;
608
609 if (pasid_state->task != task)
610 continue;
611
612 /* Drop Lock and unbind */
613 spin_unlock(&ps_lock);
614
615 dev_state = pasid_state->device_state;
616 pasid = pasid_state->pasid;
617
618 unbind_pasid(dev_state, pasid);
619
620 /* Task may be in the list multiple times */
621 goto again;
622 }
623 spin_unlock(&ps_lock);
624
625 return NOTIFY_OK;
626}
627
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100628int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
629 struct task_struct *task)
630{
631 struct pasid_state *pasid_state;
632 struct device_state *dev_state;
633 u16 devid;
634 int ret;
635
636 might_sleep();
637
638 if (!amd_iommu_v2_supported())
639 return -ENODEV;
640
641 devid = device_id(pdev);
642 dev_state = get_device_state(devid);
643
644 if (dev_state == NULL)
645 return -EINVAL;
646
647 ret = -EINVAL;
648 if (pasid < 0 || pasid >= dev_state->max_pasids)
649 goto out;
650
651 ret = -ENOMEM;
652 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
653 if (pasid_state == NULL)
654 goto out;
655
656 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100657 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100658 pasid_state->task = task;
659 pasid_state->mm = get_task_mm(task);
660 pasid_state->device_state = dev_state;
661 pasid_state->pasid = pasid;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100662 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100663
664 if (pasid_state->mm == NULL)
665 goto out_free;
666
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100667 mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
668
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100669 ret = set_pasid_state(dev_state, pasid_state, pasid);
670 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100671 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100672
673 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
674 __pa(pasid_state->mm->pgd));
675 if (ret)
676 goto out_clear_state;
677
678 link_pasid_state(pasid_state);
679
680 return 0;
681
682out_clear_state:
683 clear_pasid_state(dev_state, pasid);
684
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100685out_unregister:
686 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
687
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100688out_free:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100689 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100690
691out:
692 put_device_state(dev_state);
693
694 return ret;
695}
696EXPORT_SYMBOL(amd_iommu_bind_pasid);
697
698void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
699{
700 struct device_state *dev_state;
701 u16 devid;
702
703 might_sleep();
704
705 if (!amd_iommu_v2_supported())
706 return;
707
708 devid = device_id(pdev);
709 dev_state = get_device_state(devid);
710 if (dev_state == NULL)
711 return;
712
713 if (pasid < 0 || pasid >= dev_state->max_pasids)
714 goto out;
715
716 unbind_pasid(dev_state, pasid);
717
718out:
719 put_device_state(dev_state);
720}
721EXPORT_SYMBOL(amd_iommu_unbind_pasid);
722
Joerg Roedeled96f222011-11-23 17:30:39 +0100723int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
724{
725 struct device_state *dev_state;
726 unsigned long flags;
727 int ret, tmp;
728 u16 devid;
729
730 might_sleep();
731
732 if (!amd_iommu_v2_supported())
733 return -ENODEV;
734
735 if (pasids <= 0 || pasids > (PASID_MASK + 1))
736 return -EINVAL;
737
738 devid = device_id(pdev);
739
740 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
741 if (dev_state == NULL)
742 return -ENOMEM;
743
744 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100745 init_waitqueue_head(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100746 dev_state->pdev = pdev;
747
748 tmp = pasids;
749 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
750 dev_state->pasid_levels += 1;
751
752 atomic_set(&dev_state->count, 1);
753 dev_state->max_pasids = pasids;
754
755 ret = -ENOMEM;
756 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
757 if (dev_state->states == NULL)
758 goto out_free_dev_state;
759
760 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
761 if (dev_state->domain == NULL)
762 goto out_free_states;
763
764 amd_iommu_domain_direct_map(dev_state->domain);
765
766 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
767 if (ret)
768 goto out_free_domain;
769
770 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
771 if (ret != 0)
772 goto out_free_domain;
773
774 spin_lock_irqsave(&state_lock, flags);
775
776 if (state_table[devid] != NULL) {
777 spin_unlock_irqrestore(&state_lock, flags);
778 ret = -EBUSY;
779 goto out_free_domain;
780 }
781
782 state_table[devid] = dev_state;
783
784 spin_unlock_irqrestore(&state_lock, flags);
785
786 return 0;
787
788out_free_domain:
789 iommu_domain_free(dev_state->domain);
790
791out_free_states:
792 free_page((unsigned long)dev_state->states);
793
794out_free_dev_state:
795 kfree(dev_state);
796
797 return ret;
798}
799EXPORT_SYMBOL(amd_iommu_init_device);
800
801void amd_iommu_free_device(struct pci_dev *pdev)
802{
803 struct device_state *dev_state;
804 unsigned long flags;
805 u16 devid;
806
807 if (!amd_iommu_v2_supported())
808 return;
809
810 devid = device_id(pdev);
811
812 spin_lock_irqsave(&state_lock, flags);
813
814 dev_state = state_table[devid];
815 if (dev_state == NULL) {
816 spin_unlock_irqrestore(&state_lock, flags);
817 return;
818 }
819
820 state_table[devid] = NULL;
821
822 spin_unlock_irqrestore(&state_lock, flags);
823
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100824 /* Get rid of any remaining pasid states */
825 free_pasid_states(dev_state);
826
Joerg Roedel028eeac2011-11-24 12:48:13 +0100827 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100828}
829EXPORT_SYMBOL(amd_iommu_free_device);
830
Joerg Roedele3c495c2011-11-09 12:31:15 +0100831static int __init amd_iommu_v2_init(void)
832{
Joerg Roedeled96f222011-11-23 17:30:39 +0100833 size_t state_table_size;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100834 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100835
Joerg Roedele3c495c2011-11-09 12:31:15 +0100836 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>");
837
Joerg Roedeled96f222011-11-23 17:30:39 +0100838 spin_lock_init(&state_lock);
839
840 state_table_size = MAX_DEVICES * sizeof(struct device_state *);
841 state_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
842 get_order(state_table_size));
843 if (state_table == NULL)
844 return -ENOMEM;
845
Joerg Roedel028eeac2011-11-24 12:48:13 +0100846 ret = -ENOMEM;
847 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100848 if (iommu_wq == NULL)
Joerg Roedel028eeac2011-11-24 12:48:13 +0100849 goto out_free;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100850
851 ret = -ENOMEM;
852 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
853 if (empty_page_table == NULL)
854 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100855
856 amd_iommu_register_ppr_notifier(&ppr_nb);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100857 profile_event_register(PROFILE_TASK_EXIT, &profile_nb);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100858
Joerg Roedele3c495c2011-11-09 12:31:15 +0100859 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100860
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100861out_destroy_wq:
862 destroy_workqueue(iommu_wq);
863
Joerg Roedel028eeac2011-11-24 12:48:13 +0100864out_free:
865 free_pages((unsigned long)state_table, get_order(state_table_size));
866
867 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100868}
869
870static void __exit amd_iommu_v2_exit(void)
871{
Joerg Roedeled96f222011-11-23 17:30:39 +0100872 struct device_state *dev_state;
873 size_t state_table_size;
874 int i;
875
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100876 profile_event_unregister(PROFILE_TASK_EXIT, &profile_nb);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100877 amd_iommu_unregister_ppr_notifier(&ppr_nb);
878
879 flush_workqueue(iommu_wq);
880
881 /*
882 * The loop below might call flush_workqueue(), so call
883 * destroy_workqueue() after it
884 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100885 for (i = 0; i < MAX_DEVICES; ++i) {
886 dev_state = get_device_state(i);
887
888 if (dev_state == NULL)
889 continue;
890
891 WARN_ON_ONCE(1);
892
Joerg Roedeled96f222011-11-23 17:30:39 +0100893 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100894 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100895 }
896
Joerg Roedel028eeac2011-11-24 12:48:13 +0100897 destroy_workqueue(iommu_wq);
898
Joerg Roedeled96f222011-11-23 17:30:39 +0100899 state_table_size = MAX_DEVICES * sizeof(struct device_state *);
900 free_pages((unsigned long)state_table, get_order(state_table_size));
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100901
902 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100903}
904
905module_init(amd_iommu_v2_init);
906module_exit(amd_iommu_v2_exit);