blob: 55d8a1459f28c69a953844b1d4d698d069ba8f80 [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>
Michael Neuling2bc79ff2016-04-22 14:57:49 +100017#include <linux/delay.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110018#include <asm/synch.h>
Michael Neulingec249dd2015-05-27 16:07:16 +100019#include <misc/cxl-base.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110020
21#include "cxl.h"
Ian Munsie9bcf28c2015-01-09 20:34:36 +110022#include "trace.h"
Ian Munsief204e0b2014-10-08 19:55:02 +110023
24static int afu_control(struct cxl_afu *afu, u64 command,
25 u64 result, u64 mask, bool enabled)
26{
27 u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
28 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +110029 int rc = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +110030
31 spin_lock(&afu->afu_cntl_lock);
32 pr_devel("AFU command starting: %llx\n", command);
33
Ian Munsie9bcf28c2015-01-09 20:34:36 +110034 trace_cxl_afu_ctrl(afu, command);
35
Ian Munsief204e0b2014-10-08 19:55:02 +110036 cxl_p2n_write(afu, CXL_AFU_Cntl_An, AFU_Cntl | command);
37
38 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
39 while ((AFU_Cntl & mask) != result) {
40 if (time_after_eq(jiffies, timeout)) {
41 dev_warn(&afu->dev, "WARNING: AFU control timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +110042 rc = -EBUSY;
43 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +110044 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100045
Christophe Lombard0d400f72016-03-04 12:26:41 +010046 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100047 afu->enabled = enabled;
48 rc = -EIO;
49 goto out;
50 }
51
Rasmus Villemoesde369532015-06-11 13:27:52 +020052 pr_devel_ratelimited("AFU control... (0x%016llx)\n",
Ian Munsief204e0b2014-10-08 19:55:02 +110053 AFU_Cntl | command);
54 cpu_relax();
55 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
56 };
57 pr_devel("AFU command complete: %llx\n", command);
58 afu->enabled = enabled;
Ian Munsie9bcf28c2015-01-09 20:34:36 +110059out:
60 trace_cxl_afu_ctrl_done(afu, command, rc);
Ian Munsief204e0b2014-10-08 19:55:02 +110061 spin_unlock(&afu->afu_cntl_lock);
62
Ian Munsie9bcf28c2015-01-09 20:34:36 +110063 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +110064}
65
66static int afu_enable(struct cxl_afu *afu)
67{
68 pr_devel("AFU enable request\n");
69
70 return afu_control(afu, CXL_AFU_Cntl_An_E,
71 CXL_AFU_Cntl_An_ES_Enabled,
72 CXL_AFU_Cntl_An_ES_MASK, true);
73}
74
75int cxl_afu_disable(struct cxl_afu *afu)
76{
77 pr_devel("AFU disable request\n");
78
79 return afu_control(afu, 0, CXL_AFU_Cntl_An_ES_Disabled,
80 CXL_AFU_Cntl_An_ES_MASK, false);
81}
82
83/* This will disable as well as reset */
Frederic Barrat2b04cf32016-03-04 12:26:29 +010084static int native_afu_reset(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +110085{
86 pr_devel("AFU reset request\n");
87
88 return afu_control(afu, CXL_AFU_Cntl_An_RA,
89 CXL_AFU_Cntl_An_RS_Complete | CXL_AFU_Cntl_An_ES_Disabled,
90 CXL_AFU_Cntl_An_RS_MASK | CXL_AFU_Cntl_An_ES_MASK,
91 false);
92}
93
Frederic Barrat2b04cf32016-03-04 12:26:29 +010094static int native_afu_check_and_enable(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +110095{
Christophe Lombard0d400f72016-03-04 12:26:41 +010096 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100097 WARN(1, "Refusing to enable afu while link down!\n");
98 return -EIO;
99 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100100 if (afu->enabled)
101 return 0;
102 return afu_enable(afu);
103}
104
105int cxl_psl_purge(struct cxl_afu *afu)
106{
107 u64 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
108 u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
109 u64 dsisr, dar;
110 u64 start, end;
111 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100112 int rc = 0;
113
114 trace_cxl_psl_ctrl(afu, CXL_PSL_SCNTL_An_Pc);
Ian Munsief204e0b2014-10-08 19:55:02 +1100115
116 pr_devel("PSL purge request\n");
117
Christophe Lombard0d400f72016-03-04 12:26:41 +0100118 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000119 dev_warn(&afu->dev, "PSL Purge called with link down, ignoring\n");
120 rc = -EIO;
121 goto out;
122 }
123
Ian Munsief204e0b2014-10-08 19:55:02 +1100124 if ((AFU_Cntl & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
125 WARN(1, "psl_purge request while AFU not disabled!\n");
126 cxl_afu_disable(afu);
127 }
128
129 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
130 PSL_CNTL | CXL_PSL_SCNTL_An_Pc);
131 start = local_clock();
132 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
133 while ((PSL_CNTL & CXL_PSL_SCNTL_An_Ps_MASK)
134 == CXL_PSL_SCNTL_An_Ps_Pending) {
135 if (time_after_eq(jiffies, timeout)) {
136 dev_warn(&afu->dev, "WARNING: PSL Purge timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100137 rc = -EBUSY;
138 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100139 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100140 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000141 rc = -EIO;
142 goto out;
143 }
144
Ian Munsief204e0b2014-10-08 19:55:02 +1100145 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
Rasmus Villemoesde369532015-06-11 13:27:52 +0200146 pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%016llx PSL_DSISR: 0x%016llx\n", PSL_CNTL, dsisr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100147 if (dsisr & CXL_PSL_DSISR_TRANS) {
148 dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
Rasmus Villemoesde369532015-06-11 13:27:52 +0200149 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 +1100150 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
151 } else if (dsisr) {
Rasmus Villemoesde369532015-06-11 13:27:52 +0200152 dev_notice(&afu->dev, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%016llx\n", dsisr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100153 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
154 } else {
155 cpu_relax();
156 }
157 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
158 };
159 end = local_clock();
160 pr_devel("PSL purged in %lld ns\n", end - start);
161
162 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
163 PSL_CNTL & ~CXL_PSL_SCNTL_An_Pc);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100164out:
165 trace_cxl_psl_ctrl_done(afu, CXL_PSL_SCNTL_An_Pc, rc);
166 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100167}
168
169static int spa_max_procs(int spa_size)
170{
171 /*
172 * From the CAIA:
173 * end_of_SPA_area = SPA_Base + ((n+4) * 128) + (( ((n*8) + 127) >> 7) * 128) + 255
174 * Most of that junk is really just an overly-complicated way of saying
175 * the last 256 bytes are __aligned(128), so it's really:
176 * end_of_SPA_area = end_of_PSL_queue_area + __aligned(128) 255
177 * and
178 * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
179 * so
180 * sizeof(SPA) = ((n+4) * 128) + (n*8) + __aligned(128) 256
181 * Ignore the alignment (which is safe in this case as long as we are
182 * careful with our rounding) and solve for n:
183 */
184 return ((spa_size / 8) - 96) / 17;
185}
186
Daniel Axtens051557722015-08-14 17:41:19 +1000187int cxl_alloc_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100188{
Ian Munsie895a7982016-05-04 14:46:30 +1000189 unsigned spa_size;
190
Ian Munsief204e0b2014-10-08 19:55:02 +1100191 /* Work out how many pages to allocate */
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100192 afu->native->spa_order = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100193 do {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100194 afu->native->spa_order++;
Ian Munsie895a7982016-05-04 14:46:30 +1000195 spa_size = (1 << afu->native->spa_order) * PAGE_SIZE;
196
197 if (spa_size > 0x100000) {
198 dev_warn(&afu->dev, "num_of_processes too large for the SPA, limiting to %i (0x%x)\n",
199 afu->native->spa_max_procs, afu->native->spa_size);
200 afu->num_procs = afu->native->spa_max_procs;
201 break;
202 }
203
204 afu->native->spa_size = spa_size;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100205 afu->native->spa_max_procs = spa_max_procs(afu->native->spa_size);
206 } while (afu->native->spa_max_procs < afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100207
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100208 if (!(afu->native->spa = (struct cxl_process_element *)
209 __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->native->spa_order))) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100210 pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
211 return -ENOMEM;
212 }
213 pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100214 1<<afu->native->spa_order, afu->native->spa_max_procs, afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100215
Daniel Axtens051557722015-08-14 17:41:19 +1000216 return 0;
217}
218
219static void attach_spa(struct cxl_afu *afu)
220{
221 u64 spap;
222
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100223 afu->native->sw_command_status = (__be64 *)((char *)afu->native->spa +
224 ((afu->native->spa_max_procs + 3) * 128));
Ian Munsief204e0b2014-10-08 19:55:02 +1100225
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100226 spap = virt_to_phys(afu->native->spa) & CXL_PSL_SPAP_Addr;
227 spap |= ((afu->native->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
Ian Munsief204e0b2014-10-08 19:55:02 +1100228 spap |= CXL_PSL_SPAP_V;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100229 pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n",
230 afu->native->spa, afu->native->spa_max_procs,
231 afu->native->sw_command_status, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100232 cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100233}
234
Daniel Axtens051557722015-08-14 17:41:19 +1000235static inline void detach_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100236{
Ian Munsiedb7933f2014-12-08 19:18:00 +1100237 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
Daniel Axtens051557722015-08-14 17:41:19 +1000238}
239
240void cxl_release_spa(struct cxl_afu *afu)
241{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100242 if (afu->native->spa) {
243 free_pages((unsigned long) afu->native->spa,
244 afu->native->spa_order);
245 afu->native->spa = NULL;
Daniel Axtens051557722015-08-14 17:41:19 +1000246 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100247}
248
249int cxl_tlb_slb_invalidate(struct cxl *adapter)
250{
251 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
252
253 pr_devel("CXL adapter wide TLBIA & SLBIA\n");
254
255 cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
256
257 cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
258 while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
259 if (time_after_eq(jiffies, timeout)) {
260 dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
261 return -EBUSY;
262 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100263 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000264 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100265 cpu_relax();
266 }
267
268 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
269 while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
270 if (time_after_eq(jiffies, timeout)) {
271 dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
272 return -EBUSY;
273 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100274 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000275 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100276 cpu_relax();
277 }
278 return 0;
279}
280
Ian Munsief204e0b2014-10-08 19:55:02 +1100281static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
282{
283 int rc;
284
285 /* 1. Disable SSTP by writing 0 to SSTP1[V] */
286 cxl_p2n_write(afu, CXL_SSTP1_An, 0);
287
288 /* 2. Invalidate all SLB entries */
289 if ((rc = cxl_afu_slbia(afu)))
290 return rc;
291
292 /* 3. Set SSTP0_An */
293 cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
294
295 /* 4. Set SSTP1_An */
296 cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
297
298 return 0;
299}
300
301/* Using per slice version may improve performance here. (ie. SLBIA_An) */
302static void slb_invalid(struct cxl_context *ctx)
303{
304 struct cxl *adapter = ctx->afu->adapter;
305 u64 slbia;
306
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100307 WARN_ON(!mutex_is_locked(&ctx->afu->native->spa_mutex));
Ian Munsief204e0b2014-10-08 19:55:02 +1100308
309 cxl_p1_write(adapter, CXL_PSL_LBISEL,
310 ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
311 be32_to_cpu(ctx->elem->lpid));
312 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
313
314 while (1) {
Christophe Lombard0d400f72016-03-04 12:26:41 +0100315 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000316 break;
Ian Munsief204e0b2014-10-08 19:55:02 +1100317 slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
318 if (!(slbia & CXL_TLB_SLB_P))
319 break;
320 cpu_relax();
321 }
322}
323
324static int do_process_element_cmd(struct cxl_context *ctx,
325 u64 cmd, u64 pe_state)
326{
327 u64 state;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100328 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100329 int rc = 0;
330
331 trace_cxl_llcmd(ctx, cmd);
Ian Munsief204e0b2014-10-08 19:55:02 +1100332
333 WARN_ON(!ctx->afu->enabled);
334
335 ctx->elem->software_state = cpu_to_be32(pe_state);
336 smp_wmb();
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100337 *(ctx->afu->native->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100338 smp_mb();
339 cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
340 while (1) {
Ian Munsiea98e6e92014-12-08 19:17:56 +1100341 if (time_after_eq(jiffies, timeout)) {
342 dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100343 rc = -EBUSY;
344 goto out;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100345 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100346 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000347 dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
348 rc = -EIO;
349 goto out;
350 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100351 state = be64_to_cpup(ctx->afu->native->sw_command_status);
Ian Munsief204e0b2014-10-08 19:55:02 +1100352 if (state == ~0ULL) {
353 pr_err("cxl: Error adding process element to AFU\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100354 rc = -1;
355 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100356 }
357 if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
358 (cmd | (cmd >> 16) | ctx->pe))
359 break;
360 /*
361 * The command won't finish in the PSL if there are
362 * outstanding DSIs. Hence we need to yield here in
363 * case there are outstanding DSIs that we need to
364 * service. Tuning possiblity: we could wait for a
365 * while before sched
366 */
367 schedule();
368
369 }
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100370out:
371 trace_cxl_llcmd_done(ctx, cmd, rc);
372 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100373}
374
375static int add_process_element(struct cxl_context *ctx)
376{
377 int rc = 0;
378
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100379 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100380 pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
381 if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
382 ctx->pe_inserted = true;
383 pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100384 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100385 return rc;
386}
387
388static int terminate_process_element(struct cxl_context *ctx)
389{
390 int rc = 0;
391
392 /* fast path terminate if it's already invalid */
393 if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
394 return rc;
395
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100396 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100397 pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000398 /* We could be asked to terminate when the hw is down. That
399 * should always succeed: it's not running if the hw has gone
400 * away and is being reset.
401 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100402 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000403 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
404 CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
Ian Munsief204e0b2014-10-08 19:55:02 +1100405 ctx->elem->software_state = 0; /* Remove Valid bit */
406 pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100407 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100408 return rc;
409}
410
411static int remove_process_element(struct cxl_context *ctx)
412{
413 int rc = 0;
414
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100415 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100416 pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000417
418 /* We could be asked to remove when the hw is down. Again, if
419 * the hw is down, the PE is gone, so we succeed.
420 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100421 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000422 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
423
424 if (!rc)
Ian Munsief204e0b2014-10-08 19:55:02 +1100425 ctx->pe_inserted = false;
426 slb_invalid(ctx);
427 pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100428 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100429
430 return rc;
431}
432
433
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000434void cxl_assign_psn_space(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100435{
436 if (!ctx->afu->pp_size || ctx->master) {
437 ctx->psn_phys = ctx->afu->psn_phys;
438 ctx->psn_size = ctx->afu->adapter->ps_size;
439 } else {
440 ctx->psn_phys = ctx->afu->psn_phys +
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100441 (ctx->afu->native->pp_offset + ctx->afu->pp_size * ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100442 ctx->psn_size = ctx->afu->pp_size;
443 }
444}
445
446static int activate_afu_directed(struct cxl_afu *afu)
447{
448 int rc;
449
450 dev_info(&afu->dev, "Activating AFU directed mode\n");
451
Christophe Lombard4108efb2015-10-07 16:07:40 +1100452 afu->num_procs = afu->max_procs_virtualised;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100453 if (afu->native->spa == NULL) {
Daniel Axtens051557722015-08-14 17:41:19 +1000454 if (cxl_alloc_spa(afu))
455 return -ENOMEM;
456 }
457 attach_spa(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100458
459 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
460 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
461 cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
462
463 afu->current_mode = CXL_MODE_DIRECTED;
Ian Munsief204e0b2014-10-08 19:55:02 +1100464
465 if ((rc = cxl_chardev_m_afu_add(afu)))
466 return rc;
467
468 if ((rc = cxl_sysfs_afu_m_add(afu)))
469 goto err;
470
471 if ((rc = cxl_chardev_s_afu_add(afu)))
472 goto err1;
473
474 return 0;
475err1:
476 cxl_sysfs_afu_m_remove(afu);
477err:
478 cxl_chardev_afu_remove(afu);
479 return rc;
480}
481
482#ifdef CONFIG_CPU_LITTLE_ENDIAN
483#define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
484#else
485#define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
486#endif
487
Michael Neuling2f663522015-05-27 16:07:13 +1000488static u64 calculate_sr(struct cxl_context *ctx)
489{
490 u64 sr = 0;
491
Frederic Barrate606e032015-12-07 14:34:40 +0100492 set_endian(sr);
Michael Neuling2f663522015-05-27 16:07:13 +1000493 if (ctx->master)
494 sr |= CXL_PSL_SR_An_MP;
495 if (mfspr(SPRN_LPCR) & LPCR_TC)
496 sr |= CXL_PSL_SR_An_TC;
497 if (ctx->kernel) {
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000498 if (!ctx->real_mode)
499 sr |= CXL_PSL_SR_An_R;
500 sr |= (mfmsr() & MSR_SF) | CXL_PSL_SR_An_HV;
Michael Neuling2f663522015-05-27 16:07:13 +1000501 } else {
502 sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
Michael Neuling2f663522015-05-27 16:07:13 +1000503 sr &= ~(CXL_PSL_SR_An_HV);
504 if (!test_tsk_thread_flag(current, TIF_32BIT))
505 sr |= CXL_PSL_SR_An_SF;
506 }
507 return sr;
508}
509
Ian Munsief204e0b2014-10-08 19:55:02 +1100510static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
511{
Michael Neuling2f663522015-05-27 16:07:13 +1000512 u32 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100513 int r, result;
514
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000515 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100516
517 ctx->elem->ctxtime = 0; /* disable */
518 ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
519 ctx->elem->haurp = 0; /* disable */
520 ctx->elem->sdr = cpu_to_be64(mfspr(SPRN_SDR1));
521
Michael Neuling2f663522015-05-27 16:07:13 +1000522 pid = current->pid;
523 if (ctx->kernel)
524 pid = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100525 ctx->elem->common.tid = 0;
Michael Neuling2f663522015-05-27 16:07:13 +1000526 ctx->elem->common.pid = cpu_to_be32(pid);
527
528 ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100529
530 ctx->elem->common.csrp = 0; /* disable */
531 ctx->elem->common.aurp0 = 0; /* disable */
532 ctx->elem->common.aurp1 = 0; /* disable */
533
534 cxl_prefault(ctx, wed);
535
536 ctx->elem->common.sstp0 = cpu_to_be64(ctx->sstp0);
537 ctx->elem->common.sstp1 = cpu_to_be64(ctx->sstp1);
538
Ian Munsie3c206fa2016-05-04 14:52:58 +1000539 /*
540 * Ensure we have the multiplexed PSL interrupt set up to take faults
541 * for kernel contexts that may not have allocated any AFU IRQs at all:
542 */
543 if (ctx->irqs.range[0] == 0) {
544 ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
545 ctx->irqs.range[0] = 1;
546 }
547
Ian Munsief204e0b2014-10-08 19:55:02 +1100548 for (r = 0; r < CXL_IRQ_RANGES; r++) {
549 ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
550 ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
551 }
552
553 ctx->elem->common.amr = cpu_to_be64(amr);
554 ctx->elem->common.wed = cpu_to_be64(wed);
555
556 /* first guy needs to enable */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100557 if ((result = cxl_ops->afu_check_and_enable(ctx->afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100558 return result;
559
Daniel Axtens368857c2015-07-29 14:07:22 +1000560 return add_process_element(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100561}
562
563static int deactivate_afu_directed(struct cxl_afu *afu)
564{
565 dev_info(&afu->dev, "Deactivating AFU directed mode\n");
566
567 afu->current_mode = 0;
568 afu->num_procs = 0;
569
570 cxl_sysfs_afu_m_remove(afu);
571 cxl_chardev_afu_remove(afu);
572
Frederic Barrat5be587b2016-03-04 12:26:28 +0100573 cxl_ops->afu_reset(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100574 cxl_afu_disable(afu);
575 cxl_psl_purge(afu);
576
Ian Munsief204e0b2014-10-08 19:55:02 +1100577 return 0;
578}
579
580static int activate_dedicated_process(struct cxl_afu *afu)
581{
582 dev_info(&afu->dev, "Activating dedicated process mode\n");
583
584 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
585
586 cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
587 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
588 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
589 cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
590 cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
591 cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
592
593 cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
594 cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
595 cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
596
597 afu->current_mode = CXL_MODE_DEDICATED;
598 afu->num_procs = 1;
599
600 return cxl_chardev_d_afu_add(afu);
601}
602
603static int attach_dedicated(struct cxl_context *ctx, u64 wed, u64 amr)
604{
605 struct cxl_afu *afu = ctx->afu;
Michael Neuling2f663522015-05-27 16:07:13 +1000606 u64 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100607 int rc;
608
Michael Neuling2f663522015-05-27 16:07:13 +1000609 pid = (u64)current->pid << 32;
610 if (ctx->kernel)
611 pid = 0;
612 cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
613
614 cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100615
616 if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
617 return rc;
618
619 cxl_prefault(ctx, wed);
620
621 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
622 (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
623 (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
624 (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
625 ((u64)ctx->irqs.offset[3] & 0xffff));
626 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
627 (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
628 (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
629 (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
630 ((u64)ctx->irqs.range[3] & 0xffff));
631
632 cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
633
634 /* master only context for dedicated */
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000635 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100636
Frederic Barrat5be587b2016-03-04 12:26:28 +0100637 if ((rc = cxl_ops->afu_reset(afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100638 return rc;
639
640 cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
641
642 return afu_enable(afu);
643}
644
645static int deactivate_dedicated_process(struct cxl_afu *afu)
646{
647 dev_info(&afu->dev, "Deactivating dedicated process mode\n");
648
649 afu->current_mode = 0;
650 afu->num_procs = 0;
651
652 cxl_chardev_afu_remove(afu);
653
654 return 0;
655}
656
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100657static int native_afu_deactivate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100658{
659 if (mode == CXL_MODE_DIRECTED)
660 return deactivate_afu_directed(afu);
661 if (mode == CXL_MODE_DEDICATED)
662 return deactivate_dedicated_process(afu);
663 return 0;
664}
665
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100666static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100667{
668 if (!mode)
669 return 0;
670 if (!(mode & afu->modes_supported))
671 return -EINVAL;
672
Christophe Lombard0d400f72016-03-04 12:26:41 +0100673 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000674 WARN(1, "Device link is down, refusing to activate!\n");
675 return -EIO;
676 }
677
Ian Munsief204e0b2014-10-08 19:55:02 +1100678 if (mode == CXL_MODE_DIRECTED)
679 return activate_afu_directed(afu);
680 if (mode == CXL_MODE_DEDICATED)
681 return activate_dedicated_process(afu);
682
683 return -EINVAL;
684}
685
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100686static int native_attach_process(struct cxl_context *ctx, bool kernel,
687 u64 wed, u64 amr)
Ian Munsief204e0b2014-10-08 19:55:02 +1100688{
Christophe Lombard0d400f72016-03-04 12:26:41 +0100689 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000690 WARN(1, "Device link is down, refusing to attach process!\n");
691 return -EIO;
692 }
693
Ian Munsief204e0b2014-10-08 19:55:02 +1100694 ctx->kernel = kernel;
695 if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
696 return attach_afu_directed(ctx, wed, amr);
697
698 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
699 return attach_dedicated(ctx, wed, amr);
700
701 return -EINVAL;
702}
703
704static inline int detach_process_native_dedicated(struct cxl_context *ctx)
705{
Frederic Barrat5be587b2016-03-04 12:26:28 +0100706 cxl_ops->afu_reset(ctx->afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100707 cxl_afu_disable(ctx->afu);
708 cxl_psl_purge(ctx->afu);
709 return 0;
710}
711
Ian Munsief204e0b2014-10-08 19:55:02 +1100712static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
713{
714 if (!ctx->pe_inserted)
715 return 0;
716 if (terminate_process_element(ctx))
717 return -1;
718 if (remove_process_element(ctx))
719 return -1;
720
721 return 0;
722}
723
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100724static int native_detach_process(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100725{
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100726 trace_cxl_detach(ctx);
727
Ian Munsief204e0b2014-10-08 19:55:02 +1100728 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
729 return detach_process_native_dedicated(ctx);
730
731 return detach_process_native_afu_directed(ctx);
732}
733
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100734static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
Ian Munsief204e0b2014-10-08 19:55:02 +1100735{
736 u64 pidtid;
737
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000738 /* If the adapter has gone away, we can't get any meaningful
739 * information.
740 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100741 if (!cxl_ops->link_ok(afu->adapter, afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000742 return -EIO;
743
Ian Munsiebc78b052014-11-14 17:37:50 +1100744 info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
745 info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
746 info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
747 pidtid = cxl_p2n_read(afu, CXL_PSL_PID_TID_An);
Ian Munsief204e0b2014-10-08 19:55:02 +1100748 info->pid = pidtid >> 32;
749 info->tid = pidtid & 0xffffffff;
Ian Munsiebc78b052014-11-14 17:37:50 +1100750 info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
751 info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
Christophe Lombard444c4ba2016-03-04 12:26:34 +0100752 info->proc_handle = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100753
754 return 0;
755}
756
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100757static irqreturn_t native_handle_psl_slice_error(struct cxl_context *ctx,
758 u64 dsisr, u64 errstat)
Frederic Barratd56d3012016-03-04 12:26:26 +0100759{
760 u64 fir1, fir2, fir_slice, serr, afu_debug;
761
762 fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
763 fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
764 fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
765 serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
766 afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
767
768 dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
769 dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
770 dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
771 dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
772 dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
773 dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
774
775 dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
776 cxl_stop_trace(ctx->afu->adapter);
777
Frederic Barrat5be587b2016-03-04 12:26:28 +0100778 return cxl_ops->ack_irq(ctx, 0, errstat);
Frederic Barratd56d3012016-03-04 12:26:26 +0100779}
780
781static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
782{
783 if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
784 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
785 else
786 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
787
788 return IRQ_HANDLED;
789}
790
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100791static irqreturn_t native_irq_multiplexed(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100792{
793 struct cxl_afu *afu = data;
794 struct cxl_context *ctx;
795 struct cxl_irq_info irq_info;
796 int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
797 int ret;
798
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100799 if ((ret = native_get_irq_info(afu, &irq_info))) {
Frederic Barratd56d3012016-03-04 12:26:26 +0100800 WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
801 return fail_psl_irq(afu, &irq_info);
802 }
803
804 rcu_read_lock();
805 ctx = idr_find(&afu->contexts_idr, ph);
806 if (ctx) {
807 ret = cxl_irq(irq, ctx, &irq_info);
808 rcu_read_unlock();
809 return ret;
810 }
811 rcu_read_unlock();
812
813 WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
814 " %016llx\n(Possible AFU HW issue - was a term/remove acked"
815 " with outstanding transactions?)\n", ph, irq_info.dsisr,
816 irq_info.dar);
817 return fail_psl_irq(afu, &irq_info);
818}
819
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000820void native_irq_wait(struct cxl_context *ctx)
821{
822 u64 dsisr;
823 int timeout = 1000;
824 int ph;
825
826 /*
827 * Wait until no further interrupts are presented by the PSL
828 * for this context.
829 */
830 while (timeout--) {
831 ph = cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) & 0xffff;
832 if (ph != ctx->pe)
833 return;
834 dsisr = cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An);
835 if ((dsisr & CXL_PSL_DSISR_PENDING) == 0)
836 return;
837 /*
838 * We are waiting for the workqueue to process our
839 * irq, so need to let that run here.
840 */
841 msleep(1);
842 }
843
844 dev_warn(&ctx->afu->dev, "WARNING: waiting on DSI for PE %i"
845 " DSISR %016llx!\n", ph, dsisr);
846 return;
847}
848
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100849static irqreturn_t native_slice_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100850{
851 struct cxl_afu *afu = data;
852 u64 fir_slice, errstat, serr, afu_debug;
853
854 WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
855
856 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
857 fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
858 errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
859 afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
860 dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
861 dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
862 dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
863 dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
864
865 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
866
867 return IRQ_HANDLED;
868}
869
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100870static irqreturn_t native_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100871{
872 struct cxl *adapter = data;
873 u64 fir1, fir2, err_ivte;
874
875 WARN(1, "CXL ERROR interrupt %i\n", irq);
876
877 err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
878 dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
879
880 dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
881 cxl_stop_trace(adapter);
882
883 fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
884 fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
885
886 dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
887
888 return IRQ_HANDLED;
889}
890
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100891int cxl_native_register_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +0100892{
893 int rc;
894
895 adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
896 dev_name(&adapter->dev));
897 if (!adapter->irq_name)
898 return -ENOMEM;
899
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100900 if ((rc = cxl_register_one_irq(adapter, native_irq_err, adapter,
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100901 &adapter->native->err_hwirq,
902 &adapter->native->err_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +0100903 adapter->irq_name))) {
904 kfree(adapter->irq_name);
905 adapter->irq_name = NULL;
906 return rc;
907 }
908
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100909 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->native->err_hwirq & 0xffff);
Frederic Barratd56d3012016-03-04 12:26:26 +0100910
911 return 0;
912}
913
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100914void cxl_native_release_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +0100915{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100916 if (adapter->native->err_virq != irq_find_mapping(NULL, adapter->native->err_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +0100917 return;
918
919 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100920 cxl_unmap_irq(adapter->native->err_virq, adapter);
921 cxl_ops->release_one_irq(adapter, adapter->native->err_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100922 kfree(adapter->irq_name);
923}
924
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100925int cxl_native_register_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100926{
927 u64 serr;
928 int rc;
929
930 afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
931 dev_name(&afu->dev));
932 if (!afu->err_irq_name)
933 return -ENOMEM;
934
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100935 if ((rc = cxl_register_one_irq(afu->adapter, native_slice_irq_err, afu,
Frederic Barratd56d3012016-03-04 12:26:26 +0100936 &afu->serr_hwirq,
937 &afu->serr_virq, afu->err_irq_name))) {
938 kfree(afu->err_irq_name);
939 afu->err_irq_name = NULL;
940 return rc;
941 }
942
943 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
944 serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
945 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
946
947 return 0;
948}
949
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100950void cxl_native_release_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100951{
952 if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
953 return;
954
955 cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
956 cxl_unmap_irq(afu->serr_virq, afu);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100957 cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100958 kfree(afu->err_irq_name);
959}
960
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100961int cxl_native_register_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100962{
963 int rc;
964
965 afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
966 dev_name(&afu->dev));
967 if (!afu->psl_irq_name)
968 return -ENOMEM;
969
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100970 if ((rc = cxl_register_one_irq(afu->adapter, native_irq_multiplexed,
971 afu, &afu->native->psl_hwirq, &afu->native->psl_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +0100972 afu->psl_irq_name))) {
973 kfree(afu->psl_irq_name);
974 afu->psl_irq_name = NULL;
975 }
976 return rc;
977}
978
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100979void cxl_native_release_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100980{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100981 if (afu->native->psl_virq != irq_find_mapping(NULL, afu->native->psl_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +0100982 return;
983
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100984 cxl_unmap_irq(afu->native->psl_virq, afu);
985 cxl_ops->release_one_irq(afu->adapter, afu->native->psl_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100986 kfree(afu->psl_irq_name);
987}
988
Ian Munsief204e0b2014-10-08 19:55:02 +1100989static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
990{
991 u64 dsisr;
992
Rasmus Villemoesde369532015-06-11 13:27:52 +0200993 pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat);
Ian Munsief204e0b2014-10-08 19:55:02 +1100994
995 /* Clear PSL_DSISR[PE] */
996 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
997 cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
998
999 /* Write 1s to clear error status bits */
1000 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
1001}
1002
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001003static int native_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
Ian Munsief204e0b2014-10-08 19:55:02 +11001004{
Ian Munsie9bcf28c2015-01-09 20:34:36 +11001005 trace_cxl_psl_irq_ack(ctx, tfc);
Ian Munsief204e0b2014-10-08 19:55:02 +11001006 if (tfc)
1007 cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
1008 if (psl_reset_mask)
1009 recover_psl_err(ctx->afu, psl_reset_mask);
1010
1011 return 0;
1012}
1013
1014int cxl_check_error(struct cxl_afu *afu)
1015{
1016 return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
1017}
Frederic Barratd56d3012016-03-04 12:26:26 +01001018
Christophe Lombard47528762016-03-04 12:26:37 +01001019static bool native_support_attributes(const char *attr_name,
1020 enum cxl_attrs type)
1021{
1022 return true;
1023}
1024
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001025static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001026{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001027 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001028 return -EIO;
1029 if (unlikely(off >= afu->crs_len))
1030 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001031 *out = in_le64(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001032 (cr * afu->crs_len) + off);
1033 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001034}
1035
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001036static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001037{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001038 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001039 return -EIO;
1040 if (unlikely(off >= afu->crs_len))
1041 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001042 *out = in_le32(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001043 (cr * afu->crs_len) + off);
1044 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001045}
1046
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001047static int native_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001048{
1049 u64 aligned_off = off & ~0x3L;
1050 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001051 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001052
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001053 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001054 if (!rc)
1055 *out = (val >> ((off & 0x3) * 8)) & 0xffff;
1056 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001057}
1058
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001059static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001060{
1061 u64 aligned_off = off & ~0x3L;
1062 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001063 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001064
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001065 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001066 if (!rc)
1067 *out = (val >> ((off & 0x3) * 8)) & 0xff;
1068 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001069}
Frederic Barrat5be587b2016-03-04 12:26:28 +01001070
Frederic Barratd601ea92016-03-04 12:26:40 +01001071static int native_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
1072{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001073 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barratd601ea92016-03-04 12:26:40 +01001074 return -EIO;
1075 if (unlikely(off >= afu->crs_len))
1076 return -ERANGE;
1077 out_le32(afu->native->afu_desc_mmio + afu->crs_offset +
1078 (cr * afu->crs_len) + off, in);
1079 return 0;
1080}
1081
1082static int native_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
1083{
1084 u64 aligned_off = off & ~0x3L;
1085 u32 val32, mask, shift;
1086 int rc;
1087
1088 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1089 if (rc)
1090 return rc;
1091 shift = (off & 0x3) * 8;
1092 WARN_ON(shift == 24);
1093 mask = 0xffff << shift;
1094 val32 = (val32 & ~mask) | (in << shift);
1095
1096 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1097 return rc;
1098}
1099
1100static int native_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
1101{
1102 u64 aligned_off = off & ~0x3L;
1103 u32 val32, mask, shift;
1104 int rc;
1105
1106 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1107 if (rc)
1108 return rc;
1109 shift = (off & 0x3) * 8;
1110 mask = 0xff << shift;
1111 val32 = (val32 & ~mask) | (in << shift);
1112
1113 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1114 return rc;
1115}
1116
Frederic Barrat5be587b2016-03-04 12:26:28 +01001117const struct cxl_backend_ops cxl_native_ops = {
1118 .module = THIS_MODULE,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001119 .adapter_reset = cxl_pci_reset,
1120 .alloc_one_irq = cxl_pci_alloc_one_irq,
1121 .release_one_irq = cxl_pci_release_one_irq,
1122 .alloc_irq_ranges = cxl_pci_alloc_irq_ranges,
1123 .release_irq_ranges = cxl_pci_release_irq_ranges,
1124 .setup_irq = cxl_pci_setup_irq,
1125 .handle_psl_slice_error = native_handle_psl_slice_error,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001126 .psl_interrupt = NULL,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001127 .ack_irq = native_ack_irq,
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001128 .irq_wait = native_irq_wait,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001129 .attach_process = native_attach_process,
1130 .detach_process = native_detach_process,
Christophe Lombard47528762016-03-04 12:26:37 +01001131 .support_attributes = native_support_attributes,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001132 .link_ok = cxl_adapter_link_ok,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001133 .release_afu = cxl_pci_release_afu,
1134 .afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
1135 .afu_check_and_enable = native_afu_check_and_enable,
1136 .afu_activate_mode = native_afu_activate_mode,
1137 .afu_deactivate_mode = native_afu_deactivate_mode,
1138 .afu_reset = native_afu_reset,
1139 .afu_cr_read8 = native_afu_cr_read8,
1140 .afu_cr_read16 = native_afu_cr_read16,
1141 .afu_cr_read32 = native_afu_cr_read32,
1142 .afu_cr_read64 = native_afu_cr_read64,
Frederic Barratd601ea92016-03-04 12:26:40 +01001143 .afu_cr_write8 = native_afu_cr_write8,
1144 .afu_cr_write16 = native_afu_cr_write16,
1145 .afu_cr_write32 = native_afu_cr_write32,
1146 .read_adapter_vpd = cxl_pci_read_adapter_vpd,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001147};