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 <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>
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
)
45 ctx
->pid
= NULL
; /* Set in start work ioctl */
46 mutex_init(&ctx
->mapping_lock
);
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
);
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
);
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
);
101 mutex_unlock(&afu
->contexts_lock
);
106 if (cpu_has_feature(CPU_FTR_HVMODE
)) {
107 ctx
->elem
= &ctx
->afu
->native
->spa
[i
];
108 ctx
->external_pe
= ctx
->pe
;
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.
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
;
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
;
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
) {
159 ctx
->ff_page
= alloc_page(GFP_USER
);
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
);
169 return VM_FAULT_SIGBUS
;
172 ret
= vmf_insert_pfn(vma
, vmf
->address
, (area
+ offset
) >> PAGE_SHIFT
);
174 mutex_unlock(&ctx
->status_mutex
);
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
)
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");
205 /* Can't mmap until the AFU is enabled */
206 if (!ctx
->afu
->enabled
)
210 if (start
+ len
> ctx
->psn_size
)
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");
219 /* Can't mmap until the AFU is enabled */
220 if (!ctx
->afu
->enabled
)
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
;
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
)
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
260 if (cxl_ops
->irq_wait
)
261 cxl_ops
->irq_wait(ctx
);
263 /* release the reference to the group leader and mm handling pid */
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
);
274 mm_context_remove_copro(ctx
->mm
);
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
)
290 rc
= __detach_context(ctx
);
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
;
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
);
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
);
333 free_page((u64
)ctx
->sstp
);
335 __free_page(ctx
->ff_page
);
338 kfree(ctx
->irq_bitmap
);
340 /* Drop ref to the afu device taken during cxl_context_init */
341 cxl_afu_put(ctx
->afu
);
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
)
359 atomic_inc(&ctx
->mm
->mm_count
);
362 void cxl_context_mm_count_put(struct cxl_context
*ctx
)