1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2014 IBM Corp.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/module.h>
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/bitmap.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/pid.h>
11*4882a593Smuzhiyun #include <linux/fs.h>
12*4882a593Smuzhiyun #include <linux/mm.h>
13*4882a593Smuzhiyun #include <linux/debugfs.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/idr.h>
16*4882a593Smuzhiyun #include <linux/sched/mm.h>
17*4882a593Smuzhiyun #include <linux/mmu_context.h>
18*4882a593Smuzhiyun #include <asm/cputable.h>
19*4882a593Smuzhiyun #include <asm/current.h>
20*4882a593Smuzhiyun #include <asm/copro.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "cxl.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * Allocates space for a CXL context.
26*4882a593Smuzhiyun */
cxl_context_alloc(void)27*4882a593Smuzhiyun struct cxl_context *cxl_context_alloc(void)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Initialises a CXL context.
34*4882a593Smuzhiyun */
cxl_context_init(struct cxl_context * ctx,struct cxl_afu * afu,bool master)35*4882a593Smuzhiyun int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun int i;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun ctx->afu = afu;
40*4882a593Smuzhiyun ctx->master = master;
41*4882a593Smuzhiyun ctx->pid = NULL; /* Set in start work ioctl */
42*4882a593Smuzhiyun mutex_init(&ctx->mapping_lock);
43*4882a593Smuzhiyun ctx->mapping = NULL;
44*4882a593Smuzhiyun ctx->tidr = 0;
45*4882a593Smuzhiyun ctx->assign_tidr = false;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (cxl_is_power8()) {
48*4882a593Smuzhiyun spin_lock_init(&ctx->sste_lock);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * Allocate the segment table before we put it in the IDR so that we
52*4882a593Smuzhiyun * can always access it when dereferenced from IDR. For the same
53*4882a593Smuzhiyun * reason, the segment table is only destroyed after the context is
54*4882a593Smuzhiyun * removed from the IDR. Access to this in the IOCTL is protected by
55*4882a593Smuzhiyun * Linux filesytem symantics (can't IOCTL until open is complete).
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun i = cxl_alloc_sst(ctx);
58*4882a593Smuzhiyun if (i)
59*4882a593Smuzhiyun return i;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun INIT_WORK(&ctx->fault_work, cxl_handle_fault);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun init_waitqueue_head(&ctx->wq);
65*4882a593Smuzhiyun spin_lock_init(&ctx->lock);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun ctx->irq_bitmap = NULL;
68*4882a593Smuzhiyun ctx->pending_irq = false;
69*4882a593Smuzhiyun ctx->pending_fault = false;
70*4882a593Smuzhiyun ctx->pending_afu_err = false;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun INIT_LIST_HEAD(&ctx->irq_names);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * When we have to destroy all contexts in cxl_context_detach_all() we
76*4882a593Smuzhiyun * end up with afu_release_irqs() called from inside a
77*4882a593Smuzhiyun * idr_for_each_entry(). Hence we need to make sure that anything
78*4882a593Smuzhiyun * dereferenced from this IDR is ok before we allocate the IDR here.
79*4882a593Smuzhiyun * This clears out the IRQ ranges to ensure this.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun for (i = 0; i < CXL_IRQ_RANGES; i++)
82*4882a593Smuzhiyun ctx->irqs.range[i] = 0;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun mutex_init(&ctx->status_mutex);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun ctx->status = OPENED;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * Allocating IDR! We better make sure everything's setup that
90*4882a593Smuzhiyun * dereferences from it.
91*4882a593Smuzhiyun */
92*4882a593Smuzhiyun mutex_lock(&afu->contexts_lock);
93*4882a593Smuzhiyun idr_preload(GFP_KERNEL);
94*4882a593Smuzhiyun i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
95*4882a593Smuzhiyun ctx->afu->num_procs, GFP_NOWAIT);
96*4882a593Smuzhiyun idr_preload_end();
97*4882a593Smuzhiyun mutex_unlock(&afu->contexts_lock);
98*4882a593Smuzhiyun if (i < 0)
99*4882a593Smuzhiyun return i;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun ctx->pe = i;
102*4882a593Smuzhiyun if (cpu_has_feature(CPU_FTR_HVMODE)) {
103*4882a593Smuzhiyun ctx->elem = &ctx->afu->native->spa[i];
104*4882a593Smuzhiyun ctx->external_pe = ctx->pe;
105*4882a593Smuzhiyun } else {
106*4882a593Smuzhiyun ctx->external_pe = -1; /* assigned when attaching */
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun ctx->pe_inserted = false;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * take a ref on the afu so that it stays alive at-least till
112*4882a593Smuzhiyun * this context is reclaimed inside reclaim_ctx.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun cxl_afu_get(afu);
115*4882a593Smuzhiyun return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
cxl_context_set_mapping(struct cxl_context * ctx,struct address_space * mapping)118*4882a593Smuzhiyun void cxl_context_set_mapping(struct cxl_context *ctx,
119*4882a593Smuzhiyun struct address_space *mapping)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun mutex_lock(&ctx->mapping_lock);
122*4882a593Smuzhiyun ctx->mapping = mapping;
123*4882a593Smuzhiyun mutex_unlock(&ctx->mapping_lock);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
cxl_mmap_fault(struct vm_fault * vmf)126*4882a593Smuzhiyun static vm_fault_t cxl_mmap_fault(struct vm_fault *vmf)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct vm_area_struct *vma = vmf->vma;
129*4882a593Smuzhiyun struct cxl_context *ctx = vma->vm_file->private_data;
130*4882a593Smuzhiyun u64 area, offset;
131*4882a593Smuzhiyun vm_fault_t ret;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun offset = vmf->pgoff << PAGE_SHIFT;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
136*4882a593Smuzhiyun __func__, ctx->pe, vmf->address, offset);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
139*4882a593Smuzhiyun area = ctx->afu->psn_phys;
140*4882a593Smuzhiyun if (offset >= ctx->afu->adapter->ps_size)
141*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
142*4882a593Smuzhiyun } else {
143*4882a593Smuzhiyun area = ctx->psn_phys;
144*4882a593Smuzhiyun if (offset >= ctx->psn_size)
145*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun mutex_lock(&ctx->status_mutex);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun if (ctx->status != STARTED) {
151*4882a593Smuzhiyun mutex_unlock(&ctx->status_mutex);
152*4882a593Smuzhiyun pr_devel("%s: Context not started, failing problem state access\n", __func__);
153*4882a593Smuzhiyun if (ctx->mmio_err_ff) {
154*4882a593Smuzhiyun if (!ctx->ff_page) {
155*4882a593Smuzhiyun ctx->ff_page = alloc_page(GFP_USER);
156*4882a593Smuzhiyun if (!ctx->ff_page)
157*4882a593Smuzhiyun return VM_FAULT_OOM;
158*4882a593Smuzhiyun memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun get_page(ctx->ff_page);
161*4882a593Smuzhiyun vmf->page = ctx->ff_page;
162*4882a593Smuzhiyun vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
163*4882a593Smuzhiyun return 0;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun return VM_FAULT_SIGBUS;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun ret = vmf_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun mutex_unlock(&ctx->status_mutex);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun return ret;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun static const struct vm_operations_struct cxl_mmap_vmops = {
176*4882a593Smuzhiyun .fault = cxl_mmap_fault,
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun * Map a per-context mmio space into the given vma.
181*4882a593Smuzhiyun */
cxl_context_iomap(struct cxl_context * ctx,struct vm_area_struct * vma)182*4882a593Smuzhiyun int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun u64 start = vma->vm_pgoff << PAGE_SHIFT;
185*4882a593Smuzhiyun u64 len = vma->vm_end - vma->vm_start;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
188*4882a593Smuzhiyun if (start + len > ctx->afu->adapter->ps_size)
189*4882a593Smuzhiyun return -EINVAL;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (cxl_is_power9()) {
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun * Make sure there is a valid problem state
194*4882a593Smuzhiyun * area space for this AFU.
195*4882a593Smuzhiyun */
196*4882a593Smuzhiyun if (ctx->master && !ctx->afu->psa) {
197*4882a593Smuzhiyun pr_devel("AFU doesn't support mmio space\n");
198*4882a593Smuzhiyun return -EINVAL;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /* Can't mmap until the AFU is enabled */
202*4882a593Smuzhiyun if (!ctx->afu->enabled)
203*4882a593Smuzhiyun return -EBUSY;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun } else {
206*4882a593Smuzhiyun if (start + len > ctx->psn_size)
207*4882a593Smuzhiyun return -EINVAL;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* Make sure there is a valid per process space for this AFU */
210*4882a593Smuzhiyun if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
211*4882a593Smuzhiyun pr_devel("AFU doesn't support mmio space\n");
212*4882a593Smuzhiyun return -EINVAL;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* Can't mmap until the AFU is enabled */
216*4882a593Smuzhiyun if (!ctx->afu->enabled)
217*4882a593Smuzhiyun return -EBUSY;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
221*4882a593Smuzhiyun ctx->psn_phys, ctx->pe , ctx->master);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun vma->vm_flags |= VM_IO | VM_PFNMAP;
224*4882a593Smuzhiyun vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
225*4882a593Smuzhiyun vma->vm_ops = &cxl_mmap_vmops;
226*4882a593Smuzhiyun return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /*
230*4882a593Smuzhiyun * Detach a context from the hardware. This disables interrupts and doesn't
231*4882a593Smuzhiyun * return until all outstanding interrupts for this context have completed. The
232*4882a593Smuzhiyun * hardware should no longer access *ctx after this has returned.
233*4882a593Smuzhiyun */
__detach_context(struct cxl_context * ctx)234*4882a593Smuzhiyun int __detach_context(struct cxl_context *ctx)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun enum cxl_context_status status;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun mutex_lock(&ctx->status_mutex);
239*4882a593Smuzhiyun status = ctx->status;
240*4882a593Smuzhiyun ctx->status = CLOSED;
241*4882a593Smuzhiyun mutex_unlock(&ctx->status_mutex);
242*4882a593Smuzhiyun if (status != STARTED)
243*4882a593Smuzhiyun return -EBUSY;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* Only warn if we detached while the link was OK.
246*4882a593Smuzhiyun * If detach fails when hw is down, we don't care.
247*4882a593Smuzhiyun */
248*4882a593Smuzhiyun WARN_ON(cxl_ops->detach_process(ctx) &&
249*4882a593Smuzhiyun cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
250*4882a593Smuzhiyun flush_work(&ctx->fault_work); /* Only needed for dedicated process */
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * Wait until no further interrupts are presented by the PSL
254*4882a593Smuzhiyun * for this context.
255*4882a593Smuzhiyun */
256*4882a593Smuzhiyun if (cxl_ops->irq_wait)
257*4882a593Smuzhiyun cxl_ops->irq_wait(ctx);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* release the reference to the group leader and mm handling pid */
260*4882a593Smuzhiyun put_pid(ctx->pid);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun cxl_ctx_put();
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /* Decrease the attached context count on the adapter */
265*4882a593Smuzhiyun cxl_adapter_context_put(ctx->afu->adapter);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* Decrease the mm count on the context */
268*4882a593Smuzhiyun cxl_context_mm_count_put(ctx);
269*4882a593Smuzhiyun if (ctx->mm)
270*4882a593Smuzhiyun mm_context_remove_copro(ctx->mm);
271*4882a593Smuzhiyun ctx->mm = NULL;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun * Detach the given context from the AFU. This doesn't actually
278*4882a593Smuzhiyun * free the context but it should stop the context running in hardware
279*4882a593Smuzhiyun * (ie. prevent this context from generating any further interrupts
280*4882a593Smuzhiyun * so that it can be freed).
281*4882a593Smuzhiyun */
cxl_context_detach(struct cxl_context * ctx)282*4882a593Smuzhiyun void cxl_context_detach(struct cxl_context *ctx)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun int rc;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun rc = __detach_context(ctx);
287*4882a593Smuzhiyun if (rc)
288*4882a593Smuzhiyun return;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun afu_release_irqs(ctx, ctx);
291*4882a593Smuzhiyun wake_up_all(&ctx->wq);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun * Detach all contexts on the given AFU.
296*4882a593Smuzhiyun */
cxl_context_detach_all(struct cxl_afu * afu)297*4882a593Smuzhiyun void cxl_context_detach_all(struct cxl_afu *afu)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct cxl_context *ctx;
300*4882a593Smuzhiyun int tmp;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun mutex_lock(&afu->contexts_lock);
303*4882a593Smuzhiyun idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
304*4882a593Smuzhiyun /*
305*4882a593Smuzhiyun * Anything done in here needs to be setup before the IDR is
306*4882a593Smuzhiyun * created and torn down after the IDR removed
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun cxl_context_detach(ctx);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /*
311*4882a593Smuzhiyun * We are force detaching - remove any active PSA mappings so
312*4882a593Smuzhiyun * userspace cannot interfere with the card if it comes back.
313*4882a593Smuzhiyun * Easiest way to exercise this is to unbind and rebind the
314*4882a593Smuzhiyun * driver via sysfs while it is in use.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun mutex_lock(&ctx->mapping_lock);
317*4882a593Smuzhiyun if (ctx->mapping)
318*4882a593Smuzhiyun unmap_mapping_range(ctx->mapping, 0, 0, 1);
319*4882a593Smuzhiyun mutex_unlock(&ctx->mapping_lock);
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun mutex_unlock(&afu->contexts_lock);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
reclaim_ctx(struct rcu_head * rcu)324*4882a593Smuzhiyun static void reclaim_ctx(struct rcu_head *rcu)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun if (cxl_is_power8())
329*4882a593Smuzhiyun free_page((u64)ctx->sstp);
330*4882a593Smuzhiyun if (ctx->ff_page)
331*4882a593Smuzhiyun __free_page(ctx->ff_page);
332*4882a593Smuzhiyun ctx->sstp = NULL;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun kfree(ctx->irq_bitmap);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /* Drop ref to the afu device taken during cxl_context_init */
337*4882a593Smuzhiyun cxl_afu_put(ctx->afu);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun kfree(ctx);
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
cxl_context_free(struct cxl_context * ctx)342*4882a593Smuzhiyun void cxl_context_free(struct cxl_context *ctx)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun if (ctx->kernelapi && ctx->mapping)
345*4882a593Smuzhiyun cxl_release_mapping(ctx);
346*4882a593Smuzhiyun mutex_lock(&ctx->afu->contexts_lock);
347*4882a593Smuzhiyun idr_remove(&ctx->afu->contexts_idr, ctx->pe);
348*4882a593Smuzhiyun mutex_unlock(&ctx->afu->contexts_lock);
349*4882a593Smuzhiyun call_rcu(&ctx->rcu, reclaim_ctx);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
cxl_context_mm_count_get(struct cxl_context * ctx)352*4882a593Smuzhiyun void cxl_context_mm_count_get(struct cxl_context *ctx)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun if (ctx->mm)
355*4882a593Smuzhiyun mmgrab(ctx->mm);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
cxl_context_mm_count_put(struct cxl_context * ctx)358*4882a593Smuzhiyun void cxl_context_mm_count_put(struct cxl_context *ctx)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun if (ctx->mm)
361*4882a593Smuzhiyun mmdrop(ctx->mm);
362*4882a593Smuzhiyun }
363