blob: 2b7de882e91c4c5cf3b54f2d2e1ea371b8219190 [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 mm_struct *mm; /* mm_struct for the faults */
Joerg Roedelff6d0cc2014-07-08 12:49:50 +020051 struct mmu_notifier mn; /* mmu_notifier handle */
Joerg Roedeled96f222011-11-23 17:30:39 +010052 struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
53 struct device_state *device_state; /* Link to our device_state */
54 int pasid; /* PASID index */
Joerg Roedeld9e16112014-07-09 15:43:11 +020055 bool invalid; /* Used during setup and
56 teardown of the pasid */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020057 spinlock_t lock; /* Protect pri_queues and
58 mmu_notifer_count */
Joerg Roedel028eeac2011-11-24 12:48:13 +010059 wait_queue_head_t wq; /* To wait for count == 0 */
Joerg Roedeled96f222011-11-23 17:30:39 +010060};
61
62struct device_state {
Joerg Roedel741669c2014-05-20 23:18:23 +020063 struct list_head list;
64 u16 devid;
Joerg Roedeled96f222011-11-23 17:30:39 +010065 atomic_t count;
66 struct pci_dev *pdev;
67 struct pasid_state **states;
68 struct iommu_domain *domain;
69 int pasid_levels;
70 int max_pasids;
Joerg Roedel175d6142011-11-28 14:36:36 +010071 amd_iommu_invalid_ppr_cb inv_ppr_cb;
Joerg Roedelbc216622011-12-07 12:24:42 +010072 amd_iommu_invalidate_ctx inv_ctx_cb;
Joerg Roedeled96f222011-11-23 17:30:39 +010073 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010074 wait_queue_head_t wq;
75};
76
77struct fault {
78 struct work_struct work;
79 struct device_state *dev_state;
80 struct pasid_state *state;
81 struct mm_struct *mm;
82 u64 address;
83 u16 devid;
84 u16 pasid;
85 u16 tag;
86 u16 finish;
87 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010088};
89
Joerg Roedel741669c2014-05-20 23:18:23 +020090static LIST_HEAD(state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +010091static spinlock_t state_lock;
92
Joerg Roedel028eeac2011-11-24 12:48:13 +010093static struct workqueue_struct *iommu_wq;
94
Joerg Roedel8736b2c2011-11-24 16:21:52 +010095/*
96 * Empty page table - Used between
97 * mmu_notifier_invalidate_range_start and
98 * mmu_notifier_invalidate_range_end
99 */
100static u64 *empty_page_table;
101
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100102static void free_pasid_states(struct device_state *dev_state);
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);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100300 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
Joerg Roedel53d340e2014-07-08 15:01:43 +0200309 /*
310 * Mark pasid_state as invalid, no more faults will we added to the
311 * work queue after this is visible everywhere.
312 */
313 pasid_state->invalid = true;
314
315 /* Make sure this is visible */
316 smp_wmb();
317
318 /* After this the device/pasid can't access the mm anymore */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100319 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100320
321 /* Make sure no more pending faults are in the queue */
322 flush_workqueue(iommu_wq);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100323}
324
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100325static void free_pasid_states_level1(struct pasid_state **tbl)
326{
327 int i;
328
329 for (i = 0; i < 512; ++i) {
330 if (tbl[i] == NULL)
331 continue;
332
333 free_page((unsigned long)tbl[i]);
334 }
335}
336
337static void free_pasid_states_level2(struct pasid_state **tbl)
338{
339 struct pasid_state **ptr;
340 int i;
341
342 for (i = 0; i < 512; ++i) {
343 if (tbl[i] == NULL)
344 continue;
345
346 ptr = (struct pasid_state **)tbl[i];
347 free_pasid_states_level1(ptr);
348 }
349}
350
351static void free_pasid_states(struct device_state *dev_state)
352{
353 struct pasid_state *pasid_state;
354 int i;
355
356 for (i = 0; i < dev_state->max_pasids; ++i) {
357 pasid_state = get_pasid_state(dev_state, i);
358 if (pasid_state == NULL)
359 continue;
360
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100361 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200362
363 /*
364 * This will call the mn_release function and
365 * unbind the PASID
366 */
367 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200368
369 put_pasid_state_wait(pasid_state); /* Reference taken in
370 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100371 }
372
373 if (dev_state->pasid_levels == 2)
374 free_pasid_states_level2(dev_state->states);
375 else if (dev_state->pasid_levels == 1)
376 free_pasid_states_level1(dev_state->states);
377 else if (dev_state->pasid_levels != 0)
378 BUG();
379
380 free_page((unsigned long)dev_state->states);
381}
382
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100383static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
384{
385 return container_of(mn, struct pasid_state, mn);
386}
387
388static void __mn_flush_page(struct mmu_notifier *mn,
389 unsigned long address)
390{
391 struct pasid_state *pasid_state;
392 struct device_state *dev_state;
393
394 pasid_state = mn_to_state(mn);
395 dev_state = pasid_state->device_state;
396
397 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
398}
399
400static int mn_clear_flush_young(struct mmu_notifier *mn,
401 struct mm_struct *mm,
402 unsigned long address)
403{
404 __mn_flush_page(mn, address);
405
406 return 0;
407}
408
409static void mn_change_pte(struct mmu_notifier *mn,
410 struct mm_struct *mm,
411 unsigned long address,
412 pte_t pte)
413{
414 __mn_flush_page(mn, address);
415}
416
417static void mn_invalidate_page(struct mmu_notifier *mn,
418 struct mm_struct *mm,
419 unsigned long address)
420{
421 __mn_flush_page(mn, address);
422}
423
424static void mn_invalidate_range_start(struct mmu_notifier *mn,
425 struct mm_struct *mm,
426 unsigned long start, unsigned long end)
427{
428 struct pasid_state *pasid_state;
429 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200430 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100431
432 pasid_state = mn_to_state(mn);
433 dev_state = pasid_state->device_state;
434
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200435 spin_lock_irqsave(&pasid_state->lock, flags);
436 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200437 amd_iommu_domain_set_gcr3(dev_state->domain,
438 pasid_state->pasid,
439 __pa(empty_page_table));
440 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200441 pasid_state->mmu_notifier_count += 1;
442 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100443}
444
445static void mn_invalidate_range_end(struct mmu_notifier *mn,
446 struct mm_struct *mm,
447 unsigned long start, unsigned long end)
448{
449 struct pasid_state *pasid_state;
450 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200451 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100452
453 pasid_state = mn_to_state(mn);
454 dev_state = pasid_state->device_state;
455
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200456 spin_lock_irqsave(&pasid_state->lock, flags);
457 pasid_state->mmu_notifier_count -= 1;
458 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200459 amd_iommu_domain_set_gcr3(dev_state->domain,
460 pasid_state->pasid,
461 __pa(pasid_state->mm->pgd));
462 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200463 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100464}
465
Joerg Roedela40d4c62014-05-20 23:18:24 +0200466static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
467{
468 struct pasid_state *pasid_state;
469 struct device_state *dev_state;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200470 bool run_inv_ctx_cb;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200471
472 might_sleep();
473
Joerg Roedeld9e16112014-07-09 15:43:11 +0200474 pasid_state = mn_to_state(mn);
475 dev_state = pasid_state->device_state;
476 run_inv_ctx_cb = !pasid_state->invalid;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200477
Joerg Roedeld9e16112014-07-09 15:43:11 +0200478 if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
Joerg Roedela40d4c62014-05-20 23:18:24 +0200479 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
480
Joerg Roedel61feb432014-07-08 14:19:35 +0200481 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200482}
483
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100484static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200485 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100486 .clear_flush_young = mn_clear_flush_young,
487 .change_pte = mn_change_pte,
488 .invalidate_page = mn_invalidate_page,
489 .invalidate_range_start = mn_invalidate_range_start,
490 .invalidate_range_end = mn_invalidate_range_end,
491};
492
Joerg Roedel028eeac2011-11-24 12:48:13 +0100493static void set_pri_tag_status(struct pasid_state *pasid_state,
494 u16 tag, int status)
495{
496 unsigned long flags;
497
498 spin_lock_irqsave(&pasid_state->lock, flags);
499 pasid_state->pri[tag].status = status;
500 spin_unlock_irqrestore(&pasid_state->lock, flags);
501}
502
503static void finish_pri_tag(struct device_state *dev_state,
504 struct pasid_state *pasid_state,
505 u16 tag)
506{
507 unsigned long flags;
508
509 spin_lock_irqsave(&pasid_state->lock, flags);
510 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
511 pasid_state->pri[tag].finish) {
512 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
513 pasid_state->pri[tag].status, tag);
514 pasid_state->pri[tag].finish = false;
515 pasid_state->pri[tag].status = PPR_SUCCESS;
516 }
517 spin_unlock_irqrestore(&pasid_state->lock, flags);
518}
519
520static void do_fault(struct work_struct *work)
521{
522 struct fault *fault = container_of(work, struct fault, work);
523 int npages, write;
524 struct page *page;
525
526 write = !!(fault->flags & PPR_FAULT_WRITE);
527
Jay Cornwall4378d992014-04-28 17:27:46 -0500528 down_read(&fault->state->mm->mmap_sem);
Joerg Roedeldba38382014-07-09 17:56:43 +0200529 npages = get_user_pages(NULL, fault->state->mm,
Joerg Roedel028eeac2011-11-24 12:48:13 +0100530 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500531 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100532
Joerg Roedel175d6142011-11-28 14:36:36 +0100533 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100534 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100535 } else if (fault->dev_state->inv_ppr_cb) {
536 int status;
537
538 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
539 fault->pasid,
540 fault->address,
541 fault->flags);
542 switch (status) {
543 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
544 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
545 break;
546 case AMD_IOMMU_INV_PRI_RSP_INVALID:
547 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
548 break;
549 case AMD_IOMMU_INV_PRI_RSP_FAIL:
550 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
551 break;
552 default:
553 BUG();
554 }
555 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100556 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100557 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100558
559 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
560
561 put_pasid_state(fault->state);
562
563 kfree(fault);
564}
565
566static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
567{
568 struct amd_iommu_fault *iommu_fault;
569 struct pasid_state *pasid_state;
570 struct device_state *dev_state;
571 unsigned long flags;
572 struct fault *fault;
573 bool finish;
574 u16 tag;
575 int ret;
576
577 iommu_fault = data;
578 tag = iommu_fault->tag & 0x1ff;
579 finish = (iommu_fault->tag >> 9) & 1;
580
581 ret = NOTIFY_DONE;
582 dev_state = get_device_state(iommu_fault->device_id);
583 if (dev_state == NULL)
584 goto out;
585
586 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200587 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100588 /* We know the device but not the PASID -> send INVALID */
589 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
590 PPR_INVALID, tag);
591 goto out_drop_state;
592 }
593
594 spin_lock_irqsave(&pasid_state->lock, flags);
595 atomic_inc(&pasid_state->pri[tag].inflight);
596 if (finish)
597 pasid_state->pri[tag].finish = true;
598 spin_unlock_irqrestore(&pasid_state->lock, flags);
599
600 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
601 if (fault == NULL) {
602 /* We are OOM - send success and let the device re-fault */
603 finish_pri_tag(dev_state, pasid_state, tag);
604 goto out_drop_state;
605 }
606
607 fault->dev_state = dev_state;
608 fault->address = iommu_fault->address;
609 fault->state = pasid_state;
610 fault->tag = tag;
611 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300612 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100613 fault->flags = iommu_fault->flags;
614 INIT_WORK(&fault->work, do_fault);
615
616 queue_work(iommu_wq, &fault->work);
617
618 ret = NOTIFY_OK;
619
620out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200621
622 if (ret != NOTIFY_OK && pasid_state)
623 put_pasid_state(pasid_state);
624
Joerg Roedel028eeac2011-11-24 12:48:13 +0100625 put_device_state(dev_state);
626
627out:
628 return ret;
629}
630
631static struct notifier_block ppr_nb = {
632 .notifier_call = ppr_notifier,
633};
634
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100635int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
636 struct task_struct *task)
637{
638 struct pasid_state *pasid_state;
639 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200640 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100641 u16 devid;
642 int ret;
643
644 might_sleep();
645
646 if (!amd_iommu_v2_supported())
647 return -ENODEV;
648
649 devid = device_id(pdev);
650 dev_state = get_device_state(devid);
651
652 if (dev_state == NULL)
653 return -EINVAL;
654
655 ret = -EINVAL;
656 if (pasid < 0 || pasid >= dev_state->max_pasids)
657 goto out;
658
659 ret = -ENOMEM;
660 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
661 if (pasid_state == NULL)
662 goto out;
663
Joerg Roedelf0aac632014-07-08 15:15:07 +0200664
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100665 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100666 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200667 spin_lock_init(&pasid_state->lock);
668
Joerg Roedelf0aac632014-07-08 15:15:07 +0200669 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200670 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100671 pasid_state->device_state = dev_state;
672 pasid_state->pasid = pasid;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200673 pasid_state->invalid = true; /* Mark as valid only if we are
674 done with setting up the pasid */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100675 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100676
677 if (pasid_state->mm == NULL)
678 goto out_free;
679
Joerg Roedelf0aac632014-07-08 15:15:07 +0200680 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100681
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100682 ret = set_pasid_state(dev_state, pasid_state, pasid);
683 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100684 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100685
686 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
687 __pa(pasid_state->mm->pgd));
688 if (ret)
689 goto out_clear_state;
690
Joerg Roedeld9e16112014-07-09 15:43:11 +0200691 /* Now we are ready to handle faults */
692 pasid_state->invalid = false;
693
Joerg Roedelf0aac632014-07-08 15:15:07 +0200694 /*
695 * Drop the reference to the mm_struct here. We rely on the
696 * mmu_notifier release call-back to inform us when the mm
697 * is going away.
698 */
699 mmput(mm);
700
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100701 return 0;
702
703out_clear_state:
704 clear_pasid_state(dev_state, pasid);
705
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100706out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200707 mmu_notifier_unregister(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100708
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100709out_free:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200710 mmput(mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100711 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100712
713out:
714 put_device_state(dev_state);
715
716 return ret;
717}
718EXPORT_SYMBOL(amd_iommu_bind_pasid);
719
720void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
721{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200722 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100723 struct device_state *dev_state;
724 u16 devid;
725
726 might_sleep();
727
728 if (!amd_iommu_v2_supported())
729 return;
730
731 devid = device_id(pdev);
732 dev_state = get_device_state(devid);
733 if (dev_state == NULL)
734 return;
735
736 if (pasid < 0 || pasid >= dev_state->max_pasids)
737 goto out;
738
Joerg Roedela40d4c62014-05-20 23:18:24 +0200739 pasid_state = get_pasid_state(dev_state, pasid);
740 if (pasid_state == NULL)
741 goto out;
742 /*
743 * Drop reference taken here. We are safe because we still hold
744 * the reference taken in the amd_iommu_bind_pasid function.
745 */
746 put_pasid_state(pasid_state);
747
Joerg Roedel53d340e2014-07-08 15:01:43 +0200748 /* Clear the pasid state so that the pasid can be re-used */
749 clear_pasid_state(dev_state, pasid_state->pasid);
750
Joerg Roedelf0aac632014-07-08 15:15:07 +0200751 /*
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200752 * Call mmu_notifier_unregister to drop our reference
753 * to pasid_state->mm
Joerg Roedelf0aac632014-07-08 15:15:07 +0200754 */
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200755 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100756
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200757 put_pasid_state_wait(pasid_state); /* Reference taken in
758 amd_iommu_pasid_bind */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100759out:
760 put_device_state(dev_state);
761}
762EXPORT_SYMBOL(amd_iommu_unbind_pasid);
763
Joerg Roedeled96f222011-11-23 17:30:39 +0100764int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
765{
766 struct device_state *dev_state;
767 unsigned long flags;
768 int ret, tmp;
769 u16 devid;
770
771 might_sleep();
772
773 if (!amd_iommu_v2_supported())
774 return -ENODEV;
775
776 if (pasids <= 0 || pasids > (PASID_MASK + 1))
777 return -EINVAL;
778
779 devid = device_id(pdev);
780
781 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
782 if (dev_state == NULL)
783 return -ENOMEM;
784
785 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100786 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200787 dev_state->pdev = pdev;
788 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100789
790 tmp = pasids;
791 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
792 dev_state->pasid_levels += 1;
793
794 atomic_set(&dev_state->count, 1);
795 dev_state->max_pasids = pasids;
796
797 ret = -ENOMEM;
798 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
799 if (dev_state->states == NULL)
800 goto out_free_dev_state;
801
802 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
803 if (dev_state->domain == NULL)
804 goto out_free_states;
805
806 amd_iommu_domain_direct_map(dev_state->domain);
807
808 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
809 if (ret)
810 goto out_free_domain;
811
812 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
813 if (ret != 0)
814 goto out_free_domain;
815
816 spin_lock_irqsave(&state_lock, flags);
817
Joerg Roedel741669c2014-05-20 23:18:23 +0200818 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100819 spin_unlock_irqrestore(&state_lock, flags);
820 ret = -EBUSY;
821 goto out_free_domain;
822 }
823
Joerg Roedel741669c2014-05-20 23:18:23 +0200824 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100825
826 spin_unlock_irqrestore(&state_lock, flags);
827
828 return 0;
829
830out_free_domain:
831 iommu_domain_free(dev_state->domain);
832
833out_free_states:
834 free_page((unsigned long)dev_state->states);
835
836out_free_dev_state:
837 kfree(dev_state);
838
839 return ret;
840}
841EXPORT_SYMBOL(amd_iommu_init_device);
842
843void amd_iommu_free_device(struct pci_dev *pdev)
844{
845 struct device_state *dev_state;
846 unsigned long flags;
847 u16 devid;
848
849 if (!amd_iommu_v2_supported())
850 return;
851
852 devid = device_id(pdev);
853
854 spin_lock_irqsave(&state_lock, flags);
855
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200856 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100857 if (dev_state == NULL) {
858 spin_unlock_irqrestore(&state_lock, flags);
859 return;
860 }
861
Joerg Roedel741669c2014-05-20 23:18:23 +0200862 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100863
864 spin_unlock_irqrestore(&state_lock, flags);
865
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100866 /* Get rid of any remaining pasid states */
867 free_pasid_states(dev_state);
868
Joerg Roedel028eeac2011-11-24 12:48:13 +0100869 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100870}
871EXPORT_SYMBOL(amd_iommu_free_device);
872
Joerg Roedel175d6142011-11-28 14:36:36 +0100873int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
874 amd_iommu_invalid_ppr_cb 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 Roedel175d6142011-11-28 14:36:36 +0100890 if (dev_state == NULL)
891 goto out_unlock;
892
893 dev_state->inv_ppr_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_invalid_ppr_cb);
903
Joerg Roedelbc216622011-12-07 12:24:42 +0100904int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
905 amd_iommu_invalidate_ctx cb)
906{
907 struct device_state *dev_state;
908 unsigned long flags;
909 u16 devid;
910 int ret;
911
912 if (!amd_iommu_v2_supported())
913 return -ENODEV;
914
915 devid = device_id(pdev);
916
917 spin_lock_irqsave(&state_lock, flags);
918
919 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200920 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100921 if (dev_state == NULL)
922 goto out_unlock;
923
924 dev_state->inv_ctx_cb = cb;
925
926 ret = 0;
927
928out_unlock:
929 spin_unlock_irqrestore(&state_lock, flags);
930
931 return ret;
932}
933EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
934
Joerg Roedele3c495c2011-11-09 12:31:15 +0100935static int __init amd_iommu_v2_init(void)
936{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100937 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100938
Joerg Roedel474d567d2012-03-15 12:46:40 +0100939 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
940
941 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900942 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100943 /*
944 * Load anyway to provide the symbols to other modules
945 * which may use AMD IOMMUv2 optionally.
946 */
947 return 0;
948 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100949
Joerg Roedeled96f222011-11-23 17:30:39 +0100950 spin_lock_init(&state_lock);
951
Joerg Roedel028eeac2011-11-24 12:48:13 +0100952 ret = -ENOMEM;
953 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100954 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200955 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100956
957 ret = -ENOMEM;
958 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
959 if (empty_page_table == NULL)
960 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100961
962 amd_iommu_register_ppr_notifier(&ppr_nb);
963
Joerg Roedele3c495c2011-11-09 12:31:15 +0100964 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100965
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100966out_destroy_wq:
967 destroy_workqueue(iommu_wq);
968
Joerg Roedel741669c2014-05-20 23:18:23 +0200969out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100970 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100971}
972
973static void __exit amd_iommu_v2_exit(void)
974{
Joerg Roedeled96f222011-11-23 17:30:39 +0100975 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100976 int i;
977
Joerg Roedel474d567d2012-03-15 12:46:40 +0100978 if (!amd_iommu_v2_supported())
979 return;
980
Joerg Roedel028eeac2011-11-24 12:48:13 +0100981 amd_iommu_unregister_ppr_notifier(&ppr_nb);
982
983 flush_workqueue(iommu_wq);
984
985 /*
986 * The loop below might call flush_workqueue(), so call
987 * destroy_workqueue() after it
988 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100989 for (i = 0; i < MAX_DEVICES; ++i) {
990 dev_state = get_device_state(i);
991
992 if (dev_state == NULL)
993 continue;
994
995 WARN_ON_ONCE(1);
996
Joerg Roedeled96f222011-11-23 17:30:39 +0100997 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100998 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100999 }
1000
Joerg Roedel028eeac2011-11-24 12:48:13 +01001001 destroy_workqueue(iommu_wq);
1002
Joerg Roedel8736b2c2011-11-24 16:21:52 +01001003 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +01001004}
1005
1006module_init(amd_iommu_v2_init);
1007module_exit(amd_iommu_v2_exit);