blob: a035ef911fba789dd206cbadd1cbdac96cf3d217 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
David Woodhouse8a94ade2015-03-24 14:54:56 +00002/*
3 * Copyright © 2015 Intel Corporation.
4 *
David Woodhouse8a94ade2015-03-24 14:54:56 +00005 * Authors: David Woodhouse <dwmw2@infradead.org>
6 */
7
8#include <linux/intel-iommu.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +01009#include <linux/mmu_notifier.h>
10#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010011#include <linux/sched/mm.h>
David Woodhouse2f26e0a2015-09-09 11:40:47 +010012#include <linux/slab.h>
13#include <linux/intel-svm.h>
14#include <linux/rculist.h>
15#include <linux/pci.h>
16#include <linux/pci-ats.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010017#include <linux/dmar.h>
18#include <linux/interrupt.h>
Souptick Joarder50a7ca32018-08-17 15:44:47 -070019#include <linux/mm_types.h>
Jacob Pan59a62332020-01-02 08:18:08 +080020#include <linux/ioasid.h>
Ashok Raj9d8c3af2017-08-08 13:29:27 -070021#include <asm/page.h>
David Woodhousea222a7f2015-10-07 23:35:18 +010022
Lu Baoluaf395072018-07-14 15:46:56 +080023#include "intel-pasid.h"
24
David Woodhousea222a7f2015-10-07 23:35:18 +010025static irqreturn_t prq_event_thread(int irq, void *d);
Lu Baolu66ac4db2020-05-16 14:20:58 +080026static void intel_svm_drain_prq(struct device *dev, int pasid);
David Woodhouse2f26e0a2015-09-09 11:40:47 +010027
David Woodhousea222a7f2015-10-07 23:35:18 +010028#define PRQ_ORDER 0
29
30int intel_svm_enable_prq(struct intel_iommu *iommu)
31{
32 struct page *pages;
33 int irq, ret;
34
35 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
36 if (!pages) {
37 pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
38 iommu->name);
39 return -ENOMEM;
40 }
41 iommu->prq = page_address(pages);
42
43 irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
44 if (irq <= 0) {
45 pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
46 iommu->name);
47 ret = -EINVAL;
48 err:
49 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
50 iommu->prq = NULL;
51 return ret;
52 }
53 iommu->pr_irq = irq;
54
55 snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
56
57 ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
58 iommu->prq_name, iommu);
59 if (ret) {
60 pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
61 iommu->name);
62 dmar_free_hwirq(irq);
Jerry Snitselaar72d54812017-12-20 09:48:56 -070063 iommu->pr_irq = 0;
David Woodhousea222a7f2015-10-07 23:35:18 +010064 goto err;
65 }
66 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
67 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
68 dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
69
Lu Baolu66ac4db2020-05-16 14:20:58 +080070 init_completion(&iommu->prq_complete);
71
David Woodhousea222a7f2015-10-07 23:35:18 +010072 return 0;
73}
74
75int intel_svm_finish_prq(struct intel_iommu *iommu)
76{
77 dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
78 dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
79 dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
80
Jerry Snitselaar72d54812017-12-20 09:48:56 -070081 if (iommu->pr_irq) {
82 free_irq(iommu->pr_irq, iommu);
83 dmar_free_hwirq(iommu->pr_irq);
84 iommu->pr_irq = 0;
85 }
David Woodhousea222a7f2015-10-07 23:35:18 +010086
87 free_pages((unsigned long)iommu->prq, PRQ_ORDER);
88 iommu->prq = NULL;
89
90 return 0;
91}
92
Jacob Panff3dc652020-01-02 08:18:03 +080093static inline bool intel_svm_capable(struct intel_iommu *iommu)
94{
95 return iommu->flags & VTD_FLAG_SVM_CAPABLE;
96}
97
98void intel_svm_check(struct intel_iommu *iommu)
99{
100 if (!pasid_supported(iommu))
101 return;
102
103 if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
104 !cap_fl1gp_support(iommu->cap)) {
105 pr_err("%s SVM disabled, incompatible 1GB page capability\n",
106 iommu->name);
107 return;
108 }
109
110 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
111 !cap_5lp_support(iommu->cap)) {
112 pr_err("%s SVM disabled, incompatible paging mode\n",
113 iommu->name);
114 return;
115 }
116
117 iommu->flags |= VTD_FLAG_SVM_CAPABLE;
118}
119
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100120static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
Jacob Pan8744daf2019-08-26 08:53:29 -0700121 unsigned long address, unsigned long pages, int ih)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100122{
123 struct qi_desc desc;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100124
Lu Baoluf81b8462019-11-20 14:10:16 +0800125 if (pages == -1) {
Jacob Pan8744daf2019-08-26 08:53:29 -0700126 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
127 QI_EIOTLB_DID(sdev->did) |
128 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
129 QI_EIOTLB_TYPE;
Lu Baolu5d308fc2018-12-10 09:58:58 +0800130 desc.qw1 = 0;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100131 } else {
David Woodhouse5d52f482015-10-20 15:52:13 +0100132 int mask = ilog2(__roundup_pow_of_two(pages));
133
Lu Baolu5d308fc2018-12-10 09:58:58 +0800134 desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
135 QI_EIOTLB_DID(sdev->did) |
136 QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
137 QI_EIOTLB_TYPE;
138 desc.qw1 = QI_EIOTLB_ADDR(address) |
Lu Baolu5d308fc2018-12-10 09:58:58 +0800139 QI_EIOTLB_IH(ih) |
140 QI_EIOTLB_AM(mask);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100141 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800142 desc.qw2 = 0;
143 desc.qw3 = 0;
Lu Baolu8a1d8242020-05-16 14:20:55 +0800144 qi_submit_sync(svm->iommu, &desc, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100145
146 if (sdev->dev_iotlb) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800147 desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
148 QI_DEV_EIOTLB_SID(sdev->sid) |
149 QI_DEV_EIOTLB_QDEP(sdev->qdep) |
150 QI_DEIOTLB_TYPE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100151 if (pages == -1) {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800152 desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
153 QI_DEV_EIOTLB_SIZE;
David Woodhouse5d52f482015-10-20 15:52:13 +0100154 } else if (pages > 1) {
155 /* The least significant zero bit indicates the size. So,
156 * for example, an "address" value of 0x12345f000 will
157 * flush from 0x123440000 to 0x12347ffff (256KiB). */
158 unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
Ingo Molnared7158b2018-02-22 10:54:55 +0100159 unsigned long mask = __rounddown_pow_of_two(address ^ last);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100160
Lu Baolu5d308fc2018-12-10 09:58:58 +0800161 desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
162 (mask - 1)) | QI_DEV_EIOTLB_SIZE;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100163 } else {
Lu Baolu5d308fc2018-12-10 09:58:58 +0800164 desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100165 }
Lu Baolu5d308fc2018-12-10 09:58:58 +0800166 desc.qw2 = 0;
167 desc.qw3 = 0;
Lu Baolu8a1d8242020-05-16 14:20:55 +0800168 qi_submit_sync(svm->iommu, &desc, 1, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100169 }
170}
171
172static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
Jacob Pan8744daf2019-08-26 08:53:29 -0700173 unsigned long pages, int ih)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100174{
175 struct intel_svm_dev *sdev;
176
177 rcu_read_lock();
178 list_for_each_entry_rcu(sdev, &svm->devs, list)
Jacob Pan8744daf2019-08-26 08:53:29 -0700179 intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100180 rcu_read_unlock();
181}
182
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100183/* Pages have been freed at this point */
184static void intel_invalidate_range(struct mmu_notifier *mn,
185 struct mm_struct *mm,
186 unsigned long start, unsigned long end)
187{
188 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
189
190 intel_flush_svm_range(svm, start,
Jacob Pan8744daf2019-08-26 08:53:29 -0700191 (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100192}
193
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100194static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
195{
196 struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
David Woodhousee57e58b2016-01-12 19:18:06 +0000197 struct intel_svm_dev *sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100198
David Woodhousee57e58b2016-01-12 19:18:06 +0000199 /* This might end up being called from exit_mmap(), *before* the page
200 * tables are cleared. And __mmu_notifier_release() will delete us from
201 * the list of notifiers so that our invalidate_range() callback doesn't
202 * get called when the page tables are cleared. So we need to protect
203 * against hardware accessing those page tables.
204 *
205 * We do it by clearing the entry in the PASID table and then flushing
206 * the IOTLB and the PASID table caches. This might upset hardware;
207 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
208 * page) so that we end up taking a fault that the hardware really
209 * *has* to handle gracefully without affecting other processes.
210 */
David Woodhousee57e58b2016-01-12 19:18:06 +0000211 rcu_read_lock();
Lu Baolu81ebd912020-05-16 14:20:59 +0800212 list_for_each_entry_rcu(sdev, &svm->devs, list)
Lu Baolu37e91bd2020-05-16 14:20:57 +0800213 intel_pasid_tear_down_entry(svm->iommu, sdev->dev,
214 svm->pasid, true);
David Woodhousee57e58b2016-01-12 19:18:06 +0000215 rcu_read_unlock();
216
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100217}
218
219static const struct mmu_notifier_ops intel_mmuops = {
220 .release = intel_mm_release,
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100221 .invalidate_range = intel_invalidate_range,
222};
223
224static DEFINE_MUTEX(pasid_mutex);
Lu Baolu51261aa2018-07-14 15:46:55 +0800225static LIST_HEAD(global_svm_list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100226
Jacob Pan034d4732020-01-02 08:18:10 +0800227#define for_each_svm_dev(sdev, svm, d) \
228 list_for_each_entry((sdev), &(svm)->devs, list) \
229 if ((d) != (sdev)->dev) {} else
230
Jacob Pan56722a42020-05-16 14:20:47 +0800231int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
232 struct iommu_gpasid_bind_data *data)
233{
234 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
235 struct dmar_domain *dmar_domain;
236 struct intel_svm_dev *sdev;
237 struct intel_svm *svm;
238 int ret = 0;
239
240 if (WARN_ON(!iommu) || !data)
241 return -EINVAL;
242
243 if (data->version != IOMMU_GPASID_BIND_VERSION_1 ||
244 data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
245 return -EINVAL;
246
247 if (!dev_is_pci(dev))
248 return -ENOTSUPP;
249
250 /* VT-d supports devices with full 20 bit PASIDs only */
251 if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
252 return -EINVAL;
253
254 /*
255 * We only check host PASID range, we have no knowledge to check
256 * guest PASID range.
257 */
258 if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
259 return -EINVAL;
260
261 dmar_domain = to_dmar_domain(domain);
262
263 mutex_lock(&pasid_mutex);
264 svm = ioasid_find(NULL, data->hpasid, NULL);
265 if (IS_ERR(svm)) {
266 ret = PTR_ERR(svm);
267 goto out;
268 }
269
270 if (svm) {
271 /*
272 * If we found svm for the PASID, there must be at
273 * least one device bond, otherwise svm should be freed.
274 */
275 if (WARN_ON(list_empty(&svm->devs))) {
276 ret = -EINVAL;
277 goto out;
278 }
279
280 for_each_svm_dev(sdev, svm, dev) {
281 /*
282 * For devices with aux domains, we should allow
283 * multiple bind calls with the same PASID and pdev.
284 */
285 if (iommu_dev_feature_enabled(dev,
286 IOMMU_DEV_FEAT_AUX)) {
287 sdev->users++;
288 } else {
289 dev_warn_ratelimited(dev,
290 "Already bound with PASID %u\n",
291 svm->pasid);
292 ret = -EBUSY;
293 }
294 goto out;
295 }
296 } else {
297 /* We come here when PASID has never been bond to a device. */
298 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
299 if (!svm) {
300 ret = -ENOMEM;
301 goto out;
302 }
303 /* REVISIT: upper layer/VFIO can track host process that bind
304 * the PASID. ioasid_set = mm might be sufficient for vfio to
305 * check pasid VMM ownership. We can drop the following line
306 * once VFIO and IOASID set check is in place.
307 */
308 svm->mm = get_task_mm(current);
309 svm->pasid = data->hpasid;
310 if (data->flags & IOMMU_SVA_GPASID_VAL) {
311 svm->gpasid = data->gpasid;
312 svm->flags |= SVM_FLAG_GUEST_PASID;
313 }
314 ioasid_set_data(data->hpasid, svm);
315 INIT_LIST_HEAD_RCU(&svm->devs);
316 mmput(svm->mm);
317 }
318 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
319 if (!sdev) {
320 ret = -ENOMEM;
321 goto out;
322 }
323 sdev->dev = dev;
324
325 /* Only count users if device has aux domains */
326 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
327 sdev->users = 1;
328
329 /* Set up device context entry for PASID if not enabled already */
330 ret = intel_iommu_enable_pasid(iommu, sdev->dev);
331 if (ret) {
332 dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
333 kfree(sdev);
334 goto out;
335 }
336
337 /*
338 * PASID table is per device for better security. Therefore, for
339 * each bind of a new device even with an existing PASID, we need to
340 * call the nested mode setup function here.
341 */
342 spin_lock(&iommu->lock);
Lu Baolubfe62402020-05-19 09:34:23 +0800343 ret = intel_pasid_setup_nested(iommu, dev,
344 (pgd_t *)(uintptr_t)data->gpgd,
Jacob Pan56722a42020-05-16 14:20:47 +0800345 data->hpasid, &data->vtd, dmar_domain,
346 data->addr_width);
347 spin_unlock(&iommu->lock);
348 if (ret) {
349 dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
350 data->hpasid, ret);
351 /*
352 * PASID entry should be in cleared state if nested mode
353 * set up failed. So we only need to clear IOASID tracking
354 * data such that free call will succeed.
355 */
356 kfree(sdev);
357 goto out;
358 }
359
360 svm->flags |= SVM_FLAG_GUEST_MODE;
361
362 init_rcu_head(&sdev->rcu);
363 list_add_rcu(&sdev->list, &svm->devs);
364 out:
365 if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
366 ioasid_set_data(data->hpasid, NULL);
367 kfree(svm);
368 }
369
370 mutex_unlock(&pasid_mutex);
371 return ret;
372}
373
374int intel_svm_unbind_gpasid(struct device *dev, int pasid)
375{
376 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
377 struct intel_svm_dev *sdev;
378 struct intel_svm *svm;
379 int ret = -EINVAL;
380
381 if (WARN_ON(!iommu))
382 return -EINVAL;
383
384 mutex_lock(&pasid_mutex);
385 svm = ioasid_find(NULL, pasid, NULL);
386 if (!svm) {
387 ret = -EINVAL;
388 goto out;
389 }
390
391 if (IS_ERR(svm)) {
392 ret = PTR_ERR(svm);
393 goto out;
394 }
395
396 for_each_svm_dev(sdev, svm, dev) {
397 ret = 0;
398 if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
399 sdev->users--;
400 if (!sdev->users) {
401 list_del_rcu(&sdev->list);
Lu Baolu37e91bd2020-05-16 14:20:57 +0800402 intel_pasid_tear_down_entry(iommu, dev,
403 svm->pasid, false);
Lu Baolu66ac4db2020-05-16 14:20:58 +0800404 intel_svm_drain_prq(dev, svm->pasid);
Jacob Pan56722a42020-05-16 14:20:47 +0800405 kfree_rcu(sdev, rcu);
406
407 if (list_empty(&svm->devs)) {
408 /*
409 * We do not free the IOASID here in that
410 * IOMMU driver did not allocate it.
411 * Unlike native SVM, IOASID for guest use was
412 * allocated prior to the bind call.
413 * In any case, if the free call comes before
414 * the unbind, IOMMU driver will get notified
415 * and perform cleanup.
416 */
417 ioasid_set_data(pasid, NULL);
418 kfree(svm);
419 }
420 }
421 break;
422 }
423out:
424 mutex_unlock(&pasid_mutex);
425 return ret;
426}
427
Jacob Pan064a57d2020-05-16 14:20:54 +0800428/* Caller must hold pasid_mutex, mm reference */
429static int
430intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
431 struct mm_struct *mm, struct intel_svm_dev **sd)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100432{
433 struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800434 struct device_domain_info *info;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100435 struct intel_svm_dev *sdev;
436 struct intel_svm *svm = NULL;
437 int pasid_max;
438 int ret;
439
Lu Baoluc56cba52019-03-01 11:23:12 +0800440 if (!iommu || dmar_disabled)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100441 return -EINVAL;
442
Jacob Pan6eba09a2020-01-02 08:18:05 +0800443 if (!intel_svm_capable(iommu))
444 return -ENOTSUPP;
445
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100446 if (dev_is_pci(dev)) {
447 pasid_max = pci_max_pasids(to_pci_dev(dev));
448 if (pasid_max < 0)
449 return -EINVAL;
450 } else
451 pasid_max = 1 << 20;
452
Jacob Pan064a57d2020-05-16 14:20:54 +0800453 /* Bind supervisor PASID shuld have mm = NULL */
Lu Baolubb37f7d2018-05-04 13:08:19 +0800454 if (flags & SVM_FLAG_SUPERVISOR_MODE) {
Jacob Pan064a57d2020-05-16 14:20:54 +0800455 if (!ecap_srs(iommu->ecap) || mm) {
456 pr_err("Supervisor PASID with user provided mm.\n");
David Woodhouse5cec7532015-10-15 15:52:15 +0100457 return -EINVAL;
Jacob Pan064a57d2020-05-16 14:20:54 +0800458 }
David Woodhouse5cec7532015-10-15 15:52:15 +0100459 }
460
Jacob Pan064a57d2020-05-16 14:20:54 +0800461 if (!(flags & SVM_FLAG_PRIVATE_PASID)) {
Lu Baolu51261aa2018-07-14 15:46:55 +0800462 struct intel_svm *t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100463
Lu Baolu51261aa2018-07-14 15:46:55 +0800464 list_for_each_entry(t, &global_svm_list, list) {
465 if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100466 continue;
467
Lu Baolu51261aa2018-07-14 15:46:55 +0800468 svm = t;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100469 if (svm->pasid >= pasid_max) {
470 dev_warn(dev,
471 "Limited PASID width. Cannot use existing PASID %d\n",
472 svm->pasid);
473 ret = -ENOSPC;
474 goto out;
475 }
476
Jacob Pan034d4732020-01-02 08:18:10 +0800477 /* Find the matching device in svm list */
478 for_each_svm_dev(sdev, svm, dev) {
479 if (sdev->ops != ops) {
480 ret = -EBUSY;
481 goto out;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100482 }
Jacob Pan034d4732020-01-02 08:18:10 +0800483 sdev->users++;
484 goto success;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100485 }
486
487 break;
488 }
489 }
490
491 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
492 if (!sdev) {
493 ret = -ENOMEM;
494 goto out;
495 }
496 sdev->dev = dev;
497
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800498 ret = intel_iommu_enable_pasid(iommu, dev);
Jacob Pan064a57d2020-05-16 14:20:54 +0800499 if (ret) {
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100500 kfree(sdev);
501 goto out;
502 }
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800503
Lu Baolue85bb992020-05-16 14:20:52 +0800504 info = get_domain_info(dev);
Lu Baolud7cbc0f2019-03-25 09:30:29 +0800505 sdev->did = FLPT_DEFAULT_DID;
506 sdev->sid = PCI_DEVID(info->bus, info->devfn);
507 if (info->ats_enabled) {
508 sdev->dev_iotlb = 1;
509 sdev->qdep = info->ats_qdep;
510 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
511 sdev->qdep = 0;
512 }
513
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100514 /* Finish the setup now we know we're keeping it */
515 sdev->users = 1;
David Woodhouse0204a492015-10-13 17:18:10 +0100516 sdev->ops = ops;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100517 init_rcu_head(&sdev->rcu);
518
519 if (!svm) {
520 svm = kzalloc(sizeof(*svm), GFP_KERNEL);
521 if (!svm) {
522 ret = -ENOMEM;
523 kfree(sdev);
524 goto out;
525 }
526 svm->iommu = iommu;
527
Lu Baolu4774cc52018-07-14 15:47:01 +0800528 if (pasid_max > intel_pasid_max_id)
529 pasid_max = intel_pasid_max_id;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100530
Jacob Pan59a62332020-01-02 08:18:08 +0800531 /* Do not use PASID 0, reserved for RID to PASID */
532 svm->pasid = ioasid_alloc(NULL, PASID_MIN,
533 pasid_max - 1, svm);
534 if (svm->pasid == INVALID_IOASID) {
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100535 kfree(svm);
Lu Baolubbe4b3a2018-02-24 13:42:27 +0800536 kfree(sdev);
Jacob Pan59a62332020-01-02 08:18:08 +0800537 ret = -ENOSPC;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100538 goto out;
539 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100540 svm->notifier.ops = &intel_mmuops;
David Woodhouse5cec7532015-10-15 15:52:15 +0100541 svm->mm = mm;
David Woodhouse569e4f72015-10-15 13:59:14 +0100542 svm->flags = flags;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100543 INIT_LIST_HEAD_RCU(&svm->devs);
Lu Baolu51261aa2018-07-14 15:46:55 +0800544 INIT_LIST_HEAD(&svm->list);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100545 ret = -ENOMEM;
David Woodhouse5cec7532015-10-15 15:52:15 +0100546 if (mm) {
547 ret = mmu_notifier_register(&svm->notifier, mm);
548 if (ret) {
Jacob Pan59a62332020-01-02 08:18:08 +0800549 ioasid_free(svm->pasid);
David Woodhouse5cec7532015-10-15 15:52:15 +0100550 kfree(svm);
551 kfree(sdev);
552 goto out;
553 }
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800554 }
Sohil Mehta2f13eb72017-12-20 11:59:27 -0800555
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800556 spin_lock(&iommu->lock);
557 ret = intel_pasid_setup_first_level(iommu, dev,
558 mm ? mm->pgd : init_mm.pgd,
559 svm->pasid, FLPT_DEFAULT_DID,
Lu Baolu87208f22020-01-02 08:18:16 +0800560 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
561 (cpu_feature_enabled(X86_FEATURE_LA57) ?
562 PASID_FLAG_FL5LP : 0));
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800563 spin_unlock(&iommu->lock);
564 if (ret) {
565 if (mm)
566 mmu_notifier_unregister(&svm->notifier, mm);
Jacob Pan59a62332020-01-02 08:18:08 +0800567 ioasid_free(svm->pasid);
Lu Baolu1c4f88b2018-12-10 09:59:05 +0800568 kfree(svm);
569 kfree(sdev);
570 goto out;
571 }
Lu Baolu51261aa2018-07-14 15:46:55 +0800572
573 list_add_tail(&svm->list, &global_svm_list);
Jacob Pand7af4d92019-05-08 12:22:46 -0700574 } else {
575 /*
576 * Binding a new device with existing PASID, need to setup
577 * the PASID entry.
578 */
579 spin_lock(&iommu->lock);
580 ret = intel_pasid_setup_first_level(iommu, dev,
581 mm ? mm->pgd : init_mm.pgd,
582 svm->pasid, FLPT_DEFAULT_DID,
Lu Baolu87208f22020-01-02 08:18:16 +0800583 (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
584 (cpu_feature_enabled(X86_FEATURE_LA57) ?
585 PASID_FLAG_FL5LP : 0));
Jacob Pand7af4d92019-05-08 12:22:46 -0700586 spin_unlock(&iommu->lock);
587 if (ret) {
588 kfree(sdev);
589 goto out;
590 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100591 }
592 list_add_rcu(&sdev->list, &svm->devs);
Jacob Pan064a57d2020-05-16 14:20:54 +0800593success:
594 sdev->pasid = svm->pasid;
595 sdev->sva.dev = dev;
596 if (sd)
597 *sd = sdev;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100598 ret = 0;
599 out:
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100600 return ret;
601}
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100602
Jacob Pan064a57d2020-05-16 14:20:54 +0800603/* Caller must hold pasid_mutex */
Jacob Pan71974cf2020-05-28 11:03:51 -0700604static int intel_svm_unbind_mm(struct device *dev, int pasid)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100605{
606 struct intel_svm_dev *sdev;
607 struct intel_iommu *iommu;
608 struct intel_svm *svm;
609 int ret = -EINVAL;
610
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100611 iommu = intel_svm_device_to_iommu(dev);
Lu Baolu4774cc52018-07-14 15:47:01 +0800612 if (!iommu)
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100613 goto out;
614
Jacob Pan59a62332020-01-02 08:18:08 +0800615 svm = ioasid_find(NULL, pasid, NULL);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100616 if (!svm)
617 goto out;
618
Jacob Pan59a62332020-01-02 08:18:08 +0800619 if (IS_ERR(svm)) {
620 ret = PTR_ERR(svm);
621 goto out;
622 }
623
Jacob Pan034d4732020-01-02 08:18:10 +0800624 for_each_svm_dev(sdev, svm, dev) {
625 ret = 0;
626 sdev->users--;
627 if (!sdev->users) {
628 list_del_rcu(&sdev->list);
629 /* Flush the PASID cache and IOTLB for this device.
630 * Note that we do depend on the hardware *not* using
631 * the PASID any more. Just as we depend on other
632 * devices never using PASIDs that they have no right
633 * to use. We have a *shared* PASID table, because it's
634 * large and has to be physically contiguous. So it's
635 * hard to be as defensive as we might like. */
Lu Baolu37e91bd2020-05-16 14:20:57 +0800636 intel_pasid_tear_down_entry(iommu, dev,
637 svm->pasid, false);
Lu Baolu66ac4db2020-05-16 14:20:58 +0800638 intel_svm_drain_prq(dev, svm->pasid);
Jacob Pan034d4732020-01-02 08:18:10 +0800639 kfree_rcu(sdev, rcu);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100640
Jacob Pan034d4732020-01-02 08:18:10 +0800641 if (list_empty(&svm->devs)) {
642 ioasid_free(svm->pasid);
643 if (svm->mm)
644 mmu_notifier_unregister(&svm->notifier, svm->mm);
645 list_del(&svm->list);
646 /* We mandate that no page faults may be outstanding
647 * for the PASID when intel_svm_unbind_mm() is called.
648 * If that is not obeyed, subtle errors will happen.
649 * Let's make them less subtle... */
650 memset(svm, 0x6b, sizeof(*svm));
651 kfree(svm);
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100652 }
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100653 }
Jacob Pan034d4732020-01-02 08:18:10 +0800654 break;
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100655 }
656 out:
David Woodhouse2f26e0a2015-09-09 11:40:47 +0100657
658 return ret;
659}
CQ Tang15060ab2017-05-10 11:39:03 -0700660
David Woodhousea222a7f2015-10-07 23:35:18 +0100661/* Page request queue descriptor */
662struct page_req_dsc {
Jacob Pan5b438f42019-01-11 13:04:57 +0800663 union {
664 struct {
665 u64 type:8;
666 u64 pasid_present:1;
667 u64 priv_data_present:1;
668 u64 rsvd:6;
669 u64 rid:16;
670 u64 pasid:20;
671 u64 exe_req:1;
672 u64 pm_req:1;
673 u64 rsvd2:10;
674 };
675 u64 qw_0;
676 };
677 union {
678 struct {
679 u64 rd_req:1;
680 u64 wr_req:1;
681 u64 lpig:1;
682 u64 prg_index:9;
683 u64 addr:52;
684 };
685 u64 qw_1;
686 };
687 u64 priv_data[2];
David Woodhousea222a7f2015-10-07 23:35:18 +0100688};
689
Jacob Pan52355fb2020-03-17 09:10:18 +0800690#define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20)
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100691
692static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
693{
694 unsigned long requested = 0;
695
696 if (req->exe_req)
697 requested |= VM_EXEC;
698
699 if (req->rd_req)
700 requested |= VM_READ;
701
702 if (req->wr_req)
703 requested |= VM_WRITE;
704
705 return (requested & ~vma->vm_flags) != 0;
706}
707
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700708static bool is_canonical_address(u64 addr)
709{
710 int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
711 long saddr = (long) addr;
712
713 return (((saddr << shift) >> shift) == saddr);
714}
715
Lu Baolu66ac4db2020-05-16 14:20:58 +0800716/**
717 * intel_svm_drain_prq - Drain page requests and responses for a pasid
718 * @dev: target device
719 * @pasid: pasid for draining
720 *
721 * Drain all pending page requests and responses related to @pasid in both
722 * software and hardware. This is supposed to be called after the device
723 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
724 * and DevTLB have been invalidated.
725 *
726 * It waits until all pending page requests for @pasid in the page fault
727 * queue are completed by the prq handling thread. Then follow the steps
728 * described in VT-d spec CH7.10 to drain all page requests and page
729 * responses pending in the hardware.
730 */
731static void intel_svm_drain_prq(struct device *dev, int pasid)
732{
733 struct device_domain_info *info;
734 struct dmar_domain *domain;
735 struct intel_iommu *iommu;
736 struct qi_desc desc[3];
737 struct pci_dev *pdev;
738 int head, tail;
739 u16 sid, did;
740 int qdep;
741
742 info = get_domain_info(dev);
743 if (WARN_ON(!info || !dev_is_pci(dev)))
744 return;
745
746 if (!info->pri_enabled)
747 return;
748
749 iommu = info->iommu;
750 domain = info->domain;
751 pdev = to_pci_dev(dev);
752 sid = PCI_DEVID(info->bus, info->devfn);
753 did = domain->iommu_did[iommu->seq_id];
754 qdep = pci_ats_queue_depth(pdev);
755
756 /*
757 * Check and wait until all pending page requests in the queue are
758 * handled by the prq handling thread.
759 */
760prq_retry:
761 reinit_completion(&iommu->prq_complete);
762 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
763 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
764 while (head != tail) {
765 struct page_req_dsc *req;
766
767 req = &iommu->prq[head / sizeof(*req)];
768 if (!req->pasid_present || req->pasid != pasid) {
769 head = (head + sizeof(*req)) & PRQ_RING_MASK;
770 continue;
771 }
772
773 wait_for_completion(&iommu->prq_complete);
774 goto prq_retry;
775 }
776
777 /*
778 * Perform steps described in VT-d spec CH7.10 to drain page
779 * requests and responses in hardware.
780 */
781 memset(desc, 0, sizeof(desc));
782 desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
783 QI_IWD_FENCE |
784 QI_IWD_TYPE;
785 desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
786 QI_EIOTLB_DID(did) |
787 QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
788 QI_EIOTLB_TYPE;
789 desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
790 QI_DEV_EIOTLB_SID(sid) |
791 QI_DEV_EIOTLB_QDEP(qdep) |
792 QI_DEIOTLB_TYPE |
793 QI_DEV_IOTLB_PFSID(info->pfsid);
794qi_retry:
795 reinit_completion(&iommu->prq_complete);
796 qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
797 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
798 wait_for_completion(&iommu->prq_complete);
799 goto qi_retry;
800 }
801}
802
David Woodhousea222a7f2015-10-07 23:35:18 +0100803static irqreturn_t prq_event_thread(int irq, void *d)
804{
805 struct intel_iommu *iommu = d;
806 struct intel_svm *svm = NULL;
807 int head, tail, handled = 0;
808
David Woodhouse46924002016-02-15 12:42:38 +0000809 /* Clear PPR bit before reading head/tail registers, to
810 * ensure that we get a new interrupt if needed. */
811 writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
812
David Woodhousea222a7f2015-10-07 23:35:18 +0100813 tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
814 head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
815 while (head != tail) {
David Woodhouse0204a492015-10-13 17:18:10 +0100816 struct intel_svm_dev *sdev;
David Woodhousea222a7f2015-10-07 23:35:18 +0100817 struct vm_area_struct *vma;
818 struct page_req_dsc *req;
819 struct qi_desc resp;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700820 int result;
821 vm_fault_t ret;
David Woodhousea222a7f2015-10-07 23:35:18 +0100822 u64 address;
823
824 handled = 1;
825
826 req = &iommu->prq[head / sizeof(*req)];
827
828 result = QI_RESP_FAILURE;
David Woodhouse7f92a2e2015-10-16 17:22:31 +0100829 address = (u64)req->addr << VTD_PAGE_SHIFT;
David Woodhousea222a7f2015-10-07 23:35:18 +0100830 if (!req->pasid_present) {
831 pr_err("%s: Page request without PASID: %08llx %08llx\n",
832 iommu->name, ((unsigned long long *)req)[0],
833 ((unsigned long long *)req)[1]);
Lu Baolu19ed3e22018-11-05 10:18:58 +0800834 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100835 }
836
837 if (!svm || svm->pasid != req->pasid) {
838 rcu_read_lock();
Jacob Pan59a62332020-01-02 08:18:08 +0800839 svm = ioasid_find(NULL, req->pasid, NULL);
David Woodhousea222a7f2015-10-07 23:35:18 +0100840 /* It *can't* go away, because the driver is not permitted
841 * to unbind the mm while any page faults are outstanding.
842 * So we only need RCU to protect the internal idr code. */
843 rcu_read_unlock();
Jacob Pan59a62332020-01-02 08:18:08 +0800844 if (IS_ERR_OR_NULL(svm)) {
David Woodhousea222a7f2015-10-07 23:35:18 +0100845 pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
846 iommu->name, req->pasid, ((unsigned long long *)req)[0],
847 ((unsigned long long *)req)[1]);
David Woodhouse26322ab2015-10-15 21:12:56 +0100848 goto no_pasid;
David Woodhousea222a7f2015-10-07 23:35:18 +0100849 }
850 }
851
852 result = QI_RESP_INVALID;
David Woodhouse5cec7532015-10-15 15:52:15 +0100853 /* Since we're using init_mm.pgd directly, we should never take
854 * any faults on kernel addresses. */
855 if (!svm->mm)
856 goto bad_req;
Ashok Raj9d8c3af2017-08-08 13:29:27 -0700857
858 /* If address is not canonical, return invalid response */
859 if (!is_canonical_address(address))
860 goto bad_req;
861
Jacob Pan902baf62020-03-19 21:32:30 -0700862 /* If the mm is already defunct, don't handle faults. */
863 if (!mmget_not_zero(svm->mm))
864 goto bad_req;
865
David Woodhousea222a7f2015-10-07 23:35:18 +0100866 down_read(&svm->mm->mmap_sem);
867 vma = find_extend_vma(svm->mm, address);
868 if (!vma || address < vma->vm_start)
869 goto invalid;
870
Joerg Roedel7f8312a2015-11-17 16:11:39 +0100871 if (access_error(vma, req))
872 goto invalid;
873
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700874 ret = handle_mm_fault(vma, address,
David Woodhousea222a7f2015-10-07 23:35:18 +0100875 req->wr_req ? FAULT_FLAG_WRITE : 0);
876 if (ret & VM_FAULT_ERROR)
877 goto invalid;
878
879 result = QI_RESP_SUCCESS;
880 invalid:
881 up_read(&svm->mm->mmap_sem);
David Woodhousee57e58b2016-01-12 19:18:06 +0000882 mmput(svm->mm);
David Woodhousea222a7f2015-10-07 23:35:18 +0100883 bad_req:
884 /* Accounting for major/minor faults? */
David Woodhouse0204a492015-10-13 17:18:10 +0100885 rcu_read_lock();
886 list_for_each_entry_rcu(sdev, &svm->devs, list) {
Jacob Pan5b438f42019-01-11 13:04:57 +0800887 if (sdev->sid == req->rid)
David Woodhouse0204a492015-10-13 17:18:10 +0100888 break;
889 }
890 /* Other devices can go away, but the drivers are not permitted
891 * to unbind while any page faults might be in flight. So it's
892 * OK to drop the 'lock' here now we have it. */
893 rcu_read_unlock();
894
895 if (WARN_ON(&sdev->list == &svm->devs))
896 sdev = NULL;
897
898 if (sdev && sdev->ops && sdev->ops->fault_cb) {
899 int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800900 (req->exe_req << 1) | (req->pm_req);
901 sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
902 req->priv_data, rwxp, result);
David Woodhouse0204a492015-10-13 17:18:10 +0100903 }
David Woodhouse26322ab2015-10-15 21:12:56 +0100904 /* We get here in the error case where the PASID lookup failed,
905 and these can be NULL. Do not use them below this point! */
906 sdev = NULL;
907 svm = NULL;
908 no_pasid:
Jacob Pan5b438f42019-01-11 13:04:57 +0800909 if (req->lpig || req->priv_data_present) {
910 /*
911 * Per VT-d spec. v3.0 ch7.7, system software must
912 * respond with page group response if private data
913 * is present (PDP) or last page in group (LPIG) bit
914 * is set. This is an additional VT-d feature beyond
915 * PCI ATS spec.
916 */
Lu Baolu5d308fc2018-12-10 09:58:58 +0800917 resp.qw0 = QI_PGRP_PASID(req->pasid) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800918 QI_PGRP_DID(req->rid) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100919 QI_PGRP_PASID_P(req->pasid_present) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800920 QI_PGRP_PDP(req->pasid_present) |
921 QI_PGRP_RESP_CODE(result) |
David Woodhousea222a7f2015-10-07 23:35:18 +0100922 QI_PGRP_RESP_TYPE;
Lu Baolu5d308fc2018-12-10 09:58:58 +0800923 resp.qw1 = QI_PGRP_IDX(req->prg_index) |
Jacob Pan5b438f42019-01-11 13:04:57 +0800924 QI_PGRP_LPIG(req->lpig);
925
926 if (req->priv_data_present)
927 memcpy(&resp.qw2, req->priv_data,
928 sizeof(req->priv_data));
Jacob Pan5f755852020-01-02 08:18:09 +0800929 resp.qw2 = 0;
930 resp.qw3 = 0;
Lu Baolu8a1d8242020-05-16 14:20:55 +0800931 qi_submit_sync(iommu, &resp, 1, 0);
David Woodhousea222a7f2015-10-07 23:35:18 +0100932 }
David Woodhousea222a7f2015-10-07 23:35:18 +0100933 head = (head + sizeof(*req)) & PRQ_RING_MASK;
934 }
935
936 dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
937
Lu Baolu66ac4db2020-05-16 14:20:58 +0800938 /*
939 * Clear the page request overflow bit and wake up all threads that
940 * are waiting for the completion of this handling.
941 */
942 if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO)
943 writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
944
945 if (!completion_done(&iommu->prq_complete))
946 complete(&iommu->prq_complete);
947
David Woodhousea222a7f2015-10-07 23:35:18 +0100948 return IRQ_RETVAL(handled);
949}
Jacob Pan064a57d2020-05-16 14:20:54 +0800950
951#define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
952struct iommu_sva *
953intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
954{
955 struct iommu_sva *sva = ERR_PTR(-EINVAL);
956 struct intel_svm_dev *sdev = NULL;
957 int flags = 0;
958 int ret;
959
960 /*
961 * TODO: Consolidate with generic iommu-sva bind after it is merged.
962 * It will require shared SVM data structures, i.e. combine io_mm
963 * and intel_svm etc.
964 */
965 if (drvdata)
966 flags = *(int *)drvdata;
967 mutex_lock(&pasid_mutex);
968 ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev);
969 if (ret)
970 sva = ERR_PTR(ret);
971 else if (sdev)
972 sva = &sdev->sva;
973 else
974 WARN(!sdev, "SVM bind succeeded with no sdev!\n");
975
976 mutex_unlock(&pasid_mutex);
977
978 return sva;
979}
980
981void intel_svm_unbind(struct iommu_sva *sva)
982{
983 struct intel_svm_dev *sdev;
984
985 mutex_lock(&pasid_mutex);
986 sdev = to_intel_svm_dev(sva);
987 intel_svm_unbind_mm(sdev->dev, sdev->pasid);
988 mutex_unlock(&pasid_mutex);
989}
990
991int intel_svm_get_pasid(struct iommu_sva *sva)
992{
993 struct intel_svm_dev *sdev;
994 int pasid;
995
996 mutex_lock(&pasid_mutex);
997 sdev = to_intel_svm_dev(sva);
998 pasid = sdev->pasid;
999 mutex_unlock(&pasid_mutex);
1000
1001 return pasid;
1002}