perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / misc / cxl / context.c
blob5fe529b43ebef71d1a23bf11c75877e21ee4a1f9
1 /*
2 * Copyright 2014 IBM Corp.
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 */
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>
26 #include "cxl.h"
29 * Allocates space for a CXL context.
31 struct cxl_context *cxl_context_alloc(void)
33 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
37 * Initialises a CXL context.
39 int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
41 int i;
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;
51 if (cxl_is_power8()) {
52 spin_lock_init(&ctx->sste_lock);
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).
61 i = cxl_alloc_sst(ctx);
62 if (i)
63 return i;
66 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
68 init_waitqueue_head(&ctx->wq);
69 spin_lock_init(&ctx->lock);
71 ctx->irq_bitmap = NULL;
72 ctx->pending_irq = false;
73 ctx->pending_fault = false;
74 ctx->pending_afu_err = false;
76 INIT_LIST_HEAD(&ctx->irq_names);
79 * When we have to destroy all contexts in cxl_context_detach_all() we
80 * end up with afu_release_irqs() called from inside a
81 * idr_for_each_entry(). Hence we need to make sure that anything
82 * dereferenced from this IDR is ok before we allocate the IDR here.
83 * This clears out the IRQ ranges to ensure this.
85 for (i = 0; i < CXL_IRQ_RANGES; i++)
86 ctx->irqs.range[i] = 0;
88 mutex_init(&ctx->status_mutex);
90 ctx->status = OPENED;
93 * Allocating IDR! We better make sure everything's setup that
94 * dereferences from it.
96 mutex_lock(&afu->contexts_lock);
97 idr_preload(GFP_KERNEL);
98 i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
99 ctx->afu->num_procs, GFP_NOWAIT);
100 idr_preload_end();
101 mutex_unlock(&afu->contexts_lock);
102 if (i < 0)
103 return i;
105 ctx->pe = i;
106 if (cpu_has_feature(CPU_FTR_HVMODE)) {
107 ctx->elem = &ctx->afu->native->spa[i];
108 ctx->external_pe = ctx->pe;
109 } else {
110 ctx->external_pe = -1; /* assigned when attaching */
112 ctx->pe_inserted = false;
115 * take a ref on the afu so that it stays alive at-least till
116 * this context is reclaimed inside reclaim_ctx.
118 cxl_afu_get(afu);
119 return 0;
122 void cxl_context_set_mapping(struct cxl_context *ctx,
123 struct address_space *mapping)
125 mutex_lock(&ctx->mapping_lock);
126 ctx->mapping = mapping;
127 mutex_unlock(&ctx->mapping_lock);
130 static vm_fault_t cxl_mmap_fault(struct vm_fault *vmf)
132 struct vm_area_struct *vma = vmf->vma;
133 struct cxl_context *ctx = vma->vm_file->private_data;
134 u64 area, offset;
135 vm_fault_t ret;
137 offset = vmf->pgoff << PAGE_SHIFT;
139 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
140 __func__, ctx->pe, vmf->address, offset);
142 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
143 area = ctx->afu->psn_phys;
144 if (offset >= ctx->afu->adapter->ps_size)
145 return VM_FAULT_SIGBUS;
146 } else {
147 area = ctx->psn_phys;
148 if (offset >= ctx->psn_size)
149 return VM_FAULT_SIGBUS;
152 mutex_lock(&ctx->status_mutex);
154 if (ctx->status != STARTED) {
155 mutex_unlock(&ctx->status_mutex);
156 pr_devel("%s: Context not started, failing problem state access\n", __func__);
157 if (ctx->mmio_err_ff) {
158 if (!ctx->ff_page) {
159 ctx->ff_page = alloc_page(GFP_USER);
160 if (!ctx->ff_page)
161 return VM_FAULT_OOM;
162 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
164 get_page(ctx->ff_page);
165 vmf->page = ctx->ff_page;
166 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
167 return 0;
169 return VM_FAULT_SIGBUS;
172 ret = vmf_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
174 mutex_unlock(&ctx->status_mutex);
176 return ret;
179 static const struct vm_operations_struct cxl_mmap_vmops = {
180 .fault = cxl_mmap_fault,
184 * Map a per-context mmio space into the given vma.
186 int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
188 u64 start = vma->vm_pgoff << PAGE_SHIFT;
189 u64 len = vma->vm_end - vma->vm_start;
191 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
192 if (start + len > ctx->afu->adapter->ps_size)
193 return -EINVAL;
195 if (cxl_is_power9()) {
197 * Make sure there is a valid problem state
198 * area space for this AFU.
200 if (ctx->master && !ctx->afu->psa) {
201 pr_devel("AFU doesn't support mmio space\n");
202 return -EINVAL;
205 /* Can't mmap until the AFU is enabled */
206 if (!ctx->afu->enabled)
207 return -EBUSY;
209 } else {
210 if (start + len > ctx->psn_size)
211 return -EINVAL;
213 /* Make sure there is a valid per process space for this AFU */
214 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
215 pr_devel("AFU doesn't support mmio space\n");
216 return -EINVAL;
219 /* Can't mmap until the AFU is enabled */
220 if (!ctx->afu->enabled)
221 return -EBUSY;
224 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
225 ctx->psn_phys, ctx->pe , ctx->master);
227 vma->vm_flags |= VM_IO | VM_PFNMAP;
228 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
229 vma->vm_ops = &cxl_mmap_vmops;
230 return 0;
234 * Detach a context from the hardware. This disables interrupts and doesn't
235 * return until all outstanding interrupts for this context have completed. The
236 * hardware should no longer access *ctx after this has returned.
238 int __detach_context(struct cxl_context *ctx)
240 enum cxl_context_status status;
242 mutex_lock(&ctx->status_mutex);
243 status = ctx->status;
244 ctx->status = CLOSED;
245 mutex_unlock(&ctx->status_mutex);
246 if (status != STARTED)
247 return -EBUSY;
249 /* Only warn if we detached while the link was OK.
250 * If detach fails when hw is down, we don't care.
252 WARN_ON(cxl_ops->detach_process(ctx) &&
253 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
254 flush_work(&ctx->fault_work); /* Only needed for dedicated process */
257 * Wait until no further interrupts are presented by the PSL
258 * for this context.
260 if (cxl_ops->irq_wait)
261 cxl_ops->irq_wait(ctx);
263 /* release the reference to the group leader and mm handling pid */
264 put_pid(ctx->pid);
266 cxl_ctx_put();
268 /* Decrease the attached context count on the adapter */
269 cxl_adapter_context_put(ctx->afu->adapter);
271 /* Decrease the mm count on the context */
272 cxl_context_mm_count_put(ctx);
273 if (ctx->mm)
274 mm_context_remove_copro(ctx->mm);
275 ctx->mm = NULL;
277 return 0;
281 * Detach the given context from the AFU. This doesn't actually
282 * free the context but it should stop the context running in hardware
283 * (ie. prevent this context from generating any further interrupts
284 * so that it can be freed).
286 void cxl_context_detach(struct cxl_context *ctx)
288 int rc;
290 rc = __detach_context(ctx);
291 if (rc)
292 return;
294 afu_release_irqs(ctx, ctx);
295 wake_up_all(&ctx->wq);
299 * Detach all contexts on the given AFU.
301 void cxl_context_detach_all(struct cxl_afu *afu)
303 struct cxl_context *ctx;
304 int tmp;
306 mutex_lock(&afu->contexts_lock);
307 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
309 * Anything done in here needs to be setup before the IDR is
310 * created and torn down after the IDR removed
312 cxl_context_detach(ctx);
315 * We are force detaching - remove any active PSA mappings so
316 * userspace cannot interfere with the card if it comes back.
317 * Easiest way to exercise this is to unbind and rebind the
318 * driver via sysfs while it is in use.
320 mutex_lock(&ctx->mapping_lock);
321 if (ctx->mapping)
322 unmap_mapping_range(ctx->mapping, 0, 0, 1);
323 mutex_unlock(&ctx->mapping_lock);
325 mutex_unlock(&afu->contexts_lock);
328 static void reclaim_ctx(struct rcu_head *rcu)
330 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
332 if (cxl_is_power8())
333 free_page((u64)ctx->sstp);
334 if (ctx->ff_page)
335 __free_page(ctx->ff_page);
336 ctx->sstp = NULL;
338 kfree(ctx->irq_bitmap);
340 /* Drop ref to the afu device taken during cxl_context_init */
341 cxl_afu_put(ctx->afu);
343 kfree(ctx);
346 void cxl_context_free(struct cxl_context *ctx)
348 if (ctx->kernelapi && ctx->mapping)
349 cxl_release_mapping(ctx);
350 mutex_lock(&ctx->afu->contexts_lock);
351 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
352 mutex_unlock(&ctx->afu->contexts_lock);
353 call_rcu(&ctx->rcu, reclaim_ctx);
356 void cxl_context_mm_count_get(struct cxl_context *ctx)
358 if (ctx->mm)
359 atomic_inc(&ctx->mm->mm_count);
362 void cxl_context_mm_count_put(struct cxl_context *ctx)
364 if (ctx->mm)
365 mmdrop(ctx->mm);