]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/misc/cxl/context.c
cxl: Add psl9 specific code
[mirror_ubuntu-zesty-kernel.git] / drivers / misc / cxl / context.c
CommitLineData
f204e0b8
IM
1/*
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>
ec7beb86 20#include <linux/mm.h>
f204e0b8
IM
21#include <asm/cputable.h>
22#include <asm/current.h>
23#include <asm/copro.h>
24
25#include "cxl.h"
26
27/*
28 * Allocates space for a CXL context.
29 */
30struct cxl_context *cxl_context_alloc(void)
31{
32 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
33}
34
35/*
36 * Initialises a CXL context.
37 */
bdecf76e 38int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
f204e0b8
IM
39{
40 int i;
41
f204e0b8
IM
42 ctx->afu = afu;
43 ctx->master = master;
ec7beb86 44 ctx->pid = NULL; /* Set in start work ioctl */
b123429e 45 mutex_init(&ctx->mapping_lock);
bdecf76e 46 ctx->mapping = NULL;
f204e0b8 47
d371edf5
CL
48 if (cxl_is_psl8(afu)) {
49 spin_lock_init(&ctx->sste_lock);
50
51 /*
52 * Allocate the segment table before we put it in the IDR so that we
53 * can always access it when dereferenced from IDR. For the same
54 * reason, the segment table is only destroyed after the context is
55 * removed from the IDR. Access to this in the IOCTL is protected by
56 * Linux filesytem symantics (can't IOCTL until open is complete).
57 */
58 i = cxl_alloc_sst(ctx);
59 if (i)
60 return i;
61 }
f204e0b8
IM
62
63 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
64
65 init_waitqueue_head(&ctx->wq);
66 spin_lock_init(&ctx->lock);
67
68 ctx->irq_bitmap = NULL;
69 ctx->pending_irq = false;
70 ctx->pending_fault = false;
71 ctx->pending_afu_err = false;
72
f5c9df9a 73 INIT_LIST_HEAD(&ctx->irq_names);
cbce0917 74 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
f5c9df9a 75
f204e0b8
IM
76 /*
77 * When we have to destroy all contexts in cxl_context_detach_all() we
78 * end up with afu_release_irqs() called from inside a
79 * idr_for_each_entry(). Hence we need to make sure that anything
80 * dereferenced from this IDR is ok before we allocate the IDR here.
81 * This clears out the IRQ ranges to ensure this.
82 */
83 for (i = 0; i < CXL_IRQ_RANGES; i++)
84 ctx->irqs.range[i] = 0;
85
86 mutex_init(&ctx->status_mutex);
87
88 ctx->status = OPENED;
89
90 /*
91 * Allocating IDR! We better make sure everything's setup that
92 * dereferences from it.
93 */
ee41d11d 94 mutex_lock(&afu->contexts_lock);
f204e0b8 95 idr_preload(GFP_KERNEL);
16479337 96 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
f204e0b8 97 ctx->afu->num_procs, GFP_NOWAIT);
f204e0b8 98 idr_preload_end();
ee41d11d 99 mutex_unlock(&afu->contexts_lock);
f204e0b8
IM
100 if (i < 0)
101 return i;
102
103 ctx->pe = i;
14baf4d9 104 if (cpu_has_feature(CPU_FTR_HVMODE)) {
cbffa3a5 105 ctx->elem = &ctx->afu->native->spa[i];
14baf4d9
CL
106 ctx->external_pe = ctx->pe;
107 } else {
108 ctx->external_pe = -1; /* assigned when attaching */
109 }
f204e0b8 110 ctx->pe_inserted = false;
1b5df59e
VJ
111
112 /*
113 * take a ref on the afu so that it stays alive at-least till
114 * this context is reclaimed inside reclaim_ctx.
115 */
116 cxl_afu_get(afu);
f204e0b8
IM
117 return 0;
118}
119
bdecf76e
FB
120void cxl_context_set_mapping(struct cxl_context *ctx,
121 struct address_space *mapping)
122{
123 mutex_lock(&ctx->mapping_lock);
124 ctx->mapping = mapping;
125 mutex_unlock(&ctx->mapping_lock);
126}
127
0712dc7e
IM
128static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
129{
130 struct cxl_context *ctx = vma->vm_file->private_data;
0712dc7e
IM
131 u64 area, offset;
132
133 offset = vmf->pgoff << PAGE_SHIFT;
134
135 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
1a29d85e 136 __func__, ctx->pe, vmf->address, offset);
0712dc7e
IM
137
138 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
139 area = ctx->afu->psn_phys;
10a5894f 140 if (offset >= ctx->afu->adapter->ps_size)
0712dc7e
IM
141 return VM_FAULT_SIGBUS;
142 } else {
143 area = ctx->psn_phys;
10a5894f 144 if (offset >= ctx->psn_size)
0712dc7e
IM
145 return VM_FAULT_SIGBUS;
146 }
147
148 mutex_lock(&ctx->status_mutex);
149
150 if (ctx->status != STARTED) {
151 mutex_unlock(&ctx->status_mutex);
152 pr_devel("%s: Context not started, failing problem state access\n", __func__);
d9232a3d
IM
153 if (ctx->mmio_err_ff) {
154 if (!ctx->ff_page) {
155 ctx->ff_page = alloc_page(GFP_USER);
156 if (!ctx->ff_page)
157 return VM_FAULT_OOM;
158 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
159 }
160 get_page(ctx->ff_page);
161 vmf->page = ctx->ff_page;
162 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
163 return 0;
164 }
0712dc7e
IM
165 return VM_FAULT_SIGBUS;
166 }
167
1a29d85e 168 vm_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
0712dc7e
IM
169
170 mutex_unlock(&ctx->status_mutex);
171
172 return VM_FAULT_NOPAGE;
173}
174
175static const struct vm_operations_struct cxl_mmap_vmops = {
176 .fault = cxl_mmap_fault,
177};
178
f204e0b8
IM
179/*
180 * Map a per-context mmio space into the given vma.
181 */
182int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
183{
5caaf534 184 u64 start = vma->vm_pgoff << PAGE_SHIFT;
f204e0b8 185 u64 len = vma->vm_end - vma->vm_start;
5caaf534
IM
186
187 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
188 if (start + len > ctx->afu->adapter->ps_size)
189 return -EINVAL;
5f761c26
CL
190
191 if (cxl_is_psl9(ctx->afu)) {
192 /*
193 * Make sure there is a valid problem state
194 * area space for this AFU.
195 */
196 if (ctx->master && !ctx->afu->psa) {
197 pr_devel("AFU doesn't support mmio space\n");
198 return -EINVAL;
199 }
200
201 /* Can't mmap until the AFU is enabled */
202 if (!ctx->afu->enabled)
203 return -EBUSY;
204 }
5caaf534
IM
205 } else {
206 if (start + len > ctx->psn_size)
207 return -EINVAL;
f204e0b8 208
5f761c26 209 /* Make sure there is a valid per process space for this AFU */
0712dc7e
IM
210 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
211 pr_devel("AFU doesn't support mmio space\n");
212 return -EINVAL;
213 }
f204e0b8 214
0712dc7e
IM
215 /* Can't mmap until the AFU is enabled */
216 if (!ctx->afu->enabled)
217 return -EBUSY;
f204e0b8
IM
218 }
219
f204e0b8
IM
220 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
221 ctx->psn_phys, ctx->pe , ctx->master);
222
0712dc7e 223 vma->vm_flags |= VM_IO | VM_PFNMAP;
f204e0b8 224 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0712dc7e
IM
225 vma->vm_ops = &cxl_mmap_vmops;
226 return 0;
f204e0b8
IM
227}
228
229/*
230 * Detach a context from the hardware. This disables interrupts and doesn't
231 * return until all outstanding interrupts for this context have completed. The
232 * hardware should no longer access *ctx after this has returned.
233 */
eda3693c 234int __detach_context(struct cxl_context *ctx)
f204e0b8
IM
235{
236 enum cxl_context_status status;
237
238 mutex_lock(&ctx->status_mutex);
239 status = ctx->status;
240 ctx->status = CLOSED;
241 mutex_unlock(&ctx->status_mutex);
242 if (status != STARTED)
eda3693c 243 return -EBUSY;
f204e0b8 244
0b3f9c75
DA
245 /* Only warn if we detached while the link was OK.
246 * If detach fails when hw is down, we don't care.
247 */
5be587b1 248 WARN_ON(cxl_ops->detach_process(ctx) &&
0d400f77 249 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
7bb5d91a 250 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
7b8ad495 251
2bc79ffc
MN
252 /*
253 * Wait until no further interrupts are presented by the PSL
254 * for this context.
255 */
256 if (cxl_ops->irq_wait)
257 cxl_ops->irq_wait(ctx);
258
7b8ad495 259 /* release the reference to the group leader and mm handling pid */
7bb5d91a 260 put_pid(ctx->pid);
7b8ad495 261
7bb5d91a 262 cxl_ctx_put();
70b565bb
VJ
263
264 /* Decrease the attached context count on the adapter */
265 cxl_adapter_context_put(ctx->afu->adapter);
ec7beb86
CL
266
267 /* Decrease the mm count on the context */
268 cxl_context_mm_count_put(ctx);
269 ctx->mm = NULL;
270
eda3693c 271 return 0;
f204e0b8
IM
272}
273
274/*
275 * Detach the given context from the AFU. This doesn't actually
276 * free the context but it should stop the context running in hardware
277 * (ie. prevent this context from generating any further interrupts
278 * so that it can be freed).
279 */
280void cxl_context_detach(struct cxl_context *ctx)
281{
eda3693c
MN
282 int rc;
283
284 rc = __detach_context(ctx);
285 if (rc)
286 return;
287
288 afu_release_irqs(ctx, ctx);
eda3693c 289 wake_up_all(&ctx->wq);
f204e0b8
IM
290}
291
292/*
293 * Detach all contexts on the given AFU.
294 */
295void cxl_context_detach_all(struct cxl_afu *afu)
296{
297 struct cxl_context *ctx;
298 int tmp;
299
ee41d11d
IM
300 mutex_lock(&afu->contexts_lock);
301 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
f204e0b8
IM
302 /*
303 * Anything done in here needs to be setup before the IDR is
304 * created and torn down after the IDR removed
305 */
eda3693c 306 cxl_context_detach(ctx);
0712dc7e
IM
307
308 /*
309 * We are force detaching - remove any active PSA mappings so
310 * userspace cannot interfere with the card if it comes back.
311 * Easiest way to exercise this is to unbind and rebind the
312 * driver via sysfs while it is in use.
313 */
314 mutex_lock(&ctx->mapping_lock);
315 if (ctx->mapping)
316 unmap_mapping_range(ctx->mapping, 0, 0, 1);
317 mutex_unlock(&ctx->mapping_lock);
ee41d11d
IM
318 }
319 mutex_unlock(&afu->contexts_lock);
f204e0b8
IM
320}
321
8ac75b96 322static void reclaim_ctx(struct rcu_head *rcu)
f204e0b8 323{
8ac75b96 324 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
f204e0b8 325
d371edf5
CL
326 if (cxl_is_psl8(ctx->afu))
327 free_page((u64)ctx->sstp);
d9232a3d
IM
328 if (ctx->ff_page)
329 __free_page(ctx->ff_page);
f204e0b8
IM
330 ctx->sstp = NULL;
331
1050e689 332 kfree(ctx->irq_bitmap);
52adee58 333
1b5df59e
VJ
334 /* Drop ref to the afu device taken during cxl_context_init */
335 cxl_afu_put(ctx->afu);
336
f204e0b8
IM
337 kfree(ctx);
338}
8ac75b96
IM
339
340void cxl_context_free(struct cxl_context *ctx)
341{
bdecf76e
FB
342 if (ctx->kernelapi && ctx->mapping)
343 cxl_release_mapping(ctx);
8ac75b96
IM
344 mutex_lock(&ctx->afu->contexts_lock);
345 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
346 mutex_unlock(&ctx->afu->contexts_lock);
347 call_rcu(&ctx->rcu, reclaim_ctx);
348}
ec7beb86
CL
349
350void cxl_context_mm_count_get(struct cxl_context *ctx)
351{
352 if (ctx->mm)
353 atomic_inc(&ctx->mm->mm_count);
354}
355
356void cxl_context_mm_count_put(struct cxl_context *ctx)
357{
358 if (ctx->mm)
359 mmdrop(ctx->mm);
360}