of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / misc / cxl / context.c
blob2faa1270d085b15f92e185f8f389f5790390fbef
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 <asm/cputable.h>
21 #include <asm/current.h>
22 #include <asm/copro.h>
24 #include "cxl.h"
27 * Allocates space for a CXL context.
29 struct cxl_context *cxl_context_alloc(void)
31 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
35 * Initialises a CXL context.
37 int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
38 struct address_space *mapping)
40 int i;
42 spin_lock_init(&ctx->sste_lock);
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 = mapping;
50 * Allocate the segment table before we put it in the IDR so that we
51 * can always access it when dereferenced from IDR. For the same
52 * reason, the segment table is only destroyed after the context is
53 * removed from the IDR. Access to this in the IOCTL is protected by
54 * Linux filesytem symantics (can't IOCTL until open is complete).
56 i = cxl_alloc_sst(ctx);
57 if (i)
58 return i;
60 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
62 init_waitqueue_head(&ctx->wq);
63 spin_lock_init(&ctx->lock);
65 ctx->irq_bitmap = NULL;
66 ctx->pending_irq = false;
67 ctx->pending_fault = false;
68 ctx->pending_afu_err = false;
71 * When we have to destroy all contexts in cxl_context_detach_all() we
72 * end up with afu_release_irqs() called from inside a
73 * idr_for_each_entry(). Hence we need to make sure that anything
74 * dereferenced from this IDR is ok before we allocate the IDR here.
75 * This clears out the IRQ ranges to ensure this.
77 for (i = 0; i < CXL_IRQ_RANGES; i++)
78 ctx->irqs.range[i] = 0;
80 mutex_init(&ctx->status_mutex);
82 ctx->status = OPENED;
85 * Allocating IDR! We better make sure everything's setup that
86 * dereferences from it.
88 mutex_lock(&afu->contexts_lock);
89 idr_preload(GFP_KERNEL);
90 i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
91 ctx->afu->num_procs, GFP_NOWAIT);
92 idr_preload_end();
93 mutex_unlock(&afu->contexts_lock);
94 if (i < 0)
95 return i;
97 ctx->pe = i;
98 ctx->elem = &ctx->afu->spa[i];
99 ctx->pe_inserted = false;
100 return 0;
103 static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
105 struct cxl_context *ctx = vma->vm_file->private_data;
106 unsigned long address = (unsigned long)vmf->virtual_address;
107 u64 area, offset;
109 offset = vmf->pgoff << PAGE_SHIFT;
111 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
112 __func__, ctx->pe, address, offset);
114 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
115 area = ctx->afu->psn_phys;
116 if (offset >= ctx->afu->adapter->ps_size)
117 return VM_FAULT_SIGBUS;
118 } else {
119 area = ctx->psn_phys;
120 if (offset >= ctx->psn_size)
121 return VM_FAULT_SIGBUS;
124 mutex_lock(&ctx->status_mutex);
126 if (ctx->status != STARTED) {
127 mutex_unlock(&ctx->status_mutex);
128 pr_devel("%s: Context not started, failing problem state access\n", __func__);
129 if (ctx->mmio_err_ff) {
130 if (!ctx->ff_page) {
131 ctx->ff_page = alloc_page(GFP_USER);
132 if (!ctx->ff_page)
133 return VM_FAULT_OOM;
134 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
136 get_page(ctx->ff_page);
137 vmf->page = ctx->ff_page;
138 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
139 return 0;
141 return VM_FAULT_SIGBUS;
144 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
146 mutex_unlock(&ctx->status_mutex);
148 return VM_FAULT_NOPAGE;
151 static const struct vm_operations_struct cxl_mmap_vmops = {
152 .fault = cxl_mmap_fault,
156 * Map a per-context mmio space into the given vma.
158 int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
160 u64 start = vma->vm_pgoff << PAGE_SHIFT;
161 u64 len = vma->vm_end - vma->vm_start;
163 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
164 if (start + len > ctx->afu->adapter->ps_size)
165 return -EINVAL;
166 } else {
167 if (start + len > ctx->psn_size)
168 return -EINVAL;
171 if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
172 /* make sure there is a valid per process space for this AFU */
173 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
174 pr_devel("AFU doesn't support mmio space\n");
175 return -EINVAL;
178 /* Can't mmap until the AFU is enabled */
179 if (!ctx->afu->enabled)
180 return -EBUSY;
183 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
184 ctx->psn_phys, ctx->pe , ctx->master);
186 vma->vm_flags |= VM_IO | VM_PFNMAP;
187 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
188 vma->vm_ops = &cxl_mmap_vmops;
189 return 0;
193 * Detach a context from the hardware. This disables interrupts and doesn't
194 * return until all outstanding interrupts for this context have completed. The
195 * hardware should no longer access *ctx after this has returned.
197 int __detach_context(struct cxl_context *ctx)
199 enum cxl_context_status status;
201 mutex_lock(&ctx->status_mutex);
202 status = ctx->status;
203 ctx->status = CLOSED;
204 mutex_unlock(&ctx->status_mutex);
205 if (status != STARTED)
206 return -EBUSY;
208 /* Only warn if we detached while the link was OK.
209 * If detach fails when hw is down, we don't care.
211 WARN_ON(cxl_detach_process(ctx) &&
212 cxl_adapter_link_ok(ctx->afu->adapter));
213 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
214 put_pid(ctx->pid);
215 cxl_ctx_put();
216 return 0;
220 * Detach the given context from the AFU. This doesn't actually
221 * free the context but it should stop the context running in hardware
222 * (ie. prevent this context from generating any further interrupts
223 * so that it can be freed).
225 void cxl_context_detach(struct cxl_context *ctx)
227 int rc;
229 rc = __detach_context(ctx);
230 if (rc)
231 return;
233 afu_release_irqs(ctx, ctx);
234 wake_up_all(&ctx->wq);
238 * Detach all contexts on the given AFU.
240 void cxl_context_detach_all(struct cxl_afu *afu)
242 struct cxl_context *ctx;
243 int tmp;
245 mutex_lock(&afu->contexts_lock);
246 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
248 * Anything done in here needs to be setup before the IDR is
249 * created and torn down after the IDR removed
251 cxl_context_detach(ctx);
254 * We are force detaching - remove any active PSA mappings so
255 * userspace cannot interfere with the card if it comes back.
256 * Easiest way to exercise this is to unbind and rebind the
257 * driver via sysfs while it is in use.
259 mutex_lock(&ctx->mapping_lock);
260 if (ctx->mapping)
261 unmap_mapping_range(ctx->mapping, 0, 0, 1);
262 mutex_unlock(&ctx->mapping_lock);
264 mutex_unlock(&afu->contexts_lock);
267 static void reclaim_ctx(struct rcu_head *rcu)
269 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
271 free_page((u64)ctx->sstp);
272 if (ctx->ff_page)
273 __free_page(ctx->ff_page);
274 ctx->sstp = NULL;
275 if (ctx->kernelapi)
276 kfree(ctx->mapping);
278 if (ctx->irq_bitmap)
279 kfree(ctx->irq_bitmap);
281 kfree(ctx);
284 void cxl_context_free(struct cxl_context *ctx)
286 mutex_lock(&ctx->afu->contexts_lock);
287 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
288 mutex_unlock(&ctx->afu->contexts_lock);
289 call_rcu(&ctx->rcu, reclaim_ctx);