blob: c466ee2b0c973a7c77cb16566770dec3b426db33 [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 */
Ian Munsieb1234292014-12-08 19:18:01 +110037int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
38 struct address_space *mapping)
Ian Munsief204e0b2014-10-08 19:55:02 +110039{
40 int i;
41
42 spin_lock_init(&ctx->sste_lock);
43 ctx->afu = afu;
44 ctx->master = master;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +053045 ctx->pid = ctx->glpid = NULL; /* Set in start work ioctl */
Ian Munsieb1234292014-12-08 19:18:01 +110046 mutex_init(&ctx->mapping_lock);
47 ctx->mapping = mapping;
Ian Munsief204e0b2014-10-08 19:55:02 +110048
49 /*
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).
55 */
56 i = cxl_alloc_sst(ctx);
57 if (i)
58 return i;
59
60 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
61
62 init_waitqueue_head(&ctx->wq);
63 spin_lock_init(&ctx->lock);
64
65 ctx->irq_bitmap = NULL;
66 ctx->pending_irq = false;
67 ctx->pending_fault = false;
68 ctx->pending_afu_err = false;
69
Ian Munsief5c9df92016-06-30 04:55:17 +100070 INIT_LIST_HEAD(&ctx->irq_names);
Ian Munsiecbce0912016-07-14 07:17:09 +100071 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
Ian Munsief5c9df92016-06-30 04:55:17 +100072
Ian Munsief204e0b2014-10-08 19:55:02 +110073 /*
74 * When we have to destroy all contexts in cxl_context_detach_all() we
75 * end up with afu_release_irqs() called from inside a
76 * idr_for_each_entry(). Hence we need to make sure that anything
77 * dereferenced from this IDR is ok before we allocate the IDR here.
78 * This clears out the IRQ ranges to ensure this.
79 */
80 for (i = 0; i < CXL_IRQ_RANGES; i++)
81 ctx->irqs.range[i] = 0;
82
83 mutex_init(&ctx->status_mutex);
84
85 ctx->status = OPENED;
86
87 /*
88 * Allocating IDR! We better make sure everything's setup that
89 * dereferences from it.
90 */
Ian Munsieee41d112014-12-08 19:17:55 +110091 mutex_lock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110092 idr_preload(GFP_KERNEL);
Andrew Donnellan16479332016-07-28 15:39:41 +100093 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
Ian Munsief204e0b2014-10-08 19:55:02 +110094 ctx->afu->num_procs, GFP_NOWAIT);
Ian Munsief204e0b2014-10-08 19:55:02 +110095 idr_preload_end();
Ian Munsieee41d112014-12-08 19:17:55 +110096 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110097 if (i < 0)
98 return i;
99
100 ctx->pe = i;
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100101 if (cpu_has_feature(CPU_FTR_HVMODE)) {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100102 ctx->elem = &ctx->afu->native->spa[i];
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100103 ctx->external_pe = ctx->pe;
104 } else {
105 ctx->external_pe = -1; /* assigned when attaching */
106 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100107 ctx->pe_inserted = false;
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530108
109 /*
110 * take a ref on the afu so that it stays alive at-least till
111 * this context is reclaimed inside reclaim_ctx.
112 */
113 cxl_afu_get(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100114 return 0;
115}
116
Ian Munsie0712dc72015-01-07 16:33:04 +1100117static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
118{
119 struct cxl_context *ctx = vma->vm_file->private_data;
120 unsigned long address = (unsigned long)vmf->virtual_address;
121 u64 area, offset;
122
123 offset = vmf->pgoff << PAGE_SHIFT;
124
125 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
126 __func__, ctx->pe, address, offset);
127
128 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
129 area = ctx->afu->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000130 if (offset >= ctx->afu->adapter->ps_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100131 return VM_FAULT_SIGBUS;
132 } else {
133 area = ctx->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000134 if (offset >= ctx->psn_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100135 return VM_FAULT_SIGBUS;
136 }
137
138 mutex_lock(&ctx->status_mutex);
139
140 if (ctx->status != STARTED) {
141 mutex_unlock(&ctx->status_mutex);
142 pr_devel("%s: Context not started, failing problem state access\n", __func__);
Ian Munsied9232a32015-07-23 16:43:56 +1000143 if (ctx->mmio_err_ff) {
144 if (!ctx->ff_page) {
145 ctx->ff_page = alloc_page(GFP_USER);
146 if (!ctx->ff_page)
147 return VM_FAULT_OOM;
148 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
149 }
150 get_page(ctx->ff_page);
151 vmf->page = ctx->ff_page;
152 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
153 return 0;
154 }
Ian Munsie0712dc72015-01-07 16:33:04 +1100155 return VM_FAULT_SIGBUS;
156 }
157
158 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
159
160 mutex_unlock(&ctx->status_mutex);
161
162 return VM_FAULT_NOPAGE;
163}
164
165static const struct vm_operations_struct cxl_mmap_vmops = {
166 .fault = cxl_mmap_fault,
167};
168
Ian Munsief204e0b2014-10-08 19:55:02 +1100169/*
170 * Map a per-context mmio space into the given vma.
171 */
172int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
173{
Ian Munsie5caaf532015-07-07 15:45:46 +1000174 u64 start = vma->vm_pgoff << PAGE_SHIFT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100175 u64 len = vma->vm_end - vma->vm_start;
Ian Munsie5caaf532015-07-07 15:45:46 +1000176
177 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
178 if (start + len > ctx->afu->adapter->ps_size)
179 return -EINVAL;
180 } else {
181 if (start + len > ctx->psn_size)
182 return -EINVAL;
183 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100184
Ian Munsie0712dc72015-01-07 16:33:04 +1100185 if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
186 /* make sure there is a valid per process space for this AFU */
187 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
188 pr_devel("AFU doesn't support mmio space\n");
189 return -EINVAL;
190 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100191
Ian Munsie0712dc72015-01-07 16:33:04 +1100192 /* Can't mmap until the AFU is enabled */
193 if (!ctx->afu->enabled)
194 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100195 }
196
Ian Munsief204e0b2014-10-08 19:55:02 +1100197 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
198 ctx->psn_phys, ctx->pe , ctx->master);
199
Ian Munsie0712dc72015-01-07 16:33:04 +1100200 vma->vm_flags |= VM_IO | VM_PFNMAP;
Ian Munsief204e0b2014-10-08 19:55:02 +1100201 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Ian Munsie0712dc72015-01-07 16:33:04 +1100202 vma->vm_ops = &cxl_mmap_vmops;
203 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100204}
205
206/*
207 * Detach a context from the hardware. This disables interrupts and doesn't
208 * return until all outstanding interrupts for this context have completed. The
209 * hardware should no longer access *ctx after this has returned.
210 */
Michael Neulingeda36932015-05-27 16:07:08 +1000211int __detach_context(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100212{
213 enum cxl_context_status status;
214
215 mutex_lock(&ctx->status_mutex);
216 status = ctx->status;
217 ctx->status = CLOSED;
218 mutex_unlock(&ctx->status_mutex);
219 if (status != STARTED)
Michael Neulingeda36932015-05-27 16:07:08 +1000220 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100221
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000222 /* Only warn if we detached while the link was OK.
223 * If detach fails when hw is down, we don't care.
224 */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100225 WARN_ON(cxl_ops->detach_process(ctx) &&
Christophe Lombard0d400f72016-03-04 12:26:41 +0100226 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000227 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530228
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000229 /*
230 * Wait until no further interrupts are presented by the PSL
231 * for this context.
232 */
233 if (cxl_ops->irq_wait)
234 cxl_ops->irq_wait(ctx);
235
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530236 /* release the reference to the group leader and mm handling pid */
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000237 put_pid(ctx->pid);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530238 put_pid(ctx->glpid);
239
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000240 cxl_ctx_put();
Michael Neulingeda36932015-05-27 16:07:08 +1000241 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100242}
243
244/*
245 * Detach the given context from the AFU. This doesn't actually
246 * free the context but it should stop the context running in hardware
247 * (ie. prevent this context from generating any further interrupts
248 * so that it can be freed).
249 */
250void cxl_context_detach(struct cxl_context *ctx)
251{
Michael Neulingeda36932015-05-27 16:07:08 +1000252 int rc;
253
254 rc = __detach_context(ctx);
255 if (rc)
256 return;
257
258 afu_release_irqs(ctx, ctx);
Michael Neulingeda36932015-05-27 16:07:08 +1000259 wake_up_all(&ctx->wq);
Ian Munsief204e0b2014-10-08 19:55:02 +1100260}
261
262/*
263 * Detach all contexts on the given AFU.
264 */
265void cxl_context_detach_all(struct cxl_afu *afu)
266{
267 struct cxl_context *ctx;
268 int tmp;
269
Ian Munsieee41d112014-12-08 19:17:55 +1100270 mutex_lock(&afu->contexts_lock);
271 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100272 /*
273 * Anything done in here needs to be setup before the IDR is
274 * created and torn down after the IDR removed
275 */
Michael Neulingeda36932015-05-27 16:07:08 +1000276 cxl_context_detach(ctx);
Ian Munsie0712dc72015-01-07 16:33:04 +1100277
278 /*
279 * We are force detaching - remove any active PSA mappings so
280 * userspace cannot interfere with the card if it comes back.
281 * Easiest way to exercise this is to unbind and rebind the
282 * driver via sysfs while it is in use.
283 */
284 mutex_lock(&ctx->mapping_lock);
285 if (ctx->mapping)
286 unmap_mapping_range(ctx->mapping, 0, 0, 1);
287 mutex_unlock(&ctx->mapping_lock);
Ian Munsieee41d112014-12-08 19:17:55 +1100288 }
289 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100290}
291
Ian Munsie8ac75b92015-05-08 22:55:18 +1000292static void reclaim_ctx(struct rcu_head *rcu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100293{
Ian Munsie8ac75b92015-05-08 22:55:18 +1000294 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100295
296 free_page((u64)ctx->sstp);
Ian Munsied9232a32015-07-23 16:43:56 +1000297 if (ctx->ff_page)
298 __free_page(ctx->ff_page);
Ian Munsief204e0b2014-10-08 19:55:02 +1100299 ctx->sstp = NULL;
Ian Munsie55e07662015-08-27 19:50:19 +1000300 if (ctx->kernelapi)
301 kfree(ctx->mapping);
Ian Munsief204e0b2014-10-08 19:55:02 +1100302
Markus Elfring1050e682015-11-06 11:00:23 +0100303 kfree(ctx->irq_bitmap);
Andrew Donnellan52adee52015-09-30 11:58:06 +1000304
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530305 /* Drop ref to the afu device taken during cxl_context_init */
306 cxl_afu_put(ctx->afu);
307
Ian Munsief204e0b2014-10-08 19:55:02 +1100308 kfree(ctx);
309}
Ian Munsie8ac75b92015-05-08 22:55:18 +1000310
311void cxl_context_free(struct cxl_context *ctx)
312{
313 mutex_lock(&ctx->afu->contexts_lock);
314 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
315 mutex_unlock(&ctx->afu->contexts_lock);
316 call_rcu(&ctx->rcu, reclaim_ctx);
317}