1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014 IBM Corp.
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/bitmap.h>
9 #include <linux/sched.h>
10 #include <linux/pid.h>
13 #include <linux/debugfs.h>
14 #include <linux/slab.h>
15 #include <linux/idr.h>
16 #include <linux/sched/mm.h>
17 #include <linux/mmu_context.h>
18 #include <asm/cputable.h>
19 #include <asm/current.h>
20 #include <asm/copro.h>
25 * Allocates space for a CXL context.
27 struct cxl_context
*cxl_context_alloc(void)
29 return kzalloc(sizeof(struct cxl_context
), GFP_KERNEL
);
33 * Initialises a CXL context.
35 int cxl_context_init(struct cxl_context
*ctx
, struct cxl_afu
*afu
, bool master
)
41 ctx
->pid
= NULL
; /* Set in start work ioctl */
42 mutex_init(&ctx
->mapping_lock
);
45 ctx
->assign_tidr
= false;
47 if (cxl_is_power8()) {
48 spin_lock_init(&ctx
->sste_lock
);
51 * Allocate the segment table before we put it in the IDR so that we
52 * can always access it when dereferenced from IDR. For the same
53 * reason, the segment table is only destroyed after the context is
54 * removed from the IDR. Access to this in the IOCTL is protected by
55 * Linux filesytem symantics (can't IOCTL until open is complete).
57 i
= cxl_alloc_sst(ctx
);
62 INIT_WORK(&ctx
->fault_work
, cxl_handle_fault
);
64 init_waitqueue_head(&ctx
->wq
);
65 spin_lock_init(&ctx
->lock
);
67 ctx
->irq_bitmap
= NULL
;
68 ctx
->pending_irq
= false;
69 ctx
->pending_fault
= false;
70 ctx
->pending_afu_err
= false;
72 INIT_LIST_HEAD(&ctx
->irq_names
);
75 * When we have to destroy all contexts in cxl_context_detach_all() we
76 * end up with afu_release_irqs() called from inside a
77 * idr_for_each_entry(). Hence we need to make sure that anything
78 * dereferenced from this IDR is ok before we allocate the IDR here.
79 * This clears out the IRQ ranges to ensure this.
81 for (i
= 0; i
< CXL_IRQ_RANGES
; i
++)
82 ctx
->irqs
.range
[i
] = 0;
84 mutex_init(&ctx
->status_mutex
);
89 * Allocating IDR! We better make sure everything's setup that
90 * dereferences from it.
92 mutex_lock(&afu
->contexts_lock
);
93 idr_preload(GFP_KERNEL
);
94 i
= idr_alloc(&ctx
->afu
->contexts_idr
, ctx
, 0,
95 ctx
->afu
->num_procs
, GFP_NOWAIT
);
97 mutex_unlock(&afu
->contexts_lock
);
102 if (cpu_has_feature(CPU_FTR_HVMODE
)) {
103 ctx
->elem
= &ctx
->afu
->native
->spa
[i
];
104 ctx
->external_pe
= ctx
->pe
;
106 ctx
->external_pe
= -1; /* assigned when attaching */
108 ctx
->pe_inserted
= false;
111 * take a ref on the afu so that it stays alive at-least till
112 * this context is reclaimed inside reclaim_ctx.
118 void cxl_context_set_mapping(struct cxl_context
*ctx
,
119 struct address_space
*mapping
)
121 mutex_lock(&ctx
->mapping_lock
);
122 ctx
->mapping
= mapping
;
123 mutex_unlock(&ctx
->mapping_lock
);
126 static vm_fault_t
cxl_mmap_fault(struct vm_fault
*vmf
)
128 struct vm_area_struct
*vma
= vmf
->vma
;
129 struct cxl_context
*ctx
= vma
->vm_file
->private_data
;
133 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
135 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
136 __func__
, ctx
->pe
, vmf
->address
, offset
);
138 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
) {
139 area
= ctx
->afu
->psn_phys
;
140 if (offset
>= ctx
->afu
->adapter
->ps_size
)
141 return VM_FAULT_SIGBUS
;
143 area
= ctx
->psn_phys
;
144 if (offset
>= ctx
->psn_size
)
145 return VM_FAULT_SIGBUS
;
148 mutex_lock(&ctx
->status_mutex
);
150 if (ctx
->status
!= STARTED
) {
151 mutex_unlock(&ctx
->status_mutex
);
152 pr_devel("%s: Context not started, failing problem state access\n", __func__
);
153 if (ctx
->mmio_err_ff
) {
155 ctx
->ff_page
= alloc_page(GFP_USER
);
158 memset(page_address(ctx
->ff_page
), 0xff, PAGE_SIZE
);
160 get_page(ctx
->ff_page
);
161 vmf
->page
= ctx
->ff_page
;
162 vma
->vm_page_prot
= pgprot_cached(vma
->vm_page_prot
);
165 return VM_FAULT_SIGBUS
;
168 ret
= vmf_insert_pfn(vma
, vmf
->address
, (area
+ offset
) >> PAGE_SHIFT
);
170 mutex_unlock(&ctx
->status_mutex
);
175 static const struct vm_operations_struct cxl_mmap_vmops
= {
176 .fault
= cxl_mmap_fault
,
180 * Map a per-context mmio space into the given vma.
182 int cxl_context_iomap(struct cxl_context
*ctx
, struct vm_area_struct
*vma
)
184 u64 start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
185 u64 len
= vma
->vm_end
- vma
->vm_start
;
187 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
) {
188 if (start
+ len
> ctx
->afu
->adapter
->ps_size
)
191 if (cxl_is_power9()) {
193 * Make sure there is a valid problem state
194 * area space for this AFU.
196 if (ctx
->master
&& !ctx
->afu
->psa
) {
197 pr_devel("AFU doesn't support mmio space\n");
201 /* Can't mmap until the AFU is enabled */
202 if (!ctx
->afu
->enabled
)
206 if (start
+ len
> ctx
->psn_size
)
209 /* Make sure there is a valid per process space for this AFU */
210 if ((ctx
->master
&& !ctx
->afu
->psa
) || (!ctx
->afu
->pp_psa
)) {
211 pr_devel("AFU doesn't support mmio space\n");
215 /* Can't mmap until the AFU is enabled */
216 if (!ctx
->afu
->enabled
)
220 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__
,
221 ctx
->psn_phys
, ctx
->pe
, ctx
->master
);
223 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
;
224 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
225 vma
->vm_ops
= &cxl_mmap_vmops
;
230 * Detach a context from the hardware. This disables interrupts and doesn't
231 * return until all outstanding interrupts for this context have completed. The
232 * hardware should no longer access *ctx after this has returned.
234 int __detach_context(struct cxl_context
*ctx
)
236 enum cxl_context_status status
;
238 mutex_lock(&ctx
->status_mutex
);
239 status
= ctx
->status
;
240 ctx
->status
= CLOSED
;
241 mutex_unlock(&ctx
->status_mutex
);
242 if (status
!= STARTED
)
245 /* Only warn if we detached while the link was OK.
246 * If detach fails when hw is down, we don't care.
248 WARN_ON(cxl_ops
->detach_process(ctx
) &&
249 cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
));
250 flush_work(&ctx
->fault_work
); /* Only needed for dedicated process */
253 * Wait until no further interrupts are presented by the PSL
256 if (cxl_ops
->irq_wait
)
257 cxl_ops
->irq_wait(ctx
);
259 /* release the reference to the group leader and mm handling pid */
264 /* Decrease the attached context count on the adapter */
265 cxl_adapter_context_put(ctx
->afu
->adapter
);
267 /* Decrease the mm count on the context */
268 cxl_context_mm_count_put(ctx
);
270 mm_context_remove_copro(ctx
->mm
);
277 * Detach the given context from the AFU. This doesn't actually
278 * free the context but it should stop the context running in hardware
279 * (ie. prevent this context from generating any further interrupts
280 * so that it can be freed).
282 void cxl_context_detach(struct cxl_context
*ctx
)
286 rc
= __detach_context(ctx
);
290 afu_release_irqs(ctx
, ctx
);
291 wake_up_all(&ctx
->wq
);
295 * Detach all contexts on the given AFU.
297 void cxl_context_detach_all(struct cxl_afu
*afu
)
299 struct cxl_context
*ctx
;
302 mutex_lock(&afu
->contexts_lock
);
303 idr_for_each_entry(&afu
->contexts_idr
, ctx
, tmp
) {
305 * Anything done in here needs to be setup before the IDR is
306 * created and torn down after the IDR removed
308 cxl_context_detach(ctx
);
311 * We are force detaching - remove any active PSA mappings so
312 * userspace cannot interfere with the card if it comes back.
313 * Easiest way to exercise this is to unbind and rebind the
314 * driver via sysfs while it is in use.
316 mutex_lock(&ctx
->mapping_lock
);
318 unmap_mapping_range(ctx
->mapping
, 0, 0, 1);
319 mutex_unlock(&ctx
->mapping_lock
);
321 mutex_unlock(&afu
->contexts_lock
);
324 static void reclaim_ctx(struct rcu_head
*rcu
)
326 struct cxl_context
*ctx
= container_of(rcu
, struct cxl_context
, rcu
);
329 free_page((u64
)ctx
->sstp
);
331 __free_page(ctx
->ff_page
);
334 kfree(ctx
->irq_bitmap
);
336 /* Drop ref to the afu device taken during cxl_context_init */
337 cxl_afu_put(ctx
->afu
);
342 void cxl_context_free(struct cxl_context
*ctx
)
344 if (ctx
->kernelapi
&& ctx
->mapping
)
345 cxl_release_mapping(ctx
);
346 mutex_lock(&ctx
->afu
->contexts_lock
);
347 idr_remove(&ctx
->afu
->contexts_idr
, ctx
->pe
);
348 mutex_unlock(&ctx
->afu
->contexts_lock
);
349 call_rcu(&ctx
->rcu
, reclaim_ctx
);
352 void cxl_context_mm_count_get(struct cxl_context
*ctx
)
358 void cxl_context_mm_count_put(struct cxl_context
*ctx
)