blob: f4090979349068b3057d0cd9d8a17d15168ce734 [file] [log] [blame]
Ian Munsief204e0b2014-10-08 19:55:02 +11001/*
2 * Copyright 2014 IBM Corp.
3 *
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.
8 */
9
10#include <linux/spinlock.h>
11#include <linux/sched.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/mutex.h>
15#include <linux/mm.h>
16#include <linux/uaccess.h>
17#include <asm/synch.h>
Michael Neulingec249dd2015-05-27 16:07:16 +100018#include <misc/cxl-base.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110019
20#include "cxl.h"
Ian Munsie9bcf28c2015-01-09 20:34:36 +110021#include "trace.h"
Ian Munsief204e0b2014-10-08 19:55:02 +110022
23static int afu_control(struct cxl_afu *afu, u64 command,
24 u64 result, u64 mask, bool enabled)
25{
26 u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
27 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +110028 int rc = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +110029
30 spin_lock(&afu->afu_cntl_lock);
31 pr_devel("AFU command starting: %llx\n", command);
32
Ian Munsie9bcf28c2015-01-09 20:34:36 +110033 trace_cxl_afu_ctrl(afu, command);
34
Ian Munsief204e0b2014-10-08 19:55:02 +110035 cxl_p2n_write(afu, CXL_AFU_Cntl_An, AFU_Cntl | command);
36
37 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
38 while ((AFU_Cntl & mask) != result) {
39 if (time_after_eq(jiffies, timeout)) {
40 dev_warn(&afu->dev, "WARNING: AFU control timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +110041 rc = -EBUSY;
42 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +110043 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100044
45 if (!cxl_adapter_link_ok(afu->adapter)) {
46 afu->enabled = enabled;
47 rc = -EIO;
48 goto out;
49 }
50
Rasmus Villemoesde369532015-06-11 13:27:52 +020051 pr_devel_ratelimited("AFU control... (0x%016llx)\n",
Ian Munsief204e0b2014-10-08 19:55:02 +110052 AFU_Cntl | command);
53 cpu_relax();
54 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
55 };
56 pr_devel("AFU command complete: %llx\n", command);
57 afu->enabled = enabled;
Ian Munsie9bcf28c2015-01-09 20:34:36 +110058out:
59 trace_cxl_afu_ctrl_done(afu, command, rc);
Ian Munsief204e0b2014-10-08 19:55:02 +110060 spin_unlock(&afu->afu_cntl_lock);
61
Ian Munsie9bcf28c2015-01-09 20:34:36 +110062 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +110063}
64
65static int afu_enable(struct cxl_afu *afu)
66{
67 pr_devel("AFU enable request\n");
68
69 return afu_control(afu, CXL_AFU_Cntl_An_E,
70 CXL_AFU_Cntl_An_ES_Enabled,
71 CXL_AFU_Cntl_An_ES_MASK, true);
72}
73
74int cxl_afu_disable(struct cxl_afu *afu)
75{
76 pr_devel("AFU disable request\n");
77
78 return afu_control(afu, 0, CXL_AFU_Cntl_An_ES_Disabled,
79 CXL_AFU_Cntl_An_ES_MASK, false);
80}
81
82/* This will disable as well as reset */
Michael Neulingb12994f2015-05-27 16:07:09 +100083int __cxl_afu_reset(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +110084{
85 pr_devel("AFU reset request\n");
86
87 return afu_control(afu, CXL_AFU_Cntl_An_RA,
88 CXL_AFU_Cntl_An_RS_Complete | CXL_AFU_Cntl_An_ES_Disabled,
89 CXL_AFU_Cntl_An_RS_MASK | CXL_AFU_Cntl_An_ES_MASK,
90 false);
91}
92
Michael Neuling1a1a94b2015-05-27 16:07:10 +100093int cxl_afu_check_and_enable(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +110094{
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100095 if (!cxl_adapter_link_ok(afu->adapter)) {
96 WARN(1, "Refusing to enable afu while link down!\n");
97 return -EIO;
98 }
Ian Munsief204e0b2014-10-08 19:55:02 +110099 if (afu->enabled)
100 return 0;
101 return afu_enable(afu);
102}
103
104int cxl_psl_purge(struct cxl_afu *afu)
105{
106 u64 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
107 u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
108 u64 dsisr, dar;
109 u64 start, end;
110 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100111 int rc = 0;
112
113 trace_cxl_psl_ctrl(afu, CXL_PSL_SCNTL_An_Pc);
Ian Munsief204e0b2014-10-08 19:55:02 +1100114
115 pr_devel("PSL purge request\n");
116
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000117 if (!cxl_adapter_link_ok(afu->adapter)) {
118 dev_warn(&afu->dev, "PSL Purge called with link down, ignoring\n");
119 rc = -EIO;
120 goto out;
121 }
122
Ian Munsief204e0b2014-10-08 19:55:02 +1100123 if ((AFU_Cntl & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
124 WARN(1, "psl_purge request while AFU not disabled!\n");
125 cxl_afu_disable(afu);
126 }
127
128 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
129 PSL_CNTL | CXL_PSL_SCNTL_An_Pc);
130 start = local_clock();
131 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
132 while ((PSL_CNTL & CXL_PSL_SCNTL_An_Ps_MASK)
133 == CXL_PSL_SCNTL_An_Ps_Pending) {
134 if (time_after_eq(jiffies, timeout)) {
135 dev_warn(&afu->dev, "WARNING: PSL Purge timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100136 rc = -EBUSY;
137 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100138 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000139 if (!cxl_adapter_link_ok(afu->adapter)) {
140 rc = -EIO;
141 goto out;
142 }
143
Ian Munsief204e0b2014-10-08 19:55:02 +1100144 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
Rasmus Villemoesde369532015-06-11 13:27:52 +0200145 pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%016llx PSL_DSISR: 0x%016llx\n", PSL_CNTL, dsisr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100146 if (dsisr & CXL_PSL_DSISR_TRANS) {
147 dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
Rasmus Villemoesde369532015-06-11 13:27:52 +0200148 dev_notice(&afu->dev, "PSL purge terminating pending translation, DSISR: 0x%016llx, DAR: 0x%016llx\n", dsisr, dar);
Ian Munsief204e0b2014-10-08 19:55:02 +1100149 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
150 } else if (dsisr) {
Rasmus Villemoesde369532015-06-11 13:27:52 +0200151 dev_notice(&afu->dev, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%016llx\n", dsisr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100152 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
153 } else {
154 cpu_relax();
155 }
156 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
157 };
158 end = local_clock();
159 pr_devel("PSL purged in %lld ns\n", end - start);
160
161 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
162 PSL_CNTL & ~CXL_PSL_SCNTL_An_Pc);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100163out:
164 trace_cxl_psl_ctrl_done(afu, CXL_PSL_SCNTL_An_Pc, rc);
165 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100166}
167
168static int spa_max_procs(int spa_size)
169{
170 /*
171 * From the CAIA:
172 * end_of_SPA_area = SPA_Base + ((n+4) * 128) + (( ((n*8) + 127) >> 7) * 128) + 255
173 * Most of that junk is really just an overly-complicated way of saying
174 * the last 256 bytes are __aligned(128), so it's really:
175 * end_of_SPA_area = end_of_PSL_queue_area + __aligned(128) 255
176 * and
177 * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
178 * so
179 * sizeof(SPA) = ((n+4) * 128) + (n*8) + __aligned(128) 256
180 * Ignore the alignment (which is safe in this case as long as we are
181 * careful with our rounding) and solve for n:
182 */
183 return ((spa_size / 8) - 96) / 17;
184}
185
Daniel Axtens051557722015-08-14 17:41:19 +1000186int cxl_alloc_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100187{
Ian Munsief204e0b2014-10-08 19:55:02 +1100188 /* Work out how many pages to allocate */
189 afu->spa_order = 0;
190 do {
191 afu->spa_order++;
192 afu->spa_size = (1 << afu->spa_order) * PAGE_SIZE;
193 afu->spa_max_procs = spa_max_procs(afu->spa_size);
194 } while (afu->spa_max_procs < afu->num_procs);
195
196 WARN_ON(afu->spa_size > 0x100000); /* Max size supported by the hardware */
197
198 if (!(afu->spa = (struct cxl_process_element *)
199 __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->spa_order))) {
200 pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
201 return -ENOMEM;
202 }
203 pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
204 1<<afu->spa_order, afu->spa_max_procs, afu->num_procs);
205
Daniel Axtens051557722015-08-14 17:41:19 +1000206 return 0;
207}
208
209static void attach_spa(struct cxl_afu *afu)
210{
211 u64 spap;
212
Ian Munsief204e0b2014-10-08 19:55:02 +1100213 afu->sw_command_status = (__be64 *)((char *)afu->spa +
214 ((afu->spa_max_procs + 3) * 128));
215
216 spap = virt_to_phys(afu->spa) & CXL_PSL_SPAP_Addr;
217 spap |= ((afu->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
218 spap |= CXL_PSL_SPAP_V;
219 pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n", afu->spa, afu->spa_max_procs, afu->sw_command_status, spap);
220 cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100221}
222
Daniel Axtens051557722015-08-14 17:41:19 +1000223static inline void detach_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100224{
Ian Munsiedb7933f2014-12-08 19:18:00 +1100225 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
Daniel Axtens051557722015-08-14 17:41:19 +1000226}
227
228void cxl_release_spa(struct cxl_afu *afu)
229{
230 if (afu->spa) {
231 free_pages((unsigned long) afu->spa, afu->spa_order);
232 afu->spa = NULL;
233 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100234}
235
236int cxl_tlb_slb_invalidate(struct cxl *adapter)
237{
238 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
239
240 pr_devel("CXL adapter wide TLBIA & SLBIA\n");
241
242 cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
243
244 cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
245 while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
246 if (time_after_eq(jiffies, timeout)) {
247 dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
248 return -EBUSY;
249 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000250 if (!cxl_adapter_link_ok(adapter))
251 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100252 cpu_relax();
253 }
254
255 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
256 while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
257 if (time_after_eq(jiffies, timeout)) {
258 dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
259 return -EBUSY;
260 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000261 if (!cxl_adapter_link_ok(adapter))
262 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100263 cpu_relax();
264 }
265 return 0;
266}
267
268int cxl_afu_slbia(struct cxl_afu *afu)
269{
270 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
271
272 pr_devel("cxl_afu_slbia issuing SLBIA command\n");
273 cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
274 while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
275 if (time_after_eq(jiffies, timeout)) {
276 dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
277 return -EBUSY;
278 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000279 /* If the adapter has gone down, we can assume that we
280 * will PERST it and that will invalidate everything.
281 */
282 if (!cxl_adapter_link_ok(afu->adapter))
283 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100284 cpu_relax();
285 }
286 return 0;
287}
288
289static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
290{
291 int rc;
292
293 /* 1. Disable SSTP by writing 0 to SSTP1[V] */
294 cxl_p2n_write(afu, CXL_SSTP1_An, 0);
295
296 /* 2. Invalidate all SLB entries */
297 if ((rc = cxl_afu_slbia(afu)))
298 return rc;
299
300 /* 3. Set SSTP0_An */
301 cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
302
303 /* 4. Set SSTP1_An */
304 cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
305
306 return 0;
307}
308
309/* Using per slice version may improve performance here. (ie. SLBIA_An) */
310static void slb_invalid(struct cxl_context *ctx)
311{
312 struct cxl *adapter = ctx->afu->adapter;
313 u64 slbia;
314
315 WARN_ON(!mutex_is_locked(&ctx->afu->spa_mutex));
316
317 cxl_p1_write(adapter, CXL_PSL_LBISEL,
318 ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
319 be32_to_cpu(ctx->elem->lpid));
320 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
321
322 while (1) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000323 if (!cxl_adapter_link_ok(adapter))
324 break;
Ian Munsief204e0b2014-10-08 19:55:02 +1100325 slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
326 if (!(slbia & CXL_TLB_SLB_P))
327 break;
328 cpu_relax();
329 }
330}
331
332static int do_process_element_cmd(struct cxl_context *ctx,
333 u64 cmd, u64 pe_state)
334{
335 u64 state;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100336 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100337 int rc = 0;
338
339 trace_cxl_llcmd(ctx, cmd);
Ian Munsief204e0b2014-10-08 19:55:02 +1100340
341 WARN_ON(!ctx->afu->enabled);
342
343 ctx->elem->software_state = cpu_to_be32(pe_state);
344 smp_wmb();
345 *(ctx->afu->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
346 smp_mb();
347 cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
348 while (1) {
Ian Munsiea98e6e92014-12-08 19:17:56 +1100349 if (time_after_eq(jiffies, timeout)) {
350 dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100351 rc = -EBUSY;
352 goto out;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100353 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000354 if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
355 dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
356 rc = -EIO;
357 goto out;
358 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100359 state = be64_to_cpup(ctx->afu->sw_command_status);
360 if (state == ~0ULL) {
361 pr_err("cxl: Error adding process element to AFU\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100362 rc = -1;
363 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100364 }
365 if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
366 (cmd | (cmd >> 16) | ctx->pe))
367 break;
368 /*
369 * The command won't finish in the PSL if there are
370 * outstanding DSIs. Hence we need to yield here in
371 * case there are outstanding DSIs that we need to
372 * service. Tuning possiblity: we could wait for a
373 * while before sched
374 */
375 schedule();
376
377 }
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100378out:
379 trace_cxl_llcmd_done(ctx, cmd, rc);
380 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100381}
382
383static int add_process_element(struct cxl_context *ctx)
384{
385 int rc = 0;
386
387 mutex_lock(&ctx->afu->spa_mutex);
388 pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
389 if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
390 ctx->pe_inserted = true;
391 pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
392 mutex_unlock(&ctx->afu->spa_mutex);
393 return rc;
394}
395
396static int terminate_process_element(struct cxl_context *ctx)
397{
398 int rc = 0;
399
400 /* fast path terminate if it's already invalid */
401 if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
402 return rc;
403
404 mutex_lock(&ctx->afu->spa_mutex);
405 pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000406 /* We could be asked to terminate when the hw is down. That
407 * should always succeed: it's not running if the hw has gone
408 * away and is being reset.
409 */
410 if (cxl_adapter_link_ok(ctx->afu->adapter))
411 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
412 CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
Ian Munsief204e0b2014-10-08 19:55:02 +1100413 ctx->elem->software_state = 0; /* Remove Valid bit */
414 pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
415 mutex_unlock(&ctx->afu->spa_mutex);
416 return rc;
417}
418
419static int remove_process_element(struct cxl_context *ctx)
420{
421 int rc = 0;
422
423 mutex_lock(&ctx->afu->spa_mutex);
424 pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000425
426 /* We could be asked to remove when the hw is down. Again, if
427 * the hw is down, the PE is gone, so we succeed.
428 */
429 if (cxl_adapter_link_ok(ctx->afu->adapter))
430 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
431
432 if (!rc)
Ian Munsief204e0b2014-10-08 19:55:02 +1100433 ctx->pe_inserted = false;
434 slb_invalid(ctx);
435 pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
436 mutex_unlock(&ctx->afu->spa_mutex);
437
438 return rc;
439}
440
441
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000442void cxl_assign_psn_space(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100443{
444 if (!ctx->afu->pp_size || ctx->master) {
445 ctx->psn_phys = ctx->afu->psn_phys;
446 ctx->psn_size = ctx->afu->adapter->ps_size;
447 } else {
448 ctx->psn_phys = ctx->afu->psn_phys +
449 (ctx->afu->pp_offset + ctx->afu->pp_size * ctx->pe);
450 ctx->psn_size = ctx->afu->pp_size;
451 }
452}
453
454static int activate_afu_directed(struct cxl_afu *afu)
455{
456 int rc;
457
458 dev_info(&afu->dev, "Activating AFU directed mode\n");
459
Christophe Lombard4108efb2015-10-07 16:07:40 +1100460 afu->num_procs = afu->max_procs_virtualised;
Daniel Axtens051557722015-08-14 17:41:19 +1000461 if (afu->spa == NULL) {
462 if (cxl_alloc_spa(afu))
463 return -ENOMEM;
464 }
465 attach_spa(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100466
467 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
468 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
469 cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
470
471 afu->current_mode = CXL_MODE_DIRECTED;
Ian Munsief204e0b2014-10-08 19:55:02 +1100472
473 if ((rc = cxl_chardev_m_afu_add(afu)))
474 return rc;
475
476 if ((rc = cxl_sysfs_afu_m_add(afu)))
477 goto err;
478
479 if ((rc = cxl_chardev_s_afu_add(afu)))
480 goto err1;
481
482 return 0;
483err1:
484 cxl_sysfs_afu_m_remove(afu);
485err:
486 cxl_chardev_afu_remove(afu);
487 return rc;
488}
489
490#ifdef CONFIG_CPU_LITTLE_ENDIAN
491#define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
492#else
493#define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
494#endif
495
Michael Neuling2f663522015-05-27 16:07:13 +1000496static u64 calculate_sr(struct cxl_context *ctx)
497{
498 u64 sr = 0;
499
Frederic Barrate606e032015-12-07 14:34:40 +0100500 set_endian(sr);
Michael Neuling2f663522015-05-27 16:07:13 +1000501 if (ctx->master)
502 sr |= CXL_PSL_SR_An_MP;
503 if (mfspr(SPRN_LPCR) & LPCR_TC)
504 sr |= CXL_PSL_SR_An_TC;
505 if (ctx->kernel) {
506 sr |= CXL_PSL_SR_An_R | (mfmsr() & MSR_SF);
507 sr |= CXL_PSL_SR_An_HV;
508 } else {
509 sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
Michael Neuling2f663522015-05-27 16:07:13 +1000510 sr &= ~(CXL_PSL_SR_An_HV);
511 if (!test_tsk_thread_flag(current, TIF_32BIT))
512 sr |= CXL_PSL_SR_An_SF;
513 }
514 return sr;
515}
516
Ian Munsief204e0b2014-10-08 19:55:02 +1100517static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
518{
Michael Neuling2f663522015-05-27 16:07:13 +1000519 u32 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100520 int r, result;
521
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000522 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100523
524 ctx->elem->ctxtime = 0; /* disable */
525 ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
526 ctx->elem->haurp = 0; /* disable */
527 ctx->elem->sdr = cpu_to_be64(mfspr(SPRN_SDR1));
528
Michael Neuling2f663522015-05-27 16:07:13 +1000529 pid = current->pid;
530 if (ctx->kernel)
531 pid = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100532 ctx->elem->common.tid = 0;
Michael Neuling2f663522015-05-27 16:07:13 +1000533 ctx->elem->common.pid = cpu_to_be32(pid);
534
535 ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100536
537 ctx->elem->common.csrp = 0; /* disable */
538 ctx->elem->common.aurp0 = 0; /* disable */
539 ctx->elem->common.aurp1 = 0; /* disable */
540
541 cxl_prefault(ctx, wed);
542
543 ctx->elem->common.sstp0 = cpu_to_be64(ctx->sstp0);
544 ctx->elem->common.sstp1 = cpu_to_be64(ctx->sstp1);
545
546 for (r = 0; r < CXL_IRQ_RANGES; r++) {
547 ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
548 ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
549 }
550
551 ctx->elem->common.amr = cpu_to_be64(amr);
552 ctx->elem->common.wed = cpu_to_be64(wed);
553
554 /* first guy needs to enable */
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000555 if ((result = cxl_afu_check_and_enable(ctx->afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100556 return result;
557
Daniel Axtens368857c2015-07-29 14:07:22 +1000558 return add_process_element(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100559}
560
561static int deactivate_afu_directed(struct cxl_afu *afu)
562{
563 dev_info(&afu->dev, "Deactivating AFU directed mode\n");
564
565 afu->current_mode = 0;
566 afu->num_procs = 0;
567
568 cxl_sysfs_afu_m_remove(afu);
569 cxl_chardev_afu_remove(afu);
570
Michael Neulingb12994f2015-05-27 16:07:09 +1000571 __cxl_afu_reset(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100572 cxl_afu_disable(afu);
573 cxl_psl_purge(afu);
574
Ian Munsief204e0b2014-10-08 19:55:02 +1100575 return 0;
576}
577
578static int activate_dedicated_process(struct cxl_afu *afu)
579{
580 dev_info(&afu->dev, "Activating dedicated process mode\n");
581
582 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
583
584 cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
585 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
586 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
587 cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
588 cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
589 cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
590
591 cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
592 cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
593 cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
594
595 afu->current_mode = CXL_MODE_DEDICATED;
596 afu->num_procs = 1;
597
598 return cxl_chardev_d_afu_add(afu);
599}
600
601static int attach_dedicated(struct cxl_context *ctx, u64 wed, u64 amr)
602{
603 struct cxl_afu *afu = ctx->afu;
Michael Neuling2f663522015-05-27 16:07:13 +1000604 u64 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100605 int rc;
606
Michael Neuling2f663522015-05-27 16:07:13 +1000607 pid = (u64)current->pid << 32;
608 if (ctx->kernel)
609 pid = 0;
610 cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
611
612 cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100613
614 if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
615 return rc;
616
617 cxl_prefault(ctx, wed);
618
619 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
620 (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
621 (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
622 (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
623 ((u64)ctx->irqs.offset[3] & 0xffff));
624 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
625 (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
626 (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
627 (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
628 ((u64)ctx->irqs.range[3] & 0xffff));
629
630 cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
631
632 /* master only context for dedicated */
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000633 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100634
Michael Neulingb12994f2015-05-27 16:07:09 +1000635 if ((rc = __cxl_afu_reset(afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100636 return rc;
637
638 cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
639
640 return afu_enable(afu);
641}
642
643static int deactivate_dedicated_process(struct cxl_afu *afu)
644{
645 dev_info(&afu->dev, "Deactivating dedicated process mode\n");
646
647 afu->current_mode = 0;
648 afu->num_procs = 0;
649
650 cxl_chardev_afu_remove(afu);
651
652 return 0;
653}
654
655int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode)
656{
657 if (mode == CXL_MODE_DIRECTED)
658 return deactivate_afu_directed(afu);
659 if (mode == CXL_MODE_DEDICATED)
660 return deactivate_dedicated_process(afu);
661 return 0;
662}
663
664int cxl_afu_deactivate_mode(struct cxl_afu *afu)
665{
666 return _cxl_afu_deactivate_mode(afu, afu->current_mode);
667}
668
669int cxl_afu_activate_mode(struct cxl_afu *afu, int mode)
670{
671 if (!mode)
672 return 0;
673 if (!(mode & afu->modes_supported))
674 return -EINVAL;
675
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000676 if (!cxl_adapter_link_ok(afu->adapter)) {
677 WARN(1, "Device link is down, refusing to activate!\n");
678 return -EIO;
679 }
680
Ian Munsief204e0b2014-10-08 19:55:02 +1100681 if (mode == CXL_MODE_DIRECTED)
682 return activate_afu_directed(afu);
683 if (mode == CXL_MODE_DEDICATED)
684 return activate_dedicated_process(afu);
685
686 return -EINVAL;
687}
688
689int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr)
690{
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000691 if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
692 WARN(1, "Device link is down, refusing to attach process!\n");
693 return -EIO;
694 }
695
Ian Munsief204e0b2014-10-08 19:55:02 +1100696 ctx->kernel = kernel;
697 if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
698 return attach_afu_directed(ctx, wed, amr);
699
700 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
701 return attach_dedicated(ctx, wed, amr);
702
703 return -EINVAL;
704}
705
706static inline int detach_process_native_dedicated(struct cxl_context *ctx)
707{
Michael Neulingb12994f2015-05-27 16:07:09 +1000708 __cxl_afu_reset(ctx->afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100709 cxl_afu_disable(ctx->afu);
710 cxl_psl_purge(ctx->afu);
711 return 0;
712}
713
Ian Munsief204e0b2014-10-08 19:55:02 +1100714static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
715{
716 if (!ctx->pe_inserted)
717 return 0;
718 if (terminate_process_element(ctx))
719 return -1;
720 if (remove_process_element(ctx))
721 return -1;
722
723 return 0;
724}
725
726int cxl_detach_process(struct cxl_context *ctx)
727{
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100728 trace_cxl_detach(ctx);
729
Ian Munsief204e0b2014-10-08 19:55:02 +1100730 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
731 return detach_process_native_dedicated(ctx);
732
733 return detach_process_native_afu_directed(ctx);
734}
735
Ian Munsiebc78b052014-11-14 17:37:50 +1100736int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info)
Ian Munsief204e0b2014-10-08 19:55:02 +1100737{
738 u64 pidtid;
739
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000740 /* If the adapter has gone away, we can't get any meaningful
741 * information.
742 */
743 if (!cxl_adapter_link_ok(afu->adapter))
744 return -EIO;
745
Ian Munsiebc78b052014-11-14 17:37:50 +1100746 info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
747 info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
748 info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
749 pidtid = cxl_p2n_read(afu, CXL_PSL_PID_TID_An);
Ian Munsief204e0b2014-10-08 19:55:02 +1100750 info->pid = pidtid >> 32;
751 info->tid = pidtid & 0xffffffff;
Ian Munsiebc78b052014-11-14 17:37:50 +1100752 info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
753 info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
Ian Munsief204e0b2014-10-08 19:55:02 +1100754
755 return 0;
756}
757
758static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
759{
760 u64 dsisr;
761
Rasmus Villemoesde369532015-06-11 13:27:52 +0200762 pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat);
Ian Munsief204e0b2014-10-08 19:55:02 +1100763
764 /* Clear PSL_DSISR[PE] */
765 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
766 cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
767
768 /* Write 1s to clear error status bits */
769 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
770}
771
772int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
773{
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100774 trace_cxl_psl_irq_ack(ctx, tfc);
Ian Munsief204e0b2014-10-08 19:55:02 +1100775 if (tfc)
776 cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
777 if (psl_reset_mask)
778 recover_psl_err(ctx->afu, psl_reset_mask);
779
780 return 0;
781}
782
783int cxl_check_error(struct cxl_afu *afu)
784{
785 return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
786}