blob: 5cc140969b4321ad1255496e77b85f1e9f1f6d46 [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 Roedel2d5503b2011-11-24 10:41:57 +010095static void free_pasid_states(struct device_state *dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +010096
97static u16 device_id(struct pci_dev *pdev)
98{
99 u16 devid;
100
101 devid = pdev->bus->number;
102 devid = (devid << 8) | pdev->devfn;
103
104 return devid;
105}
106
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200107static struct device_state *__get_device_state(u16 devid)
108{
Joerg Roedel741669c2014-05-20 23:18:23 +0200109 struct device_state *dev_state;
110
111 list_for_each_entry(dev_state, &state_list, list) {
112 if (dev_state->devid == devid)
113 return dev_state;
114 }
115
116 return NULL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200117}
118
Joerg Roedeled96f222011-11-23 17:30:39 +0100119static struct device_state *get_device_state(u16 devid)
120{
121 struct device_state *dev_state;
122 unsigned long flags;
123
124 spin_lock_irqsave(&state_lock, flags);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200125 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100126 if (dev_state != NULL)
127 atomic_inc(&dev_state->count);
128 spin_unlock_irqrestore(&state_lock, flags);
129
130 return dev_state;
131}
132
133static void free_device_state(struct device_state *dev_state)
134{
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100135 /*
136 * First detach device from domain - No more PRI requests will arrive
137 * from that device after it is unbound from the IOMMUv2 domain.
138 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100139 iommu_detach_device(dev_state->domain, &dev_state->pdev->dev);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100140
141 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100142 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100143
144 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100145 kfree(dev_state);
146}
147
148static void put_device_state(struct device_state *dev_state)
149{
150 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100151 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100152}
153
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100154/* Must be called under dev_state->lock */
155static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
156 int pasid, bool alloc)
157{
158 struct pasid_state **root, **ptr;
159 int level, index;
160
161 level = dev_state->pasid_levels;
162 root = dev_state->states;
163
164 while (true) {
165
166 index = (pasid >> (9 * level)) & 0x1ff;
167 ptr = &root[index];
168
169 if (level == 0)
170 break;
171
172 if (*ptr == NULL) {
173 if (!alloc)
174 return NULL;
175
176 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
177 if (*ptr == NULL)
178 return NULL;
179 }
180
181 root = (struct pasid_state **)*ptr;
182 level -= 1;
183 }
184
185 return ptr;
186}
187
188static int set_pasid_state(struct device_state *dev_state,
189 struct pasid_state *pasid_state,
190 int pasid)
191{
192 struct pasid_state **ptr;
193 unsigned long flags;
194 int ret;
195
196 spin_lock_irqsave(&dev_state->lock, flags);
197 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
198
199 ret = -ENOMEM;
200 if (ptr == NULL)
201 goto out_unlock;
202
203 ret = -ENOMEM;
204 if (*ptr != NULL)
205 goto out_unlock;
206
207 *ptr = pasid_state;
208
209 ret = 0;
210
211out_unlock:
212 spin_unlock_irqrestore(&dev_state->lock, flags);
213
214 return ret;
215}
216
217static void clear_pasid_state(struct device_state *dev_state, int pasid)
218{
219 struct pasid_state **ptr;
220 unsigned long flags;
221
222 spin_lock_irqsave(&dev_state->lock, flags);
223 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
224
225 if (ptr == NULL)
226 goto out_unlock;
227
228 *ptr = NULL;
229
230out_unlock:
231 spin_unlock_irqrestore(&dev_state->lock, flags);
232}
233
234static struct pasid_state *get_pasid_state(struct device_state *dev_state,
235 int pasid)
236{
237 struct pasid_state **ptr, *ret = NULL;
238 unsigned long flags;
239
240 spin_lock_irqsave(&dev_state->lock, flags);
241 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
242
243 if (ptr == NULL)
244 goto out_unlock;
245
246 ret = *ptr;
247 if (ret)
248 atomic_inc(&ret->count);
249
250out_unlock:
251 spin_unlock_irqrestore(&dev_state->lock, flags);
252
253 return ret;
254}
255
256static void free_pasid_state(struct pasid_state *pasid_state)
257{
258 kfree(pasid_state);
259}
260
261static void put_pasid_state(struct pasid_state *pasid_state)
262{
Oded Gabbay1c510992014-11-10 12:21:39 +0200263 if (atomic_dec_and_test(&pasid_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100264 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100265}
266
Joerg Roedel028eeac2011-11-24 12:48:13 +0100267static void put_pasid_state_wait(struct pasid_state *pasid_state)
268{
Joerg Roedela1bec062015-02-04 15:50:38 +0100269 wait_event(pasid_state->wq, !atomic_read(&pasid_state->count));
Joerg Roedel028eeac2011-11-24 12:48:13 +0100270 free_pasid_state(pasid_state);
271}
272
Joerg Roedel61feb432014-07-08 14:19:35 +0200273static void unbind_pasid(struct pasid_state *pasid_state)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100274{
275 struct iommu_domain *domain;
276
277 domain = pasid_state->device_state->domain;
278
Joerg Roedel53d340e2014-07-08 15:01:43 +0200279 /*
280 * Mark pasid_state as invalid, no more faults will we added to the
281 * work queue after this is visible everywhere.
282 */
283 pasid_state->invalid = true;
284
285 /* Make sure this is visible */
286 smp_wmb();
287
288 /* After this the device/pasid can't access the mm anymore */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100289 amd_iommu_domain_clear_gcr3(domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100290
291 /* Make sure no more pending faults are in the queue */
292 flush_workqueue(iommu_wq);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100293}
294
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100295static void free_pasid_states_level1(struct pasid_state **tbl)
296{
297 int i;
298
299 for (i = 0; i < 512; ++i) {
300 if (tbl[i] == NULL)
301 continue;
302
303 free_page((unsigned long)tbl[i]);
304 }
305}
306
307static void free_pasid_states_level2(struct pasid_state **tbl)
308{
309 struct pasid_state **ptr;
310 int i;
311
312 for (i = 0; i < 512; ++i) {
313 if (tbl[i] == NULL)
314 continue;
315
316 ptr = (struct pasid_state **)tbl[i];
317 free_pasid_states_level1(ptr);
318 }
319}
320
321static void free_pasid_states(struct device_state *dev_state)
322{
323 struct pasid_state *pasid_state;
324 int i;
325
326 for (i = 0; i < dev_state->max_pasids; ++i) {
327 pasid_state = get_pasid_state(dev_state, i);
328 if (pasid_state == NULL)
329 continue;
330
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100331 put_pasid_state(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200332
333 /*
334 * This will call the mn_release function and
335 * unbind the PASID
336 */
337 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200338
339 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200340 amd_iommu_bind_pasid */
Joerg Roedel75058a32014-07-30 16:04:39 +0200341
342 /* Drop reference taken in amd_iommu_bind_pasid */
343 put_device_state(dev_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100344 }
345
346 if (dev_state->pasid_levels == 2)
347 free_pasid_states_level2(dev_state->states);
348 else if (dev_state->pasid_levels == 1)
349 free_pasid_states_level1(dev_state->states);
350 else if (dev_state->pasid_levels != 0)
351 BUG();
352
353 free_page((unsigned long)dev_state->states);
354}
355
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100356static struct pasid_state *mn_to_state(struct mmu_notifier *mn)
357{
358 return container_of(mn, struct pasid_state, mn);
359}
360
361static void __mn_flush_page(struct mmu_notifier *mn,
362 unsigned long address)
363{
364 struct pasid_state *pasid_state;
365 struct device_state *dev_state;
366
367 pasid_state = mn_to_state(mn);
368 dev_state = pasid_state->device_state;
369
370 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid, address);
371}
372
373static int mn_clear_flush_young(struct mmu_notifier *mn,
374 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700375 unsigned long start,
376 unsigned long end)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100377{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700378 for (; start < end; start += PAGE_SIZE)
379 __mn_flush_page(mn, start);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100380
381 return 0;
382}
383
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100384static void mn_invalidate_page(struct mmu_notifier *mn,
385 struct mm_struct *mm,
386 unsigned long address)
387{
388 __mn_flush_page(mn, address);
389}
390
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100391static void mn_invalidate_range(struct mmu_notifier *mn,
392 struct mm_struct *mm,
393 unsigned long start, unsigned long end)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100394{
395 struct pasid_state *pasid_state;
396 struct device_state *dev_state;
397
398 pasid_state = mn_to_state(mn);
399 dev_state = pasid_state->device_state;
400
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100401 if ((start ^ (end - 1)) < PAGE_SIZE)
402 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid,
403 start);
404 else
405 amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100406}
407
Joerg Roedela40d4c62014-05-20 23:18:24 +0200408static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
409{
410 struct pasid_state *pasid_state;
411 struct device_state *dev_state;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200412 bool run_inv_ctx_cb;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200413
414 might_sleep();
415
Joerg Roedeld9e16112014-07-09 15:43:11 +0200416 pasid_state = mn_to_state(mn);
417 dev_state = pasid_state->device_state;
418 run_inv_ctx_cb = !pasid_state->invalid;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200419
Joerg Roedeld9e16112014-07-09 15:43:11 +0200420 if (run_inv_ctx_cb && pasid_state->device_state->inv_ctx_cb)
Joerg Roedela40d4c62014-05-20 23:18:24 +0200421 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
422
Joerg Roedel61feb432014-07-08 14:19:35 +0200423 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200424}
425
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100426static struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200427 .release = mn_release,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100428 .clear_flush_young = mn_clear_flush_young,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100429 .invalidate_page = mn_invalidate_page,
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100430 .invalidate_range = mn_invalidate_range,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100431};
432
Joerg Roedel028eeac2011-11-24 12:48:13 +0100433static void set_pri_tag_status(struct pasid_state *pasid_state,
434 u16 tag, int status)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&pasid_state->lock, flags);
439 pasid_state->pri[tag].status = status;
440 spin_unlock_irqrestore(&pasid_state->lock, flags);
441}
442
443static void finish_pri_tag(struct device_state *dev_state,
444 struct pasid_state *pasid_state,
445 u16 tag)
446{
447 unsigned long flags;
448
449 spin_lock_irqsave(&pasid_state->lock, flags);
450 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
451 pasid_state->pri[tag].finish) {
452 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
453 pasid_state->pri[tag].status, tag);
454 pasid_state->pri[tag].finish = false;
455 pasid_state->pri[tag].status = PPR_SUCCESS;
456 }
457 spin_unlock_irqrestore(&pasid_state->lock, flags);
458}
459
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800460static void handle_fault_error(struct fault *fault)
461{
462 int status;
463
464 if (!fault->dev_state->inv_ppr_cb) {
465 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
466 return;
467 }
468
469 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
470 fault->pasid,
471 fault->address,
472 fault->flags);
473 switch (status) {
474 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
475 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
476 break;
477 case AMD_IOMMU_INV_PRI_RSP_INVALID:
478 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
479 break;
480 case AMD_IOMMU_INV_PRI_RSP_FAIL:
481 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
482 break;
483 default:
484 BUG();
485 }
486}
487
Joerg Roedel028eeac2011-11-24 12:48:13 +0100488static void do_fault(struct work_struct *work)
489{
490 struct fault *fault = container_of(work, struct fault, work);
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800491 struct mm_struct *mm;
492 struct vm_area_struct *vma;
493 u64 address;
494 int ret, write;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100495
496 write = !!(fault->flags & PPR_FAULT_WRITE);
497
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800498 mm = fault->state->mm;
499 address = fault->address;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100500
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800501 down_read(&mm->mmap_sem);
502 vma = find_extend_vma(mm, address);
503 if (!vma || address < vma->vm_start) {
504 /* failed to get a vma in the right range */
505 up_read(&mm->mmap_sem);
506 handle_fault_error(fault);
507 goto out;
Joerg Roedel175d6142011-11-28 14:36:36 +0100508 }
Joerg Roedel028eeac2011-11-24 12:48:13 +0100509
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800510 ret = handle_mm_fault(mm, vma, address, write);
511 if (ret & VM_FAULT_ERROR) {
512 /* failed to service fault */
513 up_read(&mm->mmap_sem);
514 handle_fault_error(fault);
515 goto out;
516 }
517
518 up_read(&mm->mmap_sem);
519
520out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100521 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
522
523 put_pasid_state(fault->state);
524
525 kfree(fault);
526}
527
528static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
529{
530 struct amd_iommu_fault *iommu_fault;
531 struct pasid_state *pasid_state;
532 struct device_state *dev_state;
533 unsigned long flags;
534 struct fault *fault;
535 bool finish;
536 u16 tag;
537 int ret;
538
539 iommu_fault = data;
540 tag = iommu_fault->tag & 0x1ff;
541 finish = (iommu_fault->tag >> 9) & 1;
542
543 ret = NOTIFY_DONE;
544 dev_state = get_device_state(iommu_fault->device_id);
545 if (dev_state == NULL)
546 goto out;
547
548 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200549 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100550 /* We know the device but not the PASID -> send INVALID */
551 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
552 PPR_INVALID, tag);
553 goto out_drop_state;
554 }
555
556 spin_lock_irqsave(&pasid_state->lock, flags);
557 atomic_inc(&pasid_state->pri[tag].inflight);
558 if (finish)
559 pasid_state->pri[tag].finish = true;
560 spin_unlock_irqrestore(&pasid_state->lock, flags);
561
562 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
563 if (fault == NULL) {
564 /* We are OOM - send success and let the device re-fault */
565 finish_pri_tag(dev_state, pasid_state, tag);
566 goto out_drop_state;
567 }
568
569 fault->dev_state = dev_state;
570 fault->address = iommu_fault->address;
571 fault->state = pasid_state;
572 fault->tag = tag;
573 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300574 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100575 fault->flags = iommu_fault->flags;
576 INIT_WORK(&fault->work, do_fault);
577
578 queue_work(iommu_wq, &fault->work);
579
580 ret = NOTIFY_OK;
581
582out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200583
584 if (ret != NOTIFY_OK && pasid_state)
585 put_pasid_state(pasid_state);
586
Joerg Roedel028eeac2011-11-24 12:48:13 +0100587 put_device_state(dev_state);
588
589out:
590 return ret;
591}
592
593static struct notifier_block ppr_nb = {
594 .notifier_call = ppr_notifier,
595};
596
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100597int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
598 struct task_struct *task)
599{
600 struct pasid_state *pasid_state;
601 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200602 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100603 u16 devid;
604 int ret;
605
606 might_sleep();
607
608 if (!amd_iommu_v2_supported())
609 return -ENODEV;
610
611 devid = device_id(pdev);
612 dev_state = get_device_state(devid);
613
614 if (dev_state == NULL)
615 return -EINVAL;
616
617 ret = -EINVAL;
618 if (pasid < 0 || pasid >= dev_state->max_pasids)
619 goto out;
620
621 ret = -ENOMEM;
622 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
623 if (pasid_state == NULL)
624 goto out;
625
Joerg Roedelf0aac632014-07-08 15:15:07 +0200626
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100627 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100628 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200629 spin_lock_init(&pasid_state->lock);
630
Joerg Roedelf0aac632014-07-08 15:15:07 +0200631 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200632 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100633 pasid_state->device_state = dev_state;
634 pasid_state->pasid = pasid;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200635 pasid_state->invalid = true; /* Mark as valid only if we are
636 done with setting up the pasid */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100637 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100638
639 if (pasid_state->mm == NULL)
640 goto out_free;
641
Joerg Roedelf0aac632014-07-08 15:15:07 +0200642 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100643
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100644 ret = set_pasid_state(dev_state, pasid_state, pasid);
645 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100646 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100647
648 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
649 __pa(pasid_state->mm->pgd));
650 if (ret)
651 goto out_clear_state;
652
Joerg Roedeld9e16112014-07-09 15:43:11 +0200653 /* Now we are ready to handle faults */
654 pasid_state->invalid = false;
655
Joerg Roedelf0aac632014-07-08 15:15:07 +0200656 /*
657 * Drop the reference to the mm_struct here. We rely on the
658 * mmu_notifier release call-back to inform us when the mm
659 * is going away.
660 */
661 mmput(mm);
662
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100663 return 0;
664
665out_clear_state:
666 clear_pasid_state(dev_state, pasid);
667
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100668out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200669 mmu_notifier_unregister(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100670
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100671out_free:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200672 mmput(mm);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100673 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100674
675out:
676 put_device_state(dev_state);
677
678 return ret;
679}
680EXPORT_SYMBOL(amd_iommu_bind_pasid);
681
682void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
683{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200684 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100685 struct device_state *dev_state;
686 u16 devid;
687
688 might_sleep();
689
690 if (!amd_iommu_v2_supported())
691 return;
692
693 devid = device_id(pdev);
694 dev_state = get_device_state(devid);
695 if (dev_state == NULL)
696 return;
697
698 if (pasid < 0 || pasid >= dev_state->max_pasids)
699 goto out;
700
Joerg Roedela40d4c62014-05-20 23:18:24 +0200701 pasid_state = get_pasid_state(dev_state, pasid);
702 if (pasid_state == NULL)
703 goto out;
704 /*
705 * Drop reference taken here. We are safe because we still hold
706 * the reference taken in the amd_iommu_bind_pasid function.
707 */
708 put_pasid_state(pasid_state);
709
Joerg Roedel53d340e2014-07-08 15:01:43 +0200710 /* Clear the pasid state so that the pasid can be re-used */
711 clear_pasid_state(dev_state, pasid_state->pasid);
712
Joerg Roedelf0aac632014-07-08 15:15:07 +0200713 /*
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200714 * Call mmu_notifier_unregister to drop our reference
715 * to pasid_state->mm
Joerg Roedelf0aac632014-07-08 15:15:07 +0200716 */
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200717 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100718
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200719 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200720 amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100721out:
Joerg Roedel75058a32014-07-30 16:04:39 +0200722 /* Drop reference taken in this function */
723 put_device_state(dev_state);
724
725 /* Drop reference taken in amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100726 put_device_state(dev_state);
727}
728EXPORT_SYMBOL(amd_iommu_unbind_pasid);
729
Joerg Roedeled96f222011-11-23 17:30:39 +0100730int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
731{
732 struct device_state *dev_state;
733 unsigned long flags;
734 int ret, tmp;
735 u16 devid;
736
737 might_sleep();
738
739 if (!amd_iommu_v2_supported())
740 return -ENODEV;
741
742 if (pasids <= 0 || pasids > (PASID_MASK + 1))
743 return -EINVAL;
744
745 devid = device_id(pdev);
746
747 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
748 if (dev_state == NULL)
749 return -ENOMEM;
750
751 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100752 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200753 dev_state->pdev = pdev;
754 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100755
756 tmp = pasids;
757 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
758 dev_state->pasid_levels += 1;
759
760 atomic_set(&dev_state->count, 1);
761 dev_state->max_pasids = pasids;
762
763 ret = -ENOMEM;
764 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
765 if (dev_state->states == NULL)
766 goto out_free_dev_state;
767
768 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
769 if (dev_state->domain == NULL)
770 goto out_free_states;
771
772 amd_iommu_domain_direct_map(dev_state->domain);
773
774 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
775 if (ret)
776 goto out_free_domain;
777
778 ret = iommu_attach_device(dev_state->domain, &pdev->dev);
779 if (ret != 0)
780 goto out_free_domain;
781
782 spin_lock_irqsave(&state_lock, flags);
783
Joerg Roedel741669c2014-05-20 23:18:23 +0200784 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100785 spin_unlock_irqrestore(&state_lock, flags);
786 ret = -EBUSY;
787 goto out_free_domain;
788 }
789
Joerg Roedel741669c2014-05-20 23:18:23 +0200790 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100791
792 spin_unlock_irqrestore(&state_lock, flags);
793
794 return 0;
795
796out_free_domain:
797 iommu_domain_free(dev_state->domain);
798
799out_free_states:
800 free_page((unsigned long)dev_state->states);
801
802out_free_dev_state:
803 kfree(dev_state);
804
805 return ret;
806}
807EXPORT_SYMBOL(amd_iommu_init_device);
808
809void amd_iommu_free_device(struct pci_dev *pdev)
810{
811 struct device_state *dev_state;
812 unsigned long flags;
813 u16 devid;
814
815 if (!amd_iommu_v2_supported())
816 return;
817
818 devid = device_id(pdev);
819
820 spin_lock_irqsave(&state_lock, flags);
821
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200822 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100823 if (dev_state == NULL) {
824 spin_unlock_irqrestore(&state_lock, flags);
825 return;
826 }
827
Joerg Roedel741669c2014-05-20 23:18:23 +0200828 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100829
830 spin_unlock_irqrestore(&state_lock, flags);
831
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100832 /* Get rid of any remaining pasid states */
833 free_pasid_states(dev_state);
834
Peter Zijlstra91f65fa2015-02-03 13:25:51 +0100835 put_device_state(dev_state);
836 /*
837 * Wait until the last reference is dropped before freeing
838 * the device state.
839 */
840 wait_event(dev_state->wq, !atomic_read(&dev_state->count));
841 free_device_state(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100842}
843EXPORT_SYMBOL(amd_iommu_free_device);
844
Joerg Roedel175d6142011-11-28 14:36:36 +0100845int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
846 amd_iommu_invalid_ppr_cb cb)
847{
848 struct device_state *dev_state;
849 unsigned long flags;
850 u16 devid;
851 int ret;
852
853 if (!amd_iommu_v2_supported())
854 return -ENODEV;
855
856 devid = device_id(pdev);
857
858 spin_lock_irqsave(&state_lock, flags);
859
860 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200861 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100862 if (dev_state == NULL)
863 goto out_unlock;
864
865 dev_state->inv_ppr_cb = cb;
866
867 ret = 0;
868
869out_unlock:
870 spin_unlock_irqrestore(&state_lock, flags);
871
872 return ret;
873}
874EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
875
Joerg Roedelbc216622011-12-07 12:24:42 +0100876int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
877 amd_iommu_invalidate_ctx cb)
878{
879 struct device_state *dev_state;
880 unsigned long flags;
881 u16 devid;
882 int ret;
883
884 if (!amd_iommu_v2_supported())
885 return -ENODEV;
886
887 devid = device_id(pdev);
888
889 spin_lock_irqsave(&state_lock, flags);
890
891 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200892 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100893 if (dev_state == NULL)
894 goto out_unlock;
895
896 dev_state->inv_ctx_cb = cb;
897
898 ret = 0;
899
900out_unlock:
901 spin_unlock_irqrestore(&state_lock, flags);
902
903 return ret;
904}
905EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
906
Joerg Roedele3c495c2011-11-09 12:31:15 +0100907static int __init amd_iommu_v2_init(void)
908{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100909 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100910
Joerg Roedel474d567d2012-03-15 12:46:40 +0100911 pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n");
912
913 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900914 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100915 /*
916 * Load anyway to provide the symbols to other modules
917 * which may use AMD IOMMUv2 optionally.
918 */
919 return 0;
920 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100921
Joerg Roedeled96f222011-11-23 17:30:39 +0100922 spin_lock_init(&state_lock);
923
Joerg Roedel028eeac2011-11-24 12:48:13 +0100924 ret = -ENOMEM;
925 iommu_wq = create_workqueue("amd_iommu_v2");
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100926 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200927 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100928
Joerg Roedel028eeac2011-11-24 12:48:13 +0100929 amd_iommu_register_ppr_notifier(&ppr_nb);
930
Joerg Roedele3c495c2011-11-09 12:31:15 +0100931 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100932
Joerg Roedel741669c2014-05-20 23:18:23 +0200933out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100934 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100935}
936
937static void __exit amd_iommu_v2_exit(void)
938{
Joerg Roedeled96f222011-11-23 17:30:39 +0100939 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100940 int i;
941
Joerg Roedel474d567d2012-03-15 12:46:40 +0100942 if (!amd_iommu_v2_supported())
943 return;
944
Joerg Roedel028eeac2011-11-24 12:48:13 +0100945 amd_iommu_unregister_ppr_notifier(&ppr_nb);
946
947 flush_workqueue(iommu_wq);
948
949 /*
950 * The loop below might call flush_workqueue(), so call
951 * destroy_workqueue() after it
952 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100953 for (i = 0; i < MAX_DEVICES; ++i) {
954 dev_state = get_device_state(i);
955
956 if (dev_state == NULL)
957 continue;
958
959 WARN_ON_ONCE(1);
960
Joerg Roedeled96f222011-11-23 17:30:39 +0100961 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100962 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100963 }
964
Joerg Roedel028eeac2011-11-24 12:48:13 +0100965 destroy_workqueue(iommu_wq);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100966}
967
968module_init(amd_iommu_v2_init);
969module_exit(amd_iommu_v2_exit);