blob: 8c32040b9c09f988274ac8cc9354e4b56419787f [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>
Christophe Lombard6dd2d232017-04-07 16:11:55 +020020#include <linux/sched/mm.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110021#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 */
Frederic Barratbdecf762016-11-18 23:00:31 +110038int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
Ian Munsief204e0b2014-10-08 19:55:02 +110039{
40 int i;
41
Ian Munsief204e0b2014-10-08 19:55:02 +110042 ctx->afu = afu;
43 ctx->master = master;
Christophe Lombard6dd2d232017-04-07 16:11:55 +020044 ctx->pid = 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
Christophe Lombard797625d2017-06-13 17:41:05 +020048 if (cxl_is_power8()) {
Christophe Lombardabd1d992017-04-07 16:11:58 +020049 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 }
Ian Munsief204e0b2014-10-08 19:55:02 +110062
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
Ian Munsief5c9df92016-06-30 04:55:17 +100073 INIT_LIST_HEAD(&ctx->irq_names);
Ian Munsiecbce0912016-07-14 07:17:09 +100074 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
Ian Munsief5c9df92016-06-30 04:55:17 +100075
Ian Munsief204e0b2014-10-08 19:55:02 +110076 /*
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 */
Ian Munsieee41d112014-12-08 19:17:55 +110094 mutex_lock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110095 idr_preload(GFP_KERNEL);
Andrew Donnellan16479332016-07-28 15:39:41 +100096 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
Ian Munsief204e0b2014-10-08 19:55:02 +110097 ctx->afu->num_procs, GFP_NOWAIT);
Ian Munsief204e0b2014-10-08 19:55:02 +110098 idr_preload_end();
Ian Munsieee41d112014-12-08 19:17:55 +110099 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100100 if (i < 0)
101 return i;
102
103 ctx->pe = i;
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100104 if (cpu_has_feature(CPU_FTR_HVMODE)) {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100105 ctx->elem = &ctx->afu->native->spa[i];
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100106 ctx->external_pe = ctx->pe;
107 } else {
108 ctx->external_pe = -1; /* assigned when attaching */
109 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100110 ctx->pe_inserted = false;
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530111
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);
Ian Munsief204e0b2014-10-08 19:55:02 +1100117 return 0;
118}
119
Frederic Barratbdecf762016-11-18 23:00:31 +1100120void 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
Dave Jiang11bac802017-02-24 14:56:41 -0800128static int cxl_mmap_fault(struct vm_fault *vmf)
Ian Munsie0712dc72015-01-07 16:33:04 +1100129{
Dave Jiang11bac802017-02-24 14:56:41 -0800130 struct vm_area_struct *vma = vmf->vma;
Ian Munsie0712dc72015-01-07 16:33:04 +1100131 struct cxl_context *ctx = vma->vm_file->private_data;
Ian Munsie0712dc72015-01-07 16:33:04 +1100132 u64 area, offset;
133
134 offset = vmf->pgoff << PAGE_SHIFT;
135
136 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
Jan Kara1a29d852016-12-14 15:07:01 -0800137 __func__, ctx->pe, vmf->address, offset);
Ian Munsie0712dc72015-01-07 16:33:04 +1100138
139 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
140 area = ctx->afu->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000141 if (offset >= ctx->afu->adapter->ps_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100142 return VM_FAULT_SIGBUS;
143 } else {
144 area = ctx->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000145 if (offset >= ctx->psn_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100146 return VM_FAULT_SIGBUS;
147 }
148
149 mutex_lock(&ctx->status_mutex);
150
151 if (ctx->status != STARTED) {
152 mutex_unlock(&ctx->status_mutex);
153 pr_devel("%s: Context not started, failing problem state access\n", __func__);
Ian Munsied9232a32015-07-23 16:43:56 +1000154 if (ctx->mmio_err_ff) {
155 if (!ctx->ff_page) {
156 ctx->ff_page = alloc_page(GFP_USER);
157 if (!ctx->ff_page)
158 return VM_FAULT_OOM;
159 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
160 }
161 get_page(ctx->ff_page);
162 vmf->page = ctx->ff_page;
163 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
164 return 0;
165 }
Ian Munsie0712dc72015-01-07 16:33:04 +1100166 return VM_FAULT_SIGBUS;
167 }
168
Jan Kara1a29d852016-12-14 15:07:01 -0800169 vm_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
Ian Munsie0712dc72015-01-07 16:33:04 +1100170
171 mutex_unlock(&ctx->status_mutex);
172
173 return VM_FAULT_NOPAGE;
174}
175
176static const struct vm_operations_struct cxl_mmap_vmops = {
177 .fault = cxl_mmap_fault,
178};
179
Ian Munsief204e0b2014-10-08 19:55:02 +1100180/*
181 * Map a per-context mmio space into the given vma.
182 */
183int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
184{
Ian Munsie5caaf532015-07-07 15:45:46 +1000185 u64 start = vma->vm_pgoff << PAGE_SHIFT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100186 u64 len = vma->vm_end - vma->vm_start;
Ian Munsie5caaf532015-07-07 15:45:46 +1000187
188 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
189 if (start + len > ctx->afu->adapter->ps_size)
190 return -EINVAL;
Christophe Lombardf24be422017-04-12 16:34:07 +0200191
Christophe Lombard797625d2017-06-13 17:41:05 +0200192 if (cxl_is_power9()) {
Christophe Lombardf24be422017-04-12 16:34:07 +0200193 /*
194 * Make sure there is a valid problem state
195 * area space for this AFU.
196 */
197 if (ctx->master && !ctx->afu->psa) {
198 pr_devel("AFU doesn't support mmio space\n");
199 return -EINVAL;
200 }
201
202 /* Can't mmap until the AFU is enabled */
203 if (!ctx->afu->enabled)
204 return -EBUSY;
205 }
Ian Munsie5caaf532015-07-07 15:45:46 +1000206 } else {
207 if (start + len > ctx->psn_size)
208 return -EINVAL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100209
Christophe Lombardf24be422017-04-12 16:34:07 +0200210 /* Make sure there is a valid per process space for this AFU */
Ian Munsie0712dc72015-01-07 16:33:04 +1100211 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
212 pr_devel("AFU doesn't support mmio space\n");
213 return -EINVAL;
214 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100215
Ian Munsie0712dc72015-01-07 16:33:04 +1100216 /* Can't mmap until the AFU is enabled */
217 if (!ctx->afu->enabled)
218 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100219 }
220
Ian Munsief204e0b2014-10-08 19:55:02 +1100221 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
222 ctx->psn_phys, ctx->pe , ctx->master);
223
Ian Munsie0712dc72015-01-07 16:33:04 +1100224 vma->vm_flags |= VM_IO | VM_PFNMAP;
Ian Munsief204e0b2014-10-08 19:55:02 +1100225 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Ian Munsie0712dc72015-01-07 16:33:04 +1100226 vma->vm_ops = &cxl_mmap_vmops;
227 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100228}
229
230/*
231 * Detach a context from the hardware. This disables interrupts and doesn't
232 * return until all outstanding interrupts for this context have completed. The
233 * hardware should no longer access *ctx after this has returned.
234 */
Michael Neulingeda36932015-05-27 16:07:08 +1000235int __detach_context(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100236{
237 enum cxl_context_status status;
238
239 mutex_lock(&ctx->status_mutex);
240 status = ctx->status;
241 ctx->status = CLOSED;
242 mutex_unlock(&ctx->status_mutex);
243 if (status != STARTED)
Michael Neulingeda36932015-05-27 16:07:08 +1000244 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100245
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000246 /* Only warn if we detached while the link was OK.
247 * If detach fails when hw is down, we don't care.
248 */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100249 WARN_ON(cxl_ops->detach_process(ctx) &&
Christophe Lombard0d400f72016-03-04 12:26:41 +0100250 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000251 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530252
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000253 /*
254 * Wait until no further interrupts are presented by the PSL
255 * for this context.
256 */
257 if (cxl_ops->irq_wait)
258 cxl_ops->irq_wait(ctx);
259
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530260 /* release the reference to the group leader and mm handling pid */
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000261 put_pid(ctx->pid);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530262
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000263 cxl_ctx_put();
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530264
265 /* Decrease the attached context count on the adapter */
266 cxl_adapter_context_put(ctx->afu->adapter);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200267
268 /* Decrease the mm count on the context */
269 cxl_context_mm_count_put(ctx);
270 ctx->mm = NULL;
271
Michael Neulingeda36932015-05-27 16:07:08 +1000272 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100273}
274
275/*
276 * Detach the given context from the AFU. This doesn't actually
277 * free the context but it should stop the context running in hardware
278 * (ie. prevent this context from generating any further interrupts
279 * so that it can be freed).
280 */
281void cxl_context_detach(struct cxl_context *ctx)
282{
Michael Neulingeda36932015-05-27 16:07:08 +1000283 int rc;
284
285 rc = __detach_context(ctx);
286 if (rc)
287 return;
288
289 afu_release_irqs(ctx, ctx);
Michael Neulingeda36932015-05-27 16:07:08 +1000290 wake_up_all(&ctx->wq);
Ian Munsief204e0b2014-10-08 19:55:02 +1100291}
292
293/*
294 * Detach all contexts on the given AFU.
295 */
296void cxl_context_detach_all(struct cxl_afu *afu)
297{
298 struct cxl_context *ctx;
299 int tmp;
300
Ian Munsieee41d112014-12-08 19:17:55 +1100301 mutex_lock(&afu->contexts_lock);
302 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100303 /*
304 * Anything done in here needs to be setup before the IDR is
305 * created and torn down after the IDR removed
306 */
Michael Neulingeda36932015-05-27 16:07:08 +1000307 cxl_context_detach(ctx);
Ian Munsie0712dc72015-01-07 16:33:04 +1100308
309 /*
310 * We are force detaching - remove any active PSA mappings so
311 * userspace cannot interfere with the card if it comes back.
312 * Easiest way to exercise this is to unbind and rebind the
313 * driver via sysfs while it is in use.
314 */
315 mutex_lock(&ctx->mapping_lock);
316 if (ctx->mapping)
317 unmap_mapping_range(ctx->mapping, 0, 0, 1);
318 mutex_unlock(&ctx->mapping_lock);
Ian Munsieee41d112014-12-08 19:17:55 +1100319 }
320 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100321}
322
Ian Munsie8ac75b92015-05-08 22:55:18 +1000323static void reclaim_ctx(struct rcu_head *rcu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100324{
Ian Munsie8ac75b92015-05-08 22:55:18 +1000325 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100326
Christophe Lombard797625d2017-06-13 17:41:05 +0200327 if (cxl_is_power8())
Christophe Lombardabd1d992017-04-07 16:11:58 +0200328 free_page((u64)ctx->sstp);
Ian Munsied9232a32015-07-23 16:43:56 +1000329 if (ctx->ff_page)
330 __free_page(ctx->ff_page);
Ian Munsief204e0b2014-10-08 19:55:02 +1100331 ctx->sstp = NULL;
332
Markus Elfring1050e682015-11-06 11:00:23 +0100333 kfree(ctx->irq_bitmap);
Andrew Donnellan52adee52015-09-30 11:58:06 +1000334
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530335 /* Drop ref to the afu device taken during cxl_context_init */
336 cxl_afu_put(ctx->afu);
337
Ian Munsief204e0b2014-10-08 19:55:02 +1100338 kfree(ctx);
339}
Ian Munsie8ac75b92015-05-08 22:55:18 +1000340
341void cxl_context_free(struct cxl_context *ctx)
342{
Frederic Barratbdecf762016-11-18 23:00:31 +1100343 if (ctx->kernelapi && ctx->mapping)
344 cxl_release_mapping(ctx);
Ian Munsie8ac75b92015-05-08 22:55:18 +1000345 mutex_lock(&ctx->afu->contexts_lock);
346 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
347 mutex_unlock(&ctx->afu->contexts_lock);
348 call_rcu(&ctx->rcu, reclaim_ctx);
349}
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200350
351void cxl_context_mm_count_get(struct cxl_context *ctx)
352{
353 if (ctx->mm)
354 atomic_inc(&ctx->mm->mm_count);
355}
356
357void cxl_context_mm_count_put(struct cxl_context *ctx)
358{
359 if (ctx->mm)
360 mmdrop(ctx->mm);
361}