1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2014 IBM Corp.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef _CXL_H_
7*4882a593Smuzhiyun #define _CXL_H_
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/interrupt.h>
10*4882a593Smuzhiyun #include <linux/semaphore.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/cdev.h>
14*4882a593Smuzhiyun #include <linux/pid.h>
15*4882a593Smuzhiyun #include <linux/io.h>
16*4882a593Smuzhiyun #include <linux/pci.h>
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <asm/cputable.h>
19*4882a593Smuzhiyun #include <asm/mmu.h>
20*4882a593Smuzhiyun #include <asm/reg.h>
21*4882a593Smuzhiyun #include <misc/cxl-base.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <misc/cxl.h>
24*4882a593Smuzhiyun #include <uapi/misc/cxl.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun extern uint cxl_verbose;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define CXL_TIMEOUT 5
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * Bump version each time a user API change is made, whether it is
32*4882a593Smuzhiyun * backwards compatible ot not.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun #define CXL_API_VERSION 3
35*4882a593Smuzhiyun #define CXL_API_VERSION_COMPATIBLE 1
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Opaque types to avoid accidentally passing registers for the wrong MMIO
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * At the end of the day, I'm not married to using typedef here, but it might
41*4882a593Smuzhiyun * (and has!) help avoid bugs like mixing up CXL_PSL_CtxTime and
42*4882a593Smuzhiyun * CXL_PSL_CtxTime_An, or calling cxl_p1n_write instead of cxl_p1_write.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * I'm quite happy if these are changed back to #defines before upstreaming, it
45*4882a593Smuzhiyun * should be little more than a regexp search+replace operation in this file.
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun typedef struct {
48*4882a593Smuzhiyun const int x;
49*4882a593Smuzhiyun } cxl_p1_reg_t;
50*4882a593Smuzhiyun typedef struct {
51*4882a593Smuzhiyun const int x;
52*4882a593Smuzhiyun } cxl_p1n_reg_t;
53*4882a593Smuzhiyun typedef struct {
54*4882a593Smuzhiyun const int x;
55*4882a593Smuzhiyun } cxl_p2n_reg_t;
56*4882a593Smuzhiyun #define cxl_reg_off(reg) \
57*4882a593Smuzhiyun (reg.x)
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* Memory maps. Ref CXL Appendix A */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* PSL Privilege 1 Memory Map */
62*4882a593Smuzhiyun /* Configuration and Control area - CAIA 1&2 */
63*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_CtxTime = {0x0000};
64*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_ErrIVTE = {0x0008};
65*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_KEY1 = {0x0010};
66*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_KEY2 = {0x0018};
67*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_Control = {0x0020};
68*4882a593Smuzhiyun /* Downloading */
69*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_DLCNTL = {0x0060};
70*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_DLADDR = {0x0068};
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* PSL Lookaside Buffer Management Area - CAIA 1 */
73*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_LBISEL = {0x0080};
74*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_SLBIE = {0x0088};
75*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_SLBIA = {0x0090};
76*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_TLBIE = {0x00A0};
77*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_TLBIA = {0x00A8};
78*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_AFUSEL = {0x00B0};
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* 0x00C0:7EFF Implementation dependent area */
81*4882a593Smuzhiyun /* PSL registers - CAIA 1 */
82*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_FIR1 = {0x0100};
83*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_FIR2 = {0x0108};
84*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_Timebase = {0x0110};
85*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_VERSION = {0x0118};
86*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_RESLCKTO = {0x0128};
87*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_TB_CTLSTAT = {0x0140};
88*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_FIR_CNTL = {0x0148};
89*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_DSNDCTL = {0x0150};
90*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_SNWRALLOC = {0x0158};
91*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL_TRACE = {0x0170};
92*4882a593Smuzhiyun /* PSL registers - CAIA 2 */
93*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_CONTROL = {0x0020};
94*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_INV = {0x0110};
95*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_DBG = {0x0130};
96*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_DEF = {0x0140};
97*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_DSNCTL = {0x0168};
98*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_FIR1 = {0x0300};
99*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_FIR_MASK = {0x0308};
100*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_Timebase = {0x0310};
101*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_DEBUG = {0x0320};
102*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_FIR_CNTL = {0x0348};
103*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_DSNDCTL = {0x0350};
104*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_TB_CTLSTAT = {0x0340};
105*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_TRACECFG = {0x0368};
106*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_APCDEDALLOC = {0x0378};
107*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_APCDEDTYPE = {0x0380};
108*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_TNR_ADDR = {0x0388};
109*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_CTCCFG = {0x0390};
110*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_PSL9_GP_CT = {0x0398};
111*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_IERAT = {0x0588};
112*4882a593Smuzhiyun static const cxl_p1_reg_t CXL_XSL9_ILPP = {0x0590};
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* 0x7F00:7FFF Reserved PCIe MSI-X Pending Bit Array area */
115*4882a593Smuzhiyun /* 0x8000:FFFF Reserved PCIe MSI-X Table Area */
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* PSL Slice Privilege 1 Memory Map */
118*4882a593Smuzhiyun /* Configuration Area - CAIA 1&2 */
119*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SR_An = {0x00};
120*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_LPID_An = {0x08};
121*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_AMBAR_An = {0x10};
122*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SPOffset_An = {0x18};
123*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_ID_An = {0x20};
124*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SERR_An = {0x28};
125*4882a593Smuzhiyun /* Memory Management and Lookaside Buffer Management - CAIA 1*/
126*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SDR_An = {0x30};
127*4882a593Smuzhiyun /* Memory Management and Lookaside Buffer Management - CAIA 1&2 */
128*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_AMOR_An = {0x38};
129*4882a593Smuzhiyun /* Pointer Area - CAIA 1&2 */
130*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_HAURP_An = {0x80};
131*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SPAP_An = {0x88};
132*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_LLCMD_An = {0x90};
133*4882a593Smuzhiyun /* Control Area - CAIA 1&2 */
134*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SCNTL_An = {0xA0};
135*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_CtxTime_An = {0xA8};
136*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_IVTE_Offset_An = {0xB0};
137*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_IVTE_Limit_An = {0xB8};
138*4882a593Smuzhiyun /* 0xC0:FF Implementation Dependent Area - CAIA 1&2 */
139*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_FIR_SLICE_An = {0xC0};
140*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_AFU_DEBUG_An = {0xC8};
141*4882a593Smuzhiyun /* 0xC0:FF Implementation Dependent Area - CAIA 1 */
142*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_APCALLOC_A = {0xD0};
143*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_COALLOC_A = {0xD8};
144*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_RXCTL_A = {0xE0};
145*4882a593Smuzhiyun static const cxl_p1n_reg_t CXL_PSL_SLICE_TRACE = {0xE8};
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* PSL Slice Privilege 2 Memory Map */
148*4882a593Smuzhiyun /* Configuration and Control Area - CAIA 1&2 */
149*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_PID_TID_An = {0x000};
150*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_CSRP_An = {0x008};
151*4882a593Smuzhiyun /* Configuration and Control Area - CAIA 1 */
152*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_AURP0_An = {0x010};
153*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_AURP1_An = {0x018};
154*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_SSTP0_An = {0x020};
155*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_SSTP1_An = {0x028};
156*4882a593Smuzhiyun /* Configuration and Control Area - CAIA 1 */
157*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_AMR_An = {0x030};
158*4882a593Smuzhiyun /* Segment Lookaside Buffer Management - CAIA 1 */
159*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_SLBIE_An = {0x040};
160*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_SLBIA_An = {0x048};
161*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_SLBI_Select_An = {0x050};
162*4882a593Smuzhiyun /* Interrupt Registers - CAIA 1&2 */
163*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_DSISR_An = {0x060};
164*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_DAR_An = {0x068};
165*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_DSR_An = {0x070};
166*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_TFC_An = {0x078};
167*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_PEHandle_An = {0x080};
168*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_ErrStat_An = {0x088};
169*4882a593Smuzhiyun /* AFU Registers - CAIA 1&2 */
170*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_AFU_Cntl_An = {0x090};
171*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_AFU_ERR_An = {0x098};
172*4882a593Smuzhiyun /* Work Element Descriptor - CAIA 1&2 */
173*4882a593Smuzhiyun static const cxl_p2n_reg_t CXL_PSL_WED_An = {0x0A0};
174*4882a593Smuzhiyun /* 0x0C0:FFF Implementation Dependent Area */
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun #define CXL_PSL_SPAP_Addr 0x0ffffffffffff000ULL
177*4882a593Smuzhiyun #define CXL_PSL_SPAP_Size 0x0000000000000ff0ULL
178*4882a593Smuzhiyun #define CXL_PSL_SPAP_Size_Shift 4
179*4882a593Smuzhiyun #define CXL_PSL_SPAP_V 0x0000000000000001ULL
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /****** CXL_PSL_Control ****************************************************/
182*4882a593Smuzhiyun #define CXL_PSL_Control_tb (0x1ull << (63-63))
183*4882a593Smuzhiyun #define CXL_PSL_Control_Fr (0x1ull << (63-31))
184*4882a593Smuzhiyun #define CXL_PSL_Control_Fs_MASK (0x3ull << (63-29))
185*4882a593Smuzhiyun #define CXL_PSL_Control_Fs_Complete (0x3ull << (63-29))
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /****** CXL_PSL_DLCNTL *****************************************************/
188*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_D (0x1ull << (63-28))
189*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_C (0x1ull << (63-29))
190*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_E (0x1ull << (63-30))
191*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_S (0x1ull << (63-31))
192*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_CE (CXL_PSL_DLCNTL_C | CXL_PSL_DLCNTL_E)
193*4882a593Smuzhiyun #define CXL_PSL_DLCNTL_DCES (CXL_PSL_DLCNTL_D | CXL_PSL_DLCNTL_CE | CXL_PSL_DLCNTL_S)
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /****** CXL_PSL_SR_An ******************************************************/
196*4882a593Smuzhiyun #define CXL_PSL_SR_An_SF MSR_SF /* 64bit */
197*4882a593Smuzhiyun #define CXL_PSL_SR_An_TA (1ull << (63-1)) /* Tags active, GA1: 0 */
198*4882a593Smuzhiyun #define CXL_PSL_SR_An_HV MSR_HV /* Hypervisor, GA1: 0 */
199*4882a593Smuzhiyun #define CXL_PSL_SR_An_XLAT_hpt (0ull << (63-6))/* Hashed page table (HPT) mode */
200*4882a593Smuzhiyun #define CXL_PSL_SR_An_XLAT_roh (2ull << (63-6))/* Radix on HPT mode */
201*4882a593Smuzhiyun #define CXL_PSL_SR_An_XLAT_ror (3ull << (63-6))/* Radix on Radix mode */
202*4882a593Smuzhiyun #define CXL_PSL_SR_An_BOT (1ull << (63-10)) /* Use the in-memory segment table */
203*4882a593Smuzhiyun #define CXL_PSL_SR_An_PR MSR_PR /* Problem state, GA1: 1 */
204*4882a593Smuzhiyun #define CXL_PSL_SR_An_ISL (1ull << (63-53)) /* Ignore Segment Large Page */
205*4882a593Smuzhiyun #define CXL_PSL_SR_An_TC (1ull << (63-54)) /* Page Table secondary hash */
206*4882a593Smuzhiyun #define CXL_PSL_SR_An_US (1ull << (63-56)) /* User state, GA1: X */
207*4882a593Smuzhiyun #define CXL_PSL_SR_An_SC (1ull << (63-58)) /* Segment Table secondary hash */
208*4882a593Smuzhiyun #define CXL_PSL_SR_An_R MSR_DR /* Relocate, GA1: 1 */
209*4882a593Smuzhiyun #define CXL_PSL_SR_An_MP (1ull << (63-62)) /* Master Process */
210*4882a593Smuzhiyun #define CXL_PSL_SR_An_LE (1ull << (63-63)) /* Little Endian */
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /****** CXL_PSL_ID_An ****************************************************/
213*4882a593Smuzhiyun #define CXL_PSL_ID_An_F (1ull << (63-31))
214*4882a593Smuzhiyun #define CXL_PSL_ID_An_L (1ull << (63-30))
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /****** CXL_PSL_SERR_An ****************************************************/
217*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afuto (1ull << (63-0))
218*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afudis (1ull << (63-1))
219*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afuov (1ull << (63-2))
220*4882a593Smuzhiyun #define CXL_PSL_SERR_An_badsrc (1ull << (63-3))
221*4882a593Smuzhiyun #define CXL_PSL_SERR_An_badctx (1ull << (63-4))
222*4882a593Smuzhiyun #define CXL_PSL_SERR_An_llcmdis (1ull << (63-5))
223*4882a593Smuzhiyun #define CXL_PSL_SERR_An_llcmdto (1ull << (63-6))
224*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afupar (1ull << (63-7))
225*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afudup (1ull << (63-8))
226*4882a593Smuzhiyun #define CXL_PSL_SERR_An_IRQS ( \
227*4882a593Smuzhiyun CXL_PSL_SERR_An_afuto | CXL_PSL_SERR_An_afudis | CXL_PSL_SERR_An_afuov | \
228*4882a593Smuzhiyun CXL_PSL_SERR_An_badsrc | CXL_PSL_SERR_An_badctx | CXL_PSL_SERR_An_llcmdis | \
229*4882a593Smuzhiyun CXL_PSL_SERR_An_llcmdto | CXL_PSL_SERR_An_afupar | CXL_PSL_SERR_An_afudup)
230*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afuto_mask (1ull << (63-32))
231*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afudis_mask (1ull << (63-33))
232*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afuov_mask (1ull << (63-34))
233*4882a593Smuzhiyun #define CXL_PSL_SERR_An_badsrc_mask (1ull << (63-35))
234*4882a593Smuzhiyun #define CXL_PSL_SERR_An_badctx_mask (1ull << (63-36))
235*4882a593Smuzhiyun #define CXL_PSL_SERR_An_llcmdis_mask (1ull << (63-37))
236*4882a593Smuzhiyun #define CXL_PSL_SERR_An_llcmdto_mask (1ull << (63-38))
237*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afupar_mask (1ull << (63-39))
238*4882a593Smuzhiyun #define CXL_PSL_SERR_An_afudup_mask (1ull << (63-40))
239*4882a593Smuzhiyun #define CXL_PSL_SERR_An_IRQ_MASKS ( \
240*4882a593Smuzhiyun CXL_PSL_SERR_An_afuto_mask | CXL_PSL_SERR_An_afudis_mask | CXL_PSL_SERR_An_afuov_mask | \
241*4882a593Smuzhiyun CXL_PSL_SERR_An_badsrc_mask | CXL_PSL_SERR_An_badctx_mask | CXL_PSL_SERR_An_llcmdis_mask | \
242*4882a593Smuzhiyun CXL_PSL_SERR_An_llcmdto_mask | CXL_PSL_SERR_An_afupar_mask | CXL_PSL_SERR_An_afudup_mask)
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun #define CXL_PSL_SERR_An_AE (1ull << (63-30))
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /****** CXL_PSL_SCNTL_An ****************************************************/
247*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_CR (0x1ull << (63-15))
248*4882a593Smuzhiyun /* Programming Modes: */
249*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_MASK (0xffffull << (63-31))
250*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_Shared (0x0000ull << (63-31))
251*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_OS (0x0001ull << (63-31))
252*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_Process (0x0002ull << (63-31))
253*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_AFU (0x0004ull << (63-31))
254*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_PM_AFU_PBT (0x0104ull << (63-31))
255*4882a593Smuzhiyun /* Purge Status (ro) */
256*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ps_MASK (0x3ull << (63-39))
257*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ps_Pending (0x1ull << (63-39))
258*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ps_Complete (0x3ull << (63-39))
259*4882a593Smuzhiyun /* Purge */
260*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Pc (0x1ull << (63-48))
261*4882a593Smuzhiyun /* Suspend Status (ro) */
262*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ss_MASK (0x3ull << (63-55))
263*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ss_Pending (0x1ull << (63-55))
264*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Ss_Complete (0x3ull << (63-55))
265*4882a593Smuzhiyun /* Suspend Control */
266*4882a593Smuzhiyun #define CXL_PSL_SCNTL_An_Sc (0x1ull << (63-63))
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* AFU Slice Enable Status (ro) */
269*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_ES_MASK (0x7ull << (63-2))
270*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_ES_Disabled (0x0ull << (63-2))
271*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_ES_Enabled (0x4ull << (63-2))
272*4882a593Smuzhiyun /* AFU Slice Enable */
273*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_E (0x1ull << (63-3))
274*4882a593Smuzhiyun /* AFU Slice Reset status (ro) */
275*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_RS_MASK (0x3ull << (63-5))
276*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_RS_Pending (0x1ull << (63-5))
277*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_RS_Complete (0x2ull << (63-5))
278*4882a593Smuzhiyun /* AFU Slice Reset */
279*4882a593Smuzhiyun #define CXL_AFU_Cntl_An_RA (0x1ull << (63-7))
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /****** CXL_SSTP0/1_An ******************************************************/
282*4882a593Smuzhiyun /* These top bits are for the segment that CONTAINS the segment table */
283*4882a593Smuzhiyun #define CXL_SSTP0_An_B_SHIFT SLB_VSID_SSIZE_SHIFT
284*4882a593Smuzhiyun #define CXL_SSTP0_An_KS (1ull << (63-2))
285*4882a593Smuzhiyun #define CXL_SSTP0_An_KP (1ull << (63-3))
286*4882a593Smuzhiyun #define CXL_SSTP0_An_N (1ull << (63-4))
287*4882a593Smuzhiyun #define CXL_SSTP0_An_L (1ull << (63-5))
288*4882a593Smuzhiyun #define CXL_SSTP0_An_C (1ull << (63-6))
289*4882a593Smuzhiyun #define CXL_SSTP0_An_TA (1ull << (63-7))
290*4882a593Smuzhiyun #define CXL_SSTP0_An_LP_SHIFT (63-9) /* 2 Bits */
291*4882a593Smuzhiyun /* And finally, the virtual address & size of the segment table: */
292*4882a593Smuzhiyun #define CXL_SSTP0_An_SegTableSize_SHIFT (63-31) /* 12 Bits */
293*4882a593Smuzhiyun #define CXL_SSTP0_An_SegTableSize_MASK \
294*4882a593Smuzhiyun (((1ull << 12) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT)
295*4882a593Smuzhiyun #define CXL_SSTP0_An_STVA_U_MASK ((1ull << (63-49))-1)
296*4882a593Smuzhiyun #define CXL_SSTP1_An_STVA_L_MASK (~((1ull << (63-55))-1))
297*4882a593Smuzhiyun #define CXL_SSTP1_An_V (1ull << (63-63))
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /****** CXL_PSL_SLBIE_[An] - CAIA 1 **************************************************/
300*4882a593Smuzhiyun /* write: */
301*4882a593Smuzhiyun #define CXL_SLBIE_C PPC_BIT(36) /* Class */
302*4882a593Smuzhiyun #define CXL_SLBIE_SS PPC_BITMASK(37, 38) /* Segment Size */
303*4882a593Smuzhiyun #define CXL_SLBIE_SS_SHIFT PPC_BITLSHIFT(38)
304*4882a593Smuzhiyun #define CXL_SLBIE_TA PPC_BIT(38) /* Tags Active */
305*4882a593Smuzhiyun /* read: */
306*4882a593Smuzhiyun #define CXL_SLBIE_MAX PPC_BITMASK(24, 31)
307*4882a593Smuzhiyun #define CXL_SLBIE_PENDING PPC_BITMASK(56, 63)
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /****** Common to all CXL_TLBIA/SLBIA_[An] - CAIA 1 **********************************/
310*4882a593Smuzhiyun #define CXL_TLB_SLB_P (1ull) /* Pending (read) */
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /****** Common to all CXL_TLB/SLB_IA/IE_[An] registers - CAIA 1 **********************/
313*4882a593Smuzhiyun #define CXL_TLB_SLB_IQ_ALL (0ull) /* Inv qualifier */
314*4882a593Smuzhiyun #define CXL_TLB_SLB_IQ_LPID (1ull) /* Inv qualifier */
315*4882a593Smuzhiyun #define CXL_TLB_SLB_IQ_LPIDPID (3ull) /* Inv qualifier */
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /****** CXL_PSL_AFUSEL ******************************************************/
318*4882a593Smuzhiyun #define CXL_PSL_AFUSEL_A (1ull << (63-55)) /* Adapter wide invalidates affect all AFUs */
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /****** CXL_PSL_DSISR_An - CAIA 1 ****************************************************/
321*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_DS (1ull << (63-0)) /* Segment not found */
322*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_DM (1ull << (63-1)) /* PTE not found (See also: M) or protection fault */
323*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_ST (1ull << (63-2)) /* Segment Table PTE not found */
324*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_UR (1ull << (63-3)) /* AURP PTE not found */
325*4882a593Smuzhiyun #define CXL_PSL_DSISR_TRANS (CXL_PSL_DSISR_An_DS | CXL_PSL_DSISR_An_DM | CXL_PSL_DSISR_An_ST | CXL_PSL_DSISR_An_UR)
326*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_PE (1ull << (63-4)) /* PSL Error (implementation specific) */
327*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_AE (1ull << (63-5)) /* AFU Error */
328*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_OC (1ull << (63-6)) /* OS Context Warning */
329*4882a593Smuzhiyun #define CXL_PSL_DSISR_PENDING (CXL_PSL_DSISR_TRANS | CXL_PSL_DSISR_An_PE | CXL_PSL_DSISR_An_AE | CXL_PSL_DSISR_An_OC)
330*4882a593Smuzhiyun /* NOTE: Bits 32:63 are undefined if DSISR[DS] = 1 */
331*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_M DSISR_NOHPTE /* PTE not found */
332*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_P DSISR_PROTFAULT /* Storage protection violation */
333*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_A (1ull << (63-37)) /* AFU lock access to write through or cache inhibited storage */
334*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_S DSISR_ISSTORE /* Access was afu_wr or afu_zero */
335*4882a593Smuzhiyun #define CXL_PSL_DSISR_An_K DSISR_KEYFAULT /* Access not permitted by virtual page class key protection */
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /****** CXL_PSL_DSISR_An - CAIA 2 ****************************************************/
338*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_TF (1ull << (63-3)) /* Translation fault */
339*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PE (1ull << (63-4)) /* PSL Error (implementation specific) */
340*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_AE (1ull << (63-5)) /* AFU Error */
341*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_OC (1ull << (63-6)) /* OS Context Warning */
342*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_S (1ull << (63-38)) /* TF for a write operation */
343*4882a593Smuzhiyun #define CXL_PSL9_DSISR_PENDING (CXL_PSL9_DSISR_An_TF | CXL_PSL9_DSISR_An_PE | CXL_PSL9_DSISR_An_AE | CXL_PSL9_DSISR_An_OC)
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun * NOTE: Bits 56:63 (Checkout Response Status) are valid when DSISR_An[TF] = 1
346*4882a593Smuzhiyun * Status (0:7) Encoding
347*4882a593Smuzhiyun */
348*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_CO_MASK 0x00000000000000ffULL
349*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_SF 0x0000000000000080ULL /* Segment Fault 0b10000000 */
350*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PF_SLR 0x0000000000000088ULL /* PTE not found (Single Level Radix) 0b10001000 */
351*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PF_RGC 0x000000000000008CULL /* PTE not found (Radix Guest (child)) 0b10001100 */
352*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PF_RGP 0x0000000000000090ULL /* PTE not found (Radix Guest (parent)) 0b10010000 */
353*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PF_HRH 0x0000000000000094ULL /* PTE not found (HPT/Radix Host) 0b10010100 */
354*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_PF_STEG 0x000000000000009CULL /* PTE not found (STEG VA) 0b10011100 */
355*4882a593Smuzhiyun #define CXL_PSL9_DSISR_An_URTCH 0x00000000000000B4ULL /* Unsupported Radix Tree Configuration 0b10110100 */
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /****** CXL_PSL_TFC_An ******************************************************/
358*4882a593Smuzhiyun #define CXL_PSL_TFC_An_A (1ull << (63-28)) /* Acknowledge non-translation fault */
359*4882a593Smuzhiyun #define CXL_PSL_TFC_An_C (1ull << (63-29)) /* Continue (abort transaction) */
360*4882a593Smuzhiyun #define CXL_PSL_TFC_An_AE (1ull << (63-30)) /* Restart PSL with address error */
361*4882a593Smuzhiyun #define CXL_PSL_TFC_An_R (1ull << (63-31)) /* Restart PSL transaction */
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /****** CXL_PSL_DEBUG *****************************************************/
364*4882a593Smuzhiyun #define CXL_PSL_DEBUG_CDC (1ull << (63-27)) /* Coherent Data cache support */
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /****** CXL_XSL9_IERAT_ERAT - CAIA 2 **********************************/
367*4882a593Smuzhiyun #define CXL_XSL9_IERAT_MLPID (1ull << (63-0)) /* Match LPID */
368*4882a593Smuzhiyun #define CXL_XSL9_IERAT_MPID (1ull << (63-1)) /* Match PID */
369*4882a593Smuzhiyun #define CXL_XSL9_IERAT_PRS (1ull << (63-4)) /* PRS bit for Radix invalidations */
370*4882a593Smuzhiyun #define CXL_XSL9_IERAT_INVR (1ull << (63-3)) /* Invalidate Radix */
371*4882a593Smuzhiyun #define CXL_XSL9_IERAT_IALL (1ull << (63-8)) /* Invalidate All */
372*4882a593Smuzhiyun #define CXL_XSL9_IERAT_IINPROG (1ull << (63-63)) /* Invalidate in progress */
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /* cxl_process_element->software_status */
375*4882a593Smuzhiyun #define CXL_PE_SOFTWARE_STATE_V (1ul << (31 - 0)) /* Valid */
376*4882a593Smuzhiyun #define CXL_PE_SOFTWARE_STATE_C (1ul << (31 - 29)) /* Complete */
377*4882a593Smuzhiyun #define CXL_PE_SOFTWARE_STATE_S (1ul << (31 - 30)) /* Suspend */
378*4882a593Smuzhiyun #define CXL_PE_SOFTWARE_STATE_T (1ul << (31 - 31)) /* Terminate */
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /****** CXL_PSL_RXCTL_An (Implementation Specific) **************************
381*4882a593Smuzhiyun * Controls AFU Hang Pulse, which sets the timeout for the AFU to respond to
382*4882a593Smuzhiyun * the PSL for any response (except MMIO). Timeouts will occur between 1x to 2x
383*4882a593Smuzhiyun * of the hang pulse frequency.
384*4882a593Smuzhiyun */
385*4882a593Smuzhiyun #define CXL_PSL_RXCTL_AFUHP_4S 0x7000000000000000ULL
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /* SPA->sw_command_status */
388*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_MASK 0xffff000000000000ULL
389*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_TERMINATE 0x0001000000000000ULL
390*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_REMOVE 0x0002000000000000ULL
391*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_SUSPEND 0x0003000000000000ULL
392*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_RESUME 0x0004000000000000ULL
393*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_ADD 0x0005000000000000ULL
394*4882a593Smuzhiyun #define CXL_SPA_SW_CMD_UPDATE 0x0006000000000000ULL
395*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_MASK 0x0000ffff00000000ULL
396*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_TERMINATED 0x0000000100000000ULL
397*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_REMOVED 0x0000000200000000ULL
398*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_SUSPENDED 0x0000000300000000ULL
399*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_RESUMED 0x0000000400000000ULL
400*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_ADDED 0x0000000500000000ULL
401*4882a593Smuzhiyun #define CXL_SPA_SW_STATE_UPDATED 0x0000000600000000ULL
402*4882a593Smuzhiyun #define CXL_SPA_SW_PSL_ID_MASK 0x00000000ffff0000ULL
403*4882a593Smuzhiyun #define CXL_SPA_SW_LINK_MASK 0x000000000000ffffULL
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun #define CXL_MAX_SLICES 4
406*4882a593Smuzhiyun #define MAX_AFU_MMIO_REGS 3
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun #define CXL_MODE_TIME_SLICED 0x4
409*4882a593Smuzhiyun #define CXL_SUPPORTED_MODES (CXL_MODE_DEDICATED | CXL_MODE_DIRECTED)
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun #define CXL_DEV_MINORS 13 /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
412*4882a593Smuzhiyun #define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
413*4882a593Smuzhiyun #define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun #define CXL_PSL9_TRACEID_MAX 0xAU
416*4882a593Smuzhiyun #define CXL_PSL9_TRACESTATE_FIN 0x3U
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun enum cxl_context_status {
419*4882a593Smuzhiyun CLOSED,
420*4882a593Smuzhiyun OPENED,
421*4882a593Smuzhiyun STARTED
422*4882a593Smuzhiyun };
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun enum prefault_modes {
425*4882a593Smuzhiyun CXL_PREFAULT_NONE,
426*4882a593Smuzhiyun CXL_PREFAULT_WED,
427*4882a593Smuzhiyun CXL_PREFAULT_ALL,
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun enum cxl_attrs {
431*4882a593Smuzhiyun CXL_ADAPTER_ATTRS,
432*4882a593Smuzhiyun CXL_AFU_MASTER_ATTRS,
433*4882a593Smuzhiyun CXL_AFU_ATTRS,
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun struct cxl_sste {
437*4882a593Smuzhiyun __be64 esid_data;
438*4882a593Smuzhiyun __be64 vsid_data;
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun #define to_cxl_adapter(d) container_of(d, struct cxl, dev)
442*4882a593Smuzhiyun #define to_cxl_afu(d) container_of(d, struct cxl_afu, dev)
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun struct cxl_afu_native {
445*4882a593Smuzhiyun void __iomem *p1n_mmio;
446*4882a593Smuzhiyun void __iomem *afu_desc_mmio;
447*4882a593Smuzhiyun irq_hw_number_t psl_hwirq;
448*4882a593Smuzhiyun unsigned int psl_virq;
449*4882a593Smuzhiyun struct mutex spa_mutex;
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun * Only the first part of the SPA is used for the process element
452*4882a593Smuzhiyun * linked list. The only other part that software needs to worry about
453*4882a593Smuzhiyun * is sw_command_status, which we store a separate pointer to.
454*4882a593Smuzhiyun * Everything else in the SPA is only used by hardware
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun struct cxl_process_element *spa;
457*4882a593Smuzhiyun __be64 *sw_command_status;
458*4882a593Smuzhiyun unsigned int spa_size;
459*4882a593Smuzhiyun int spa_order;
460*4882a593Smuzhiyun int spa_max_procs;
461*4882a593Smuzhiyun u64 pp_offset;
462*4882a593Smuzhiyun };
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun struct cxl_afu_guest {
465*4882a593Smuzhiyun struct cxl_afu *parent;
466*4882a593Smuzhiyun u64 handle;
467*4882a593Smuzhiyun phys_addr_t p2n_phys;
468*4882a593Smuzhiyun u64 p2n_size;
469*4882a593Smuzhiyun int max_ints;
470*4882a593Smuzhiyun bool handle_err;
471*4882a593Smuzhiyun struct delayed_work work_err;
472*4882a593Smuzhiyun int previous_state;
473*4882a593Smuzhiyun };
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun struct cxl_afu {
476*4882a593Smuzhiyun struct cxl_afu_native *native;
477*4882a593Smuzhiyun struct cxl_afu_guest *guest;
478*4882a593Smuzhiyun irq_hw_number_t serr_hwirq;
479*4882a593Smuzhiyun unsigned int serr_virq;
480*4882a593Smuzhiyun char *psl_irq_name;
481*4882a593Smuzhiyun char *err_irq_name;
482*4882a593Smuzhiyun void __iomem *p2n_mmio;
483*4882a593Smuzhiyun phys_addr_t psn_phys;
484*4882a593Smuzhiyun u64 pp_size;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun struct cxl *adapter;
487*4882a593Smuzhiyun struct device dev;
488*4882a593Smuzhiyun struct cdev afu_cdev_s, afu_cdev_m, afu_cdev_d;
489*4882a593Smuzhiyun struct device *chardev_s, *chardev_m, *chardev_d;
490*4882a593Smuzhiyun struct idr contexts_idr;
491*4882a593Smuzhiyun struct dentry *debugfs;
492*4882a593Smuzhiyun struct mutex contexts_lock;
493*4882a593Smuzhiyun spinlock_t afu_cntl_lock;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /* -1: AFU deconfigured/locked, >= 0: number of readers */
496*4882a593Smuzhiyun atomic_t configured_state;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun /* AFU error buffer fields and bin attribute for sysfs */
499*4882a593Smuzhiyun u64 eb_len, eb_offset;
500*4882a593Smuzhiyun struct bin_attribute attr_eb;
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /* pointer to the vphb */
503*4882a593Smuzhiyun struct pci_controller *phb;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun int pp_irqs;
506*4882a593Smuzhiyun int irqs_max;
507*4882a593Smuzhiyun int num_procs;
508*4882a593Smuzhiyun int max_procs_virtualised;
509*4882a593Smuzhiyun int slice;
510*4882a593Smuzhiyun int modes_supported;
511*4882a593Smuzhiyun int current_mode;
512*4882a593Smuzhiyun int crs_num;
513*4882a593Smuzhiyun u64 crs_len;
514*4882a593Smuzhiyun u64 crs_offset;
515*4882a593Smuzhiyun struct list_head crs;
516*4882a593Smuzhiyun enum prefault_modes prefault_mode;
517*4882a593Smuzhiyun bool psa;
518*4882a593Smuzhiyun bool pp_psa;
519*4882a593Smuzhiyun bool enabled;
520*4882a593Smuzhiyun };
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun struct cxl_irq_name {
524*4882a593Smuzhiyun struct list_head list;
525*4882a593Smuzhiyun char *name;
526*4882a593Smuzhiyun };
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun struct irq_avail {
529*4882a593Smuzhiyun irq_hw_number_t offset;
530*4882a593Smuzhiyun irq_hw_number_t range;
531*4882a593Smuzhiyun unsigned long *bitmap;
532*4882a593Smuzhiyun };
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /*
535*4882a593Smuzhiyun * This is a cxl context. If the PSL is in dedicated mode, there will be one
536*4882a593Smuzhiyun * of these per AFU. If in AFU directed there can be lots of these.
537*4882a593Smuzhiyun */
538*4882a593Smuzhiyun struct cxl_context {
539*4882a593Smuzhiyun struct cxl_afu *afu;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun /* Problem state MMIO */
542*4882a593Smuzhiyun phys_addr_t psn_phys;
543*4882a593Smuzhiyun u64 psn_size;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* Used to unmap any mmaps when force detaching */
546*4882a593Smuzhiyun struct address_space *mapping;
547*4882a593Smuzhiyun struct mutex mapping_lock;
548*4882a593Smuzhiyun struct page *ff_page;
549*4882a593Smuzhiyun bool mmio_err_ff;
550*4882a593Smuzhiyun bool kernelapi;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun spinlock_t sste_lock; /* Protects segment table entries */
553*4882a593Smuzhiyun struct cxl_sste *sstp;
554*4882a593Smuzhiyun u64 sstp0, sstp1;
555*4882a593Smuzhiyun unsigned int sst_size, sst_lru;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun wait_queue_head_t wq;
558*4882a593Smuzhiyun /* use mm context associated with this pid for ds faults */
559*4882a593Smuzhiyun struct pid *pid;
560*4882a593Smuzhiyun spinlock_t lock; /* Protects pending_irq_mask, pending_fault and fault_addr */
561*4882a593Smuzhiyun /* Only used in PR mode */
562*4882a593Smuzhiyun u64 process_token;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /* driver private data */
565*4882a593Smuzhiyun void *priv;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun unsigned long *irq_bitmap; /* Accessed from IRQ context */
568*4882a593Smuzhiyun struct cxl_irq_ranges irqs;
569*4882a593Smuzhiyun struct list_head irq_names;
570*4882a593Smuzhiyun u64 fault_addr;
571*4882a593Smuzhiyun u64 fault_dsisr;
572*4882a593Smuzhiyun u64 afu_err;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun /*
575*4882a593Smuzhiyun * This status and it's lock pretects start and detach context
576*4882a593Smuzhiyun * from racing. It also prevents detach from racing with
577*4882a593Smuzhiyun * itself
578*4882a593Smuzhiyun */
579*4882a593Smuzhiyun enum cxl_context_status status;
580*4882a593Smuzhiyun struct mutex status_mutex;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /* XXX: Is it possible to need multiple work items at once? */
584*4882a593Smuzhiyun struct work_struct fault_work;
585*4882a593Smuzhiyun u64 dsisr;
586*4882a593Smuzhiyun u64 dar;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun struct cxl_process_element *elem;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /*
591*4882a593Smuzhiyun * pe is the process element handle, assigned by this driver when the
592*4882a593Smuzhiyun * context is initialized.
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * external_pe is the PE shown outside of cxl.
595*4882a593Smuzhiyun * On bare-metal, pe=external_pe, because we decide what the handle is.
596*4882a593Smuzhiyun * In a guest, we only find out about the pe used by pHyp when the
597*4882a593Smuzhiyun * context is attached, and that's the value we want to report outside
598*4882a593Smuzhiyun * of cxl.
599*4882a593Smuzhiyun */
600*4882a593Smuzhiyun int pe;
601*4882a593Smuzhiyun int external_pe;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun u32 irq_count;
604*4882a593Smuzhiyun bool pe_inserted;
605*4882a593Smuzhiyun bool master;
606*4882a593Smuzhiyun bool kernel;
607*4882a593Smuzhiyun bool pending_irq;
608*4882a593Smuzhiyun bool pending_fault;
609*4882a593Smuzhiyun bool pending_afu_err;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /* Used by AFU drivers for driver specific event delivery */
612*4882a593Smuzhiyun struct cxl_afu_driver_ops *afu_driver_ops;
613*4882a593Smuzhiyun atomic_t afu_driver_events;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun struct rcu_head rcu;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun struct mm_struct *mm;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun u16 tidr;
620*4882a593Smuzhiyun bool assign_tidr;
621*4882a593Smuzhiyun };
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun struct cxl_irq_info;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun struct cxl_service_layer_ops {
626*4882a593Smuzhiyun int (*adapter_regs_init)(struct cxl *adapter, struct pci_dev *dev);
627*4882a593Smuzhiyun int (*invalidate_all)(struct cxl *adapter);
628*4882a593Smuzhiyun int (*afu_regs_init)(struct cxl_afu *afu);
629*4882a593Smuzhiyun int (*sanitise_afu_regs)(struct cxl_afu *afu);
630*4882a593Smuzhiyun int (*register_serr_irq)(struct cxl_afu *afu);
631*4882a593Smuzhiyun void (*release_serr_irq)(struct cxl_afu *afu);
632*4882a593Smuzhiyun irqreturn_t (*handle_interrupt)(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
633*4882a593Smuzhiyun irqreturn_t (*fail_irq)(struct cxl_afu *afu, struct cxl_irq_info *irq_info);
634*4882a593Smuzhiyun int (*activate_dedicated_process)(struct cxl_afu *afu);
635*4882a593Smuzhiyun int (*attach_afu_directed)(struct cxl_context *ctx, u64 wed, u64 amr);
636*4882a593Smuzhiyun int (*attach_dedicated_process)(struct cxl_context *ctx, u64 wed, u64 amr);
637*4882a593Smuzhiyun void (*update_dedicated_ivtes)(struct cxl_context *ctx);
638*4882a593Smuzhiyun void (*debugfs_add_adapter_regs)(struct cxl *adapter, struct dentry *dir);
639*4882a593Smuzhiyun void (*debugfs_add_afu_regs)(struct cxl_afu *afu, struct dentry *dir);
640*4882a593Smuzhiyun void (*psl_irq_dump_registers)(struct cxl_context *ctx);
641*4882a593Smuzhiyun void (*err_irq_dump_registers)(struct cxl *adapter);
642*4882a593Smuzhiyun void (*debugfs_stop_trace)(struct cxl *adapter);
643*4882a593Smuzhiyun void (*write_timebase_ctrl)(struct cxl *adapter);
644*4882a593Smuzhiyun u64 (*timebase_read)(struct cxl *adapter);
645*4882a593Smuzhiyun int capi_mode;
646*4882a593Smuzhiyun bool needs_reset_before_disable;
647*4882a593Smuzhiyun };
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun struct cxl_native {
650*4882a593Smuzhiyun u64 afu_desc_off;
651*4882a593Smuzhiyun u64 afu_desc_size;
652*4882a593Smuzhiyun void __iomem *p1_mmio;
653*4882a593Smuzhiyun void __iomem *p2_mmio;
654*4882a593Smuzhiyun irq_hw_number_t err_hwirq;
655*4882a593Smuzhiyun unsigned int err_virq;
656*4882a593Smuzhiyun u64 ps_off;
657*4882a593Smuzhiyun bool no_data_cache; /* set if no data cache on the card */
658*4882a593Smuzhiyun const struct cxl_service_layer_ops *sl_ops;
659*4882a593Smuzhiyun };
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun struct cxl_guest {
662*4882a593Smuzhiyun struct platform_device *pdev;
663*4882a593Smuzhiyun int irq_nranges;
664*4882a593Smuzhiyun struct cdev cdev;
665*4882a593Smuzhiyun irq_hw_number_t irq_base_offset;
666*4882a593Smuzhiyun struct irq_avail *irq_avail;
667*4882a593Smuzhiyun spinlock_t irq_alloc_lock;
668*4882a593Smuzhiyun u64 handle;
669*4882a593Smuzhiyun char *status;
670*4882a593Smuzhiyun u16 vendor;
671*4882a593Smuzhiyun u16 device;
672*4882a593Smuzhiyun u16 subsystem_vendor;
673*4882a593Smuzhiyun u16 subsystem;
674*4882a593Smuzhiyun };
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun struct cxl {
677*4882a593Smuzhiyun struct cxl_native *native;
678*4882a593Smuzhiyun struct cxl_guest *guest;
679*4882a593Smuzhiyun spinlock_t afu_list_lock;
680*4882a593Smuzhiyun struct cxl_afu *afu[CXL_MAX_SLICES];
681*4882a593Smuzhiyun struct device dev;
682*4882a593Smuzhiyun struct dentry *trace;
683*4882a593Smuzhiyun struct dentry *psl_err_chk;
684*4882a593Smuzhiyun struct dentry *debugfs;
685*4882a593Smuzhiyun char *irq_name;
686*4882a593Smuzhiyun struct bin_attribute cxl_attr;
687*4882a593Smuzhiyun int adapter_num;
688*4882a593Smuzhiyun int user_irqs;
689*4882a593Smuzhiyun u64 ps_size;
690*4882a593Smuzhiyun u16 psl_rev;
691*4882a593Smuzhiyun u16 base_image;
692*4882a593Smuzhiyun u8 vsec_status;
693*4882a593Smuzhiyun u8 caia_major;
694*4882a593Smuzhiyun u8 caia_minor;
695*4882a593Smuzhiyun u8 slices;
696*4882a593Smuzhiyun bool user_image_loaded;
697*4882a593Smuzhiyun bool perst_loads_image;
698*4882a593Smuzhiyun bool perst_select_user;
699*4882a593Smuzhiyun bool perst_same_image;
700*4882a593Smuzhiyun bool psl_timebase_synced;
701*4882a593Smuzhiyun bool tunneled_ops_supported;
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /*
704*4882a593Smuzhiyun * number of contexts mapped on to this card. Possible values are:
705*4882a593Smuzhiyun * >0: Number of contexts mapped and new one can be mapped.
706*4882a593Smuzhiyun * 0: No active contexts and new ones can be mapped.
707*4882a593Smuzhiyun * -1: No contexts mapped and new ones cannot be mapped.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun atomic_t contexts_num;
710*4882a593Smuzhiyun };
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun int cxl_pci_alloc_one_irq(struct cxl *adapter);
713*4882a593Smuzhiyun void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq);
714*4882a593Smuzhiyun int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num);
715*4882a593Smuzhiyun void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter);
716*4882a593Smuzhiyun int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq);
717*4882a593Smuzhiyun int cxl_update_image_control(struct cxl *adapter);
718*4882a593Smuzhiyun int cxl_pci_reset(struct cxl *adapter);
719*4882a593Smuzhiyun void cxl_pci_release_afu(struct device *dev);
720*4882a593Smuzhiyun ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* common == phyp + powernv - CAIA 1&2 */
723*4882a593Smuzhiyun struct cxl_process_element_common {
724*4882a593Smuzhiyun __be32 tid;
725*4882a593Smuzhiyun __be32 pid;
726*4882a593Smuzhiyun __be64 csrp;
727*4882a593Smuzhiyun union {
728*4882a593Smuzhiyun struct {
729*4882a593Smuzhiyun __be64 aurp0;
730*4882a593Smuzhiyun __be64 aurp1;
731*4882a593Smuzhiyun __be64 sstp0;
732*4882a593Smuzhiyun __be64 sstp1;
733*4882a593Smuzhiyun } psl8; /* CAIA 1 */
734*4882a593Smuzhiyun struct {
735*4882a593Smuzhiyun u8 reserved2[8];
736*4882a593Smuzhiyun u8 reserved3[8];
737*4882a593Smuzhiyun u8 reserved4[8];
738*4882a593Smuzhiyun u8 reserved5[8];
739*4882a593Smuzhiyun } psl9; /* CAIA 2 */
740*4882a593Smuzhiyun } u;
741*4882a593Smuzhiyun __be64 amr;
742*4882a593Smuzhiyun u8 reserved6[4];
743*4882a593Smuzhiyun __be64 wed;
744*4882a593Smuzhiyun } __packed;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /* just powernv - CAIA 1&2 */
747*4882a593Smuzhiyun struct cxl_process_element {
748*4882a593Smuzhiyun __be64 sr;
749*4882a593Smuzhiyun __be64 SPOffset;
750*4882a593Smuzhiyun union {
751*4882a593Smuzhiyun __be64 sdr; /* CAIA 1 */
752*4882a593Smuzhiyun u8 reserved1[8]; /* CAIA 2 */
753*4882a593Smuzhiyun } u;
754*4882a593Smuzhiyun __be64 haurp;
755*4882a593Smuzhiyun __be32 ctxtime;
756*4882a593Smuzhiyun __be16 ivte_offsets[4];
757*4882a593Smuzhiyun __be16 ivte_ranges[4];
758*4882a593Smuzhiyun __be32 lpid;
759*4882a593Smuzhiyun struct cxl_process_element_common common;
760*4882a593Smuzhiyun __be32 software_state;
761*4882a593Smuzhiyun } __packed;
762*4882a593Smuzhiyun
cxl_adapter_link_ok(struct cxl * cxl,struct cxl_afu * afu)763*4882a593Smuzhiyun static inline bool cxl_adapter_link_ok(struct cxl *cxl, struct cxl_afu *afu)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun struct pci_dev *pdev;
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun if (cpu_has_feature(CPU_FTR_HVMODE)) {
768*4882a593Smuzhiyun pdev = to_pci_dev(cxl->dev.parent);
769*4882a593Smuzhiyun return !pci_channel_offline(pdev);
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun return true;
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun
_cxl_p1_addr(struct cxl * cxl,cxl_p1_reg_t reg)774*4882a593Smuzhiyun static inline void __iomem *_cxl_p1_addr(struct cxl *cxl, cxl_p1_reg_t reg)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun WARN_ON(!cpu_has_feature(CPU_FTR_HVMODE));
777*4882a593Smuzhiyun return cxl->native->p1_mmio + cxl_reg_off(reg);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
cxl_p1_write(struct cxl * cxl,cxl_p1_reg_t reg,u64 val)780*4882a593Smuzhiyun static inline void cxl_p1_write(struct cxl *cxl, cxl_p1_reg_t reg, u64 val)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(cxl, NULL)))
783*4882a593Smuzhiyun out_be64(_cxl_p1_addr(cxl, reg), val);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
cxl_p1_read(struct cxl * cxl,cxl_p1_reg_t reg)786*4882a593Smuzhiyun static inline u64 cxl_p1_read(struct cxl *cxl, cxl_p1_reg_t reg)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(cxl, NULL)))
789*4882a593Smuzhiyun return in_be64(_cxl_p1_addr(cxl, reg));
790*4882a593Smuzhiyun else
791*4882a593Smuzhiyun return ~0ULL;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
_cxl_p1n_addr(struct cxl_afu * afu,cxl_p1n_reg_t reg)794*4882a593Smuzhiyun static inline void __iomem *_cxl_p1n_addr(struct cxl_afu *afu, cxl_p1n_reg_t reg)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun WARN_ON(!cpu_has_feature(CPU_FTR_HVMODE));
797*4882a593Smuzhiyun return afu->native->p1n_mmio + cxl_reg_off(reg);
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
cxl_p1n_write(struct cxl_afu * afu,cxl_p1n_reg_t reg,u64 val)800*4882a593Smuzhiyun static inline void cxl_p1n_write(struct cxl_afu *afu, cxl_p1n_reg_t reg, u64 val)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
803*4882a593Smuzhiyun out_be64(_cxl_p1n_addr(afu, reg), val);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun
cxl_p1n_read(struct cxl_afu * afu,cxl_p1n_reg_t reg)806*4882a593Smuzhiyun static inline u64 cxl_p1n_read(struct cxl_afu *afu, cxl_p1n_reg_t reg)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
809*4882a593Smuzhiyun return in_be64(_cxl_p1n_addr(afu, reg));
810*4882a593Smuzhiyun else
811*4882a593Smuzhiyun return ~0ULL;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
_cxl_p2n_addr(struct cxl_afu * afu,cxl_p2n_reg_t reg)814*4882a593Smuzhiyun static inline void __iomem *_cxl_p2n_addr(struct cxl_afu *afu, cxl_p2n_reg_t reg)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun return afu->p2n_mmio + cxl_reg_off(reg);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
cxl_p2n_write(struct cxl_afu * afu,cxl_p2n_reg_t reg,u64 val)819*4882a593Smuzhiyun static inline void cxl_p2n_write(struct cxl_afu *afu, cxl_p2n_reg_t reg, u64 val)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
822*4882a593Smuzhiyun out_be64(_cxl_p2n_addr(afu, reg), val);
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
cxl_p2n_read(struct cxl_afu * afu,cxl_p2n_reg_t reg)825*4882a593Smuzhiyun static inline u64 cxl_p2n_read(struct cxl_afu *afu, cxl_p2n_reg_t reg)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
828*4882a593Smuzhiyun return in_be64(_cxl_p2n_addr(afu, reg));
829*4882a593Smuzhiyun else
830*4882a593Smuzhiyun return ~0ULL;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
cxl_is_power8(void)833*4882a593Smuzhiyun static inline bool cxl_is_power8(void)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun if ((pvr_version_is(PVR_POWER8E)) ||
836*4882a593Smuzhiyun (pvr_version_is(PVR_POWER8NVL)) ||
837*4882a593Smuzhiyun (pvr_version_is(PVR_POWER8)))
838*4882a593Smuzhiyun return true;
839*4882a593Smuzhiyun return false;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun
cxl_is_power9(void)842*4882a593Smuzhiyun static inline bool cxl_is_power9(void)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun if (pvr_version_is(PVR_POWER9))
845*4882a593Smuzhiyun return true;
846*4882a593Smuzhiyun return false;
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
850*4882a593Smuzhiyun loff_t off, size_t count);
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun struct cxl_calls {
854*4882a593Smuzhiyun void (*cxl_slbia)(struct mm_struct *mm);
855*4882a593Smuzhiyun struct module *owner;
856*4882a593Smuzhiyun };
857*4882a593Smuzhiyun int register_cxl_calls(struct cxl_calls *calls);
858*4882a593Smuzhiyun void unregister_cxl_calls(struct cxl_calls *calls);
859*4882a593Smuzhiyun int cxl_update_properties(struct device_node *dn, struct property *new_prop);
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun void cxl_remove_adapter_nr(struct cxl *adapter);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun void cxl_release_spa(struct cxl_afu *afu);
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun dev_t cxl_get_dev(void);
866*4882a593Smuzhiyun int cxl_file_init(void);
867*4882a593Smuzhiyun void cxl_file_exit(void);
868*4882a593Smuzhiyun int cxl_register_adapter(struct cxl *adapter);
869*4882a593Smuzhiyun int cxl_register_afu(struct cxl_afu *afu);
870*4882a593Smuzhiyun int cxl_chardev_d_afu_add(struct cxl_afu *afu);
871*4882a593Smuzhiyun int cxl_chardev_m_afu_add(struct cxl_afu *afu);
872*4882a593Smuzhiyun int cxl_chardev_s_afu_add(struct cxl_afu *afu);
873*4882a593Smuzhiyun void cxl_chardev_afu_remove(struct cxl_afu *afu);
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun void cxl_context_detach_all(struct cxl_afu *afu);
876*4882a593Smuzhiyun void cxl_context_free(struct cxl_context *ctx);
877*4882a593Smuzhiyun void cxl_context_detach(struct cxl_context *ctx);
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun int cxl_sysfs_adapter_add(struct cxl *adapter);
880*4882a593Smuzhiyun void cxl_sysfs_adapter_remove(struct cxl *adapter);
881*4882a593Smuzhiyun int cxl_sysfs_afu_add(struct cxl_afu *afu);
882*4882a593Smuzhiyun void cxl_sysfs_afu_remove(struct cxl_afu *afu);
883*4882a593Smuzhiyun int cxl_sysfs_afu_m_add(struct cxl_afu *afu);
884*4882a593Smuzhiyun void cxl_sysfs_afu_m_remove(struct cxl_afu *afu);
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun struct cxl *cxl_alloc_adapter(void);
887*4882a593Smuzhiyun struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
888*4882a593Smuzhiyun int cxl_afu_select_best_mode(struct cxl_afu *afu);
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun int cxl_native_register_psl_irq(struct cxl_afu *afu);
891*4882a593Smuzhiyun void cxl_native_release_psl_irq(struct cxl_afu *afu);
892*4882a593Smuzhiyun int cxl_native_register_psl_err_irq(struct cxl *adapter);
893*4882a593Smuzhiyun void cxl_native_release_psl_err_irq(struct cxl *adapter);
894*4882a593Smuzhiyun int cxl_native_register_serr_irq(struct cxl_afu *afu);
895*4882a593Smuzhiyun void cxl_native_release_serr_irq(struct cxl_afu *afu);
896*4882a593Smuzhiyun int afu_register_irqs(struct cxl_context *ctx, u32 count);
897*4882a593Smuzhiyun void afu_release_irqs(struct cxl_context *ctx, void *cookie);
898*4882a593Smuzhiyun void afu_irq_name_free(struct cxl_context *ctx);
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun int cxl_attach_afu_directed_psl9(struct cxl_context *ctx, u64 wed, u64 amr);
901*4882a593Smuzhiyun int cxl_attach_afu_directed_psl8(struct cxl_context *ctx, u64 wed, u64 amr);
902*4882a593Smuzhiyun int cxl_activate_dedicated_process_psl9(struct cxl_afu *afu);
903*4882a593Smuzhiyun int cxl_activate_dedicated_process_psl8(struct cxl_afu *afu);
904*4882a593Smuzhiyun int cxl_attach_dedicated_process_psl9(struct cxl_context *ctx, u64 wed, u64 amr);
905*4882a593Smuzhiyun int cxl_attach_dedicated_process_psl8(struct cxl_context *ctx, u64 wed, u64 amr);
906*4882a593Smuzhiyun void cxl_update_dedicated_ivtes_psl9(struct cxl_context *ctx);
907*4882a593Smuzhiyun void cxl_update_dedicated_ivtes_psl8(struct cxl_context *ctx);
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun void cxl_debugfs_init(void);
912*4882a593Smuzhiyun void cxl_debugfs_exit(void);
913*4882a593Smuzhiyun void cxl_debugfs_adapter_add(struct cxl *adapter);
914*4882a593Smuzhiyun void cxl_debugfs_adapter_remove(struct cxl *adapter);
915*4882a593Smuzhiyun void cxl_debugfs_afu_add(struct cxl_afu *afu);
916*4882a593Smuzhiyun void cxl_debugfs_afu_remove(struct cxl_afu *afu);
917*4882a593Smuzhiyun void cxl_debugfs_add_adapter_regs_psl9(struct cxl *adapter, struct dentry *dir);
918*4882a593Smuzhiyun void cxl_debugfs_add_adapter_regs_psl8(struct cxl *adapter, struct dentry *dir);
919*4882a593Smuzhiyun void cxl_debugfs_add_afu_regs_psl9(struct cxl_afu *afu, struct dentry *dir);
920*4882a593Smuzhiyun void cxl_debugfs_add_afu_regs_psl8(struct cxl_afu *afu, struct dentry *dir);
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun #else /* CONFIG_DEBUG_FS */
923*4882a593Smuzhiyun
cxl_debugfs_init(void)924*4882a593Smuzhiyun static inline void __init cxl_debugfs_init(void)
925*4882a593Smuzhiyun {
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun
cxl_debugfs_exit(void)928*4882a593Smuzhiyun static inline void cxl_debugfs_exit(void)
929*4882a593Smuzhiyun {
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun
cxl_debugfs_adapter_add(struct cxl * adapter)932*4882a593Smuzhiyun static inline void cxl_debugfs_adapter_add(struct cxl *adapter)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
cxl_debugfs_adapter_remove(struct cxl * adapter)936*4882a593Smuzhiyun static inline void cxl_debugfs_adapter_remove(struct cxl *adapter)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun
cxl_debugfs_afu_add(struct cxl_afu * afu)940*4882a593Smuzhiyun static inline void cxl_debugfs_afu_add(struct cxl_afu *afu)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
cxl_debugfs_afu_remove(struct cxl_afu * afu)944*4882a593Smuzhiyun static inline void cxl_debugfs_afu_remove(struct cxl_afu *afu)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun
cxl_debugfs_add_adapter_regs_psl9(struct cxl * adapter,struct dentry * dir)948*4882a593Smuzhiyun static inline void cxl_debugfs_add_adapter_regs_psl9(struct cxl *adapter,
949*4882a593Smuzhiyun struct dentry *dir)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun
cxl_debugfs_add_adapter_regs_psl8(struct cxl * adapter,struct dentry * dir)953*4882a593Smuzhiyun static inline void cxl_debugfs_add_adapter_regs_psl8(struct cxl *adapter,
954*4882a593Smuzhiyun struct dentry *dir)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun
cxl_debugfs_add_afu_regs_psl9(struct cxl_afu * afu,struct dentry * dir)958*4882a593Smuzhiyun static inline void cxl_debugfs_add_afu_regs_psl9(struct cxl_afu *afu, struct dentry *dir)
959*4882a593Smuzhiyun {
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun
cxl_debugfs_add_afu_regs_psl8(struct cxl_afu * afu,struct dentry * dir)962*4882a593Smuzhiyun static inline void cxl_debugfs_add_afu_regs_psl8(struct cxl_afu *afu, struct dentry *dir)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun void cxl_handle_fault(struct work_struct *work);
969*4882a593Smuzhiyun void cxl_prefault(struct cxl_context *ctx, u64 wed);
970*4882a593Smuzhiyun int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar);
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun struct cxl *get_cxl_adapter(int num);
973*4882a593Smuzhiyun int cxl_alloc_sst(struct cxl_context *ctx);
974*4882a593Smuzhiyun void cxl_dump_debug_buffer(void *addr, size_t size);
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun void init_cxl_native(void);
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun struct cxl_context *cxl_context_alloc(void);
979*4882a593Smuzhiyun int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master);
980*4882a593Smuzhiyun void cxl_context_set_mapping(struct cxl_context *ctx,
981*4882a593Smuzhiyun struct address_space *mapping);
982*4882a593Smuzhiyun void cxl_context_free(struct cxl_context *ctx);
983*4882a593Smuzhiyun int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma);
984*4882a593Smuzhiyun unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
985*4882a593Smuzhiyun irq_handler_t handler, void *cookie, const char *name);
986*4882a593Smuzhiyun void cxl_unmap_irq(unsigned int virq, void *cookie);
987*4882a593Smuzhiyun int __detach_context(struct cxl_context *ctx);
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /*
990*4882a593Smuzhiyun * This must match the layout of the H_COLLECT_CA_INT_INFO retbuf defined
991*4882a593Smuzhiyun * in PAPR.
992*4882a593Smuzhiyun * Field pid_tid is now 'reserved' because it's no more used on bare-metal.
993*4882a593Smuzhiyun * On a guest environment, PSL_PID_An is located on the upper 32 bits and
994*4882a593Smuzhiyun * PSL_TID_An register in the lower 32 bits.
995*4882a593Smuzhiyun */
996*4882a593Smuzhiyun struct cxl_irq_info {
997*4882a593Smuzhiyun u64 dsisr;
998*4882a593Smuzhiyun u64 dar;
999*4882a593Smuzhiyun u64 dsr;
1000*4882a593Smuzhiyun u64 reserved;
1001*4882a593Smuzhiyun u64 afu_err;
1002*4882a593Smuzhiyun u64 errstat;
1003*4882a593Smuzhiyun u64 proc_handle;
1004*4882a593Smuzhiyun u64 padding[2]; /* to match the expected retbuf size for plpar_hcall9 */
1005*4882a593Smuzhiyun };
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun void cxl_assign_psn_space(struct cxl_context *ctx);
1008*4882a593Smuzhiyun int cxl_invalidate_all_psl9(struct cxl *adapter);
1009*4882a593Smuzhiyun int cxl_invalidate_all_psl8(struct cxl *adapter);
1010*4882a593Smuzhiyun irqreturn_t cxl_irq_psl9(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
1011*4882a593Smuzhiyun irqreturn_t cxl_irq_psl8(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
1012*4882a593Smuzhiyun irqreturn_t cxl_fail_irq_psl(struct cxl_afu *afu, struct cxl_irq_info *irq_info);
1013*4882a593Smuzhiyun int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
1014*4882a593Smuzhiyun void *cookie, irq_hw_number_t *dest_hwirq,
1015*4882a593Smuzhiyun unsigned int *dest_virq, const char *name);
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun int cxl_check_error(struct cxl_afu *afu);
1018*4882a593Smuzhiyun int cxl_afu_slbia(struct cxl_afu *afu);
1019*4882a593Smuzhiyun int cxl_data_cache_flush(struct cxl *adapter);
1020*4882a593Smuzhiyun int cxl_afu_disable(struct cxl_afu *afu);
1021*4882a593Smuzhiyun int cxl_psl_purge(struct cxl_afu *afu);
1022*4882a593Smuzhiyun int cxl_calc_capp_routing(struct pci_dev *dev, u64 *chipid,
1023*4882a593Smuzhiyun u32 *phb_index, u64 *capp_unit_id);
1024*4882a593Smuzhiyun int cxl_slot_is_switched(struct pci_dev *dev);
1025*4882a593Smuzhiyun int cxl_get_xsl9_dsnctl(struct pci_dev *dev, u64 capp_unit_id, u64 *reg);
1026*4882a593Smuzhiyun u64 cxl_calculate_sr(bool master, bool kernel, bool real_mode, bool p9);
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun void cxl_native_irq_dump_regs_psl9(struct cxl_context *ctx);
1029*4882a593Smuzhiyun void cxl_native_irq_dump_regs_psl8(struct cxl_context *ctx);
1030*4882a593Smuzhiyun void cxl_native_err_irq_dump_regs_psl8(struct cxl *adapter);
1031*4882a593Smuzhiyun void cxl_native_err_irq_dump_regs_psl9(struct cxl *adapter);
1032*4882a593Smuzhiyun int cxl_pci_vphb_add(struct cxl_afu *afu);
1033*4882a593Smuzhiyun void cxl_pci_vphb_remove(struct cxl_afu *afu);
1034*4882a593Smuzhiyun void cxl_release_mapping(struct cxl_context *ctx);
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun extern struct pci_driver cxl_pci_driver;
1037*4882a593Smuzhiyun extern struct platform_driver cxl_of_driver;
1038*4882a593Smuzhiyun int afu_allocate_irqs(struct cxl_context *ctx, u32 count);
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun int afu_open(struct inode *inode, struct file *file);
1041*4882a593Smuzhiyun int afu_release(struct inode *inode, struct file *file);
1042*4882a593Smuzhiyun long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1043*4882a593Smuzhiyun int afu_mmap(struct file *file, struct vm_area_struct *vm);
1044*4882a593Smuzhiyun __poll_t afu_poll(struct file *file, struct poll_table_struct *poll);
1045*4882a593Smuzhiyun ssize_t afu_read(struct file *file, char __user *buf, size_t count, loff_t *off);
1046*4882a593Smuzhiyun extern const struct file_operations afu_fops;
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *dev);
1049*4882a593Smuzhiyun void cxl_guest_remove_adapter(struct cxl *adapter);
1050*4882a593Smuzhiyun int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np);
1051*4882a593Smuzhiyun int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np);
1052*4882a593Smuzhiyun ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
1053*4882a593Smuzhiyun ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len);
1054*4882a593Smuzhiyun int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np);
1055*4882a593Smuzhiyun void cxl_guest_remove_afu(struct cxl_afu *afu);
1056*4882a593Smuzhiyun int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np);
1057*4882a593Smuzhiyun int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *afu_np);
1058*4882a593Smuzhiyun int cxl_guest_add_chardev(struct cxl *adapter);
1059*4882a593Smuzhiyun void cxl_guest_remove_chardev(struct cxl *adapter);
1060*4882a593Smuzhiyun void cxl_guest_reload_module(struct cxl *adapter);
1061*4882a593Smuzhiyun int cxl_of_probe(struct platform_device *pdev);
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun struct cxl_backend_ops {
1064*4882a593Smuzhiyun struct module *module;
1065*4882a593Smuzhiyun int (*adapter_reset)(struct cxl *adapter);
1066*4882a593Smuzhiyun int (*alloc_one_irq)(struct cxl *adapter);
1067*4882a593Smuzhiyun void (*release_one_irq)(struct cxl *adapter, int hwirq);
1068*4882a593Smuzhiyun int (*alloc_irq_ranges)(struct cxl_irq_ranges *irqs,
1069*4882a593Smuzhiyun struct cxl *adapter, unsigned int num);
1070*4882a593Smuzhiyun void (*release_irq_ranges)(struct cxl_irq_ranges *irqs,
1071*4882a593Smuzhiyun struct cxl *adapter);
1072*4882a593Smuzhiyun int (*setup_irq)(struct cxl *adapter, unsigned int hwirq,
1073*4882a593Smuzhiyun unsigned int virq);
1074*4882a593Smuzhiyun irqreturn_t (*handle_psl_slice_error)(struct cxl_context *ctx,
1075*4882a593Smuzhiyun u64 dsisr, u64 errstat);
1076*4882a593Smuzhiyun irqreturn_t (*psl_interrupt)(int irq, void *data);
1077*4882a593Smuzhiyun int (*ack_irq)(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
1078*4882a593Smuzhiyun void (*irq_wait)(struct cxl_context *ctx);
1079*4882a593Smuzhiyun int (*attach_process)(struct cxl_context *ctx, bool kernel,
1080*4882a593Smuzhiyun u64 wed, u64 amr);
1081*4882a593Smuzhiyun int (*detach_process)(struct cxl_context *ctx);
1082*4882a593Smuzhiyun void (*update_ivtes)(struct cxl_context *ctx);
1083*4882a593Smuzhiyun bool (*support_attributes)(const char *attr_name, enum cxl_attrs type);
1084*4882a593Smuzhiyun bool (*link_ok)(struct cxl *cxl, struct cxl_afu *afu);
1085*4882a593Smuzhiyun void (*release_afu)(struct device *dev);
1086*4882a593Smuzhiyun ssize_t (*afu_read_err_buffer)(struct cxl_afu *afu, char *buf,
1087*4882a593Smuzhiyun loff_t off, size_t count);
1088*4882a593Smuzhiyun int (*afu_check_and_enable)(struct cxl_afu *afu);
1089*4882a593Smuzhiyun int (*afu_activate_mode)(struct cxl_afu *afu, int mode);
1090*4882a593Smuzhiyun int (*afu_deactivate_mode)(struct cxl_afu *afu, int mode);
1091*4882a593Smuzhiyun int (*afu_reset)(struct cxl_afu *afu);
1092*4882a593Smuzhiyun int (*afu_cr_read8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8 *val);
1093*4882a593Smuzhiyun int (*afu_cr_read16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 *val);
1094*4882a593Smuzhiyun int (*afu_cr_read32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 *val);
1095*4882a593Smuzhiyun int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset, u64 *val);
1096*4882a593Smuzhiyun int (*afu_cr_write8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8 val);
1097*4882a593Smuzhiyun int (*afu_cr_write16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 val);
1098*4882a593Smuzhiyun int (*afu_cr_write32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 val);
1099*4882a593Smuzhiyun ssize_t (*read_adapter_vpd)(struct cxl *adapter, void *buf, size_t count);
1100*4882a593Smuzhiyun };
1101*4882a593Smuzhiyun extern const struct cxl_backend_ops cxl_native_ops;
1102*4882a593Smuzhiyun extern const struct cxl_backend_ops cxl_guest_ops;
1103*4882a593Smuzhiyun extern const struct cxl_backend_ops *cxl_ops;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun /* check if the given pci_dev is on the the cxl vphb bus */
1106*4882a593Smuzhiyun bool cxl_pci_is_vphb_device(struct pci_dev *dev);
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /* decode AFU error bits in the PSL register PSL_SERR_An */
1109*4882a593Smuzhiyun void cxl_afu_decode_psl_serr(struct cxl_afu *afu, u64 serr);
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun /*
1112*4882a593Smuzhiyun * Increments the number of attached contexts on an adapter.
1113*4882a593Smuzhiyun * In case an adapter_context_lock is taken the return -EBUSY.
1114*4882a593Smuzhiyun */
1115*4882a593Smuzhiyun int cxl_adapter_context_get(struct cxl *adapter);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /* Decrements the number of attached contexts on an adapter */
1118*4882a593Smuzhiyun void cxl_adapter_context_put(struct cxl *adapter);
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun /* If no active contexts then prevents contexts from being attached */
1121*4882a593Smuzhiyun int cxl_adapter_context_lock(struct cxl *adapter);
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /* Unlock the contexts-lock if taken. Warn and force unlock otherwise */
1124*4882a593Smuzhiyun void cxl_adapter_context_unlock(struct cxl *adapter);
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun /* Increases the reference count to "struct mm_struct" */
1127*4882a593Smuzhiyun void cxl_context_mm_count_get(struct cxl_context *ctx);
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun /* Decrements the reference count to "struct mm_struct" */
1130*4882a593Smuzhiyun void cxl_context_mm_count_put(struct cxl_context *ctx);
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun #endif
1133