blob: 1fdd22c3bc0481c35e07376f26c45e77c17d418f [file] [log] [blame]
Joerg Roedele3c495c2011-11-09 12:31:15 +01001/*
2 * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
Joerg Roedel8736b2c2011-11-24 16:21:52 +010019#include <linux/mmu_notifier.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010020#include <linux/amd-iommu.h>
21#include <linux/mm_types.h>
Joerg Roedel8736b2c2011-11-24 16:21:52 +010022#include <linux/profile.h>
Joerg Roedele3c495c2011-11-09 12:31:15 +010023#include <linux/module.h>
Joerg Roedel2d5503b2011-11-24 10:41:57 +010024#include <linux/sched.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010025#include <linux/iommu.h>
Joerg Roedel028eeac2011-11-24 12:48:13 +010026#include <linux/wait.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010027#include <linux/pci.h>
28#include <linux/gfp.h>
29
Joerg Roedel028eeac2011-11-24 12:48:13 +010030#include "amd_iommu_types.h"
Joerg Roedeled96f222011-11-23 17:30:39 +010031#include "amd_iommu_proto.h"
Joerg Roedele3c495c2011-11-09 12:31:15 +010032
33MODULE_LICENSE("GPL v2");
34MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>");
35
Joerg Roedeled96f222011-11-23 17:30:39 +010036#define MAX_DEVICES 0x10000
37#define PRI_QUEUE_SIZE 512
38
39struct pri_queue {
40 atomic_t inflight;
41 bool finish;
Joerg Roedel028eeac2011-11-24 12:48:13 +010042 int status;
Joerg Roedeled96f222011-11-23 17:30:39 +010043};
44
45struct pasid_state {
46 struct list_head list; /* For global state-list */
47 atomic_t count; /* Reference count */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020048 unsigned mmu_notifier_count; /* Counting nested mmu_notifier
Joerg Roedele79df312014-05-20 23:18:26 +020049 calls */
Joerg Roedeled96f222011-11-23 17:30:39 +010050 struct task_struct *task; /* Task bound to this PASID */
51 struct mm_struct *mm; /* mm_struct for the faults */
Joerg Roedelff6d0cc2014-07-08 12:49:50 +020052 struct mmu_notifier mn; /* mmu_notifier handle */
Joerg Roedeled96f222011-11-23 17:30:39 +010053 struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
54 struct device_state *device_state; /* Link to our device_state */
55 int pasid; /* PASID index */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020056 spinlock_t lock; /* Protect pri_queues and
57 mmu_notifer_count */
Joerg Roedel028eeac2011-11-24 12:48:13 +010058 wait_queue_head_t wq; /* To wait for count == 0 */
Joerg Roedeled96f222011-11-23 17:30:39 +010059};
60
61struct device_state {
Joerg Roedel741669c2014-05-20 23:18:23 +020062 struct list_head list;
63 u16 devid;
Joerg Roedeled96f222011-11-23 17:30:39 +010064 atomic_t count;
65 struct pci_dev *pdev;
66 struct pasid_state **states;
67 struct iommu_domain *domain;
68 int pasid_levels;
69 int max_pasids;
Joerg Roedel175d6142011-11-28 14:36:36 +010070 amd_iommu_invalid_ppr_cb inv_ppr_cb;
Joerg Roedelbc216622011-12-07 12:24:42 +010071 amd_iommu_invalidate_ctx inv_ctx_cb;
Joerg Roedeled96f222011-11-23 17:30:39 +010072 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010073 wait_queue_head_t wq;
74};
75
76struct fault {
77 struct work_struct work;
78 struct device_state *dev_state;
79 struct pasid_state *state;
80 struct mm_struct *mm;
81 u64 address;
82 u16 devid;
83 u16 pasid;
84 u16 tag;
85 u16 finish;
86 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010087};
88
Joerg Roedel741669c2014-05-20 23:18:23 +020089static LIST_HEAD(state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +010090static spinlock_t state_lock;
91
Joerg Roedel028eeac2011-11-24 12:48:13 +010092static struct workqueue_struct *iommu_wq;
93
Joerg Roedel8736b2c2011-11-24 16:21:52 +010094/*
95 * Empty page table - Used between
96 * mmu_notifier_invalidate_range_start and
97 * mmu_notifier_invalidate_range_end
98 */
99static u64 *empty_page_table;
100
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100101static void free_pasid_states(struct device_state *dev_state);
102static void unbind_pasid(struct device_state *dev_state, int pasid);
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{
Joerg Roedel741669c2014-05-20 23:18:23 +0200116 struct device_state *dev_state;
117
118 list_for_each_entry(dev_state, &state_list, list) {
119 if (dev_state->devid == devid)
120 return dev_state;
121 }
122
123 return NULL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200124}
125
Joerg Roedeled96f222011-11-23 17:30:39 +0100126static struct device_state *get_device_state(u16 devid)
127{
128 struct device_state *dev_state;
129 unsigned long flags;
130
131 spin_lock_irqsave(&state_lock, flags);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200132 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100133 if (dev_state != NULL)
134 atomic_inc(&dev_state->count);
135 spin_unlock_irqrestore(&state_lock, flags);
136
137 return dev_state;
138}
139
140static void free_device_state(struct device_state *dev_state)
141{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100142 /*
143 * First detach device from domain - No more PRI requests will arrive
144 * from that device after it is unbound from the IOMMUv2 domain.
145 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100146 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100147
148 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100149 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100150
151 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100152 kfree(dev_state);
153}
154
155static void put_device_state(struct device_state *dev_state)
156{
157 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100158 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100159}
160
Joerg Roedel028eeac2011-11-24 12:48:13 +0100161static void put_device_state_wait(struct device_state *dev_state)
162{
163 DEFINE_WAIT(wait);
164
165 prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
166 if (!atomic_dec_and_test(&dev_state->count))
167 schedule();
168 finish_wait(&dev_state->wq, &wait);
169
170 free_device_state(dev_state);
171}
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100172
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100173/* Must be called under dev_state->lock */
174static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
175 int pasid, bool alloc)
176{
177 struct pasid_state **root, **ptr;
178 int level, index;
179
180 level = dev_state->pasid_levels;
181 root = dev_state->states;
182
183 while (true) {
184
185 index = (pasid >> (9 * level)) & 0x1ff;
186 ptr = &root[index];
187
188 if (level == 0)
189 break;
190
191 if (*ptr == NULL) {
192 if (!alloc)
193 return NULL;
194
195 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
196 if (*ptr == NULL)
197 return NULL;
198 }
199
200 root = (struct pasid_state **)*ptr;
201 level -= 1;
202 }
203
204 return ptr;
205}
206
207static int set_pasid_state(struct device_state *dev_state,
208 struct pasid_state *pasid_state,
209 int pasid)
210{
211 struct pasid_state **ptr;
212 unsigned long flags;
213 int ret;
214
215 spin_lock_irqsave(&dev_state->lock, flags);
216 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
217
218 ret = -ENOMEM;
219 if (ptr == NULL)
220 goto out_unlock;
221
222 ret = -ENOMEM;
223 if (*ptr != NULL)
224 goto out_unlock;
225
226 *ptr = pasid_state;
227
228 ret = 0;
229
230out_unlock:
231 spin_unlock_irqrestore(&dev_state->lock, flags);
232
233 return ret;
234}
235
236static void clear_pasid_state(struct device_state *dev_state, int pasid)
237{
238 struct pasid_state **ptr;
239 unsigned long flags;
240
241 spin_lock_irqsave(&dev_state->lock, flags);
242 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
243
244 if (ptr == NULL)
245 goto out_unlock;
246
247 *ptr = NULL;
248
249out_unlock:
250 spin_unlock_irqrestore(&dev_state->lock, flags);
251}
252
253static struct pasid_state *get_pasid_state(struct device_state *dev_state,
254 int pasid)
255{
256 struct pasid_state **ptr, *ret = NULL;
257 unsigned long flags;
258
259 spin_lock_irqsave(&dev_state->lock, flags);
260 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
261
262 if (ptr == NULL)
263 goto out_unlock;
264
265 ret = *ptr;
266 if (ret)
267 atomic_inc(&ret->count);
268
269out_unlock:
270 spin_unlock_irqrestore(&dev_state->lock, flags);
271
272 return ret;
273}
274
275static void free_pasid_state(struct pasid_state *pasid_state)
276{
277 kfree(pasid_state);
278}
279
280static void put_pasid_state(struct pasid_state *pasid_state)
281{
282 if (atomic_dec_and_test(&pasid_state->count)) {
283 put_device_state(pasid_state->device_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100284 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100285 }
286}
287
Joerg Roedel028eeac2011-11-24 12:48:13 +0100288static void put_pasid_state_wait(struct pasid_state *pasid_state)
289{
290 DEFINE_WAIT(wait);
291
292 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
293
294 if (atomic_dec_and_test(&pasid_state->count))
295 put_device_state(pasid_state->device_state);
296 else
297 schedule();
298
299 finish_wait(&pasid_state->wq, &wait);
300 mmput(pasid_state->mm);
301 free_pasid_state(pasid_state);
302}
303
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100304static void __unbind_pasid(struct pasid_state *pasid_state)
305{
306 struct iommu_domain *domain;
307
308 domain = pasid_state->device_state->domain;
309
310 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
311 clear_pasid_state(pasid_state->device_state, pasid_state->pasid);
312
313 /* Make sure no more pending faults are in the queue */
314 flush_workqueue(iommu_wq);
315
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100316 put_pasid_state(pasid_state); /* Reference taken in bind() function */
317}
318
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100319static void unbind_pasid(struct device_state *dev_state, int pasid)
320{
321 struct pasid_state *pasid_state;
322
323 pasid_state = get_pasid_state(dev_state, pasid);
324 if (pasid_state == NULL)
325 return;
326
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100327 __unbind_pasid(pasid_state);
328 put_pasid_state_wait(pasid_state); /* Reference taken in this function */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100329}
330
331static void free_pasid_states_level1(struct pasid_state **tbl)
332{
333 int i;
334
335 for (i = 0; i < 512; ++i) {
336 if (tbl[i] == NULL)
337 continue;
338
339 free_page((unsigned long)tbl[i]);
340 }
341}
342
343static void free_pasid_states_level2(struct pasid_state **tbl)
344{
345 struct pasid_state **ptr;
346 int i;
347
348 for (i = 0; i < 512; ++i) {
349 if (tbl[i] == NULL)
350 continue;
351
352 ptr = (struct pasid_state **)tbl[i];
353 free_pasid_states_level1(ptr);
354 }
355}
356
357static void free_pasid_states(struct device_state *dev_state)
358{
359 struct pasid_state *pasid_state;
360 int i;
361
362 for (i = 0; i < dev_state->max_pasids; ++i) {
363 pasid_state = get_pasid_state(dev_state, i);
364 if (pasid_state == NULL)
365 continue;
366
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100367 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200368
369 /*
370 * This will call the mn_release function and
371 * unbind the PASID
372 */
373 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100374 }
375
376 if (dev_state->pasid_levels == 2)
377 free_pasid_states_level2(dev_state->states);
378 else if (dev_state->pasid_levels == 1)
379 free_pasid_states_level1(dev_state->states);
380 else if (dev_state->pasid_levels != 0)
381 BUG();
382
383 free_page((unsigned long)dev_state->states);
384}
385
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100386static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
387{
388 return container_of(mn, struct pasid_state, mn);
389}
390
391static void __mn_flush_page(struct mmu_notifier *mn,
392 unsigned long address)
393{
394 struct pasid_state *pasid_state;
395 struct device_state *dev_state;
396
397 pasid_state = mn_to_state(mn);
398 dev_state = pasid_state->device_state;
399
400 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
401}
402
403static int mn_clear_flush_young(struct mmu_notifier *mn,
404 struct mm_struct *mm,
405 unsigned long address)
406{
407 __mn_flush_page(mn, address);
408
409 return 0;
410}
411
412static void mn_change_pte(struct mmu_notifier *mn,
413 struct mm_struct *mm,
414 unsigned long address,
415 pte_t pte)
416{
417 __mn_flush_page(mn, address);
418}
419
420static void mn_invalidate_page(struct mmu_notifier *mn,
421 struct mm_struct *mm,
422 unsigned long address)
423{
424 __mn_flush_page(mn, address);
425}
426
427static void mn_invalidate_range_start(struct mmu_notifier *mn,
428 struct mm_struct *mm,
429 unsigned long start, unsigned long end)
430{
431 struct pasid_state *pasid_state;
432 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200433 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100434
435 pasid_state = mn_to_state(mn);
436 dev_state = pasid_state->device_state;
437
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200438 spin_lock_irqsave(&pasid_state->lock, flags);
439 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200440 amd_iommu_domain_set_gcr3(dev_state->domain,
441 pasid_state->pasid,
442 __pa(empty_page_table));
443 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200444 pasid_state->mmu_notifier_count += 1;
445 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100446}
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;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200454 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100455
456 pasid_state = mn_to_state(mn);
457 dev_state = pasid_state->device_state;
458
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200459 spin_lock_irqsave(&pasid_state->lock, flags);
460 pasid_state->mmu_notifier_count -= 1;
461 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200462 amd_iommu_domain_set_gcr3(dev_state->domain,
463 pasid_state->pasid,
464 __pa(pasid_state->mm->pgd));
465 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200466 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100467}
468
Joerg Roedela40d4c62014-05-20 23:18:24 +0200469static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
470{
471 struct pasid_state *pasid_state;
472 struct device_state *dev_state;
473
474 might_sleep();
475
476 pasid_state = mn_to_state(mn);
477 dev_state = pasid_state->device_state;
478
479 if (pasid_state->device_state->inv_ctx_cb)
480 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
481
482 unbind_pasid(dev_state, pasid_state->pasid);
483}
484
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100485static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200486 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100487 .clear_flush_young = mn_clear_flush_young,
488 .change_pte = mn_change_pte,
489 .invalidate_page = mn_invalidate_page,
490 .invalidate_range_start = mn_invalidate_range_start,
491 .invalidate_range_end = mn_invalidate_range_end,
492};
493
Joerg Roedel028eeac2011-11-24 12:48:13 +0100494static void set_pri_tag_status(struct pasid_state *pasid_state,
495 u16 tag, int status)
496{
497 unsigned long flags;
498
499 spin_lock_irqsave(&pasid_state->lock, flags);
500 pasid_state->pri[tag].status = status;
501 spin_unlock_irqrestore(&pasid_state->lock, flags);
502}
503
504static void finish_pri_tag(struct device_state *dev_state,
505 struct pasid_state *pasid_state,
506 u16 tag)
507{
508 unsigned long flags;
509
510 spin_lock_irqsave(&pasid_state->lock, flags);
511 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
512 pasid_state->pri[tag].finish) {
513 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
514 pasid_state->pri[tag].status, tag);
515 pasid_state->pri[tag].finish = false;
516 pasid_state->pri[tag].status = PPR_SUCCESS;
517 }
518 spin_unlock_irqrestore(&pasid_state->lock, flags);
519}
520
521static void do_fault(struct work_struct *work)
522{
523 struct fault *fault = container_of(work, struct fault, work);
524 int npages, write;
525 struct page *page;
526
527 write = !!(fault->flags & PPR_FAULT_WRITE);
528
Jay Cornwall4378d992014-04-28 17:27:46 -0500529 down_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100530 npages = get_user_pages(fault->state->task, fault->state->mm,
531 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500532 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100533
Joerg Roedel175d6142011-11-28 14:36:36 +0100534 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100535 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100536 } else if (fault->dev_state->inv_ppr_cb) {
537 int status;
538
539 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
540 fault->pasid,
541 fault->address,
542 fault->flags);
543 switch (status) {
544 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
545 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
546 break;
547 case AMD_IOMMU_INV_PRI_RSP_INVALID:
548 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
549 break;
550 case AMD_IOMMU_INV_PRI_RSP_FAIL:
551 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
552 break;
553 default:
554 BUG();
555 }
556 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100557 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100558 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100559
560 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
561
562 put_pasid_state(fault->state);
563
564 kfree(fault);
565}
566
567static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
568{
569 struct amd_iommu_fault *iommu_fault;
570 struct pasid_state *pasid_state;
571 struct device_state *dev_state;
572 unsigned long flags;
573 struct fault *fault;
574 bool finish;
575 u16 tag;
576 int ret;
577
578 iommu_fault = data;
579 tag = iommu_fault->tag & 0x1ff;
580 finish = (iommu_fault->tag >> 9) & 1;
581
582 ret = NOTIFY_DONE;
583 dev_state = get_device_state(iommu_fault->device_id);
584 if (dev_state == NULL)
585 goto out;
586
587 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
588 if (pasid_state == NULL) {
589 /* We know the device but not the PASID -> send INVALID */
590 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
591 PPR_INVALID, tag);
592 goto out_drop_state;
593 }
594
595 spin_lock_irqsave(&pasid_state->lock, flags);
596 atomic_inc(&pasid_state->pri[tag].inflight);
597 if (finish)
598 pasid_state->pri[tag].finish = true;
599 spin_unlock_irqrestore(&pasid_state->lock, flags);
600
601 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
602 if (fault == NULL) {
603 /* We are OOM - send success and let the device re-fault */
604 finish_pri_tag(dev_state, pasid_state, tag);
605 goto out_drop_state;
606 }
607
608 fault->dev_state = dev_state;
609 fault->address = iommu_fault->address;
610 fault->state = pasid_state;
611 fault->tag = tag;
612 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300613 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100614 fault->flags = iommu_fault->flags;
615 INIT_WORK(&fault->work, do_fault);
616
617 queue_work(iommu_wq, &fault->work);
618
619 ret = NOTIFY_OK;
620
621out_drop_state:
622 put_device_state(dev_state);
623
624out:
625 return ret;
626}
627
628static struct notifier_block ppr_nb = {
629 .notifier_call = ppr_notifier,
630};
631
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100632int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
633 struct task_struct *task)
634{
635 struct pasid_state *pasid_state;
636 struct device_state *dev_state;
637 u16 devid;
638 int ret;
639
640 might_sleep();
641
642 if (!amd_iommu_v2_supported())
643 return -ENODEV;
644
645 devid = device_id(pdev);
646 dev_state = get_device_state(devid);
647
648 if (dev_state == NULL)
649 return -EINVAL;
650
651 ret = -EINVAL;
652 if (pasid < 0 || pasid >= dev_state->max_pasids)
653 goto out;
654
655 ret = -ENOMEM;
656 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
657 if (pasid_state == NULL)
658 goto out;
659
660 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100661 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200662 spin_lock_init(&pasid_state->lock);
663
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100664 pasid_state->task = task;
665 pasid_state->mm = get_task_mm(task);
666 pasid_state->device_state = dev_state;
667 pasid_state->pasid = pasid;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100668 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100669
670 if (pasid_state->mm == NULL)
671 goto out_free;
672
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100673 mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
674
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100675 ret = set_pasid_state(dev_state, pasid_state, pasid);
676 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100677 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100678
679 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
680 __pa(pasid_state->mm->pgd));
681 if (ret)
682 goto out_clear_state;
683
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100684 return 0;
685
686out_clear_state:
687 clear_pasid_state(dev_state, pasid);
688
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100689out_unregister:
690 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
691
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100692out_free:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100693 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100694
695out:
696 put_device_state(dev_state);
697
698 return ret;
699}
700EXPORT_SYMBOL(amd_iommu_bind_pasid);
701
702void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
703{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200704 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100705 struct device_state *dev_state;
706 u16 devid;
707
708 might_sleep();
709
710 if (!amd_iommu_v2_supported())
711 return;
712
713 devid = device_id(pdev);
714 dev_state = get_device_state(devid);
715 if (dev_state == NULL)
716 return;
717
718 if (pasid < 0 || pasid >= dev_state->max_pasids)
719 goto out;
720
Joerg Roedela40d4c62014-05-20 23:18:24 +0200721 pasid_state = get_pasid_state(dev_state, pasid);
722 if (pasid_state == NULL)
723 goto out;
724 /*
725 * Drop reference taken here. We are safe because we still hold
726 * the reference taken in the amd_iommu_bind_pasid function.
727 */
728 put_pasid_state(pasid_state);
729
730 /* This will call the mn_release function and unbind the PASID */
731 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100732
733out:
734 put_device_state(dev_state);
735}
736EXPORT_SYMBOL(amd_iommu_unbind_pasid);
737
Joerg Roedeled96f222011-11-23 17:30:39 +0100738int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
739{
740 struct device_state *dev_state;
741 unsigned long flags;
742 int ret, tmp;
743 u16 devid;
744
745 might_sleep();
746
747 if (!amd_iommu_v2_supported())
748 return -ENODEV;
749
750 if (pasids <= 0 || pasids > (PASID_MASK + 1))
751 return -EINVAL;
752
753 devid = device_id(pdev);
754
755 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
756 if (dev_state == NULL)
757 return -ENOMEM;
758
759 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100760 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200761 dev_state->pdev = pdev;
762 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100763
764 tmp = pasids;
765 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
766 dev_state->pasid_levels += 1;
767
768 atomic_set(&dev_state->count, 1);
769 dev_state->max_pasids = pasids;
770
771 ret = -ENOMEM;
772 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
773 if (dev_state->states == NULL)
774 goto out_free_dev_state;
775
776 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
777 if (dev_state->domain == NULL)
778 goto out_free_states;
779
780 amd_iommu_domain_direct_map(dev_state->domain);
781
782 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
783 if (ret)
784 goto out_free_domain;
785
786 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
787 if (ret != 0)
788 goto out_free_domain;
789
790 spin_lock_irqsave(&state_lock, flags);
791
Joerg Roedel741669c2014-05-20 23:18:23 +0200792 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100793 spin_unlock_irqrestore(&state_lock, flags);
794 ret = -EBUSY;
795 goto out_free_domain;
796 }
797
Joerg Roedel741669c2014-05-20 23:18:23 +0200798 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100799
800 spin_unlock_irqrestore(&state_lock, flags);
801
802 return 0;
803
804out_free_domain:
805 iommu_domain_free(dev_state->domain);
806
807out_free_states:
808 free_page((unsigned long)dev_state->states);
809
810out_free_dev_state:
811 kfree(dev_state);
812
813 return ret;
814}
815EXPORT_SYMBOL(amd_iommu_init_device);
816
817void amd_iommu_free_device(struct pci_dev *pdev)
818{
819 struct device_state *dev_state;
820 unsigned long flags;
821 u16 devid;
822
823 if (!amd_iommu_v2_supported())
824 return;
825
826 devid = device_id(pdev);
827
828 spin_lock_irqsave(&state_lock, flags);
829
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200830 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100831 if (dev_state == NULL) {
832 spin_unlock_irqrestore(&state_lock, flags);
833 return;
834 }
835
Joerg Roedel741669c2014-05-20 23:18:23 +0200836 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100837
838 spin_unlock_irqrestore(&state_lock, flags);
839
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100840 /* Get rid of any remaining pasid states */
841 free_pasid_states(dev_state);
842
Joerg Roedel028eeac2011-11-24 12:48:13 +0100843 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100844}
845EXPORT_SYMBOL(amd_iommu_free_device);
846
Joerg Roedel175d6142011-11-28 14:36:36 +0100847int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
848 amd_iommu_invalid_ppr_cb cb)
849{
850 struct device_state *dev_state;
851 unsigned long flags;
852 u16 devid;
853 int ret;
854
855 if (!amd_iommu_v2_supported())
856 return -ENODEV;
857
858 devid = device_id(pdev);
859
860 spin_lock_irqsave(&state_lock, flags);
861
862 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200863 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100864 if (dev_state == NULL)
865 goto out_unlock;
866
867 dev_state->inv_ppr_cb = cb;
868
869 ret = 0;
870
871out_unlock:
872 spin_unlock_irqrestore(&state_lock, flags);
873
874 return ret;
875}
876EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
877
Joerg Roedelbc216622011-12-07 12:24:42 +0100878int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
879 amd_iommu_invalidate_ctx cb)
880{
881 struct device_state *dev_state;
882 unsigned long flags;
883 u16 devid;
884 int ret;
885
886 if (!amd_iommu_v2_supported())
887 return -ENODEV;
888
889 devid = device_id(pdev);
890
891 spin_lock_irqsave(&state_lock, flags);
892
893 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200894 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100895 if (dev_state == NULL)
896 goto out_unlock;
897
898 dev_state->inv_ctx_cb = cb;
899
900 ret = 0;
901
902out_unlock:
903 spin_unlock_irqrestore(&state_lock, flags);
904
905 return ret;
906}
907EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
908
Joerg Roedele3c495c2011-11-09 12:31:15 +0100909static int __init amd_iommu_v2_init(void)
910{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100911 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100912
Joerg Roedel474d567d2012-03-15 12:46:40 +0100913 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
914
915 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900916 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100917 /*
918 * Load anyway to provide the symbols to other modules
919 * which may use AMD IOMMUv2 optionally.
920 */
921 return 0;
922 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100923
Joerg Roedeled96f222011-11-23 17:30:39 +0100924 spin_lock_init(&state_lock);
925
Joerg Roedel028eeac2011-11-24 12:48:13 +0100926 ret = -ENOMEM;
927 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100928 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200929 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100930
931 ret = -ENOMEM;
932 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
933 if (empty_page_table == NULL)
934 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100935
936 amd_iommu_register_ppr_notifier(&ppr_nb);
937
Joerg Roedele3c495c2011-11-09 12:31:15 +0100938 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100939
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100940out_destroy_wq:
941 destroy_workqueue(iommu_wq);
942
Joerg Roedel741669c2014-05-20 23:18:23 +0200943out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100944 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100945}
946
947static void __exit amd_iommu_v2_exit(void)
948{
Joerg Roedeled96f222011-11-23 17:30:39 +0100949 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100950 int i;
951
Joerg Roedel474d567d2012-03-15 12:46:40 +0100952 if (!amd_iommu_v2_supported())
953 return;
954
Joerg Roedel028eeac2011-11-24 12:48:13 +0100955 amd_iommu_unregister_ppr_notifier(&ppr_nb);
956
957 flush_workqueue(iommu_wq);
958
959 /*
960 * The loop below might call flush_workqueue(), so call
961 * destroy_workqueue() after it
962 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100963 for (i = 0; i < MAX_DEVICES; ++i) {
964 dev_state = get_device_state(i);
965
966 if (dev_state == NULL)
967 continue;
968
969 WARN_ON_ONCE(1);
970
Joerg Roedeled96f222011-11-23 17:30:39 +0100971 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100972 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100973 }
974
Joerg Roedel028eeac2011-11-24 12:48:13 +0100975 destroy_workqueue(iommu_wq);
976
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100977 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100978}
979
980module_init(amd_iommu_v2_init);
981module_exit(amd_iommu_v2_exit);