blob: 5db3675cad76d35fd24394a5c750610c703d458b [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;
Joerg Roedel175d6142011-11-28 14:36:36 +010065 amd_iommu_invalid_ppr_cb inv_ppr_cb;
Joerg Roedelbc216622011-12-07 12:24:42 +010066 amd_iommu_invalidate_ctx inv_ctx_cb;
Joerg Roedeled96f222011-11-23 17:30:39 +010067 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010068 wait_queue_head_t wq;
69};
70
71struct fault {
72 struct work_struct work;
73 struct device_state *dev_state;
74 struct pasid_state *state;
75 struct mm_struct *mm;
76 u64 address;
77 u16 devid;
78 u16 pasid;
79 u16 tag;
80 u16 finish;
81 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010082};
83
Joerg Roedel98f1ad22012-07-06 13:28:37 +020084static struct device_state **state_table;
Joerg Roedeled96f222011-11-23 17:30:39 +010085static spinlock_t state_lock;
86
87/* List and lock for all pasid_states */
88static LIST_HEAD(pasid_state_list);
Joerg Roedel2d5503b2011-11-24 10:41:57 +010089static DEFINE_SPINLOCK(ps_lock);
90
Joerg Roedel028eeac2011-11-24 12:48:13 +010091static struct workqueue_struct *iommu_wq;
92
Joerg Roedel8736b2c2011-11-24 16:21:52 +010093/*
94 * Empty page table - Used between
95 * mmu_notifier_invalidate_range_start and
96 * mmu_notifier_invalidate_range_end
97 */
98static u64 *empty_page_table;
99
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100100static void free_pasid_states(struct device_state *dev_state);
101static void unbind_pasid(struct device_state *dev_state, int pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100102static int task_exit(struct notifier_block *nb, unsigned long e, void *data);
Joerg Roedeled96f222011-11-23 17:30:39 +0100103
104static u16 device_id(struct pci_dev *pdev)
105{
106 u16 devid;
107
108 devid = pdev->bus->number;
109 devid = (devid << 8) | pdev->devfn;
110
111 return devid;
112}
113
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200114static struct device_state *__get_device_state(u16 devid)
115{
116 return state_table[devid];
117}
118
Joerg Roedeled96f222011-11-23 17:30:39 +0100119static 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);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200125 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100126 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{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100135 /*
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 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100139 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100140
141 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100142 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100143
144 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100145 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))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100151 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100152}
153
Joerg Roedel028eeac2011-11-24 12:48:13 +0100154static void put_device_state_wait(struct device_state *dev_state)
155{
156 DEFINE_WAIT(wait);
157
158 prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
159 if (!atomic_dec_and_test(&dev_state->count))
160 schedule();
161 finish_wait(&dev_state->wq, &wait);
162
163 free_device_state(dev_state);
164}
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100165
166static struct notifier_block profile_nb = {
167 .notifier_call = task_exit,
168};
169
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100170static void link_pasid_state(struct pasid_state *pasid_state)
171{
172 spin_lock(&ps_lock);
173 list_add_tail(&pasid_state->list, &pasid_state_list);
174 spin_unlock(&ps_lock);
175}
176
177static void __unlink_pasid_state(struct pasid_state *pasid_state)
178{
179 list_del(&pasid_state->list);
180}
181
182static void unlink_pasid_state(struct pasid_state *pasid_state)
183{
184 spin_lock(&ps_lock);
185 __unlink_pasid_state(pasid_state);
186 spin_unlock(&ps_lock);
187}
188
189/* Must be called under dev_state->lock */
190static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
191 int pasid, bool alloc)
192{
193 struct pasid_state **root, **ptr;
194 int level, index;
195
196 level = dev_state->pasid_levels;
197 root = dev_state->states;
198
199 while (true) {
200
201 index = (pasid >> (9 * level)) & 0x1ff;
202 ptr = &root[index];
203
204 if (level == 0)
205 break;
206
207 if (*ptr == NULL) {
208 if (!alloc)
209 return NULL;
210
211 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
212 if (*ptr == NULL)
213 return NULL;
214 }
215
216 root = (struct pasid_state **)*ptr;
217 level -= 1;
218 }
219
220 return ptr;
221}
222
223static int set_pasid_state(struct device_state *dev_state,
224 struct pasid_state *pasid_state,
225 int pasid)
226{
227 struct pasid_state **ptr;
228 unsigned long flags;
229 int ret;
230
231 spin_lock_irqsave(&dev_state->lock, flags);
232 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
233
234 ret = -ENOMEM;
235 if (ptr == NULL)
236 goto out_unlock;
237
238 ret = -ENOMEM;
239 if (*ptr != NULL)
240 goto out_unlock;
241
242 *ptr = pasid_state;
243
244 ret = 0;
245
246out_unlock:
247 spin_unlock_irqrestore(&dev_state->lock, flags);
248
249 return ret;
250}
251
252static void clear_pasid_state(struct device_state *dev_state, int pasid)
253{
254 struct pasid_state **ptr;
255 unsigned long flags;
256
257 spin_lock_irqsave(&dev_state->lock, flags);
258 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
259
260 if (ptr == NULL)
261 goto out_unlock;
262
263 *ptr = NULL;
264
265out_unlock:
266 spin_unlock_irqrestore(&dev_state->lock, flags);
267}
268
269static struct pasid_state *get_pasid_state(struct device_state *dev_state,
270 int pasid)
271{
272 struct pasid_state **ptr, *ret = NULL;
273 unsigned long flags;
274
275 spin_lock_irqsave(&dev_state->lock, flags);
276 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
277
278 if (ptr == NULL)
279 goto out_unlock;
280
281 ret = *ptr;
282 if (ret)
283 atomic_inc(&ret->count);
284
285out_unlock:
286 spin_unlock_irqrestore(&dev_state->lock, flags);
287
288 return ret;
289}
290
291static void free_pasid_state(struct pasid_state *pasid_state)
292{
293 kfree(pasid_state);
294}
295
296static void put_pasid_state(struct pasid_state *pasid_state)
297{
298 if (atomic_dec_and_test(&pasid_state->count)) {
299 put_device_state(pasid_state->device_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100300 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100301 }
302}
303
Joerg Roedel028eeac2011-11-24 12:48:13 +0100304static void put_pasid_state_wait(struct pasid_state *pasid_state)
305{
306 DEFINE_WAIT(wait);
307
308 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
309
310 if (atomic_dec_and_test(&pasid_state->count))
311 put_device_state(pasid_state->device_state);
312 else
313 schedule();
314
315 finish_wait(&pasid_state->wq, &wait);
316 mmput(pasid_state->mm);
317 free_pasid_state(pasid_state);
318}
319
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100320static void __unbind_pasid(struct pasid_state *pasid_state)
321{
322 struct iommu_domain *domain;
323
324 domain = pasid_state->device_state->domain;
325
326 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
327 clear_pasid_state(pasid_state->device_state, pasid_state->pasid);
328
329 /* Make sure no more pending faults are in the queue */
330 flush_workqueue(iommu_wq);
331
332 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
333
334 put_pasid_state(pasid_state); /* Reference taken in bind() function */
335}
336
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100337static void unbind_pasid(struct device_state *dev_state, int pasid)
338{
339 struct pasid_state *pasid_state;
340
341 pasid_state = get_pasid_state(dev_state, pasid);
342 if (pasid_state == NULL)
343 return;
344
345 unlink_pasid_state(pasid_state);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100346 __unbind_pasid(pasid_state);
347 put_pasid_state_wait(pasid_state); /* Reference taken in this function */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100348}
349
350static void free_pasid_states_level1(struct pasid_state **tbl)
351{
352 int i;
353
354 for (i = 0; i < 512; ++i) {
355 if (tbl[i] == NULL)
356 continue;
357
358 free_page((unsigned long)tbl[i]);
359 }
360}
361
362static void free_pasid_states_level2(struct pasid_state **tbl)
363{
364 struct pasid_state **ptr;
365 int i;
366
367 for (i = 0; i < 512; ++i) {
368 if (tbl[i] == NULL)
369 continue;
370
371 ptr = (struct pasid_state **)tbl[i];
372 free_pasid_states_level1(ptr);
373 }
374}
375
376static void free_pasid_states(struct device_state *dev_state)
377{
378 struct pasid_state *pasid_state;
379 int i;
380
381 for (i = 0; i < dev_state->max_pasids; ++i) {
382 pasid_state = get_pasid_state(dev_state, i);
383 if (pasid_state == NULL)
384 continue;
385
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100386 put_pasid_state(pasid_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100387 unbind_pasid(dev_state, i);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100388 }
389
390 if (dev_state->pasid_levels == 2)
391 free_pasid_states_level2(dev_state->states);
392 else if (dev_state->pasid_levels == 1)
393 free_pasid_states_level1(dev_state->states);
394 else if (dev_state->pasid_levels != 0)
395 BUG();
396
397 free_page((unsigned long)dev_state->states);
398}
399
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100400static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
401{
402 return container_of(mn, struct pasid_state, mn);
403}
404
405static void __mn_flush_page(struct mmu_notifier *mn,
406 unsigned long address)
407{
408 struct pasid_state *pasid_state;
409 struct device_state *dev_state;
410
411 pasid_state = mn_to_state(mn);
412 dev_state = pasid_state->device_state;
413
414 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
415}
416
417static int mn_clear_flush_young(struct mmu_notifier *mn,
418 struct mm_struct *mm,
419 unsigned long address)
420{
421 __mn_flush_page(mn, address);
422
423 return 0;
424}
425
426static void mn_change_pte(struct mmu_notifier *mn,
427 struct mm_struct *mm,
428 unsigned long address,
429 pte_t pte)
430{
431 __mn_flush_page(mn, address);
432}
433
434static void mn_invalidate_page(struct mmu_notifier *mn,
435 struct mm_struct *mm,
436 unsigned long address)
437{
438 __mn_flush_page(mn, address);
439}
440
441static void mn_invalidate_range_start(struct mmu_notifier *mn,
442 struct mm_struct *mm,
443 unsigned long start, unsigned long end)
444{
445 struct pasid_state *pasid_state;
446 struct device_state *dev_state;
447
448 pasid_state = mn_to_state(mn);
449 dev_state = pasid_state->device_state;
450
451 amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid,
452 __pa(empty_page_table));
453}
454
455static void mn_invalidate_range_end(struct mmu_notifier *mn,
456 struct mm_struct *mm,
457 unsigned long start, unsigned long end)
458{
459 struct pasid_state *pasid_state;
460 struct device_state *dev_state;
461
462 pasid_state = mn_to_state(mn);
463 dev_state = pasid_state->device_state;
464
465 amd_iommu_domain_set_gcr3(dev_state->domain, pasid_state->pasid,
466 __pa(pasid_state->mm->pgd));
467}
468
469static struct mmu_notifier_ops iommu_mn = {
470 .clear_flush_young = mn_clear_flush_young,
471 .change_pte = mn_change_pte,
472 .invalidate_page = mn_invalidate_page,
473 .invalidate_range_start = mn_invalidate_range_start,
474 .invalidate_range_end = mn_invalidate_range_end,
475};
476
Joerg Roedel028eeac2011-11-24 12:48:13 +0100477static void set_pri_tag_status(struct pasid_state *pasid_state,
478 u16 tag, int status)
479{
480 unsigned long flags;
481
482 spin_lock_irqsave(&pasid_state->lock, flags);
483 pasid_state->pri[tag].status = status;
484 spin_unlock_irqrestore(&pasid_state->lock, flags);
485}
486
487static void finish_pri_tag(struct device_state *dev_state,
488 struct pasid_state *pasid_state,
489 u16 tag)
490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&pasid_state->lock, flags);
494 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
495 pasid_state->pri[tag].finish) {
496 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
497 pasid_state->pri[tag].status, tag);
498 pasid_state->pri[tag].finish = false;
499 pasid_state->pri[tag].status = PPR_SUCCESS;
500 }
501 spin_unlock_irqrestore(&pasid_state->lock, flags);
502}
503
504static void do_fault(struct work_struct *work)
505{
506 struct fault *fault = container_of(work, struct fault, work);
507 int npages, write;
508 struct page *page;
509
510 write = !!(fault->flags & PPR_FAULT_WRITE);
511
Jay Cornwall4378d992014-04-28 17:27:46 -0500512 down_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100513 npages = get_user_pages(fault->state->task, fault->state->mm,
514 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500515 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100516
Joerg Roedel175d6142011-11-28 14:36:36 +0100517 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100518 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100519 } else if (fault->dev_state->inv_ppr_cb) {
520 int status;
521
522 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
523 fault->pasid,
524 fault->address,
525 fault->flags);
526 switch (status) {
527 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
528 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
529 break;
530 case AMD_IOMMU_INV_PRI_RSP_INVALID:
531 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
532 break;
533 case AMD_IOMMU_INV_PRI_RSP_FAIL:
534 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
535 break;
536 default:
537 BUG();
538 }
539 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100540 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100541 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100542
543 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
544
545 put_pasid_state(fault->state);
546
547 kfree(fault);
548}
549
550static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
551{
552 struct amd_iommu_fault *iommu_fault;
553 struct pasid_state *pasid_state;
554 struct device_state *dev_state;
555 unsigned long flags;
556 struct fault *fault;
557 bool finish;
558 u16 tag;
559 int ret;
560
561 iommu_fault = data;
562 tag = iommu_fault->tag & 0x1ff;
563 finish = (iommu_fault->tag >> 9) & 1;
564
565 ret = NOTIFY_DONE;
566 dev_state = get_device_state(iommu_fault->device_id);
567 if (dev_state == NULL)
568 goto out;
569
570 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
571 if (pasid_state == NULL) {
572 /* We know the device but not the PASID -> send INVALID */
573 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
574 PPR_INVALID, tag);
575 goto out_drop_state;
576 }
577
578 spin_lock_irqsave(&pasid_state->lock, flags);
579 atomic_inc(&pasid_state->pri[tag].inflight);
580 if (finish)
581 pasid_state->pri[tag].finish = true;
582 spin_unlock_irqrestore(&pasid_state->lock, flags);
583
584 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
585 if (fault == NULL) {
586 /* We are OOM - send success and let the device re-fault */
587 finish_pri_tag(dev_state, pasid_state, tag);
588 goto out_drop_state;
589 }
590
591 fault->dev_state = dev_state;
592 fault->address = iommu_fault->address;
593 fault->state = pasid_state;
594 fault->tag = tag;
595 fault->finish = finish;
596 fault->flags = iommu_fault->flags;
597 INIT_WORK(&fault->work, do_fault);
598
599 queue_work(iommu_wq, &fault->work);
600
601 ret = NOTIFY_OK;
602
603out_drop_state:
604 put_device_state(dev_state);
605
606out:
607 return ret;
608}
609
610static struct notifier_block ppr_nb = {
611 .notifier_call = ppr_notifier,
612};
613
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100614static int task_exit(struct notifier_block *nb, unsigned long e, void *data)
615{
616 struct pasid_state *pasid_state;
617 struct task_struct *task;
618
619 task = data;
620
621 /*
622 * Using this notifier is a hack - but there is no other choice
623 * at the moment. What I really want is a sleeping notifier that
624 * is called when an MM goes down. But such a notifier doesn't
625 * exist yet. The notifier needs to sleep because it has to make
626 * sure that the device does not use the PASID and the address
627 * space anymore before it is destroyed. This includes waiting
628 * for pending PRI requests to pass the workqueue. The
629 * MMU-Notifiers would be a good fit, but they use RCU and so
630 * they are not allowed to sleep. Lets see how we can solve this
631 * in a more intelligent way in the future.
632 */
633again:
634 spin_lock(&ps_lock);
635 list_for_each_entry(pasid_state, &pasid_state_list, list) {
636 struct device_state *dev_state;
637 int pasid;
638
639 if (pasid_state->task != task)
640 continue;
641
642 /* Drop Lock and unbind */
643 spin_unlock(&ps_lock);
644
645 dev_state = pasid_state->device_state;
646 pasid = pasid_state->pasid;
647
Joerg Roedelbc216622011-12-07 12:24:42 +0100648 if (pasid_state->device_state->inv_ctx_cb)
649 dev_state->inv_ctx_cb(dev_state->pdev, pasid);
650
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100651 unbind_pasid(dev_state, pasid);
652
653 /* Task may be in the list multiple times */
654 goto again;
655 }
656 spin_unlock(&ps_lock);
657
658 return NOTIFY_OK;
659}
660
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100661int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
662 struct task_struct *task)
663{
664 struct pasid_state *pasid_state;
665 struct device_state *dev_state;
666 u16 devid;
667 int ret;
668
669 might_sleep();
670
671 if (!amd_iommu_v2_supported())
672 return -ENODEV;
673
674 devid = device_id(pdev);
675 dev_state = get_device_state(devid);
676
677 if (dev_state == NULL)
678 return -EINVAL;
679
680 ret = -EINVAL;
681 if (pasid < 0 || pasid >= dev_state->max_pasids)
682 goto out;
683
684 ret = -ENOMEM;
685 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
686 if (pasid_state == NULL)
687 goto out;
688
689 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100690 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200691 spin_lock_init(&pasid_state->lock);
692
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100693 pasid_state->task = task;
694 pasid_state->mm = get_task_mm(task);
695 pasid_state->device_state = dev_state;
696 pasid_state->pasid = pasid;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100697 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100698
699 if (pasid_state->mm == NULL)
700 goto out_free;
701
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100702 mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
703
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100704 ret = set_pasid_state(dev_state, pasid_state, pasid);
705 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100706 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100707
708 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
709 __pa(pasid_state->mm->pgd));
710 if (ret)
711 goto out_clear_state;
712
713 link_pasid_state(pasid_state);
714
715 return 0;
716
717out_clear_state:
718 clear_pasid_state(dev_state, pasid);
719
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100720out_unregister:
721 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
722
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100723out_free:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100724 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100725
726out:
727 put_device_state(dev_state);
728
729 return ret;
730}
731EXPORT_SYMBOL(amd_iommu_bind_pasid);
732
733void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
734{
735 struct device_state *dev_state;
736 u16 devid;
737
738 might_sleep();
739
740 if (!amd_iommu_v2_supported())
741 return;
742
743 devid = device_id(pdev);
744 dev_state = get_device_state(devid);
745 if (dev_state == NULL)
746 return;
747
748 if (pasid < 0 || pasid >= dev_state->max_pasids)
749 goto out;
750
751 unbind_pasid(dev_state, pasid);
752
753out:
754 put_device_state(dev_state);
755}
756EXPORT_SYMBOL(amd_iommu_unbind_pasid);
757
Joerg Roedeled96f222011-11-23 17:30:39 +0100758int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
759{
760 struct device_state *dev_state;
761 unsigned long flags;
762 int ret, tmp;
763 u16 devid;
764
765 might_sleep();
766
767 if (!amd_iommu_v2_supported())
768 return -ENODEV;
769
770 if (pasids <= 0 || pasids > (PASID_MASK + 1))
771 return -EINVAL;
772
773 devid = device_id(pdev);
774
775 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
776 if (dev_state == NULL)
777 return -ENOMEM;
778
779 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100780 init_waitqueue_head(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100781 dev_state->pdev = pdev;
782
783 tmp = pasids;
784 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
785 dev_state->pasid_levels += 1;
786
787 atomic_set(&dev_state->count, 1);
788 dev_state->max_pasids = pasids;
789
790 ret = -ENOMEM;
791 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
792 if (dev_state->states == NULL)
793 goto out_free_dev_state;
794
795 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
796 if (dev_state->domain == NULL)
797 goto out_free_states;
798
799 amd_iommu_domain_direct_map(dev_state->domain);
800
801 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
802 if (ret)
803 goto out_free_domain;
804
805 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
806 if (ret != 0)
807 goto out_free_domain;
808
809 spin_lock_irqsave(&state_lock, flags);
810
811 if (state_table[devid] != NULL) {
812 spin_unlock_irqrestore(&state_lock, flags);
813 ret = -EBUSY;
814 goto out_free_domain;
815 }
816
817 state_table[devid] = dev_state;
818
819 spin_unlock_irqrestore(&state_lock, flags);
820
821 return 0;
822
823out_free_domain:
824 iommu_domain_free(dev_state->domain);
825
826out_free_states:
827 free_page((unsigned long)dev_state->states);
828
829out_free_dev_state:
830 kfree(dev_state);
831
832 return ret;
833}
834EXPORT_SYMBOL(amd_iommu_init_device);
835
836void amd_iommu_free_device(struct pci_dev *pdev)
837{
838 struct device_state *dev_state;
839 unsigned long flags;
840 u16 devid;
841
842 if (!amd_iommu_v2_supported())
843 return;
844
845 devid = device_id(pdev);
846
847 spin_lock_irqsave(&state_lock, flags);
848
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200849 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100850 if (dev_state == NULL) {
851 spin_unlock_irqrestore(&state_lock, flags);
852 return;
853 }
854
855 state_table[devid] = NULL;
856
857 spin_unlock_irqrestore(&state_lock, flags);
858
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100859 /* Get rid of any remaining pasid states */
860 free_pasid_states(dev_state);
861
Joerg Roedel028eeac2011-11-24 12:48:13 +0100862 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100863}
864EXPORT_SYMBOL(amd_iommu_free_device);
865
Joerg Roedel175d6142011-11-28 14:36:36 +0100866int 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;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200882 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100883 if (dev_state == NULL)
884 goto out_unlock;
885
886 dev_state->inv_ppr_cb = cb;
887
888 ret = 0;
889
890out_unlock:
891 spin_unlock_irqrestore(&state_lock, flags);
892
893 return ret;
894}
895EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
896
Joerg Roedelbc216622011-12-07 12:24:42 +0100897int 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;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200913 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100914 if (dev_state == NULL)
915 goto out_unlock;
916
917 dev_state->inv_ctx_cb = cb;
918
919 ret = 0;
920
921out_unlock:
922 spin_unlock_irqrestore(&state_lock, flags);
923
924 return ret;
925}
926EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
927
Joerg Roedele3c495c2011-11-09 12:31:15 +0100928static int __init amd_iommu_v2_init(void)
929{
Joerg Roedeled96f222011-11-23 17:30:39 +0100930 size_t state_table_size;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100931 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100932
Joerg Roedel474d567d2012-03-15 12:46:40 +0100933 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
934
935 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900936 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100937 /*
938 * Load anyway to provide the symbols to other modules
939 * which may use AMD IOMMUv2 optionally.
940 */
941 return 0;
942 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100943
Joerg Roedeled96f222011-11-23 17:30:39 +0100944 spin_lock_init(&state_lock);
945
946 state_table_size = MAX_DEVICES * sizeof(struct device_state *);
947 state_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
948 get_order(state_table_size));
949 if (state_table == NULL)
950 return -ENOMEM;
951
Joerg Roedel028eeac2011-11-24 12:48:13 +0100952 ret = -ENOMEM;
953 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100954 if (iommu_wq == NULL)
Joerg Roedel028eeac2011-11-24 12:48:13 +0100955 goto out_free;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100956
957 ret = -ENOMEM;
958 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
959 if (empty_page_table == NULL)
960 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100961
962 amd_iommu_register_ppr_notifier(&ppr_nb);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100963 profile_event_register(PROFILE_TASK_EXIT, &profile_nb);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100964
Joerg Roedele3c495c2011-11-09 12:31:15 +0100965 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100966
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100967out_destroy_wq:
968 destroy_workqueue(iommu_wq);
969
Joerg Roedel028eeac2011-11-24 12:48:13 +0100970out_free:
971 free_pages((unsigned long)state_table, get_order(state_table_size));
972
973 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100974}
975
976static void __exit amd_iommu_v2_exit(void)
977{
Joerg Roedeled96f222011-11-23 17:30:39 +0100978 struct device_state *dev_state;
979 size_t state_table_size;
980 int i;
981
Joerg Roedel474d567d2012-03-15 12:46:40 +0100982 if (!amd_iommu_v2_supported())
983 return;
984
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100985 profile_event_unregister(PROFILE_TASK_EXIT, &profile_nb);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100986 amd_iommu_unregister_ppr_notifier(&ppr_nb);
987
988 flush_workqueue(iommu_wq);
989
990 /*
991 * The loop below might call flush_workqueue(), so call
992 * destroy_workqueue() after it
993 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100994 for (i = 0; i < MAX_DEVICES; ++i) {
995 dev_state = get_device_state(i);
996
997 if (dev_state == NULL)
998 continue;
999
1000 WARN_ON_ONCE(1);
1001
Joerg Roedeled96f222011-11-23 17:30:39 +01001002 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +01001003 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +01001004 }
1005
Joerg Roedel028eeac2011-11-24 12:48:13 +01001006 destroy_workqueue(iommu_wq);
1007
Joerg Roedeled96f222011-11-23 17:30:39 +01001008 state_table_size = MAX_DEVICES * sizeof(struct device_state *);
1009 free_pages((unsigned long)state_table, get_order(state_table_size));
Joerg Roedel8736b2c2011-11-24 16:21:52 +01001010
1011 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +01001012}
1013
1014module_init(amd_iommu_v2_init);
1015module_exit(amd_iommu_v2_exit);