2 * Copyright 2014 IBM Corp.
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.
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>
16 #include <linux/uaccess.h>
17 #include <linux/delay.h>
18 #include <asm/synch.h>
19 #include <misc/cxl-base.h>
24 static int afu_control(struct cxl_afu
*afu
, u64 command
,
25 u64 result
, u64 mask
, bool enabled
)
27 u64 AFU_Cntl
= cxl_p2n_read(afu
, CXL_AFU_Cntl_An
);
28 unsigned long timeout
= jiffies
+ (HZ
* CXL_TIMEOUT
);
31 spin_lock(&afu
->afu_cntl_lock
);
32 pr_devel("AFU command starting: %llx\n", command
);
34 trace_cxl_afu_ctrl(afu
, command
);
36 cxl_p2n_write(afu
, CXL_AFU_Cntl_An
, AFU_Cntl
| command
);
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");
46 if (!cxl_ops
->link_ok(afu
->adapter
, afu
)) {
47 afu
->enabled
= enabled
;
52 pr_devel_ratelimited("AFU control... (0x%016llx)\n",
55 AFU_Cntl
= cxl_p2n_read(afu
, CXL_AFU_Cntl_An
);
57 pr_devel("AFU command complete: %llx\n", command
);
58 afu
->enabled
= enabled
;
60 trace_cxl_afu_ctrl_done(afu
, command
, rc
);
61 spin_unlock(&afu
->afu_cntl_lock
);
66 static int afu_enable(struct cxl_afu
*afu
)
68 pr_devel("AFU enable request\n");
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);
75 int cxl_afu_disable(struct cxl_afu
*afu
)
77 pr_devel("AFU disable request\n");
79 return afu_control(afu
, 0, CXL_AFU_Cntl_An_ES_Disabled
,
80 CXL_AFU_Cntl_An_ES_MASK
, false);
83 /* This will disable as well as reset */
84 static int native_afu_reset(struct cxl_afu
*afu
)
86 pr_devel("AFU reset request\n");
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
,
94 static int native_afu_check_and_enable(struct cxl_afu
*afu
)
96 if (!cxl_ops
->link_ok(afu
->adapter
, afu
)) {
97 WARN(1, "Refusing to enable afu while link down!\n");
102 return afu_enable(afu
);
105 int cxl_psl_purge(struct cxl_afu
*afu
)
107 u64 PSL_CNTL
= cxl_p1n_read(afu
, CXL_PSL_SCNTL_An
);
108 u64 AFU_Cntl
= cxl_p2n_read(afu
, CXL_AFU_Cntl_An
);
111 unsigned long timeout
= jiffies
+ (HZ
* CXL_TIMEOUT
);
114 trace_cxl_psl_ctrl(afu
, CXL_PSL_SCNTL_An_Pc
);
116 pr_devel("PSL purge request\n");
118 if (!cxl_ops
->link_ok(afu
->adapter
, afu
)) {
119 dev_warn(&afu
->dev
, "PSL Purge called with link down, ignoring\n");
124 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
);
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");
140 if (!cxl_ops
->link_ok(afu
->adapter
, afu
)) {
145 dsisr
= cxl_p2n_read(afu
, CXL_PSL_DSISR_An
);
146 pr_devel_ratelimited("PSL purging... PSL_CNTL: 0x%016llx PSL_DSISR: 0x%016llx\n", PSL_CNTL
, dsisr
);
147 if (dsisr
& CXL_PSL_DSISR_TRANS
) {
148 dar
= cxl_p2n_read(afu
, CXL_PSL_DAR_An
);
149 dev_notice(&afu
->dev
, "PSL purge terminating pending translation, DSISR: 0x%016llx, DAR: 0x%016llx\n", dsisr
, dar
);
150 cxl_p2n_write(afu
, CXL_PSL_TFC_An
, CXL_PSL_TFC_An_AE
);
152 dev_notice(&afu
->dev
, "PSL purge acknowledging pending non-translation fault, DSISR: 0x%016llx\n", dsisr
);
153 cxl_p2n_write(afu
, CXL_PSL_TFC_An
, CXL_PSL_TFC_An_A
);
157 PSL_CNTL
= cxl_p1n_read(afu
, CXL_PSL_SCNTL_An
);
160 pr_devel("PSL purged in %lld ns\n", end
- start
);
162 cxl_p1n_write(afu
, CXL_PSL_SCNTL_An
,
163 PSL_CNTL
& ~CXL_PSL_SCNTL_An_Pc
);
165 trace_cxl_psl_ctrl_done(afu
, CXL_PSL_SCNTL_An_Pc
, rc
);
169 static int spa_max_procs(int spa_size
)
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
178 * end_of_PSL_queue_area = SPA_Base + ((n+4) * 128) + (n*8) - 1
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:
184 return ((spa_size
/ 8) - 96) / 17;
187 int cxl_alloc_spa(struct cxl_afu
*afu
)
189 /* Work out how many pages to allocate */
190 afu
->native
->spa_order
= 0;
192 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
);
197 WARN_ON(afu
->native
->spa_size
> 0x100000); /* Max size supported by the hardware */
199 if (!(afu
->native
->spa
= (struct cxl_process_element
*)
200 __get_free_pages(GFP_KERNEL
| __GFP_ZERO
, afu
->native
->spa_order
))) {
201 pr_err("cxl_alloc_spa: Unable to allocate scheduled process area\n");
204 pr_devel("spa pages: %i afu->spa_max_procs: %i afu->num_procs: %i\n",
205 1<<afu
->native
->spa_order
, afu
->native
->spa_max_procs
, afu
->num_procs
);
210 static void attach_spa(struct cxl_afu
*afu
)
214 afu
->native
->sw_command_status
= (__be64
*)((char *)afu
->native
->spa
+
215 ((afu
->native
->spa_max_procs
+ 3) * 128));
217 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
;
219 spap
|= CXL_PSL_SPAP_V
;
220 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
);
223 cxl_p1n_write(afu
, CXL_PSL_SPAP_An
, spap
);
226 static inline void detach_spa(struct cxl_afu
*afu
)
228 cxl_p1n_write(afu
, CXL_PSL_SPAP_An
, 0);
231 void cxl_release_spa(struct cxl_afu
*afu
)
233 if (afu
->native
->spa
) {
234 free_pages((unsigned long) afu
->native
->spa
,
235 afu
->native
->spa_order
);
236 afu
->native
->spa
= NULL
;
240 int cxl_tlb_slb_invalidate(struct cxl
*adapter
)
242 unsigned long timeout
= jiffies
+ (HZ
* CXL_TIMEOUT
);
244 pr_devel("CXL adapter wide TLBIA & SLBIA\n");
246 cxl_p1_write(adapter
, CXL_PSL_AFUSEL
, CXL_PSL_AFUSEL_A
);
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");
254 if (!cxl_ops
->link_ok(adapter
, NULL
))
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");
265 if (!cxl_ops
->link_ok(adapter
, NULL
))
272 static int cxl_write_sstp(struct cxl_afu
*afu
, u64 sstp0
, u64 sstp1
)
276 /* 1. Disable SSTP by writing 0 to SSTP1[V] */
277 cxl_p2n_write(afu
, CXL_SSTP1_An
, 0);
279 /* 2. Invalidate all SLB entries */
280 if ((rc
= cxl_afu_slbia(afu
)))
283 /* 3. Set SSTP0_An */
284 cxl_p2n_write(afu
, CXL_SSTP0_An
, sstp0
);
286 /* 4. Set SSTP1_An */
287 cxl_p2n_write(afu
, CXL_SSTP1_An
, sstp1
);
292 /* Using per slice version may improve performance here. (ie. SLBIA_An) */
293 static void slb_invalid(struct cxl_context
*ctx
)
295 struct cxl
*adapter
= ctx
->afu
->adapter
;
298 WARN_ON(!mutex_is_locked(&ctx
->afu
->native
->spa_mutex
));
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
);
306 if (!cxl_ops
->link_ok(adapter
, NULL
))
308 slbia
= cxl_p1_read(adapter
, CXL_PSL_SLBIA
);
309 if (!(slbia
& CXL_TLB_SLB_P
))
315 static int do_process_element_cmd(struct cxl_context
*ctx
,
316 u64 cmd
, u64 pe_state
)
319 unsigned long timeout
= jiffies
+ (HZ
* CXL_TIMEOUT
);
322 trace_cxl_llcmd(ctx
, cmd
);
324 WARN_ON(!ctx
->afu
->enabled
);
326 ctx
->elem
->software_state
= cpu_to_be32(pe_state
);
328 *(ctx
->afu
->native
->sw_command_status
) = cpu_to_be64(cmd
| 0 | ctx
->pe
);
330 cxl_p1n_write(ctx
->afu
, CXL_PSL_LLCMD_An
, cmd
| ctx
->pe
);
332 if (time_after_eq(jiffies
, timeout
)) {
333 dev_warn(&ctx
->afu
->dev
, "WARNING: Process Element Command timed out!\n");
337 if (!cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
)) {
338 dev_warn(&ctx
->afu
->dev
, "WARNING: Device link down, aborting Process Element Command!\n");
342 state
= be64_to_cpup(ctx
->afu
->native
->sw_command_status
);
343 if (state
== ~0ULL) {
344 pr_err("cxl: Error adding process element to AFU\n");
348 if ((state
& (CXL_SPA_SW_CMD_MASK
| CXL_SPA_SW_STATE_MASK
| CXL_SPA_SW_LINK_MASK
)) ==
349 (cmd
| (cmd
>> 16) | ctx
->pe
))
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
362 trace_cxl_llcmd_done(ctx
, cmd
, rc
);
366 static int add_process_element(struct cxl_context
*ctx
)
370 mutex_lock(&ctx
->afu
->native
->spa_mutex
);
371 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
);
375 mutex_unlock(&ctx
->afu
->native
->spa_mutex
);
379 static int terminate_process_element(struct cxl_context
*ctx
)
383 /* fast path terminate if it's already invalid */
384 if (!(ctx
->elem
->software_state
& cpu_to_be32(CXL_PE_SOFTWARE_STATE_V
)))
387 mutex_lock(&ctx
->afu
->native
->spa_mutex
);
388 pr_devel("%s Terminate pe: %i started\n", __func__
, ctx
->pe
);
389 /* 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.
393 if (cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
))
394 rc
= do_process_element_cmd(ctx
, CXL_SPA_SW_CMD_TERMINATE
,
395 CXL_PE_SOFTWARE_STATE_V
| CXL_PE_SOFTWARE_STATE_T
);
396 ctx
->elem
->software_state
= 0; /* Remove Valid bit */
397 pr_devel("%s Terminate pe: %i finished\n", __func__
, ctx
->pe
);
398 mutex_unlock(&ctx
->afu
->native
->spa_mutex
);
402 static int remove_process_element(struct cxl_context
*ctx
)
406 mutex_lock(&ctx
->afu
->native
->spa_mutex
);
407 pr_devel("%s Remove pe: %i started\n", __func__
, ctx
->pe
);
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.
412 if (cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
))
413 rc
= do_process_element_cmd(ctx
, CXL_SPA_SW_CMD_REMOVE
, 0);
416 ctx
->pe_inserted
= false;
418 pr_devel("%s Remove pe: %i finished\n", __func__
, ctx
->pe
);
419 mutex_unlock(&ctx
->afu
->native
->spa_mutex
);
425 void cxl_assign_psn_space(struct cxl_context
*ctx
)
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
;
431 ctx
->psn_phys
= ctx
->afu
->psn_phys
+
432 (ctx
->afu
->native
->pp_offset
+ ctx
->afu
->pp_size
* ctx
->pe
);
433 ctx
->psn_size
= ctx
->afu
->pp_size
;
437 static int activate_afu_directed(struct cxl_afu
*afu
)
441 dev_info(&afu
->dev
, "Activating AFU directed mode\n");
443 afu
->num_procs
= afu
->max_procs_virtualised
;
444 if (afu
->native
->spa
== NULL
) {
445 if (cxl_alloc_spa(afu
))
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
);
454 afu
->current_mode
= CXL_MODE_DIRECTED
;
456 if ((rc
= cxl_chardev_m_afu_add(afu
)))
459 if ((rc
= cxl_sysfs_afu_m_add(afu
)))
462 if ((rc
= cxl_chardev_s_afu_add(afu
)))
467 cxl_sysfs_afu_m_remove(afu
);
469 cxl_chardev_afu_remove(afu
);
473 #ifdef CONFIG_CPU_LITTLE_ENDIAN
474 #define set_endian(sr) ((sr) |= CXL_PSL_SR_An_LE)
476 #define set_endian(sr) ((sr) &= ~(CXL_PSL_SR_An_LE))
479 static u64
calculate_sr(struct cxl_context
*ctx
)
485 sr
|= CXL_PSL_SR_An_MP
;
486 if (mfspr(SPRN_LPCR
) & LPCR_TC
)
487 sr
|= CXL_PSL_SR_An_TC
;
489 sr
|= CXL_PSL_SR_An_R
| (mfmsr() & MSR_SF
);
490 sr
|= CXL_PSL_SR_An_HV
;
492 sr
|= CXL_PSL_SR_An_PR
| CXL_PSL_SR_An_R
;
493 sr
&= ~(CXL_PSL_SR_An_HV
);
494 if (!test_tsk_thread_flag(current
, TIF_32BIT
))
495 sr
|= CXL_PSL_SR_An_SF
;
500 static int attach_afu_directed(struct cxl_context
*ctx
, u64 wed
, u64 amr
)
505 cxl_assign_psn_space(ctx
);
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
));
515 ctx
->elem
->common
.tid
= 0;
516 ctx
->elem
->common
.pid
= cpu_to_be32(pid
);
518 ctx
->elem
->sr
= cpu_to_be64(calculate_sr(ctx
));
520 ctx
->elem
->common
.csrp
= 0; /* disable */
521 ctx
->elem
->common
.aurp0
= 0; /* disable */
522 ctx
->elem
->common
.aurp1
= 0; /* disable */
524 cxl_prefault(ctx
, wed
);
526 ctx
->elem
->common
.sstp0
= cpu_to_be64(ctx
->sstp0
);
527 ctx
->elem
->common
.sstp1
= cpu_to_be64(ctx
->sstp1
);
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
]);
534 ctx
->elem
->common
.amr
= cpu_to_be64(amr
);
535 ctx
->elem
->common
.wed
= cpu_to_be64(wed
);
537 /* first guy needs to enable */
538 if ((result
= cxl_ops
->afu_check_and_enable(ctx
->afu
)))
541 return add_process_element(ctx
);
544 static int deactivate_afu_directed(struct cxl_afu
*afu
)
546 dev_info(&afu
->dev
, "Deactivating AFU directed mode\n");
548 afu
->current_mode
= 0;
551 cxl_sysfs_afu_m_remove(afu
);
552 cxl_chardev_afu_remove(afu
);
554 cxl_ops
->afu_reset(afu
);
555 cxl_afu_disable(afu
);
561 static int activate_dedicated_process(struct cxl_afu
*afu
)
563 dev_info(&afu
->dev
, "Activating dedicated process mode\n");
565 cxl_p1n_write(afu
, CXL_PSL_SCNTL_An
, CXL_PSL_SCNTL_An_PM_Process
);
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
));
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 */
578 afu
->current_mode
= CXL_MODE_DEDICATED
;
581 return cxl_chardev_d_afu_add(afu
);
584 static int attach_dedicated(struct cxl_context
*ctx
, u64 wed
, u64 amr
)
586 struct cxl_afu
*afu
= ctx
->afu
;
590 pid
= (u64
)current
->pid
<< 32;
593 cxl_p2n_write(afu
, CXL_PSL_PID_TID_An
, pid
);
595 cxl_p1n_write(afu
, CXL_PSL_SR_An
, calculate_sr(ctx
));
597 if ((rc
= cxl_write_sstp(afu
, ctx
->sstp0
, ctx
->sstp1
)))
600 cxl_prefault(ctx
, wed
);
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));
613 cxl_p2n_write(afu
, CXL_PSL_AMR_An
, amr
);
615 /* master only context for dedicated */
616 cxl_assign_psn_space(ctx
);
618 if ((rc
= cxl_ops
->afu_reset(afu
)))
621 cxl_p2n_write(afu
, CXL_PSL_WED_An
, wed
);
623 return afu_enable(afu
);
626 static int deactivate_dedicated_process(struct cxl_afu
*afu
)
628 dev_info(&afu
->dev
, "Deactivating dedicated process mode\n");
630 afu
->current_mode
= 0;
633 cxl_chardev_afu_remove(afu
);
638 static int native_afu_deactivate_mode(struct cxl_afu
*afu
, int mode
)
640 if (mode
== CXL_MODE_DIRECTED
)
641 return deactivate_afu_directed(afu
);
642 if (mode
== CXL_MODE_DEDICATED
)
643 return deactivate_dedicated_process(afu
);
647 static int native_afu_activate_mode(struct cxl_afu
*afu
, int mode
)
651 if (!(mode
& afu
->modes_supported
))
654 if (!cxl_ops
->link_ok(afu
->adapter
, afu
)) {
655 WARN(1, "Device link is down, refusing to activate!\n");
659 if (mode
== CXL_MODE_DIRECTED
)
660 return activate_afu_directed(afu
);
661 if (mode
== CXL_MODE_DEDICATED
)
662 return activate_dedicated_process(afu
);
667 static int native_attach_process(struct cxl_context
*ctx
, bool kernel
,
670 if (!cxl_ops
->link_ok(ctx
->afu
->adapter
, ctx
->afu
)) {
671 WARN(1, "Device link is down, refusing to attach process!\n");
675 ctx
->kernel
= kernel
;
676 if (ctx
->afu
->current_mode
== CXL_MODE_DIRECTED
)
677 return attach_afu_directed(ctx
, wed
, amr
);
679 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
)
680 return attach_dedicated(ctx
, wed
, amr
);
685 static inline int detach_process_native_dedicated(struct cxl_context
*ctx
)
687 cxl_ops
->afu_reset(ctx
->afu
);
688 cxl_afu_disable(ctx
->afu
);
689 cxl_psl_purge(ctx
->afu
);
693 static inline int detach_process_native_afu_directed(struct cxl_context
*ctx
)
695 if (!ctx
->pe_inserted
)
697 if (terminate_process_element(ctx
))
699 if (remove_process_element(ctx
))
705 static int native_detach_process(struct cxl_context
*ctx
)
707 trace_cxl_detach(ctx
);
709 if (ctx
->afu
->current_mode
== CXL_MODE_DEDICATED
)
710 return detach_process_native_dedicated(ctx
);
712 return detach_process_native_afu_directed(ctx
);
715 static int native_get_irq_info(struct cxl_afu
*afu
, struct cxl_irq_info
*info
)
719 /* If the adapter has gone away, we can't get any meaningful
722 if (!cxl_ops
->link_ok(afu
->adapter
, afu
))
725 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
);
729 info
->pid
= pidtid
>> 32;
730 info
->tid
= pidtid
& 0xffffffff;
731 info
->afu_err
= cxl_p2n_read(afu
, CXL_AFU_ERR_An
);
732 info
->errstat
= cxl_p2n_read(afu
, CXL_PSL_ErrStat_An
);
733 info
->proc_handle
= 0;
738 static irqreturn_t
native_handle_psl_slice_error(struct cxl_context
*ctx
,
739 u64 dsisr
, u64 errstat
)
741 u64 fir1
, fir2
, fir_slice
, serr
, afu_debug
;
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
);
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
);
756 dev_crit(&ctx
->afu
->dev
, "STOPPING CXL TRACE\n");
757 cxl_stop_trace(ctx
->afu
->adapter
);
759 return cxl_ops
->ack_irq(ctx
, 0, errstat
);
762 static irqreturn_t
fail_psl_irq(struct cxl_afu
*afu
, struct cxl_irq_info
*irq_info
)
764 if (irq_info
->dsisr
& CXL_PSL_DSISR_TRANS
)
765 cxl_p2n_write(afu
, CXL_PSL_TFC_An
, CXL_PSL_TFC_An_AE
);
767 cxl_p2n_write(afu
, CXL_PSL_TFC_An
, CXL_PSL_TFC_An_A
);
772 static irqreturn_t
native_irq_multiplexed(int irq
, void *data
)
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;
780 if ((ret
= native_get_irq_info(afu
, &irq_info
))) {
781 WARN(1, "Unable to get CXL IRQ Info: %i\n", ret
);
782 return fail_psl_irq(afu
, &irq_info
);
786 ctx
= idr_find(&afu
->contexts_idr
, ph
);
788 ret
= cxl_irq(irq
, ctx
, &irq_info
);
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
,
798 return fail_psl_irq(afu
, &irq_info
);
801 void native_irq_wait(struct cxl_context
*ctx
)
808 * Wait until no further interrupts are presented by the PSL
812 ph
= cxl_p2n_read(ctx
->afu
, CXL_PSL_PEHandle_An
) & 0xffff;
815 dsisr
= cxl_p2n_read(ctx
->afu
, CXL_PSL_DSISR_An
);
816 if ((dsisr
& CXL_PSL_DSISR_PENDING
) == 0)
819 * We are waiting for the workqueue to process our
820 * irq, so need to let that run here.
825 dev_warn(&ctx
->afu
->dev
, "WARNING: waiting on DSI for PE %i"
826 " DSISR %016llx!\n", ph
, dsisr
);
830 static irqreturn_t
native_slice_irq_err(int irq
, void *data
)
832 struct cxl_afu
*afu
= data
;
833 u64 fir_slice
, errstat
, serr
, afu_debug
;
835 WARN(irq
, "CXL SLICE ERROR interrupt %i\n", irq
);
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
);
846 cxl_p1n_write(afu
, CXL_PSL_SERR_An
, serr
);
851 static irqreturn_t
native_irq_err(int irq
, void *data
)
853 struct cxl
*adapter
= data
;
854 u64 fir1
, fir2
, err_ivte
;
856 WARN(1, "CXL ERROR interrupt %i\n", irq
);
858 err_ivte
= cxl_p1_read(adapter
, CXL_PSL_ErrIVTE
);
859 dev_crit(&adapter
->dev
, "PSL_ErrIVTE: 0x%016llx\n", err_ivte
);
861 dev_crit(&adapter
->dev
, "STOPPING CXL TRACE\n");
862 cxl_stop_trace(adapter
);
864 fir1
= cxl_p1_read(adapter
, CXL_PSL_FIR1
);
865 fir2
= cxl_p1_read(adapter
, CXL_PSL_FIR2
);
867 dev_crit(&adapter
->dev
, "PSL_FIR1: 0x%016llx\nPSL_FIR2: 0x%016llx\n", fir1
, fir2
);
872 int cxl_native_register_psl_err_irq(struct cxl
*adapter
)
876 adapter
->irq_name
= kasprintf(GFP_KERNEL
, "cxl-%s-err",
877 dev_name(&adapter
->dev
));
878 if (!adapter
->irq_name
)
881 if ((rc
= cxl_register_one_irq(adapter
, native_irq_err
, adapter
,
882 &adapter
->native
->err_hwirq
,
883 &adapter
->native
->err_virq
,
884 adapter
->irq_name
))) {
885 kfree(adapter
->irq_name
);
886 adapter
->irq_name
= NULL
;
890 cxl_p1_write(adapter
, CXL_PSL_ErrIVTE
, adapter
->native
->err_hwirq
& 0xffff);
895 void cxl_native_release_psl_err_irq(struct cxl
*adapter
)
897 if (adapter
->native
->err_virq
!= irq_find_mapping(NULL
, adapter
->native
->err_hwirq
))
900 cxl_p1_write(adapter
, CXL_PSL_ErrIVTE
, 0x0000000000000000);
901 cxl_unmap_irq(adapter
->native
->err_virq
, adapter
);
902 cxl_ops
->release_one_irq(adapter
, adapter
->native
->err_hwirq
);
903 kfree(adapter
->irq_name
);
906 int cxl_native_register_serr_irq(struct cxl_afu
*afu
)
911 afu
->err_irq_name
= kasprintf(GFP_KERNEL
, "cxl-%s-err",
912 dev_name(&afu
->dev
));
913 if (!afu
->err_irq_name
)
916 if ((rc
= cxl_register_one_irq(afu
->adapter
, native_slice_irq_err
, afu
,
918 &afu
->serr_virq
, afu
->err_irq_name
))) {
919 kfree(afu
->err_irq_name
);
920 afu
->err_irq_name
= NULL
;
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
);
931 void cxl_native_release_serr_irq(struct cxl_afu
*afu
)
933 if (afu
->serr_virq
!= irq_find_mapping(NULL
, afu
->serr_hwirq
))
936 cxl_p1n_write(afu
, CXL_PSL_SERR_An
, 0x0000000000000000);
937 cxl_unmap_irq(afu
->serr_virq
, afu
);
938 cxl_ops
->release_one_irq(afu
->adapter
, afu
->serr_hwirq
);
939 kfree(afu
->err_irq_name
);
942 int cxl_native_register_psl_irq(struct cxl_afu
*afu
)
946 afu
->psl_irq_name
= kasprintf(GFP_KERNEL
, "cxl-%s",
947 dev_name(&afu
->dev
));
948 if (!afu
->psl_irq_name
)
951 if ((rc
= cxl_register_one_irq(afu
->adapter
, native_irq_multiplexed
,
952 afu
, &afu
->native
->psl_hwirq
, &afu
->native
->psl_virq
,
953 afu
->psl_irq_name
))) {
954 kfree(afu
->psl_irq_name
);
955 afu
->psl_irq_name
= NULL
;
960 void cxl_native_release_psl_irq(struct cxl_afu
*afu
)
962 if (afu
->native
->psl_virq
!= irq_find_mapping(NULL
, afu
->native
->psl_hwirq
))
965 cxl_unmap_irq(afu
->native
->psl_virq
, afu
);
966 cxl_ops
->release_one_irq(afu
->adapter
, afu
->native
->psl_hwirq
);
967 kfree(afu
->psl_irq_name
);
970 static void recover_psl_err(struct cxl_afu
*afu
, u64 errstat
)
974 pr_devel("RECOVERING FROM PSL ERROR... (0x%016llx)\n", errstat
);
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
);
980 /* Write 1s to clear error status bits */
981 cxl_p2n_write(afu
, CXL_PSL_ErrStat_An
, errstat
);
984 static int native_ack_irq(struct cxl_context
*ctx
, u64 tfc
, u64 psl_reset_mask
)
986 trace_cxl_psl_irq_ack(ctx
, tfc
);
988 cxl_p2n_write(ctx
->afu
, CXL_PSL_TFC_An
, tfc
);
990 recover_psl_err(ctx
->afu
, psl_reset_mask
);
995 int cxl_check_error(struct cxl_afu
*afu
)
997 return (cxl_p1n_read(afu
, CXL_PSL_SCNTL_An
) == ~0ULL);
1000 static bool native_support_attributes(const char *attr_name
,
1001 enum cxl_attrs type
)
1006 static int native_afu_cr_read64(struct cxl_afu
*afu
, int cr
, u64 off
, u64
*out
)
1008 if (unlikely(!cxl_ops
->link_ok(afu
->adapter
, afu
)))
1010 if (unlikely(off
>= afu
->crs_len
))
1012 *out
= in_le64(afu
->native
->afu_desc_mmio
+ afu
->crs_offset
+
1013 (cr
* afu
->crs_len
) + off
);
1017 static int native_afu_cr_read32(struct cxl_afu
*afu
, int cr
, u64 off
, u32
*out
)
1019 if (unlikely(!cxl_ops
->link_ok(afu
->adapter
, afu
)))
1021 if (unlikely(off
>= afu
->crs_len
))
1023 *out
= in_le32(afu
->native
->afu_desc_mmio
+ afu
->crs_offset
+
1024 (cr
* afu
->crs_len
) + off
);
1028 static int native_afu_cr_read16(struct cxl_afu
*afu
, int cr
, u64 off
, u16
*out
)
1030 u64 aligned_off
= off
& ~0x3L
;
1034 rc
= native_afu_cr_read32(afu
, cr
, aligned_off
, &val
);
1036 *out
= (val
>> ((off
& 0x3) * 8)) & 0xffff;
1040 static int native_afu_cr_read8(struct cxl_afu
*afu
, int cr
, u64 off
, u8
*out
)
1042 u64 aligned_off
= off
& ~0x3L
;
1046 rc
= native_afu_cr_read32(afu
, cr
, aligned_off
, &val
);
1048 *out
= (val
>> ((off
& 0x3) * 8)) & 0xff;
1052 static int native_afu_cr_write32(struct cxl_afu
*afu
, int cr
, u64 off
, u32 in
)
1054 if (unlikely(!cxl_ops
->link_ok(afu
->adapter
, afu
)))
1056 if (unlikely(off
>= afu
->crs_len
))
1058 out_le32(afu
->native
->afu_desc_mmio
+ afu
->crs_offset
+
1059 (cr
* afu
->crs_len
) + off
, in
);
1063 static int native_afu_cr_write16(struct cxl_afu
*afu
, int cr
, u64 off
, u16 in
)
1065 u64 aligned_off
= off
& ~0x3L
;
1066 u32 val32
, mask
, shift
;
1069 rc
= native_afu_cr_read32(afu
, cr
, aligned_off
, &val32
);
1072 shift
= (off
& 0x3) * 8;
1073 WARN_ON(shift
== 24);
1074 mask
= 0xffff << shift
;
1075 val32
= (val32
& ~mask
) | (in
<< shift
);
1077 rc
= native_afu_cr_write32(afu
, cr
, aligned_off
, val32
);
1081 static int native_afu_cr_write8(struct cxl_afu
*afu
, int cr
, u64 off
, u8 in
)
1083 u64 aligned_off
= off
& ~0x3L
;
1084 u32 val32
, mask
, shift
;
1087 rc
= native_afu_cr_read32(afu
, cr
, aligned_off
, &val32
);
1090 shift
= (off
& 0x3) * 8;
1091 mask
= 0xff << shift
;
1092 val32
= (val32
& ~mask
) | (in
<< shift
);
1094 rc
= native_afu_cr_write32(afu
, cr
, aligned_off
, val32
);
1098 const struct cxl_backend_ops cxl_native_ops
= {
1099 .module
= THIS_MODULE
,
1100 .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
,
1107 .psl_interrupt
= NULL
,
1108 .ack_irq
= native_ack_irq
,
1109 .irq_wait
= native_irq_wait
,
1110 .attach_process
= native_attach_process
,
1111 .detach_process
= native_detach_process
,
1112 .support_attributes
= native_support_attributes
,
1113 .link_ok
= cxl_adapter_link_ok
,
1114 .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
,
1124 .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
,