blob: ecf7557cd657f66caa932fce83c785bc241bda9e [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 Munsief204e0b2014-10-08 19:55:02 +1100189 /* Work out how many pages to allocate */
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100190 afu->native->spa_order = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100191 do {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100192 afu->native->spa_order++;
193 afu->native->spa_size = (1 << afu->native->spa_order) * PAGE_SIZE;
194 afu->native->spa_max_procs = spa_max_procs(afu->native->spa_size);
195 } while (afu->native->spa_max_procs < afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100196
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100197 WARN_ON(afu->native->spa_size > 0x100000); /* Max size supported by the hardware */
Ian Munsief204e0b2014-10-08 19:55:02 +1100198
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100199 if (!(afu->native->spa = (struct cxl_process_element *)
200 __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->native->spa_order))) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100201 pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
202 return -ENOMEM;
203 }
204 pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100205 1<<afu->native->spa_order, afu->native->spa_max_procs, afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100206
Daniel Axtens051557722015-08-14 17:41:19 +1000207 return 0;
208}
209
210static void attach_spa(struct cxl_afu *afu)
211{
212 u64 spap;
213
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100214 afu->native->sw_command_status = (__be64 *)((char *)afu->native->spa +
215 ((afu->native->spa_max_procs + 3) * 128));
Ian Munsief204e0b2014-10-08 19:55:02 +1100216
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100217 spap = virt_to_phys(afu->native->spa) & CXL_PSL_SPAP_Addr;
218 spap |= ((afu->native->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
Ian Munsief204e0b2014-10-08 19:55:02 +1100219 spap |= CXL_PSL_SPAP_V;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100220 pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n",
221 afu->native->spa, afu->native->spa_max_procs,
222 afu->native->sw_command_status, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100223 cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100224}
225
Daniel Axtens051557722015-08-14 17:41:19 +1000226static inline void detach_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100227{
Ian Munsiedb7933f2014-12-08 19:18:00 +1100228 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
Daniel Axtens051557722015-08-14 17:41:19 +1000229}
230
231void cxl_release_spa(struct cxl_afu *afu)
232{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100233 if (afu->native->spa) {
234 free_pages((unsigned long) afu->native->spa,
235 afu->native->spa_order);
236 afu->native->spa = NULL;
Daniel Axtens051557722015-08-14 17:41:19 +1000237 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100238}
239
240int cxl_tlb_slb_invalidate(struct cxl *adapter)
241{
242 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
243
244 pr_devel("CXL adapter wide TLBIA & SLBIA\n");
245
246 cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
247
248 cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
249 while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
250 if (time_after_eq(jiffies, timeout)) {
251 dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
252 return -EBUSY;
253 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100254 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000255 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100256 cpu_relax();
257 }
258
259 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
260 while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
261 if (time_after_eq(jiffies, timeout)) {
262 dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
263 return -EBUSY;
264 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100265 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000266 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100267 cpu_relax();
268 }
269 return 0;
270}
271
Ian Munsief204e0b2014-10-08 19:55:02 +1100272static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
273{
274 int rc;
275
276 /* 1. Disable SSTP by writing 0 to SSTP1[V] */
277 cxl_p2n_write(afu, CXL_SSTP1_An, 0);
278
279 /* 2. Invalidate all SLB entries */
280 if ((rc = cxl_afu_slbia(afu)))
281 return rc;
282
283 /* 3. Set SSTP0_An */
284 cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
285
286 /* 4. Set SSTP1_An */
287 cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
288
289 return 0;
290}
291
292/* Using per slice version may improve performance here. (ie. SLBIA_An) */
293static void slb_invalid(struct cxl_context *ctx)
294{
295 struct cxl *adapter = ctx->afu->adapter;
296 u64 slbia;
297
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100298 WARN_ON(!mutex_is_locked(&ctx->afu->native->spa_mutex));
Ian Munsief204e0b2014-10-08 19:55:02 +1100299
300 cxl_p1_write(adapter, CXL_PSL_LBISEL,
301 ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
302 be32_to_cpu(ctx->elem->lpid));
303 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
304
305 while (1) {
Christophe Lombard0d400f72016-03-04 12:26:41 +0100306 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000307 break;
Ian Munsief204e0b2014-10-08 19:55:02 +1100308 slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
309 if (!(slbia & CXL_TLB_SLB_P))
310 break;
311 cpu_relax();
312 }
313}
314
315static int do_process_element_cmd(struct cxl_context *ctx,
316 u64 cmd, u64 pe_state)
317{
318 u64 state;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100319 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100320 int rc = 0;
321
322 trace_cxl_llcmd(ctx, cmd);
Ian Munsief204e0b2014-10-08 19:55:02 +1100323
324 WARN_ON(!ctx->afu->enabled);
325
326 ctx->elem->software_state = cpu_to_be32(pe_state);
327 smp_wmb();
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100328 *(ctx->afu->native->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100329 smp_mb();
330 cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
331 while (1) {
Ian Munsiea98e6e92014-12-08 19:17:56 +1100332 if (time_after_eq(jiffies, timeout)) {
333 dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100334 rc = -EBUSY;
335 goto out;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100336 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100337 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000338 dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
339 rc = -EIO;
340 goto out;
341 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100342 state = be64_to_cpup(ctx->afu->native->sw_command_status);
Ian Munsief204e0b2014-10-08 19:55:02 +1100343 if (state == ~0ULL) {
344 pr_err("cxl: Error adding process element to AFU\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100345 rc = -1;
346 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100347 }
348 if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
349 (cmd | (cmd >> 16) | ctx->pe))
350 break;
351 /*
352 * The command won't finish in the PSL if there are
353 * outstanding DSIs. Hence we need to yield here in
354 * case there are outstanding DSIs that we need to
355 * service. Tuning possiblity: we could wait for a
356 * while before sched
357 */
358 schedule();
359
360 }
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100361out:
362 trace_cxl_llcmd_done(ctx, cmd, rc);
363 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100364}
365
366static int add_process_element(struct cxl_context *ctx)
367{
368 int rc = 0;
369
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100370 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100371 pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
372 if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
373 ctx->pe_inserted = true;
374 pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100375 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100376 return rc;
377}
378
379static int terminate_process_element(struct cxl_context *ctx)
380{
381 int rc = 0;
382
383 /* fast path terminate if it's already invalid */
384 if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
385 return rc;
386
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100387 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100388 pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000389 /* We could be asked to terminate when the hw is down. That
390 * should always succeed: it's not running if the hw has gone
391 * away and is being reset.
392 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100393 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000394 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
395 CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
Ian Munsief204e0b2014-10-08 19:55:02 +1100396 ctx->elem->software_state = 0; /* Remove Valid bit */
397 pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100398 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100399 return rc;
400}
401
402static int remove_process_element(struct cxl_context *ctx)
403{
404 int rc = 0;
405
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100406 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100407 pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000408
409 /* We could be asked to remove when the hw is down. Again, if
410 * the hw is down, the PE is gone, so we succeed.
411 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100412 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000413 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
414
415 if (!rc)
Ian Munsief204e0b2014-10-08 19:55:02 +1100416 ctx->pe_inserted = false;
417 slb_invalid(ctx);
418 pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100419 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100420
421 return rc;
422}
423
424
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000425void cxl_assign_psn_space(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100426{
427 if (!ctx->afu->pp_size || ctx->master) {
428 ctx->psn_phys = ctx->afu->psn_phys;
429 ctx->psn_size = ctx->afu->adapter->ps_size;
430 } else {
431 ctx->psn_phys = ctx->afu->psn_phys +
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100432 (ctx->afu->native->pp_offset + ctx->afu->pp_size * ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100433 ctx->psn_size = ctx->afu->pp_size;
434 }
435}
436
437static int activate_afu_directed(struct cxl_afu *afu)
438{
439 int rc;
440
441 dev_info(&afu->dev, "Activating AFU directed mode\n");
442
Christophe Lombard4108efb2015-10-07 16:07:40 +1100443 afu->num_procs = afu->max_procs_virtualised;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100444 if (afu->native->spa == NULL) {
Daniel Axtens051557722015-08-14 17:41:19 +1000445 if (cxl_alloc_spa(afu))
446 return -ENOMEM;
447 }
448 attach_spa(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100449
450 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
451 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
452 cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
453
454 afu->current_mode = CXL_MODE_DIRECTED;
Ian Munsief204e0b2014-10-08 19:55:02 +1100455
456 if ((rc = cxl_chardev_m_afu_add(afu)))
457 return rc;
458
459 if ((rc = cxl_sysfs_afu_m_add(afu)))
460 goto err;
461
462 if ((rc = cxl_chardev_s_afu_add(afu)))
463 goto err1;
464
465 return 0;
466err1:
467 cxl_sysfs_afu_m_remove(afu);
468err:
469 cxl_chardev_afu_remove(afu);
470 return rc;
471}
472
473#ifdef CONFIG_CPU_LITTLE_ENDIAN
474#define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
475#else
476#define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
477#endif
478
Michael Neuling2f663522015-05-27 16:07:13 +1000479static u64 calculate_sr(struct cxl_context *ctx)
480{
481 u64 sr = 0;
482
Frederic Barrate606e032015-12-07 14:34:40 +0100483 set_endian(sr);
Michael Neuling2f663522015-05-27 16:07:13 +1000484 if (ctx->master)
485 sr |= CXL_PSL_SR_An_MP;
486 if (mfspr(SPRN_LPCR) & LPCR_TC)
487 sr |= CXL_PSL_SR_An_TC;
488 if (ctx->kernel) {
489 sr |= CXL_PSL_SR_An_R | (mfmsr() & MSR_SF);
490 sr |= CXL_PSL_SR_An_HV;
491 } else {
492 sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
Michael Neuling2f663522015-05-27 16:07:13 +1000493 sr &= ~(CXL_PSL_SR_An_HV);
494 if (!test_tsk_thread_flag(current, TIF_32BIT))
495 sr |= CXL_PSL_SR_An_SF;
496 }
497 return sr;
498}
499
Ian Munsief204e0b2014-10-08 19:55:02 +1100500static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
501{
Michael Neuling2f663522015-05-27 16:07:13 +1000502 u32 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100503 int r, result;
504
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000505 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100506
507 ctx->elem->ctxtime = 0; /* disable */
508 ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
509 ctx->elem->haurp = 0; /* disable */
510 ctx->elem->sdr = cpu_to_be64(mfspr(SPRN_SDR1));
511
Michael Neuling2f663522015-05-27 16:07:13 +1000512 pid = current->pid;
513 if (ctx->kernel)
514 pid = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100515 ctx->elem->common.tid = 0;
Michael Neuling2f663522015-05-27 16:07:13 +1000516 ctx->elem->common.pid = cpu_to_be32(pid);
517
518 ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100519
520 ctx->elem->common.csrp = 0; /* disable */
521 ctx->elem->common.aurp0 = 0; /* disable */
522 ctx->elem->common.aurp1 = 0; /* disable */
523
524 cxl_prefault(ctx, wed);
525
526 ctx->elem->common.sstp0 = cpu_to_be64(ctx->sstp0);
527 ctx->elem->common.sstp1 = cpu_to_be64(ctx->sstp1);
528
529 for (r = 0; r < CXL_IRQ_RANGES; r++) {
530 ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
531 ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
532 }
533
534 ctx->elem->common.amr = cpu_to_be64(amr);
535 ctx->elem->common.wed = cpu_to_be64(wed);
536
537 /* first guy needs to enable */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100538 if ((result = cxl_ops->afu_check_and_enable(ctx->afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100539 return result;
540
Daniel Axtens368857c2015-07-29 14:07:22 +1000541 return add_process_element(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100542}
543
544static int deactivate_afu_directed(struct cxl_afu *afu)
545{
546 dev_info(&afu->dev, "Deactivating AFU directed mode\n");
547
548 afu->current_mode = 0;
549 afu->num_procs = 0;
550
551 cxl_sysfs_afu_m_remove(afu);
552 cxl_chardev_afu_remove(afu);
553
Frederic Barrat5be587b2016-03-04 12:26:28 +0100554 cxl_ops->afu_reset(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100555 cxl_afu_disable(afu);
556 cxl_psl_purge(afu);
557
Ian Munsief204e0b2014-10-08 19:55:02 +1100558 return 0;
559}
560
561static int activate_dedicated_process(struct cxl_afu *afu)
562{
563 dev_info(&afu->dev, "Activating dedicated process mode\n");
564
565 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
566
567 cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
568 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
569 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
570 cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
571 cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
572 cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
573
574 cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
575 cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
576 cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
577
578 afu->current_mode = CXL_MODE_DEDICATED;
579 afu->num_procs = 1;
580
581 return cxl_chardev_d_afu_add(afu);
582}
583
584static int attach_dedicated(struct cxl_context *ctx, u64 wed, u64 amr)
585{
586 struct cxl_afu *afu = ctx->afu;
Michael Neuling2f663522015-05-27 16:07:13 +1000587 u64 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100588 int rc;
589
Michael Neuling2f663522015-05-27 16:07:13 +1000590 pid = (u64)current->pid << 32;
591 if (ctx->kernel)
592 pid = 0;
593 cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
594
595 cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100596
597 if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
598 return rc;
599
600 cxl_prefault(ctx, wed);
601
602 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
603 (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
604 (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
605 (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
606 ((u64)ctx->irqs.offset[3] & 0xffff));
607 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
608 (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
609 (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
610 (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
611 ((u64)ctx->irqs.range[3] & 0xffff));
612
613 cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
614
615 /* master only context for dedicated */
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000616 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100617
Frederic Barrat5be587b2016-03-04 12:26:28 +0100618 if ((rc = cxl_ops->afu_reset(afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100619 return rc;
620
621 cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
622
623 return afu_enable(afu);
624}
625
626static int deactivate_dedicated_process(struct cxl_afu *afu)
627{
628 dev_info(&afu->dev, "Deactivating dedicated process mode\n");
629
630 afu->current_mode = 0;
631 afu->num_procs = 0;
632
633 cxl_chardev_afu_remove(afu);
634
635 return 0;
636}
637
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100638static int native_afu_deactivate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100639{
640 if (mode == CXL_MODE_DIRECTED)
641 return deactivate_afu_directed(afu);
642 if (mode == CXL_MODE_DEDICATED)
643 return deactivate_dedicated_process(afu);
644 return 0;
645}
646
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100647static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100648{
649 if (!mode)
650 return 0;
651 if (!(mode & afu->modes_supported))
652 return -EINVAL;
653
Christophe Lombard0d400f72016-03-04 12:26:41 +0100654 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000655 WARN(1, "Device link is down, refusing to activate!\n");
656 return -EIO;
657 }
658
Ian Munsief204e0b2014-10-08 19:55:02 +1100659 if (mode == CXL_MODE_DIRECTED)
660 return activate_afu_directed(afu);
661 if (mode == CXL_MODE_DEDICATED)
662 return activate_dedicated_process(afu);
663
664 return -EINVAL;
665}
666
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100667static int native_attach_process(struct cxl_context *ctx, bool kernel,
668 u64 wed, u64 amr)
Ian Munsief204e0b2014-10-08 19:55:02 +1100669{
Christophe Lombard0d400f72016-03-04 12:26:41 +0100670 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000671 WARN(1, "Device link is down, refusing to attach process!\n");
672 return -EIO;
673 }
674
Ian Munsief204e0b2014-10-08 19:55:02 +1100675 ctx->kernel = kernel;
676 if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
677 return attach_afu_directed(ctx, wed, amr);
678
679 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
680 return attach_dedicated(ctx, wed, amr);
681
682 return -EINVAL;
683}
684
685static inline int detach_process_native_dedicated(struct cxl_context *ctx)
686{
Frederic Barrat5be587b2016-03-04 12:26:28 +0100687 cxl_ops->afu_reset(ctx->afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100688 cxl_afu_disable(ctx->afu);
689 cxl_psl_purge(ctx->afu);
690 return 0;
691}
692
Ian Munsief204e0b2014-10-08 19:55:02 +1100693static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
694{
695 if (!ctx->pe_inserted)
696 return 0;
697 if (terminate_process_element(ctx))
698 return -1;
699 if (remove_process_element(ctx))
700 return -1;
701
702 return 0;
703}
704
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100705static int native_detach_process(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100706{
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100707 trace_cxl_detach(ctx);
708
Ian Munsief204e0b2014-10-08 19:55:02 +1100709 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
710 return detach_process_native_dedicated(ctx);
711
712 return detach_process_native_afu_directed(ctx);
713}
714
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100715static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
Ian Munsief204e0b2014-10-08 19:55:02 +1100716{
717 u64 pidtid;
718
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000719 /* If the adapter has gone away, we can't get any meaningful
720 * information.
721 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100722 if (!cxl_ops->link_ok(afu->adapter, afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000723 return -EIO;
724
Ian Munsiebc78b052014-11-14 17:37:50 +1100725 info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
726 info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
727 info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
728 pidtid = cxl_p2n_read(afu, CXL_PSL_PID_TID_An);
Ian Munsief204e0b2014-10-08 19:55:02 +1100729 info->pid = pidtid >> 32;
730 info->tid = pidtid & 0xffffffff;
Ian Munsiebc78b052014-11-14 17:37:50 +1100731 info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
732 info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
Christophe Lombard444c4ba2016-03-04 12:26:34 +0100733 info->proc_handle = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100734
735 return 0;
736}
737
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100738static irqreturn_t native_handle_psl_slice_error(struct cxl_context *ctx,
739 u64 dsisr, u64 errstat)
Frederic Barratd56d3012016-03-04 12:26:26 +0100740{
741 u64 fir1, fir2, fir_slice, serr, afu_debug;
742
743 fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
744 fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
745 fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
746 serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
747 afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
748
749 dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
750 dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
751 dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
752 dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
753 dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
754 dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
755
756 dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
757 cxl_stop_trace(ctx->afu->adapter);
758
Frederic Barrat5be587b2016-03-04 12:26:28 +0100759 return cxl_ops->ack_irq(ctx, 0, errstat);
Frederic Barratd56d3012016-03-04 12:26:26 +0100760}
761
762static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
763{
764 if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
765 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
766 else
767 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
768
769 return IRQ_HANDLED;
770}
771
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100772static irqreturn_t native_irq_multiplexed(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100773{
774 struct cxl_afu *afu = data;
775 struct cxl_context *ctx;
776 struct cxl_irq_info irq_info;
777 int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
778 int ret;
779
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100780 if ((ret = native_get_irq_info(afu, &irq_info))) {
Frederic Barratd56d3012016-03-04 12:26:26 +0100781 WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
782 return fail_psl_irq(afu, &irq_info);
783 }
784
785 rcu_read_lock();
786 ctx = idr_find(&afu->contexts_idr, ph);
787 if (ctx) {
788 ret = cxl_irq(irq, ctx, &irq_info);
789 rcu_read_unlock();
790 return ret;
791 }
792 rcu_read_unlock();
793
794 WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
795 " %016llx\n(Possible AFU HW issue - was a term/remove acked"
796 " with outstanding transactions?)\n", ph, irq_info.dsisr,
797 irq_info.dar);
798 return fail_psl_irq(afu, &irq_info);
799}
800
Michael Neuling2bc79ff2016-04-22 14:57:49 +1000801void native_irq_wait(struct cxl_context *ctx)
802{
803 u64 dsisr;
804 int timeout = 1000;
805 int ph;
806
807 /*
808 * Wait until no further interrupts are presented by the PSL
809 * for this context.
810 */
811 while (timeout--) {
812 ph = cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) & 0xffff;
813 if (ph != ctx->pe)
814 return;
815 dsisr = cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An);
816 if ((dsisr & CXL_PSL_DSISR_PENDING) == 0)
817 return;
818 /*
819 * We are waiting for the workqueue to process our
820 * irq, so need to let that run here.
821 */
822 msleep(1);
823 }
824
825 dev_warn(&ctx->afu->dev, "WARNING: waiting on DSI for PE %i"
826 " DSISR %016llx!\n", ph, dsisr);
827 return;
828}
829
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100830static irqreturn_t native_slice_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100831{
832 struct cxl_afu *afu = data;
833 u64 fir_slice, errstat, serr, afu_debug;
834
835 WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
836
837 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
838 fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
839 errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
840 afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
841 dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
842 dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
843 dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
844 dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
845
846 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
847
848 return IRQ_HANDLED;
849}
850
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100851static irqreturn_t native_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +0100852{
853 struct cxl *adapter = data;
854 u64 fir1, fir2, err_ivte;
855
856 WARN(1, "CXL ERROR interrupt %i\n", irq);
857
858 err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
859 dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
860
861 dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
862 cxl_stop_trace(adapter);
863
864 fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
865 fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
866
867 dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
868
869 return IRQ_HANDLED;
870}
871
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100872int cxl_native_register_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +0100873{
874 int rc;
875
876 adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
877 dev_name(&adapter->dev));
878 if (!adapter->irq_name)
879 return -ENOMEM;
880
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100881 if ((rc = cxl_register_one_irq(adapter, native_irq_err, adapter,
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100882 &adapter->native->err_hwirq,
883 &adapter->native->err_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +0100884 adapter->irq_name))) {
885 kfree(adapter->irq_name);
886 adapter->irq_name = NULL;
887 return rc;
888 }
889
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100890 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->native->err_hwirq & 0xffff);
Frederic Barratd56d3012016-03-04 12:26:26 +0100891
892 return 0;
893}
894
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100895void cxl_native_release_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +0100896{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100897 if (adapter->native->err_virq != irq_find_mapping(NULL, adapter->native->err_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +0100898 return;
899
900 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100901 cxl_unmap_irq(adapter->native->err_virq, adapter);
902 cxl_ops->release_one_irq(adapter, adapter->native->err_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100903 kfree(adapter->irq_name);
904}
905
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100906int cxl_native_register_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100907{
908 u64 serr;
909 int rc;
910
911 afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
912 dev_name(&afu->dev));
913 if (!afu->err_irq_name)
914 return -ENOMEM;
915
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100916 if ((rc = cxl_register_one_irq(afu->adapter, native_slice_irq_err, afu,
Frederic Barratd56d3012016-03-04 12:26:26 +0100917 &afu->serr_hwirq,
918 &afu->serr_virq, afu->err_irq_name))) {
919 kfree(afu->err_irq_name);
920 afu->err_irq_name = NULL;
921 return rc;
922 }
923
924 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
925 serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
926 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
927
928 return 0;
929}
930
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100931void cxl_native_release_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100932{
933 if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
934 return;
935
936 cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
937 cxl_unmap_irq(afu->serr_virq, afu);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100938 cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100939 kfree(afu->err_irq_name);
940}
941
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100942int cxl_native_register_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100943{
944 int rc;
945
946 afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
947 dev_name(&afu->dev));
948 if (!afu->psl_irq_name)
949 return -ENOMEM;
950
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100951 if ((rc = cxl_register_one_irq(afu->adapter, native_irq_multiplexed,
952 afu, &afu->native->psl_hwirq, &afu->native->psl_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +0100953 afu->psl_irq_name))) {
954 kfree(afu->psl_irq_name);
955 afu->psl_irq_name = NULL;
956 }
957 return rc;
958}
959
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100960void cxl_native_release_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +0100961{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100962 if (afu->native->psl_virq != irq_find_mapping(NULL, afu->native->psl_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +0100963 return;
964
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100965 cxl_unmap_irq(afu->native->psl_virq, afu);
966 cxl_ops->release_one_irq(afu->adapter, afu->native->psl_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +0100967 kfree(afu->psl_irq_name);
968}
969
Ian Munsief204e0b2014-10-08 19:55:02 +1100970static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
971{
972 u64 dsisr;
973
Rasmus Villemoesde369532015-06-11 13:27:52 +0200974 pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat);
Ian Munsief204e0b2014-10-08 19:55:02 +1100975
976 /* Clear PSL_DSISR[PE] */
977 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
978 cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
979
980 /* Write 1s to clear error status bits */
981 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
982}
983
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100984static int native_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
Ian Munsief204e0b2014-10-08 19:55:02 +1100985{
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100986 trace_cxl_psl_irq_ack(ctx, tfc);
Ian Munsief204e0b2014-10-08 19:55:02 +1100987 if (tfc)
988 cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
989 if (psl_reset_mask)
990 recover_psl_err(ctx->afu, psl_reset_mask);
991
992 return 0;
993}
994
995int cxl_check_error(struct cxl_afu *afu)
996{
997 return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
998}
Frederic Barratd56d3012016-03-04 12:26:26 +0100999
Christophe Lombard47528762016-03-04 12:26:37 +01001000static bool native_support_attributes(const char *attr_name,
1001 enum cxl_attrs type)
1002{
1003 return true;
1004}
1005
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001006static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001007{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001008 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001009 return -EIO;
1010 if (unlikely(off >= afu->crs_len))
1011 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001012 *out = in_le64(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001013 (cr * afu->crs_len) + off);
1014 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001015}
1016
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001017static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001018{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001019 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001020 return -EIO;
1021 if (unlikely(off >= afu->crs_len))
1022 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001023 *out = in_le32(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001024 (cr * afu->crs_len) + off);
1025 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001026}
1027
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001028static int native_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001029{
1030 u64 aligned_off = off & ~0x3L;
1031 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001032 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001033
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001034 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001035 if (!rc)
1036 *out = (val >> ((off & 0x3) * 8)) & 0xffff;
1037 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001038}
1039
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001040static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001041{
1042 u64 aligned_off = off & ~0x3L;
1043 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001044 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001045
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001046 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001047 if (!rc)
1048 *out = (val >> ((off & 0x3) * 8)) & 0xff;
1049 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001050}
Frederic Barrat5be587b2016-03-04 12:26:28 +01001051
Frederic Barratd601ea92016-03-04 12:26:40 +01001052static int native_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
1053{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001054 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barratd601ea92016-03-04 12:26:40 +01001055 return -EIO;
1056 if (unlikely(off >= afu->crs_len))
1057 return -ERANGE;
1058 out_le32(afu->native->afu_desc_mmio + afu->crs_offset +
1059 (cr * afu->crs_len) + off, in);
1060 return 0;
1061}
1062
1063static int native_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
1064{
1065 u64 aligned_off = off & ~0x3L;
1066 u32 val32, mask, shift;
1067 int rc;
1068
1069 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1070 if (rc)
1071 return rc;
1072 shift = (off & 0x3) * 8;
1073 WARN_ON(shift == 24);
1074 mask = 0xffff << shift;
1075 val32 = (val32 & ~mask) | (in << shift);
1076
1077 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1078 return rc;
1079}
1080
1081static int native_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
1082{
1083 u64 aligned_off = off & ~0x3L;
1084 u32 val32, mask, shift;
1085 int rc;
1086
1087 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1088 if (rc)
1089 return rc;
1090 shift = (off & 0x3) * 8;
1091 mask = 0xff << shift;
1092 val32 = (val32 & ~mask) | (in << shift);
1093
1094 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1095 return rc;
1096}
1097
Frederic Barrat5be587b2016-03-04 12:26:28 +01001098const struct cxl_backend_ops cxl_native_ops = {
1099 .module = THIS_MODULE,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001100 .adapter_reset = cxl_pci_reset,
1101 .alloc_one_irq = cxl_pci_alloc_one_irq,
1102 .release_one_irq = cxl_pci_release_one_irq,
1103 .alloc_irq_ranges = cxl_pci_alloc_irq_ranges,
1104 .release_irq_ranges = cxl_pci_release_irq_ranges,
1105 .setup_irq = cxl_pci_setup_irq,
1106 .handle_psl_slice_error = native_handle_psl_slice_error,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001107 .psl_interrupt = NULL,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001108 .ack_irq = native_ack_irq,
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001109 .irq_wait = native_irq_wait,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001110 .attach_process = native_attach_process,
1111 .detach_process = native_detach_process,
Christophe Lombard47528762016-03-04 12:26:37 +01001112 .support_attributes = native_support_attributes,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001113 .link_ok = cxl_adapter_link_ok,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001114 .release_afu = cxl_pci_release_afu,
1115 .afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
1116 .afu_check_and_enable = native_afu_check_and_enable,
1117 .afu_activate_mode = native_afu_activate_mode,
1118 .afu_deactivate_mode = native_afu_deactivate_mode,
1119 .afu_reset = native_afu_reset,
1120 .afu_cr_read8 = native_afu_cr_read8,
1121 .afu_cr_read16 = native_afu_cr_read16,
1122 .afu_cr_read32 = native_afu_cr_read32,
1123 .afu_cr_read64 = native_afu_cr_read64,
Frederic Barratd601ea92016-03-04 12:26:40 +01001124 .afu_cr_write8 = native_afu_cr_write8,
1125 .afu_cr_write16 = native_afu_cr_write16,
1126 .afu_cr_write32 = native_afu_cr_write32,
1127 .read_adapter_vpd = cxl_pci_read_adapter_vpd,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001128};