4 * Copyright (c) 2020 Intel
6 * This work is licensed under the terms of the GNU GPL, version 2. See the
7 * COPYING file in the top-level directory.
13 #include "hw/cxl/cxl_component.h"
14 #include "hw/pci/pci_device.h"
15 #include "hw/register.h"
16 #include "hw/cxl/cxl_events.h"
19 * The following is how a CXL device's Memory Device registers are laid out.
20 * The only requirement from the spec is that the capabilities array and the
21 * capability headers start at offset 0 and are contiguously packed. The headers
22 * themselves provide offsets to the register fields. For this emulation, the
23 * actual registers * will start at offset 0x80 (m == 0x80). No secondary
24 * mailbox is implemented which means that the offset of the start of the
25 * mailbox payload (n) is given by
26 * n = m + sizeof(mailbox registers) + sizeof(device registers).
28 * +---------------------------------+
30 * | Memory Device Registers |
32 * n + PAYLOAD_SIZE_MAX -----------------------------------
38 * | | Mailbox Payload |
42 * n -----------------------------------
43 * ^ | Mailbox Registers |
45 * | -----------------------------------
47 * | | Device Registers |
49 * m ---------------------------------->
50 * ^ | Memory Device Capability Header|
51 * | -----------------------------------
52 * | | Mailbox Capability Header |
53 * | -----------------------------------
54 * | | Device Capability Header |
55 * | -----------------------------------
56 * | | Device Cap Array Register |
57 * 0 +---------------------------------+
61 #define CXL_DEVICE_CAP_HDR1_OFFSET 0x10 /* Figure 138 */
62 #define CXL_DEVICE_CAP_REG_SIZE 0x10 /* 8.2.8.2 */
63 #define CXL_DEVICE_CAPS_MAX 4 /* 8.2.8.2.1 + 8.2.8.5 */
64 #define CXL_CAPS_SIZE \
65 (CXL_DEVICE_CAP_REG_SIZE * (CXL_DEVICE_CAPS_MAX + 1)) /* +1 for header */
67 #define CXL_DEVICE_STATUS_REGISTERS_OFFSET 0x80 /* Read comment above */
68 #define CXL_DEVICE_STATUS_REGISTERS_LENGTH 0x8 /* 8.2.8.3.1 */
70 #define CXL_MAILBOX_REGISTERS_OFFSET \
71 (CXL_DEVICE_STATUS_REGISTERS_OFFSET + CXL_DEVICE_STATUS_REGISTERS_LENGTH)
72 #define CXL_MAILBOX_REGISTERS_SIZE 0x20 /* 8.2.8.4, Figure 139 */
73 #define CXL_MAILBOX_PAYLOAD_SHIFT 11
74 #define CXL_MAILBOX_MAX_PAYLOAD_SIZE (1 << CXL_MAILBOX_PAYLOAD_SHIFT)
75 #define CXL_MAILBOX_REGISTERS_LENGTH \
76 (CXL_MAILBOX_REGISTERS_SIZE + CXL_MAILBOX_MAX_PAYLOAD_SIZE)
78 #define CXL_MEMORY_DEVICE_REGISTERS_OFFSET \
79 (CXL_MAILBOX_REGISTERS_OFFSET + CXL_MAILBOX_REGISTERS_LENGTH)
80 #define CXL_MEMORY_DEVICE_REGISTERS_LENGTH 0x8
82 #define CXL_MMIO_SIZE \
83 (CXL_DEVICE_CAP_REG_SIZE + CXL_DEVICE_STATUS_REGISTERS_LENGTH + \
84 CXL_MAILBOX_REGISTERS_LENGTH + CXL_MEMORY_DEVICE_REGISTERS_LENGTH)
86 /* 8.2.8.4.5.1 Command Return Codes */
88 CXL_MBOX_SUCCESS
= 0x0,
89 CXL_MBOX_BG_STARTED
= 0x1,
90 CXL_MBOX_INVALID_INPUT
= 0x2,
91 CXL_MBOX_UNSUPPORTED
= 0x3,
92 CXL_MBOX_INTERNAL_ERROR
= 0x4,
93 CXL_MBOX_RETRY_REQUIRED
= 0x5,
95 CXL_MBOX_MEDIA_DISABLED
= 0x7,
96 CXL_MBOX_FW_XFER_IN_PROGRESS
= 0x8,
97 CXL_MBOX_FW_XFER_OUT_OF_ORDER
= 0x9,
98 CXL_MBOX_FW_AUTH_FAILED
= 0xa,
99 CXL_MBOX_FW_INVALID_SLOT
= 0xb,
100 CXL_MBOX_FW_ROLLEDBACK
= 0xc,
101 CXL_MBOX_FW_REST_REQD
= 0xd,
102 CXL_MBOX_INVALID_HANDLE
= 0xe,
103 CXL_MBOX_INVALID_PA
= 0xf,
104 CXL_MBOX_INJECT_POISON_LIMIT
= 0x10,
105 CXL_MBOX_PERMANENT_MEDIA_FAILURE
= 0x11,
106 CXL_MBOX_ABORTED
= 0x12,
107 CXL_MBOX_INVALID_SECURITY_STATE
= 0x13,
108 CXL_MBOX_INCORRECT_PASSPHRASE
= 0x14,
109 CXL_MBOX_UNSUPPORTED_MAILBOX
= 0x15,
110 CXL_MBOX_INVALID_PAYLOAD_LENGTH
= 0x16,
114 typedef struct CXLCCI CXLCCI
;
115 typedef struct cxl_device_state CXLDeviceState
;
117 typedef CXLRetCode (*opcode_handler
)(const struct cxl_cmd
*cmd
,
118 uint8_t *payload_in
, size_t len_in
,
119 uint8_t *payload_out
, size_t *len_out
,
123 opcode_handler handler
;
125 uint16_t effect
; /* Reported in CEL */
128 typedef struct CXLEvent
{
129 CXLEventRecordRaw data
;
130 QSIMPLEQ_ENTRY(CXLEvent
) node
;
133 typedef struct CXLEventLog
{
134 uint16_t next_handle
;
135 uint16_t overflow_err_count
;
136 uint64_t first_overflow_timestamp
;
137 uint64_t last_overflow_timestamp
;
141 QSIMPLEQ_HEAD(, CXLEvent
) events
;
144 typedef struct CXLCCI
{
145 const struct cxl_cmd (*cxl_cmd_set
)[256];
152 /* background command handling (times in ms) */
155 uint16_t complete_pct
;
156 uint16_t ret_code
; /* Current value of retcode */
158 /* set by each bg cmd, cleared by the bg_timer when complete */
163 /* Pointer to device hosting the CCI */
165 /* Pointer to the device hosting the protocol conversion */
169 typedef struct cxl_device_state
{
170 MemoryRegion device_registers
;
172 /* mmio for device capabilities array - 8.2.8.2 */
176 uint8_t dev_reg_state
[CXL_DEVICE_STATUS_REGISTERS_LENGTH
];
177 uint16_t dev_reg_state16
[CXL_DEVICE_STATUS_REGISTERS_LENGTH
/ 2];
178 uint32_t dev_reg_state32
[CXL_DEVICE_STATUS_REGISTERS_LENGTH
/ 4];
179 uint64_t dev_reg_state64
[CXL_DEVICE_STATUS_REGISTERS_LENGTH
/ 8];
181 uint64_t event_status
;
183 MemoryRegion memory_device
;
187 uint32_t caps_reg_state32
[CXL_CAPS_SIZE
/ 4];
188 uint64_t caps_reg_state64
[CXL_CAPS_SIZE
/ 8];
192 /* mmio for the mailbox registers 8.2.8.4 */
194 MemoryRegion mailbox
;
195 uint16_t payload_size
;
198 uint8_t mbox_reg_state
[CXL_MAILBOX_REGISTERS_LENGTH
];
199 uint16_t mbox_reg_state16
[CXL_MAILBOX_REGISTERS_LENGTH
/ 2];
200 uint32_t mbox_reg_state32
[CXL_MAILBOX_REGISTERS_LENGTH
/ 4];
201 uint64_t mbox_reg_state64
[CXL_MAILBOX_REGISTERS_LENGTH
/ 8];
211 /* memory region size, HDM */
216 const struct cxl_cmd (*cxl_cmd_set
)[256];
217 CXLEventLog event_logs
[CXL_EVENT_TYPE_MAX
];
220 /* Initialize the register block for a device */
221 void cxl_device_register_block_init(Object
*obj
, CXLDeviceState
*dev
,
224 typedef struct CXLType3Dev CXLType3Dev
;
225 typedef struct CSWMBCCIDev CSWMBCCIDev
;
226 /* Set up default values for the register block */
227 void cxl_device_register_init_t3(CXLType3Dev
*ct3d
);
228 void cxl_device_register_init_swcci(CSWMBCCIDev
*sw
);
231 * CXL 2.0 - 8.2.8.1 including errata F4
232 * Documented as a 128 bit register, but 64 bit accesses and the second
233 * 64 bits are currently reserved.
235 REG64(CXL_DEV_CAP_ARRAY
, 0)
236 FIELD(CXL_DEV_CAP_ARRAY
, CAP_ID
, 0, 16)
237 FIELD(CXL_DEV_CAP_ARRAY
, CAP_VERSION
, 16, 8)
238 FIELD(CXL_DEV_CAP_ARRAY
, CAP_COUNT
, 32, 16)
240 void cxl_event_set_status(CXLDeviceState
*cxl_dstate
, CXLEventLogType log_type
,
244 * Helper macro to initialize capability headers for CXL devices.
246 * In the 8.2.8.2, this is listed as a 128b register, but in 8.2.8, it says:
247 * > No registers defined in Section 8.2.8 are larger than 64-bits wide so that
248 * > is the maximum access size allowed for these registers. If this rule is not
249 * > followed, the behavior is undefined
251 * CXL 2.0 Errata F4 states further that the layouts in the specification are
252 * shown as greater than 128 bits, but implementations are expected to
253 * use any size of access up to 64 bits.
255 * Here we've chosen to make it 4 dwords. The spec allows any pow2 multiple
256 * access to be used for a register up to 64 bits.
258 #define CXL_DEVICE_CAPABILITY_HEADER_REGISTER(n, offset) \
259 REG32(CXL_DEV_##n##_CAP_HDR0, offset) \
260 FIELD(CXL_DEV_##n##_CAP_HDR0, CAP_ID, 0, 16) \
261 FIELD(CXL_DEV_##n##_CAP_HDR0, CAP_VERSION, 16, 8) \
262 REG32(CXL_DEV_##n##_CAP_HDR1, offset + 4) \
263 FIELD(CXL_DEV_##n##_CAP_HDR1, CAP_OFFSET, 0, 32) \
264 REG32(CXL_DEV_##n##_CAP_HDR2, offset + 8) \
265 FIELD(CXL_DEV_##n##_CAP_HDR2, CAP_LENGTH, 0, 32)
267 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(DEVICE_STATUS
, CXL_DEVICE_CAP_HDR1_OFFSET
)
268 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(MAILBOX
, CXL_DEVICE_CAP_HDR1_OFFSET
+ \
269 CXL_DEVICE_CAP_REG_SIZE
)
270 CXL_DEVICE_CAPABILITY_HEADER_REGISTER(MEMORY_DEVICE
,
271 CXL_DEVICE_CAP_HDR1_OFFSET
+
272 CXL_DEVICE_CAP_REG_SIZE
* 2)
274 void cxl_initialize_mailbox_t3(CXLCCI
*cci
, DeviceState
*d
, size_t payload_max
);
275 void cxl_initialize_mailbox_swcci(CXLCCI
*cci
, DeviceState
*intf
,
276 DeviceState
*d
, size_t payload_max
);
277 void cxl_init_cci(CXLCCI
*cci
, size_t payload_max
);
278 int cxl_process_cci_message(CXLCCI
*cci
, uint8_t set
, uint8_t cmd
,
279 size_t len_in
, uint8_t *pl_in
,
280 size_t *len_out
, uint8_t *pl_out
,
282 void cxl_initialize_t3_fm_owned_ld_mctpcci(CXLCCI
*cci
, DeviceState
*d
,
286 void cxl_initialize_t3_ld_cci(CXLCCI
*cci
, DeviceState
*d
,
287 DeviceState
*intf
, size_t payload_max
);
289 #define cxl_device_cap_init(dstate, reg, cap_id, ver) \
291 uint32_t *cap_hdrs = dstate->caps_reg_state32; \
292 int which = R_CXL_DEV_##reg##_CAP_HDR0; \
294 FIELD_DP32(cap_hdrs[which], CXL_DEV_##reg##_CAP_HDR0, \
296 cap_hdrs[which] = FIELD_DP32( \
297 cap_hdrs[which], CXL_DEV_##reg##_CAP_HDR0, CAP_VERSION, ver); \
298 cap_hdrs[which + 1] = \
299 FIELD_DP32(cap_hdrs[which + 1], CXL_DEV_##reg##_CAP_HDR1, \
300 CAP_OFFSET, CXL_##reg##_REGISTERS_OFFSET); \
301 cap_hdrs[which + 2] = \
302 FIELD_DP32(cap_hdrs[which + 2], CXL_DEV_##reg##_CAP_HDR2, \
303 CAP_LENGTH, CXL_##reg##_REGISTERS_LENGTH); \
306 /* CXL 3.0 8.2.8.3.1 Event Status Register */
307 REG64(CXL_DEV_EVENT_STATUS
, 0)
308 FIELD(CXL_DEV_EVENT_STATUS
, EVENT_STATUS
, 0, 32)
310 /* CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register */
311 REG32(CXL_DEV_MAILBOX_CAP
, 0)
312 FIELD(CXL_DEV_MAILBOX_CAP
, PAYLOAD_SIZE
, 0, 5)
313 FIELD(CXL_DEV_MAILBOX_CAP
, INT_CAP
, 5, 1)
314 FIELD(CXL_DEV_MAILBOX_CAP
, BG_INT_CAP
, 6, 1)
315 FIELD(CXL_DEV_MAILBOX_CAP
, MSI_N
, 7, 4)
317 /* CXL 2.0 8.2.8.4.4 Mailbox Control Register */
318 REG32(CXL_DEV_MAILBOX_CTRL
, 4)
319 FIELD(CXL_DEV_MAILBOX_CTRL
, DOORBELL
, 0, 1)
320 FIELD(CXL_DEV_MAILBOX_CTRL
, INT_EN
, 1, 1)
321 FIELD(CXL_DEV_MAILBOX_CTRL
, BG_INT_EN
, 2, 1)
323 /* CXL 2.0 8.2.8.4.5 Command Register */
324 REG64(CXL_DEV_MAILBOX_CMD
, 8)
325 FIELD(CXL_DEV_MAILBOX_CMD
, COMMAND
, 0, 8)
326 FIELD(CXL_DEV_MAILBOX_CMD
, COMMAND_SET
, 8, 8)
327 FIELD(CXL_DEV_MAILBOX_CMD
, LENGTH
, 16, 20)
329 /* CXL 2.0 8.2.8.4.6 Mailbox Status Register */
330 REG64(CXL_DEV_MAILBOX_STS
, 0x10)
331 FIELD(CXL_DEV_MAILBOX_STS
, BG_OP
, 0, 1)
332 FIELD(CXL_DEV_MAILBOX_STS
, ERRNO
, 32, 16)
333 FIELD(CXL_DEV_MAILBOX_STS
, VENDOR_ERRNO
, 48, 16)
335 /* CXL 2.0 8.2.8.4.7 Background Command Status Register */
336 REG64(CXL_DEV_BG_CMD_STS
, 0x18)
337 FIELD(CXL_DEV_BG_CMD_STS
, OP
, 0, 16)
338 FIELD(CXL_DEV_BG_CMD_STS
, PERCENTAGE_COMP
, 16, 7)
339 FIELD(CXL_DEV_BG_CMD_STS
, RET_CODE
, 32, 16)
340 FIELD(CXL_DEV_BG_CMD_STS
, VENDOR_RET_CODE
, 48, 16)
342 /* CXL 2.0 8.2.8.4.8 Command Payload Registers */
343 REG32(CXL_DEV_CMD_PAYLOAD
, 0x20)
345 REG64(CXL_MEM_DEV_STS
, 0)
346 FIELD(CXL_MEM_DEV_STS
, FATAL
, 0, 1)
347 FIELD(CXL_MEM_DEV_STS
, FW_HALT
, 1, 1)
348 FIELD(CXL_MEM_DEV_STS
, MEDIA_STATUS
, 2, 2)
349 FIELD(CXL_MEM_DEV_STS
, MBOX_READY
, 4, 1)
350 FIELD(CXL_MEM_DEV_STS
, RESET_NEEDED
, 5, 3)
352 static inline void __toggle_media(CXLDeviceState
*cxl_dstate
, int val
)
354 uint64_t dev_status_reg
;
356 dev_status_reg
= FIELD_DP64(0, CXL_MEM_DEV_STS
, MEDIA_STATUS
, val
);
357 cxl_dstate
->mbox_reg_state64
[R_CXL_MEM_DEV_STS
] = dev_status_reg
;
359 #define cxl_dev_disable_media(cxlds) \
360 do { __toggle_media((cxlds), 0x3); } while (0)
361 #define cxl_dev_enable_media(cxlds) \
362 do { __toggle_media((cxlds), 0x1); } while (0)
364 static inline bool sanitize_running(CXLCCI
*cci
)
366 return !!cci
->bg
.runtime
&& cci
->bg
.opcode
== 0x4400;
369 typedef struct CXLError
{
370 QTAILQ_ENTRY(CXLError
) node
;
371 int type
; /* Error code as per FE definition */
372 uint32_t header
[CXL_RAS_ERR_HEADER_NUM
];
375 typedef QTAILQ_HEAD(, CXLError
) CXLErrorList
;
377 typedef struct CXLPoison
{
378 uint64_t start
, length
;
380 #define CXL_POISON_TYPE_EXTERNAL 0x1
381 #define CXL_POISON_TYPE_INTERNAL 0x2
382 #define CXL_POISON_TYPE_INJECTED 0x3
383 QLIST_ENTRY(CXLPoison
) node
;
386 typedef QLIST_HEAD(, CXLPoison
) CXLPoisonList
;
387 #define CXL_POISON_LIST_LIMIT 256
391 PCIDevice parent_obj
;
394 HostMemoryBackend
*hostmem
; /* deprecated */
395 HostMemoryBackend
*hostvmem
;
396 HostMemoryBackend
*hostpmem
;
397 HostMemoryBackend
*lsa
;
401 AddressSpace hostvmem_as
;
402 AddressSpace hostpmem_as
;
403 CXLComponentState cxl_cstate
;
404 CXLDeviceState cxl_dstate
;
405 CXLCCI cci
; /* Primary PCI mailbox CCI */
406 /* Always intialized as no way to know if a VDM might show up */
407 CXLCCI vdm_fm_owned_ld_mctp_cci
;
413 /* Error injection */
414 CXLErrorList error_list
;
416 /* Poison Injection - cache */
417 CXLPoisonList poison_list
;
418 unsigned int poison_list_cnt
;
419 bool poison_list_overflowed
;
420 uint64_t poison_list_overflow_ts
;
423 #define TYPE_CXL_TYPE3 "cxl-type3"
424 OBJECT_DECLARE_TYPE(CXLType3Dev
, CXLType3Class
, CXL_TYPE3
)
426 struct CXLType3Class
{
428 PCIDeviceClass parent_class
;
431 uint64_t (*get_lsa_size
)(CXLType3Dev
*ct3d
);
433 uint64_t (*get_lsa
)(CXLType3Dev
*ct3d
, void *buf
, uint64_t size
,
435 void (*set_lsa
)(CXLType3Dev
*ct3d
, const void *buf
, uint64_t size
,
437 bool (*set_cacheline
)(CXLType3Dev
*ct3d
, uint64_t dpa_offset
,
442 PCIDevice parent_obj
;
444 CXLComponentState cxl_cstate
;
445 CXLDeviceState cxl_dstate
;
449 #define TYPE_CXL_SWITCH_MAILBOX_CCI "cxl-switch-mailbox-cci"
450 OBJECT_DECLARE_TYPE(CSWMBCCIDev
, CSWMBCCIClass
, CXL_SWITCH_MAILBOX_CCI
)
452 MemTxResult
cxl_type3_read(PCIDevice
*d
, hwaddr host_addr
, uint64_t *data
,
453 unsigned size
, MemTxAttrs attrs
);
454 MemTxResult
cxl_type3_write(PCIDevice
*d
, hwaddr host_addr
, uint64_t data
,
455 unsigned size
, MemTxAttrs attrs
);
457 uint64_t cxl_device_get_timestamp(CXLDeviceState
*cxlds
);
459 void cxl_event_init(CXLDeviceState
*cxlds
, int start_msg_num
);
460 bool cxl_event_insert(CXLDeviceState
*cxlds
, CXLEventLogType log_type
,
461 CXLEventRecordRaw
*event
);
462 CXLRetCode
cxl_event_get_records(CXLDeviceState
*cxlds
, CXLGetEventPayload
*pl
,
463 uint8_t log_type
, int max_recs
,
465 CXLRetCode
cxl_event_clear_records(CXLDeviceState
*cxlds
,
466 CXLClearEventPayload
*pl
);
468 void cxl_event_irq_assert(CXLType3Dev
*ct3d
);
470 void cxl_set_poison_list_overflowed(CXLType3Dev
*ct3d
);