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