blob: 2b2f8894149df307b04a8cd17b2309873e962fc3 [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>
Ingo Molnare6017572017-02-01 16:36:40 +010012#include <linux/sched/clock.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110013#include <linux/slab.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110014#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
Ian Munsie5e7823c2016-07-01 02:50:40 +100024static int afu_control(struct cxl_afu *afu, u64 command, u64 clear,
Ian Munsief204e0b2014-10-08 19:55:02 +110025 u64 result, u64 mask, bool enabled)
26{
Ian Munsie5e7823c2016-07-01 02:50:40 +100027 u64 AFU_Cntl;
Ian Munsief204e0b2014-10-08 19:55:02 +110028 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 Munsie5e7823c2016-07-01 02:50:40 +100036 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
37 cxl_p2n_write(afu, CXL_AFU_Cntl_An, (AFU_Cntl & ~clear) | command);
Ian Munsief204e0b2014-10-08 19:55:02 +110038
39 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
40 while ((AFU_Cntl & mask) != result) {
41 if (time_after_eq(jiffies, timeout)) {
42 dev_warn(&afu->dev, "WARNING: AFU control timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +110043 rc = -EBUSY;
44 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +110045 }
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100046
Christophe Lombard0d400f72016-03-04 12:26:41 +010047 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100048 afu->enabled = enabled;
49 rc = -EIO;
50 goto out;
51 }
52
Rasmus Villemoesde369532015-06-11 13:27:52 +020053 pr_devel_ratelimited("AFU control... (0x%016llx)\n",
Ian Munsief204e0b2014-10-08 19:55:02 +110054 AFU_Cntl | command);
55 cpu_relax();
56 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
Andrew Donnellan3382a622016-11-22 21:13:27 +110057 }
Ian Munsie2a4f6672016-06-30 04:51:26 +100058
59 if (AFU_Cntl & CXL_AFU_Cntl_An_RA) {
60 /*
61 * Workaround for a bug in the XSL used in the Mellanox CX4
62 * that fails to clear the RA bit after an AFU reset,
63 * preventing subsequent AFU resets from working.
64 */
65 cxl_p2n_write(afu, CXL_AFU_Cntl_An, AFU_Cntl & ~CXL_AFU_Cntl_An_RA);
66 }
67
Ian Munsief204e0b2014-10-08 19:55:02 +110068 pr_devel("AFU command complete: %llx\n", command);
69 afu->enabled = enabled;
Ian Munsie9bcf28c2015-01-09 20:34:36 +110070out:
71 trace_cxl_afu_ctrl_done(afu, command, rc);
Ian Munsief204e0b2014-10-08 19:55:02 +110072 spin_unlock(&afu->afu_cntl_lock);
73
Ian Munsie9bcf28c2015-01-09 20:34:36 +110074 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +110075}
76
77static int afu_enable(struct cxl_afu *afu)
78{
79 pr_devel("AFU enable request\n");
80
Ian Munsie5e7823c2016-07-01 02:50:40 +100081 return afu_control(afu, CXL_AFU_Cntl_An_E, 0,
Ian Munsief204e0b2014-10-08 19:55:02 +110082 CXL_AFU_Cntl_An_ES_Enabled,
83 CXL_AFU_Cntl_An_ES_MASK, true);
84}
85
86int cxl_afu_disable(struct cxl_afu *afu)
87{
88 pr_devel("AFU disable request\n");
89
Ian Munsie5e7823c2016-07-01 02:50:40 +100090 return afu_control(afu, 0, CXL_AFU_Cntl_An_E,
91 CXL_AFU_Cntl_An_ES_Disabled,
Ian Munsief204e0b2014-10-08 19:55:02 +110092 CXL_AFU_Cntl_An_ES_MASK, false);
93}
94
95/* This will disable as well as reset */
Frederic Barrat2b04cf32016-03-04 12:26:29 +010096static int native_afu_reset(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +110097{
Alastair D'Silvaa7156262017-05-01 10:53:31 +100098 int rc;
99 u64 serr;
100
Ian Munsief204e0b2014-10-08 19:55:02 +1100101 pr_devel("AFU reset request\n");
102
Alastair D'Silvaa7156262017-05-01 10:53:31 +1000103 rc = afu_control(afu, CXL_AFU_Cntl_An_RA, 0,
Ian Munsief204e0b2014-10-08 19:55:02 +1100104 CXL_AFU_Cntl_An_RS_Complete | CXL_AFU_Cntl_An_ES_Disabled,
105 CXL_AFU_Cntl_An_RS_MASK | CXL_AFU_Cntl_An_ES_MASK,
106 false);
Alastair D'Silvaa7156262017-05-01 10:53:31 +1000107
Christophe Lombard797625d2017-06-13 17:41:05 +0200108 /*
109 * Re-enable any masked interrupts when the AFU is not
110 * activated to avoid side effects after attaching a process
111 * in dedicated mode.
112 */
113 if (afu->current_mode == 0) {
114 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
115 serr &= ~CXL_PSL_SERR_An_IRQ_MASKS;
116 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
117 }
Alastair D'Silvaa7156262017-05-01 10:53:31 +1000118
119 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100120}
121
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100122static int native_afu_check_and_enable(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100123{
Christophe Lombard0d400f72016-03-04 12:26:41 +0100124 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000125 WARN(1, "Refusing to enable afu while link down!\n");
126 return -EIO;
127 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100128 if (afu->enabled)
129 return 0;
130 return afu_enable(afu);
131}
132
133int cxl_psl_purge(struct cxl_afu *afu)
134{
135 u64 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
136 u64 AFU_Cntl = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
137 u64 dsisr, dar;
138 u64 start, end;
Christophe Lombardf24be422017-04-12 16:34:07 +0200139 u64 trans_fault = 0x0ULL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100140 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100141 int rc = 0;
142
143 trace_cxl_psl_ctrl(afu, CXL_PSL_SCNTL_An_Pc);
Ian Munsief204e0b2014-10-08 19:55:02 +1100144
145 pr_devel("PSL purge request\n");
146
Christophe Lombard797625d2017-06-13 17:41:05 +0200147 if (cxl_is_power8())
Christophe Lombardf24be422017-04-12 16:34:07 +0200148 trans_fault = CXL_PSL_DSISR_TRANS;
Christophe Lombard797625d2017-06-13 17:41:05 +0200149 if (cxl_is_power9())
Christophe Lombardf24be422017-04-12 16:34:07 +0200150 trans_fault = CXL_PSL9_DSISR_An_TF;
151
Christophe Lombard0d400f72016-03-04 12:26:41 +0100152 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000153 dev_warn(&afu->dev, "PSL Purge called with link down, ignoring\n");
154 rc = -EIO;
155 goto out;
156 }
157
Ian Munsief204e0b2014-10-08 19:55:02 +1100158 if ((AFU_Cntl & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
159 WARN(1, "psl_purge request while AFU not disabled!\n");
160 cxl_afu_disable(afu);
161 }
162
163 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
164 PSL_CNTL | CXL_PSL_SCNTL_An_Pc);
165 start = local_clock();
166 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
167 while ((PSL_CNTL & CXL_PSL_SCNTL_An_Ps_MASK)
168 == CXL_PSL_SCNTL_An_Ps_Pending) {
169 if (time_after_eq(jiffies, timeout)) {
170 dev_warn(&afu->dev, "WARNING: PSL Purge timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100171 rc = -EBUSY;
172 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100173 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100174 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000175 rc = -EIO;
176 goto out;
177 }
178
Ian Munsief204e0b2014-10-08 19:55:02 +1100179 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
Christophe Lombardabd1d992017-04-07 16:11:58 +0200180 pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%016llx PSL_DSISR: 0x%016llx\n",
181 PSL_CNTL, dsisr);
182
Christophe Lombardf24be422017-04-12 16:34:07 +0200183 if (dsisr & trans_fault) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100184 dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
Christophe Lombardabd1d992017-04-07 16:11:58 +0200185 dev_notice(&afu->dev, "PSL purge terminating pending translation, DSISR: 0x%016llx, DAR: 0x%016llx\n",
186 dsisr, dar);
Ian Munsief204e0b2014-10-08 19:55:02 +1100187 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
188 } else if (dsisr) {
Christophe Lombardabd1d992017-04-07 16:11:58 +0200189 dev_notice(&afu->dev, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%016llx\n",
190 dsisr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100191 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
192 } else {
193 cpu_relax();
194 }
195 PSL_CNTL = cxl_p1n_read(afu, CXL_PSL_SCNTL_An);
Andrew Donnellan3382a622016-11-22 21:13:27 +1100196 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100197 end = local_clock();
198 pr_devel("PSL purged in %lld ns\n", end - start);
199
200 cxl_p1n_write(afu, CXL_PSL_SCNTL_An,
201 PSL_CNTL & ~CXL_PSL_SCNTL_An_Pc);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100202out:
203 trace_cxl_psl_ctrl_done(afu, CXL_PSL_SCNTL_An_Pc, rc);
204 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100205}
206
207static int spa_max_procs(int spa_size)
208{
209 /*
210 * From the CAIA:
211 * end_of_SPA_area = SPA_Base + ((n+4) * 128) + (( ((n*8) + 127) >> 7) * 128) + 255
212 * Most of that junk is really just an overly-complicated way of saying
213 * the last 256 bytes are __aligned(128), so it's really:
214 * end_of_SPA_area = end_of_PSL_queue_area + __aligned(128) 255
215 * and
216 * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
217 * so
218 * sizeof(SPA) = ((n+4) * 128) + (n*8) + __aligned(128) 256
219 * Ignore the alignment (which is safe in this case as long as we are
220 * careful with our rounding) and solve for n:
221 */
222 return ((spa_size / 8) - 96) / 17;
223}
224
Christophe Lombardf24be422017-04-12 16:34:07 +0200225static int cxl_alloc_spa(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100226{
Ian Munsie895a7982016-05-04 14:46:30 +1000227 unsigned spa_size;
228
Ian Munsief204e0b2014-10-08 19:55:02 +1100229 /* Work out how many pages to allocate */
Ian Munsie2224b672016-06-29 22:16:26 +1000230 afu->native->spa_order = -1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100231 do {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100232 afu->native->spa_order++;
Ian Munsie895a7982016-05-04 14:46:30 +1000233 spa_size = (1 << afu->native->spa_order) * PAGE_SIZE;
234
235 if (spa_size > 0x100000) {
236 dev_warn(&afu->dev, "num_of_processes too large for the SPA, limiting to %i (0x%x)\n",
237 afu->native->spa_max_procs, afu->native->spa_size);
Christophe Lombardf24be422017-04-12 16:34:07 +0200238 if (mode != CXL_MODE_DEDICATED)
239 afu->num_procs = afu->native->spa_max_procs;
Ian Munsie895a7982016-05-04 14:46:30 +1000240 break;
241 }
242
243 afu->native->spa_size = spa_size;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100244 afu->native->spa_max_procs = spa_max_procs(afu->native->spa_size);
245 } while (afu->native->spa_max_procs < afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100246
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100247 if (!(afu->native->spa = (struct cxl_process_element *)
248 __get_free_pages(GFP_KERNEL | __GFP_ZERO, afu->native->spa_order))) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100249 pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
250 return -ENOMEM;
251 }
252 pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100253 1<<afu->native->spa_order, afu->native->spa_max_procs, afu->num_procs);
Ian Munsief204e0b2014-10-08 19:55:02 +1100254
Daniel Axtens051557722015-08-14 17:41:19 +1000255 return 0;
256}
257
258static void attach_spa(struct cxl_afu *afu)
259{
260 u64 spap;
261
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100262 afu->native->sw_command_status = (__be64 *)((char *)afu->native->spa +
263 ((afu->native->spa_max_procs + 3) * 128));
Ian Munsief204e0b2014-10-08 19:55:02 +1100264
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100265 spap = virt_to_phys(afu->native->spa) & CXL_PSL_SPAP_Addr;
266 spap |= ((afu->native->spa_size >> (12 - CXL_PSL_SPAP_Size_Shift)) - 1) & CXL_PSL_SPAP_Size;
Ian Munsief204e0b2014-10-08 19:55:02 +1100267 spap |= CXL_PSL_SPAP_V;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100268 pr_devel("cxl: SPA allocated at 0x%p. Max processes: %i, sw_command_status: 0x%p CXL_PSL_SPAP_An=0x%016llx\n",
269 afu->native->spa, afu->native->spa_max_procs,
270 afu->native->sw_command_status, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100271 cxl_p1n_write(afu, CXL_PSL_SPAP_An, spap);
Ian Munsief204e0b2014-10-08 19:55:02 +1100272}
273
Daniel Axtens051557722015-08-14 17:41:19 +1000274static inline void detach_spa(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100275{
Ian Munsiedb7933f2014-12-08 19:18:00 +1100276 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0);
Daniel Axtens051557722015-08-14 17:41:19 +1000277}
278
279void cxl_release_spa(struct cxl_afu *afu)
280{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100281 if (afu->native->spa) {
282 free_pages((unsigned long) afu->native->spa,
283 afu->native->spa_order);
284 afu->native->spa = NULL;
Daniel Axtens051557722015-08-14 17:41:19 +1000285 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100286}
287
Christophe Lombardf24be422017-04-12 16:34:07 +0200288/*
289 * Invalidation of all ERAT entries is no longer required by CAIA2. Use
290 * only for debug.
291 */
292int cxl_invalidate_all_psl9(struct cxl *adapter)
293{
294 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
295 u64 ierat;
296
297 pr_devel("CXL adapter - invalidation of all ERAT entries\n");
298
299 /* Invalidates all ERAT entries for Radix or HPT */
300 ierat = CXL_XSL9_IERAT_IALL;
301 if (radix_enabled())
302 ierat |= CXL_XSL9_IERAT_INVR;
303 cxl_p1_write(adapter, CXL_XSL9_IERAT, ierat);
304
305 while (cxl_p1_read(adapter, CXL_XSL9_IERAT) & CXL_XSL9_IERAT_IINPROG) {
306 if (time_after_eq(jiffies, timeout)) {
307 dev_warn(&adapter->dev,
308 "WARNING: CXL adapter invalidation of all ERAT entries timed out!\n");
309 return -EBUSY;
310 }
311 if (!cxl_ops->link_ok(adapter, NULL))
312 return -EIO;
313 cpu_relax();
314 }
315 return 0;
316}
317
Christophe Lombard64663f32017-04-07 16:11:57 +0200318int cxl_invalidate_all_psl8(struct cxl *adapter)
Ian Munsief204e0b2014-10-08 19:55:02 +1100319{
320 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
321
322 pr_devel("CXL adapter wide TLBIA & SLBIA\n");
323
324 cxl_p1_write(adapter, CXL_PSL_AFUSEL, CXL_PSL_AFUSEL_A);
325
326 cxl_p1_write(adapter, CXL_PSL_TLBIA, CXL_TLB_SLB_IQ_ALL);
327 while (cxl_p1_read(adapter, CXL_PSL_TLBIA) & CXL_TLB_SLB_P) {
328 if (time_after_eq(jiffies, timeout)) {
329 dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
330 return -EBUSY;
331 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100332 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000333 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100334 cpu_relax();
335 }
336
337 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_ALL);
338 while (cxl_p1_read(adapter, CXL_PSL_SLBIA) & CXL_TLB_SLB_P) {
339 if (time_after_eq(jiffies, timeout)) {
340 dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
341 return -EBUSY;
342 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100343 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000344 return -EIO;
Ian Munsief204e0b2014-10-08 19:55:02 +1100345 cpu_relax();
346 }
347 return 0;
348}
349
Frederic Barrataaa22452016-10-03 21:36:02 +0200350int cxl_data_cache_flush(struct cxl *adapter)
351{
352 u64 reg;
353 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
354
355 pr_devel("Flushing data cache\n");
356
357 reg = cxl_p1_read(adapter, CXL_PSL_Control);
358 reg |= CXL_PSL_Control_Fr;
359 cxl_p1_write(adapter, CXL_PSL_Control, reg);
360
361 reg = cxl_p1_read(adapter, CXL_PSL_Control);
362 while ((reg & CXL_PSL_Control_Fs_MASK) != CXL_PSL_Control_Fs_Complete) {
363 if (time_after_eq(jiffies, timeout)) {
364 dev_warn(&adapter->dev, "WARNING: cache flush timed out!\n");
365 return -EBUSY;
366 }
367
368 if (!cxl_ops->link_ok(adapter, NULL)) {
369 dev_warn(&adapter->dev, "WARNING: link down when flushing cache\n");
370 return -EIO;
371 }
372 cpu_relax();
373 reg = cxl_p1_read(adapter, CXL_PSL_Control);
374 }
375
376 reg &= ~CXL_PSL_Control_Fr;
377 cxl_p1_write(adapter, CXL_PSL_Control, reg);
378 return 0;
379}
380
Ian Munsief204e0b2014-10-08 19:55:02 +1100381static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
382{
383 int rc;
384
385 /* 1. Disable SSTP by writing 0 to SSTP1[V] */
386 cxl_p2n_write(afu, CXL_SSTP1_An, 0);
387
388 /* 2. Invalidate all SLB entries */
389 if ((rc = cxl_afu_slbia(afu)))
390 return rc;
391
392 /* 3. Set SSTP0_An */
393 cxl_p2n_write(afu, CXL_SSTP0_An, sstp0);
394
395 /* 4. Set SSTP1_An */
396 cxl_p2n_write(afu, CXL_SSTP1_An, sstp1);
397
398 return 0;
399}
400
401/* Using per slice version may improve performance here. (ie. SLBIA_An) */
402static void slb_invalid(struct cxl_context *ctx)
403{
404 struct cxl *adapter = ctx->afu->adapter;
405 u64 slbia;
406
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100407 WARN_ON(!mutex_is_locked(&ctx->afu->native->spa_mutex));
Ian Munsief204e0b2014-10-08 19:55:02 +1100408
409 cxl_p1_write(adapter, CXL_PSL_LBISEL,
410 ((u64)be32_to_cpu(ctx->elem->common.pid) << 32) |
411 be32_to_cpu(ctx->elem->lpid));
412 cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
413
414 while (1) {
Christophe Lombard0d400f72016-03-04 12:26:41 +0100415 if (!cxl_ops->link_ok(adapter, NULL))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000416 break;
Ian Munsief204e0b2014-10-08 19:55:02 +1100417 slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
418 if (!(slbia & CXL_TLB_SLB_P))
419 break;
420 cpu_relax();
421 }
422}
423
424static int do_process_element_cmd(struct cxl_context *ctx,
425 u64 cmd, u64 pe_state)
426{
427 u64 state;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100428 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100429 int rc = 0;
430
431 trace_cxl_llcmd(ctx, cmd);
Ian Munsief204e0b2014-10-08 19:55:02 +1100432
433 WARN_ON(!ctx->afu->enabled);
434
435 ctx->elem->software_state = cpu_to_be32(pe_state);
436 smp_wmb();
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100437 *(ctx->afu->native->sw_command_status) = cpu_to_be64(cmd | 0 | ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100438 smp_mb();
439 cxl_p1n_write(ctx->afu, CXL_PSL_LLCMD_An, cmd | ctx->pe);
440 while (1) {
Ian Munsiea98e6e92014-12-08 19:17:56 +1100441 if (time_after_eq(jiffies, timeout)) {
442 dev_warn(&ctx->afu->dev, "WARNING: Process Element Command timed out!\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100443 rc = -EBUSY;
444 goto out;
Ian Munsiea98e6e92014-12-08 19:17:56 +1100445 }
Christophe Lombard0d400f72016-03-04 12:26:41 +0100446 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000447 dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
448 rc = -EIO;
449 goto out;
450 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100451 state = be64_to_cpup(ctx->afu->native->sw_command_status);
Ian Munsief204e0b2014-10-08 19:55:02 +1100452 if (state == ~0ULL) {
453 pr_err("cxl: Error adding process element to AFU\n");
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100454 rc = -1;
455 goto out;
Ian Munsief204e0b2014-10-08 19:55:02 +1100456 }
457 if ((state & (CXL_SPA_SW_CMD_MASK | CXL_SPA_SW_STATE_MASK | CXL_SPA_SW_LINK_MASK)) ==
458 (cmd | (cmd >> 16) | ctx->pe))
459 break;
460 /*
461 * The command won't finish in the PSL if there are
462 * outstanding DSIs. Hence we need to yield here in
463 * case there are outstanding DSIs that we need to
464 * service. Tuning possiblity: we could wait for a
465 * while before sched
466 */
467 schedule();
468
469 }
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100470out:
471 trace_cxl_llcmd_done(ctx, cmd, rc);
472 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100473}
474
475static int add_process_element(struct cxl_context *ctx)
476{
477 int rc = 0;
478
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100479 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100480 pr_devel("%s Adding pe: %i started\n", __func__, ctx->pe);
481 if (!(rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_ADD, CXL_PE_SOFTWARE_STATE_V)))
482 ctx->pe_inserted = true;
483 pr_devel("%s Adding pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100484 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100485 return rc;
486}
487
488static int terminate_process_element(struct cxl_context *ctx)
489{
490 int rc = 0;
491
492 /* fast path terminate if it's already invalid */
493 if (!(ctx->elem->software_state & cpu_to_be32(CXL_PE_SOFTWARE_STATE_V)))
494 return rc;
495
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100496 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100497 pr_devel("%s Terminate pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000498 /* We could be asked to terminate when the hw is down. That
499 * should always succeed: it's not running if the hw has gone
500 * away and is being reset.
501 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100502 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000503 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
504 CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
Ian Munsief204e0b2014-10-08 19:55:02 +1100505 ctx->elem->software_state = 0; /* Remove Valid bit */
506 pr_devel("%s Terminate pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100507 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100508 return rc;
509}
510
511static int remove_process_element(struct cxl_context *ctx)
512{
513 int rc = 0;
514
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100515 mutex_lock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100516 pr_devel("%s Remove pe: %i started\n", __func__, ctx->pe);
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000517
518 /* We could be asked to remove when the hw is down. Again, if
519 * the hw is down, the PE is gone, so we succeed.
520 */
Christophe Lombard0d400f72016-03-04 12:26:41 +0100521 if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000522 rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
523
524 if (!rc)
Ian Munsief204e0b2014-10-08 19:55:02 +1100525 ctx->pe_inserted = false;
Christophe Lombardabd1d992017-04-07 16:11:58 +0200526 if (cxl_is_power8())
527 slb_invalid(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100528 pr_devel("%s Remove pe: %i finished\n", __func__, ctx->pe);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100529 mutex_unlock(&ctx->afu->native->spa_mutex);
Ian Munsief204e0b2014-10-08 19:55:02 +1100530
531 return rc;
532}
533
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000534void cxl_assign_psn_space(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +1100535{
536 if (!ctx->afu->pp_size || ctx->master) {
537 ctx->psn_phys = ctx->afu->psn_phys;
538 ctx->psn_size = ctx->afu->adapter->ps_size;
539 } else {
540 ctx->psn_phys = ctx->afu->psn_phys +
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100541 (ctx->afu->native->pp_offset + ctx->afu->pp_size * ctx->pe);
Ian Munsief204e0b2014-10-08 19:55:02 +1100542 ctx->psn_size = ctx->afu->pp_size;
543 }
544}
545
546static int activate_afu_directed(struct cxl_afu *afu)
547{
548 int rc;
549
550 dev_info(&afu->dev, "Activating AFU directed mode\n");
551
Christophe Lombard4108efb2015-10-07 16:07:40 +1100552 afu->num_procs = afu->max_procs_virtualised;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100553 if (afu->native->spa == NULL) {
Christophe Lombardf24be422017-04-12 16:34:07 +0200554 if (cxl_alloc_spa(afu, CXL_MODE_DIRECTED))
Daniel Axtens051557722015-08-14 17:41:19 +1000555 return -ENOMEM;
556 }
557 attach_spa(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100558
559 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_AFU);
Christophe Lombardabd1d992017-04-07 16:11:58 +0200560 if (cxl_is_power8())
561 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
Ian Munsief204e0b2014-10-08 19:55:02 +1100562 cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
563
564 afu->current_mode = CXL_MODE_DIRECTED;
Ian Munsief204e0b2014-10-08 19:55:02 +1100565
566 if ((rc = cxl_chardev_m_afu_add(afu)))
567 return rc;
568
569 if ((rc = cxl_sysfs_afu_m_add(afu)))
570 goto err;
571
572 if ((rc = cxl_chardev_s_afu_add(afu)))
573 goto err1;
574
575 return 0;
576err1:
577 cxl_sysfs_afu_m_remove(afu);
578err:
579 cxl_chardev_afu_remove(afu);
580 return rc;
581}
582
583#ifdef CONFIG_CPU_LITTLE_ENDIAN
584#define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
585#else
586#define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
587#endif
588
Michael Neuling2f663522015-05-27 16:07:13 +1000589static u64 calculate_sr(struct cxl_context *ctx)
590{
591 u64 sr = 0;
592
Frederic Barrate606e032015-12-07 14:34:40 +0100593 set_endian(sr);
Michael Neuling2f663522015-05-27 16:07:13 +1000594 if (ctx->master)
595 sr |= CXL_PSL_SR_An_MP;
596 if (mfspr(SPRN_LPCR) & LPCR_TC)
597 sr |= CXL_PSL_SR_An_TC;
598 if (ctx->kernel) {
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000599 if (!ctx->real_mode)
600 sr |= CXL_PSL_SR_An_R;
601 sr |= (mfmsr() & MSR_SF) | CXL_PSL_SR_An_HV;
Michael Neuling2f663522015-05-27 16:07:13 +1000602 } else {
603 sr |= CXL_PSL_SR_An_PR | CXL_PSL_SR_An_R;
Christophe Lombardf24be422017-04-12 16:34:07 +0200604 if (radix_enabled())
605 sr |= CXL_PSL_SR_An_HV;
606 else
607 sr &= ~(CXL_PSL_SR_An_HV);
Michael Neuling2f663522015-05-27 16:07:13 +1000608 if (!test_tsk_thread_flag(current, TIF_32BIT))
609 sr |= CXL_PSL_SR_An_SF;
610 }
Christophe Lombard797625d2017-06-13 17:41:05 +0200611 if (cxl_is_power9()) {
Christophe Lombardf24be422017-04-12 16:34:07 +0200612 if (radix_enabled())
613 sr |= CXL_PSL_SR_An_XLAT_ror;
614 else
615 sr |= CXL_PSL_SR_An_XLAT_hpt;
616 }
Michael Neuling2f663522015-05-27 16:07:13 +1000617 return sr;
618}
619
Ian Munsie292841b2016-05-24 02:14:05 +1000620static void update_ivtes_directed(struct cxl_context *ctx)
621{
622 bool need_update = (ctx->status == STARTED);
623 int r;
624
625 if (need_update) {
626 WARN_ON(terminate_process_element(ctx));
627 WARN_ON(remove_process_element(ctx));
628 }
629
630 for (r = 0; r < CXL_IRQ_RANGES; r++) {
631 ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
632 ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
633 }
634
635 /*
636 * Theoretically we could use the update llcmd, instead of a
637 * terminate/remove/add (or if an atomic update was required we could
638 * do a suspend/update/resume), however it seems there might be issues
639 * with the update llcmd on some cards (including those using an XSL on
640 * an ASIC) so for now it's safest to go with the commands that are
641 * known to work. In the future if we come across a situation where the
642 * card may be performing transactions using the same PE while we are
643 * doing this update we might need to revisit this.
644 */
645 if (need_update)
646 WARN_ON(add_process_element(ctx));
647}
648
Christophe Lombardf24be422017-04-12 16:34:07 +0200649static int process_element_entry_psl9(struct cxl_context *ctx, u64 wed, u64 amr)
650{
651 u32 pid;
652
653 cxl_assign_psn_space(ctx);
654
655 ctx->elem->ctxtime = 0; /* disable */
656 ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
657 ctx->elem->haurp = 0; /* disable */
658
659 if (ctx->kernel)
660 pid = 0;
661 else {
662 if (ctx->mm == NULL) {
663 pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
664 __func__, ctx->pe, pid_nr(ctx->pid));
665 return -EINVAL;
666 }
667 pid = ctx->mm->context.id;
668 }
669
670 ctx->elem->common.tid = 0;
671 ctx->elem->common.pid = cpu_to_be32(pid);
672
673 ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
674
675 ctx->elem->common.csrp = 0; /* disable */
676
677 cxl_prefault(ctx, wed);
678
679 /*
680 * Ensure we have the multiplexed PSL interrupt set up to take faults
681 * for kernel contexts that may not have allocated any AFU IRQs at all:
682 */
683 if (ctx->irqs.range[0] == 0) {
684 ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
685 ctx->irqs.range[0] = 1;
686 }
687
688 ctx->elem->common.amr = cpu_to_be64(amr);
689 ctx->elem->common.wed = cpu_to_be64(wed);
690
691 return 0;
692}
693
694int cxl_attach_afu_directed_psl9(struct cxl_context *ctx, u64 wed, u64 amr)
695{
696 int result;
697
698 /* fill the process element entry */
699 result = process_element_entry_psl9(ctx, wed, amr);
700 if (result)
701 return result;
702
703 update_ivtes_directed(ctx);
704
705 /* first guy needs to enable */
706 result = cxl_ops->afu_check_and_enable(ctx->afu);
707 if (result)
708 return result;
709
710 return add_process_element(ctx);
711}
712
Christophe Lombard64663f32017-04-07 16:11:57 +0200713int cxl_attach_afu_directed_psl8(struct cxl_context *ctx, u64 wed, u64 amr)
Ian Munsief204e0b2014-10-08 19:55:02 +1100714{
Michael Neuling2f663522015-05-27 16:07:13 +1000715 u32 pid;
Ian Munsie292841b2016-05-24 02:14:05 +1000716 int result;
Ian Munsief204e0b2014-10-08 19:55:02 +1100717
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000718 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100719
720 ctx->elem->ctxtime = 0; /* disable */
721 ctx->elem->lpid = cpu_to_be32(mfspr(SPRN_LPID));
722 ctx->elem->haurp = 0; /* disable */
Christophe Lombardf24be422017-04-12 16:34:07 +0200723 ctx->elem->u.sdr = cpu_to_be64(mfspr(SPRN_SDR1));
Ian Munsief204e0b2014-10-08 19:55:02 +1100724
Michael Neuling2f663522015-05-27 16:07:13 +1000725 pid = current->pid;
726 if (ctx->kernel)
727 pid = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100728 ctx->elem->common.tid = 0;
Michael Neuling2f663522015-05-27 16:07:13 +1000729 ctx->elem->common.pid = cpu_to_be32(pid);
730
731 ctx->elem->sr = cpu_to_be64(calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100732
733 ctx->elem->common.csrp = 0; /* disable */
Christophe Lombardf24be422017-04-12 16:34:07 +0200734 ctx->elem->common.u.psl8.aurp0 = 0; /* disable */
735 ctx->elem->common.u.psl8.aurp1 = 0; /* disable */
Ian Munsief204e0b2014-10-08 19:55:02 +1100736
737 cxl_prefault(ctx, wed);
738
Christophe Lombardf24be422017-04-12 16:34:07 +0200739 ctx->elem->common.u.psl8.sstp0 = cpu_to_be64(ctx->sstp0);
740 ctx->elem->common.u.psl8.sstp1 = cpu_to_be64(ctx->sstp1);
Ian Munsief204e0b2014-10-08 19:55:02 +1100741
Ian Munsie3c206fa2016-05-04 14:52:58 +1000742 /*
743 * Ensure we have the multiplexed PSL interrupt set up to take faults
744 * for kernel contexts that may not have allocated any AFU IRQs at all:
745 */
746 if (ctx->irqs.range[0] == 0) {
747 ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
748 ctx->irqs.range[0] = 1;
749 }
750
Ian Munsie292841b2016-05-24 02:14:05 +1000751 update_ivtes_directed(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100752
753 ctx->elem->common.amr = cpu_to_be64(amr);
754 ctx->elem->common.wed = cpu_to_be64(wed);
755
756 /* first guy needs to enable */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100757 if ((result = cxl_ops->afu_check_and_enable(ctx->afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100758 return result;
759
Daniel Axtens368857c2015-07-29 14:07:22 +1000760 return add_process_element(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100761}
762
763static int deactivate_afu_directed(struct cxl_afu *afu)
764{
765 dev_info(&afu->dev, "Deactivating AFU directed mode\n");
766
767 afu->current_mode = 0;
768 afu->num_procs = 0;
769
770 cxl_sysfs_afu_m_remove(afu);
771 cxl_chardev_afu_remove(afu);
772
Ian Munsie5e7823c2016-07-01 02:50:40 +1000773 /*
774 * The CAIA section 2.2.1 indicates that the procedure for starting and
775 * stopping an AFU in AFU directed mode is AFU specific, which is not
776 * ideal since this code is generic and with one exception has no
777 * knowledge of the AFU. This is in contrast to the procedure for
778 * disabling a dedicated process AFU, which is documented to just
779 * require a reset. The architecture does indicate that both an AFU
780 * reset and an AFU disable should result in the AFU being disabled and
781 * we do both followed by a PSL purge for safety.
782 *
783 * Notably we used to have some issues with the disable sequence on PSL
784 * cards, which is why we ended up using this heavy weight procedure in
785 * the first place, however a bug was discovered that had rendered the
786 * disable operation ineffective, so it is conceivable that was the
787 * sole explanation for those difficulties. Careful regression testing
788 * is recommended if anyone attempts to remove or reorder these
789 * operations.
790 *
791 * The XSL on the Mellanox CX4 behaves a little differently from the
792 * PSL based cards and will time out an AFU reset if the AFU is still
793 * enabled. That card is special in that we do have a means to identify
794 * it from this code, so in that case we skip the reset and just use a
795 * disable/purge to avoid the timeout and corresponding noise in the
796 * kernel log.
797 */
798 if (afu->adapter->native->sl_ops->needs_reset_before_disable)
799 cxl_ops->afu_reset(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100800 cxl_afu_disable(afu);
801 cxl_psl_purge(afu);
802
Ian Munsief204e0b2014-10-08 19:55:02 +1100803 return 0;
804}
805
Christophe Lombardf24be422017-04-12 16:34:07 +0200806int cxl_activate_dedicated_process_psl9(struct cxl_afu *afu)
807{
808 dev_info(&afu->dev, "Activating dedicated process mode\n");
809
810 /*
811 * If XSL is set to dedicated mode (Set in PSL_SCNTL reg), the
812 * XSL and AFU are programmed to work with a single context.
813 * The context information should be configured in the SPA area
814 * index 0 (so PSL_SPAP must be configured before enabling the
815 * AFU).
816 */
817 afu->num_procs = 1;
818 if (afu->native->spa == NULL) {
819 if (cxl_alloc_spa(afu, CXL_MODE_DEDICATED))
820 return -ENOMEM;
821 }
822 attach_spa(afu);
823
824 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
825 cxl_p1n_write(afu, CXL_PSL_ID_An, CXL_PSL_ID_An_F | CXL_PSL_ID_An_L);
826
827 afu->current_mode = CXL_MODE_DEDICATED;
828
829 return cxl_chardev_d_afu_add(afu);
830}
831
Christophe Lombard64663f32017-04-07 16:11:57 +0200832int cxl_activate_dedicated_process_psl8(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100833{
834 dev_info(&afu->dev, "Activating dedicated process mode\n");
835
836 cxl_p1n_write(afu, CXL_PSL_SCNTL_An, CXL_PSL_SCNTL_An_PM_Process);
837
838 cxl_p1n_write(afu, CXL_PSL_CtxTime_An, 0); /* disable */
839 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0); /* disable */
840 cxl_p1n_write(afu, CXL_PSL_AMOR_An, 0xFFFFFFFFFFFFFFFFULL);
841 cxl_p1n_write(afu, CXL_PSL_LPID_An, mfspr(SPRN_LPID));
842 cxl_p1n_write(afu, CXL_HAURP_An, 0); /* disable */
843 cxl_p1n_write(afu, CXL_PSL_SDR_An, mfspr(SPRN_SDR1));
844
845 cxl_p2n_write(afu, CXL_CSRP_An, 0); /* disable */
846 cxl_p2n_write(afu, CXL_AURP0_An, 0); /* disable */
847 cxl_p2n_write(afu, CXL_AURP1_An, 0); /* disable */
848
849 afu->current_mode = CXL_MODE_DEDICATED;
850 afu->num_procs = 1;
851
852 return cxl_chardev_d_afu_add(afu);
853}
854
Christophe Lombardf24be422017-04-12 16:34:07 +0200855void cxl_update_dedicated_ivtes_psl9(struct cxl_context *ctx)
856{
857 int r;
858
859 for (r = 0; r < CXL_IRQ_RANGES; r++) {
860 ctx->elem->ivte_offsets[r] = cpu_to_be16(ctx->irqs.offset[r]);
861 ctx->elem->ivte_ranges[r] = cpu_to_be16(ctx->irqs.range[r]);
862 }
863}
864
Christophe Lombard64663f32017-04-07 16:11:57 +0200865void cxl_update_dedicated_ivtes_psl8(struct cxl_context *ctx)
Ian Munsie292841b2016-05-24 02:14:05 +1000866{
867 struct cxl_afu *afu = ctx->afu;
868
869 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An,
870 (((u64)ctx->irqs.offset[0] & 0xffff) << 48) |
871 (((u64)ctx->irqs.offset[1] & 0xffff) << 32) |
872 (((u64)ctx->irqs.offset[2] & 0xffff) << 16) |
873 ((u64)ctx->irqs.offset[3] & 0xffff));
874 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, (u64)
875 (((u64)ctx->irqs.range[0] & 0xffff) << 48) |
876 (((u64)ctx->irqs.range[1] & 0xffff) << 32) |
877 (((u64)ctx->irqs.range[2] & 0xffff) << 16) |
878 ((u64)ctx->irqs.range[3] & 0xffff));
879}
880
Christophe Lombardf24be422017-04-12 16:34:07 +0200881int cxl_attach_dedicated_process_psl9(struct cxl_context *ctx, u64 wed, u64 amr)
882{
883 struct cxl_afu *afu = ctx->afu;
884 int result;
885
886 /* fill the process element entry */
887 result = process_element_entry_psl9(ctx, wed, amr);
888 if (result)
889 return result;
890
891 if (ctx->afu->adapter->native->sl_ops->update_dedicated_ivtes)
892 afu->adapter->native->sl_ops->update_dedicated_ivtes(ctx);
893
894 result = cxl_ops->afu_reset(afu);
895 if (result)
896 return result;
897
898 return afu_enable(afu);
899}
900
Christophe Lombard64663f32017-04-07 16:11:57 +0200901int cxl_attach_dedicated_process_psl8(struct cxl_context *ctx, u64 wed, u64 amr)
Ian Munsief204e0b2014-10-08 19:55:02 +1100902{
903 struct cxl_afu *afu = ctx->afu;
Michael Neuling2f663522015-05-27 16:07:13 +1000904 u64 pid;
Ian Munsief204e0b2014-10-08 19:55:02 +1100905 int rc;
906
Michael Neuling2f663522015-05-27 16:07:13 +1000907 pid = (u64)current->pid << 32;
908 if (ctx->kernel)
909 pid = 0;
910 cxl_p2n_write(afu, CXL_PSL_PID_TID_An, pid);
911
912 cxl_p1n_write(afu, CXL_PSL_SR_An, calculate_sr(ctx));
Ian Munsief204e0b2014-10-08 19:55:02 +1100913
914 if ((rc = cxl_write_sstp(afu, ctx->sstp0, ctx->sstp1)))
915 return rc;
916
917 cxl_prefault(ctx, wed);
918
Christophe Lombardbdd2e712017-04-07 16:11:56 +0200919 if (ctx->afu->adapter->native->sl_ops->update_dedicated_ivtes)
920 afu->adapter->native->sl_ops->update_dedicated_ivtes(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100921
922 cxl_p2n_write(afu, CXL_PSL_AMR_An, amr);
923
924 /* master only context for dedicated */
Michael Neuling1a1a94b2015-05-27 16:07:10 +1000925 cxl_assign_psn_space(ctx);
Ian Munsief204e0b2014-10-08 19:55:02 +1100926
Frederic Barrat5be587b2016-03-04 12:26:28 +0100927 if ((rc = cxl_ops->afu_reset(afu)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100928 return rc;
929
930 cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
931
932 return afu_enable(afu);
933}
934
935static int deactivate_dedicated_process(struct cxl_afu *afu)
936{
937 dev_info(&afu->dev, "Deactivating dedicated process mode\n");
938
939 afu->current_mode = 0;
940 afu->num_procs = 0;
941
942 cxl_chardev_afu_remove(afu);
943
944 return 0;
945}
946
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100947static int native_afu_deactivate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100948{
949 if (mode == CXL_MODE_DIRECTED)
950 return deactivate_afu_directed(afu);
951 if (mode == CXL_MODE_DEDICATED)
952 return deactivate_dedicated_process(afu);
953 return 0;
954}
955
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100956static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
Ian Munsief204e0b2014-10-08 19:55:02 +1100957{
958 if (!mode)
959 return 0;
960 if (!(mode & afu->modes_supported))
961 return -EINVAL;
962
Christophe Lombard0d400f72016-03-04 12:26:41 +0100963 if (!cxl_ops->link_ok(afu->adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000964 WARN(1, "Device link is down, refusing to activate!\n");
965 return -EIO;
966 }
967
Ian Munsief204e0b2014-10-08 19:55:02 +1100968 if (mode == CXL_MODE_DIRECTED)
969 return activate_afu_directed(afu);
Christophe Lombardbdd2e712017-04-07 16:11:56 +0200970 if ((mode == CXL_MODE_DEDICATED) &&
971 (afu->adapter->native->sl_ops->activate_dedicated_process))
972 return afu->adapter->native->sl_ops->activate_dedicated_process(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100973
974 return -EINVAL;
975}
976
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100977static int native_attach_process(struct cxl_context *ctx, bool kernel,
978 u64 wed, u64 amr)
Ian Munsief204e0b2014-10-08 19:55:02 +1100979{
Christophe Lombard0d400f72016-03-04 12:26:41 +0100980 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000981 WARN(1, "Device link is down, refusing to attach process!\n");
982 return -EIO;
983 }
984
Ian Munsief204e0b2014-10-08 19:55:02 +1100985 ctx->kernel = kernel;
Christophe Lombardbdd2e712017-04-07 16:11:56 +0200986 if ((ctx->afu->current_mode == CXL_MODE_DIRECTED) &&
987 (ctx->afu->adapter->native->sl_ops->attach_afu_directed))
988 return ctx->afu->adapter->native->sl_ops->attach_afu_directed(ctx, wed, amr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100989
Christophe Lombardbdd2e712017-04-07 16:11:56 +0200990 if ((ctx->afu->current_mode == CXL_MODE_DEDICATED) &&
991 (ctx->afu->adapter->native->sl_ops->attach_dedicated_process))
992 return ctx->afu->adapter->native->sl_ops->attach_dedicated_process(ctx, wed, amr);
Ian Munsief204e0b2014-10-08 19:55:02 +1100993
994 return -EINVAL;
995}
996
997static inline int detach_process_native_dedicated(struct cxl_context *ctx)
998{
Ian Munsie5e7823c2016-07-01 02:50:40 +1000999 /*
1000 * The CAIA section 2.1.1 indicates that we need to do an AFU reset to
1001 * stop the AFU in dedicated mode (we therefore do not make that
1002 * optional like we do in the afu directed path). It does not indicate
1003 * that we need to do an explicit disable (which should occur
1004 * implicitly as part of the reset) or purge, but we do these as well
1005 * to be on the safe side.
1006 *
1007 * Notably we used to have some issues with the disable sequence
1008 * (before the sequence was spelled out in the architecture) which is
1009 * why we were so heavy weight in the first place, however a bug was
1010 * discovered that had rendered the disable operation ineffective, so
1011 * it is conceivable that was the sole explanation for those
1012 * difficulties. Point is, we should be careful and do some regression
1013 * testing if we ever attempt to remove any part of this procedure.
1014 */
Frederic Barrat5be587b2016-03-04 12:26:28 +01001015 cxl_ops->afu_reset(ctx->afu);
Ian Munsief204e0b2014-10-08 19:55:02 +11001016 cxl_afu_disable(ctx->afu);
1017 cxl_psl_purge(ctx->afu);
1018 return 0;
1019}
1020
Ian Munsie292841b2016-05-24 02:14:05 +10001021static void native_update_ivtes(struct cxl_context *ctx)
1022{
1023 if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
1024 return update_ivtes_directed(ctx);
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001025 if ((ctx->afu->current_mode == CXL_MODE_DEDICATED) &&
1026 (ctx->afu->adapter->native->sl_ops->update_dedicated_ivtes))
1027 return ctx->afu->adapter->native->sl_ops->update_dedicated_ivtes(ctx);
Ian Munsie292841b2016-05-24 02:14:05 +10001028 WARN(1, "native_update_ivtes: Bad mode\n");
1029}
1030
Ian Munsief204e0b2014-10-08 19:55:02 +11001031static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
1032{
1033 if (!ctx->pe_inserted)
1034 return 0;
1035 if (terminate_process_element(ctx))
1036 return -1;
1037 if (remove_process_element(ctx))
1038 return -1;
1039
1040 return 0;
1041}
1042
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001043static int native_detach_process(struct cxl_context *ctx)
Ian Munsief204e0b2014-10-08 19:55:02 +11001044{
Ian Munsie9bcf28c2015-01-09 20:34:36 +11001045 trace_cxl_detach(ctx);
1046
Ian Munsief204e0b2014-10-08 19:55:02 +11001047 if (ctx->afu->current_mode == CXL_MODE_DEDICATED)
1048 return detach_process_native_dedicated(ctx);
1049
1050 return detach_process_native_afu_directed(ctx);
1051}
1052
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001053static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
Ian Munsief204e0b2014-10-08 19:55:02 +11001054{
Daniel Axtens0b3f9c72015-08-14 17:41:18 +10001055 /* If the adapter has gone away, we can't get any meaningful
1056 * information.
1057 */
Christophe Lombard0d400f72016-03-04 12:26:41 +01001058 if (!cxl_ops->link_ok(afu->adapter, afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +10001059 return -EIO;
1060
Ian Munsiebc78b052014-11-14 17:37:50 +11001061 info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
1062 info->dar = cxl_p2n_read(afu, CXL_PSL_DAR_An);
Christophe Lombardabd1d992017-04-07 16:11:58 +02001063 if (cxl_is_power8())
1064 info->dsr = cxl_p2n_read(afu, CXL_PSL_DSR_An);
Ian Munsiebc78b052014-11-14 17:37:50 +11001065 info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
1066 info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
Christophe Lombard444c4ba2016-03-04 12:26:34 +01001067 info->proc_handle = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +11001068
1069 return 0;
1070}
1071
Christophe Lombardf24be422017-04-12 16:34:07 +02001072void cxl_native_irq_dump_regs_psl9(struct cxl_context *ctx)
1073{
1074 u64 fir1, fir2, serr;
1075
1076 fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL9_FIR1);
1077 fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL9_FIR2);
1078
1079 dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
1080 dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
1081 if (ctx->afu->adapter->native->sl_ops->register_serr_irq) {
1082 serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
1083 cxl_afu_decode_psl_serr(ctx->afu, serr);
1084 }
1085}
1086
Christophe Lombard64663f32017-04-07 16:11:57 +02001087void cxl_native_irq_dump_regs_psl8(struct cxl_context *ctx)
Frederic Barratd56d3012016-03-04 12:26:26 +01001088{
1089 u64 fir1, fir2, fir_slice, serr, afu_debug;
1090
1091 fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
1092 fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
1093 fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
Frederic Barratd56d3012016-03-04 12:26:26 +01001094 afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
1095
Frederic Barratd56d3012016-03-04 12:26:26 +01001096 dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
1097 dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
Frederic Barrat6d382612016-05-24 03:39:18 +10001098 if (ctx->afu->adapter->native->sl_ops->register_serr_irq) {
1099 serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
Philippe Bergheaud6e0c50f2016-07-05 13:08:06 +02001100 cxl_afu_decode_psl_serr(ctx->afu, serr);
Frederic Barrat6d382612016-05-24 03:39:18 +10001101 }
Frederic Barratd56d3012016-03-04 12:26:26 +01001102 dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
1103 dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
Frederic Barrat6d382612016-05-24 03:39:18 +10001104}
Frederic Barratd56d3012016-03-04 12:26:26 +01001105
Frederic Barrat6d382612016-05-24 03:39:18 +10001106static irqreturn_t native_handle_psl_slice_error(struct cxl_context *ctx,
1107 u64 dsisr, u64 errstat)
1108{
1109
1110 dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
1111
1112 if (ctx->afu->adapter->native->sl_ops->psl_irq_dump_registers)
1113 ctx->afu->adapter->native->sl_ops->psl_irq_dump_registers(ctx);
1114
1115 if (ctx->afu->adapter->native->sl_ops->debugfs_stop_trace) {
1116 dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
1117 ctx->afu->adapter->native->sl_ops->debugfs_stop_trace(ctx->afu->adapter);
1118 }
Frederic Barratd56d3012016-03-04 12:26:26 +01001119
Frederic Barrat5be587b2016-03-04 12:26:28 +01001120 return cxl_ops->ack_irq(ctx, 0, errstat);
Frederic Barratd56d3012016-03-04 12:26:26 +01001121}
1122
Christophe Lombardf24be422017-04-12 16:34:07 +02001123static bool cxl_is_translation_fault(struct cxl_afu *afu, u64 dsisr)
1124{
Christophe Lombard797625d2017-06-13 17:41:05 +02001125 if ((cxl_is_power8()) && (dsisr & CXL_PSL_DSISR_TRANS))
Christophe Lombardf24be422017-04-12 16:34:07 +02001126 return true;
1127
Christophe Lombard797625d2017-06-13 17:41:05 +02001128 if ((cxl_is_power9()) && (dsisr & CXL_PSL9_DSISR_An_TF))
Christophe Lombardf24be422017-04-12 16:34:07 +02001129 return true;
1130
1131 return false;
1132}
1133
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001134irqreturn_t cxl_fail_irq_psl(struct cxl_afu *afu, struct cxl_irq_info *irq_info)
Frederic Barratd56d3012016-03-04 12:26:26 +01001135{
Christophe Lombardf24be422017-04-12 16:34:07 +02001136 if (cxl_is_translation_fault(afu, irq_info->dsisr))
Frederic Barratd56d3012016-03-04 12:26:26 +01001137 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
1138 else
1139 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
1140
1141 return IRQ_HANDLED;
1142}
1143
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001144static irqreturn_t native_irq_multiplexed(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +01001145{
1146 struct cxl_afu *afu = data;
1147 struct cxl_context *ctx;
1148 struct cxl_irq_info irq_info;
Vaibhav Jainabf051b2016-11-16 19:39:33 +05301149 u64 phreg = cxl_p2n_read(afu, CXL_PSL_PEHandle_An);
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001150 int ph, ret = IRQ_HANDLED, res;
Frederic Barratd56d3012016-03-04 12:26:26 +01001151
Vaibhav Jainabf051b2016-11-16 19:39:33 +05301152 /* check if eeh kicked in while the interrupt was in flight */
1153 if (unlikely(phreg == ~0ULL)) {
1154 dev_warn(&afu->dev,
1155 "Ignoring slice interrupt(%d) due to fenced card",
1156 irq);
1157 return IRQ_HANDLED;
1158 }
1159 /* Mask the pe-handle from register value */
1160 ph = phreg & 0xffff;
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001161 if ((res = native_get_irq_info(afu, &irq_info))) {
1162 WARN(1, "Unable to get CXL IRQ Info: %i\n", res);
1163 if (afu->adapter->native->sl_ops->fail_irq)
1164 return afu->adapter->native->sl_ops->fail_irq(afu, &irq_info);
1165 return ret;
Frederic Barratd56d3012016-03-04 12:26:26 +01001166 }
1167
1168 rcu_read_lock();
1169 ctx = idr_find(&afu->contexts_idr, ph);
1170 if (ctx) {
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001171 if (afu->adapter->native->sl_ops->handle_interrupt)
1172 ret = afu->adapter->native->sl_ops->handle_interrupt(irq, ctx, &irq_info);
Frederic Barratd56d3012016-03-04 12:26:26 +01001173 rcu_read_unlock();
1174 return ret;
1175 }
1176 rcu_read_unlock();
1177
1178 WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx DAR"
1179 " %016llx\n(Possible AFU HW issue - was a term/remove acked"
1180 " with outstanding transactions?)\n", ph, irq_info.dsisr,
1181 irq_info.dar);
Christophe Lombardbdd2e712017-04-07 16:11:56 +02001182 if (afu->adapter->native->sl_ops->fail_irq)
1183 ret = afu->adapter->native->sl_ops->fail_irq(afu, &irq_info);
1184 return ret;
Frederic Barratd56d3012016-03-04 12:26:26 +01001185}
1186
Andrew Donnellan6fd40f12016-07-22 19:01:36 +10001187static void native_irq_wait(struct cxl_context *ctx)
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001188{
1189 u64 dsisr;
1190 int timeout = 1000;
1191 int ph;
1192
1193 /*
1194 * Wait until no further interrupts are presented by the PSL
1195 * for this context.
1196 */
1197 while (timeout--) {
1198 ph = cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) & 0xffff;
1199 if (ph != ctx->pe)
1200 return;
1201 dsisr = cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An);
Christophe Lombard797625d2017-06-13 17:41:05 +02001202 if (cxl_is_power8() &&
Christophe Lombardabd1d992017-04-07 16:11:58 +02001203 ((dsisr & CXL_PSL_DSISR_PENDING) == 0))
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001204 return;
Christophe Lombard797625d2017-06-13 17:41:05 +02001205 if (cxl_is_power9() &&
Christophe Lombardf24be422017-04-12 16:34:07 +02001206 ((dsisr & CXL_PSL9_DSISR_PENDING) == 0))
1207 return;
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001208 /*
1209 * We are waiting for the workqueue to process our
1210 * irq, so need to let that run here.
1211 */
1212 msleep(1);
1213 }
1214
1215 dev_warn(&ctx->afu->dev, "WARNING: waiting on DSI for PE %i"
1216 " DSISR %016llx!\n", ph, dsisr);
1217 return;
1218}
1219
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001220static irqreturn_t native_slice_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +01001221{
1222 struct cxl_afu *afu = data;
Christophe Lombardabd1d992017-04-07 16:11:58 +02001223 u64 errstat, serr, afu_error, dsisr;
Alastair D'Silvaa7156262017-05-01 10:53:31 +10001224 u64 fir_slice, afu_debug, irq_mask;
Frederic Barratd56d3012016-03-04 12:26:26 +01001225
Frederic Barrat6d382612016-05-24 03:39:18 +10001226 /*
1227 * slice err interrupt is only used with full PSL (no XSL)
1228 */
Frederic Barratd56d3012016-03-04 12:26:26 +01001229 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
Frederic Barratd56d3012016-03-04 12:26:26 +01001230 errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
Philippe Bergheaud6e0c50f2016-07-05 13:08:06 +02001231 afu_error = cxl_p2n_read(afu, CXL_AFU_ERR_An);
1232 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
1233 cxl_afu_decode_psl_serr(afu, serr);
Christophe Lombardabd1d992017-04-07 16:11:58 +02001234
1235 if (cxl_is_power8()) {
1236 fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
1237 afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
1238 dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
1239 dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
1240 }
Frederic Barratd56d3012016-03-04 12:26:26 +01001241 dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
Philippe Bergheaud6e0c50f2016-07-05 13:08:06 +02001242 dev_crit(&afu->dev, "AFU_ERR_An: 0x%.16llx\n", afu_error);
1243 dev_crit(&afu->dev, "PSL_DSISR_An: 0x%.16llx\n", dsisr);
Frederic Barratd56d3012016-03-04 12:26:26 +01001244
Alastair D'Silvaa7156262017-05-01 10:53:31 +10001245 /* mask off the IRQ so it won't retrigger until the AFU is reset */
1246 irq_mask = (serr & CXL_PSL_SERR_An_IRQS) >> 32;
1247 serr |= irq_mask;
Frederic Barratd56d3012016-03-04 12:26:26 +01001248 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
Alastair D'Silvaa7156262017-05-01 10:53:31 +10001249 dev_info(&afu->dev, "Further such interrupts will be masked until the AFU is reset\n");
Frederic Barratd56d3012016-03-04 12:26:26 +01001250
1251 return IRQ_HANDLED;
1252}
1253
Frederic Barrat6d382612016-05-24 03:39:18 +10001254void cxl_native_err_irq_dump_regs(struct cxl *adapter)
1255{
1256 u64 fir1, fir2;
1257
1258 fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
1259 fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
1260
1261 dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1, fir2);
1262}
1263
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001264static irqreturn_t native_irq_err(int irq, void *data)
Frederic Barratd56d3012016-03-04 12:26:26 +01001265{
1266 struct cxl *adapter = data;
Frederic Barrat6d382612016-05-24 03:39:18 +10001267 u64 err_ivte;
Frederic Barratd56d3012016-03-04 12:26:26 +01001268
1269 WARN(1, "CXL ERROR interrupt %i\n", irq);
1270
1271 err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
1272 dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
1273
Frederic Barrat6d382612016-05-24 03:39:18 +10001274 if (adapter->native->sl_ops->debugfs_stop_trace) {
1275 dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
1276 adapter->native->sl_ops->debugfs_stop_trace(adapter);
1277 }
Frederic Barratd56d3012016-03-04 12:26:26 +01001278
Frederic Barrat6d382612016-05-24 03:39:18 +10001279 if (adapter->native->sl_ops->err_irq_dump_registers)
1280 adapter->native->sl_ops->err_irq_dump_registers(adapter);
Frederic Barratd56d3012016-03-04 12:26:26 +01001281
1282 return IRQ_HANDLED;
1283}
1284
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001285int cxl_native_register_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +01001286{
1287 int rc;
1288
1289 adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
1290 dev_name(&adapter->dev));
1291 if (!adapter->irq_name)
1292 return -ENOMEM;
1293
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001294 if ((rc = cxl_register_one_irq(adapter, native_irq_err, adapter,
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001295 &adapter->native->err_hwirq,
1296 &adapter->native->err_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +01001297 adapter->irq_name))) {
1298 kfree(adapter->irq_name);
1299 adapter->irq_name = NULL;
1300 return rc;
1301 }
1302
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001303 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->native->err_hwirq & 0xffff);
Frederic Barratd56d3012016-03-04 12:26:26 +01001304
1305 return 0;
1306}
1307
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001308void cxl_native_release_psl_err_irq(struct cxl *adapter)
Frederic Barratd56d3012016-03-04 12:26:26 +01001309{
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301310 if (adapter->native->err_virq == 0 ||
1311 adapter->native->err_virq !=
1312 irq_find_mapping(NULL, adapter->native->err_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +01001313 return;
1314
1315 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001316 cxl_unmap_irq(adapter->native->err_virq, adapter);
1317 cxl_ops->release_one_irq(adapter, adapter->native->err_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +01001318 kfree(adapter->irq_name);
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301319 adapter->native->err_virq = 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001320}
1321
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001322int cxl_native_register_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +01001323{
1324 u64 serr;
1325 int rc;
1326
1327 afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
1328 dev_name(&afu->dev));
1329 if (!afu->err_irq_name)
1330 return -ENOMEM;
1331
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001332 if ((rc = cxl_register_one_irq(afu->adapter, native_slice_irq_err, afu,
Frederic Barratd56d3012016-03-04 12:26:26 +01001333 &afu->serr_hwirq,
1334 &afu->serr_virq, afu->err_irq_name))) {
1335 kfree(afu->err_irq_name);
1336 afu->err_irq_name = NULL;
1337 return rc;
1338 }
1339
1340 serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
Christophe Lombardabd1d992017-04-07 16:11:58 +02001341 if (cxl_is_power8())
1342 serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
Christophe Lombardf24be422017-04-12 16:34:07 +02001343 if (cxl_is_power9()) {
1344 /*
1345 * By default, all errors are masked. So don't set all masks.
1346 * Slice errors will be transfered.
1347 */
1348 serr = (serr & ~0xff0000007fffffffULL) | (afu->serr_hwirq & 0xffff);
1349 }
Frederic Barratd56d3012016-03-04 12:26:26 +01001350 cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
1351
1352 return 0;
1353}
1354
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001355void cxl_native_release_serr_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +01001356{
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301357 if (afu->serr_virq == 0 ||
1358 afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +01001359 return;
1360
1361 cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
1362 cxl_unmap_irq(afu->serr_virq, afu);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001363 cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +01001364 kfree(afu->err_irq_name);
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301365 afu->serr_virq = 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001366}
1367
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001368int cxl_native_register_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +01001369{
1370 int rc;
1371
1372 afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
1373 dev_name(&afu->dev));
1374 if (!afu->psl_irq_name)
1375 return -ENOMEM;
1376
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001377 if ((rc = cxl_register_one_irq(afu->adapter, native_irq_multiplexed,
1378 afu, &afu->native->psl_hwirq, &afu->native->psl_virq,
Frederic Barratd56d3012016-03-04 12:26:26 +01001379 afu->psl_irq_name))) {
1380 kfree(afu->psl_irq_name);
1381 afu->psl_irq_name = NULL;
1382 }
1383 return rc;
1384}
1385
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001386void cxl_native_release_psl_irq(struct cxl_afu *afu)
Frederic Barratd56d3012016-03-04 12:26:26 +01001387{
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301388 if (afu->native->psl_virq == 0 ||
1389 afu->native->psl_virq !=
1390 irq_find_mapping(NULL, afu->native->psl_hwirq))
Frederic Barratd56d3012016-03-04 12:26:26 +01001391 return;
1392
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001393 cxl_unmap_irq(afu->native->psl_virq, afu);
1394 cxl_ops->release_one_irq(afu->adapter, afu->native->psl_hwirq);
Frederic Barratd56d3012016-03-04 12:26:26 +01001395 kfree(afu->psl_irq_name);
Vaibhav Jainb3aa20b2017-06-02 22:26:48 +05301396 afu->native->psl_virq = 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001397}
1398
Ian Munsief204e0b2014-10-08 19:55:02 +11001399static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
1400{
1401 u64 dsisr;
1402
Rasmus Villemoesde369532015-06-11 13:27:52 +02001403 pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat);
Ian Munsief204e0b2014-10-08 19:55:02 +11001404
1405 /* Clear PSL_DSISR[PE] */
1406 dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
1407 cxl_p2n_write(afu, CXL_PSL_DSISR_An, dsisr & ~CXL_PSL_DSISR_An_PE);
1408
1409 /* Write 1s to clear error status bits */
1410 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
1411}
1412
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001413static int native_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
Ian Munsief204e0b2014-10-08 19:55:02 +11001414{
Ian Munsie9bcf28c2015-01-09 20:34:36 +11001415 trace_cxl_psl_irq_ack(ctx, tfc);
Ian Munsief204e0b2014-10-08 19:55:02 +11001416 if (tfc)
1417 cxl_p2n_write(ctx->afu, CXL_PSL_TFC_An, tfc);
1418 if (psl_reset_mask)
1419 recover_psl_err(ctx->afu, psl_reset_mask);
1420
1421 return 0;
1422}
1423
1424int cxl_check_error(struct cxl_afu *afu)
1425{
1426 return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
1427}
Frederic Barratd56d3012016-03-04 12:26:26 +01001428
Christophe Lombard47528762016-03-04 12:26:37 +01001429static bool native_support_attributes(const char *attr_name,
1430 enum cxl_attrs type)
1431{
1432 return true;
1433}
1434
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001435static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001436{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001437 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001438 return -EIO;
1439 if (unlikely(off >= afu->crs_len))
1440 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001441 *out = in_le64(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001442 (cr * afu->crs_len) + off);
1443 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001444}
1445
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001446static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001447{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001448 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barrat5be587b2016-03-04 12:26:28 +01001449 return -EIO;
1450 if (unlikely(off >= afu->crs_len))
1451 return -ERANGE;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001452 *out = in_le32(afu->native->afu_desc_mmio + afu->crs_offset +
Frederic Barrat5be587b2016-03-04 12:26:28 +01001453 (cr * afu->crs_len) + off);
1454 return 0;
Frederic Barratd56d3012016-03-04 12:26:26 +01001455}
1456
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001457static int native_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001458{
1459 u64 aligned_off = off & ~0x3L;
1460 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001461 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001462
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001463 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001464 if (!rc)
1465 *out = (val >> ((off & 0x3) * 8)) & 0xffff;
1466 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001467}
1468
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001469static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
Frederic Barratd56d3012016-03-04 12:26:26 +01001470{
1471 u64 aligned_off = off & ~0x3L;
1472 u32 val;
Frederic Barrat5be587b2016-03-04 12:26:28 +01001473 int rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001474
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001475 rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001476 if (!rc)
1477 *out = (val >> ((off & 0x3) * 8)) & 0xff;
1478 return rc;
Frederic Barratd56d3012016-03-04 12:26:26 +01001479}
Frederic Barrat5be587b2016-03-04 12:26:28 +01001480
Frederic Barratd601ea92016-03-04 12:26:40 +01001481static int native_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
1482{
Christophe Lombard0d400f72016-03-04 12:26:41 +01001483 if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
Frederic Barratd601ea92016-03-04 12:26:40 +01001484 return -EIO;
1485 if (unlikely(off >= afu->crs_len))
1486 return -ERANGE;
1487 out_le32(afu->native->afu_desc_mmio + afu->crs_offset +
1488 (cr * afu->crs_len) + off, in);
1489 return 0;
1490}
1491
1492static int native_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
1493{
1494 u64 aligned_off = off & ~0x3L;
1495 u32 val32, mask, shift;
1496 int rc;
1497
1498 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1499 if (rc)
1500 return rc;
1501 shift = (off & 0x3) * 8;
1502 WARN_ON(shift == 24);
1503 mask = 0xffff << shift;
1504 val32 = (val32 & ~mask) | (in << shift);
1505
1506 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1507 return rc;
1508}
1509
1510static int native_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
1511{
1512 u64 aligned_off = off & ~0x3L;
1513 u32 val32, mask, shift;
1514 int rc;
1515
1516 rc = native_afu_cr_read32(afu, cr, aligned_off, &val32);
1517 if (rc)
1518 return rc;
1519 shift = (off & 0x3) * 8;
1520 mask = 0xff << shift;
1521 val32 = (val32 & ~mask) | (in << shift);
1522
1523 rc = native_afu_cr_write32(afu, cr, aligned_off, val32);
1524 return rc;
1525}
1526
Frederic Barrat5be587b2016-03-04 12:26:28 +01001527const struct cxl_backend_ops cxl_native_ops = {
1528 .module = THIS_MODULE,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001529 .adapter_reset = cxl_pci_reset,
1530 .alloc_one_irq = cxl_pci_alloc_one_irq,
1531 .release_one_irq = cxl_pci_release_one_irq,
1532 .alloc_irq_ranges = cxl_pci_alloc_irq_ranges,
1533 .release_irq_ranges = cxl_pci_release_irq_ranges,
1534 .setup_irq = cxl_pci_setup_irq,
1535 .handle_psl_slice_error = native_handle_psl_slice_error,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001536 .psl_interrupt = NULL,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001537 .ack_irq = native_ack_irq,
Michael Neuling2bc79ff2016-04-22 14:57:49 +10001538 .irq_wait = native_irq_wait,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001539 .attach_process = native_attach_process,
1540 .detach_process = native_detach_process,
Ian Munsie292841b2016-05-24 02:14:05 +10001541 .update_ivtes = native_update_ivtes,
Christophe Lombard47528762016-03-04 12:26:37 +01001542 .support_attributes = native_support_attributes,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001543 .link_ok = cxl_adapter_link_ok,
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001544 .release_afu = cxl_pci_release_afu,
1545 .afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
1546 .afu_check_and_enable = native_afu_check_and_enable,
1547 .afu_activate_mode = native_afu_activate_mode,
1548 .afu_deactivate_mode = native_afu_deactivate_mode,
1549 .afu_reset = native_afu_reset,
1550 .afu_cr_read8 = native_afu_cr_read8,
1551 .afu_cr_read16 = native_afu_cr_read16,
1552 .afu_cr_read32 = native_afu_cr_read32,
1553 .afu_cr_read64 = native_afu_cr_read64,
Frederic Barratd601ea92016-03-04 12:26:40 +01001554 .afu_cr_write8 = native_afu_cr_write8,
1555 .afu_cr_write16 = native_afu_cr_write16,
1556 .afu_cr_write32 = native_afu_cr_write32,
1557 .read_adapter_vpd = cxl_pci_read_adapter_vpd,
Frederic Barrat5be587b2016-03-04 12:26:28 +01001558};