]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/misc/cxl/context.c
mm, page_alloc: only use per-cpu allocator for irq-safe requests
[mirror_ubuntu-bionic-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>
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 */
bdecf76e 37int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
f204e0b8
IM
38{
39 int i;
40
41 spin_lock_init(&ctx->sste_lock);
42 ctx->afu = afu;
43 ctx->master = master;
7b8ad495 44 ctx->pid = ctx->glpid = NULL; /* Set in start work ioctl */
b123429e 45 mutex_init(&ctx->mapping_lock);
bdecf76e 46 ctx->mapping = NULL;
f204e0b8
IM
47
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
f5c9df9a 69 INIT_LIST_HEAD(&ctx->irq_names);
cbce0917 70 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
f5c9df9a 71
f204e0b8
IM
72 /*
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 */
ee41d11d 90 mutex_lock(&afu->contexts_lock);
f204e0b8 91 idr_preload(GFP_KERNEL);
16479337 92 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
f204e0b8 93 ctx->afu->num_procs, GFP_NOWAIT);
f204e0b8 94 idr_preload_end();
ee41d11d 95 mutex_unlock(&afu->contexts_lock);
f204e0b8
IM
96 if (i < 0)
97 return i;
98
99 ctx->pe = i;
14baf4d9 100 if (cpu_has_feature(CPU_FTR_HVMODE)) {
cbffa3a5 101 ctx->elem = &ctx->afu->native->spa[i];
14baf4d9
CL
102 ctx->external_pe = ctx->pe;
103 } else {
104 ctx->external_pe = -1; /* assigned when attaching */
105 }
f204e0b8 106 ctx->pe_inserted = false;
1b5df59e
VJ
107
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);
f204e0b8
IM
113 return 0;
114}
115
bdecf76e
FB
116void 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
0712dc7e
IM
124static int cxl_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
125{
126 struct cxl_context *ctx = vma->vm_file->private_data;
0712dc7e
IM
127 u64 area, offset;
128
129 offset = vmf->pgoff << PAGE_SHIFT;
130
131 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
1a29d85e 132 __func__, ctx->pe, vmf->address, offset);
0712dc7e
IM
133
134 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
135 area = ctx->afu->psn_phys;
10a5894f 136 if (offset >= ctx->afu->adapter->ps_size)
0712dc7e
IM
137 return VM_FAULT_SIGBUS;
138 } else {
139 area = ctx->psn_phys;
10a5894f 140 if (offset >= ctx->psn_size)
0712dc7e
IM
141 return VM_FAULT_SIGBUS;
142 }
143
144 mutex_lock(&ctx->status_mutex);
145
146 if (ctx->status != STARTED) {
147 mutex_unlock(&ctx->status_mutex);
148 pr_devel("%s: Context not started, failing problem state access\n", __func__);
d9232a3d
IM
149 if (ctx->mmio_err_ff) {
150 if (!ctx->ff_page) {
151 ctx->ff_page = alloc_page(GFP_USER);
152 if (!ctx->ff_page)
153 return VM_FAULT_OOM;
154 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
155 }
156 get_page(ctx->ff_page);
157 vmf->page = ctx->ff_page;
158 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
159 return 0;
160 }
0712dc7e
IM
161 return VM_FAULT_SIGBUS;
162 }
163
1a29d85e 164 vm_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
0712dc7e
IM
165
166 mutex_unlock(&ctx->status_mutex);
167
168 return VM_FAULT_NOPAGE;
169}
170
171static const struct vm_operations_struct cxl_mmap_vmops = {
172 .fault = cxl_mmap_fault,
173};
174
f204e0b8
IM
175/*
176 * Map a per-context mmio space into the given vma.
177 */
178int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
179{
5caaf534 180 u64 start = vma->vm_pgoff << PAGE_SHIFT;
f204e0b8 181 u64 len = vma->vm_end - vma->vm_start;
5caaf534
IM
182
183 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
184 if (start + len > ctx->afu->adapter->ps_size)
185 return -EINVAL;
186 } else {
187 if (start + len > ctx->psn_size)
188 return -EINVAL;
189 }
f204e0b8 190
0712dc7e
IM
191 if (ctx->afu->current_mode != CXL_MODE_DEDICATED) {
192 /* make sure there is a valid per process space for this AFU */
193 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
194 pr_devel("AFU doesn't support mmio space\n");
195 return -EINVAL;
196 }
f204e0b8 197
0712dc7e
IM
198 /* Can't mmap until the AFU is enabled */
199 if (!ctx->afu->enabled)
200 return -EBUSY;
f204e0b8
IM
201 }
202
f204e0b8
IM
203 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
204 ctx->psn_phys, ctx->pe , ctx->master);
205
0712dc7e 206 vma->vm_flags |= VM_IO | VM_PFNMAP;
f204e0b8 207 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0712dc7e
IM
208 vma->vm_ops = &cxl_mmap_vmops;
209 return 0;
f204e0b8
IM
210}
211
212/*
213 * Detach a context from the hardware. This disables interrupts and doesn't
214 * return until all outstanding interrupts for this context have completed. The
215 * hardware should no longer access *ctx after this has returned.
216 */
eda3693c 217int __detach_context(struct cxl_context *ctx)
f204e0b8
IM
218{
219 enum cxl_context_status status;
220
221 mutex_lock(&ctx->status_mutex);
222 status = ctx->status;
223 ctx->status = CLOSED;
224 mutex_unlock(&ctx->status_mutex);
225 if (status != STARTED)
eda3693c 226 return -EBUSY;
f204e0b8 227
0b3f9c75
DA
228 /* Only warn if we detached while the link was OK.
229 * If detach fails when hw is down, we don't care.
230 */
5be587b1 231 WARN_ON(cxl_ops->detach_process(ctx) &&
0d400f77 232 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
7bb5d91a 233 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
7b8ad495 234
2bc79ffc
MN
235 /*
236 * Wait until no further interrupts are presented by the PSL
237 * for this context.
238 */
239 if (cxl_ops->irq_wait)
240 cxl_ops->irq_wait(ctx);
241
7b8ad495 242 /* release the reference to the group leader and mm handling pid */
7bb5d91a 243 put_pid(ctx->pid);
7b8ad495
VJ
244 put_pid(ctx->glpid);
245
7bb5d91a 246 cxl_ctx_put();
70b565bb
VJ
247
248 /* Decrease the attached context count on the adapter */
249 cxl_adapter_context_put(ctx->afu->adapter);
eda3693c 250 return 0;
f204e0b8
IM
251}
252
253/*
254 * Detach the given context from the AFU. This doesn't actually
255 * free the context but it should stop the context running in hardware
256 * (ie. prevent this context from generating any further interrupts
257 * so that it can be freed).
258 */
259void cxl_context_detach(struct cxl_context *ctx)
260{
eda3693c
MN
261 int rc;
262
263 rc = __detach_context(ctx);
264 if (rc)
265 return;
266
267 afu_release_irqs(ctx, ctx);
eda3693c 268 wake_up_all(&ctx->wq);
f204e0b8
IM
269}
270
271/*
272 * Detach all contexts on the given AFU.
273 */
274void cxl_context_detach_all(struct cxl_afu *afu)
275{
276 struct cxl_context *ctx;
277 int tmp;
278
ee41d11d
IM
279 mutex_lock(&afu->contexts_lock);
280 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
f204e0b8
IM
281 /*
282 * Anything done in here needs to be setup before the IDR is
283 * created and torn down after the IDR removed
284 */
eda3693c 285 cxl_context_detach(ctx);
0712dc7e
IM
286
287 /*
288 * We are force detaching - remove any active PSA mappings so
289 * userspace cannot interfere with the card if it comes back.
290 * Easiest way to exercise this is to unbind and rebind the
291 * driver via sysfs while it is in use.
292 */
293 mutex_lock(&ctx->mapping_lock);
294 if (ctx->mapping)
295 unmap_mapping_range(ctx->mapping, 0, 0, 1);
296 mutex_unlock(&ctx->mapping_lock);
ee41d11d
IM
297 }
298 mutex_unlock(&afu->contexts_lock);
f204e0b8
IM
299}
300
8ac75b96 301static void reclaim_ctx(struct rcu_head *rcu)
f204e0b8 302{
8ac75b96 303 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
f204e0b8
IM
304
305 free_page((u64)ctx->sstp);
d9232a3d
IM
306 if (ctx->ff_page)
307 __free_page(ctx->ff_page);
f204e0b8
IM
308 ctx->sstp = NULL;
309
1050e689 310 kfree(ctx->irq_bitmap);
52adee58 311
1b5df59e
VJ
312 /* Drop ref to the afu device taken during cxl_context_init */
313 cxl_afu_put(ctx->afu);
314
f204e0b8
IM
315 kfree(ctx);
316}
8ac75b96
IM
317
318void cxl_context_free(struct cxl_context *ctx)
319{
bdecf76e
FB
320 if (ctx->kernelapi && ctx->mapping)
321 cxl_release_mapping(ctx);
8ac75b96
IM
322 mutex_lock(&ctx->afu->contexts_lock);
323 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
324 mutex_unlock(&ctx->afu->contexts_lock);
325 call_rcu(&ctx->rcu, reclaim_ctx);
326}