blob: edbb99e9311433ed54c5f5249af5c1fa0cb5de82 [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);
71
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);
Ian Munsief204e0b2014-10-08 19:55:02 +110092 i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
93 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
Ian Munsie0712dc72015-01-07 16:33:04 +1100116static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
117{
118 struct cxl_context *ctx = vma->vm_file->private_data;
119 unsigned long address = (unsigned long)vmf->virtual_address;
120 u64 area, offset;
121
122 offset = vmf->pgoff << PAGE_SHIFT;
123
124 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
125 __func__, ctx->pe, address, offset);
126
127 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
128 area = ctx->afu->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000129 if (offset >= ctx->afu->adapter->ps_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100130 return VM_FAULT_SIGBUS;
131 } else {
132 area = ctx->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000133 if (offset >= ctx->psn_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100134 return VM_FAULT_SIGBUS;
135 }
136
137 mutex_lock(&ctx->status_mutex);
138
139 if (ctx->status != STARTED) {
140 mutex_unlock(&ctx->status_mutex);
141 pr_devel("%s: Context not started, failing problem state access\n", __func__);
Ian Munsied9232a32015-07-23 16:43:56 +1000142 if (ctx->mmio_err_ff) {
143 if (!ctx->ff_page) {
144 ctx->ff_page = alloc_page(GFP_USER);
145 if (!ctx->ff_page)
146 return VM_FAULT_OOM;
147 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
148 }
149 get_page(ctx->ff_page);
150 vmf->page = ctx->ff_page;
151 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
152 return 0;
153 }
Ian Munsie0712dc72015-01-07 16:33:04 +1100154 return VM_FAULT_SIGBUS;
155 }
156
157 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
158
159 mutex_unlock(&ctx->status_mutex);
160
161 return VM_FAULT_NOPAGE;
162}
163
164static const struct vm_operations_struct cxl_mmap_vmops = {
165 .fault = cxl_mmap_fault,
166};
167
Ian Munsief204e0b2014-10-08 19:55:02 +1100168/*
169 * Map a per-context mmio space into the given vma.
170 */
171int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
172{
Ian Munsie5caaf532015-07-07 15:45:46 +1000173 u64 start = vma->vm_pgoff << PAGE_SHIFT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100174 u64 len = vma->vm_end - vma->vm_start;
Ian Munsie5caaf532015-07-07 15:45:46 +1000175
176 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
177 if (start + len > ctx->afu->adapter->ps_size)
178 return -EINVAL;
179 } else {
180 if (start + len > ctx->psn_size)
181 return -EINVAL;
182 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100183
Ian Munsie0712dc72015-01-07 16:33:04 +1100184 if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
185 /* make sure there is a valid per process space for this AFU */
186 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
187 pr_devel("AFU doesn't support mmio space\n");
188 return -EINVAL;
189 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100190
Ian Munsie0712dc72015-01-07 16:33:04 +1100191 /* Can't mmap until the AFU is enabled */
192 if (!ctx->afu->enabled)
193 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100194 }
195
Ian Munsief204e0b2014-10-08 19:55:02 +1100196 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
197 ctx->psn_phys, ctx->pe , ctx->master);
198
Ian Munsie0712dc72015-01-07 16:33:04 +1100199 vma->vm_flags |= VM_IO | VM_PFNMAP;
Ian Munsief204e0b2014-10-08 19:55:02 +1100200 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Ian Munsie0712dc72015-01-07 16:33:04 +1100201 vma->vm_ops = &cxl_mmap_vmops;
202 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100203}
204
205/*
206 * Detach a context from the hardware. This disables interrupts and doesn't
207 * return until all outstanding interrupts for this context have completed. The
208 * hardware should no longer access *ctx after this has returned.
209 */
Michael Neulingeda36932015-05-27 16:07:08 +1000210int __detach_context(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100211{
212 enum cxl_context_status status;
213
214 mutex_lock(&ctx->status_mutex);
215 status = ctx->status;
216 ctx->status = CLOSED;
217 mutex_unlock(&ctx->status_mutex);
218 if (status != STARTED)
Michael Neulingeda36932015-05-27 16:07:08 +1000219 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100220
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000221 /* Only warn if we detached while the link was OK.
222 * If detach fails when hw is down, we don't care.
223 */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100224 WARN_ON(cxl_ops->detach_process(ctx) &&
Christophe Lombard0d400f72016-03-04 12:26:41 +0100225 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000226 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530227
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000228 /*
229 * Wait until no further interrupts are presented by the PSL
230 * for this context.
231 */
232 if (cxl_ops->irq_wait)
233 cxl_ops->irq_wait(ctx);
234
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530235 /* release the reference to the group leader and mm handling pid */
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000236 put_pid(ctx->pid);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530237 put_pid(ctx->glpid);
238
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000239 cxl_ctx_put();
Michael Neulingeda36932015-05-27 16:07:08 +1000240 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100241}
242
243/*
244 * Detach the given context from the AFU. This doesn't actually
245 * free the context but it should stop the context running in hardware
246 * (ie. prevent this context from generating any further interrupts
247 * so that it can be freed).
248 */
249void cxl_context_detach(struct cxl_context *ctx)
250{
Michael Neulingeda36932015-05-27 16:07:08 +1000251 int rc;
252
253 rc = __detach_context(ctx);
254 if (rc)
255 return;
256
257 afu_release_irqs(ctx, ctx);
Michael Neulingeda36932015-05-27 16:07:08 +1000258 wake_up_all(&ctx->wq);
Ian Munsief204e0b2014-10-08 19:55:02 +1100259}
260
261/*
262 * Detach all contexts on the given AFU.
263 */
264void cxl_context_detach_all(struct cxl_afu *afu)
265{
266 struct cxl_context *ctx;
267 int tmp;
268
Ian Munsieee41d112014-12-08 19:17:55 +1100269 mutex_lock(&afu->contexts_lock);
270 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100271 /*
272 * Anything done in here needs to be setup before the IDR is
273 * created and torn down after the IDR removed
274 */
Michael Neulingeda36932015-05-27 16:07:08 +1000275 cxl_context_detach(ctx);
Ian Munsie0712dc72015-01-07 16:33:04 +1100276
277 /*
278 * We are force detaching - remove any active PSA mappings so
279 * userspace cannot interfere with the card if it comes back.
280 * Easiest way to exercise this is to unbind and rebind the
281 * driver via sysfs while it is in use.
282 */
283 mutex_lock(&ctx->mapping_lock);
284 if (ctx->mapping)
285 unmap_mapping_range(ctx->mapping, 0, 0, 1);
286 mutex_unlock(&ctx->mapping_lock);
Ian Munsieee41d112014-12-08 19:17:55 +1100287 }
288 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100289}
290
Ian Munsie8ac75b92015-05-08 22:55:18 +1000291static void reclaim_ctx(struct rcu_head *rcu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100292{
Ian Munsie8ac75b92015-05-08 22:55:18 +1000293 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100294
295 free_page((u64)ctx->sstp);
Ian Munsied9232a32015-07-23 16:43:56 +1000296 if (ctx->ff_page)
297 __free_page(ctx->ff_page);
Ian Munsief204e0b2014-10-08 19:55:02 +1100298 ctx->sstp = NULL;
Ian Munsie55e07662015-08-27 19:50:19 +1000299 if (ctx->kernelapi)
300 kfree(ctx->mapping);
Ian Munsief204e0b2014-10-08 19:55:02 +1100301
Markus Elfring1050e682015-11-06 11:00:23 +0100302 kfree(ctx->irq_bitmap);
Andrew Donnellan52adee52015-09-30 11:58:06 +1000303
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530304 /* Drop ref to the afu device taken during cxl_context_init */
305 cxl_afu_put(ctx->afu);
306
Ian Munsief204e0b2014-10-08 19:55:02 +1100307 kfree(ctx);
308}
Ian Munsie8ac75b92015-05-08 22:55:18 +1000309
310void cxl_context_free(struct cxl_context *ctx)
311{
312 mutex_lock(&ctx->afu->contexts_lock);
313 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
314 mutex_unlock(&ctx->afu->contexts_lock);
315 call_rcu(&ctx->rcu, reclaim_ctx);
316}