blob: d6d85debd01b0060205348d31dfdf4e4170bb059 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedele3c495c2011-11-09 12:31:15 +01002/*
3 * Copyright (C) 2010-2012 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01004 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedele3c495c2011-11-09 12:31:15 +01005 */
6
Joerg Roedel101fa032018-11-27 16:22:31 +01007#define pr_fmt(fmt) "AMD-Vi: " fmt
8
Joerg Roedel8736b2c2011-11-24 16:21:52 +01009#include <linux/mmu_notifier.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010010#include <linux/amd-iommu.h>
11#include <linux/mm_types.h>
Joerg Roedel8736b2c2011-11-24 16:21:52 +010012#include <linux/profile.h>
Joerg Roedele3c495c2011-11-09 12:31:15 +010013#include <linux/module.h>
Joerg Roedel2d5503b2011-11-24 10:41:57 +010014#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010015#include <linux/sched/mm.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010016#include <linux/iommu.h>
Joerg Roedel028eeac2011-11-24 12:48:13 +010017#include <linux/wait.h>
Joerg Roedeled96f222011-11-23 17:30:39 +010018#include <linux/pci.h>
19#include <linux/gfp.h>
20
Joerg Roedel028eeac2011-11-24 12:48:13 +010021#include "amd_iommu_types.h"
Joerg Roedeled96f222011-11-23 17:30:39 +010022#include "amd_iommu_proto.h"
Joerg Roedele3c495c2011-11-09 12:31:15 +010023
24MODULE_LICENSE("GPL v2");
Joerg Roedel63ce3ae2015-02-04 16:12:55 +010025MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>");
Joerg Roedele3c495c2011-11-09 12:31:15 +010026
Joerg Roedeled96f222011-11-23 17:30:39 +010027#define MAX_DEVICES 0x10000
28#define PRI_QUEUE_SIZE 512
29
30struct pri_queue {
31 atomic_t inflight;
32 bool finish;
Joerg Roedel028eeac2011-11-24 12:48:13 +010033 int status;
Joerg Roedeled96f222011-11-23 17:30:39 +010034};
35
36struct pasid_state {
37 struct list_head list; /* For global state-list */
38 atomic_t count; /* Reference count */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020039 unsigned mmu_notifier_count; /* Counting nested mmu_notifier
Joerg Roedele79df312014-05-20 23:18:26 +020040 calls */
Joerg Roedeled96f222011-11-23 17:30:39 +010041 struct mm_struct *mm; /* mm_struct for the faults */
Joerg Roedelff6d0cc2014-07-08 12:49:50 +020042 struct mmu_notifier mn; /* mmu_notifier handle */
Joerg Roedeled96f222011-11-23 17:30:39 +010043 struct pri_queue pri[PRI_QUEUE_SIZE]; /* PRI tag states */
44 struct device_state *device_state; /* Link to our device_state */
45 int pasid; /* PASID index */
Joerg Roedeld9e16112014-07-09 15:43:11 +020046 bool invalid; /* Used during setup and
47 teardown of the pasid */
Joerg Roedeld73a6d72014-06-20 16:14:22 +020048 spinlock_t lock; /* Protect pri_queues and
49 mmu_notifer_count */
Joerg Roedel028eeac2011-11-24 12:48:13 +010050 wait_queue_head_t wq; /* To wait for count == 0 */
Joerg Roedeled96f222011-11-23 17:30:39 +010051};
52
53struct device_state {
Joerg Roedel741669c2014-05-20 23:18:23 +020054 struct list_head list;
55 u16 devid;
Joerg Roedeled96f222011-11-23 17:30:39 +010056 atomic_t count;
57 struct pci_dev *pdev;
58 struct pasid_state **states;
59 struct iommu_domain *domain;
60 int pasid_levels;
61 int max_pasids;
Joerg Roedel175d6142011-11-28 14:36:36 +010062 amd_iommu_invalid_ppr_cb inv_ppr_cb;
Joerg Roedelbc216622011-12-07 12:24:42 +010063 amd_iommu_invalidate_ctx inv_ctx_cb;
Joerg Roedeled96f222011-11-23 17:30:39 +010064 spinlock_t lock;
Joerg Roedel028eeac2011-11-24 12:48:13 +010065 wait_queue_head_t wq;
66};
67
68struct fault {
69 struct work_struct work;
70 struct device_state *dev_state;
71 struct pasid_state *state;
72 struct mm_struct *mm;
73 u64 address;
74 u16 devid;
75 u16 pasid;
76 u16 tag;
77 u16 finish;
78 u16 flags;
Joerg Roedeled96f222011-11-23 17:30:39 +010079};
80
Joerg Roedel741669c2014-05-20 23:18:23 +020081static LIST_HEAD(state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +010082static spinlock_t state_lock;
83
Joerg Roedel028eeac2011-11-24 12:48:13 +010084static struct workqueue_struct *iommu_wq;
85
Joerg Roedel2d5503b2011-11-24 10:41:57 +010086static void free_pasid_states(struct device_state *dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +010087
88static u16 device_id(struct pci_dev *pdev)
89{
90 u16 devid;
91
92 devid = pdev->bus->number;
93 devid = (devid << 8) | pdev->devfn;
94
95 return devid;
96}
97
Joerg Roedelb87d2d72014-05-20 23:18:22 +020098static struct device_state *__get_device_state(u16 devid)
99{
Joerg Roedel741669c2014-05-20 23:18:23 +0200100 struct device_state *dev_state;
101
102 list_for_each_entry(dev_state, &state_list, list) {
103 if (dev_state->devid == devid)
104 return dev_state;
105 }
106
107 return NULL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200108}
109
Joerg Roedeled96f222011-11-23 17:30:39 +0100110static struct device_state *get_device_state(u16 devid)
111{
112 struct device_state *dev_state;
113 unsigned long flags;
114
115 spin_lock_irqsave(&state_lock, flags);
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200116 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100117 if (dev_state != NULL)
118 atomic_inc(&dev_state->count);
119 spin_unlock_irqrestore(&state_lock, flags);
120
121 return dev_state;
122}
123
124static void free_device_state(struct device_state *dev_state)
125{
Joerg Roedel55c99a42015-07-28 16:58:47 +0200126 struct iommu_group *group;
127
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100128 /*
129 * First detach device from domain - No more PRI requests will arrive
130 * from that device after it is unbound from the IOMMUv2 domain.
131 */
Joerg Roedel55c99a42015-07-28 16:58:47 +0200132 group = iommu_group_get(&dev_state->pdev->dev);
133 if (WARN_ON(!group))
134 return;
135
136 iommu_detach_group(dev_state->domain, group);
137
138 iommu_group_put(group);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100139
140 /* Everything is down now, free the IOMMUv2 domain */
Joerg Roedeled96f222011-11-23 17:30:39 +0100141 iommu_domain_free(dev_state->domain);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100142
143 /* Finally get rid of the device-state */
Joerg Roedeled96f222011-11-23 17:30:39 +0100144 kfree(dev_state);
145}
146
147static void put_device_state(struct device_state *dev_state)
148{
149 if (atomic_dec_and_test(&dev_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100150 wake_up(&dev_state->wq);
Joerg Roedeled96f222011-11-23 17:30:39 +0100151}
152
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100153/* Must be called under dev_state->lock */
154static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state,
155 int pasid, bool alloc)
156{
157 struct pasid_state **root, **ptr;
158 int level, index;
159
160 level = dev_state->pasid_levels;
161 root = dev_state->states;
162
163 while (true) {
164
165 index = (pasid >> (9 * level)) & 0x1ff;
166 ptr = &root[index];
167
168 if (level == 0)
169 break;
170
171 if (*ptr == NULL) {
172 if (!alloc)
173 return NULL;
174
175 *ptr = (void *)get_zeroed_page(GFP_ATOMIC);
176 if (*ptr == NULL)
177 return NULL;
178 }
179
180 root = (struct pasid_state **)*ptr;
181 level -= 1;
182 }
183
184 return ptr;
185}
186
187static int set_pasid_state(struct device_state *dev_state,
188 struct pasid_state *pasid_state,
189 int pasid)
190{
191 struct pasid_state **ptr;
192 unsigned long flags;
193 int ret;
194
195 spin_lock_irqsave(&dev_state->lock, flags);
196 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
197
198 ret = -ENOMEM;
199 if (ptr == NULL)
200 goto out_unlock;
201
202 ret = -ENOMEM;
203 if (*ptr != NULL)
204 goto out_unlock;
205
206 *ptr = pasid_state;
207
208 ret = 0;
209
210out_unlock:
211 spin_unlock_irqrestore(&dev_state->lock, flags);
212
213 return ret;
214}
215
216static void clear_pasid_state(struct device_state *dev_state, int pasid)
217{
218 struct pasid_state **ptr;
219 unsigned long flags;
220
221 spin_lock_irqsave(&dev_state->lock, flags);
222 ptr = __get_pasid_state_ptr(dev_state, pasid, true);
223
224 if (ptr == NULL)
225 goto out_unlock;
226
227 *ptr = NULL;
228
229out_unlock:
230 spin_unlock_irqrestore(&dev_state->lock, flags);
231}
232
233static struct pasid_state *get_pasid_state(struct device_state *dev_state,
234 int pasid)
235{
236 struct pasid_state **ptr, *ret = NULL;
237 unsigned long flags;
238
239 spin_lock_irqsave(&dev_state->lock, flags);
240 ptr = __get_pasid_state_ptr(dev_state, pasid, false);
241
242 if (ptr == NULL)
243 goto out_unlock;
244
245 ret = *ptr;
246 if (ret)
247 atomic_inc(&ret->count);
248
249out_unlock:
250 spin_unlock_irqrestore(&dev_state->lock, flags);
251
252 return ret;
253}
254
255static void free_pasid_state(struct pasid_state *pasid_state)
256{
257 kfree(pasid_state);
258}
259
260static void put_pasid_state(struct pasid_state *pasid_state)
261{
Oded Gabbay1c510992014-11-10 12:21:39 +0200262 if (atomic_dec_and_test(&pasid_state->count))
Joerg Roedel028eeac2011-11-24 12:48:13 +0100263 wake_up(&pasid_state->wq);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100264}
265
Joerg Roedel028eeac2011-11-24 12:48:13 +0100266static void put_pasid_state_wait(struct pasid_state *pasid_state)
267{
Oded Gabbay1bf1b432015-04-16 17:08:44 +0300268 atomic_dec(&pasid_state->count);
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);
Joerg Roedel23d3a982015-08-13 11:15:13 +0200350 else
351 BUG_ON(dev_state->pasid_levels != 0);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100352
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
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100361static void mn_invalidate_range(struct mmu_notifier *mn,
362 struct mm_struct *mm,
363 unsigned long start, unsigned long end)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100364{
365 struct pasid_state *pasid_state;
366 struct device_state *dev_state;
367
368 pasid_state = mn_to_state(mn);
369 dev_state = pasid_state->device_state;
370
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100371 if ((start ^ (end - 1)) < PAGE_SIZE)
372 amd_iommu_flush_page(dev_state->domain, pasid_state->pasid,
373 start);
374 else
375 amd_iommu_flush_tlb(dev_state->domain, pasid_state->pasid);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100376}
377
Joerg Roedela40d4c62014-05-20 23:18:24 +0200378static void mn_release(struct mmu_notifier *mn, struct mm_struct *mm)
379{
380 struct pasid_state *pasid_state;
381 struct device_state *dev_state;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200382 bool run_inv_ctx_cb;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200383
384 might_sleep();
385
Joerg Roedeld9e16112014-07-09 15:43:11 +0200386 pasid_state = mn_to_state(mn);
387 dev_state = pasid_state->device_state;
388 run_inv_ctx_cb = !pasid_state->invalid;
Joerg Roedela40d4c62014-05-20 23:18:24 +0200389
Dan Carpenter940f700d2015-02-20 13:52:01 +0300390 if (run_inv_ctx_cb && dev_state->inv_ctx_cb)
Joerg Roedela40d4c62014-05-20 23:18:24 +0200391 dev_state->inv_ctx_cb(dev_state->pdev, pasid_state->pasid);
392
Joerg Roedel61feb432014-07-08 14:19:35 +0200393 unbind_pasid(pasid_state);
Joerg Roedela40d4c62014-05-20 23:18:24 +0200394}
395
Julia Lawall759ce232015-11-29 23:02:50 +0100396static const struct mmu_notifier_ops iommu_mn = {
Joerg Roedela40d4c62014-05-20 23:18:24 +0200397 .release = mn_release,
Joerg Roedele7cc3dd2014-11-13 13:46:09 +1100398 .invalidate_range = mn_invalidate_range,
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100399};
400
Joerg Roedel028eeac2011-11-24 12:48:13 +0100401static void set_pri_tag_status(struct pasid_state *pasid_state,
402 u16 tag, int status)
403{
404 unsigned long flags;
405
406 spin_lock_irqsave(&pasid_state->lock, flags);
407 pasid_state->pri[tag].status = status;
408 spin_unlock_irqrestore(&pasid_state->lock, flags);
409}
410
411static void finish_pri_tag(struct device_state *dev_state,
412 struct pasid_state *pasid_state,
413 u16 tag)
414{
415 unsigned long flags;
416
417 spin_lock_irqsave(&pasid_state->lock, flags);
418 if (atomic_dec_and_test(&pasid_state->pri[tag].inflight) &&
419 pasid_state->pri[tag].finish) {
420 amd_iommu_complete_ppr(dev_state->pdev, pasid_state->pasid,
421 pasid_state->pri[tag].status, tag);
422 pasid_state->pri[tag].finish = false;
423 pasid_state->pri[tag].status = PPR_SUCCESS;
424 }
425 spin_unlock_irqrestore(&pasid_state->lock, flags);
426}
427
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800428static void handle_fault_error(struct fault *fault)
429{
430 int status;
431
432 if (!fault->dev_state->inv_ppr_cb) {
433 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
434 return;
435 }
436
437 status = fault->dev_state->inv_ppr_cb(fault->dev_state->pdev,
438 fault->pasid,
439 fault->address,
440 fault->flags);
441 switch (status) {
442 case AMD_IOMMU_INV_PRI_RSP_SUCCESS:
443 set_pri_tag_status(fault->state, fault->tag, PPR_SUCCESS);
444 break;
445 case AMD_IOMMU_INV_PRI_RSP_INVALID:
446 set_pri_tag_status(fault->state, fault->tag, PPR_INVALID);
447 break;
448 case AMD_IOMMU_INV_PRI_RSP_FAIL:
449 set_pri_tag_status(fault->state, fault->tag, PPR_FAILURE);
450 break;
451 default:
452 BUG();
453 }
454}
455
Joerg Roedel7b5cc1a2015-11-17 16:11:36 +0100456static bool access_error(struct vm_area_struct *vma, struct fault *fault)
457{
458 unsigned long requested = 0;
459
460 if (fault->flags & PPR_FAULT_EXEC)
461 requested |= VM_EXEC;
462
463 if (fault->flags & PPR_FAULT_READ)
464 requested |= VM_READ;
465
466 if (fault->flags & PPR_FAULT_WRITE)
467 requested |= VM_WRITE;
468
469 return (requested & ~vma->vm_flags) != 0;
470}
471
Joerg Roedel028eeac2011-11-24 12:48:13 +0100472static void do_fault(struct work_struct *work)
473{
474 struct fault *fault = container_of(work, struct fault, work);
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800475 struct vm_area_struct *vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700476 vm_fault_t ret = VM_FAULT_ERROR;
Joerg Roedel43c0ea22015-11-17 16:11:37 +0100477 unsigned int flags = 0;
478 struct mm_struct *mm;
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800479 u64 address;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100480
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800481 mm = fault->state->mm;
482 address = fault->address;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100483
Joerg Roedel43c0ea22015-11-17 16:11:37 +0100484 if (fault->flags & PPR_FAULT_USER)
485 flags |= FAULT_FLAG_USER;
486 if (fault->flags & PPR_FAULT_WRITE)
487 flags |= FAULT_FLAG_WRITE;
Dave Hansen1b2ee122016-02-12 13:02:21 -0800488 flags |= FAULT_FLAG_REMOTE;
Joerg Roedel43c0ea22015-11-17 16:11:37 +0100489
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800490 down_read(&mm->mmap_sem);
491 vma = find_extend_vma(mm, address);
Joerg Roedel492e7452015-11-17 16:11:38 +0100492 if (!vma || address < vma->vm_start)
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800493 /* failed to get a vma in the right range */
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800494 goto out;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100495
Joerg Roedel7b5cc1a2015-11-17 16:11:36 +0100496 /* Check if we have the right permissions on the vma */
Joerg Roedel492e7452015-11-17 16:11:38 +0100497 if (access_error(vma, fault))
Jay Cornwalld14f6fc2015-09-16 14:10:03 -0500498 goto out;
Jay Cornwalld14f6fc2015-09-16 14:10:03 -0500499
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700500 ret = handle_mm_fault(vma, address, flags);
Jesse Barnes9dc00f42014-12-12 16:55:30 -0800501out:
Joerg Roedel492e7452015-11-17 16:11:38 +0100502 up_read(&mm->mmap_sem);
503
504 if (ret & VM_FAULT_ERROR)
505 /* failed to service fault */
506 handle_fault_error(fault);
507
Joerg Roedel028eeac2011-11-24 12:48:13 +0100508 finish_pri_tag(fault->dev_state, fault->state, fault->tag);
509
510 put_pasid_state(fault->state);
511
512 kfree(fault);
513}
514
515static int ppr_notifier(struct notifier_block *nb, unsigned long e, void *data)
516{
517 struct amd_iommu_fault *iommu_fault;
518 struct pasid_state *pasid_state;
519 struct device_state *dev_state;
520 unsigned long flags;
521 struct fault *fault;
522 bool finish;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800523 u16 tag, devid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100524 int ret;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800525 struct iommu_dev_data *dev_data;
526 struct pci_dev *pdev = NULL;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100527
528 iommu_fault = data;
529 tag = iommu_fault->tag & 0x1ff;
530 finish = (iommu_fault->tag >> 9) & 1;
531
Baoquan Hedaae2d22017-08-09 16:33:43 +0800532 devid = iommu_fault->device_id;
Sinan Kayad5bf0f42017-12-19 00:37:47 -0500533 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
534 devid & 0xff);
Baoquan Hedaae2d22017-08-09 16:33:43 +0800535 if (!pdev)
536 return -ENODEV;
537 dev_data = get_dev_data(&pdev->dev);
538
539 /* In kdump kernel pci dev is not initialized yet -> send INVALID */
Joerg Roedel028eeac2011-11-24 12:48:13 +0100540 ret = NOTIFY_DONE;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800541 if (translation_pre_enabled(amd_iommu_rlookup_table[devid])
542 && dev_data->defer_attach) {
543 amd_iommu_complete_ppr(pdev, iommu_fault->pasid,
544 PPR_INVALID, tag);
545 goto out;
546 }
547
Joerg Roedel028eeac2011-11-24 12:48:13 +0100548 dev_state = get_device_state(iommu_fault->device_id);
549 if (dev_state == NULL)
550 goto out;
551
552 pasid_state = get_pasid_state(dev_state, iommu_fault->pasid);
Joerg Roedel53d340e2014-07-08 15:01:43 +0200553 if (pasid_state == NULL || pasid_state->invalid) {
Joerg Roedel028eeac2011-11-24 12:48:13 +0100554 /* We know the device but not the PASID -> send INVALID */
555 amd_iommu_complete_ppr(dev_state->pdev, iommu_fault->pasid,
556 PPR_INVALID, tag);
557 goto out_drop_state;
558 }
559
560 spin_lock_irqsave(&pasid_state->lock, flags);
561 atomic_inc(&pasid_state->pri[tag].inflight);
562 if (finish)
563 pasid_state->pri[tag].finish = true;
564 spin_unlock_irqrestore(&pasid_state->lock, flags);
565
566 fault = kzalloc(sizeof(*fault), GFP_ATOMIC);
567 if (fault == NULL) {
568 /* We are OOM - send success and let the device re-fault */
569 finish_pri_tag(dev_state, pasid_state, tag);
570 goto out_drop_state;
571 }
572
573 fault->dev_state = dev_state;
574 fault->address = iommu_fault->address;
575 fault->state = pasid_state;
576 fault->tag = tag;
577 fault->finish = finish;
Alexey Skidanovb00675b2014-07-08 17:30:16 +0300578 fault->pasid = iommu_fault->pasid;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100579 fault->flags = iommu_fault->flags;
580 INIT_WORK(&fault->work, do_fault);
581
582 queue_work(iommu_wq, &fault->work);
583
584 ret = NOTIFY_OK;
585
586out_drop_state:
Joerg Roedeldc88db72014-07-08 14:55:10 +0200587
588 if (ret != NOTIFY_OK && pasid_state)
589 put_pasid_state(pasid_state);
590
Joerg Roedel028eeac2011-11-24 12:48:13 +0100591 put_device_state(dev_state);
592
593out:
594 return ret;
595}
596
597static struct notifier_block ppr_nb = {
598 .notifier_call = ppr_notifier,
599};
600
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100601int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,
602 struct task_struct *task)
603{
604 struct pasid_state *pasid_state;
605 struct device_state *dev_state;
Joerg Roedelf0aac632014-07-08 15:15:07 +0200606 struct mm_struct *mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100607 u16 devid;
608 int ret;
609
610 might_sleep();
611
612 if (!amd_iommu_v2_supported())
613 return -ENODEV;
614
615 devid = device_id(pdev);
616 dev_state = get_device_state(devid);
617
618 if (dev_state == NULL)
619 return -EINVAL;
620
621 ret = -EINVAL;
622 if (pasid < 0 || pasid >= dev_state->max_pasids)
623 goto out;
624
625 ret = -ENOMEM;
626 pasid_state = kzalloc(sizeof(*pasid_state), GFP_KERNEL);
627 if (pasid_state == NULL)
628 goto out;
629
Joerg Roedelf0aac632014-07-08 15:15:07 +0200630
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100631 atomic_set(&pasid_state->count, 1);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100632 init_waitqueue_head(&pasid_state->wq);
Joerg Roedel2c13d472012-07-19 10:56:10 +0200633 spin_lock_init(&pasid_state->lock);
634
Joerg Roedelf0aac632014-07-08 15:15:07 +0200635 mm = get_task_mm(task);
Joerg Roedelf0aac632014-07-08 15:15:07 +0200636 pasid_state->mm = mm;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100637 pasid_state->device_state = dev_state;
638 pasid_state->pasid = pasid;
Joerg Roedeld9e16112014-07-09 15:43:11 +0200639 pasid_state->invalid = true; /* Mark as valid only if we are
640 done with setting up the pasid */
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100641 pasid_state->mn.ops = &iommu_mn;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100642
643 if (pasid_state->mm == NULL)
644 goto out_free;
645
Joerg Roedelf0aac632014-07-08 15:15:07 +0200646 mmu_notifier_register(&pasid_state->mn, mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100647
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100648 ret = set_pasid_state(dev_state, pasid_state, pasid);
649 if (ret)
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100650 goto out_unregister;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100651
652 ret = amd_iommu_domain_set_gcr3(dev_state->domain, pasid,
653 __pa(pasid_state->mm->pgd));
654 if (ret)
655 goto out_clear_state;
656
Joerg Roedeld9e16112014-07-09 15:43:11 +0200657 /* Now we are ready to handle faults */
658 pasid_state->invalid = false;
659
Joerg Roedelf0aac632014-07-08 15:15:07 +0200660 /*
661 * Drop the reference to the mm_struct here. We rely on the
662 * mmu_notifier release call-back to inform us when the mm
663 * is going away.
664 */
665 mmput(mm);
666
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100667 return 0;
668
669out_clear_state:
670 clear_pasid_state(dev_state, pasid);
671
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100672out_unregister:
Joerg Roedelf0aac632014-07-08 15:15:07 +0200673 mmu_notifier_unregister(&pasid_state->mn, mm);
Pan Bian73dbd4a2017-04-23 18:23:21 +0800674 mmput(mm);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100675
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100676out_free:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100677 free_pasid_state(pasid_state);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100678
679out:
680 put_device_state(dev_state);
681
682 return ret;
683}
684EXPORT_SYMBOL(amd_iommu_bind_pasid);
685
686void amd_iommu_unbind_pasid(struct pci_dev *pdev, int pasid)
687{
Joerg Roedela40d4c62014-05-20 23:18:24 +0200688 struct pasid_state *pasid_state;
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100689 struct device_state *dev_state;
690 u16 devid;
691
692 might_sleep();
693
694 if (!amd_iommu_v2_supported())
695 return;
696
697 devid = device_id(pdev);
698 dev_state = get_device_state(devid);
699 if (dev_state == NULL)
700 return;
701
702 if (pasid < 0 || pasid >= dev_state->max_pasids)
703 goto out;
704
Joerg Roedela40d4c62014-05-20 23:18:24 +0200705 pasid_state = get_pasid_state(dev_state, pasid);
706 if (pasid_state == NULL)
707 goto out;
708 /*
709 * Drop reference taken here. We are safe because we still hold
710 * the reference taken in the amd_iommu_bind_pasid function.
711 */
712 put_pasid_state(pasid_state);
713
Joerg Roedel53d340e2014-07-08 15:01:43 +0200714 /* Clear the pasid state so that the pasid can be re-used */
715 clear_pasid_state(dev_state, pasid_state->pasid);
716
Joerg Roedelf0aac632014-07-08 15:15:07 +0200717 /*
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200718 * Call mmu_notifier_unregister to drop our reference
719 * to pasid_state->mm
Joerg Roedelf0aac632014-07-08 15:15:07 +0200720 */
Joerg Roedelfcaa9602014-07-30 16:04:37 +0200721 mmu_notifier_unregister(&pasid_state->mn, pasid_state->mm);
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100722
Joerg Roedelc5db16a2014-07-08 14:15:45 +0200723 put_pasid_state_wait(pasid_state); /* Reference taken in
Joerg Roedeldaff2f92014-07-30 16:04:40 +0200724 amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100725out:
Joerg Roedel75058a32014-07-30 16:04:39 +0200726 /* Drop reference taken in this function */
727 put_device_state(dev_state);
728
729 /* Drop reference taken in amd_iommu_bind_pasid */
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100730 put_device_state(dev_state);
731}
732EXPORT_SYMBOL(amd_iommu_unbind_pasid);
733
Joerg Roedeled96f222011-11-23 17:30:39 +0100734int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
735{
736 struct device_state *dev_state;
Joerg Roedel55c99a42015-07-28 16:58:47 +0200737 struct iommu_group *group;
Joerg Roedeled96f222011-11-23 17:30:39 +0100738 unsigned long flags;
739 int ret, tmp;
740 u16 devid;
741
742 might_sleep();
743
744 if (!amd_iommu_v2_supported())
745 return -ENODEV;
746
747 if (pasids <= 0 || pasids > (PASID_MASK + 1))
748 return -EINVAL;
749
750 devid = device_id(pdev);
751
752 dev_state = kzalloc(sizeof(*dev_state), GFP_KERNEL);
753 if (dev_state == NULL)
754 return -ENOMEM;
755
756 spin_lock_init(&dev_state->lock);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100757 init_waitqueue_head(&dev_state->wq);
Joerg Roedel741669c2014-05-20 23:18:23 +0200758 dev_state->pdev = pdev;
759 dev_state->devid = devid;
Joerg Roedeled96f222011-11-23 17:30:39 +0100760
761 tmp = pasids;
762 for (dev_state->pasid_levels = 0; (tmp - 1) & ~0x1ff; tmp >>= 9)
763 dev_state->pasid_levels += 1;
764
765 atomic_set(&dev_state->count, 1);
766 dev_state->max_pasids = pasids;
767
768 ret = -ENOMEM;
769 dev_state->states = (void *)get_zeroed_page(GFP_KERNEL);
770 if (dev_state->states == NULL)
771 goto out_free_dev_state;
772
773 dev_state->domain = iommu_domain_alloc(&pci_bus_type);
774 if (dev_state->domain == NULL)
775 goto out_free_states;
776
777 amd_iommu_domain_direct_map(dev_state->domain);
778
779 ret = amd_iommu_domain_enable_v2(dev_state->domain, pasids);
780 if (ret)
781 goto out_free_domain;
782
Joerg Roedel55c99a42015-07-28 16:58:47 +0200783 group = iommu_group_get(&pdev->dev);
Dan Carpenter24c790f2016-11-24 14:05:44 +0300784 if (!group) {
785 ret = -EINVAL;
Joerg Roedeled96f222011-11-23 17:30:39 +0100786 goto out_free_domain;
Dan Carpenter24c790f2016-11-24 14:05:44 +0300787 }
Joerg Roedeled96f222011-11-23 17:30:39 +0100788
Joerg Roedel55c99a42015-07-28 16:58:47 +0200789 ret = iommu_attach_group(dev_state->domain, group);
790 if (ret != 0)
791 goto out_drop_group;
792
793 iommu_group_put(group);
794
Joerg Roedeled96f222011-11-23 17:30:39 +0100795 spin_lock_irqsave(&state_lock, flags);
796
Joerg Roedel741669c2014-05-20 23:18:23 +0200797 if (__get_device_state(devid) != NULL) {
Joerg Roedeled96f222011-11-23 17:30:39 +0100798 spin_unlock_irqrestore(&state_lock, flags);
799 ret = -EBUSY;
800 goto out_free_domain;
801 }
802
Joerg Roedel741669c2014-05-20 23:18:23 +0200803 list_add_tail(&dev_state->list, &state_list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100804
805 spin_unlock_irqrestore(&state_lock, flags);
806
807 return 0;
808
Joerg Roedel55c99a42015-07-28 16:58:47 +0200809out_drop_group:
810 iommu_group_put(group);
811
Joerg Roedeled96f222011-11-23 17:30:39 +0100812out_free_domain:
813 iommu_domain_free(dev_state->domain);
814
815out_free_states:
816 free_page((unsigned long)dev_state->states);
817
818out_free_dev_state:
819 kfree(dev_state);
820
821 return ret;
822}
823EXPORT_SYMBOL(amd_iommu_init_device);
824
825void amd_iommu_free_device(struct pci_dev *pdev)
826{
827 struct device_state *dev_state;
828 unsigned long flags;
829 u16 devid;
830
831 if (!amd_iommu_v2_supported())
832 return;
833
834 devid = device_id(pdev);
835
836 spin_lock_irqsave(&state_lock, flags);
837
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200838 dev_state = __get_device_state(devid);
Joerg Roedeled96f222011-11-23 17:30:39 +0100839 if (dev_state == NULL) {
840 spin_unlock_irqrestore(&state_lock, flags);
841 return;
842 }
843
Joerg Roedel741669c2014-05-20 23:18:23 +0200844 list_del(&dev_state->list);
Joerg Roedeled96f222011-11-23 17:30:39 +0100845
846 spin_unlock_irqrestore(&state_lock, flags);
847
Joerg Roedel2d5503b2011-11-24 10:41:57 +0100848 /* Get rid of any remaining pasid states */
849 free_pasid_states(dev_state);
850
Peter Zijlstra91f65fa2015-02-03 13:25:51 +0100851 put_device_state(dev_state);
852 /*
853 * Wait until the last reference is dropped before freeing
854 * the device state.
855 */
856 wait_event(dev_state->wq, !atomic_read(&dev_state->count));
857 free_device_state(dev_state);
Joerg Roedeled96f222011-11-23 17:30:39 +0100858}
859EXPORT_SYMBOL(amd_iommu_free_device);
860
Joerg Roedel175d6142011-11-28 14:36:36 +0100861int amd_iommu_set_invalid_ppr_cb(struct pci_dev *pdev,
862 amd_iommu_invalid_ppr_cb cb)
863{
864 struct device_state *dev_state;
865 unsigned long flags;
866 u16 devid;
867 int ret;
868
869 if (!amd_iommu_v2_supported())
870 return -ENODEV;
871
872 devid = device_id(pdev);
873
874 spin_lock_irqsave(&state_lock, flags);
875
876 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200877 dev_state = __get_device_state(devid);
Joerg Roedel175d6142011-11-28 14:36:36 +0100878 if (dev_state == NULL)
879 goto out_unlock;
880
881 dev_state->inv_ppr_cb = cb;
882
883 ret = 0;
884
885out_unlock:
886 spin_unlock_irqrestore(&state_lock, flags);
887
888 return ret;
889}
890EXPORT_SYMBOL(amd_iommu_set_invalid_ppr_cb);
891
Joerg Roedelbc216622011-12-07 12:24:42 +0100892int amd_iommu_set_invalidate_ctx_cb(struct pci_dev *pdev,
893 amd_iommu_invalidate_ctx cb)
894{
895 struct device_state *dev_state;
896 unsigned long flags;
897 u16 devid;
898 int ret;
899
900 if (!amd_iommu_v2_supported())
901 return -ENODEV;
902
903 devid = device_id(pdev);
904
905 spin_lock_irqsave(&state_lock, flags);
906
907 ret = -EINVAL;
Joerg Roedelb87d2d72014-05-20 23:18:22 +0200908 dev_state = __get_device_state(devid);
Joerg Roedelbc216622011-12-07 12:24:42 +0100909 if (dev_state == NULL)
910 goto out_unlock;
911
912 dev_state->inv_ctx_cb = cb;
913
914 ret = 0;
915
916out_unlock:
917 spin_unlock_irqrestore(&state_lock, flags);
918
919 return ret;
920}
921EXPORT_SYMBOL(amd_iommu_set_invalidate_ctx_cb);
922
Joerg Roedele3c495c2011-11-09 12:31:15 +0100923static int __init amd_iommu_v2_init(void)
924{
Joerg Roedel028eeac2011-11-24 12:48:13 +0100925 int ret;
Joerg Roedeled96f222011-11-23 17:30:39 +0100926
Joerg Roedel63ce3ae2015-02-04 16:12:55 +0100927 pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100928
929 if (!amd_iommu_v2_supported()) {
Masanari Iida07db0402012-07-22 02:21:32 +0900930 pr_info("AMD IOMMUv2 functionality not available on this system\n");
Joerg Roedel474d567d2012-03-15 12:46:40 +0100931 /*
932 * Load anyway to provide the symbols to other modules
933 * which may use AMD IOMMUv2 optionally.
934 */
935 return 0;
936 }
Joerg Roedele3c495c2011-11-09 12:31:15 +0100937
Joerg Roedeled96f222011-11-23 17:30:39 +0100938 spin_lock_init(&state_lock);
939
Joerg Roedel028eeac2011-11-24 12:48:13 +0100940 ret = -ENOMEM;
Bhaktipriya Shridharcf7513e2016-06-18 13:58:30 +0530941 iommu_wq = alloc_workqueue("amd_iommu_v2", WQ_MEM_RECLAIM, 0);
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100942 if (iommu_wq == NULL)
Joerg Roedel741669c2014-05-20 23:18:23 +0200943 goto out;
Joerg Roedel8736b2c2011-11-24 16:21:52 +0100944
Joerg Roedel028eeac2011-11-24 12:48:13 +0100945 amd_iommu_register_ppr_notifier(&ppr_nb);
946
Joerg Roedele3c495c2011-11-09 12:31:15 +0100947 return 0;
Joerg Roedel028eeac2011-11-24 12:48:13 +0100948
Joerg Roedel741669c2014-05-20 23:18:23 +0200949out:
Joerg Roedel028eeac2011-11-24 12:48:13 +0100950 return ret;
Joerg Roedele3c495c2011-11-09 12:31:15 +0100951}
952
953static void __exit amd_iommu_v2_exit(void)
954{
Joerg Roedeled96f222011-11-23 17:30:39 +0100955 struct device_state *dev_state;
Joerg Roedeled96f222011-11-23 17:30:39 +0100956 int i;
957
Joerg Roedel474d567d2012-03-15 12:46:40 +0100958 if (!amd_iommu_v2_supported())
959 return;
960
Joerg Roedel028eeac2011-11-24 12:48:13 +0100961 amd_iommu_unregister_ppr_notifier(&ppr_nb);
962
963 flush_workqueue(iommu_wq);
964
965 /*
966 * The loop below might call flush_workqueue(), so call
967 * destroy_workqueue() after it
968 */
Joerg Roedeled96f222011-11-23 17:30:39 +0100969 for (i = 0; i < MAX_DEVICES; ++i) {
970 dev_state = get_device_state(i);
971
972 if (dev_state == NULL)
973 continue;
974
975 WARN_ON_ONCE(1);
976
Joerg Roedeled96f222011-11-23 17:30:39 +0100977 put_device_state(dev_state);
Joerg Roedel028eeac2011-11-24 12:48:13 +0100978 amd_iommu_free_device(dev_state->pdev);
Joerg Roedeled96f222011-11-23 17:30:39 +0100979 }
980
Joerg Roedel028eeac2011-11-24 12:48:13 +0100981 destroy_workqueue(iommu_wq);
Joerg Roedele3c495c2011-11-09 12:31:15 +0100982}
983
984module_init(amd_iommu_v2_init);
985module_exit(amd_iommu_v2_exit);