blob: 6ba707b998ff136c8ca7cbdd83a0122342427ed5 [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);
Joerg Roedeled96f222011-11-23 17:30:39 +0100102
103static u16 device_id(struct pci_dev *pdev)
104{
105 u16 devid;
106
107 devid = pdev->bus->number;
108 devid = (devid << 8) | pdev->devfn;
109
110 return devid;
111}
112
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200113static struct device_state *__get_device_state(u16 devid)
114{
Joerg Roedel741669c2014-05-20 23:18:23 +0200115 struct device_state *dev_state;
116
117 list_for_each_entry(dev_state, &state_list, list) {
118 if (dev_state->devid == devid)
119 return dev_state;
120 }
121
122 return NULL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200123}
124
Joerg Roedeled96f222011-11-23 17:30:39 +0100125static struct device_state *get_device_state(u16 devid)
126{
127 struct device_state *dev_state;
128 unsigned long flags;
129
130 spin_lock_irqsave(&state_lock, flags);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200131 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100132 if (dev_state != NULL)
133 atomic_inc(&dev_state->count);
134 spin_unlock_irqrestore(&state_lock, flags);
135
136 return dev_state;
137}
138
139static void free_device_state(struct device_state *dev_state)
140{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100141 /*
142 * First detach device from domain - No more PRI requests will arrive
143 * from that device after it is unbound from the IOMMUv2 domain.
144 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100145 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100146
147 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100148 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100149
150 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100151 kfree(dev_state);
152}
153
154static void put_device_state(struct device_state *dev_state)
155{
156 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100157 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100158}
159
Joerg Roedel028eeac2011-11-24 12:48:13 +0100160static void put_device_state_wait(struct device_state *dev_state)
161{
162 DEFINE_WAIT(wait);
163
164 prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE);
165 if (!atomic_dec_and_test(&dev_state->count))
166 schedule();
167 finish_wait(&dev_state->wq, &wait);
168
169 free_device_state(dev_state);
170}
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100171
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100172/* Must be called under dev_state->lock */
173static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
174 int pasid, bool alloc)
175{
176 struct pasid_state **root, **ptr;
177 int level, index;
178
179 level = dev_state->pasid_levels;
180 root = dev_state->states;
181
182 while (true) {
183
184 index = (pasid >> (9 * level)) & 0x1ff;
185 ptr = &root[index];
186
187 if (level == 0)
188 break;
189
190 if (*ptr == NULL) {
191 if (!alloc)
192 return NULL;
193
194 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
195 if (*ptr == NULL)
196 return NULL;
197 }
198
199 root = (struct pasid_state **)*ptr;
200 level -= 1;
201 }
202
203 return ptr;
204}
205
206static int set_pasid_state(struct device_state *dev_state,
207 struct pasid_state *pasid_state,
208 int pasid)
209{
210 struct pasid_state **ptr;
211 unsigned long flags;
212 int ret;
213
214 spin_lock_irqsave(&dev_state->lock, flags);
215 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
216
217 ret = -ENOMEM;
218 if (ptr == NULL)
219 goto out_unlock;
220
221 ret = -ENOMEM;
222 if (*ptr != NULL)
223 goto out_unlock;
224
225 *ptr = pasid_state;
226
227 ret = 0;
228
229out_unlock:
230 spin_unlock_irqrestore(&dev_state->lock, flags);
231
232 return ret;
233}
234
235static void clear_pasid_state(struct device_state *dev_state, int pasid)
236{
237 struct pasid_state **ptr;
238 unsigned long flags;
239
240 spin_lock_irqsave(&dev_state->lock, flags);
241 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
242
243 if (ptr == NULL)
244 goto out_unlock;
245
246 *ptr = NULL;
247
248out_unlock:
249 spin_unlock_irqrestore(&dev_state->lock, flags);
250}
251
252static struct pasid_state *get_pasid_state(struct device_state *dev_state,
253 int pasid)
254{
255 struct pasid_state **ptr, *ret = NULL;
256 unsigned long flags;
257
258 spin_lock_irqsave(&dev_state->lock, flags);
259 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
260
261 if (ptr == NULL)
262 goto out_unlock;
263
264 ret = *ptr;
265 if (ret)
266 atomic_inc(&ret->count);
267
268out_unlock:
269 spin_unlock_irqrestore(&dev_state->lock, flags);
270
271 return ret;
272}
273
274static void free_pasid_state(struct pasid_state *pasid_state)
275{
276 kfree(pasid_state);
277}
278
279static void put_pasid_state(struct pasid_state *pasid_state)
280{
281 if (atomic_dec_and_test(&pasid_state->count)) {
282 put_device_state(pasid_state->device_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100283 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100284 }
285}
286
Joerg Roedel028eeac2011-11-24 12:48:13 +0100287static void put_pasid_state_wait(struct pasid_state *pasid_state)
288{
289 DEFINE_WAIT(wait);
290
291 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
292
293 if (atomic_dec_and_test(&pasid_state->count))
294 put_device_state(pasid_state->device_state);
295 else
296 schedule();
297
298 finish_wait(&pasid_state->wq, &wait);
299 mmput(pasid_state->mm);
300 free_pasid_state(pasid_state);
301}
302
Joerg Roedel61feb432014-07-08 14:19:35 +0200303static void unbind_pasid(struct pasid_state *pasid_state)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100304{
305 struct iommu_domain *domain;
306
307 domain = pasid_state->device_state->domain;
308
309 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
310 clear_pasid_state(pasid_state->device_state, pasid_state->pasid);
311
312 /* Make sure no more pending faults are in the queue */
313 flush_workqueue(iommu_wq);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100314}
315
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100316static void free_pasid_states_level1(struct pasid_state **tbl)
317{
318 int i;
319
320 for (i = 0; i < 512; ++i) {
321 if (tbl[i] == NULL)
322 continue;
323
324 free_page((unsigned long)tbl[i]);
325 }
326}
327
328static void free_pasid_states_level2(struct pasid_state **tbl)
329{
330 struct pasid_state **ptr;
331 int i;
332
333 for (i = 0; i < 512; ++i) {
334 if (tbl[i] == NULL)
335 continue;
336
337 ptr = (struct pasid_state **)tbl[i];
338 free_pasid_states_level1(ptr);
339 }
340}
341
342static void free_pasid_states(struct device_state *dev_state)
343{
344 struct pasid_state *pasid_state;
345 int i;
346
347 for (i = 0; i < dev_state->max_pasids; ++i) {
348 pasid_state = get_pasid_state(dev_state, i);
349 if (pasid_state == NULL)
350 continue;
351
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100352 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200353
354 /*
355 * This will call the mn_release function and
356 * unbind the PASID
357 */
358 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200359
360 put_pasid_state_wait(pasid_state); /* Reference taken in
361 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100362 }
363
364 if (dev_state->pasid_levels == 2)
365 free_pasid_states_level2(dev_state->states);
366 else if (dev_state->pasid_levels == 1)
367 free_pasid_states_level1(dev_state->states);
368 else if (dev_state->pasid_levels != 0)
369 BUG();
370
371 free_page((unsigned long)dev_state->states);
372}
373
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100374static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
375{
376 return container_of(mn, struct pasid_state, mn);
377}
378
379static void __mn_flush_page(struct mmu_notifier *mn,
380 unsigned long address)
381{
382 struct pasid_state *pasid_state;
383 struct device_state *dev_state;
384
385 pasid_state = mn_to_state(mn);
386 dev_state = pasid_state->device_state;
387
388 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
389}
390
391static int mn_clear_flush_young(struct mmu_notifier *mn,
392 struct mm_struct *mm,
393 unsigned long address)
394{
395 __mn_flush_page(mn, address);
396
397 return 0;
398}
399
400static void mn_change_pte(struct mmu_notifier *mn,
401 struct mm_struct *mm,
402 unsigned long address,
403 pte_t pte)
404{
405 __mn_flush_page(mn, address);
406}
407
408static void mn_invalidate_page(struct mmu_notifier *mn,
409 struct mm_struct *mm,
410 unsigned long address)
411{
412 __mn_flush_page(mn, address);
413}
414
415static void mn_invalidate_range_start(struct mmu_notifier *mn,
416 struct mm_struct *mm,
417 unsigned long start, unsigned long end)
418{
419 struct pasid_state *pasid_state;
420 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200421 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100422
423 pasid_state = mn_to_state(mn);
424 dev_state = pasid_state->device_state;
425
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200426 spin_lock_irqsave(&pasid_state->lock, flags);
427 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200428 amd_iommu_domain_set_gcr3(dev_state->domain,
429 pasid_state->pasid,
430 __pa(empty_page_table));
431 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200432 pasid_state->mmu_notifier_count += 1;
433 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100434}
435
436static void mn_invalidate_range_end(struct mmu_notifier *mn,
437 struct mm_struct *mm,
438 unsigned long start, unsigned long end)
439{
440 struct pasid_state *pasid_state;
441 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200442 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100443
444 pasid_state = mn_to_state(mn);
445 dev_state = pasid_state->device_state;
446
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200447 spin_lock_irqsave(&pasid_state->lock, flags);
448 pasid_state->mmu_notifier_count -= 1;
449 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200450 amd_iommu_domain_set_gcr3(dev_state->domain,
451 pasid_state->pasid,
452 __pa(pasid_state->mm->pgd));
453 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200454 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100455}
456
Joerg Roedela40d4c62014-05-20 23:18:24 +0200457static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
458{
459 struct pasid_state *pasid_state;
460 struct device_state *dev_state;
461
462 might_sleep();
463
464 pasid_state = mn_to_state(mn);
465 dev_state = pasid_state->device_state;
466
467 if (pasid_state->device_state->inv_ctx_cb)
468 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
469
Joerg Roedel61feb432014-07-08 14:19:35 +0200470 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200471}
472
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100473static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200474 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100475 .clear_flush_young = mn_clear_flush_young,
476 .change_pte = mn_change_pte,
477 .invalidate_page = mn_invalidate_page,
478 .invalidate_range_start = mn_invalidate_range_start,
479 .invalidate_range_end = mn_invalidate_range_end,
480};
481
Joerg Roedel028eeac2011-11-24 12:48:13 +0100482static void set_pri_tag_status(struct pasid_state *pasid_state,
483 u16 tag, int status)
484{
485 unsigned long flags;
486
487 spin_lock_irqsave(&pasid_state->lock, flags);
488 pasid_state->pri[tag].status = status;
489 spin_unlock_irqrestore(&pasid_state->lock, flags);
490}
491
492static void finish_pri_tag(struct device_state *dev_state,
493 struct pasid_state *pasid_state,
494 u16 tag)
495{
496 unsigned long flags;
497
498 spin_lock_irqsave(&pasid_state->lock, flags);
499 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
500 pasid_state->pri[tag].finish) {
501 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
502 pasid_state->pri[tag].status, tag);
503 pasid_state->pri[tag].finish = false;
504 pasid_state->pri[tag].status = PPR_SUCCESS;
505 }
506 spin_unlock_irqrestore(&pasid_state->lock, flags);
507}
508
509static void do_fault(struct work_struct *work)
510{
511 struct fault *fault = container_of(work, struct fault, work);
512 int npages, write;
513 struct page *page;
514
515 write = !!(fault->flags & PPR_FAULT_WRITE);
516
Jay Cornwall4378d992014-04-28 17:27:46 -0500517 down_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100518 npages = get_user_pages(fault->state->task, fault->state->mm,
519 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500520 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100521
Joerg Roedel175d6142011-11-28 14:36:36 +0100522 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100523 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100524 } else if (fault->dev_state->inv_ppr_cb) {
525 int status;
526
527 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
528 fault->pasid,
529 fault->address,
530 fault->flags);
531 switch (status) {
532 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
533 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
534 break;
535 case AMD_IOMMU_INV_PRI_RSP_INVALID:
536 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
537 break;
538 case AMD_IOMMU_INV_PRI_RSP_FAIL:
539 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
540 break;
541 default:
542 BUG();
543 }
544 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100545 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100546 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100547
548 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
549
550 put_pasid_state(fault->state);
551
552 kfree(fault);
553}
554
555static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
556{
557 struct amd_iommu_fault *iommu_fault;
558 struct pasid_state *pasid_state;
559 struct device_state *dev_state;
560 unsigned long flags;
561 struct fault *fault;
562 bool finish;
563 u16 tag;
564 int ret;
565
566 iommu_fault = data;
567 tag = iommu_fault->tag & 0x1ff;
568 finish = (iommu_fault->tag >> 9) & 1;
569
570 ret = NOTIFY_DONE;
571 dev_state = get_device_state(iommu_fault->device_id);
572 if (dev_state == NULL)
573 goto out;
574
575 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
576 if (pasid_state == NULL) {
577 /* We know the device but not the PASID -> send INVALID */
578 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
579 PPR_INVALID, tag);
580 goto out_drop_state;
581 }
582
583 spin_lock_irqsave(&pasid_state->lock, flags);
584 atomic_inc(&pasid_state->pri[tag].inflight);
585 if (finish)
586 pasid_state->pri[tag].finish = true;
587 spin_unlock_irqrestore(&pasid_state->lock, flags);
588
589 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
590 if (fault == NULL) {
591 /* We are OOM - send success and let the device re-fault */
592 finish_pri_tag(dev_state, pasid_state, tag);
593 goto out_drop_state;
594 }
595
596 fault->dev_state = dev_state;
597 fault->address = iommu_fault->address;
598 fault->state = pasid_state;
599 fault->tag = tag;
600 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300601 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100602 fault->flags = iommu_fault->flags;
603 INIT_WORK(&fault->work, do_fault);
604
605 queue_work(iommu_wq, &fault->work);
606
607 ret = NOTIFY_OK;
608
609out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200610
611 if (ret != NOTIFY_OK && pasid_state)
612 put_pasid_state(pasid_state);
613
Joerg Roedel028eeac2011-11-24 12:48:13 +0100614 put_device_state(dev_state);
615
616out:
617 return ret;
618}
619
620static struct notifier_block ppr_nb = {
621 .notifier_call = ppr_notifier,
622};
623
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100624int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
625 struct task_struct *task)
626{
627 struct pasid_state *pasid_state;
628 struct device_state *dev_state;
629 u16 devid;
630 int ret;
631
632 might_sleep();
633
634 if (!amd_iommu_v2_supported())
635 return -ENODEV;
636
637 devid = device_id(pdev);
638 dev_state = get_device_state(devid);
639
640 if (dev_state == NULL)
641 return -EINVAL;
642
643 ret = -EINVAL;
644 if (pasid < 0 || pasid >= dev_state->max_pasids)
645 goto out;
646
647 ret = -ENOMEM;
648 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
649 if (pasid_state == NULL)
650 goto out;
651
652 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100653 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200654 spin_lock_init(&pasid_state->lock);
655
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100656 pasid_state->task = task;
657 pasid_state->mm = get_task_mm(task);
658 pasid_state->device_state = dev_state;
659 pasid_state->pasid = pasid;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100660 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100661
662 if (pasid_state->mm == NULL)
663 goto out_free;
664
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100665 mmu_notifier_register(&pasid_state->mn, pasid_state->mm);
666
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100667 ret = set_pasid_state(dev_state, pasid_state, pasid);
668 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100669 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100670
671 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
672 __pa(pasid_state->mm->pgd));
673 if (ret)
674 goto out_clear_state;
675
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100676 return 0;
677
678out_clear_state:
679 clear_pasid_state(dev_state, pasid);
680
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100681out_unregister:
682 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
683
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100684out_free:
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200685 mmput(pasid_state->mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100686 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100687
688out:
689 put_device_state(dev_state);
690
691 return ret;
692}
693EXPORT_SYMBOL(amd_iommu_bind_pasid);
694
695void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
696{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200697 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100698 struct device_state *dev_state;
699 u16 devid;
700
701 might_sleep();
702
703 if (!amd_iommu_v2_supported())
704 return;
705
706 devid = device_id(pdev);
707 dev_state = get_device_state(devid);
708 if (dev_state == NULL)
709 return;
710
711 if (pasid < 0 || pasid >= dev_state->max_pasids)
712 goto out;
713
Joerg Roedela40d4c62014-05-20 23:18:24 +0200714 pasid_state = get_pasid_state(dev_state, pasid);
715 if (pasid_state == NULL)
716 goto out;
717 /*
718 * Drop reference taken here. We are safe because we still hold
719 * the reference taken in the amd_iommu_bind_pasid function.
720 */
721 put_pasid_state(pasid_state);
722
723 /* This will call the mn_release function and unbind the PASID */
724 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100725
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200726 put_pasid_state_wait(pasid_state); /* Reference taken in
727 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100728out:
729 put_device_state(dev_state);
730}
731EXPORT_SYMBOL(amd_iommu_unbind_pasid);
732
Joerg Roedeled96f222011-11-23 17:30:39 +0100733int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
734{
735 struct device_state *dev_state;
736 unsigned long flags;
737 int ret, tmp;
738 u16 devid;
739
740 might_sleep();
741
742 if (!amd_iommu_v2_supported())
743 return -ENODEV;
744
745 if (pasids <= 0 || pasids > (PASID_MASK + 1))
746 return -EINVAL;
747
748 devid = device_id(pdev);
749
750 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
751 if (dev_state == NULL)
752 return -ENOMEM;
753
754 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100755 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200756 dev_state->pdev = pdev;
757 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100758
759 tmp = pasids;
760 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
761 dev_state->pasid_levels += 1;
762
763 atomic_set(&dev_state->count, 1);
764 dev_state->max_pasids = pasids;
765
766 ret = -ENOMEM;
767 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
768 if (dev_state->states == NULL)
769 goto out_free_dev_state;
770
771 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
772 if (dev_state->domain == NULL)
773 goto out_free_states;
774
775 amd_iommu_domain_direct_map(dev_state->domain);
776
777 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
778 if (ret)
779 goto out_free_domain;
780
781 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
782 if (ret != 0)
783 goto out_free_domain;
784
785 spin_lock_irqsave(&state_lock, flags);
786
Joerg Roedel741669c2014-05-20 23:18:23 +0200787 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100788 spin_unlock_irqrestore(&state_lock, flags);
789 ret = -EBUSY;
790 goto out_free_domain;
791 }
792
Joerg Roedel741669c2014-05-20 23:18:23 +0200793 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100794
795 spin_unlock_irqrestore(&state_lock, flags);
796
797 return 0;
798
799out_free_domain:
800 iommu_domain_free(dev_state->domain);
801
802out_free_states:
803 free_page((unsigned long)dev_state->states);
804
805out_free_dev_state:
806 kfree(dev_state);
807
808 return ret;
809}
810EXPORT_SYMBOL(amd_iommu_init_device);
811
812void amd_iommu_free_device(struct pci_dev *pdev)
813{
814 struct device_state *dev_state;
815 unsigned long flags;
816 u16 devid;
817
818 if (!amd_iommu_v2_supported())
819 return;
820
821 devid = device_id(pdev);
822
823 spin_lock_irqsave(&state_lock, flags);
824
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200825 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100826 if (dev_state == NULL) {
827 spin_unlock_irqrestore(&state_lock, flags);
828 return;
829 }
830
Joerg Roedel741669c2014-05-20 23:18:23 +0200831 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100832
833 spin_unlock_irqrestore(&state_lock, flags);
834
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100835 /* Get rid of any remaining pasid states */
836 free_pasid_states(dev_state);
837
Joerg Roedel028eeac2011-11-24 12:48:13 +0100838 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100839}
840EXPORT_SYMBOL(amd_iommu_free_device);
841
Joerg Roedel175d6142011-11-28 14:36:36 +0100842int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
843 amd_iommu_invalid_ppr_cb cb)
844{
845 struct device_state *dev_state;
846 unsigned long flags;
847 u16 devid;
848 int ret;
849
850 if (!amd_iommu_v2_supported())
851 return -ENODEV;
852
853 devid = device_id(pdev);
854
855 spin_lock_irqsave(&state_lock, flags);
856
857 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200858 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100859 if (dev_state == NULL)
860 goto out_unlock;
861
862 dev_state->inv_ppr_cb = cb;
863
864 ret = 0;
865
866out_unlock:
867 spin_unlock_irqrestore(&state_lock, flags);
868
869 return ret;
870}
871EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
872
Joerg Roedelbc216622011-12-07 12:24:42 +0100873int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
874 amd_iommu_invalidate_ctx cb)
875{
876 struct device_state *dev_state;
877 unsigned long flags;
878 u16 devid;
879 int ret;
880
881 if (!amd_iommu_v2_supported())
882 return -ENODEV;
883
884 devid = device_id(pdev);
885
886 spin_lock_irqsave(&state_lock, flags);
887
888 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200889 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100890 if (dev_state == NULL)
891 goto out_unlock;
892
893 dev_state->inv_ctx_cb = cb;
894
895 ret = 0;
896
897out_unlock:
898 spin_unlock_irqrestore(&state_lock, flags);
899
900 return ret;
901}
902EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
903
Joerg Roedele3c495c2011-11-09 12:31:15 +0100904static int __init amd_iommu_v2_init(void)
905{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100906 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100907
Joerg Roedel474d567d2012-03-15 12:46:40 +0100908 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
909
910 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900911 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100912 /*
913 * Load anyway to provide the symbols to other modules
914 * which may use AMD IOMMUv2 optionally.
915 */
916 return 0;
917 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100918
Joerg Roedeled96f222011-11-23 17:30:39 +0100919 spin_lock_init(&state_lock);
920
Joerg Roedel028eeac2011-11-24 12:48:13 +0100921 ret = -ENOMEM;
922 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100923 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200924 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100925
926 ret = -ENOMEM;
927 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
928 if (empty_page_table == NULL)
929 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100930
931 amd_iommu_register_ppr_notifier(&ppr_nb);
932
Joerg Roedele3c495c2011-11-09 12:31:15 +0100933 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100934
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100935out_destroy_wq:
936 destroy_workqueue(iommu_wq);
937
Joerg Roedel741669c2014-05-20 23:18:23 +0200938out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100939 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100940}
941
942static void __exit amd_iommu_v2_exit(void)
943{
Joerg Roedeled96f222011-11-23 17:30:39 +0100944 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100945 int i;
946
Joerg Roedel474d567d2012-03-15 12:46:40 +0100947 if (!amd_iommu_v2_supported())
948 return;
949
Joerg Roedel028eeac2011-11-24 12:48:13 +0100950 amd_iommu_unregister_ppr_notifier(&ppr_nb);
951
952 flush_workqueue(iommu_wq);
953
954 /*
955 * The loop below might call flush_workqueue(), so call
956 * destroy_workqueue() after it
957 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100958 for (i = 0; i < MAX_DEVICES; ++i) {
959 dev_state = get_device_state(i);
960
961 if (dev_state == NULL)
962 continue;
963
964 WARN_ON_ONCE(1);
965
Joerg Roedeled96f222011-11-23 17:30:39 +0100966 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100967 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100968 }
969
Joerg Roedel028eeac2011-11-24 12:48:13 +0100970 destroy_workqueue(iommu_wq);
971
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100972 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100973}
974
975module_init(amd_iommu_v2_init);
976module_exit(amd_iommu_v2_exit);