Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / cxl / context.c
blobc6ec872800a204c608827704755b773ee8c5cbe8
1 /*
2 * Copyright 2014 IBM Corp.
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 */
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>
26 #include "cxl.h"
29 * Allocates space for a CXL context.
31 struct cxl_context *cxl_context_alloc(void)
33 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
37 * Initialises a CXL context.
39 int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
41 int i;
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;
51 if (cxl_is_power8()) {
52 spin_lock_init(&ctx->sste_lock);
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).
61 i = cxl_alloc_sst(ctx);
62 if (i)
63 return i;
66 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
68 init_waitqueue_head(&ctx->wq);
69 spin_lock_init(&ctx->lock);
71 ctx->irq_bitmap = NULL;
72 ctx->pending_irq = false;
73 ctx->pending_fault = false;
74 ctx->pending_afu_err = false;
76 INIT_LIST_HEAD(&ctx->irq_names);
77 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
80 * When we have to destroy all contexts in cxl_context_detach_all() we
81 * end up with afu_release_irqs() called from inside a
82 * idr_for_each_entry(). Hence we need to make sure that anything
83 * dereferenced from this IDR is ok before we allocate the IDR here.
84 * This clears out the IRQ ranges to ensure this.
86 for (i = 0; i < CXL_IRQ_RANGES; i++)
87 ctx->irqs.range[i] = 0;
89 mutex_init(&ctx->status_mutex);
91 ctx->status = OPENED;
94 * Allocating IDR! We better make sure everything's setup that
95 * dereferences from it.
97 mutex_lock(&afu->contexts_lock);
98 idr_preload(GFP_KERNEL);
99 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
100 ctx->afu->num_procs, GFP_NOWAIT);
101 idr_preload_end();
102 mutex_unlock(&afu->contexts_lock);
103 if (i < 0)
104 return i;
106 ctx->pe = i;
107 if (cpu_has_feature(CPU_FTR_HVMODE)) {
108 ctx->elem = &ctx->afu->native->spa[i];
109 ctx->external_pe = ctx->pe;
110 } else {
111 ctx->external_pe = -1; /* assigned when attaching */
113 ctx->pe_inserted = false;
116 * take a ref on the afu so that it stays alive at-least till
117 * this context is reclaimed inside reclaim_ctx.
119 cxl_afu_get(afu);
120 return 0;
123 void cxl_context_set_mapping(struct cxl_context *ctx,
124 struct address_space *mapping)
126 mutex_lock(&ctx->mapping_lock);
127 ctx->mapping = mapping;
128 mutex_unlock(&ctx->mapping_lock);
131 static vm_fault_t cxl_mmap_fault(struct vm_fault *vmf)
133 struct vm_area_struct *vma = vmf->vma;
134 struct cxl_context *ctx = vma->vm_file->private_data;
135 u64 area, offset;
136 vm_fault_t ret;
138 offset = vmf->pgoff << PAGE_SHIFT;
140 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
141 __func__, ctx->pe, vmf->address, offset);
143 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
144 area = ctx->afu->psn_phys;
145 if (offset >= ctx->afu->adapter->ps_size)
146 return VM_FAULT_SIGBUS;
147 } else {
148 area = ctx->psn_phys;
149 if (offset >= ctx->psn_size)
150 return VM_FAULT_SIGBUS;
153 mutex_lock(&ctx->status_mutex);
155 if (ctx->status != STARTED) {
156 mutex_unlock(&ctx->status_mutex);
157 pr_devel("%s: Context not started, failing problem state access\n", __func__);
158 if (ctx->mmio_err_ff) {
159 if (!ctx->ff_page) {
160 ctx->ff_page = alloc_page(GFP_USER);
161 if (!ctx->ff_page)
162 return VM_FAULT_OOM;
163 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
165 get_page(ctx->ff_page);
166 vmf->page = ctx->ff_page;
167 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
168 return 0;
170 return VM_FAULT_SIGBUS;
173 ret = vmf_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
175 mutex_unlock(&ctx->status_mutex);
177 return ret;
180 static const struct vm_operations_struct cxl_mmap_vmops = {
181 .fault = cxl_mmap_fault,
185 * Map a per-context mmio space into the given vma.
187 int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
189 u64 start = vma->vm_pgoff << PAGE_SHIFT;
190 u64 len = vma->vm_end - vma->vm_start;
192 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
193 if (start + len > ctx->afu->adapter->ps_size)
194 return -EINVAL;
196 if (cxl_is_power9()) {
198 * Make sure there is a valid problem state
199 * area space for this AFU.
201 if (ctx->master && !ctx->afu->psa) {
202 pr_devel("AFU doesn't support mmio space\n");
203 return -EINVAL;
206 /* Can't mmap until the AFU is enabled */
207 if (!ctx->afu->enabled)
208 return -EBUSY;
210 } else {
211 if (start + len > ctx->psn_size)
212 return -EINVAL;
214 /* Make sure there is a valid per process space for this AFU */
215 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
216 pr_devel("AFU doesn't support mmio space\n");
217 return -EINVAL;
220 /* Can't mmap until the AFU is enabled */
221 if (!ctx->afu->enabled)
222 return -EBUSY;
225 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
226 ctx->psn_phys, ctx->pe , ctx->master);
228 vma->vm_flags |= VM_IO | VM_PFNMAP;
229 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
230 vma->vm_ops = &cxl_mmap_vmops;
231 return 0;
235 * Detach a context from the hardware. This disables interrupts and doesn't
236 * return until all outstanding interrupts for this context have completed. The
237 * hardware should no longer access *ctx after this has returned.
239 int __detach_context(struct cxl_context *ctx)
241 enum cxl_context_status status;
243 mutex_lock(&ctx->status_mutex);
244 status = ctx->status;
245 ctx->status = CLOSED;
246 mutex_unlock(&ctx->status_mutex);
247 if (status != STARTED)
248 return -EBUSY;
250 /* Only warn if we detached while the link was OK.
251 * If detach fails when hw is down, we don't care.
253 WARN_ON(cxl_ops->detach_process(ctx) &&
254 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
255 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
258 * Wait until no further interrupts are presented by the PSL
259 * for this context.
261 if (cxl_ops->irq_wait)
262 cxl_ops->irq_wait(ctx);
264 /* release the reference to the group leader and mm handling pid */
265 put_pid(ctx->pid);
267 cxl_ctx_put();
269 /* Decrease the attached context count on the adapter */
270 cxl_adapter_context_put(ctx->afu->adapter);
272 /* Decrease the mm count on the context */
273 cxl_context_mm_count_put(ctx);
274 if (ctx->mm)
275 mm_context_remove_copro(ctx->mm);
276 ctx->mm = NULL;
278 return 0;
282 * Detach the given context from the AFU. This doesn't actually
283 * free the context but it should stop the context running in hardware
284 * (ie. prevent this context from generating any further interrupts
285 * so that it can be freed).
287 void cxl_context_detach(struct cxl_context *ctx)
289 int rc;
291 rc = __detach_context(ctx);
292 if (rc)
293 return;
295 afu_release_irqs(ctx, ctx);
296 wake_up_all(&ctx->wq);
300 * Detach all contexts on the given AFU.
302 void cxl_context_detach_all(struct cxl_afu *afu)
304 struct cxl_context *ctx;
305 int tmp;
307 mutex_lock(&afu->contexts_lock);
308 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
310 * Anything done in here needs to be setup before the IDR is
311 * created and torn down after the IDR removed
313 cxl_context_detach(ctx);
316 * We are force detaching - remove any active PSA mappings so
317 * userspace cannot interfere with the card if it comes back.
318 * Easiest way to exercise this is to unbind and rebind the
319 * driver via sysfs while it is in use.
321 mutex_lock(&ctx->mapping_lock);
322 if (ctx->mapping)
323 unmap_mapping_range(ctx->mapping, 0, 0, 1);
324 mutex_unlock(&ctx->mapping_lock);
326 mutex_unlock(&afu->contexts_lock);
329 static void reclaim_ctx(struct rcu_head *rcu)
331 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
333 if (cxl_is_power8())
334 free_page((u64)ctx->sstp);
335 if (ctx->ff_page)
336 __free_page(ctx->ff_page);
337 ctx->sstp = NULL;
339 kfree(ctx->irq_bitmap);
341 /* Drop ref to the afu device taken during cxl_context_init */
342 cxl_afu_put(ctx->afu);
344 kfree(ctx);
347 void cxl_context_free(struct cxl_context *ctx)
349 if (ctx->kernelapi && ctx->mapping)
350 cxl_release_mapping(ctx);
351 mutex_lock(&ctx->afu->contexts_lock);
352 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
353 mutex_unlock(&ctx->afu->contexts_lock);
354 call_rcu(&ctx->rcu, reclaim_ctx);
357 void cxl_context_mm_count_get(struct cxl_context *ctx)
359 if (ctx->mm)
360 atomic_inc(&ctx->mm->mm_count);
363 void cxl_context_mm_count_put(struct cxl_context *ctx)
365 if (ctx->mm)
366 mmdrop(ctx->mm);