blob: ff5e7e8cb1d151f32bc04e813b7ba05f531e04d2 [file] [log] [blame]
Ian Munsief204e0b2014-10-08 19:55:02 +11001/*
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 <asm/cputable.h>
21#include <asm/current.h>
22#include <asm/copro.h>
23
24#include "cxl.h"
25
26/*
27 * Allocates space for a CXL context.
28 */
29struct cxl_context *cxl_context_alloc(void)
30{
31 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
32}
33
34/*
35 * Initialises a CXL context.
36 */
Frederic Barratbdecf762016-11-18 23:00:31 +110037int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
Ian Munsief204e0b2014-10-08 19:55:02 +110038{
39 int i;
40
41 spin_lock_init(&ctx->sste_lock);
42 ctx->afu = afu;
43 ctx->master = master;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +053044 ctx->pid = ctx->glpid = NULL; /* Set in start work ioctl */
Ian Munsieb1234292014-12-08 19:18:01 +110045 mutex_init(&ctx->mapping_lock);
Frederic Barratbdecf762016-11-18 23:00:31 +110046 ctx->mapping = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +110047
48 /*
49 * Allocate the segment table before we put it in the IDR so that we
50 * can always access it when dereferenced from IDR. For the same
51 * reason, the segment table is only destroyed after the context is
52 * removed from the IDR. Access to this in the IOCTL is protected by
53 * Linux filesytem symantics (can't IOCTL until open is complete).
54 */
55 i = cxl_alloc_sst(ctx);
56 if (i)
57 return i;
58
59 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
60
61 init_waitqueue_head(&ctx->wq);
62 spin_lock_init(&ctx->lock);
63
64 ctx->irq_bitmap = NULL;
65 ctx->pending_irq = false;
66 ctx->pending_fault = false;
67 ctx->pending_afu_err = false;
68
Ian Munsief5c9df92016-06-30 04:55:17 +100069 INIT_LIST_HEAD(&ctx->irq_names);
Ian Munsiecbce0912016-07-14 07:17:09 +100070 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
Ian Munsief5c9df92016-06-30 04:55:17 +100071
Ian Munsief204e0b2014-10-08 19:55:02 +110072 /*
73 * When we have to destroy all contexts in cxl_context_detach_all() we
74 * end up with afu_release_irqs() called from inside a
75 * idr_for_each_entry(). Hence we need to make sure that anything
76 * dereferenced from this IDR is ok before we allocate the IDR here.
77 * This clears out the IRQ ranges to ensure this.
78 */
79 for (i = 0; i < CXL_IRQ_RANGES; i++)
80 ctx->irqs.range[i] = 0;
81
82 mutex_init(&ctx->status_mutex);
83
84 ctx->status = OPENED;
85
86 /*
87 * Allocating IDR! We better make sure everything's setup that
88 * dereferences from it.
89 */
Ian Munsieee41d112014-12-08 19:17:55 +110090 mutex_lock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110091 idr_preload(GFP_KERNEL);
Andrew Donnellan16479332016-07-28 15:39:41 +100092 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
Ian Munsief204e0b2014-10-08 19:55:02 +110093 ctx->afu->num_procs, GFP_NOWAIT);
Ian Munsief204e0b2014-10-08 19:55:02 +110094 idr_preload_end();
Ian Munsieee41d112014-12-08 19:17:55 +110095 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110096 if (i < 0)
97 return i;
98
99 ctx->pe = i;
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100100 if (cpu_has_feature(CPU_FTR_HVMODE)) {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100101 ctx->elem = &ctx->afu->native->spa[i];
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100102 ctx->external_pe = ctx->pe;
103 } else {
104 ctx->external_pe = -1; /* assigned when attaching */
105 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100106 ctx->pe_inserted = false;
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530107
108 /*
109 * take a ref on the afu so that it stays alive at-least till
110 * this context is reclaimed inside reclaim_ctx.
111 */
112 cxl_afu_get(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100113 return 0;
114}
115
Frederic Barratbdecf762016-11-18 23:00:31 +1100116void cxl_context_set_mapping(struct cxl_context *ctx,
117 struct address_space *mapping)
118{
119 mutex_lock(&ctx->mapping_lock);
120 ctx->mapping = mapping;
121 mutex_unlock(&ctx->mapping_lock);
122}
123
Ian Munsie0712dc72015-01-07 16:33:04 +1100124static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
125{
126 struct cxl_context *ctx = vma->vm_file->private_data;
127 unsigned long address = (unsigned long)vmf->virtual_address;
128 u64 area, offset;
129
130 offset = vmf->pgoff << PAGE_SHIFT;
131
132 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
133 __func__, ctx->pe, address, offset);
134
135 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
136 area = ctx->afu->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000137 if (offset >= ctx->afu->adapter->ps_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100138 return VM_FAULT_SIGBUS;
139 } else {
140 area = ctx->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000141 if (offset >= ctx->psn_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100142 return VM_FAULT_SIGBUS;
143 }
144
145 mutex_lock(&ctx->status_mutex);
146
147 if (ctx->status != STARTED) {
148 mutex_unlock(&ctx->status_mutex);
149 pr_devel("%s: Context not started, failing problem state access\n", __func__);
Ian Munsied9232a32015-07-23 16:43:56 +1000150 if (ctx->mmio_err_ff) {
151 if (!ctx->ff_page) {
152 ctx->ff_page = alloc_page(GFP_USER);
153 if (!ctx->ff_page)
154 return VM_FAULT_OOM;
155 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
156 }
157 get_page(ctx->ff_page);
158 vmf->page = ctx->ff_page;
159 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
160 return 0;
161 }
Ian Munsie0712dc72015-01-07 16:33:04 +1100162 return VM_FAULT_SIGBUS;
163 }
164
165 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
166
167 mutex_unlock(&ctx->status_mutex);
168
169 return VM_FAULT_NOPAGE;
170}
171
172static const struct vm_operations_struct cxl_mmap_vmops = {
173 .fault = cxl_mmap_fault,
174};
175
Ian Munsief204e0b2014-10-08 19:55:02 +1100176/*
177 * Map a per-context mmio space into the given vma.
178 */
179int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
180{
Ian Munsie5caaf532015-07-07 15:45:46 +1000181 u64 start = vma->vm_pgoff << PAGE_SHIFT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100182 u64 len = vma->vm_end - vma->vm_start;
Ian Munsie5caaf532015-07-07 15:45:46 +1000183
184 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
185 if (start + len > ctx->afu->adapter->ps_size)
186 return -EINVAL;
187 } else {
188 if (start + len > ctx->psn_size)
189 return -EINVAL;
190 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100191
Ian Munsie0712dc72015-01-07 16:33:04 +1100192 if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
193 /* make sure there is a valid per process space for this AFU */
194 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
195 pr_devel("AFU doesn't support mmio space\n");
196 return -EINVAL;
197 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100198
Ian Munsie0712dc72015-01-07 16:33:04 +1100199 /* Can't mmap until the AFU is enabled */
200 if (!ctx->afu->enabled)
201 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100202 }
203
Ian Munsief204e0b2014-10-08 19:55:02 +1100204 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
205 ctx->psn_phys, ctx->pe , ctx->master);
206
Ian Munsie0712dc72015-01-07 16:33:04 +1100207 vma->vm_flags |= VM_IO | VM_PFNMAP;
Ian Munsief204e0b2014-10-08 19:55:02 +1100208 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Ian Munsie0712dc72015-01-07 16:33:04 +1100209 vma->vm_ops = &cxl_mmap_vmops;
210 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100211}
212
213/*
214 * Detach a context from the hardware. This disables interrupts and doesn't
215 * return until all outstanding interrupts for this context have completed. The
216 * hardware should no longer access *ctx after this has returned.
217 */
Michael Neulingeda36932015-05-27 16:07:08 +1000218int __detach_context(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100219{
220 enum cxl_context_status status;
221
222 mutex_lock(&ctx->status_mutex);
223 status = ctx->status;
224 ctx->status = CLOSED;
225 mutex_unlock(&ctx->status_mutex);
226 if (status != STARTED)
Michael Neulingeda36932015-05-27 16:07:08 +1000227 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100228
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000229 /* Only warn if we detached while the link was OK.
230 * If detach fails when hw is down, we don't care.
231 */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100232 WARN_ON(cxl_ops->detach_process(ctx) &&
Christophe Lombard0d400f72016-03-04 12:26:41 +0100233 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000234 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530235
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000236 /*
237 * Wait until no further interrupts are presented by the PSL
238 * for this context.
239 */
240 if (cxl_ops->irq_wait)
241 cxl_ops->irq_wait(ctx);
242
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530243 /* release the reference to the group leader and mm handling pid */
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000244 put_pid(ctx->pid);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530245 put_pid(ctx->glpid);
246
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000247 cxl_ctx_put();
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530248
249 /* Decrease the attached context count on the adapter */
250 cxl_adapter_context_put(ctx->afu->adapter);
Michael Neulingeda36932015-05-27 16:07:08 +1000251 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100252}
253
254/*
255 * Detach the given context from the AFU. This doesn't actually
256 * free the context but it should stop the context running in hardware
257 * (ie. prevent this context from generating any further interrupts
258 * so that it can be freed).
259 */
260void cxl_context_detach(struct cxl_context *ctx)
261{
Michael Neulingeda36932015-05-27 16:07:08 +1000262 int rc;
263
264 rc = __detach_context(ctx);
265 if (rc)
266 return;
267
268 afu_release_irqs(ctx, ctx);
Michael Neulingeda36932015-05-27 16:07:08 +1000269 wake_up_all(&ctx->wq);
Ian Munsief204e0b2014-10-08 19:55:02 +1100270}
271
272/*
273 * Detach all contexts on the given AFU.
274 */
275void cxl_context_detach_all(struct cxl_afu *afu)
276{
277 struct cxl_context *ctx;
278 int tmp;
279
Ian Munsieee41d112014-12-08 19:17:55 +1100280 mutex_lock(&afu->contexts_lock);
281 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100282 /*
283 * Anything done in here needs to be setup before the IDR is
284 * created and torn down after the IDR removed
285 */
Michael Neulingeda36932015-05-27 16:07:08 +1000286 cxl_context_detach(ctx);
Ian Munsie0712dc72015-01-07 16:33:04 +1100287
288 /*
289 * We are force detaching - remove any active PSA mappings so
290 * userspace cannot interfere with the card if it comes back.
291 * Easiest way to exercise this is to unbind and rebind the
292 * driver via sysfs while it is in use.
293 */
294 mutex_lock(&ctx->mapping_lock);
295 if (ctx->mapping)
296 unmap_mapping_range(ctx->mapping, 0, 0, 1);
297 mutex_unlock(&ctx->mapping_lock);
Ian Munsieee41d112014-12-08 19:17:55 +1100298 }
299 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100300}
301
Ian Munsie8ac75b92015-05-08 22:55:18 +1000302static void reclaim_ctx(struct rcu_head *rcu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100303{
Ian Munsie8ac75b92015-05-08 22:55:18 +1000304 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100305
306 free_page((u64)ctx->sstp);
Ian Munsied9232a32015-07-23 16:43:56 +1000307 if (ctx->ff_page)
308 __free_page(ctx->ff_page);
Ian Munsief204e0b2014-10-08 19:55:02 +1100309 ctx->sstp = NULL;
310
Markus Elfring1050e682015-11-06 11:00:23 +0100311 kfree(ctx->irq_bitmap);
Andrew Donnellan52adee52015-09-30 11:58:06 +1000312
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530313 /* Drop ref to the afu device taken during cxl_context_init */
314 cxl_afu_put(ctx->afu);
315
Ian Munsief204e0b2014-10-08 19:55:02 +1100316 kfree(ctx);
317}
Ian Munsie8ac75b92015-05-08 22:55:18 +1000318
319void cxl_context_free(struct cxl_context *ctx)
320{
Frederic Barratbdecf762016-11-18 23:00:31 +1100321 if (ctx->kernelapi && ctx->mapping)
322 cxl_release_mapping(ctx);
Ian Munsie8ac75b92015-05-08 22:55:18 +1000323 mutex_lock(&ctx->afu->contexts_lock);
324 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
325 mutex_unlock(&ctx->afu->contexts_lock);
326 call_rcu(&ctx->rcu, reclaim_ctx);
327}