xref: /openbmc/qemu/include/hw/cxl/cxl_device.h (revision d044b7c3)
1 /*
2  * QEMU CXL Devices
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_DEVICE_H
11 #define CXL_DEVICE_H
12 
13 #include "hw/cxl/cxl_component.h"
14 #include "hw/pci/pci_device.h"
15 #include "hw/register.h"
16 
17 /*
18  * The following is how a CXL device's Memory Device registers are laid out.
19  * The only requirement from the spec is that the capabilities array and the
20  * capability headers start at offset 0 and are contiguously packed. The headers
21  * themselves provide offsets to the register fields. For this emulation, the
22  * actual registers  * will start at offset 0x80 (m == 0x80). No secondary
23  * mailbox is implemented which means that the offset of the start of the
24  * mailbox payload (n) is given by
25  * n = m + sizeof(mailbox registers) + sizeof(device registers).
26  *
27  *                       +---------------------------------+
28  *                       |                                 |
29  *                       |    Memory Device Registers      |
30  *                       |                                 |
31  * n + PAYLOAD_SIZE_MAX  -----------------------------------
32  *                  ^    |                                 |
33  *                  |    |                                 |
34  *                  |    |                                 |
35  *                  |    |                                 |
36  *                  |    |                                 |
37  *                  |    |         Mailbox Payload         |
38  *                  |    |                                 |
39  *                  |    |                                 |
40  *                  |    |                                 |
41  *                  n    -----------------------------------
42  *                  ^    |       Mailbox Registers         |
43  *                  |    |                                 |
44  *                  |    -----------------------------------
45  *                  |    |                                 |
46  *                  |    |        Device Registers         |
47  *                  |    |                                 |
48  *                  m    ---------------------------------->
49  *                  ^    |  Memory Device Capability Header|
50  *                  |    -----------------------------------
51  *                  |    |     Mailbox Capability Header   |
52  *                  |    -----------------------------------
53  *                  |    |     Device Capability Header    |
54  *                  |    -----------------------------------
55  *                  |    |     Device Cap Array Register   |
56  *                  0    +---------------------------------+
57  *
58  */
59 
60 #define CXL_DEVICE_CAP_HDR1_OFFSET 0x10 /* Figure 138 */
61 #define CXL_DEVICE_CAP_REG_SIZE 0x10 /* 8.2.8.2 */
62 #define CXL_DEVICE_CAPS_MAX 4 /* 8.2.8.2.1 + 8.2.8.5 */
63 #define CXL_CAPS_SIZE \
64     (CXL_DEVICE_CAP_REG_SIZE * (CXL_DEVICE_CAPS_MAX + 1)) /* +1 for header */
65 
66 #define CXL_DEVICE_STATUS_REGISTERS_OFFSET 0x80 /* Read comment above */
67 #define CXL_DEVICE_STATUS_REGISTERS_LENGTH 0x8 /* 8.2.8.3.1 */
68 
69 #define CXL_MAILBOX_REGISTERS_OFFSET \
70     (CXL_DEVICE_STATUS_REGISTERS_OFFSET + CXL_DEVICE_STATUS_REGISTERS_LENGTH)
71 #define CXL_MAILBOX_REGISTERS_SIZE 0x20 /* 8.2.8.4, Figure 139 */
72 #define CXL_MAILBOX_PAYLOAD_SHIFT 11
73 #define CXL_MAILBOX_MAX_PAYLOAD_SIZE (1 << CXL_MAILBOX_PAYLOAD_SHIFT)
74 #define CXL_MAILBOX_REGISTERS_LENGTH \
75     (CXL_MAILBOX_REGISTERS_SIZE + CXL_MAILBOX_MAX_PAYLOAD_SIZE)
76 
77 #define CXL_MEMORY_DEVICE_REGISTERS_OFFSET \
78     (CXL_MAILBOX_REGISTERS_OFFSET + CXL_MAILBOX_REGISTERS_LENGTH)
79 #define CXL_MEMORY_DEVICE_REGISTERS_LENGTH 0x8
80 
81 #define CXL_MMIO_SIZE                                                   \
82     (CXL_DEVICE_CAP_REG_SIZE + CXL_DEVICE_STATUS_REGISTERS_LENGTH +     \
83      CXL_MAILBOX_REGISTERS_LENGTH + CXL_MEMORY_DEVICE_REGISTERS_LENGTH)
84 
85 typedef struct cxl_device_state {
86     MemoryRegion device_registers;
87 
88     /* mmio for device capabilities array - 8.2.8.2 */
89     MemoryRegion device;
90     MemoryRegion memory_device;
91     struct {
92         MemoryRegion caps;
93         union {
94             uint32_t caps_reg_state32[CXL_CAPS_SIZE / 4];
95             uint64_t caps_reg_state64[CXL_CAPS_SIZE / 8];
96         };
97     };
98 
99     /* mmio for the mailbox registers 8.2.8.4 */
100     struct {
101         MemoryRegion mailbox;
102         uint16_t payload_size;
103         union {
104             uint8_t mbox_reg_state[CXL_MAILBOX_REGISTERS_LENGTH];
105             uint16_t mbox_reg_state16[CXL_MAILBOX_REGISTERS_LENGTH / 2];
106             uint32_t mbox_reg_state32[CXL_MAILBOX_REGISTERS_LENGTH / 4];
107             uint64_t mbox_reg_state64[CXL_MAILBOX_REGISTERS_LENGTH / 8];
108         };
109         struct cel_log {
110             uint16_t opcode;
111             uint16_t effect;
112         } cel_log[1 << 16];
113         size_t cel_size;
114     };
115 
116     struct {
117         bool set;
118         uint64_t last_set;
119         uint64_t host_set;
120     } timestamp;
121 
122     /* memory region for persistent memory, HDM */
123     uint64_t pmem_size;
124 } CXLDeviceState;
125 
126 /* Initialize the register block for a device */
127 void cxl_device_register_block_init(Object *obj, CXLDeviceState *dev);
128 
129 /* Set up default values for the register block */
130 void cxl_device_register_init_common(CXLDeviceState *dev);
131 
132 /*
133  * CXL 2.0 - 8.2.8.1 including errata F4
134  * Documented as a 128 bit register, but 64 bit accesses and the second
135  * 64 bits are currently reserved.
136  */
137 REG64(CXL_DEV_CAP_ARRAY, 0) /* Documented as 128 bit register but 64 byte accesses */
138     FIELD(CXL_DEV_CAP_ARRAY, CAP_ID, 0, 16)
139     FIELD(CXL_DEV_CAP_ARRAY, CAP_VERSION, 16, 8)
140     FIELD(CXL_DEV_CAP_ARRAY, CAP_COUNT, 32, 16)
141 
142 /*
143  * Helper macro to initialize capability headers for CXL devices.
144  *
145  * In the 8.2.8.2, this is listed as a 128b register, but in 8.2.8, it says:
146  * > No registers defined in Section 8.2.8 are larger than 64-bits wide so that
147  * > is the maximum access size allowed for these registers. If this rule is not
148  * > followed, the behavior is undefined
149  *
150  * CXL 2.0 Errata F4 states futher that the layouts in the specification are
151  * shown as greater than 128 bits, but implementations are expected to
152  * use any size of access up to 64 bits.
153  *
154  * Here we've chosen to make it 4 dwords. The spec allows any pow2 multiple
155  * access to be used for a register up to 64 bits.
156  */
157 #define CXL_DEVICE_CAPABILITY_HEADER_REGISTER(n, offset)  \
158     REG32(CXL_DEV_##n##_CAP_HDR0, offset)                 \
159         FIELD(CXL_DEV_##n##_CAP_HDR0, CAP_ID, 0, 16)      \
160         FIELD(CXL_DEV_##n##_CAP_HDR0, CAP_VERSION, 16, 8) \
161     REG32(CXL_DEV_##n##_CAP_HDR1, offset + 4)             \
162         FIELD(CXL_DEV_##n##_CAP_HDR1, CAP_OFFSET, 0, 32)  \
163     REG32(CXL_DEV_##n##_CAP_HDR2, offset + 8)             \
164         FIELD(CXL_DEV_##n##_CAP_HDR2, CAP_LENGTH, 0, 32)
165 
166 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(DEVICE_STATUS, CXL_DEVICE_CAP_HDR1_OFFSET)
167 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(MAILBOX, CXL_DEVICE_CAP_HDR1_OFFSET + \
168                                                CXL_DEVICE_CAP_REG_SIZE)
169 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(MEMORY_DEVICE,
170                                       CXL_DEVICE_CAP_HDR1_OFFSET +
171                                           CXL_DEVICE_CAP_REG_SIZE * 2)
172 
173 void cxl_initialize_mailbox(CXLDeviceState *cxl_dstate);
174 void cxl_process_mailbox(CXLDeviceState *cxl_dstate);
175 
176 #define cxl_device_cap_init(dstate, reg, cap_id)                           \
177     do {                                                                   \
178         uint32_t *cap_hdrs = dstate->caps_reg_state32;                     \
179         int which = R_CXL_DEV_##reg##_CAP_HDR0;                            \
180         cap_hdrs[which] =                                                  \
181             FIELD_DP32(cap_hdrs[which], CXL_DEV_##reg##_CAP_HDR0,          \
182                        CAP_ID, cap_id);                                    \
183         cap_hdrs[which] = FIELD_DP32(                                      \
184             cap_hdrs[which], CXL_DEV_##reg##_CAP_HDR0, CAP_VERSION, 1);    \
185         cap_hdrs[which + 1] =                                              \
186             FIELD_DP32(cap_hdrs[which + 1], CXL_DEV_##reg##_CAP_HDR1,      \
187                        CAP_OFFSET, CXL_##reg##_REGISTERS_OFFSET);          \
188         cap_hdrs[which + 2] =                                              \
189             FIELD_DP32(cap_hdrs[which + 2], CXL_DEV_##reg##_CAP_HDR2,      \
190                        CAP_LENGTH, CXL_##reg##_REGISTERS_LENGTH);          \
191     } while (0)
192 
193 /* CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register */
194 REG32(CXL_DEV_MAILBOX_CAP, 0)
195     FIELD(CXL_DEV_MAILBOX_CAP, PAYLOAD_SIZE, 0, 5)
196     FIELD(CXL_DEV_MAILBOX_CAP, INT_CAP, 5, 1)
197     FIELD(CXL_DEV_MAILBOX_CAP, BG_INT_CAP, 6, 1)
198     FIELD(CXL_DEV_MAILBOX_CAP, MSI_N, 7, 4)
199 
200 /* CXL 2.0 8.2.8.4.4 Mailbox Control Register */
201 REG32(CXL_DEV_MAILBOX_CTRL, 4)
202     FIELD(CXL_DEV_MAILBOX_CTRL, DOORBELL, 0, 1)
203     FIELD(CXL_DEV_MAILBOX_CTRL, INT_EN, 1, 1)
204     FIELD(CXL_DEV_MAILBOX_CTRL, BG_INT_EN, 2, 1)
205 
206 /* CXL 2.0 8.2.8.4.5 Command Register */
207 REG64(CXL_DEV_MAILBOX_CMD, 8)
208     FIELD(CXL_DEV_MAILBOX_CMD, COMMAND, 0, 8)
209     FIELD(CXL_DEV_MAILBOX_CMD, COMMAND_SET, 8, 8)
210     FIELD(CXL_DEV_MAILBOX_CMD, LENGTH, 16, 20)
211 
212 /* CXL 2.0 8.2.8.4.6 Mailbox Status Register */
213 REG64(CXL_DEV_MAILBOX_STS, 0x10)
214     FIELD(CXL_DEV_MAILBOX_STS, BG_OP, 0, 1)
215     FIELD(CXL_DEV_MAILBOX_STS, ERRNO, 32, 16)
216     FIELD(CXL_DEV_MAILBOX_STS, VENDOR_ERRNO, 48, 16)
217 
218 /* CXL 2.0 8.2.8.4.7 Background Command Status Register */
219 REG64(CXL_DEV_BG_CMD_STS, 0x18)
220     FIELD(CXL_DEV_BG_CMD_STS, OP, 0, 16)
221     FIELD(CXL_DEV_BG_CMD_STS, PERCENTAGE_COMP, 16, 7)
222     FIELD(CXL_DEV_BG_CMD_STS, RET_CODE, 32, 16)
223     FIELD(CXL_DEV_BG_CMD_STS, VENDOR_RET_CODE, 48, 16)
224 
225 /* CXL 2.0 8.2.8.4.8 Command Payload Registers */
226 REG32(CXL_DEV_CMD_PAYLOAD, 0x20)
227 
228 REG64(CXL_MEM_DEV_STS, 0)
229     FIELD(CXL_MEM_DEV_STS, FATAL, 0, 1)
230     FIELD(CXL_MEM_DEV_STS, FW_HALT, 1, 1)
231     FIELD(CXL_MEM_DEV_STS, MEDIA_STATUS, 2, 2)
232     FIELD(CXL_MEM_DEV_STS, MBOX_READY, 4, 1)
233     FIELD(CXL_MEM_DEV_STS, RESET_NEEDED, 5, 3)
234 
235 typedef struct CXLError {
236     QTAILQ_ENTRY(CXLError) node;
237     int type; /* Error code as per FE definition */
238     uint32_t header[32];
239 } CXLError;
240 
241 typedef QTAILQ_HEAD(, CXLError) CXLErrorList;
242 
243 struct CXLType3Dev {
244     /* Private */
245     PCIDevice parent_obj;
246 
247     /* Properties */
248     HostMemoryBackend *hostmem;
249     HostMemoryBackend *lsa;
250     uint64_t sn;
251 
252     /* State */
253     AddressSpace hostmem_as;
254     CXLComponentState cxl_cstate;
255     CXLDeviceState cxl_dstate;
256 
257     /* DOE */
258     DOECap doe_cdat;
259 
260     /* Error injection */
261     CXLErrorList error_list;
262 };
263 
264 #define TYPE_CXL_TYPE3 "cxl-type3"
265 OBJECT_DECLARE_TYPE(CXLType3Dev, CXLType3Class, CXL_TYPE3)
266 
267 struct CXLType3Class {
268     /* Private */
269     PCIDeviceClass parent_class;
270 
271     /* public */
272     uint64_t (*get_lsa_size)(CXLType3Dev *ct3d);
273 
274     uint64_t (*get_lsa)(CXLType3Dev *ct3d, void *buf, uint64_t size,
275                         uint64_t offset);
276     void (*set_lsa)(CXLType3Dev *ct3d, const void *buf, uint64_t size,
277                     uint64_t offset);
278 };
279 
280 MemTxResult cxl_type3_read(PCIDevice *d, hwaddr host_addr, uint64_t *data,
281                            unsigned size, MemTxAttrs attrs);
282 MemTxResult cxl_type3_write(PCIDevice *d, hwaddr host_addr, uint64_t data,
283                             unsigned size, MemTxAttrs attrs);
284 
285 #endif
286