1
2
3
4
5
6
7
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
30
31struct cxl_context *cxl_context_alloc(void)
32{
33 return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
34}
35
36
37
38
39int 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;
46 mutex_init(&ctx->mapping_lock);
47 ctx->mapping = NULL;
48
49 if (cxl_is_power8()) {
50 spin_lock_init(&ctx->sste_lock);
51
52
53
54
55
56
57
58
59 i = cxl_alloc_sst(ctx);
60 if (i)
61 return i;
62 }
63
64 INIT_WORK(&ctx->fault_work, cxl_handle_fault);
65
66 init_waitqueue_head(&ctx->wq);
67 spin_lock_init(&ctx->lock);
68
69 ctx->irq_bitmap = NULL;
70 ctx->pending_irq = false;
71 ctx->pending_fault = false;
72 ctx->pending_afu_err = false;
73
74 INIT_LIST_HEAD(&ctx->irq_names);
75 INIT_LIST_HEAD(&ctx->extra_irq_contexts);
76
77
78
79
80
81
82
83
84 for (i = 0; i < CXL_IRQ_RANGES; i++)
85 ctx->irqs.range[i] = 0;
86
87 mutex_init(&ctx->status_mutex);
88
89 ctx->status = OPENED;
90
91
92
93
94
95 mutex_lock(&afu->contexts_lock);
96 idr_preload(GFP_KERNEL);
97 i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
98 ctx->afu->num_procs, GFP_NOWAIT);
99 idr_preload_end();
100 mutex_unlock(&afu->contexts_lock);
101 if (i < 0)
102 return i;
103
104 ctx->pe = i;
105 if (cpu_has_feature(CPU_FTR_HVMODE)) {
106 ctx->elem = &ctx->afu->native->spa[i];
107 ctx->external_pe = ctx->pe;
108 } else {
109 ctx->external_pe = -1;
110 }
111 ctx->pe_inserted = false;
112
113
114
115
116
117 cxl_afu_get(afu);
118 return 0;
119}
120
121void cxl_context_set_mapping(struct cxl_context *ctx,
122 struct address_space *mapping)
123{
124 mutex_lock(&ctx->mapping_lock);
125 ctx->mapping = mapping;
126 mutex_unlock(&ctx->mapping_lock);
127}
128
129static int cxl_mmap_fault(struct vm_fault *vmf)
130{
131 struct vm_area_struct *vma = vmf->vma;
132 struct cxl_context *ctx = vma->vm_file->private_data;
133 u64 area, offset;
134
135 offset = vmf->pgoff << PAGE_SHIFT;
136
137 pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
138 __func__, ctx->pe, vmf->address, offset);
139
140 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
141 area = ctx->afu->psn_phys;
142 if (offset >= ctx->afu->adapter->ps_size)
143 return VM_FAULT_SIGBUS;
144 } else {
145 area = ctx->psn_phys;
146 if (offset >= ctx->psn_size)
147 return VM_FAULT_SIGBUS;
148 }
149
150 mutex_lock(&ctx->status_mutex);
151
152 if (ctx->status != STARTED) {
153 mutex_unlock(&ctx->status_mutex);
154 pr_devel("%s: Context not started, failing problem state access\n", __func__);
155 if (ctx->mmio_err_ff) {
156 if (!ctx->ff_page) {
157 ctx->ff_page = alloc_page(GFP_USER);
158 if (!ctx->ff_page)
159 return VM_FAULT_OOM;
160 memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
161 }
162 get_page(ctx->ff_page);
163 vmf->page = ctx->ff_page;
164 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
165 return 0;
166 }
167 return VM_FAULT_SIGBUS;
168 }
169
170 vm_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
171
172 mutex_unlock(&ctx->status_mutex);
173
174 return VM_FAULT_NOPAGE;
175}
176
177static const struct vm_operations_struct cxl_mmap_vmops = {
178 .fault = cxl_mmap_fault,
179};
180
181
182
183
184int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
185{
186 u64 start = vma->vm_pgoff << PAGE_SHIFT;
187 u64 len = vma->vm_end - vma->vm_start;
188
189 if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
190 if (start + len > ctx->afu->adapter->ps_size)
191 return -EINVAL;
192
193 if (cxl_is_power9()) {
194
195
196
197
198 if (ctx->master && !ctx->afu->psa) {
199 pr_devel("AFU doesn't support mmio space\n");
200 return -EINVAL;
201 }
202
203
204 if (!ctx->afu->enabled)
205 return -EBUSY;
206 }
207 } else {
208 if (start + len > ctx->psn_size)
209 return -EINVAL;
210
211
212 if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
213 pr_devel("AFU doesn't support mmio space\n");
214 return -EINVAL;
215 }
216
217
218 if (!ctx->afu->enabled)
219 return -EBUSY;
220 }
221
222 pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
223 ctx->psn_phys, ctx->pe , ctx->master);
224
225 vma->vm_flags |= VM_IO | VM_PFNMAP;
226 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
227 vma->vm_ops = &cxl_mmap_vmops;
228 return 0;
229}
230
231
232
233
234
235
236int __detach_context(struct cxl_context *ctx)
237{
238 enum cxl_context_status status;
239
240 mutex_lock(&ctx->status_mutex);
241 status = ctx->status;
242 ctx->status = CLOSED;
243 mutex_unlock(&ctx->status_mutex);
244 if (status != STARTED)
245 return -EBUSY;
246
247
248
249
250 WARN_ON(cxl_ops->detach_process(ctx) &&
251 cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
252 flush_work(&ctx->fault_work);
253
254
255
256
257
258 if (cxl_ops->irq_wait)
259 cxl_ops->irq_wait(ctx);
260
261
262 put_pid(ctx->pid);
263
264 cxl_ctx_put();
265
266
267 cxl_adapter_context_put(ctx->afu->adapter);
268
269
270 cxl_context_mm_count_put(ctx);
271 if (ctx->mm)
272 mm_context_remove_copro(ctx->mm);
273 ctx->mm = NULL;
274
275 return 0;
276}
277
278
279
280
281
282
283
284void cxl_context_detach(struct cxl_context *ctx)
285{
286 int rc;
287
288 rc = __detach_context(ctx);
289 if (rc)
290 return;
291
292 afu_release_irqs(ctx, ctx);
293 wake_up_all(&ctx->wq);
294}
295
296
297
298
299void cxl_context_detach_all(struct cxl_afu *afu)
300{
301 struct cxl_context *ctx;
302 int tmp;
303
304 mutex_lock(&afu->contexts_lock);
305 idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
306
307
308
309
310 cxl_context_detach(ctx);
311
312
313
314
315
316
317
318 mutex_lock(&ctx->mapping_lock);
319 if (ctx->mapping)
320 unmap_mapping_range(ctx->mapping, 0, 0, 1);
321 mutex_unlock(&ctx->mapping_lock);
322 }
323 mutex_unlock(&afu->contexts_lock);
324}
325
326static void reclaim_ctx(struct rcu_head *rcu)
327{
328 struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
329
330 if (cxl_is_power8())
331 free_page((u64)ctx->sstp);
332 if (ctx->ff_page)
333 __free_page(ctx->ff_page);
334 ctx->sstp = NULL;
335
336 kfree(ctx->irq_bitmap);
337
338
339 cxl_afu_put(ctx->afu);
340
341 kfree(ctx);
342}
343
344void cxl_context_free(struct cxl_context *ctx)
345{
346 if (ctx->kernelapi && ctx->mapping)
347 cxl_release_mapping(ctx);
348 mutex_lock(&ctx->afu->contexts_lock);
349 idr_remove(&ctx->afu->contexts_idr, ctx->pe);
350 mutex_unlock(&ctx->afu->contexts_lock);
351 call_rcu(&ctx->rcu, reclaim_ctx);
352}
353
354void cxl_context_mm_count_get(struct cxl_context *ctx)
355{
356 if (ctx->mm)
357 atomic_inc(&ctx->mm->mm_count);
358}
359
360void cxl_context_mm_count_put(struct cxl_context *ctx)
361{
362 if (ctx->mm)
363 mmdrop(ctx->mm);
364}
365