blob: 12a41b2753f05721350982a5041d292ed5149d08 [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>
Frederic Barrat03b8abe2017-09-03 20:15:13 +020021#include <linux/mmu_context.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110022#include <asm/cputable.h>
23#include <asm/current.h>
24#include <asm/copro.h>
25
26#include "cxl.h"
27
28/*
29 * Allocates space for a CXL context.
30 */
31struct cxl_context *cxl_context_alloc(void)
32{
33 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
34}
35
36/*
37 * Initialises a CXL context.
38 */
Frederic Barratbdecf762016-11-18 23:00:31 +110039int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
Ian Munsief204e0b2014-10-08 19:55:02 +110040{
41 int i;
42
Ian Munsief204e0b2014-10-08 19:55:02 +110043 ctx->afu = afu;
44 ctx->master = master;
Christophe Lombard6dd2d232017-04-07 16:11:55 +020045 ctx->pid = NULL; /* Set in start work ioctl */
Ian Munsieb1234292014-12-08 19:18:01 +110046 mutex_init(&ctx->mapping_lock);
Frederic Barratbdecf762016-11-18 23:00:31 +110047 ctx->mapping = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +110048
Christophe Lombard797625d2017-06-13 17:41:05 +020049 if (cxl_is_power8()) {
Christophe Lombardabd1d992017-04-07 16:11:58 +020050 spin_lock_init(&ctx->sste_lock);
51
52 /*
53 * Allocate the segment table before we put it in the IDR so that we
54 * can always access it when dereferenced from IDR. For the same
55 * reason, the segment table is only destroyed after the context is
56 * removed from the IDR. Access to this in the IOCTL is protected by
57 * Linux filesytem symantics (can't IOCTL until open is complete).
58 */
59 i = cxl_alloc_sst(ctx);
60 if (i)
61 return i;
62 }
Ian Munsief204e0b2014-10-08 19:55:02 +110063
64 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
65
66 init_waitqueue_head(&ctx->wq);
67 spin_lock_init(&ctx->lock);
68
69 ctx->irq_bitmap = NULL;
70 ctx->pending_irq = false;
71 ctx->pending_fault = false;
72 ctx->pending_afu_err = false;
73
Ian Munsief5c9df92016-06-30 04:55:17 +100074 INIT_LIST_HEAD(&ctx->irq_names);
Ian Munsiecbce0912016-07-14 07:17:09 +100075 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
Ian Munsief5c9df92016-06-30 04:55:17 +100076
Ian Munsief204e0b2014-10-08 19:55:02 +110077 /*
78 * When we have to destroy all contexts in cxl_context_detach_all() we
79 * end up with afu_release_irqs() called from inside a
80 * idr_for_each_entry(). Hence we need to make sure that anything
81 * dereferenced from this IDR is ok before we allocate the IDR here.
82 * This clears out the IRQ ranges to ensure this.
83 */
84 for (i = 0; i < CXL_IRQ_RANGES; i++)
85 ctx->irqs.range[i] = 0;
86
87 mutex_init(&ctx->status_mutex);
88
89 ctx->status = OPENED;
90
91 /*
92 * Allocating IDR! We better make sure everything's setup that
93 * dereferences from it.
94 */
Ian Munsieee41d112014-12-08 19:17:55 +110095 mutex_lock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +110096 idr_preload(GFP_KERNEL);
Andrew Donnellan16479332016-07-28 15:39:41 +100097 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
Ian Munsief204e0b2014-10-08 19:55:02 +110098 ctx->afu->num_procs, GFP_NOWAIT);
Ian Munsief204e0b2014-10-08 19:55:02 +110099 idr_preload_end();
Ian Munsieee41d112014-12-08 19:17:55 +1100100 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100101 if (i < 0)
102 return i;
103
104 ctx->pe = i;
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100105 if (cpu_has_feature(CPU_FTR_HVMODE)) {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100106 ctx->elem = &ctx->afu->native->spa[i];
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100107 ctx->external_pe = ctx->pe;
108 } else {
109 ctx->external_pe = -1; /* assigned when attaching */
110 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100111 ctx->pe_inserted = false;
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530112
113 /*
114 * take a ref on the afu so that it stays alive at-least till
115 * this context is reclaimed inside reclaim_ctx.
116 */
117 cxl_afu_get(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100118 return 0;
119}
120
Frederic Barratbdecf762016-11-18 23:00:31 +1100121void cxl_context_set_mapping(struct cxl_context *ctx,
122 struct address_space *mapping)
123{
124 mutex_lock(&ctx->mapping_lock);
125 ctx->mapping = mapping;
126 mutex_unlock(&ctx->mapping_lock);
127}
128
Dave Jiang11bac802017-02-24 14:56:41 -0800129static int cxl_mmap_fault(struct vm_fault *vmf)
Ian Munsie0712dc72015-01-07 16:33:04 +1100130{
Dave Jiang11bac802017-02-24 14:56:41 -0800131 struct vm_area_struct *vma = vmf->vma;
Ian Munsie0712dc72015-01-07 16:33:04 +1100132 struct cxl_context *ctx = vma->vm_file->private_data;
Ian Munsie0712dc72015-01-07 16:33:04 +1100133 u64 area, offset;
134
135 offset = vmf->pgoff << PAGE_SHIFT;
136
137 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
Jan Kara1a29d852016-12-14 15:07:01 -0800138 __func__, ctx->pe, vmf->address, offset);
Ian Munsie0712dc72015-01-07 16:33:04 +1100139
140 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
141 area = ctx->afu->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000142 if (offset >= ctx->afu->adapter->ps_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100143 return VM_FAULT_SIGBUS;
144 } else {
145 area = ctx->psn_phys;
Ian Munsie10a58942015-07-07 15:45:45 +1000146 if (offset >= ctx->psn_size)
Ian Munsie0712dc72015-01-07 16:33:04 +1100147 return VM_FAULT_SIGBUS;
148 }
149
150 mutex_lock(&ctx->status_mutex);
151
152 if (ctx->status != STARTED) {
153 mutex_unlock(&ctx->status_mutex);
154 pr_devel("%s: Context not started, failing problem state access\n", __func__);
Ian Munsied9232a32015-07-23 16:43:56 +1000155 if (ctx->mmio_err_ff) {
156 if (!ctx->ff_page) {
157 ctx->ff_page = alloc_page(GFP_USER);
158 if (!ctx->ff_page)
159 return VM_FAULT_OOM;
160 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
161 }
162 get_page(ctx->ff_page);
163 vmf->page = ctx->ff_page;
164 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
165 return 0;
166 }
Ian Munsie0712dc72015-01-07 16:33:04 +1100167 return VM_FAULT_SIGBUS;
168 }
169
Jan Kara1a29d852016-12-14 15:07:01 -0800170 vm_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
Ian Munsie0712dc72015-01-07 16:33:04 +1100171
172 mutex_unlock(&ctx->status_mutex);
173
174 return VM_FAULT_NOPAGE;
175}
176
177static const struct vm_operations_struct cxl_mmap_vmops = {
178 .fault = cxl_mmap_fault,
179};
180
Ian Munsief204e0b2014-10-08 19:55:02 +1100181/*
182 * Map a per-context mmio space into the given vma.
183 */
184int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
185{
Ian Munsie5caaf532015-07-07 15:45:46 +1000186 u64 start = vma->vm_pgoff << PAGE_SHIFT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100187 u64 len = vma->vm_end - vma->vm_start;
Ian Munsie5caaf532015-07-07 15:45:46 +1000188
189 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
190 if (start + len > ctx->afu->adapter->ps_size)
191 return -EINVAL;
Christophe Lombardf24be422017-04-12 16:34:07 +0200192
Christophe Lombard797625d2017-06-13 17:41:05 +0200193 if (cxl_is_power9()) {
Christophe Lombardf24be422017-04-12 16:34:07 +0200194 /*
195 * Make sure there is a valid problem state
196 * area space for this AFU.
197 */
198 if (ctx->master && !ctx->afu->psa) {
199 pr_devel("AFU doesn't support mmio space\n");
200 return -EINVAL;
201 }
202
203 /* Can't mmap until the AFU is enabled */
204 if (!ctx->afu->enabled)
205 return -EBUSY;
206 }
Ian Munsie5caaf532015-07-07 15:45:46 +1000207 } else {
208 if (start + len > ctx->psn_size)
209 return -EINVAL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100210
Christophe Lombardf24be422017-04-12 16:34:07 +0200211 /* Make sure there is a valid per process space for this AFU */
Ian Munsie0712dc72015-01-07 16:33:04 +1100212 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
213 pr_devel("AFU doesn't support mmio space\n");
214 return -EINVAL;
215 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100216
Ian Munsie0712dc72015-01-07 16:33:04 +1100217 /* Can't mmap until the AFU is enabled */
218 if (!ctx->afu->enabled)
219 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100220 }
221
Ian Munsief204e0b2014-10-08 19:55:02 +1100222 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
223 ctx->psn_phys, ctx->pe , ctx->master);
224
Ian Munsie0712dc72015-01-07 16:33:04 +1100225 vma->vm_flags |= VM_IO | VM_PFNMAP;
Ian Munsief204e0b2014-10-08 19:55:02 +1100226 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Ian Munsie0712dc72015-01-07 16:33:04 +1100227 vma->vm_ops = &cxl_mmap_vmops;
228 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100229}
230
231/*
232 * Detach a context from the hardware. This disables interrupts and doesn't
233 * return until all outstanding interrupts for this context have completed. The
234 * hardware should no longer access *ctx after this has returned.
235 */
Michael Neulingeda36932015-05-27 16:07:08 +1000236int __detach_context(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100237{
238 enum cxl_context_status status;
239
240 mutex_lock(&ctx->status_mutex);
241 status = ctx->status;
242 ctx->status = CLOSED;
243 mutex_unlock(&ctx->status_mutex);
244 if (status != STARTED)
Michael Neulingeda36932015-05-27 16:07:08 +1000245 return -EBUSY;
Ian Munsief204e0b2014-10-08 19:55:02 +1100246
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000247 /* Only warn if we detached while the link was OK.
248 * If detach fails when hw is down, we don't care.
249 */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100250 WARN_ON(cxl_ops->detach_process(ctx) &&
Christophe Lombard0d400f72016-03-04 12:26:41 +0100251 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000252 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530253
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000254 /*
255 * Wait until no further interrupts are presented by the PSL
256 * for this context.
257 */
258 if (cxl_ops->irq_wait)
259 cxl_ops->irq_wait(ctx);
260
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530261 /* release the reference to the group leader and mm handling pid */
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000262 put_pid(ctx->pid);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530263
Michael Neuling7bb5d91a2015-05-27 16:07:14 +1000264 cxl_ctx_put();
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530265
266 /* Decrease the attached context count on the adapter */
267 cxl_adapter_context_put(ctx->afu->adapter);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200268
269 /* Decrease the mm count on the context */
270 cxl_context_mm_count_put(ctx);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200271 if (ctx->mm)
272 mm_context_remove_copro(ctx->mm);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200273 ctx->mm = NULL;
274
Michael Neulingeda36932015-05-27 16:07:08 +1000275 return 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100276}
277
278/*
279 * Detach the given context from the AFU. This doesn't actually
280 * free the context but it should stop the context running in hardware
281 * (ie. prevent this context from generating any further interrupts
282 * so that it can be freed).
283 */
284void cxl_context_detach(struct cxl_context *ctx)
285{
Michael Neulingeda36932015-05-27 16:07:08 +1000286 int rc;
287
288 rc = __detach_context(ctx);
289 if (rc)
290 return;
291
292 afu_release_irqs(ctx, ctx);
Michael Neulingeda36932015-05-27 16:07:08 +1000293 wake_up_all(&ctx->wq);
Ian Munsief204e0b2014-10-08 19:55:02 +1100294}
295
296/*
297 * Detach all contexts on the given AFU.
298 */
299void cxl_context_detach_all(struct cxl_afu *afu)
300{
301 struct cxl_context *ctx;
302 int tmp;
303
Ian Munsieee41d112014-12-08 19:17:55 +1100304 mutex_lock(&afu->contexts_lock);
305 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100306 /*
307 * Anything done in here needs to be setup before the IDR is
308 * created and torn down after the IDR removed
309 */
Michael Neulingeda36932015-05-27 16:07:08 +1000310 cxl_context_detach(ctx);
Ian Munsie0712dc72015-01-07 16:33:04 +1100311
312 /*
313 * We are force detaching - remove any active PSA mappings so
314 * userspace cannot interfere with the card if it comes back.
315 * Easiest way to exercise this is to unbind and rebind the
316 * driver via sysfs while it is in use.
317 */
318 mutex_lock(&ctx->mapping_lock);
319 if (ctx->mapping)
320 unmap_mapping_range(ctx->mapping, 0, 0, 1);
321 mutex_unlock(&ctx->mapping_lock);
Ian Munsieee41d112014-12-08 19:17:55 +1100322 }
323 mutex_unlock(&afu->contexts_lock);
Ian Munsief204e0b2014-10-08 19:55:02 +1100324}
325
Ian Munsie8ac75b92015-05-08 22:55:18 +1000326static void reclaim_ctx(struct rcu_head *rcu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100327{
Ian Munsie8ac75b92015-05-08 22:55:18 +1000328 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100329
Christophe Lombard797625d2017-06-13 17:41:05 +0200330 if (cxl_is_power8())
Christophe Lombardabd1d992017-04-07 16:11:58 +0200331 free_page((u64)ctx->sstp);
Ian Munsied9232a32015-07-23 16:43:56 +1000332 if (ctx->ff_page)
333 __free_page(ctx->ff_page);
Ian Munsief204e0b2014-10-08 19:55:02 +1100334 ctx->sstp = NULL;
335
Markus Elfring1050e682015-11-06 11:00:23 +0100336 kfree(ctx->irq_bitmap);
Andrew Donnellan52adee52015-09-30 11:58:06 +1000337
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530338 /* Drop ref to the afu device taken during cxl_context_init */
339 cxl_afu_put(ctx->afu);
340
Ian Munsief204e0b2014-10-08 19:55:02 +1100341 kfree(ctx);
342}
Ian Munsie8ac75b92015-05-08 22:55:18 +1000343
344void cxl_context_free(struct cxl_context *ctx)
345{
Frederic Barratbdecf762016-11-18 23:00:31 +1100346 if (ctx->kernelapi && ctx->mapping)
347 cxl_release_mapping(ctx);
Ian Munsie8ac75b92015-05-08 22:55:18 +1000348 mutex_lock(&ctx->afu->contexts_lock);
349 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
350 mutex_unlock(&ctx->afu->contexts_lock);
351 call_rcu(&ctx->rcu, reclaim_ctx);
352}
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200353
354void cxl_context_mm_count_get(struct cxl_context *ctx)
355{
356 if (ctx->mm)
357 atomic_inc(&ctx->mm->mm_count);
358}
359
360void cxl_context_mm_count_put(struct cxl_context *ctx)
361{
362 if (ctx->mm)
363 mmdrop(ctx->mm);
364}