xref: /linux/drivers/iommu/intel/svm.c (revision ae7f09b14b4ff18f65790a906d7c2fe2561568b5)
1672cf6dfSJoerg Roedel // SPDX-License-Identifier: GPL-2.0-only
2672cf6dfSJoerg Roedel /*
3672cf6dfSJoerg Roedel  * Copyright © 2015 Intel Corporation.
4672cf6dfSJoerg Roedel  *
5672cf6dfSJoerg Roedel  * Authors: David Woodhouse <dwmw2@infradead.org>
6672cf6dfSJoerg Roedel  */
7672cf6dfSJoerg Roedel 
8672cf6dfSJoerg Roedel #include <linux/intel-iommu.h>
9672cf6dfSJoerg Roedel #include <linux/mmu_notifier.h>
10672cf6dfSJoerg Roedel #include <linux/sched.h>
11672cf6dfSJoerg Roedel #include <linux/sched/mm.h>
12672cf6dfSJoerg Roedel #include <linux/slab.h>
13672cf6dfSJoerg Roedel #include <linux/intel-svm.h>
14672cf6dfSJoerg Roedel #include <linux/rculist.h>
15672cf6dfSJoerg Roedel #include <linux/pci.h>
16672cf6dfSJoerg Roedel #include <linux/pci-ats.h>
17672cf6dfSJoerg Roedel #include <linux/dmar.h>
18672cf6dfSJoerg Roedel #include <linux/interrupt.h>
19672cf6dfSJoerg Roedel #include <linux/mm_types.h>
20100b8a14SLu Baolu #include <linux/xarray.h>
21672cf6dfSJoerg Roedel #include <linux/ioasid.h>
22672cf6dfSJoerg Roedel #include <asm/page.h>
2320f0afd1SFenghua Yu #include <asm/fpu/api.h>
24672cf6dfSJoerg Roedel 
2502f3effdSLu Baolu #include "pasid.h"
2640483774SLu Baolu #include "../iommu-sva-lib.h"
27672cf6dfSJoerg Roedel 
28672cf6dfSJoerg Roedel static irqreturn_t prq_event_thread(int irq, void *d);
29c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid);
3040483774SLu Baolu #define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
31672cf6dfSJoerg Roedel 
32672cf6dfSJoerg Roedel #define PRQ_ORDER 0
33672cf6dfSJoerg Roedel 
34100b8a14SLu Baolu static DEFINE_XARRAY_ALLOC(pasid_private_array);
35100b8a14SLu Baolu static int pasid_private_add(ioasid_t pasid, void *priv)
36100b8a14SLu Baolu {
37100b8a14SLu Baolu 	return xa_alloc(&pasid_private_array, &pasid, priv,
38100b8a14SLu Baolu 			XA_LIMIT(pasid, pasid), GFP_ATOMIC);
39100b8a14SLu Baolu }
40100b8a14SLu Baolu 
41100b8a14SLu Baolu static void pasid_private_remove(ioasid_t pasid)
42100b8a14SLu Baolu {
43100b8a14SLu Baolu 	xa_erase(&pasid_private_array, pasid);
44100b8a14SLu Baolu }
45100b8a14SLu Baolu 
46100b8a14SLu Baolu static void *pasid_private_find(ioasid_t pasid)
47100b8a14SLu Baolu {
48100b8a14SLu Baolu 	return xa_load(&pasid_private_array, pasid);
49100b8a14SLu Baolu }
50100b8a14SLu Baolu 
519e52cc0fSLu Baolu static struct intel_svm_dev *
529e52cc0fSLu Baolu svm_lookup_device_by_sid(struct intel_svm *svm, u16 sid)
539e52cc0fSLu Baolu {
549e52cc0fSLu Baolu 	struct intel_svm_dev *sdev = NULL, *t;
559e52cc0fSLu Baolu 
569e52cc0fSLu Baolu 	rcu_read_lock();
579e52cc0fSLu Baolu 	list_for_each_entry_rcu(t, &svm->devs, list) {
589e52cc0fSLu Baolu 		if (t->sid == sid) {
599e52cc0fSLu Baolu 			sdev = t;
609e52cc0fSLu Baolu 			break;
619e52cc0fSLu Baolu 		}
629e52cc0fSLu Baolu 	}
639e52cc0fSLu Baolu 	rcu_read_unlock();
649e52cc0fSLu Baolu 
659e52cc0fSLu Baolu 	return sdev;
669e52cc0fSLu Baolu }
679e52cc0fSLu Baolu 
689e52cc0fSLu Baolu static struct intel_svm_dev *
699e52cc0fSLu Baolu svm_lookup_device_by_dev(struct intel_svm *svm, struct device *dev)
709e52cc0fSLu Baolu {
719e52cc0fSLu Baolu 	struct intel_svm_dev *sdev = NULL, *t;
729e52cc0fSLu Baolu 
739e52cc0fSLu Baolu 	rcu_read_lock();
749e52cc0fSLu Baolu 	list_for_each_entry_rcu(t, &svm->devs, list) {
759e52cc0fSLu Baolu 		if (t->dev == dev) {
769e52cc0fSLu Baolu 			sdev = t;
779e52cc0fSLu Baolu 			break;
789e52cc0fSLu Baolu 		}
799e52cc0fSLu Baolu 	}
809e52cc0fSLu Baolu 	rcu_read_unlock();
819e52cc0fSLu Baolu 
829e52cc0fSLu Baolu 	return sdev;
839e52cc0fSLu Baolu }
849e52cc0fSLu Baolu 
85672cf6dfSJoerg Roedel int intel_svm_enable_prq(struct intel_iommu *iommu)
86672cf6dfSJoerg Roedel {
87672cf6dfSJoerg Roedel 	struct page *pages;
88672cf6dfSJoerg Roedel 	int irq, ret;
89672cf6dfSJoerg Roedel 
90672cf6dfSJoerg Roedel 	pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
91672cf6dfSJoerg Roedel 	if (!pages) {
92672cf6dfSJoerg Roedel 		pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
93672cf6dfSJoerg Roedel 			iommu->name);
94672cf6dfSJoerg Roedel 		return -ENOMEM;
95672cf6dfSJoerg Roedel 	}
96672cf6dfSJoerg Roedel 	iommu->prq = page_address(pages);
97672cf6dfSJoerg Roedel 
98672cf6dfSJoerg Roedel 	irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
99672cf6dfSJoerg Roedel 	if (irq <= 0) {
100672cf6dfSJoerg Roedel 		pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
101672cf6dfSJoerg Roedel 		       iommu->name);
102672cf6dfSJoerg Roedel 		ret = -EINVAL;
103672cf6dfSJoerg Roedel 	err:
104672cf6dfSJoerg Roedel 		free_pages((unsigned long)iommu->prq, PRQ_ORDER);
105672cf6dfSJoerg Roedel 		iommu->prq = NULL;
106672cf6dfSJoerg Roedel 		return ret;
107672cf6dfSJoerg Roedel 	}
108672cf6dfSJoerg Roedel 	iommu->pr_irq = irq;
109672cf6dfSJoerg Roedel 
110672cf6dfSJoerg Roedel 	snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
111672cf6dfSJoerg Roedel 
112672cf6dfSJoerg Roedel 	ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
113672cf6dfSJoerg Roedel 				   iommu->prq_name, iommu);
114672cf6dfSJoerg Roedel 	if (ret) {
115672cf6dfSJoerg Roedel 		pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
116672cf6dfSJoerg Roedel 		       iommu->name);
117672cf6dfSJoerg Roedel 		dmar_free_hwirq(irq);
118672cf6dfSJoerg Roedel 		iommu->pr_irq = 0;
119672cf6dfSJoerg Roedel 		goto err;
120672cf6dfSJoerg Roedel 	}
121672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
122672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
123672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
124672cf6dfSJoerg Roedel 
125672cf6dfSJoerg Roedel 	init_completion(&iommu->prq_complete);
126672cf6dfSJoerg Roedel 
127672cf6dfSJoerg Roedel 	return 0;
128672cf6dfSJoerg Roedel }
129672cf6dfSJoerg Roedel 
130672cf6dfSJoerg Roedel int intel_svm_finish_prq(struct intel_iommu *iommu)
131672cf6dfSJoerg Roedel {
132672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
133672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
134672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
135672cf6dfSJoerg Roedel 
136672cf6dfSJoerg Roedel 	if (iommu->pr_irq) {
137672cf6dfSJoerg Roedel 		free_irq(iommu->pr_irq, iommu);
138672cf6dfSJoerg Roedel 		dmar_free_hwirq(iommu->pr_irq);
139672cf6dfSJoerg Roedel 		iommu->pr_irq = 0;
140672cf6dfSJoerg Roedel 	}
141672cf6dfSJoerg Roedel 
142672cf6dfSJoerg Roedel 	free_pages((unsigned long)iommu->prq, PRQ_ORDER);
143672cf6dfSJoerg Roedel 	iommu->prq = NULL;
144672cf6dfSJoerg Roedel 
145672cf6dfSJoerg Roedel 	return 0;
146672cf6dfSJoerg Roedel }
147672cf6dfSJoerg Roedel 
148672cf6dfSJoerg Roedel static inline bool intel_svm_capable(struct intel_iommu *iommu)
149672cf6dfSJoerg Roedel {
150672cf6dfSJoerg Roedel 	return iommu->flags & VTD_FLAG_SVM_CAPABLE;
151672cf6dfSJoerg Roedel }
152672cf6dfSJoerg Roedel 
153672cf6dfSJoerg Roedel void intel_svm_check(struct intel_iommu *iommu)
154672cf6dfSJoerg Roedel {
155672cf6dfSJoerg Roedel 	if (!pasid_supported(iommu))
156672cf6dfSJoerg Roedel 		return;
157672cf6dfSJoerg Roedel 
158672cf6dfSJoerg Roedel 	if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
159672cf6dfSJoerg Roedel 	    !cap_fl1gp_support(iommu->cap)) {
160672cf6dfSJoerg Roedel 		pr_err("%s SVM disabled, incompatible 1GB page capability\n",
161672cf6dfSJoerg Roedel 		       iommu->name);
162672cf6dfSJoerg Roedel 		return;
163672cf6dfSJoerg Roedel 	}
164672cf6dfSJoerg Roedel 
165672cf6dfSJoerg Roedel 	if (cpu_feature_enabled(X86_FEATURE_LA57) &&
166672cf6dfSJoerg Roedel 	    !cap_5lp_support(iommu->cap)) {
167672cf6dfSJoerg Roedel 		pr_err("%s SVM disabled, incompatible paging mode\n",
168672cf6dfSJoerg Roedel 		       iommu->name);
169672cf6dfSJoerg Roedel 		return;
170672cf6dfSJoerg Roedel 	}
171672cf6dfSJoerg Roedel 
172672cf6dfSJoerg Roedel 	iommu->flags |= VTD_FLAG_SVM_CAPABLE;
173672cf6dfSJoerg Roedel }
174672cf6dfSJoerg Roedel 
1752d6ffc63SLu Baolu static void __flush_svm_range_dev(struct intel_svm *svm,
1762d6ffc63SLu Baolu 				  struct intel_svm_dev *sdev,
1772d6ffc63SLu Baolu 				  unsigned long address,
1782d6ffc63SLu Baolu 				  unsigned long pages, int ih)
179672cf6dfSJoerg Roedel {
1809872f9bdSLu Baolu 	struct device_domain_info *info = get_domain_info(sdev->dev);
181672cf6dfSJoerg Roedel 
1829872f9bdSLu Baolu 	if (WARN_ON(!pages))
1839872f9bdSLu Baolu 		return;
184672cf6dfSJoerg Roedel 
1859872f9bdSLu Baolu 	qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, address, pages, ih);
1869872f9bdSLu Baolu 	if (info->ats_enabled)
1879872f9bdSLu Baolu 		qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid,
1889872f9bdSLu Baolu 					 svm->pasid, sdev->qdep, address,
1899872f9bdSLu Baolu 					 order_base_2(pages));
190672cf6dfSJoerg Roedel }
191672cf6dfSJoerg Roedel 
1922d6ffc63SLu Baolu static void intel_flush_svm_range_dev(struct intel_svm *svm,
1932d6ffc63SLu Baolu 				      struct intel_svm_dev *sdev,
1942d6ffc63SLu Baolu 				      unsigned long address,
1952d6ffc63SLu Baolu 				      unsigned long pages, int ih)
1962d6ffc63SLu Baolu {
1972d6ffc63SLu Baolu 	unsigned long shift = ilog2(__roundup_pow_of_two(pages));
1982d6ffc63SLu Baolu 	unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
1992d6ffc63SLu Baolu 	unsigned long start = ALIGN_DOWN(address, align);
2002d6ffc63SLu Baolu 	unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
2012d6ffc63SLu Baolu 
2022d6ffc63SLu Baolu 	while (start < end) {
2032d6ffc63SLu Baolu 		__flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih);
2042d6ffc63SLu Baolu 		start += align;
2052d6ffc63SLu Baolu 	}
2062d6ffc63SLu Baolu }
2072d6ffc63SLu Baolu 
208672cf6dfSJoerg Roedel static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
209672cf6dfSJoerg Roedel 				unsigned long pages, int ih)
210672cf6dfSJoerg Roedel {
211672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
212672cf6dfSJoerg Roedel 
213672cf6dfSJoerg Roedel 	rcu_read_lock();
214672cf6dfSJoerg Roedel 	list_for_each_entry_rcu(sdev, &svm->devs, list)
215672cf6dfSJoerg Roedel 		intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
216672cf6dfSJoerg Roedel 	rcu_read_unlock();
217672cf6dfSJoerg Roedel }
218672cf6dfSJoerg Roedel 
219672cf6dfSJoerg Roedel /* Pages have been freed at this point */
220672cf6dfSJoerg Roedel static void intel_invalidate_range(struct mmu_notifier *mn,
221672cf6dfSJoerg Roedel 				   struct mm_struct *mm,
222672cf6dfSJoerg Roedel 				   unsigned long start, unsigned long end)
223672cf6dfSJoerg Roedel {
224672cf6dfSJoerg Roedel 	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
225672cf6dfSJoerg Roedel 
226672cf6dfSJoerg Roedel 	intel_flush_svm_range(svm, start,
227672cf6dfSJoerg Roedel 			      (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
228672cf6dfSJoerg Roedel }
229672cf6dfSJoerg Roedel 
230672cf6dfSJoerg Roedel static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
231672cf6dfSJoerg Roedel {
232672cf6dfSJoerg Roedel 	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
233672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
234672cf6dfSJoerg Roedel 
235672cf6dfSJoerg Roedel 	/* This might end up being called from exit_mmap(), *before* the page
236672cf6dfSJoerg Roedel 	 * tables are cleared. And __mmu_notifier_release() will delete us from
237672cf6dfSJoerg Roedel 	 * the list of notifiers so that our invalidate_range() callback doesn't
238672cf6dfSJoerg Roedel 	 * get called when the page tables are cleared. So we need to protect
239672cf6dfSJoerg Roedel 	 * against hardware accessing those page tables.
240672cf6dfSJoerg Roedel 	 *
241672cf6dfSJoerg Roedel 	 * We do it by clearing the entry in the PASID table and then flushing
242672cf6dfSJoerg Roedel 	 * the IOTLB and the PASID table caches. This might upset hardware;
243672cf6dfSJoerg Roedel 	 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
244672cf6dfSJoerg Roedel 	 * page) so that we end up taking a fault that the hardware really
245672cf6dfSJoerg Roedel 	 * *has* to handle gracefully without affecting other processes.
246672cf6dfSJoerg Roedel 	 */
247672cf6dfSJoerg Roedel 	rcu_read_lock();
248672cf6dfSJoerg Roedel 	list_for_each_entry_rcu(sdev, &svm->devs, list)
2499ad9f45bSLiu Yi L 		intel_pasid_tear_down_entry(sdev->iommu, sdev->dev,
250672cf6dfSJoerg Roedel 					    svm->pasid, true);
251672cf6dfSJoerg Roedel 	rcu_read_unlock();
252672cf6dfSJoerg Roedel 
253672cf6dfSJoerg Roedel }
254672cf6dfSJoerg Roedel 
255672cf6dfSJoerg Roedel static const struct mmu_notifier_ops intel_mmuops = {
256672cf6dfSJoerg Roedel 	.release = intel_mm_release,
257672cf6dfSJoerg Roedel 	.invalidate_range = intel_invalidate_range,
258672cf6dfSJoerg Roedel };
259672cf6dfSJoerg Roedel 
260672cf6dfSJoerg Roedel static DEFINE_MUTEX(pasid_mutex);
261672cf6dfSJoerg Roedel 
26219abcf70SLu Baolu static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
26319abcf70SLu Baolu 			     struct intel_svm **rsvm,
26419abcf70SLu Baolu 			     struct intel_svm_dev **rsdev)
26519abcf70SLu Baolu {
2669e52cc0fSLu Baolu 	struct intel_svm_dev *sdev = NULL;
26719abcf70SLu Baolu 	struct intel_svm *svm;
26819abcf70SLu Baolu 
26919abcf70SLu Baolu 	/* The caller should hold the pasid_mutex lock */
27019abcf70SLu Baolu 	if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
27119abcf70SLu Baolu 		return -EINVAL;
27219abcf70SLu Baolu 
27319abcf70SLu Baolu 	if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
27419abcf70SLu Baolu 		return -EINVAL;
27519abcf70SLu Baolu 
276100b8a14SLu Baolu 	svm = pasid_private_find(pasid);
27719abcf70SLu Baolu 	if (IS_ERR(svm))
27819abcf70SLu Baolu 		return PTR_ERR(svm);
27919abcf70SLu Baolu 
28019abcf70SLu Baolu 	if (!svm)
28119abcf70SLu Baolu 		goto out;
28219abcf70SLu Baolu 
28319abcf70SLu Baolu 	/*
28419abcf70SLu Baolu 	 * If we found svm for the PASID, there must be at least one device
28519abcf70SLu Baolu 	 * bond.
28619abcf70SLu Baolu 	 */
28719abcf70SLu Baolu 	if (WARN_ON(list_empty(&svm->devs)))
28819abcf70SLu Baolu 		return -EINVAL;
2899e52cc0fSLu Baolu 	sdev = svm_lookup_device_by_dev(svm, dev);
29019abcf70SLu Baolu 
29119abcf70SLu Baolu out:
29219abcf70SLu Baolu 	*rsvm = svm;
29319abcf70SLu Baolu 	*rsdev = sdev;
29419abcf70SLu Baolu 
29519abcf70SLu Baolu 	return 0;
29619abcf70SLu Baolu }
29719abcf70SLu Baolu 
298672cf6dfSJoerg Roedel int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
299672cf6dfSJoerg Roedel 			  struct iommu_gpasid_bind_data *data)
300672cf6dfSJoerg Roedel {
301dd6692f1SLu Baolu 	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
30219abcf70SLu Baolu 	struct intel_svm_dev *sdev = NULL;
303672cf6dfSJoerg Roedel 	struct dmar_domain *dmar_domain;
304eea4e29aSLiu Yi L 	struct device_domain_info *info;
30519abcf70SLu Baolu 	struct intel_svm *svm = NULL;
306420d42f6SLu Baolu 	unsigned long iflags;
307672cf6dfSJoerg Roedel 	int ret = 0;
308672cf6dfSJoerg Roedel 
309672cf6dfSJoerg Roedel 	if (WARN_ON(!iommu) || !data)
310672cf6dfSJoerg Roedel 		return -EINVAL;
311672cf6dfSJoerg Roedel 
3126278eecbSJacob Pan 	if (data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
3136278eecbSJacob Pan 		return -EINVAL;
3146278eecbSJacob Pan 
3156278eecbSJacob Pan 	/* IOMMU core ensures argsz is more than the start of the union */
3166278eecbSJacob Pan 	if (data->argsz < offsetofend(struct iommu_gpasid_bind_data, vendor.vtd))
3176278eecbSJacob Pan 		return -EINVAL;
3186278eecbSJacob Pan 
3196278eecbSJacob Pan 	/* Make sure no undefined flags are used in vendor data */
3206278eecbSJacob Pan 	if (data->vendor.vtd.flags & ~(IOMMU_SVA_VTD_GPASID_LAST - 1))
321672cf6dfSJoerg Roedel 		return -EINVAL;
322672cf6dfSJoerg Roedel 
323672cf6dfSJoerg Roedel 	if (!dev_is_pci(dev))
324672cf6dfSJoerg Roedel 		return -ENOTSUPP;
325672cf6dfSJoerg Roedel 
326672cf6dfSJoerg Roedel 	/* VT-d supports devices with full 20 bit PASIDs only */
327672cf6dfSJoerg Roedel 	if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
328672cf6dfSJoerg Roedel 		return -EINVAL;
329672cf6dfSJoerg Roedel 
330672cf6dfSJoerg Roedel 	/*
331672cf6dfSJoerg Roedel 	 * We only check host PASID range, we have no knowledge to check
332672cf6dfSJoerg Roedel 	 * guest PASID range.
333672cf6dfSJoerg Roedel 	 */
334672cf6dfSJoerg Roedel 	if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
335672cf6dfSJoerg Roedel 		return -EINVAL;
336672cf6dfSJoerg Roedel 
337eea4e29aSLiu Yi L 	info = get_domain_info(dev);
338eea4e29aSLiu Yi L 	if (!info)
339eea4e29aSLiu Yi L 		return -EINVAL;
340eea4e29aSLiu Yi L 
341672cf6dfSJoerg Roedel 	dmar_domain = to_dmar_domain(domain);
342672cf6dfSJoerg Roedel 
343672cf6dfSJoerg Roedel 	mutex_lock(&pasid_mutex);
34419abcf70SLu Baolu 	ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
34519abcf70SLu Baolu 	if (ret)
346672cf6dfSJoerg Roedel 		goto out;
347672cf6dfSJoerg Roedel 
34819abcf70SLu Baolu 	if (sdev) {
349672cf6dfSJoerg Roedel 		/*
350d315e9e6SJacob Pan 		 * Do not allow multiple bindings of the same device-PASID since
351d315e9e6SJacob Pan 		 * there is only one SL page tables per PASID. We may revisit
352d315e9e6SJacob Pan 		 * once sharing PGD across domains are supported.
353672cf6dfSJoerg Roedel 		 */
35419abcf70SLu Baolu 		dev_warn_ratelimited(dev, "Already bound with PASID %u\n",
355672cf6dfSJoerg Roedel 				     svm->pasid);
356672cf6dfSJoerg Roedel 		ret = -EBUSY;
357672cf6dfSJoerg Roedel 		goto out;
358672cf6dfSJoerg Roedel 	}
35919abcf70SLu Baolu 
36019abcf70SLu Baolu 	if (!svm) {
361672cf6dfSJoerg Roedel 		/* We come here when PASID has never been bond to a device. */
362672cf6dfSJoerg Roedel 		svm = kzalloc(sizeof(*svm), GFP_KERNEL);
363672cf6dfSJoerg Roedel 		if (!svm) {
364672cf6dfSJoerg Roedel 			ret = -ENOMEM;
365672cf6dfSJoerg Roedel 			goto out;
366672cf6dfSJoerg Roedel 		}
367672cf6dfSJoerg Roedel 		/* REVISIT: upper layer/VFIO can track host process that bind
368672cf6dfSJoerg Roedel 		 * the PASID. ioasid_set = mm might be sufficient for vfio to
369672cf6dfSJoerg Roedel 		 * check pasid VMM ownership. We can drop the following line
370672cf6dfSJoerg Roedel 		 * once VFIO and IOASID set check is in place.
371672cf6dfSJoerg Roedel 		 */
372672cf6dfSJoerg Roedel 		svm->mm = get_task_mm(current);
373672cf6dfSJoerg Roedel 		svm->pasid = data->hpasid;
374672cf6dfSJoerg Roedel 		if (data->flags & IOMMU_SVA_GPASID_VAL) {
375672cf6dfSJoerg Roedel 			svm->gpasid = data->gpasid;
376672cf6dfSJoerg Roedel 			svm->flags |= SVM_FLAG_GUEST_PASID;
377672cf6dfSJoerg Roedel 		}
378100b8a14SLu Baolu 		pasid_private_add(data->hpasid, svm);
379672cf6dfSJoerg Roedel 		INIT_LIST_HEAD_RCU(&svm->devs);
380672cf6dfSJoerg Roedel 		mmput(svm->mm);
381672cf6dfSJoerg Roedel 	}
382672cf6dfSJoerg Roedel 	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
383672cf6dfSJoerg Roedel 	if (!sdev) {
384672cf6dfSJoerg Roedel 		ret = -ENOMEM;
385672cf6dfSJoerg Roedel 		goto out;
386672cf6dfSJoerg Roedel 	}
387672cf6dfSJoerg Roedel 	sdev->dev = dev;
388eea4e29aSLiu Yi L 	sdev->sid = PCI_DEVID(info->bus, info->devfn);
3899ad9f45bSLiu Yi L 	sdev->iommu = iommu;
390672cf6dfSJoerg Roedel 
391672cf6dfSJoerg Roedel 	/* Only count users if device has aux domains */
392672cf6dfSJoerg Roedel 	if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
393672cf6dfSJoerg Roedel 		sdev->users = 1;
394672cf6dfSJoerg Roedel 
395672cf6dfSJoerg Roedel 	/* Set up device context entry for PASID if not enabled already */
396672cf6dfSJoerg Roedel 	ret = intel_iommu_enable_pasid(iommu, sdev->dev);
397672cf6dfSJoerg Roedel 	if (ret) {
398672cf6dfSJoerg Roedel 		dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
399672cf6dfSJoerg Roedel 		kfree(sdev);
400672cf6dfSJoerg Roedel 		goto out;
401672cf6dfSJoerg Roedel 	}
402672cf6dfSJoerg Roedel 
403672cf6dfSJoerg Roedel 	/*
404672cf6dfSJoerg Roedel 	 * PASID table is per device for better security. Therefore, for
405672cf6dfSJoerg Roedel 	 * each bind of a new device even with an existing PASID, we need to
406672cf6dfSJoerg Roedel 	 * call the nested mode setup function here.
407672cf6dfSJoerg Roedel 	 */
408420d42f6SLu Baolu 	spin_lock_irqsave(&iommu->lock, iflags);
409672cf6dfSJoerg Roedel 	ret = intel_pasid_setup_nested(iommu, dev,
410672cf6dfSJoerg Roedel 				       (pgd_t *)(uintptr_t)data->gpgd,
4118d3bb3b8SJacob Pan 				       data->hpasid, &data->vendor.vtd, dmar_domain,
412672cf6dfSJoerg Roedel 				       data->addr_width);
413420d42f6SLu Baolu 	spin_unlock_irqrestore(&iommu->lock, iflags);
414672cf6dfSJoerg Roedel 	if (ret) {
415672cf6dfSJoerg Roedel 		dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
416672cf6dfSJoerg Roedel 				    data->hpasid, ret);
417672cf6dfSJoerg Roedel 		/*
418672cf6dfSJoerg Roedel 		 * PASID entry should be in cleared state if nested mode
419672cf6dfSJoerg Roedel 		 * set up failed. So we only need to clear IOASID tracking
420672cf6dfSJoerg Roedel 		 * data such that free call will succeed.
421672cf6dfSJoerg Roedel 		 */
422672cf6dfSJoerg Roedel 		kfree(sdev);
423672cf6dfSJoerg Roedel 		goto out;
424672cf6dfSJoerg Roedel 	}
425672cf6dfSJoerg Roedel 
426672cf6dfSJoerg Roedel 	svm->flags |= SVM_FLAG_GUEST_MODE;
427672cf6dfSJoerg Roedel 
428672cf6dfSJoerg Roedel 	init_rcu_head(&sdev->rcu);
429672cf6dfSJoerg Roedel 	list_add_rcu(&sdev->list, &svm->devs);
430672cf6dfSJoerg Roedel  out:
431672cf6dfSJoerg Roedel 	if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
432100b8a14SLu Baolu 		pasid_private_remove(data->hpasid);
433672cf6dfSJoerg Roedel 		kfree(svm);
434672cf6dfSJoerg Roedel 	}
435672cf6dfSJoerg Roedel 
436672cf6dfSJoerg Roedel 	mutex_unlock(&pasid_mutex);
437672cf6dfSJoerg Roedel 	return ret;
438672cf6dfSJoerg Roedel }
439672cf6dfSJoerg Roedel 
440c7b6bac9SFenghua Yu int intel_svm_unbind_gpasid(struct device *dev, u32 pasid)
441672cf6dfSJoerg Roedel {
442dd6692f1SLu Baolu 	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
443672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
444672cf6dfSJoerg Roedel 	struct intel_svm *svm;
44519abcf70SLu Baolu 	int ret;
446672cf6dfSJoerg Roedel 
447672cf6dfSJoerg Roedel 	if (WARN_ON(!iommu))
448672cf6dfSJoerg Roedel 		return -EINVAL;
449672cf6dfSJoerg Roedel 
450672cf6dfSJoerg Roedel 	mutex_lock(&pasid_mutex);
45119abcf70SLu Baolu 	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
45219abcf70SLu Baolu 	if (ret)
453672cf6dfSJoerg Roedel 		goto out;
454672cf6dfSJoerg Roedel 
45519abcf70SLu Baolu 	if (sdev) {
456672cf6dfSJoerg Roedel 		if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
457672cf6dfSJoerg Roedel 			sdev->users--;
458672cf6dfSJoerg Roedel 		if (!sdev->users) {
459672cf6dfSJoerg Roedel 			list_del_rcu(&sdev->list);
460672cf6dfSJoerg Roedel 			intel_pasid_tear_down_entry(iommu, dev,
461672cf6dfSJoerg Roedel 						    svm->pasid, false);
462672cf6dfSJoerg Roedel 			intel_svm_drain_prq(dev, svm->pasid);
463672cf6dfSJoerg Roedel 			kfree_rcu(sdev, rcu);
464672cf6dfSJoerg Roedel 
465672cf6dfSJoerg Roedel 			if (list_empty(&svm->devs)) {
466672cf6dfSJoerg Roedel 				/*
467672cf6dfSJoerg Roedel 				 * We do not free the IOASID here in that
468672cf6dfSJoerg Roedel 				 * IOMMU driver did not allocate it.
469672cf6dfSJoerg Roedel 				 * Unlike native SVM, IOASID for guest use was
470672cf6dfSJoerg Roedel 				 * allocated prior to the bind call.
471672cf6dfSJoerg Roedel 				 * In any case, if the free call comes before
472672cf6dfSJoerg Roedel 				 * the unbind, IOMMU driver will get notified
473672cf6dfSJoerg Roedel 				 * and perform cleanup.
474672cf6dfSJoerg Roedel 				 */
475100b8a14SLu Baolu 				pasid_private_remove(pasid);
476672cf6dfSJoerg Roedel 				kfree(svm);
477672cf6dfSJoerg Roedel 			}
478672cf6dfSJoerg Roedel 		}
479672cf6dfSJoerg Roedel 	}
480672cf6dfSJoerg Roedel out:
481672cf6dfSJoerg Roedel 	mutex_unlock(&pasid_mutex);
482672cf6dfSJoerg Roedel 	return ret;
483672cf6dfSJoerg Roedel }
484672cf6dfSJoerg Roedel 
48520f0afd1SFenghua Yu static void _load_pasid(void *unused)
48620f0afd1SFenghua Yu {
48720f0afd1SFenghua Yu 	update_pasid();
48820f0afd1SFenghua Yu }
48920f0afd1SFenghua Yu 
49020f0afd1SFenghua Yu static void load_pasid(struct mm_struct *mm, u32 pasid)
49120f0afd1SFenghua Yu {
49220f0afd1SFenghua Yu 	mutex_lock(&mm->context.lock);
49320f0afd1SFenghua Yu 
49420f0afd1SFenghua Yu 	/* Synchronize with READ_ONCE in update_pasid(). */
49520f0afd1SFenghua Yu 	smp_store_release(&mm->pasid, pasid);
49620f0afd1SFenghua Yu 
49720f0afd1SFenghua Yu 	/* Update PASID MSR on all CPUs running the mm's tasks. */
49820f0afd1SFenghua Yu 	on_each_cpu_mask(mm_cpumask(mm), _load_pasid, NULL, true);
49920f0afd1SFenghua Yu 
50020f0afd1SFenghua Yu 	mutex_unlock(&mm->context.lock);
50120f0afd1SFenghua Yu }
50220f0afd1SFenghua Yu 
50340483774SLu Baolu static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm,
50440483774SLu Baolu 				 unsigned int flags)
505672cf6dfSJoerg Roedel {
50640483774SLu Baolu 	ioasid_t max_pasid = dev_is_pci(dev) ?
50740483774SLu Baolu 			pci_max_pasids(to_pci_dev(dev)) : intel_pasid_max_id;
50840483774SLu Baolu 
50940483774SLu Baolu 	return iommu_sva_alloc_pasid(mm, PASID_MIN, max_pasid - 1);
51040483774SLu Baolu }
51140483774SLu Baolu 
51240483774SLu Baolu static void intel_svm_free_pasid(struct mm_struct *mm)
51340483774SLu Baolu {
51440483774SLu Baolu 	iommu_sva_free_pasid(mm);
51540483774SLu Baolu }
51640483774SLu Baolu 
51740483774SLu Baolu static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
51840483774SLu Baolu 					   struct device *dev,
51940483774SLu Baolu 					   struct mm_struct *mm,
52040483774SLu Baolu 					   unsigned int flags)
52140483774SLu Baolu {
52240483774SLu Baolu 	struct device_domain_info *info = get_domain_info(dev);
52340483774SLu Baolu 	unsigned long iflags, sflags;
524672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
52540483774SLu Baolu 	struct intel_svm *svm;
52640483774SLu Baolu 	int ret = 0;
527672cf6dfSJoerg Roedel 
52840483774SLu Baolu 	svm = pasid_private_find(mm->pasid);
52940483774SLu Baolu 	if (!svm) {
53040483774SLu Baolu 		svm = kzalloc(sizeof(*svm), GFP_KERNEL);
53140483774SLu Baolu 		if (!svm)
53240483774SLu Baolu 			return ERR_PTR(-ENOMEM);
533672cf6dfSJoerg Roedel 
53440483774SLu Baolu 		svm->pasid = mm->pasid;
53540483774SLu Baolu 		svm->mm = mm;
53640483774SLu Baolu 		svm->flags = flags;
53740483774SLu Baolu 		INIT_LIST_HEAD_RCU(&svm->devs);
538672cf6dfSJoerg Roedel 
53940483774SLu Baolu 		if (!(flags & SVM_FLAG_SUPERVISOR_MODE)) {
54040483774SLu Baolu 			svm->notifier.ops = &intel_mmuops;
54140483774SLu Baolu 			ret = mmu_notifier_register(&svm->notifier, mm);
54240483774SLu Baolu 			if (ret) {
54340483774SLu Baolu 				kfree(svm);
54440483774SLu Baolu 				return ERR_PTR(ret);
545672cf6dfSJoerg Roedel 			}
546672cf6dfSJoerg Roedel 		}
547672cf6dfSJoerg Roedel 
54840483774SLu Baolu 		ret = pasid_private_add(svm->pasid, svm);
54940483774SLu Baolu 		if (ret) {
55040483774SLu Baolu 			if (svm->notifier.ops)
55140483774SLu Baolu 				mmu_notifier_unregister(&svm->notifier, mm);
55240483774SLu Baolu 			kfree(svm);
55340483774SLu Baolu 			return ERR_PTR(ret);
55440483774SLu Baolu 		}
555672cf6dfSJoerg Roedel 	}
556672cf6dfSJoerg Roedel 
557672cf6dfSJoerg Roedel 	/* Find the matching device in svm list */
5589e52cc0fSLu Baolu 	sdev = svm_lookup_device_by_dev(svm, dev);
5599e52cc0fSLu Baolu 	if (sdev) {
560672cf6dfSJoerg Roedel 		sdev->users++;
561672cf6dfSJoerg Roedel 		goto success;
562672cf6dfSJoerg Roedel 	}
563672cf6dfSJoerg Roedel 
564672cf6dfSJoerg Roedel 	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
565672cf6dfSJoerg Roedel 	if (!sdev) {
566672cf6dfSJoerg Roedel 		ret = -ENOMEM;
56740483774SLu Baolu 		goto free_svm;
568672cf6dfSJoerg Roedel 	}
56940483774SLu Baolu 
570672cf6dfSJoerg Roedel 	sdev->dev = dev;
5719ad9f45bSLiu Yi L 	sdev->iommu = iommu;
572672cf6dfSJoerg Roedel 	sdev->did = FLPT_DEFAULT_DID;
573672cf6dfSJoerg Roedel 	sdev->sid = PCI_DEVID(info->bus, info->devfn);
57440483774SLu Baolu 	sdev->users = 1;
57540483774SLu Baolu 	sdev->pasid = svm->pasid;
57640483774SLu Baolu 	sdev->sva.dev = dev;
57740483774SLu Baolu 	init_rcu_head(&sdev->rcu);
578672cf6dfSJoerg Roedel 	if (info->ats_enabled) {
579672cf6dfSJoerg Roedel 		sdev->dev_iotlb = 1;
580672cf6dfSJoerg Roedel 		sdev->qdep = info->ats_qdep;
581672cf6dfSJoerg Roedel 		if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
582672cf6dfSJoerg Roedel 			sdev->qdep = 0;
583672cf6dfSJoerg Roedel 	}
584672cf6dfSJoerg Roedel 
58540483774SLu Baolu 	/* Setup the pasid table: */
58640483774SLu Baolu 	sflags = (flags & SVM_FLAG_SUPERVISOR_MODE) ?
58740483774SLu Baolu 			PASID_FLAG_SUPERVISOR_MODE : 0;
58840483774SLu Baolu 	sflags |= cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
589420d42f6SLu Baolu 	spin_lock_irqsave(&iommu->lock, iflags);
59040483774SLu Baolu 	ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid,
59140483774SLu Baolu 					    FLPT_DEFAULT_DID, sflags);
592420d42f6SLu Baolu 	spin_unlock_irqrestore(&iommu->lock, iflags);
593672cf6dfSJoerg Roedel 
59440483774SLu Baolu 	if (ret)
59540483774SLu Baolu 		goto free_sdev;
59640483774SLu Baolu 
59720f0afd1SFenghua Yu 	/* The newly allocated pasid is loaded to the mm. */
59840483774SLu Baolu 	if (!(flags & SVM_FLAG_SUPERVISOR_MODE) && list_empty(&svm->devs))
59920f0afd1SFenghua Yu 		load_pasid(mm, svm->pasid);
60040483774SLu Baolu 
601672cf6dfSJoerg Roedel 	list_add_rcu(&sdev->list, &svm->devs);
602672cf6dfSJoerg Roedel success:
60340483774SLu Baolu 	return &sdev->sva;
60440483774SLu Baolu 
60540483774SLu Baolu free_sdev:
60640483774SLu Baolu 	kfree(sdev);
60740483774SLu Baolu free_svm:
60840483774SLu Baolu 	if (list_empty(&svm->devs)) {
60940483774SLu Baolu 		if (svm->notifier.ops)
61040483774SLu Baolu 			mmu_notifier_unregister(&svm->notifier, mm);
61140483774SLu Baolu 		pasid_private_remove(mm->pasid);
61240483774SLu Baolu 		kfree(svm);
61340483774SLu Baolu 	}
61440483774SLu Baolu 
61540483774SLu Baolu 	return ERR_PTR(ret);
616672cf6dfSJoerg Roedel }
617672cf6dfSJoerg Roedel 
618672cf6dfSJoerg Roedel /* Caller must hold pasid_mutex */
619c7b6bac9SFenghua Yu static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
620672cf6dfSJoerg Roedel {
621672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
622672cf6dfSJoerg Roedel 	struct intel_iommu *iommu;
623672cf6dfSJoerg Roedel 	struct intel_svm *svm;
62440483774SLu Baolu 	struct mm_struct *mm;
625672cf6dfSJoerg Roedel 	int ret = -EINVAL;
626672cf6dfSJoerg Roedel 
627dd6692f1SLu Baolu 	iommu = device_to_iommu(dev, NULL, NULL);
628672cf6dfSJoerg Roedel 	if (!iommu)
629672cf6dfSJoerg Roedel 		goto out;
630672cf6dfSJoerg Roedel 
63119abcf70SLu Baolu 	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
63219abcf70SLu Baolu 	if (ret)
633672cf6dfSJoerg Roedel 		goto out;
63440483774SLu Baolu 	mm = svm->mm;
635672cf6dfSJoerg Roedel 
63619abcf70SLu Baolu 	if (sdev) {
637672cf6dfSJoerg Roedel 		sdev->users--;
638672cf6dfSJoerg Roedel 		if (!sdev->users) {
639672cf6dfSJoerg Roedel 			list_del_rcu(&sdev->list);
640672cf6dfSJoerg Roedel 			/* Flush the PASID cache and IOTLB for this device.
641672cf6dfSJoerg Roedel 			 * Note that we do depend on the hardware *not* using
642672cf6dfSJoerg Roedel 			 * the PASID any more. Just as we depend on other
643672cf6dfSJoerg Roedel 			 * devices never using PASIDs that they have no right
644672cf6dfSJoerg Roedel 			 * to use. We have a *shared* PASID table, because it's
645672cf6dfSJoerg Roedel 			 * large and has to be physically contiguous. So it's
646672cf6dfSJoerg Roedel 			 * hard to be as defensive as we might like. */
647672cf6dfSJoerg Roedel 			intel_pasid_tear_down_entry(iommu, dev,
648672cf6dfSJoerg Roedel 						    svm->pasid, false);
649672cf6dfSJoerg Roedel 			intel_svm_drain_prq(dev, svm->pasid);
650672cf6dfSJoerg Roedel 			kfree_rcu(sdev, rcu);
651672cf6dfSJoerg Roedel 
652672cf6dfSJoerg Roedel 			if (list_empty(&svm->devs)) {
65340483774SLu Baolu 				intel_svm_free_pasid(mm);
65440483774SLu Baolu 				if (svm->notifier.ops) {
65540483774SLu Baolu 					mmu_notifier_unregister(&svm->notifier, mm);
65620f0afd1SFenghua Yu 					/* Clear mm's pasid. */
65740483774SLu Baolu 					load_pasid(mm, PASID_DISABLED);
65820f0afd1SFenghua Yu 				}
659100b8a14SLu Baolu 				pasid_private_remove(svm->pasid);
660672cf6dfSJoerg Roedel 				/* We mandate that no page faults may be outstanding
661672cf6dfSJoerg Roedel 				 * for the PASID when intel_svm_unbind_mm() is called.
662672cf6dfSJoerg Roedel 				 * If that is not obeyed, subtle errors will happen.
663672cf6dfSJoerg Roedel 				 * Let's make them less subtle... */
664672cf6dfSJoerg Roedel 				memset(svm, 0x6b, sizeof(*svm));
665672cf6dfSJoerg Roedel 				kfree(svm);
666672cf6dfSJoerg Roedel 			}
667672cf6dfSJoerg Roedel 		}
668672cf6dfSJoerg Roedel 	}
669672cf6dfSJoerg Roedel out:
670672cf6dfSJoerg Roedel 	return ret;
671672cf6dfSJoerg Roedel }
672672cf6dfSJoerg Roedel 
673672cf6dfSJoerg Roedel /* Page request queue descriptor */
674672cf6dfSJoerg Roedel struct page_req_dsc {
675672cf6dfSJoerg Roedel 	union {
676672cf6dfSJoerg Roedel 		struct {
677672cf6dfSJoerg Roedel 			u64 type:8;
678672cf6dfSJoerg Roedel 			u64 pasid_present:1;
679672cf6dfSJoerg Roedel 			u64 priv_data_present:1;
680672cf6dfSJoerg Roedel 			u64 rsvd:6;
681672cf6dfSJoerg Roedel 			u64 rid:16;
682672cf6dfSJoerg Roedel 			u64 pasid:20;
683672cf6dfSJoerg Roedel 			u64 exe_req:1;
684672cf6dfSJoerg Roedel 			u64 pm_req:1;
685672cf6dfSJoerg Roedel 			u64 rsvd2:10;
686672cf6dfSJoerg Roedel 		};
687672cf6dfSJoerg Roedel 		u64 qw_0;
688672cf6dfSJoerg Roedel 	};
689672cf6dfSJoerg Roedel 	union {
690672cf6dfSJoerg Roedel 		struct {
691672cf6dfSJoerg Roedel 			u64 rd_req:1;
692672cf6dfSJoerg Roedel 			u64 wr_req:1;
693672cf6dfSJoerg Roedel 			u64 lpig:1;
694672cf6dfSJoerg Roedel 			u64 prg_index:9;
695672cf6dfSJoerg Roedel 			u64 addr:52;
696672cf6dfSJoerg Roedel 		};
697672cf6dfSJoerg Roedel 		u64 qw_1;
698672cf6dfSJoerg Roedel 	};
699672cf6dfSJoerg Roedel 	u64 priv_data[2];
700672cf6dfSJoerg Roedel };
701672cf6dfSJoerg Roedel 
702672cf6dfSJoerg Roedel #define PRQ_RING_MASK	((0x1000 << PRQ_ORDER) - 0x20)
703672cf6dfSJoerg Roedel 
704672cf6dfSJoerg Roedel static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
705672cf6dfSJoerg Roedel {
706672cf6dfSJoerg Roedel 	unsigned long requested = 0;
707672cf6dfSJoerg Roedel 
708672cf6dfSJoerg Roedel 	if (req->exe_req)
709672cf6dfSJoerg Roedel 		requested |= VM_EXEC;
710672cf6dfSJoerg Roedel 
711672cf6dfSJoerg Roedel 	if (req->rd_req)
712672cf6dfSJoerg Roedel 		requested |= VM_READ;
713672cf6dfSJoerg Roedel 
714672cf6dfSJoerg Roedel 	if (req->wr_req)
715672cf6dfSJoerg Roedel 		requested |= VM_WRITE;
716672cf6dfSJoerg Roedel 
717672cf6dfSJoerg Roedel 	return (requested & ~vma->vm_flags) != 0;
718672cf6dfSJoerg Roedel }
719672cf6dfSJoerg Roedel 
720672cf6dfSJoerg Roedel static bool is_canonical_address(u64 addr)
721672cf6dfSJoerg Roedel {
722672cf6dfSJoerg Roedel 	int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
723672cf6dfSJoerg Roedel 	long saddr = (long) addr;
724672cf6dfSJoerg Roedel 
725672cf6dfSJoerg Roedel 	return (((saddr << shift) >> shift) == saddr);
726672cf6dfSJoerg Roedel }
727672cf6dfSJoerg Roedel 
728672cf6dfSJoerg Roedel /**
729672cf6dfSJoerg Roedel  * intel_svm_drain_prq - Drain page requests and responses for a pasid
730672cf6dfSJoerg Roedel  * @dev: target device
731672cf6dfSJoerg Roedel  * @pasid: pasid for draining
732672cf6dfSJoerg Roedel  *
733672cf6dfSJoerg Roedel  * Drain all pending page requests and responses related to @pasid in both
734672cf6dfSJoerg Roedel  * software and hardware. This is supposed to be called after the device
735672cf6dfSJoerg Roedel  * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
736672cf6dfSJoerg Roedel  * and DevTLB have been invalidated.
737672cf6dfSJoerg Roedel  *
738672cf6dfSJoerg Roedel  * It waits until all pending page requests for @pasid in the page fault
739672cf6dfSJoerg Roedel  * queue are completed by the prq handling thread. Then follow the steps
740672cf6dfSJoerg Roedel  * described in VT-d spec CH7.10 to drain all page requests and page
741672cf6dfSJoerg Roedel  * responses pending in the hardware.
742672cf6dfSJoerg Roedel  */
743c7b6bac9SFenghua Yu static void intel_svm_drain_prq(struct device *dev, u32 pasid)
744672cf6dfSJoerg Roedel {
745672cf6dfSJoerg Roedel 	struct device_domain_info *info;
746672cf6dfSJoerg Roedel 	struct dmar_domain *domain;
747672cf6dfSJoerg Roedel 	struct intel_iommu *iommu;
748672cf6dfSJoerg Roedel 	struct qi_desc desc[3];
749672cf6dfSJoerg Roedel 	struct pci_dev *pdev;
750672cf6dfSJoerg Roedel 	int head, tail;
751672cf6dfSJoerg Roedel 	u16 sid, did;
752672cf6dfSJoerg Roedel 	int qdep;
753672cf6dfSJoerg Roedel 
754672cf6dfSJoerg Roedel 	info = get_domain_info(dev);
755672cf6dfSJoerg Roedel 	if (WARN_ON(!info || !dev_is_pci(dev)))
756672cf6dfSJoerg Roedel 		return;
757672cf6dfSJoerg Roedel 
758672cf6dfSJoerg Roedel 	if (!info->pri_enabled)
759672cf6dfSJoerg Roedel 		return;
760672cf6dfSJoerg Roedel 
761672cf6dfSJoerg Roedel 	iommu = info->iommu;
762672cf6dfSJoerg Roedel 	domain = info->domain;
763672cf6dfSJoerg Roedel 	pdev = to_pci_dev(dev);
764672cf6dfSJoerg Roedel 	sid = PCI_DEVID(info->bus, info->devfn);
765672cf6dfSJoerg Roedel 	did = domain->iommu_did[iommu->seq_id];
766672cf6dfSJoerg Roedel 	qdep = pci_ats_queue_depth(pdev);
767672cf6dfSJoerg Roedel 
768672cf6dfSJoerg Roedel 	/*
769672cf6dfSJoerg Roedel 	 * Check and wait until all pending page requests in the queue are
770672cf6dfSJoerg Roedel 	 * handled by the prq handling thread.
771672cf6dfSJoerg Roedel 	 */
772672cf6dfSJoerg Roedel prq_retry:
773672cf6dfSJoerg Roedel 	reinit_completion(&iommu->prq_complete);
774672cf6dfSJoerg Roedel 	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
775672cf6dfSJoerg Roedel 	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
776672cf6dfSJoerg Roedel 	while (head != tail) {
777672cf6dfSJoerg Roedel 		struct page_req_dsc *req;
778672cf6dfSJoerg Roedel 
779672cf6dfSJoerg Roedel 		req = &iommu->prq[head / sizeof(*req)];
780672cf6dfSJoerg Roedel 		if (!req->pasid_present || req->pasid != pasid) {
781672cf6dfSJoerg Roedel 			head = (head + sizeof(*req)) & PRQ_RING_MASK;
782672cf6dfSJoerg Roedel 			continue;
783672cf6dfSJoerg Roedel 		}
784672cf6dfSJoerg Roedel 
785672cf6dfSJoerg Roedel 		wait_for_completion(&iommu->prq_complete);
786672cf6dfSJoerg Roedel 		goto prq_retry;
787672cf6dfSJoerg Roedel 	}
788672cf6dfSJoerg Roedel 
789672cf6dfSJoerg Roedel 	/*
790672cf6dfSJoerg Roedel 	 * Perform steps described in VT-d spec CH7.10 to drain page
791672cf6dfSJoerg Roedel 	 * requests and responses in hardware.
792672cf6dfSJoerg Roedel 	 */
793672cf6dfSJoerg Roedel 	memset(desc, 0, sizeof(desc));
794672cf6dfSJoerg Roedel 	desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
795672cf6dfSJoerg Roedel 			QI_IWD_FENCE |
796672cf6dfSJoerg Roedel 			QI_IWD_TYPE;
797672cf6dfSJoerg Roedel 	desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
798672cf6dfSJoerg Roedel 			QI_EIOTLB_DID(did) |
799672cf6dfSJoerg Roedel 			QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
800672cf6dfSJoerg Roedel 			QI_EIOTLB_TYPE;
801672cf6dfSJoerg Roedel 	desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
802672cf6dfSJoerg Roedel 			QI_DEV_EIOTLB_SID(sid) |
803672cf6dfSJoerg Roedel 			QI_DEV_EIOTLB_QDEP(qdep) |
804672cf6dfSJoerg Roedel 			QI_DEIOTLB_TYPE |
805672cf6dfSJoerg Roedel 			QI_DEV_IOTLB_PFSID(info->pfsid);
806672cf6dfSJoerg Roedel qi_retry:
807672cf6dfSJoerg Roedel 	reinit_completion(&iommu->prq_complete);
808672cf6dfSJoerg Roedel 	qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
809672cf6dfSJoerg Roedel 	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
810672cf6dfSJoerg Roedel 		wait_for_completion(&iommu->prq_complete);
811672cf6dfSJoerg Roedel 		goto qi_retry;
812672cf6dfSJoerg Roedel 	}
813672cf6dfSJoerg Roedel }
814672cf6dfSJoerg Roedel 
815eb8d93eaSLu Baolu static int prq_to_iommu_prot(struct page_req_dsc *req)
816eb8d93eaSLu Baolu {
817eb8d93eaSLu Baolu 	int prot = 0;
818eb8d93eaSLu Baolu 
819eb8d93eaSLu Baolu 	if (req->rd_req)
820eb8d93eaSLu Baolu 		prot |= IOMMU_FAULT_PERM_READ;
821eb8d93eaSLu Baolu 	if (req->wr_req)
822eb8d93eaSLu Baolu 		prot |= IOMMU_FAULT_PERM_WRITE;
823eb8d93eaSLu Baolu 	if (req->exe_req)
824eb8d93eaSLu Baolu 		prot |= IOMMU_FAULT_PERM_EXEC;
825eb8d93eaSLu Baolu 	if (req->pm_req)
826eb8d93eaSLu Baolu 		prot |= IOMMU_FAULT_PERM_PRIV;
827eb8d93eaSLu Baolu 
828eb8d93eaSLu Baolu 	return prot;
829eb8d93eaSLu Baolu }
830eb8d93eaSLu Baolu 
831eb8d93eaSLu Baolu static int
832eb8d93eaSLu Baolu intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc)
833eb8d93eaSLu Baolu {
834eb8d93eaSLu Baolu 	struct iommu_fault_event event;
835eb8d93eaSLu Baolu 
836eb8d93eaSLu Baolu 	if (!dev || !dev_is_pci(dev))
837eb8d93eaSLu Baolu 		return -ENODEV;
838eb8d93eaSLu Baolu 
839eb8d93eaSLu Baolu 	/* Fill in event data for device specific processing */
840eb8d93eaSLu Baolu 	memset(&event, 0, sizeof(struct iommu_fault_event));
841eb8d93eaSLu Baolu 	event.fault.type = IOMMU_FAULT_PAGE_REQ;
84203d20509SLu Baolu 	event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT;
843eb8d93eaSLu Baolu 	event.fault.prm.pasid = desc->pasid;
844eb8d93eaSLu Baolu 	event.fault.prm.grpid = desc->prg_index;
845eb8d93eaSLu Baolu 	event.fault.prm.perm = prq_to_iommu_prot(desc);
846eb8d93eaSLu Baolu 
847eb8d93eaSLu Baolu 	if (desc->lpig)
848eb8d93eaSLu Baolu 		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
849eb8d93eaSLu Baolu 	if (desc->pasid_present) {
850eb8d93eaSLu Baolu 		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
851eb8d93eaSLu Baolu 		event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
852eb8d93eaSLu Baolu 	}
853eb8d93eaSLu Baolu 	if (desc->priv_data_present) {
854eb8d93eaSLu Baolu 		/*
855eb8d93eaSLu Baolu 		 * Set last page in group bit if private data is present,
856eb8d93eaSLu Baolu 		 * page response is required as it does for LPIG.
857eb8d93eaSLu Baolu 		 * iommu_report_device_fault() doesn't understand this vendor
858eb8d93eaSLu Baolu 		 * specific requirement thus we set last_page as a workaround.
859eb8d93eaSLu Baolu 		 */
860eb8d93eaSLu Baolu 		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
861eb8d93eaSLu Baolu 		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
862eb8d93eaSLu Baolu 		memcpy(event.fault.prm.private_data, desc->priv_data,
863eb8d93eaSLu Baolu 		       sizeof(desc->priv_data));
864eb8d93eaSLu Baolu 	}
865eb8d93eaSLu Baolu 
866eb8d93eaSLu Baolu 	return iommu_report_device_fault(dev, &event);
867eb8d93eaSLu Baolu }
868eb8d93eaSLu Baolu 
869*ae7f09b1SLu Baolu static void handle_bad_prq_event(struct intel_iommu *iommu,
870*ae7f09b1SLu Baolu 				 struct page_req_dsc *req, int result)
871672cf6dfSJoerg Roedel {
872*ae7f09b1SLu Baolu 	struct qi_desc desc;
873672cf6dfSJoerg Roedel 
874*ae7f09b1SLu Baolu 	pr_err("%s: Invalid page request: %08llx %08llx\n",
875672cf6dfSJoerg Roedel 	       iommu->name, ((unsigned long long *)req)[0],
876672cf6dfSJoerg Roedel 	       ((unsigned long long *)req)[1]);
877672cf6dfSJoerg Roedel 
878eb8d93eaSLu Baolu 	/*
879*ae7f09b1SLu Baolu 	 * Per VT-d spec. v3.0 ch7.7, system software must
880*ae7f09b1SLu Baolu 	 * respond with page group response if private data
881*ae7f09b1SLu Baolu 	 * is present (PDP) or last page in group (LPIG) bit
882*ae7f09b1SLu Baolu 	 * is set. This is an additional VT-d feature beyond
883*ae7f09b1SLu Baolu 	 * PCI ATS spec.
884eb8d93eaSLu Baolu 	 */
885*ae7f09b1SLu Baolu 	if (!req->lpig && !req->priv_data_present)
886*ae7f09b1SLu Baolu 		return;
887*ae7f09b1SLu Baolu 
888*ae7f09b1SLu Baolu 	desc.qw0 = QI_PGRP_PASID(req->pasid) |
889*ae7f09b1SLu Baolu 			QI_PGRP_DID(req->rid) |
890*ae7f09b1SLu Baolu 			QI_PGRP_PASID_P(req->pasid_present) |
891*ae7f09b1SLu Baolu 			QI_PGRP_PDP(req->priv_data_present) |
892*ae7f09b1SLu Baolu 			QI_PGRP_RESP_CODE(result) |
893*ae7f09b1SLu Baolu 			QI_PGRP_RESP_TYPE;
894*ae7f09b1SLu Baolu 	desc.qw1 = QI_PGRP_IDX(req->prg_index) |
895*ae7f09b1SLu Baolu 			QI_PGRP_LPIG(req->lpig);
896*ae7f09b1SLu Baolu 	desc.qw2 = 0;
897*ae7f09b1SLu Baolu 	desc.qw3 = 0;
898*ae7f09b1SLu Baolu 
899*ae7f09b1SLu Baolu 	if (req->priv_data_present)
900*ae7f09b1SLu Baolu 		memcpy(&desc.qw2, req->priv_data, sizeof(req->priv_data));
901*ae7f09b1SLu Baolu 	qi_submit_sync(iommu, &desc, 1, 0);
902eb8d93eaSLu Baolu }
903eb8d93eaSLu Baolu 
904*ae7f09b1SLu Baolu static void handle_single_prq_event(struct intel_iommu *iommu,
905*ae7f09b1SLu Baolu 				    struct mm_struct *mm,
906*ae7f09b1SLu Baolu 				    struct page_req_dsc *req)
907*ae7f09b1SLu Baolu {
908*ae7f09b1SLu Baolu 	u64 address = (u64)req->addr << VTD_PAGE_SHIFT;
909*ae7f09b1SLu Baolu 	int result = QI_RESP_INVALID;
910*ae7f09b1SLu Baolu 	struct vm_area_struct *vma;
911*ae7f09b1SLu Baolu 	struct qi_desc desc;
912*ae7f09b1SLu Baolu 	unsigned int flags;
913*ae7f09b1SLu Baolu 	vm_fault_t ret;
914672cf6dfSJoerg Roedel 
915*ae7f09b1SLu Baolu 	/* If the mm is already defunct, don't handle faults. */
916*ae7f09b1SLu Baolu 	if (!mmget_not_zero(mm))
917*ae7f09b1SLu Baolu 		goto response;
918*ae7f09b1SLu Baolu 
919*ae7f09b1SLu Baolu 	mmap_read_lock(mm);
920*ae7f09b1SLu Baolu 	vma = find_extend_vma(mm, address);
921672cf6dfSJoerg Roedel 	if (!vma || address < vma->vm_start)
922672cf6dfSJoerg Roedel 		goto invalid;
923672cf6dfSJoerg Roedel 
924672cf6dfSJoerg Roedel 	if (access_error(vma, req))
925672cf6dfSJoerg Roedel 		goto invalid;
926672cf6dfSJoerg Roedel 
927396bd6f3SJacob Pan 	flags = FAULT_FLAG_USER | FAULT_FLAG_REMOTE;
928396bd6f3SJacob Pan 	if (req->wr_req)
929396bd6f3SJacob Pan 		flags |= FAULT_FLAG_WRITE;
930396bd6f3SJacob Pan 
931396bd6f3SJacob Pan 	ret = handle_mm_fault(vma, address, flags, NULL);
932*ae7f09b1SLu Baolu 	if (!(ret & VM_FAULT_ERROR))
933672cf6dfSJoerg Roedel 		result = QI_RESP_SUCCESS;
934672cf6dfSJoerg Roedel invalid:
935*ae7f09b1SLu Baolu 	mmap_read_unlock(mm);
936*ae7f09b1SLu Baolu 	mmput(mm);
937*ae7f09b1SLu Baolu 
938*ae7f09b1SLu Baolu response:
939*ae7f09b1SLu Baolu 	if (!(req->lpig || req->priv_data_present))
940*ae7f09b1SLu Baolu 		return;
941*ae7f09b1SLu Baolu 
942*ae7f09b1SLu Baolu 	desc.qw0 = QI_PGRP_PASID(req->pasid) |
943672cf6dfSJoerg Roedel 			QI_PGRP_DID(req->rid) |
944672cf6dfSJoerg Roedel 			QI_PGRP_PASID_P(req->pasid_present) |
94571cd8e2dSLiu, Yi L 			QI_PGRP_PDP(req->priv_data_present) |
946672cf6dfSJoerg Roedel 			QI_PGRP_RESP_CODE(result) |
947672cf6dfSJoerg Roedel 			QI_PGRP_RESP_TYPE;
948*ae7f09b1SLu Baolu 	desc.qw1 = QI_PGRP_IDX(req->prg_index) |
949672cf6dfSJoerg Roedel 			QI_PGRP_LPIG(req->lpig);
950*ae7f09b1SLu Baolu 	desc.qw2 = 0;
951*ae7f09b1SLu Baolu 	desc.qw3 = 0;
952672cf6dfSJoerg Roedel 
953672cf6dfSJoerg Roedel 	if (req->priv_data_present)
954*ae7f09b1SLu Baolu 		memcpy(&desc.qw2, req->priv_data, sizeof(req->priv_data));
955*ae7f09b1SLu Baolu 
956*ae7f09b1SLu Baolu 	qi_submit_sync(iommu, &desc, 1, 0);
957672cf6dfSJoerg Roedel }
958*ae7f09b1SLu Baolu 
959*ae7f09b1SLu Baolu static irqreturn_t prq_event_thread(int irq, void *d)
960*ae7f09b1SLu Baolu {
961*ae7f09b1SLu Baolu 	struct intel_svm_dev *sdev = NULL;
962*ae7f09b1SLu Baolu 	struct intel_iommu *iommu = d;
963*ae7f09b1SLu Baolu 	struct intel_svm *svm = NULL;
964*ae7f09b1SLu Baolu 	struct page_req_dsc *req;
965*ae7f09b1SLu Baolu 	int head, tail, handled;
966*ae7f09b1SLu Baolu 	u64 address;
967*ae7f09b1SLu Baolu 
968*ae7f09b1SLu Baolu 	/*
969*ae7f09b1SLu Baolu 	 * Clear PPR bit before reading head/tail registers, to ensure that
970*ae7f09b1SLu Baolu 	 * we get a new interrupt if needed.
971*ae7f09b1SLu Baolu 	 */
972*ae7f09b1SLu Baolu 	writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
973*ae7f09b1SLu Baolu 
974*ae7f09b1SLu Baolu 	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
975*ae7f09b1SLu Baolu 	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
976*ae7f09b1SLu Baolu 	handled = (head != tail);
977*ae7f09b1SLu Baolu 	while (head != tail) {
978*ae7f09b1SLu Baolu 		req = &iommu->prq[head / sizeof(*req)];
979*ae7f09b1SLu Baolu 		address = (u64)req->addr << VTD_PAGE_SHIFT;
980*ae7f09b1SLu Baolu 
981*ae7f09b1SLu Baolu 		if (unlikely(!req->pasid_present)) {
982*ae7f09b1SLu Baolu 			pr_err("IOMMU: %s: Page request without PASID\n",
983*ae7f09b1SLu Baolu 			       iommu->name);
984*ae7f09b1SLu Baolu bad_req:
985*ae7f09b1SLu Baolu 			svm = NULL;
986*ae7f09b1SLu Baolu 			sdev = NULL;
987*ae7f09b1SLu Baolu 			handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
988*ae7f09b1SLu Baolu 			goto prq_advance;
989*ae7f09b1SLu Baolu 		}
990*ae7f09b1SLu Baolu 
991*ae7f09b1SLu Baolu 		if (unlikely(!is_canonical_address(address))) {
992*ae7f09b1SLu Baolu 			pr_err("IOMMU: %s: Address is not canonical\n",
993*ae7f09b1SLu Baolu 			       iommu->name);
994*ae7f09b1SLu Baolu 			goto bad_req;
995*ae7f09b1SLu Baolu 		}
996*ae7f09b1SLu Baolu 
997*ae7f09b1SLu Baolu 		if (unlikely(req->pm_req && (req->rd_req | req->wr_req))) {
998*ae7f09b1SLu Baolu 			pr_err("IOMMU: %s: Page request in Privilege Mode\n",
999*ae7f09b1SLu Baolu 			       iommu->name);
1000*ae7f09b1SLu Baolu 			goto bad_req;
1001*ae7f09b1SLu Baolu 		}
1002*ae7f09b1SLu Baolu 
1003*ae7f09b1SLu Baolu 		if (unlikely(req->exe_req && req->rd_req)) {
1004*ae7f09b1SLu Baolu 			pr_err("IOMMU: %s: Execution request not supported\n",
1005*ae7f09b1SLu Baolu 			       iommu->name);
1006*ae7f09b1SLu Baolu 			goto bad_req;
1007*ae7f09b1SLu Baolu 		}
1008*ae7f09b1SLu Baolu 
1009*ae7f09b1SLu Baolu 		if (!svm || svm->pasid != req->pasid) {
1010*ae7f09b1SLu Baolu 			/*
1011*ae7f09b1SLu Baolu 			 * It can't go away, because the driver is not permitted
1012*ae7f09b1SLu Baolu 			 * to unbind the mm while any page faults are outstanding.
1013*ae7f09b1SLu Baolu 			 */
1014*ae7f09b1SLu Baolu 			svm = pasid_private_find(req->pasid);
1015*ae7f09b1SLu Baolu 			if (IS_ERR_OR_NULL(svm) || (svm->flags & SVM_FLAG_SUPERVISOR_MODE))
1016*ae7f09b1SLu Baolu 				goto bad_req;
1017*ae7f09b1SLu Baolu 		}
1018*ae7f09b1SLu Baolu 
1019*ae7f09b1SLu Baolu 		if (!sdev || sdev->sid != req->rid) {
1020*ae7f09b1SLu Baolu 			sdev = svm_lookup_device_by_sid(svm, req->rid);
1021*ae7f09b1SLu Baolu 			if (!sdev)
1022*ae7f09b1SLu Baolu 				goto bad_req;
1023*ae7f09b1SLu Baolu 		}
1024*ae7f09b1SLu Baolu 
1025*ae7f09b1SLu Baolu 		/*
1026*ae7f09b1SLu Baolu 		 * If prq is to be handled outside iommu driver via receiver of
1027*ae7f09b1SLu Baolu 		 * the fault notifiers, we skip the page response here.
1028*ae7f09b1SLu Baolu 		 */
1029*ae7f09b1SLu Baolu 		if (svm->flags & SVM_FLAG_GUEST_MODE) {
1030*ae7f09b1SLu Baolu 			if (!intel_svm_prq_report(sdev->dev, req))
1031*ae7f09b1SLu Baolu 				goto prq_advance;
1032*ae7f09b1SLu Baolu 			else
1033*ae7f09b1SLu Baolu 				goto bad_req;
1034*ae7f09b1SLu Baolu 		}
1035*ae7f09b1SLu Baolu 
1036*ae7f09b1SLu Baolu 		handle_single_prq_event(iommu, svm->mm, req);
1037eb8d93eaSLu Baolu prq_advance:
1038672cf6dfSJoerg Roedel 		head = (head + sizeof(*req)) & PRQ_RING_MASK;
1039672cf6dfSJoerg Roedel 	}
1040672cf6dfSJoerg Roedel 
1041672cf6dfSJoerg Roedel 	dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
1042672cf6dfSJoerg Roedel 
1043672cf6dfSJoerg Roedel 	/*
1044672cf6dfSJoerg Roedel 	 * Clear the page request overflow bit and wake up all threads that
1045672cf6dfSJoerg Roedel 	 * are waiting for the completion of this handling.
1046672cf6dfSJoerg Roedel 	 */
104728a77185SLu Baolu 	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
104828a77185SLu Baolu 		pr_info_ratelimited("IOMMU: %s: PRQ overflow detected\n",
104928a77185SLu Baolu 				    iommu->name);
105028a77185SLu Baolu 		head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
105128a77185SLu Baolu 		tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
105228a77185SLu Baolu 		if (head == tail) {
1053672cf6dfSJoerg Roedel 			writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
105428a77185SLu Baolu 			pr_info_ratelimited("IOMMU: %s: PRQ overflow cleared",
105528a77185SLu Baolu 					    iommu->name);
105628a77185SLu Baolu 		}
105728a77185SLu Baolu 	}
1058672cf6dfSJoerg Roedel 
1059672cf6dfSJoerg Roedel 	if (!completion_done(&iommu->prq_complete))
1060672cf6dfSJoerg Roedel 		complete(&iommu->prq_complete);
1061672cf6dfSJoerg Roedel 
1062672cf6dfSJoerg Roedel 	return IRQ_RETVAL(handled);
1063672cf6dfSJoerg Roedel }
1064672cf6dfSJoerg Roedel 
106540483774SLu Baolu struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
1066672cf6dfSJoerg Roedel {
106740483774SLu Baolu 	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
10682a5054c6SFenghua Yu 	unsigned int flags = 0;
106940483774SLu Baolu 	struct iommu_sva *sva;
1070672cf6dfSJoerg Roedel 	int ret;
1071672cf6dfSJoerg Roedel 
1072672cf6dfSJoerg Roedel 	if (drvdata)
10732a5054c6SFenghua Yu 		flags = *(unsigned int *)drvdata;
1074672cf6dfSJoerg Roedel 
107540483774SLu Baolu 	if (flags & SVM_FLAG_SUPERVISOR_MODE) {
107640483774SLu Baolu 		if (!ecap_srs(iommu->ecap)) {
107740483774SLu Baolu 			dev_err(dev, "%s: Supervisor PASID not supported\n",
107840483774SLu Baolu 				iommu->name);
107940483774SLu Baolu 			return ERR_PTR(-EOPNOTSUPP);
108040483774SLu Baolu 		}
108140483774SLu Baolu 
108240483774SLu Baolu 		if (mm) {
108340483774SLu Baolu 			dev_err(dev, "%s: Supervisor PASID with user provided mm\n",
108440483774SLu Baolu 				iommu->name);
108540483774SLu Baolu 			return ERR_PTR(-EINVAL);
108640483774SLu Baolu 		}
108740483774SLu Baolu 
108840483774SLu Baolu 		mm = &init_mm;
108940483774SLu Baolu 	}
109040483774SLu Baolu 
109140483774SLu Baolu 	mutex_lock(&pasid_mutex);
109240483774SLu Baolu 	ret = intel_svm_alloc_pasid(dev, mm, flags);
109340483774SLu Baolu 	if (ret) {
109440483774SLu Baolu 		mutex_unlock(&pasid_mutex);
109540483774SLu Baolu 		return ERR_PTR(ret);
109640483774SLu Baolu 	}
109740483774SLu Baolu 
109840483774SLu Baolu 	sva = intel_svm_bind_mm(iommu, dev, mm, flags);
109940483774SLu Baolu 	if (IS_ERR_OR_NULL(sva))
110040483774SLu Baolu 		intel_svm_free_pasid(mm);
1101672cf6dfSJoerg Roedel 	mutex_unlock(&pasid_mutex);
1102672cf6dfSJoerg Roedel 
1103672cf6dfSJoerg Roedel 	return sva;
1104672cf6dfSJoerg Roedel }
1105672cf6dfSJoerg Roedel 
1106672cf6dfSJoerg Roedel void intel_svm_unbind(struct iommu_sva *sva)
1107672cf6dfSJoerg Roedel {
110840483774SLu Baolu 	struct intel_svm_dev *sdev = to_intel_svm_dev(sva);
1109672cf6dfSJoerg Roedel 
1110672cf6dfSJoerg Roedel 	mutex_lock(&pasid_mutex);
1111672cf6dfSJoerg Roedel 	intel_svm_unbind_mm(sdev->dev, sdev->pasid);
1112672cf6dfSJoerg Roedel 	mutex_unlock(&pasid_mutex);
1113672cf6dfSJoerg Roedel }
1114672cf6dfSJoerg Roedel 
1115c7b6bac9SFenghua Yu u32 intel_svm_get_pasid(struct iommu_sva *sva)
1116672cf6dfSJoerg Roedel {
1117672cf6dfSJoerg Roedel 	struct intel_svm_dev *sdev;
1118c7b6bac9SFenghua Yu 	u32 pasid;
1119672cf6dfSJoerg Roedel 
1120672cf6dfSJoerg Roedel 	mutex_lock(&pasid_mutex);
1121672cf6dfSJoerg Roedel 	sdev = to_intel_svm_dev(sva);
1122672cf6dfSJoerg Roedel 	pasid = sdev->pasid;
1123672cf6dfSJoerg Roedel 	mutex_unlock(&pasid_mutex);
1124672cf6dfSJoerg Roedel 
1125672cf6dfSJoerg Roedel 	return pasid;
1126672cf6dfSJoerg Roedel }
11278b737121SLu Baolu 
11288b737121SLu Baolu int intel_svm_page_response(struct device *dev,
11298b737121SLu Baolu 			    struct iommu_fault_event *evt,
11308b737121SLu Baolu 			    struct iommu_page_response *msg)
11318b737121SLu Baolu {
11328b737121SLu Baolu 	struct iommu_fault_page_request *prm;
11338b737121SLu Baolu 	struct intel_svm_dev *sdev = NULL;
11348b737121SLu Baolu 	struct intel_svm *svm = NULL;
11358b737121SLu Baolu 	struct intel_iommu *iommu;
11368b737121SLu Baolu 	bool private_present;
11378b737121SLu Baolu 	bool pasid_present;
11388b737121SLu Baolu 	bool last_page;
11398b737121SLu Baolu 	u8 bus, devfn;
11408b737121SLu Baolu 	int ret = 0;
11418b737121SLu Baolu 	u16 sid;
11428b737121SLu Baolu 
11438b737121SLu Baolu 	if (!dev || !dev_is_pci(dev))
11448b737121SLu Baolu 		return -ENODEV;
11458b737121SLu Baolu 
11468b737121SLu Baolu 	iommu = device_to_iommu(dev, &bus, &devfn);
11478b737121SLu Baolu 	if (!iommu)
11488b737121SLu Baolu 		return -ENODEV;
11498b737121SLu Baolu 
11508b737121SLu Baolu 	if (!msg || !evt)
11518b737121SLu Baolu 		return -EINVAL;
11528b737121SLu Baolu 
11538b737121SLu Baolu 	mutex_lock(&pasid_mutex);
11548b737121SLu Baolu 
11558b737121SLu Baolu 	prm = &evt->fault.prm;
11568b737121SLu Baolu 	sid = PCI_DEVID(bus, devfn);
11578b737121SLu Baolu 	pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
11588b737121SLu Baolu 	private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
11598b737121SLu Baolu 	last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
11608b737121SLu Baolu 
11618b737121SLu Baolu 	if (!pasid_present) {
11628b737121SLu Baolu 		ret = -EINVAL;
11638b737121SLu Baolu 		goto out;
11648b737121SLu Baolu 	}
11658b737121SLu Baolu 
11668b737121SLu Baolu 	if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
11678b737121SLu Baolu 		ret = -EINVAL;
11688b737121SLu Baolu 		goto out;
11698b737121SLu Baolu 	}
11708b737121SLu Baolu 
11718b737121SLu Baolu 	ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
11728b737121SLu Baolu 	if (ret || !sdev) {
11738b737121SLu Baolu 		ret = -ENODEV;
11748b737121SLu Baolu 		goto out;
11758b737121SLu Baolu 	}
11768b737121SLu Baolu 
11778b737121SLu Baolu 	/*
11788b737121SLu Baolu 	 * For responses from userspace, need to make sure that the
11798b737121SLu Baolu 	 * pasid has been bound to its mm.
11808b737121SLu Baolu 	 */
11818b737121SLu Baolu 	if (svm->flags & SVM_FLAG_GUEST_MODE) {
11828b737121SLu Baolu 		struct mm_struct *mm;
11838b737121SLu Baolu 
11848b737121SLu Baolu 		mm = get_task_mm(current);
11858b737121SLu Baolu 		if (!mm) {
11868b737121SLu Baolu 			ret = -EINVAL;
11878b737121SLu Baolu 			goto out;
11888b737121SLu Baolu 		}
11898b737121SLu Baolu 
11908b737121SLu Baolu 		if (mm != svm->mm) {
11918b737121SLu Baolu 			ret = -ENODEV;
11928b737121SLu Baolu 			mmput(mm);
11938b737121SLu Baolu 			goto out;
11948b737121SLu Baolu 		}
11958b737121SLu Baolu 
11968b737121SLu Baolu 		mmput(mm);
11978b737121SLu Baolu 	}
11988b737121SLu Baolu 
11998b737121SLu Baolu 	/*
12008b737121SLu Baolu 	 * Per VT-d spec. v3.0 ch7.7, system software must respond
12018b737121SLu Baolu 	 * with page group response if private data is present (PDP)
12028b737121SLu Baolu 	 * or last page in group (LPIG) bit is set. This is an
12038b737121SLu Baolu 	 * additional VT-d requirement beyond PCI ATS spec.
12048b737121SLu Baolu 	 */
12058b737121SLu Baolu 	if (last_page || private_present) {
12068b737121SLu Baolu 		struct qi_desc desc;
12078b737121SLu Baolu 
12088b737121SLu Baolu 		desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
12098b737121SLu Baolu 				QI_PGRP_PASID_P(pasid_present) |
12108b737121SLu Baolu 				QI_PGRP_PDP(private_present) |
12118b737121SLu Baolu 				QI_PGRP_RESP_CODE(msg->code) |
12128b737121SLu Baolu 				QI_PGRP_RESP_TYPE;
12138b737121SLu Baolu 		desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
12148b737121SLu Baolu 		desc.qw2 = 0;
12158b737121SLu Baolu 		desc.qw3 = 0;
12168b737121SLu Baolu 		if (private_present)
12178b737121SLu Baolu 			memcpy(&desc.qw2, prm->private_data,
12188b737121SLu Baolu 			       sizeof(prm->private_data));
12198b737121SLu Baolu 
12208b737121SLu Baolu 		qi_submit_sync(iommu, &desc, 1, 0);
12218b737121SLu Baolu 	}
12228b737121SLu Baolu out:
12238b737121SLu Baolu 	mutex_unlock(&pasid_mutex);
12248b737121SLu Baolu 	return ret;
12258b737121SLu Baolu }
1226