blob: 9c0d6e2900972108bfaa942fa72b39028cec5c6e [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
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200370 amd_iommu_bind_pasid */
Joerg Roedel75058a32014-07-30 16:04:39 +0200371
372 /* Drop reference taken in amd_iommu_bind_pasid */
373 put_device_state(dev_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100374 }
375
376 if (dev_state->pasid_levels == 2)
377 free_pasid_states_level2(dev_state->states);
378 else if (dev_state->pasid_levels == 1)
379 free_pasid_states_level1(dev_state->states);
380 else if (dev_state->pasid_levels != 0)
381 BUG();
382
383 free_page((unsigned long)dev_state->states);
384}
385
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100386static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
387{
388 return container_of(mn, struct pasid_state, mn);
389}
390
391static void __mn_flush_page(struct mmu_notifier *mn,
392 unsigned long address)
393{
394 struct pasid_state *pasid_state;
395 struct device_state *dev_state;
396
397 pasid_state = mn_to_state(mn);
398 dev_state = pasid_state->device_state;
399
400 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
401}
402
403static int mn_clear_flush_young(struct mmu_notifier *mn,
404 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700405 unsigned long start,
406 unsigned long end)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100407{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700408 for (; start < end; start += PAGE_SIZE)
409 __mn_flush_page(mn, start);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100410
411 return 0;
412}
413
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100414static void mn_invalidate_page(struct mmu_notifier *mn,
415 struct mm_struct *mm,
416 unsigned long address)
417{
418 __mn_flush_page(mn, address);
419}
420
421static void mn_invalidate_range_start(struct mmu_notifier *mn,
422 struct mm_struct *mm,
423 unsigned long start, unsigned long end)
424{
425 struct pasid_state *pasid_state;
426 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200427 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100428
429 pasid_state = mn_to_state(mn);
430 dev_state = pasid_state->device_state;
431
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200432 spin_lock_irqsave(&pasid_state->lock, flags);
433 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200434 amd_iommu_domain_set_gcr3(dev_state->domain,
435 pasid_state->pasid,
436 __pa(empty_page_table));
437 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200438 pasid_state->mmu_notifier_count += 1;
439 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100440}
441
442static void mn_invalidate_range_end(struct mmu_notifier *mn,
443 struct mm_struct *mm,
444 unsigned long start, unsigned long end)
445{
446 struct pasid_state *pasid_state;
447 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200448 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100449
450 pasid_state = mn_to_state(mn);
451 dev_state = pasid_state->device_state;
452
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200453 spin_lock_irqsave(&pasid_state->lock, flags);
454 pasid_state->mmu_notifier_count -= 1;
455 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200456 amd_iommu_domain_set_gcr3(dev_state->domain,
457 pasid_state->pasid,
458 __pa(pasid_state->mm->pgd));
459 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200460 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100461}
462
Joerg Roedela40d4c62014-05-20 23:18:24 +0200463static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
464{
465 struct pasid_state *pasid_state;
466 struct device_state *dev_state;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200467 bool run_inv_ctx_cb;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200468
469 might_sleep();
470
Joerg Roedeld9e16112014-07-09 15:43:11 +0200471 pasid_state = mn_to_state(mn);
472 dev_state = pasid_state->device_state;
473 run_inv_ctx_cb = !pasid_state->invalid;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200474
Joerg Roedeld9e16112014-07-09 15:43:11 +0200475 if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
Joerg Roedela40d4c62014-05-20 23:18:24 +0200476 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
477
Joerg Roedel61feb432014-07-08 14:19:35 +0200478 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200479}
480
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100481static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200482 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100483 .clear_flush_young = mn_clear_flush_young,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100484 .invalidate_page = mn_invalidate_page,
485 .invalidate_range_start = mn_invalidate_range_start,
486 .invalidate_range_end = mn_invalidate_range_end,
487};
488
Joerg Roedel028eeac2011-11-24 12:48:13 +0100489static void set_pri_tag_status(struct pasid_state *pasid_state,
490 u16 tag, int status)
491{
492 unsigned long flags;
493
494 spin_lock_irqsave(&pasid_state->lock, flags);
495 pasid_state->pri[tag].status = status;
496 spin_unlock_irqrestore(&pasid_state->lock, flags);
497}
498
499static void finish_pri_tag(struct device_state *dev_state,
500 struct pasid_state *pasid_state,
501 u16 tag)
502{
503 unsigned long flags;
504
505 spin_lock_irqsave(&pasid_state->lock, flags);
506 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
507 pasid_state->pri[tag].finish) {
508 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
509 pasid_state->pri[tag].status, tag);
510 pasid_state->pri[tag].finish = false;
511 pasid_state->pri[tag].status = PPR_SUCCESS;
512 }
513 spin_unlock_irqrestore(&pasid_state->lock, flags);
514}
515
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800516static void handle_fault_error(struct fault *fault)
517{
518 int status;
519
520 if (!fault->dev_state->inv_ppr_cb) {
521 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
522 return;
523 }
524
525 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
526 fault->pasid,
527 fault->address,
528 fault->flags);
529 switch (status) {
530 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
531 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
532 break;
533 case AMD_IOMMU_INV_PRI_RSP_INVALID:
534 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
535 break;
536 case AMD_IOMMU_INV_PRI_RSP_FAIL:
537 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
538 break;
539 default:
540 BUG();
541 }
542}
543
Joerg Roedel028eeac2011-11-24 12:48:13 +0100544static void do_fault(struct work_struct *work)
545{
546 struct fault *fault = container_of(work, struct fault, work);
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800547 struct mm_struct *mm;
548 struct vm_area_struct *vma;
549 u64 address;
550 int ret, write;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100551
552 write = !!(fault->flags & PPR_FAULT_WRITE);
553
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800554 mm = fault->state->mm;
555 address = fault->address;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100556
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800557 down_read(&mm->mmap_sem);
558 vma = find_extend_vma(mm, address);
559 if (!vma || address < vma->vm_start) {
560 /* failed to get a vma in the right range */
561 up_read(&mm->mmap_sem);
562 handle_fault_error(fault);
563 goto out;
Joerg Roedel175d6142011-11-28 14:36:36 +0100564 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100565
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800566 ret = handle_mm_fault(mm, vma, address, write);
567 if (ret & VM_FAULT_ERROR) {
568 /* failed to service fault */
569 up_read(&mm->mmap_sem);
570 handle_fault_error(fault);
571 goto out;
572 }
573
574 up_read(&mm->mmap_sem);
575
576out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100577 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
578
579 put_pasid_state(fault->state);
580
581 kfree(fault);
582}
583
584static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
585{
586 struct amd_iommu_fault *iommu_fault;
587 struct pasid_state *pasid_state;
588 struct device_state *dev_state;
589 unsigned long flags;
590 struct fault *fault;
591 bool finish;
592 u16 tag;
593 int ret;
594
595 iommu_fault = data;
596 tag = iommu_fault->tag & 0x1ff;
597 finish = (iommu_fault->tag >> 9) & 1;
598
599 ret = NOTIFY_DONE;
600 dev_state = get_device_state(iommu_fault->device_id);
601 if (dev_state == NULL)
602 goto out;
603
604 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200605 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100606 /* We know the device but not the PASID -> send INVALID */
607 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
608 PPR_INVALID, tag);
609 goto out_drop_state;
610 }
611
612 spin_lock_irqsave(&pasid_state->lock, flags);
613 atomic_inc(&pasid_state->pri[tag].inflight);
614 if (finish)
615 pasid_state->pri[tag].finish = true;
616 spin_unlock_irqrestore(&pasid_state->lock, flags);
617
618 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
619 if (fault == NULL) {
620 /* We are OOM - send success and let the device re-fault */
621 finish_pri_tag(dev_state, pasid_state, tag);
622 goto out_drop_state;
623 }
624
625 fault->dev_state = dev_state;
626 fault->address = iommu_fault->address;
627 fault->state = pasid_state;
628 fault->tag = tag;
629 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300630 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100631 fault->flags = iommu_fault->flags;
632 INIT_WORK(&fault->work, do_fault);
633
634 queue_work(iommu_wq, &fault->work);
635
636 ret = NOTIFY_OK;
637
638out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200639
640 if (ret != NOTIFY_OK && pasid_state)
641 put_pasid_state(pasid_state);
642
Joerg Roedel028eeac2011-11-24 12:48:13 +0100643 put_device_state(dev_state);
644
645out:
646 return ret;
647}
648
649static struct notifier_block ppr_nb = {
650 .notifier_call = ppr_notifier,
651};
652
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100653int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
654 struct task_struct *task)
655{
656 struct pasid_state *pasid_state;
657 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200658 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100659 u16 devid;
660 int ret;
661
662 might_sleep();
663
664 if (!amd_iommu_v2_supported())
665 return -ENODEV;
666
667 devid = device_id(pdev);
668 dev_state = get_device_state(devid);
669
670 if (dev_state == NULL)
671 return -EINVAL;
672
673 ret = -EINVAL;
674 if (pasid < 0 || pasid >= dev_state->max_pasids)
675 goto out;
676
677 ret = -ENOMEM;
678 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
679 if (pasid_state == NULL)
680 goto out;
681
Joerg Roedelf0aac632014-07-08 15:15:07 +0200682
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100683 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100684 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200685 spin_lock_init(&pasid_state->lock);
686
Joerg Roedelf0aac632014-07-08 15:15:07 +0200687 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200688 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100689 pasid_state->device_state = dev_state;
690 pasid_state->pasid = pasid;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200691 pasid_state->invalid = true; /* Mark as valid only if we are
692 done with setting up the pasid */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100693 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100694
695 if (pasid_state->mm == NULL)
696 goto out_free;
697
Joerg Roedelf0aac632014-07-08 15:15:07 +0200698 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100699
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100700 ret = set_pasid_state(dev_state, pasid_state, pasid);
701 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100702 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100703
704 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
705 __pa(pasid_state->mm->pgd));
706 if (ret)
707 goto out_clear_state;
708
Joerg Roedeld9e16112014-07-09 15:43:11 +0200709 /* Now we are ready to handle faults */
710 pasid_state->invalid = false;
711
Joerg Roedelf0aac632014-07-08 15:15:07 +0200712 /*
713 * Drop the reference to the mm_struct here. We rely on the
714 * mmu_notifier release call-back to inform us when the mm
715 * is going away.
716 */
717 mmput(mm);
718
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100719 return 0;
720
721out_clear_state:
722 clear_pasid_state(dev_state, pasid);
723
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100724out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200725 mmu_notifier_unregister(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100726
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100727out_free:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200728 mmput(mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100729 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100730
731out:
732 put_device_state(dev_state);
733
734 return ret;
735}
736EXPORT_SYMBOL(amd_iommu_bind_pasid);
737
738void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
739{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200740 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100741 struct device_state *dev_state;
742 u16 devid;
743
744 might_sleep();
745
746 if (!amd_iommu_v2_supported())
747 return;
748
749 devid = device_id(pdev);
750 dev_state = get_device_state(devid);
751 if (dev_state == NULL)
752 return;
753
754 if (pasid < 0 || pasid >= dev_state->max_pasids)
755 goto out;
756
Joerg Roedela40d4c62014-05-20 23:18:24 +0200757 pasid_state = get_pasid_state(dev_state, pasid);
758 if (pasid_state == NULL)
759 goto out;
760 /*
761 * Drop reference taken here. We are safe because we still hold
762 * the reference taken in the amd_iommu_bind_pasid function.
763 */
764 put_pasid_state(pasid_state);
765
Joerg Roedel53d340e2014-07-08 15:01:43 +0200766 /* Clear the pasid state so that the pasid can be re-used */
767 clear_pasid_state(dev_state, pasid_state->pasid);
768
Joerg Roedelf0aac632014-07-08 15:15:07 +0200769 /*
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200770 * Call mmu_notifier_unregister to drop our reference
771 * to pasid_state->mm
Joerg Roedelf0aac632014-07-08 15:15:07 +0200772 */
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200773 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100774
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200775 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200776 amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100777out:
Joerg Roedel75058a32014-07-30 16:04:39 +0200778 /* Drop reference taken in this function */
779 put_device_state(dev_state);
780
781 /* Drop reference taken in amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100782 put_device_state(dev_state);
783}
784EXPORT_SYMBOL(amd_iommu_unbind_pasid);
785
Joerg Roedeled96f222011-11-23 17:30:39 +0100786int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
787{
788 struct device_state *dev_state;
789 unsigned long flags;
790 int ret, tmp;
791 u16 devid;
792
793 might_sleep();
794
795 if (!amd_iommu_v2_supported())
796 return -ENODEV;
797
798 if (pasids <= 0 || pasids > (PASID_MASK + 1))
799 return -EINVAL;
800
801 devid = device_id(pdev);
802
803 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
804 if (dev_state == NULL)
805 return -ENOMEM;
806
807 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100808 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200809 dev_state->pdev = pdev;
810 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100811
812 tmp = pasids;
813 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
814 dev_state->pasid_levels += 1;
815
816 atomic_set(&dev_state->count, 1);
817 dev_state->max_pasids = pasids;
818
819 ret = -ENOMEM;
820 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
821 if (dev_state->states == NULL)
822 goto out_free_dev_state;
823
824 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
825 if (dev_state->domain == NULL)
826 goto out_free_states;
827
828 amd_iommu_domain_direct_map(dev_state->domain);
829
830 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
831 if (ret)
832 goto out_free_domain;
833
834 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
835 if (ret != 0)
836 goto out_free_domain;
837
838 spin_lock_irqsave(&state_lock, flags);
839
Joerg Roedel741669c2014-05-20 23:18:23 +0200840 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100841 spin_unlock_irqrestore(&state_lock, flags);
842 ret = -EBUSY;
843 goto out_free_domain;
844 }
845
Joerg Roedel741669c2014-05-20 23:18:23 +0200846 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100847
848 spin_unlock_irqrestore(&state_lock, flags);
849
850 return 0;
851
852out_free_domain:
853 iommu_domain_free(dev_state->domain);
854
855out_free_states:
856 free_page((unsigned long)dev_state->states);
857
858out_free_dev_state:
859 kfree(dev_state);
860
861 return ret;
862}
863EXPORT_SYMBOL(amd_iommu_init_device);
864
865void amd_iommu_free_device(struct pci_dev *pdev)
866{
867 struct device_state *dev_state;
868 unsigned long flags;
869 u16 devid;
870
871 if (!amd_iommu_v2_supported())
872 return;
873
874 devid = device_id(pdev);
875
876 spin_lock_irqsave(&state_lock, flags);
877
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200878 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100879 if (dev_state == NULL) {
880 spin_unlock_irqrestore(&state_lock, flags);
881 return;
882 }
883
Joerg Roedel741669c2014-05-20 23:18:23 +0200884 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100885
886 spin_unlock_irqrestore(&state_lock, flags);
887
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100888 /* Get rid of any remaining pasid states */
889 free_pasid_states(dev_state);
890
Joerg Roedel028eeac2011-11-24 12:48:13 +0100891 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100892}
893EXPORT_SYMBOL(amd_iommu_free_device);
894
Joerg Roedel175d6142011-11-28 14:36:36 +0100895int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
896 amd_iommu_invalid_ppr_cb cb)
897{
898 struct device_state *dev_state;
899 unsigned long flags;
900 u16 devid;
901 int ret;
902
903 if (!amd_iommu_v2_supported())
904 return -ENODEV;
905
906 devid = device_id(pdev);
907
908 spin_lock_irqsave(&state_lock, flags);
909
910 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200911 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100912 if (dev_state == NULL)
913 goto out_unlock;
914
915 dev_state->inv_ppr_cb = cb;
916
917 ret = 0;
918
919out_unlock:
920 spin_unlock_irqrestore(&state_lock, flags);
921
922 return ret;
923}
924EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
925
Joerg Roedelbc216622011-12-07 12:24:42 +0100926int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
927 amd_iommu_invalidate_ctx cb)
928{
929 struct device_state *dev_state;
930 unsigned long flags;
931 u16 devid;
932 int ret;
933
934 if (!amd_iommu_v2_supported())
935 return -ENODEV;
936
937 devid = device_id(pdev);
938
939 spin_lock_irqsave(&state_lock, flags);
940
941 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200942 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100943 if (dev_state == NULL)
944 goto out_unlock;
945
946 dev_state->inv_ctx_cb = cb;
947
948 ret = 0;
949
950out_unlock:
951 spin_unlock_irqrestore(&state_lock, flags);
952
953 return ret;
954}
955EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
956
Joerg Roedele3c495c2011-11-09 12:31:15 +0100957static int __init amd_iommu_v2_init(void)
958{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100959 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100960
Joerg Roedel474d567d2012-03-15 12:46:40 +0100961 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
962
963 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900964 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100965 /*
966 * Load anyway to provide the symbols to other modules
967 * which may use AMD IOMMUv2 optionally.
968 */
969 return 0;
970 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100971
Joerg Roedeled96f222011-11-23 17:30:39 +0100972 spin_lock_init(&state_lock);
973
Joerg Roedel028eeac2011-11-24 12:48:13 +0100974 ret = -ENOMEM;
975 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100976 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200977 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100978
979 ret = -ENOMEM;
980 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
981 if (empty_page_table == NULL)
982 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100983
984 amd_iommu_register_ppr_notifier(&ppr_nb);
985
Joerg Roedele3c495c2011-11-09 12:31:15 +0100986 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100987
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100988out_destroy_wq:
989 destroy_workqueue(iommu_wq);
990
Joerg Roedel741669c2014-05-20 23:18:23 +0200991out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100992 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100993}
994
995static void __exit amd_iommu_v2_exit(void)
996{
Joerg Roedeled96f222011-11-23 17:30:39 +0100997 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100998 int i;
999
Joerg Roedel474d567d2012-03-15 12:46:40 +01001000 if (!amd_iommu_v2_supported())
1001 return;
1002
Joerg Roedel028eeac2011-11-24 12:48:13 +01001003 amd_iommu_unregister_ppr_notifier(&ppr_nb);
1004
1005 flush_workqueue(iommu_wq);
1006
1007 /*
1008 * The loop below might call flush_workqueue(), so call
1009 * destroy_workqueue() after it
1010 */
Joerg Roedeled96f222011-11-23 17:30:39 +01001011 for (i = 0; i < MAX_DEVICES; ++i) {
1012 dev_state = get_device_state(i);
1013
1014 if (dev_state == NULL)
1015 continue;
1016
1017 WARN_ON_ONCE(1);
1018
Joerg Roedeled96f222011-11-23 17:30:39 +01001019 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +01001020 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +01001021 }
1022
Joerg Roedel028eeac2011-11-24 12:48:13 +01001023 destroy_workqueue(iommu_wq);
1024
Joerg Roedel8736b2c2011-11-24 16:21:52 +01001025 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +01001026}
1027
1028module_init(amd_iommu_v2_init);
1029module_exit(amd_iommu_v2_exit);