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