blob: 0e29f6f66e834d8e50d661e4c02cc383d2ba0f24 [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
316 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
317
318 put_pasid_state(pasid_state); /* Reference taken in bind() function */
319}
320
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100321static void unbind_pasid(struct device_state *dev_state, int pasid)
322{
323 struct pasid_state *pasid_state;
324
325 pasid_state = get_pasid_state(dev_state, pasid);
326 if (pasid_state == NULL)
327 return;
328
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100329 __unbind_pasid(pasid_state);
330 put_pasid_state_wait(pasid_state); /* Reference taken in this function */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100331}
332
333static void free_pasid_states_level1(struct pasid_state **tbl)
334{
335 int i;
336
337 for (i = 0; i < 512; ++i) {
338 if (tbl[i] == NULL)
339 continue;
340
341 free_page((unsigned long)tbl[i]);
342 }
343}
344
345static void free_pasid_states_level2(struct pasid_state **tbl)
346{
347 struct pasid_state **ptr;
348 int i;
349
350 for (i = 0; i < 512; ++i) {
351 if (tbl[i] == NULL)
352 continue;
353
354 ptr = (struct pasid_state **)tbl[i];
355 free_pasid_states_level1(ptr);
356 }
357}
358
359static void free_pasid_states(struct device_state *dev_state)
360{
361 struct pasid_state *pasid_state;
362 int i;
363
364 for (i = 0; i < dev_state->max_pasids; ++i) {
365 pasid_state = get_pasid_state(dev_state, i);
366 if (pasid_state == NULL)
367 continue;
368
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100369 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200370
371 /*
372 * This will call the mn_release function and
373 * unbind the PASID
374 */
375 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100376 }
377
378 if (dev_state->pasid_levels == 2)
379 free_pasid_states_level2(dev_state->states);
380 else if (dev_state->pasid_levels == 1)
381 free_pasid_states_level1(dev_state->states);
382 else if (dev_state->pasid_levels != 0)
383 BUG();
384
385 free_page((unsigned long)dev_state->states);
386}
387
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100388static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
389{
390 return container_of(mn, struct pasid_state, mn);
391}
392
393static void __mn_flush_page(struct mmu_notifier *mn,
394 unsigned long address)
395{
396 struct pasid_state *pasid_state;
397 struct device_state *dev_state;
398
399 pasid_state = mn_to_state(mn);
400 dev_state = pasid_state->device_state;
401
402 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
403}
404
405static int mn_clear_flush_young(struct mmu_notifier *mn,
406 struct mm_struct *mm,
407 unsigned long address)
408{
409 __mn_flush_page(mn, address);
410
411 return 0;
412}
413
414static void mn_change_pte(struct mmu_notifier *mn,
415 struct mm_struct *mm,
416 unsigned long address,
417 pte_t pte)
418{
419 __mn_flush_page(mn, address);
420}
421
422static void mn_invalidate_page(struct mmu_notifier *mn,
423 struct mm_struct *mm,
424 unsigned long address)
425{
426 __mn_flush_page(mn, address);
427}
428
429static void mn_invalidate_range_start(struct mmu_notifier *mn,
430 struct mm_struct *mm,
431 unsigned long start, unsigned long end)
432{
433 struct pasid_state *pasid_state;
434 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200435 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100436
437 pasid_state = mn_to_state(mn);
438 dev_state = pasid_state->device_state;
439
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200440 spin_lock_irqsave(&pasid_state->lock, flags);
441 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200442 amd_iommu_domain_set_gcr3(dev_state->domain,
443 pasid_state->pasid,
444 __pa(empty_page_table));
445 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200446 pasid_state->mmu_notifier_count += 1;
447 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100448}
449
450static void mn_invalidate_range_end(struct mmu_notifier *mn,
451 struct mm_struct *mm,
452 unsigned long start, unsigned long end)
453{
454 struct pasid_state *pasid_state;
455 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200456 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100457
458 pasid_state = mn_to_state(mn);
459 dev_state = pasid_state->device_state;
460
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200461 spin_lock_irqsave(&pasid_state->lock, flags);
462 pasid_state->mmu_notifier_count -= 1;
463 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200464 amd_iommu_domain_set_gcr3(dev_state->domain,
465 pasid_state->pasid,
466 __pa(pasid_state->mm->pgd));
467 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200468 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100469}
470
Joerg Roedela40d4c62014-05-20 23:18:24 +0200471static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
472{
473 struct pasid_state *pasid_state;
474 struct device_state *dev_state;
475
476 might_sleep();
477
478 pasid_state = mn_to_state(mn);
479 dev_state = pasid_state->device_state;
480
481 if (pasid_state->device_state->inv_ctx_cb)
482 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
483
484 unbind_pasid(dev_state, pasid_state->pasid);
485}
486
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100487static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200488 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100489 .clear_flush_young = mn_clear_flush_young,
490 .change_pte = mn_change_pte,
491 .invalidate_page = mn_invalidate_page,
492 .invalidate_range_start = mn_invalidate_range_start,
493 .invalidate_range_end = mn_invalidate_range_end,
494};
495
Joerg Roedel028eeac2011-11-24 12:48:13 +0100496static void set_pri_tag_status(struct pasid_state *pasid_state,
497 u16 tag, int status)
498{
499 unsigned long flags;
500
501 spin_lock_irqsave(&pasid_state->lock, flags);
502 pasid_state->pri[tag].status = status;
503 spin_unlock_irqrestore(&pasid_state->lock, flags);
504}
505
506static void finish_pri_tag(struct device_state *dev_state,
507 struct pasid_state *pasid_state,
508 u16 tag)
509{
510 unsigned long flags;
511
512 spin_lock_irqsave(&pasid_state->lock, flags);
513 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
514 pasid_state->pri[tag].finish) {
515 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
516 pasid_state->pri[tag].status, tag);
517 pasid_state->pri[tag].finish = false;
518 pasid_state->pri[tag].status = PPR_SUCCESS;
519 }
520 spin_unlock_irqrestore(&pasid_state->lock, flags);
521}
522
523static void do_fault(struct work_struct *work)
524{
525 struct fault *fault = container_of(work, struct fault, work);
526 int npages, write;
527 struct page *page;
528
529 write = !!(fault->flags & PPR_FAULT_WRITE);
530
Jay Cornwall4378d992014-04-28 17:27:46 -0500531 down_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100532 npages = get_user_pages(fault->state->task, fault->state->mm,
533 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500534 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100535
Joerg Roedel175d6142011-11-28 14:36:36 +0100536 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100537 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100538 } else if (fault->dev_state->inv_ppr_cb) {
539 int status;
540
541 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
542 fault->pasid,
543 fault->address,
544 fault->flags);
545 switch (status) {
546 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
547 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
548 break;
549 case AMD_IOMMU_INV_PRI_RSP_INVALID:
550 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
551 break;
552 case AMD_IOMMU_INV_PRI_RSP_FAIL:
553 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
554 break;
555 default:
556 BUG();
557 }
558 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100559 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100560 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100561
562 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
563
564 put_pasid_state(fault->state);
565
566 kfree(fault);
567}
568
569static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
570{
571 struct amd_iommu_fault *iommu_fault;
572 struct pasid_state *pasid_state;
573 struct device_state *dev_state;
574 unsigned long flags;
575 struct fault *fault;
576 bool finish;
577 u16 tag;
578 int ret;
579
580 iommu_fault = data;
581 tag = iommu_fault->tag & 0x1ff;
582 finish = (iommu_fault->tag >> 9) & 1;
583
584 ret = NOTIFY_DONE;
585 dev_state = get_device_state(iommu_fault->device_id);
586 if (dev_state == NULL)
587 goto out;
588
589 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
590 if (pasid_state == NULL) {
591 /* We know the device but not the PASID -> send INVALID */
592 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
593 PPR_INVALID, tag);
594 goto out_drop_state;
595 }
596
597 spin_lock_irqsave(&pasid_state->lock, flags);
598 atomic_inc(&pasid_state->pri[tag].inflight);
599 if (finish)
600 pasid_state->pri[tag].finish = true;
601 spin_unlock_irqrestore(&pasid_state->lock, flags);
602
603 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
604 if (fault == NULL) {
605 /* We are OOM - send success and let the device re-fault */
606 finish_pri_tag(dev_state, pasid_state, tag);
607 goto out_drop_state;
608 }
609
610 fault->dev_state = dev_state;
611 fault->address = iommu_fault->address;
612 fault->state = pasid_state;
613 fault->tag = tag;
614 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300615 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100616 fault->flags = iommu_fault->flags;
617 INIT_WORK(&fault->work, do_fault);
618
619 queue_work(iommu_wq, &fault->work);
620
621 ret = NOTIFY_OK;
622
623out_drop_state:
624 put_device_state(dev_state);
625
626out:
627 return ret;
628}
629
630static struct notifier_block ppr_nb = {
631 .notifier_call = ppr_notifier,
632};
633
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100634int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
635 struct task_struct *task)
636{
637 struct pasid_state *pasid_state;
638 struct device_state *dev_state;
639 u16 devid;
640 int ret;
641
642 might_sleep();
643
644 if (!amd_iommu_v2_supported())
645 return -ENODEV;
646
647 devid = device_id(pdev);
648 dev_state = get_device_state(devid);
649
650 if (dev_state == NULL)
651 return -EINVAL;
652
653 ret = -EINVAL;
654 if (pasid < 0 || pasid >= dev_state->max_pasids)
655 goto out;
656
657 ret = -ENOMEM;
658 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
659 if (pasid_state == NULL)
660 goto out;
661
662 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100663 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200664 spin_lock_init(&pasid_state->lock);
665
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100666 pasid_state->task = task;
667 pasid_state->mm = get_task_mm(task);
668 pasid_state->device_state = dev_state;
669 pasid_state->pasid = pasid;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100670 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100671
672 if (pasid_state->mm == NULL)
673 goto out_free;
674
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100675 mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
676
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100677 ret = set_pasid_state(dev_state, pasid_state, pasid);
678 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100679 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100680
681 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
682 __pa(pasid_state->mm->pgd));
683 if (ret)
684 goto out_clear_state;
685
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100686 return 0;
687
688out_clear_state:
689 clear_pasid_state(dev_state, pasid);
690
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100691out_unregister:
692 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
693
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100694out_free:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100695 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100696
697out:
698 put_device_state(dev_state);
699
700 return ret;
701}
702EXPORT_SYMBOL(amd_iommu_bind_pasid);
703
704void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
705{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200706 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100707 struct device_state *dev_state;
708 u16 devid;
709
710 might_sleep();
711
712 if (!amd_iommu_v2_supported())
713 return;
714
715 devid = device_id(pdev);
716 dev_state = get_device_state(devid);
717 if (dev_state == NULL)
718 return;
719
720 if (pasid < 0 || pasid >= dev_state->max_pasids)
721 goto out;
722
Joerg Roedela40d4c62014-05-20 23:18:24 +0200723 pasid_state = get_pasid_state(dev_state, pasid);
724 if (pasid_state == NULL)
725 goto out;
726 /*
727 * Drop reference taken here. We are safe because we still hold
728 * the reference taken in the amd_iommu_bind_pasid function.
729 */
730 put_pasid_state(pasid_state);
731
732 /* This will call the mn_release function and unbind the PASID */
733 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100734
735out:
736 put_device_state(dev_state);
737}
738EXPORT_SYMBOL(amd_iommu_unbind_pasid);
739
Joerg Roedeled96f222011-11-23 17:30:39 +0100740int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
741{
742 struct device_state *dev_state;
743 unsigned long flags;
744 int ret, tmp;
745 u16 devid;
746
747 might_sleep();
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);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100762 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200763 dev_state->pdev = pdev;
764 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100765
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 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
789 if (ret != 0)
790 goto out_free_domain;
791
792 spin_lock_irqsave(&state_lock, flags);
793
Joerg Roedel741669c2014-05-20 23:18:23 +0200794 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100795 spin_unlock_irqrestore(&state_lock, flags);
796 ret = -EBUSY;
797 goto out_free_domain;
798 }
799
Joerg Roedel741669c2014-05-20 23:18:23 +0200800 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100801
802 spin_unlock_irqrestore(&state_lock, flags);
803
804 return 0;
805
806out_free_domain:
807 iommu_domain_free(dev_state->domain);
808
809out_free_states:
810 free_page((unsigned long)dev_state->states);
811
812out_free_dev_state:
813 kfree(dev_state);
814
815 return ret;
816}
817EXPORT_SYMBOL(amd_iommu_init_device);
818
819void amd_iommu_free_device(struct pci_dev *pdev)
820{
821 struct device_state *dev_state;
822 unsigned long flags;
823 u16 devid;
824
825 if (!amd_iommu_v2_supported())
826 return;
827
828 devid = device_id(pdev);
829
830 spin_lock_irqsave(&state_lock, flags);
831
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200832 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100833 if (dev_state == NULL) {
834 spin_unlock_irqrestore(&state_lock, flags);
835 return;
836 }
837
Joerg Roedel741669c2014-05-20 23:18:23 +0200838 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100839
840 spin_unlock_irqrestore(&state_lock, flags);
841
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100842 /* Get rid of any remaining pasid states */
843 free_pasid_states(dev_state);
844
Joerg Roedel028eeac2011-11-24 12:48:13 +0100845 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100846}
847EXPORT_SYMBOL(amd_iommu_free_device);
848
Joerg Roedel175d6142011-11-28 14:36:36 +0100849int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
850 amd_iommu_invalid_ppr_cb cb)
851{
852 struct device_state *dev_state;
853 unsigned long flags;
854 u16 devid;
855 int ret;
856
857 if (!amd_iommu_v2_supported())
858 return -ENODEV;
859
860 devid = device_id(pdev);
861
862 spin_lock_irqsave(&state_lock, flags);
863
864 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200865 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100866 if (dev_state == NULL)
867 goto out_unlock;
868
869 dev_state->inv_ppr_cb = cb;
870
871 ret = 0;
872
873out_unlock:
874 spin_unlock_irqrestore(&state_lock, flags);
875
876 return ret;
877}
878EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
879
Joerg Roedelbc216622011-12-07 12:24:42 +0100880int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
881 amd_iommu_invalidate_ctx cb)
882{
883 struct device_state *dev_state;
884 unsigned long flags;
885 u16 devid;
886 int ret;
887
888 if (!amd_iommu_v2_supported())
889 return -ENODEV;
890
891 devid = device_id(pdev);
892
893 spin_lock_irqsave(&state_lock, flags);
894
895 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200896 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100897 if (dev_state == NULL)
898 goto out_unlock;
899
900 dev_state->inv_ctx_cb = cb;
901
902 ret = 0;
903
904out_unlock:
905 spin_unlock_irqrestore(&state_lock, flags);
906
907 return ret;
908}
909EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
910
Joerg Roedele3c495c2011-11-09 12:31:15 +0100911static int __init amd_iommu_v2_init(void)
912{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100913 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100914
Joerg Roedel474d567d2012-03-15 12:46:40 +0100915 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
916
917 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900918 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100919 /*
920 * Load anyway to provide the symbols to other modules
921 * which may use AMD IOMMUv2 optionally.
922 */
923 return 0;
924 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100925
Joerg Roedeled96f222011-11-23 17:30:39 +0100926 spin_lock_init(&state_lock);
927
Joerg Roedel028eeac2011-11-24 12:48:13 +0100928 ret = -ENOMEM;
929 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100930 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200931 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100932
933 ret = -ENOMEM;
934 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
935 if (empty_page_table == NULL)
936 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100937
938 amd_iommu_register_ppr_notifier(&ppr_nb);
939
Joerg Roedele3c495c2011-11-09 12:31:15 +0100940 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100941
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100942out_destroy_wq:
943 destroy_workqueue(iommu_wq);
944
Joerg Roedel741669c2014-05-20 23:18:23 +0200945out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100946 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100947}
948
949static void __exit amd_iommu_v2_exit(void)
950{
Joerg Roedeled96f222011-11-23 17:30:39 +0100951 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100952 int i;
953
Joerg Roedel474d567d2012-03-15 12:46:40 +0100954 if (!amd_iommu_v2_supported())
955 return;
956
Joerg Roedel028eeac2011-11-24 12:48:13 +0100957 amd_iommu_unregister_ppr_notifier(&ppr_nb);
958
959 flush_workqueue(iommu_wq);
960
961 /*
962 * The loop below might call flush_workqueue(), so call
963 * destroy_workqueue() after it
964 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100965 for (i = 0; i < MAX_DEVICES; ++i) {
966 dev_state = get_device_state(i);
967
968 if (dev_state == NULL)
969 continue;
970
971 WARN_ON_ONCE(1);
972
Joerg Roedeled96f222011-11-23 17:30:39 +0100973 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100974 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100975 }
976
Joerg Roedel028eeac2011-11-24 12:48:13 +0100977 destroy_workqueue(iommu_wq);
978
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100979 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100980}
981
982module_init(amd_iommu_v2_init);
983module_exit(amd_iommu_v2_exit);