xref: /OK3568_Linux_fs/kernel/drivers/misc/ocxl/context.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun // Copyright 2017 IBM Corp.
3*4882a593Smuzhiyun #include <linux/sched/mm.h>
4*4882a593Smuzhiyun #include "trace.h"
5*4882a593Smuzhiyun #include "ocxl_internal.h"
6*4882a593Smuzhiyun 
ocxl_context_alloc(struct ocxl_context ** context,struct ocxl_afu * afu,struct address_space * mapping)7*4882a593Smuzhiyun int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
8*4882a593Smuzhiyun 		struct address_space *mapping)
9*4882a593Smuzhiyun {
10*4882a593Smuzhiyun 	int pasid;
11*4882a593Smuzhiyun 	struct ocxl_context *ctx;
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
14*4882a593Smuzhiyun 	if (!ctx)
15*4882a593Smuzhiyun 		return -ENOMEM;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 	ctx->afu = afu;
18*4882a593Smuzhiyun 	mutex_lock(&afu->contexts_lock);
19*4882a593Smuzhiyun 	pasid = idr_alloc(&afu->contexts_idr, ctx, afu->pasid_base,
20*4882a593Smuzhiyun 			afu->pasid_base + afu->pasid_max, GFP_KERNEL);
21*4882a593Smuzhiyun 	if (pasid < 0) {
22*4882a593Smuzhiyun 		mutex_unlock(&afu->contexts_lock);
23*4882a593Smuzhiyun 		kfree(ctx);
24*4882a593Smuzhiyun 		return pasid;
25*4882a593Smuzhiyun 	}
26*4882a593Smuzhiyun 	afu->pasid_count++;
27*4882a593Smuzhiyun 	mutex_unlock(&afu->contexts_lock);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	ctx->pasid = pasid;
30*4882a593Smuzhiyun 	ctx->status = OPENED;
31*4882a593Smuzhiyun 	mutex_init(&ctx->status_mutex);
32*4882a593Smuzhiyun 	ctx->mapping = mapping;
33*4882a593Smuzhiyun 	mutex_init(&ctx->mapping_lock);
34*4882a593Smuzhiyun 	init_waitqueue_head(&ctx->events_wq);
35*4882a593Smuzhiyun 	mutex_init(&ctx->xsl_error_lock);
36*4882a593Smuzhiyun 	mutex_init(&ctx->irq_lock);
37*4882a593Smuzhiyun 	idr_init(&ctx->irq_idr);
38*4882a593Smuzhiyun 	ctx->tidr = 0;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/*
41*4882a593Smuzhiyun 	 * Keep a reference on the AFU to make sure it's valid for the
42*4882a593Smuzhiyun 	 * duration of the life of the context
43*4882a593Smuzhiyun 	 */
44*4882a593Smuzhiyun 	ocxl_afu_get(afu);
45*4882a593Smuzhiyun 	*context = ctx;
46*4882a593Smuzhiyun 	return 0;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocxl_context_alloc);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * Callback for when a translation fault triggers an error
52*4882a593Smuzhiyun  * data:	a pointer to the context which triggered the fault
53*4882a593Smuzhiyun  * addr:	the address that triggered the error
54*4882a593Smuzhiyun  * dsisr:	the value of the PPC64 dsisr register
55*4882a593Smuzhiyun  */
xsl_fault_error(void * data,u64 addr,u64 dsisr)56*4882a593Smuzhiyun static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct ocxl_context *ctx = (struct ocxl_context *) data;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	mutex_lock(&ctx->xsl_error_lock);
61*4882a593Smuzhiyun 	ctx->xsl_error.addr = addr;
62*4882a593Smuzhiyun 	ctx->xsl_error.dsisr = dsisr;
63*4882a593Smuzhiyun 	ctx->xsl_error.count++;
64*4882a593Smuzhiyun 	mutex_unlock(&ctx->xsl_error_lock);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	wake_up_all(&ctx->events_wq);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
ocxl_context_attach(struct ocxl_context * ctx,u64 amr,struct mm_struct * mm)69*4882a593Smuzhiyun int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	int rc;
72*4882a593Smuzhiyun 	unsigned long pidr = 0;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	// Locks both status & tidr
75*4882a593Smuzhiyun 	mutex_lock(&ctx->status_mutex);
76*4882a593Smuzhiyun 	if (ctx->status != OPENED) {
77*4882a593Smuzhiyun 		rc = -EIO;
78*4882a593Smuzhiyun 		goto out;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if (mm)
82*4882a593Smuzhiyun 		pidr = mm->context.id;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
85*4882a593Smuzhiyun 			      amr, mm, xsl_fault_error, ctx);
86*4882a593Smuzhiyun 	if (rc)
87*4882a593Smuzhiyun 		goto out;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	ctx->status = ATTACHED;
90*4882a593Smuzhiyun out:
91*4882a593Smuzhiyun 	mutex_unlock(&ctx->status_mutex);
92*4882a593Smuzhiyun 	return rc;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocxl_context_attach);
95*4882a593Smuzhiyun 
map_afu_irq(struct vm_area_struct * vma,unsigned long address,u64 offset,struct ocxl_context * ctx)96*4882a593Smuzhiyun static vm_fault_t map_afu_irq(struct vm_area_struct *vma, unsigned long address,
97*4882a593Smuzhiyun 		u64 offset, struct ocxl_context *ctx)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	u64 trigger_addr;
100*4882a593Smuzhiyun 	int irq_id = ocxl_irq_offset_to_id(ctx, offset);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	trigger_addr = ocxl_afu_irq_get_addr(ctx, irq_id);
103*4882a593Smuzhiyun 	if (!trigger_addr)
104*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return vmf_insert_pfn(vma, address, trigger_addr >> PAGE_SHIFT);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
map_pp_mmio(struct vm_area_struct * vma,unsigned long address,u64 offset,struct ocxl_context * ctx)109*4882a593Smuzhiyun static vm_fault_t map_pp_mmio(struct vm_area_struct *vma, unsigned long address,
110*4882a593Smuzhiyun 		u64 offset, struct ocxl_context *ctx)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	u64 pp_mmio_addr;
113*4882a593Smuzhiyun 	int pasid_off;
114*4882a593Smuzhiyun 	vm_fault_t ret;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (offset >= ctx->afu->config.pp_mmio_stride)
117*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	mutex_lock(&ctx->status_mutex);
120*4882a593Smuzhiyun 	if (ctx->status != ATTACHED) {
121*4882a593Smuzhiyun 		mutex_unlock(&ctx->status_mutex);
122*4882a593Smuzhiyun 		pr_debug("%s: Context not attached, failing mmio mmap\n",
123*4882a593Smuzhiyun 			__func__);
124*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	pasid_off = ctx->pasid - ctx->afu->pasid_base;
128*4882a593Smuzhiyun 	pp_mmio_addr = ctx->afu->pp_mmio_start +
129*4882a593Smuzhiyun 		pasid_off * ctx->afu->config.pp_mmio_stride +
130*4882a593Smuzhiyun 		offset;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	ret = vmf_insert_pfn(vma, address, pp_mmio_addr >> PAGE_SHIFT);
133*4882a593Smuzhiyun 	mutex_unlock(&ctx->status_mutex);
134*4882a593Smuzhiyun 	return ret;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
ocxl_mmap_fault(struct vm_fault * vmf)137*4882a593Smuzhiyun static vm_fault_t ocxl_mmap_fault(struct vm_fault *vmf)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	struct vm_area_struct *vma = vmf->vma;
140*4882a593Smuzhiyun 	struct ocxl_context *ctx = vma->vm_file->private_data;
141*4882a593Smuzhiyun 	u64 offset;
142*4882a593Smuzhiyun 	vm_fault_t ret;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	offset = vmf->pgoff << PAGE_SHIFT;
145*4882a593Smuzhiyun 	pr_debug("%s: pasid %d address 0x%lx offset 0x%llx\n", __func__,
146*4882a593Smuzhiyun 		ctx->pasid, vmf->address, offset);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	if (offset < ctx->afu->irq_base_offset)
149*4882a593Smuzhiyun 		ret = map_pp_mmio(vma, vmf->address, offset, ctx);
150*4882a593Smuzhiyun 	else
151*4882a593Smuzhiyun 		ret = map_afu_irq(vma, vmf->address, offset, ctx);
152*4882a593Smuzhiyun 	return ret;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static const struct vm_operations_struct ocxl_vmops = {
156*4882a593Smuzhiyun 	.fault = ocxl_mmap_fault,
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun 
check_mmap_afu_irq(struct ocxl_context * ctx,struct vm_area_struct * vma)159*4882a593Smuzhiyun static int check_mmap_afu_irq(struct ocxl_context *ctx,
160*4882a593Smuzhiyun 			struct vm_area_struct *vma)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	int irq_id = ocxl_irq_offset_to_id(ctx, vma->vm_pgoff << PAGE_SHIFT);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* only one page */
165*4882a593Smuzhiyun 	if (vma_pages(vma) != 1)
166*4882a593Smuzhiyun 		return -EINVAL;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* check offset validty */
169*4882a593Smuzhiyun 	if (!ocxl_afu_irq_get_addr(ctx, irq_id))
170*4882a593Smuzhiyun 		return -EINVAL;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/*
173*4882a593Smuzhiyun 	 * trigger page should only be accessible in write mode.
174*4882a593Smuzhiyun 	 *
175*4882a593Smuzhiyun 	 * It's a bit theoretical, as a page mmaped with only
176*4882a593Smuzhiyun 	 * PROT_WRITE is currently readable, but it doesn't hurt.
177*4882a593Smuzhiyun 	 */
178*4882a593Smuzhiyun 	if ((vma->vm_flags & VM_READ) || (vma->vm_flags & VM_EXEC) ||
179*4882a593Smuzhiyun 		!(vma->vm_flags & VM_WRITE))
180*4882a593Smuzhiyun 		return -EINVAL;
181*4882a593Smuzhiyun 	vma->vm_flags &= ~(VM_MAYREAD | VM_MAYEXEC);
182*4882a593Smuzhiyun 	return 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
check_mmap_mmio(struct ocxl_context * ctx,struct vm_area_struct * vma)185*4882a593Smuzhiyun static int check_mmap_mmio(struct ocxl_context *ctx,
186*4882a593Smuzhiyun 			struct vm_area_struct *vma)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	if ((vma_pages(vma) + vma->vm_pgoff) >
189*4882a593Smuzhiyun 		(ctx->afu->config.pp_mmio_stride >> PAGE_SHIFT))
190*4882a593Smuzhiyun 		return -EINVAL;
191*4882a593Smuzhiyun 	return 0;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
ocxl_context_mmap(struct ocxl_context * ctx,struct vm_area_struct * vma)194*4882a593Smuzhiyun int ocxl_context_mmap(struct ocxl_context *ctx, struct vm_area_struct *vma)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	int rc;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if ((vma->vm_pgoff << PAGE_SHIFT) < ctx->afu->irq_base_offset)
199*4882a593Smuzhiyun 		rc = check_mmap_mmio(ctx, vma);
200*4882a593Smuzhiyun 	else
201*4882a593Smuzhiyun 		rc = check_mmap_afu_irq(ctx, vma);
202*4882a593Smuzhiyun 	if (rc)
203*4882a593Smuzhiyun 		return rc;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	vma->vm_flags |= VM_IO | VM_PFNMAP;
206*4882a593Smuzhiyun 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
207*4882a593Smuzhiyun 	vma->vm_ops = &ocxl_vmops;
208*4882a593Smuzhiyun 	return 0;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
ocxl_context_detach(struct ocxl_context * ctx)211*4882a593Smuzhiyun int ocxl_context_detach(struct ocxl_context *ctx)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	struct pci_dev *dev;
214*4882a593Smuzhiyun 	int afu_control_pos;
215*4882a593Smuzhiyun 	enum ocxl_context_status status;
216*4882a593Smuzhiyun 	int rc;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	mutex_lock(&ctx->status_mutex);
219*4882a593Smuzhiyun 	status = ctx->status;
220*4882a593Smuzhiyun 	ctx->status = CLOSED;
221*4882a593Smuzhiyun 	mutex_unlock(&ctx->status_mutex);
222*4882a593Smuzhiyun 	if (status != ATTACHED)
223*4882a593Smuzhiyun 		return 0;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	dev = to_pci_dev(ctx->afu->fn->dev.parent);
226*4882a593Smuzhiyun 	afu_control_pos = ctx->afu->config.dvsec_afu_control_pos;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	mutex_lock(&ctx->afu->afu_control_lock);
229*4882a593Smuzhiyun 	rc = ocxl_config_terminate_pasid(dev, afu_control_pos, ctx->pasid);
230*4882a593Smuzhiyun 	mutex_unlock(&ctx->afu->afu_control_lock);
231*4882a593Smuzhiyun 	trace_ocxl_terminate_pasid(ctx->pasid, rc);
232*4882a593Smuzhiyun 	if (rc) {
233*4882a593Smuzhiyun 		/*
234*4882a593Smuzhiyun 		 * If we timeout waiting for the AFU to terminate the
235*4882a593Smuzhiyun 		 * pasid, then it's dangerous to clean up the Process
236*4882a593Smuzhiyun 		 * Element entry in the SPA, as it may be referenced
237*4882a593Smuzhiyun 		 * in the future by the AFU. In which case, we would
238*4882a593Smuzhiyun 		 * checkstop because of an invalid PE access (FIR
239*4882a593Smuzhiyun 		 * register 2, bit 42). So leave the PE
240*4882a593Smuzhiyun 		 * defined. Caller shouldn't free the context so that
241*4882a593Smuzhiyun 		 * PASID remains allocated.
242*4882a593Smuzhiyun 		 *
243*4882a593Smuzhiyun 		 * A link reset will be required to cleanup the AFU
244*4882a593Smuzhiyun 		 * and the SPA.
245*4882a593Smuzhiyun 		 */
246*4882a593Smuzhiyun 		if (rc == -EBUSY)
247*4882a593Smuzhiyun 			return rc;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 	rc = ocxl_link_remove_pe(ctx->afu->fn->link, ctx->pasid);
250*4882a593Smuzhiyun 	if (rc) {
251*4882a593Smuzhiyun 		dev_warn(&dev->dev,
252*4882a593Smuzhiyun 			"Couldn't remove PE entry cleanly: %d\n", rc);
253*4882a593Smuzhiyun 	}
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocxl_context_detach);
257*4882a593Smuzhiyun 
ocxl_context_detach_all(struct ocxl_afu * afu)258*4882a593Smuzhiyun void ocxl_context_detach_all(struct ocxl_afu *afu)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	struct ocxl_context *ctx;
261*4882a593Smuzhiyun 	int tmp;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	mutex_lock(&afu->contexts_lock);
264*4882a593Smuzhiyun 	idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
265*4882a593Smuzhiyun 		ocxl_context_detach(ctx);
266*4882a593Smuzhiyun 		/*
267*4882a593Smuzhiyun 		 * We are force detaching - remove any active mmio
268*4882a593Smuzhiyun 		 * mappings so userspace cannot interfere with the
269*4882a593Smuzhiyun 		 * card if it comes back.  Easiest way to exercise
270*4882a593Smuzhiyun 		 * this is to unbind and rebind the driver via sysfs
271*4882a593Smuzhiyun 		 * while it is in use.
272*4882a593Smuzhiyun 		 */
273*4882a593Smuzhiyun 		mutex_lock(&ctx->mapping_lock);
274*4882a593Smuzhiyun 		if (ctx->mapping)
275*4882a593Smuzhiyun 			unmap_mapping_range(ctx->mapping, 0, 0, 1);
276*4882a593Smuzhiyun 		mutex_unlock(&ctx->mapping_lock);
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 	mutex_unlock(&afu->contexts_lock);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
ocxl_context_free(struct ocxl_context * ctx)281*4882a593Smuzhiyun void ocxl_context_free(struct ocxl_context *ctx)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	mutex_lock(&ctx->afu->contexts_lock);
284*4882a593Smuzhiyun 	ctx->afu->pasid_count--;
285*4882a593Smuzhiyun 	idr_remove(&ctx->afu->contexts_idr, ctx->pasid);
286*4882a593Smuzhiyun 	mutex_unlock(&ctx->afu->contexts_lock);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	ocxl_afu_irq_free_all(ctx);
289*4882a593Smuzhiyun 	idr_destroy(&ctx->irq_idr);
290*4882a593Smuzhiyun 	/* reference to the AFU taken in ocxl_context_alloc() */
291*4882a593Smuzhiyun 	ocxl_afu_put(ctx->afu);
292*4882a593Smuzhiyun 	kfree(ctx);
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocxl_context_free);
295