xref: /openbmc/qemu/include/hw/cxl/cxl_component.h (revision a185ff05fed2aa445f81d16a472e809d2cbea91b)
1 /*
2  * QEMU CXL Component
3  *
4  * Copyright (c) 2020 Intel
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2. See the
7  * COPYING file in the top-level directory.
8  */
9 
10 #ifndef CXL_COMPONENT_H
11 #define CXL_COMPONENT_H
12 
13 /* CXL 2.0 - 8.2.4 */
14 #define CXL2_COMPONENT_IO_REGION_SIZE 0x1000
15 #define CXL2_COMPONENT_CM_REGION_SIZE 0x1000
16 #define CXL2_COMPONENT_BLOCK_SIZE 0x10000
17 
18 #include "qemu/range.h"
19 #include "hw/cxl/cxl_cdat.h"
20 #include "hw/register.h"
21 #include "qapi/error.h"
22 
23 enum reg_type {
24     CXL2_DEVICE,
25     CXL2_TYPE3_DEVICE,
26     CXL2_LOGICAL_DEVICE,
27     CXL2_ROOT_PORT,
28     CXL2_UPSTREAM_PORT,
29     CXL2_DOWNSTREAM_PORT,
30     CXL3_SWITCH_MAILBOX_CCI,
31 };
32 
33 /*
34  * Capability registers are defined at the top of the CXL.cache/mem region and
35  * are packed. For our purposes we will always define the caps in the same
36  * order.
37  * CXL 2.0 - 8.2.5 Table 142 for details.
38  */
39 
40 /* CXL 2.0 - 8.2.5.1 */
41 REG32(CXL_CAPABILITY_HEADER, 0)
42     FIELD(CXL_CAPABILITY_HEADER, ID, 0, 16)
43     FIELD(CXL_CAPABILITY_HEADER, VERSION, 16, 4)
44     FIELD(CXL_CAPABILITY_HEADER, CACHE_MEM_VERSION, 20, 4)
45     FIELD(CXL_CAPABILITY_HEADER, ARRAY_SIZE, 24, 8)
46 
47 #define CXLx_CAPABILITY_HEADER(type, offset)                  \
48     REG32(CXL_##type##_CAPABILITY_HEADER, offset)             \
49         FIELD(CXL_##type##_CAPABILITY_HEADER, ID, 0, 16)      \
50         FIELD(CXL_##type##_CAPABILITY_HEADER, VERSION, 16, 4) \
51         FIELD(CXL_##type##_CAPABILITY_HEADER, PTR, 20, 12)
52 CXLx_CAPABILITY_HEADER(RAS, 0x4)
53 CXLx_CAPABILITY_HEADER(LINK, 0x8)
54 CXLx_CAPABILITY_HEADER(HDM, 0xc)
55 CXLx_CAPABILITY_HEADER(EXTSEC, 0x10)
56 CXLx_CAPABILITY_HEADER(SNOOP, 0x14)
57 
58 /*
59  * Capability structures contain the actual registers that the CXL component
60  * implements. Some of these are specific to certain types of components, but
61  * this implementation leaves enough space regardless.
62  */
63 
64 /* CXL r3.1 Section 8.2.4.17: CXL RAS Capability Structure */
65 #define CXL_RAS_CAPABILITY_VERSION 3
66 /* Give ample space for caps before this */
67 #define CXL_RAS_REGISTERS_OFFSET 0x80
68 #define CXL_RAS_REGISTERS_SIZE   0x58
69 REG32(CXL_RAS_UNC_ERR_STATUS, CXL_RAS_REGISTERS_OFFSET)
70 #define CXL_RAS_UNC_ERR_CACHE_DATA_PARITY 0
71 #define CXL_RAS_UNC_ERR_CACHE_ADDRESS_PARITY 1
72 #define CXL_RAS_UNC_ERR_CACHE_BE_PARITY 2
73 #define CXL_RAS_UNC_ERR_CACHE_DATA_ECC 3
74 #define CXL_RAS_UNC_ERR_MEM_DATA_PARITY 4
75 #define CXL_RAS_UNC_ERR_MEM_ADDRESS_PARITY 5
76 #define CXL_RAS_UNC_ERR_MEM_BE_PARITY 6
77 #define CXL_RAS_UNC_ERR_MEM_DATA_ECC 7
78 #define CXL_RAS_UNC_ERR_REINIT_THRESHOLD 8
79 #define CXL_RAS_UNC_ERR_RSVD_ENCODING 9
80 #define CXL_RAS_UNC_ERR_POISON_RECEIVED 10
81 #define CXL_RAS_UNC_ERR_RECEIVER_OVERFLOW 11
82 #define CXL_RAS_UNC_ERR_INTERNAL 14
83 #define CXL_RAS_UNC_ERR_CXL_IDE_TX 15
84 #define CXL_RAS_UNC_ERR_CXL_IDE_RX 16
85 #define CXL_RAS_UNC_ERR_CXL_UNUSED 63 /* Magic value */
86 REG32(CXL_RAS_UNC_ERR_MASK, CXL_RAS_REGISTERS_OFFSET + 0x4)
87 REG32(CXL_RAS_UNC_ERR_SEVERITY, CXL_RAS_REGISTERS_OFFSET + 0x8)
88 REG32(CXL_RAS_COR_ERR_STATUS, CXL_RAS_REGISTERS_OFFSET + 0xc)
89 #define CXL_RAS_COR_ERR_CACHE_DATA_ECC 0
90 #define CXL_RAS_COR_ERR_MEM_DATA_ECC 1
91 #define CXL_RAS_COR_ERR_CRC_THRESHOLD 2
92 #define CXL_RAS_COR_ERR_RETRY_THRESHOLD 3
93 #define CXL_RAS_COR_ERR_CACHE_POISON_RECEIVED 4
94 #define CXL_RAS_COR_ERR_MEM_POISON_RECEIVED 5
95 #define CXL_RAS_COR_ERR_PHYSICAL 6
96 REG32(CXL_RAS_COR_ERR_MASK, CXL_RAS_REGISTERS_OFFSET + 0x10)
97 REG32(CXL_RAS_ERR_CAP_CTRL, CXL_RAS_REGISTERS_OFFSET + 0x14)
98     FIELD(CXL_RAS_ERR_CAP_CTRL, FIRST_ERROR_POINTER, 0, 6)
99     FIELD(CXL_RAS_ERR_CAP_CTRL, MULTIPLE_HEADER_RECORDING_CAP, 9, 1)
100     FIELD(CXL_RAS_ERR_POISON_ENABLED, POISON_ENABLED, 13, 1)
101 REG32(CXL_RAS_ERR_HEADER0, CXL_RAS_REGISTERS_OFFSET + 0x18)
102 #define CXL_RAS_ERR_HEADER_NUM 32
103 /* Offset 0x18 - 0x58 reserved for RAS logs */
104 
105 /* 8.2.5.10 - CXL Security Capability Structure */
106 #define CXL_SEC_REGISTERS_OFFSET \
107     (CXL_RAS_REGISTERS_OFFSET + CXL_RAS_REGISTERS_SIZE)
108 #define CXL_SEC_REGISTERS_SIZE   0 /* We don't implement 1.1 downstream ports */
109 
110 /* CXL r3.1 Section 8.2.4.19: CXL Link Capability Structure */
111 #define CXL_LINK_REGISTERS_OFFSET \
112     (CXL_SEC_REGISTERS_OFFSET + CXL_SEC_REGISTERS_SIZE)
113 #define CXL_LINK_REGISTERS_SIZE   0x50
114 
115 /* CXL r3.1 Section 8.2.4.20: CXL HDM Decoder Capability Structure */
116 #define HDM_DECODE_MAX 10 /* Maximum decoders for Devices */
117 #define CXL_HDM_CAPABILITY_VERSION 3
118 #define CXL_HDM_REGISTERS_OFFSET \
119     (CXL_LINK_REGISTERS_OFFSET + CXL_LINK_REGISTERS_SIZE)
120 #define CXL_HDM_REGISTERS_SIZE (0x10 + 0x20 * HDM_DECODE_MAX)
121 #define HDM_DECODER_INIT(n)                                                    \
122   REG32(CXL_HDM_DECODER##n##_BASE_LO,                                          \
123         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x10)                          \
124             FIELD(CXL_HDM_DECODER##n##_BASE_LO, L, 28, 4)                      \
125   REG32(CXL_HDM_DECODER##n##_BASE_HI,                                          \
126         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x14)                          \
127   REG32(CXL_HDM_DECODER##n##_SIZE_LO,                                          \
128         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x18)                          \
129   REG32(CXL_HDM_DECODER##n##_SIZE_HI,                                          \
130         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x1C)                          \
131   REG32(CXL_HDM_DECODER##n##_CTRL,                                             \
132         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x20)                          \
133             FIELD(CXL_HDM_DECODER##n##_CTRL, IG, 0, 4)                         \
134             FIELD(CXL_HDM_DECODER##n##_CTRL, IW, 4, 4)                         \
135             FIELD(CXL_HDM_DECODER##n##_CTRL, LOCK_ON_COMMIT, 8, 1)             \
136             FIELD(CXL_HDM_DECODER##n##_CTRL, COMMIT, 9, 1)                     \
137             FIELD(CXL_HDM_DECODER##n##_CTRL, COMMITTED, 10, 1)                 \
138             FIELD(CXL_HDM_DECODER##n##_CTRL, ERR, 11, 1)                       \
139             FIELD(CXL_HDM_DECODER##n##_CTRL, TYPE, 12, 1)                      \
140             FIELD(CXL_HDM_DECODER##n##_CTRL, BI, 13, 1)                        \
141             FIELD(CXL_HDM_DECODER##n##_CTRL, UIO, 14, 1)                       \
142             FIELD(CXL_HDM_DECODER##n##_CTRL, UIG, 16, 4)                       \
143             FIELD(CXL_HDM_DECODER##n##_CTRL, UIW, 20, 4)                       \
144             FIELD(CXL_HDM_DECODER##n##_CTRL, ISP, 24, 4)                       \
145   REG32(CXL_HDM_DECODER##n##_TARGET_LIST_LO,                                   \
146         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x24)                          \
147   REG32(CXL_HDM_DECODER##n##_TARGET_LIST_HI,                                   \
148         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x28)                          \
149   REG32(CXL_HDM_DECODER##n##_DPA_SKIP_LO,                                      \
150         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x24)                          \
151   REG32(CXL_HDM_DECODER##n##_DPA_SKIP_HI,                                      \
152         CXL_HDM_REGISTERS_OFFSET + (0x20 * n) + 0x28)
153 
154 REG32(CXL_HDM_DECODER_CAPABILITY, CXL_HDM_REGISTERS_OFFSET)
155     FIELD(CXL_HDM_DECODER_CAPABILITY, DECODER_COUNT, 0, 4)
156     FIELD(CXL_HDM_DECODER_CAPABILITY, TARGET_COUNT, 4, 4)
157     FIELD(CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_256B, 8, 1)
158     FIELD(CXL_HDM_DECODER_CAPABILITY, INTERLEAVE_4K, 9, 1)
159     FIELD(CXL_HDM_DECODER_CAPABILITY, POISON_ON_ERR_CAP, 10, 1)
160     FIELD(CXL_HDM_DECODER_CAPABILITY, 3_6_12_WAY, 11, 1)
161     FIELD(CXL_HDM_DECODER_CAPABILITY, 16_WAY, 12, 1)
162     FIELD(CXL_HDM_DECODER_CAPABILITY, UIO, 13, 1)
163     FIELD(CXL_HDM_DECODER_CAPABILITY, UIO_DECODER_COUNT, 16, 4)
164     FIELD(CXL_HDM_DECODER_CAPABILITY, MEMDATA_NXM_CAP, 20, 1)
165     FIELD(CXL_HDM_DECODER_CAPABILITY, SUPPORTED_COHERENCY_MODEL, 21, 2)
166 REG32(CXL_HDM_DECODER_GLOBAL_CONTROL, CXL_HDM_REGISTERS_OFFSET + 4)
167     FIELD(CXL_HDM_DECODER_GLOBAL_CONTROL, POISON_ON_ERR_EN, 0, 1)
168     FIELD(CXL_HDM_DECODER_GLOBAL_CONTROL, HDM_DECODER_ENABLE, 1, 1)
169 
170 /* Support 4 decoders at all levels of topology */
171 #define CXL_HDM_DECODER_COUNT 4
172 
173 HDM_DECODER_INIT(0);
174 HDM_DECODER_INIT(1);
175 HDM_DECODER_INIT(2);
176 HDM_DECODER_INIT(3);
177 
178 /* 8.2.5.13 - CXL Extended Security Capability Structure (Root complex only) */
179 #define EXTSEC_ENTRY_MAX        256
180 #define CXL_EXTSEC_REGISTERS_OFFSET \
181     (CXL_HDM_REGISTERS_OFFSET + CXL_HDM_REGISTERS_SIZE)
182 #define CXL_EXTSEC_REGISTERS_SIZE   (8 * EXTSEC_ENTRY_MAX + 4)
183 
184 /* 8.2.5.14 - CXL IDE Capability Structure */
185 #define CXL_IDE_REGISTERS_OFFSET \
186     (CXL_EXTSEC_REGISTERS_OFFSET + CXL_EXTSEC_REGISTERS_SIZE)
187 #define CXL_IDE_REGISTERS_SIZE   0x20
188 
189 /* 8.2.5.15 - CXL Snoop Filter Capability Structure */
190 #define CXL_SNOOP_REGISTERS_OFFSET \
191     (CXL_IDE_REGISTERS_OFFSET + CXL_IDE_REGISTERS_SIZE)
192 #define CXL_SNOOP_REGISTERS_SIZE   0x8
193 
194 QEMU_BUILD_BUG_MSG((CXL_SNOOP_REGISTERS_OFFSET +
195                     CXL_SNOOP_REGISTERS_SIZE) >= 0x1000,
196                    "No space for registers");
197 
198 typedef struct component_registers {
199     /*
200      * Main memory region to be registered with QEMU core.
201      */
202     MemoryRegion component_registers;
203 
204     /*
205      * 8.2.4 Table 141:
206      *   0x0000 - 0x0fff CXL.io registers
207      *   0x1000 - 0x1fff CXL.cache and CXL.mem
208      *   0x2000 - 0xdfff Implementation specific
209      *   0xe000 - 0xe3ff CXL ARB/MUX registers
210      *   0xe400 - 0xffff RSVD
211      */
212     uint32_t io_registers[CXL2_COMPONENT_IO_REGION_SIZE >> 2];
213     MemoryRegion io;
214 
215     uint32_t cache_mem_registers[CXL2_COMPONENT_CM_REGION_SIZE >> 2];
216     uint32_t cache_mem_regs_write_mask[CXL2_COMPONENT_CM_REGION_SIZE >> 2];
217     MemoryRegion cache_mem;
218 
219     MemoryRegion impl_specific;
220     MemoryRegion arb_mux;
221     MemoryRegion rsvd;
222 
223     /* special_ops is used for any component that needs any specific handling */
224     MemoryRegionOps *special_ops;
225 } ComponentRegisters;
226 
227 /*
228  * A CXL component represents all entities in a CXL hierarchy. This includes,
229  * host bridges, root ports, upstream/downstream switch ports, and devices
230  */
231 typedef struct cxl_component {
232     ComponentRegisters crb;
233     union {
234         struct {
235             Range dvsecs[CXL20_MAX_DVSEC];
236             uint16_t dvsec_offset;
237             struct PCIDevice *pdev;
238         };
239     };
240 
241     CDATObject cdat;
242 } CXLComponentState;
243 
244 void cxl_component_register_block_init(Object *obj,
245                                        CXLComponentState *cxl_cstate,
246                                        const char *type);
247 void cxl_component_register_init_common(uint32_t *reg_state,
248                                         uint32_t *write_msk,
249                                         enum reg_type type);
250 
251 void cxl_component_create_dvsec(CXLComponentState *cxl_cstate,
252                                 enum reg_type cxl_dev_type, uint16_t length,
253                                 uint16_t type, uint8_t rev, uint8_t *body);
254 
255 int cxl_decoder_count_enc(int count);
256 int cxl_decoder_count_dec(int enc_cnt);
257 
258 uint8_t cxl_interleave_ways_enc(int iw, Error **errp);
259 int cxl_interleave_ways_dec(uint8_t iw_enc, Error **errp);
260 uint8_t cxl_interleave_granularity_enc(uint64_t gran, Error **errp);
261 
262 hwaddr cxl_decode_ig(int ig);
263 
264 CXLComponentState *cxl_get_hb_cstate(PCIHostState *hb);
265 bool cxl_get_hb_passthrough(PCIHostState *hb);
266 
267 void cxl_doe_cdat_init(CXLComponentState *cxl_cstate, Error **errp);
268 void cxl_doe_cdat_release(CXLComponentState *cxl_cstate);
269 void cxl_doe_cdat_update(CXLComponentState *cxl_cstate, Error **errp);
270 
271 #endif
272