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