blob: a2d87a60c27f1c8d2c91264690b036b4c7174f5b [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{
Oded Gabbay1c510992014-11-10 12:21:39 +0200282 if (atomic_dec_and_test(&pasid_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100283 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100284}
285
Joerg Roedel028eeac2011-11-24 12:48:13 +0100286static void put_pasid_state_wait(struct pasid_state *pasid_state)
287{
288 DEFINE_WAIT(wait);
289
290 prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE);
291
Oded Gabbay1c510992014-11-10 12:21:39 +0200292 if (!atomic_dec_and_test(&pasid_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100293 schedule();
294
295 finish_wait(&pasid_state->wq, &wait);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100296 free_pasid_state(pasid_state);
297}
298
Joerg Roedel61feb432014-07-08 14:19:35 +0200299static void unbind_pasid(struct pasid_state *pasid_state)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100300{
301 struct iommu_domain *domain;
302
303 domain = pasid_state->device_state->domain;
304
Joerg Roedel53d340e2014-07-08 15:01:43 +0200305 /*
306 * Mark pasid_state as invalid, no more faults will we added to the
307 * work queue after this is visible everywhere.
308 */
309 pasid_state->invalid = true;
310
311 /* Make sure this is visible */
312 smp_wmb();
313
314 /* After this the device/pasid can't access the mm anymore */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100315 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100316
317 /* Make sure no more pending faults are in the queue */
318 flush_workqueue(iommu_wq);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100319}
320
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100321static void free_pasid_states_level1(struct pasid_state **tbl)
322{
323 int i;
324
325 for (i = 0; i < 512; ++i) {
326 if (tbl[i] == NULL)
327 continue;
328
329 free_page((unsigned long)tbl[i]);
330 }
331}
332
333static void free_pasid_states_level2(struct pasid_state **tbl)
334{
335 struct pasid_state **ptr;
336 int i;
337
338 for (i = 0; i < 512; ++i) {
339 if (tbl[i] == NULL)
340 continue;
341
342 ptr = (struct pasid_state **)tbl[i];
343 free_pasid_states_level1(ptr);
344 }
345}
346
347static void free_pasid_states(struct device_state *dev_state)
348{
349 struct pasid_state *pasid_state;
350 int i;
351
352 for (i = 0; i < dev_state->max_pasids; ++i) {
353 pasid_state = get_pasid_state(dev_state, i);
354 if (pasid_state == NULL)
355 continue;
356
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100357 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200358
359 /*
360 * This will call the mn_release function and
361 * unbind the PASID
362 */
363 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200364
365 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200366 amd_iommu_bind_pasid */
Joerg Roedel75058a32014-07-30 16:04:39 +0200367
368 /* Drop reference taken in amd_iommu_bind_pasid */
369 put_device_state(dev_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100370 }
371
372 if (dev_state->pasid_levels == 2)
373 free_pasid_states_level2(dev_state->states);
374 else if (dev_state->pasid_levels == 1)
375 free_pasid_states_level1(dev_state->states);
376 else if (dev_state->pasid_levels != 0)
377 BUG();
378
379 free_page((unsigned long)dev_state->states);
380}
381
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100382static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
383{
384 return container_of(mn, struct pasid_state, mn);
385}
386
387static void __mn_flush_page(struct mmu_notifier *mn,
388 unsigned long address)
389{
390 struct pasid_state *pasid_state;
391 struct device_state *dev_state;
392
393 pasid_state = mn_to_state(mn);
394 dev_state = pasid_state->device_state;
395
396 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
397}
398
399static int mn_clear_flush_young(struct mmu_notifier *mn,
400 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700401 unsigned long start,
402 unsigned long end)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100403{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700404 for (; start < end; start += PAGE_SIZE)
405 __mn_flush_page(mn, start);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100406
407 return 0;
408}
409
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100410static void mn_invalidate_page(struct mmu_notifier *mn,
411 struct mm_struct *mm,
412 unsigned long address)
413{
414 __mn_flush_page(mn, address);
415}
416
417static void mn_invalidate_range_start(struct mmu_notifier *mn,
418 struct mm_struct *mm,
419 unsigned long start, unsigned long end)
420{
421 struct pasid_state *pasid_state;
422 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200423 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100424
425 pasid_state = mn_to_state(mn);
426 dev_state = pasid_state->device_state;
427
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200428 spin_lock_irqsave(&pasid_state->lock, flags);
429 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200430 amd_iommu_domain_set_gcr3(dev_state->domain,
431 pasid_state->pasid,
432 __pa(empty_page_table));
433 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200434 pasid_state->mmu_notifier_count += 1;
435 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100436}
437
438static void mn_invalidate_range_end(struct mmu_notifier *mn,
439 struct mm_struct *mm,
440 unsigned long start, unsigned long end)
441{
442 struct pasid_state *pasid_state;
443 struct device_state *dev_state;
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200444 unsigned long flags;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100445
446 pasid_state = mn_to_state(mn);
447 dev_state = pasid_state->device_state;
448
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200449 spin_lock_irqsave(&pasid_state->lock, flags);
450 pasid_state->mmu_notifier_count -= 1;
451 if (pasid_state->mmu_notifier_count == 0) {
Joerg Roedele79df312014-05-20 23:18:26 +0200452 amd_iommu_domain_set_gcr3(dev_state->domain,
453 pasid_state->pasid,
454 __pa(pasid_state->mm->pgd));
455 }
Joerg Roedeld73a6d72014-06-20 16:14:22 +0200456 spin_unlock_irqrestore(&pasid_state->lock, flags);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100457}
458
Joerg Roedela40d4c62014-05-20 23:18:24 +0200459static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
460{
461 struct pasid_state *pasid_state;
462 struct device_state *dev_state;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200463 bool run_inv_ctx_cb;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200464
465 might_sleep();
466
Joerg Roedeld9e16112014-07-09 15:43:11 +0200467 pasid_state = mn_to_state(mn);
468 dev_state = pasid_state->device_state;
469 run_inv_ctx_cb = !pasid_state->invalid;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200470
Joerg Roedeld9e16112014-07-09 15:43:11 +0200471 if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
Joerg Roedela40d4c62014-05-20 23:18:24 +0200472 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
473
Joerg Roedel61feb432014-07-08 14:19:35 +0200474 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200475}
476
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100477static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200478 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100479 .clear_flush_young = mn_clear_flush_young,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100480 .invalidate_page = mn_invalidate_page,
481 .invalidate_range_start = mn_invalidate_range_start,
482 .invalidate_range_end = mn_invalidate_range_end,
483};
484
Joerg Roedel028eeac2011-11-24 12:48:13 +0100485static void set_pri_tag_status(struct pasid_state *pasid_state,
486 u16 tag, int status)
487{
488 unsigned long flags;
489
490 spin_lock_irqsave(&pasid_state->lock, flags);
491 pasid_state->pri[tag].status = status;
492 spin_unlock_irqrestore(&pasid_state->lock, flags);
493}
494
495static void finish_pri_tag(struct device_state *dev_state,
496 struct pasid_state *pasid_state,
497 u16 tag)
498{
499 unsigned long flags;
500
501 spin_lock_irqsave(&pasid_state->lock, flags);
502 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
503 pasid_state->pri[tag].finish) {
504 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
505 pasid_state->pri[tag].status, tag);
506 pasid_state->pri[tag].finish = false;
507 pasid_state->pri[tag].status = PPR_SUCCESS;
508 }
509 spin_unlock_irqrestore(&pasid_state->lock, flags);
510}
511
512static void do_fault(struct work_struct *work)
513{
514 struct fault *fault = container_of(work, struct fault, work);
515 int npages, write;
516 struct page *page;
517
518 write = !!(fault->flags & PPR_FAULT_WRITE);
519
Jay Cornwall4378d992014-04-28 17:27:46 -0500520 down_read(&fault->state->mm->mmap_sem);
Joerg Roedeldba38382014-07-09 17:56:43 +0200521 npages = get_user_pages(NULL, fault->state->mm,
Joerg Roedel028eeac2011-11-24 12:48:13 +0100522 fault->address, 1, write, 0, &page, NULL);
Jay Cornwall4378d992014-04-28 17:27:46 -0500523 up_read(&fault->state->mm->mmap_sem);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100524
Joerg Roedel175d6142011-11-28 14:36:36 +0100525 if (npages == 1) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100526 put_page(page);
Joerg Roedel175d6142011-11-28 14:36:36 +0100527 } else if (fault->dev_state->inv_ppr_cb) {
528 int status;
529
530 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
531 fault->pasid,
532 fault->address,
533 fault->flags);
534 switch (status) {
535 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
536 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
537 break;
538 case AMD_IOMMU_INV_PRI_RSP_INVALID:
539 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
540 break;
541 case AMD_IOMMU_INV_PRI_RSP_FAIL:
542 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
543 break;
544 default:
545 BUG();
546 }
547 } else {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100548 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
Joerg Roedel175d6142011-11-28 14:36:36 +0100549 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100550
551 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
552
553 put_pasid_state(fault->state);
554
555 kfree(fault);
556}
557
558static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
559{
560 struct amd_iommu_fault *iommu_fault;
561 struct pasid_state *pasid_state;
562 struct device_state *dev_state;
563 unsigned long flags;
564 struct fault *fault;
565 bool finish;
566 u16 tag;
567 int ret;
568
569 iommu_fault = data;
570 tag = iommu_fault->tag & 0x1ff;
571 finish = (iommu_fault->tag >> 9) & 1;
572
573 ret = NOTIFY_DONE;
574 dev_state = get_device_state(iommu_fault->device_id);
575 if (dev_state == NULL)
576 goto out;
577
578 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200579 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100580 /* We know the device but not the PASID -> send INVALID */
581 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
582 PPR_INVALID, tag);
583 goto out_drop_state;
584 }
585
586 spin_lock_irqsave(&pasid_state->lock, flags);
587 atomic_inc(&pasid_state->pri[tag].inflight);
588 if (finish)
589 pasid_state->pri[tag].finish = true;
590 spin_unlock_irqrestore(&pasid_state->lock, flags);
591
592 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
593 if (fault == NULL) {
594 /* We are OOM - send success and let the device re-fault */
595 finish_pri_tag(dev_state, pasid_state, tag);
596 goto out_drop_state;
597 }
598
599 fault->dev_state = dev_state;
600 fault->address = iommu_fault->address;
601 fault->state = pasid_state;
602 fault->tag = tag;
603 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300604 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100605 fault->flags = iommu_fault->flags;
606 INIT_WORK(&fault->work, do_fault);
607
608 queue_work(iommu_wq, &fault->work);
609
610 ret = NOTIFY_OK;
611
612out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200613
614 if (ret != NOTIFY_OK && pasid_state)
615 put_pasid_state(pasid_state);
616
Joerg Roedel028eeac2011-11-24 12:48:13 +0100617 put_device_state(dev_state);
618
619out:
620 return ret;
621}
622
623static struct notifier_block ppr_nb = {
624 .notifier_call = ppr_notifier,
625};
626
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100627int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
628 struct task_struct *task)
629{
630 struct pasid_state *pasid_state;
631 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200632 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100633 u16 devid;
634 int ret;
635
636 might_sleep();
637
638 if (!amd_iommu_v2_supported())
639 return -ENODEV;
640
641 devid = device_id(pdev);
642 dev_state = get_device_state(devid);
643
644 if (dev_state == NULL)
645 return -EINVAL;
646
647 ret = -EINVAL;
648 if (pasid < 0 || pasid >= dev_state->max_pasids)
649 goto out;
650
651 ret = -ENOMEM;
652 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
653 if (pasid_state == NULL)
654 goto out;
655
Joerg Roedelf0aac632014-07-08 15:15:07 +0200656
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100657 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100658 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200659 spin_lock_init(&pasid_state->lock);
660
Joerg Roedelf0aac632014-07-08 15:15:07 +0200661 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200662 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100663 pasid_state->device_state = dev_state;
664 pasid_state->pasid = pasid;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200665 pasid_state->invalid = true; /* Mark as valid only if we are
666 done with setting up the pasid */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100667 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100668
669 if (pasid_state->mm == NULL)
670 goto out_free;
671
Joerg Roedelf0aac632014-07-08 15:15:07 +0200672 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100673
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100674 ret = set_pasid_state(dev_state, pasid_state, pasid);
675 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100676 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100677
678 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
679 __pa(pasid_state->mm->pgd));
680 if (ret)
681 goto out_clear_state;
682
Joerg Roedeld9e16112014-07-09 15:43:11 +0200683 /* Now we are ready to handle faults */
684 pasid_state->invalid = false;
685
Joerg Roedelf0aac632014-07-08 15:15:07 +0200686 /*
687 * Drop the reference to the mm_struct here. We rely on the
688 * mmu_notifier release call-back to inform us when the mm
689 * is going away.
690 */
691 mmput(mm);
692
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100693 return 0;
694
695out_clear_state:
696 clear_pasid_state(dev_state, pasid);
697
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100698out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200699 mmu_notifier_unregister(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100700
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100701out_free:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200702 mmput(mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100703 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100704
705out:
706 put_device_state(dev_state);
707
708 return ret;
709}
710EXPORT_SYMBOL(amd_iommu_bind_pasid);
711
712void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
713{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200714 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100715 struct device_state *dev_state;
716 u16 devid;
717
718 might_sleep();
719
720 if (!amd_iommu_v2_supported())
721 return;
722
723 devid = device_id(pdev);
724 dev_state = get_device_state(devid);
725 if (dev_state == NULL)
726 return;
727
728 if (pasid < 0 || pasid >= dev_state->max_pasids)
729 goto out;
730
Joerg Roedela40d4c62014-05-20 23:18:24 +0200731 pasid_state = get_pasid_state(dev_state, pasid);
732 if (pasid_state == NULL)
733 goto out;
734 /*
735 * Drop reference taken here. We are safe because we still hold
736 * the reference taken in the amd_iommu_bind_pasid function.
737 */
738 put_pasid_state(pasid_state);
739
Joerg Roedel53d340e2014-07-08 15:01:43 +0200740 /* Clear the pasid state so that the pasid can be re-used */
741 clear_pasid_state(dev_state, pasid_state->pasid);
742
Joerg Roedelf0aac632014-07-08 15:15:07 +0200743 /*
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200744 * Call mmu_notifier_unregister to drop our reference
745 * to pasid_state->mm
Joerg Roedelf0aac632014-07-08 15:15:07 +0200746 */
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200747 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100748
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200749 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200750 amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100751out:
Joerg Roedel75058a32014-07-30 16:04:39 +0200752 /* Drop reference taken in this function */
753 put_device_state(dev_state);
754
755 /* Drop reference taken in amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100756 put_device_state(dev_state);
757}
758EXPORT_SYMBOL(amd_iommu_unbind_pasid);
759
Joerg Roedeled96f222011-11-23 17:30:39 +0100760int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
761{
762 struct device_state *dev_state;
763 unsigned long flags;
764 int ret, tmp;
765 u16 devid;
766
767 might_sleep();
768
769 if (!amd_iommu_v2_supported())
770 return -ENODEV;
771
772 if (pasids <= 0 || pasids > (PASID_MASK + 1))
773 return -EINVAL;
774
775 devid = device_id(pdev);
776
777 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
778 if (dev_state == NULL)
779 return -ENOMEM;
780
781 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100782 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200783 dev_state->pdev = pdev;
784 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100785
786 tmp = pasids;
787 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
788 dev_state->pasid_levels += 1;
789
790 atomic_set(&dev_state->count, 1);
791 dev_state->max_pasids = pasids;
792
793 ret = -ENOMEM;
794 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
795 if (dev_state->states == NULL)
796 goto out_free_dev_state;
797
798 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
799 if (dev_state->domain == NULL)
800 goto out_free_states;
801
802 amd_iommu_domain_direct_map(dev_state->domain);
803
804 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
805 if (ret)
806 goto out_free_domain;
807
808 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
809 if (ret != 0)
810 goto out_free_domain;
811
812 spin_lock_irqsave(&state_lock, flags);
813
Joerg Roedel741669c2014-05-20 23:18:23 +0200814 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100815 spin_unlock_irqrestore(&state_lock, flags);
816 ret = -EBUSY;
817 goto out_free_domain;
818 }
819
Joerg Roedel741669c2014-05-20 23:18:23 +0200820 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100821
822 spin_unlock_irqrestore(&state_lock, flags);
823
824 return 0;
825
826out_free_domain:
827 iommu_domain_free(dev_state->domain);
828
829out_free_states:
830 free_page((unsigned long)dev_state->states);
831
832out_free_dev_state:
833 kfree(dev_state);
834
835 return ret;
836}
837EXPORT_SYMBOL(amd_iommu_init_device);
838
839void amd_iommu_free_device(struct pci_dev *pdev)
840{
841 struct device_state *dev_state;
842 unsigned long flags;
843 u16 devid;
844
845 if (!amd_iommu_v2_supported())
846 return;
847
848 devid = device_id(pdev);
849
850 spin_lock_irqsave(&state_lock, flags);
851
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200852 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100853 if (dev_state == NULL) {
854 spin_unlock_irqrestore(&state_lock, flags);
855 return;
856 }
857
Joerg Roedel741669c2014-05-20 23:18:23 +0200858 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100859
860 spin_unlock_irqrestore(&state_lock, flags);
861
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100862 /* Get rid of any remaining pasid states */
863 free_pasid_states(dev_state);
864
Joerg Roedel028eeac2011-11-24 12:48:13 +0100865 put_device_state_wait(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100866}
867EXPORT_SYMBOL(amd_iommu_free_device);
868
Joerg Roedel175d6142011-11-28 14:36:36 +0100869int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
870 amd_iommu_invalid_ppr_cb cb)
871{
872 struct device_state *dev_state;
873 unsigned long flags;
874 u16 devid;
875 int ret;
876
877 if (!amd_iommu_v2_supported())
878 return -ENODEV;
879
880 devid = device_id(pdev);
881
882 spin_lock_irqsave(&state_lock, flags);
883
884 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200885 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100886 if (dev_state == NULL)
887 goto out_unlock;
888
889 dev_state->inv_ppr_cb = cb;
890
891 ret = 0;
892
893out_unlock:
894 spin_unlock_irqrestore(&state_lock, flags);
895
896 return ret;
897}
898EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
899
Joerg Roedelbc216622011-12-07 12:24:42 +0100900int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
901 amd_iommu_invalidate_ctx cb)
902{
903 struct device_state *dev_state;
904 unsigned long flags;
905 u16 devid;
906 int ret;
907
908 if (!amd_iommu_v2_supported())
909 return -ENODEV;
910
911 devid = device_id(pdev);
912
913 spin_lock_irqsave(&state_lock, flags);
914
915 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200916 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100917 if (dev_state == NULL)
918 goto out_unlock;
919
920 dev_state->inv_ctx_cb = cb;
921
922 ret = 0;
923
924out_unlock:
925 spin_unlock_irqrestore(&state_lock, flags);
926
927 return ret;
928}
929EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
930
Joerg Roedele3c495c2011-11-09 12:31:15 +0100931static int __init amd_iommu_v2_init(void)
932{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100933 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100934
Joerg Roedel474d567d2012-03-15 12:46:40 +0100935 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
936
937 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900938 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100939 /*
940 * Load anyway to provide the symbols to other modules
941 * which may use AMD IOMMUv2 optionally.
942 */
943 return 0;
944 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100945
Joerg Roedeled96f222011-11-23 17:30:39 +0100946 spin_lock_init(&state_lock);
947
Joerg Roedel028eeac2011-11-24 12:48:13 +0100948 ret = -ENOMEM;
949 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100950 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200951 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100952
953 ret = -ENOMEM;
954 empty_page_table = (u64 *)get_zeroed_page(GFP_KERNEL);
955 if (empty_page_table == NULL)
956 goto out_destroy_wq;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100957
958 amd_iommu_register_ppr_notifier(&ppr_nb);
959
Joerg Roedele3c495c2011-11-09 12:31:15 +0100960 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100961
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100962out_destroy_wq:
963 destroy_workqueue(iommu_wq);
964
Joerg Roedel741669c2014-05-20 23:18:23 +0200965out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100966 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100967}
968
969static void __exit amd_iommu_v2_exit(void)
970{
Joerg Roedeled96f222011-11-23 17:30:39 +0100971 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100972 int i;
973
Joerg Roedel474d567d2012-03-15 12:46:40 +0100974 if (!amd_iommu_v2_supported())
975 return;
976
Joerg Roedel028eeac2011-11-24 12:48:13 +0100977 amd_iommu_unregister_ppr_notifier(&ppr_nb);
978
979 flush_workqueue(iommu_wq);
980
981 /*
982 * The loop below might call flush_workqueue(), so call
983 * destroy_workqueue() after it
984 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100985 for (i = 0; i < MAX_DEVICES; ++i) {
986 dev_state = get_device_state(i);
987
988 if (dev_state == NULL)
989 continue;
990
991 WARN_ON_ONCE(1);
992
Joerg Roedeled96f222011-11-23 17:30:39 +0100993 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100994 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100995 }
996
Joerg Roedel028eeac2011-11-24 12:48:13 +0100997 destroy_workqueue(iommu_wq);
998
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100999 free_page((unsigned long)empty_page_table);
Joerg Roedele3c495c2011-11-09 12:31:15 +01001000}
1001
1002module_init(amd_iommu_v2_init);
1003module_exit(amd_iommu_v2_exit);