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.
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>
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>
27 * Allocates space for a CXL context.
29 struct cxl_context
*cxl_context_alloc(void)
31 return kzalloc(sizeof(struct cxl_context
), GFP_KERNEL
);
35 * Initialises a CXL context.
37 int cxl_context_init(struct cxl_context
*ctx
, struct cxl_afu
*afu
, bool master
)
41 spin_lock_init(&ctx
->sste_lock
);
44 ctx
->pid
= ctx
->glpid
= NULL
; /* Set in start work ioctl */
45 mutex_init(&ctx
->mapping_lock
);
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).
55 i
= cxl_alloc_sst(ctx
);
59 INIT_WORK(&ctx
->fault_work
, cxl_handle_fault
);
61 init_waitqueue_head(&ctx
->wq
);
62 spin_lock_init(&ctx
->lock
);
64 ctx
->irq_bitmap
= NULL
;
65 ctx
->pending_irq
= false;
66 ctx
->pending_fault
= false;
67 ctx
->pending_afu_err
= false;
69 INIT_LIST_HEAD(&ctx
->irq_names
);
70 INIT_LIST_HEAD(&ctx
->extra_irq_contexts
);
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.
79 for (i
= 0; i
< CXL_IRQ_RANGES
; i
++)
80 ctx
->irqs
.range
[i
] = 0;
82 mutex_init(&ctx
->status_mutex
);
87 * Allocating IDR! We better make sure everything's setup that
88 * dereferences from it.
90 mutex_lock(&afu
->contexts_lock
);
91 idr_preload(GFP_KERNEL
);
92 i
= idr_alloc(&ctx
->afu
->contexts_idr
, ctx
, ctx
->afu
->adapter
->min_pe
,
93 ctx
->afu
->num_procs
, GFP_NOWAIT
);
95 mutex_unlock(&afu
->contexts_lock
);
100 if (cpu_has_feature(CPU_FTR_HVMODE
)) {
101 ctx
->elem
= &ctx
->afu
->native
->spa
[i
];
102 ctx
->external_pe
= ctx
->pe
;
104 ctx
->external_pe
= -1; /* assigned when attaching */
106 ctx
->pe_inserted
= false;
109 * take a ref on the afu so that it stays alive at-least till
110 * this context is reclaimed inside reclaim_ctx.
116 void cxl_context_set_mapping(struct cxl_context
*ctx
,
117 struct address_space
*mapping
)
119 mutex_lock(&ctx
->mapping_lock
);
120 ctx
->mapping
= mapping
;
121 mutex_unlock(&ctx
->mapping_lock
);
124 static int cxl_mmap_fault(struct vm_fault
*vmf
)
126 struct vm_area_struct
*vma
= vmf
->vma
;
127 struct cxl_context
*ctx
= vma
->vm_file
->private_data
;
130 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
132 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
133 __func__
, ctx
->pe
, vmf
->address
, offset
);
135 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
) {
136 area
= ctx
->afu
->psn_phys
;
137 if (offset
>= ctx
->afu
->adapter
->ps_size
)
138 return VM_FAULT_SIGBUS
;
140 area
= ctx
->psn_phys
;
141 if (offset
>= ctx
->psn_size
)
142 return VM_FAULT_SIGBUS
;
145 mutex_lock(&ctx
->status_mutex
);
147 if (ctx
->status
!= STARTED
) {
148 mutex_unlock(&ctx
->status_mutex
);
149 pr_devel("%s: Context not started, failing problem state access\n", __func__
);
150 if (ctx
->mmio_err_ff
) {
152 ctx
->ff_page
= alloc_page(GFP_USER
);
155 memset(page_address(ctx
->ff_page
), 0xff, PAGE_SIZE
);
157 get_page(ctx
->ff_page
);
158 vmf
->page
= ctx
->ff_page
;
159 vma
->vm_page_prot
= pgprot_cached(vma
->vm_page_prot
);
162 return VM_FAULT_SIGBUS
;
165 vm_insert_pfn(vma
, vmf
->address
, (area
+ offset
) >> PAGE_SHIFT
);
167 mutex_unlock(&ctx
->status_mutex
);
169 return VM_FAULT_NOPAGE
;
172 static const struct vm_operations_struct cxl_mmap_vmops
= {
173 .fault
= cxl_mmap_fault
,
177 * Map a per-context mmio space into the given vma.
179 int cxl_context_iomap(struct cxl_context
*ctx
, struct vm_area_struct
*vma
)
181 u64 start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
182 u64 len
= vma
->vm_end
- vma
->vm_start
;
184 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
) {
185 if (start
+ len
> ctx
->afu
->adapter
->ps_size
)
188 if (start
+ len
> ctx
->psn_size
)
192 if (ctx
->afu
->current_mode
!= CXL_MODE_DEDICATED
) {
193 /* make sure there is a valid per process space for this AFU */
194 if ((ctx
->master
&& !ctx
->afu
->psa
) || (!ctx
->afu
->pp_psa
)) {
195 pr_devel("AFU doesn't support mmio space\n");
199 /* Can't mmap until the AFU is enabled */
200 if (!ctx
->afu
->enabled
)
204 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__
,
205 ctx
->psn_phys
, ctx
->pe
, ctx
->master
);
207 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
;
208 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
209 vma
->vm_ops
= &cxl_mmap_vmops
;
214 * Detach a context from the hardware. This disables interrupts and doesn't
215 * return until all outstanding interrupts for this context have completed. The
216 * hardware should no longer access *ctx after this has returned.
218 int __detach_context(struct cxl_context
*ctx
)
220 enum cxl_context_status status
;
222 mutex_lock(&ctx
->status_mutex
);
223 status
= ctx
->status
;
224 ctx
->status
= CLOSED
;
225 mutex_unlock(&ctx
->status_mutex
);
226 if (status
!= STARTED
)
229 /* Only warn if we detached while the link was OK.
230 * If detach fails when hw is down, we don't care.
232 WARN_ON(cxl_ops
->detach_process(ctx
) &&
233 cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
));
234 flush_work(&ctx
->fault_work
); /* Only needed for dedicated process */
237 * Wait until no further interrupts are presented by the PSL
240 if (cxl_ops
->irq_wait
)
241 cxl_ops
->irq_wait(ctx
);
243 /* release the reference to the group leader and mm handling pid */
249 /* Decrease the attached context count on the adapter */
250 cxl_adapter_context_put(ctx
->afu
->adapter
);
255 * Detach the given context from the AFU. This doesn't actually
256 * free the context but it should stop the context running in hardware
257 * (ie. prevent this context from generating any further interrupts
258 * so that it can be freed).
260 void cxl_context_detach(struct cxl_context
*ctx
)
264 rc
= __detach_context(ctx
);
268 afu_release_irqs(ctx
, ctx
);
269 wake_up_all(&ctx
->wq
);
273 * Detach all contexts on the given AFU.
275 void cxl_context_detach_all(struct cxl_afu
*afu
)
277 struct cxl_context
*ctx
;
280 mutex_lock(&afu
->contexts_lock
);
281 idr_for_each_entry(&afu
->contexts_idr
, ctx
, tmp
) {
283 * Anything done in here needs to be setup before the IDR is
284 * created and torn down after the IDR removed
286 cxl_context_detach(ctx
);
289 * We are force detaching - remove any active PSA mappings so
290 * userspace cannot interfere with the card if it comes back.
291 * Easiest way to exercise this is to unbind and rebind the
292 * driver via sysfs while it is in use.
294 mutex_lock(&ctx
->mapping_lock
);
296 unmap_mapping_range(ctx
->mapping
, 0, 0, 1);
297 mutex_unlock(&ctx
->mapping_lock
);
299 mutex_unlock(&afu
->contexts_lock
);
302 static void reclaim_ctx(struct rcu_head
*rcu
)
304 struct cxl_context
*ctx
= container_of(rcu
, struct cxl_context
, rcu
);
306 free_page((u64
)ctx
->sstp
);
308 __free_page(ctx
->ff_page
);
311 kfree(ctx
->irq_bitmap
);
313 /* Drop ref to the afu device taken during cxl_context_init */
314 cxl_afu_put(ctx
->afu
);
319 void cxl_context_free(struct cxl_context
*ctx
)
321 if (ctx
->kernelapi
&& ctx
->mapping
)
322 cxl_release_mapping(ctx
);
323 mutex_lock(&ctx
->afu
->contexts_lock
);
324 idr_remove(&ctx
->afu
->contexts_idr
, ctx
->pe
);
325 mutex_unlock(&ctx
->afu
->contexts_lock
);
326 call_rcu(&ctx
->rcu
, reclaim_ctx
);