2 * CXL Utility library for mailbox interface
4 * Copyright(C) 2020 Intel Corporation.
6 * This work is licensed under the terms of the GNU GPL, version 2. See the
7 * COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "hw/cxl/cxl.h"
12 #include "hw/pci/pci.h"
13 #include "qemu/cutils.h"
15 #include "qemu/uuid.h"
18 * How to add a new command, example. The command set FOO, with cmd BAR.
19 * 1. Add the command set and cmd to the enum.
22 * 2. Implement the handler
23 * static ret_code cmd_foo_bar(struct cxl_cmd *cmd,
24 * CXLDeviceState *cxl_dstate, uint16_t *len)
25 * 3. Add the command to the cxl_cmd_set[][]
26 * [FOO][BAR] = { "FOO_BAR", cmd_foo_bar, x, y },
27 * 4. Implement your handler
28 * define_mailbox_handler(FOO_BAR) { ... return CXL_MBOX_SUCCESS; }
31 * Writing the handler:
32 * The handler will provide the &struct cxl_cmd, the &CXLDeviceState, and the
33 * in/out length of the payload. The handler is responsible for consuming the
34 * payload from cmd->payload and operating upon it as necessary. It must then
35 * fill the output data into cmd->payload (overwriting what was there),
36 * setting the length, and returning a valid return code.
38 * XXX: The handler need not worry about endianess. The payload is read out of
39 * a register interface that already deals with it.
44 #define GET_RECORDS 0x0
45 #define CLEAR_RECORDS 0x1
46 #define GET_INTERRUPT_POLICY 0x2
47 #define SET_INTERRUPT_POLICY 0x3
48 FIRMWARE_UPDATE
= 0x02,
54 #define GET_SUPPORTED 0x0
57 #define MEMORY_DEVICE 0x0
59 #define GET_PARTITION_INFO 0x0
62 /* 8.2.8.4.5.1 Command Return Codes */
64 CXL_MBOX_SUCCESS
= 0x0,
65 CXL_MBOX_BG_STARTED
= 0x1,
66 CXL_MBOX_INVALID_INPUT
= 0x2,
67 CXL_MBOX_UNSUPPORTED
= 0x3,
68 CXL_MBOX_INTERNAL_ERROR
= 0x4,
69 CXL_MBOX_RETRY_REQUIRED
= 0x5,
71 CXL_MBOX_MEDIA_DISABLED
= 0x7,
72 CXL_MBOX_FW_XFER_IN_PROGRESS
= 0x8,
73 CXL_MBOX_FW_XFER_OUT_OF_ORDER
= 0x9,
74 CXL_MBOX_FW_AUTH_FAILED
= 0xa,
75 CXL_MBOX_FW_INVALID_SLOT
= 0xb,
76 CXL_MBOX_FW_ROLLEDBACK
= 0xc,
77 CXL_MBOX_FW_REST_REQD
= 0xd,
78 CXL_MBOX_INVALID_HANDLE
= 0xe,
79 CXL_MBOX_INVALID_PA
= 0xf,
80 CXL_MBOX_INJECT_POISON_LIMIT
= 0x10,
81 CXL_MBOX_PERMANENT_MEDIA_FAILURE
= 0x11,
82 CXL_MBOX_ABORTED
= 0x12,
83 CXL_MBOX_INVALID_SECURITY_STATE
= 0x13,
84 CXL_MBOX_INCORRECT_PASSPHRASE
= 0x14,
85 CXL_MBOX_UNSUPPORTED_MAILBOX
= 0x15,
86 CXL_MBOX_INVALID_PAYLOAD_LENGTH
= 0x16,
91 typedef ret_code (*opcode_handler
)(struct cxl_cmd
*cmd
,
92 CXLDeviceState
*cxl_dstate
, uint16_t *len
);
95 opcode_handler handler
;
97 uint16_t effect
; /* Reported in CEL */
101 #define DEFINE_MAILBOX_HANDLER_ZEROED(name, size) \
102 uint16_t __zero##name = size; \
103 static ret_code cmd_##name(struct cxl_cmd *cmd, \
104 CXLDeviceState *cxl_dstate, uint16_t *len) \
106 *len = __zero##name; \
107 memset(cmd->payload, 0, *len); \
108 return CXL_MBOX_SUCCESS; \
110 #define DEFINE_MAILBOX_HANDLER_NOP(name) \
111 static ret_code cmd_##name(struct cxl_cmd *cmd, \
112 CXLDeviceState *cxl_dstate, uint16_t *len) \
114 return CXL_MBOX_SUCCESS; \
117 DEFINE_MAILBOX_HANDLER_ZEROED(events_get_records
, 0x20);
118 DEFINE_MAILBOX_HANDLER_NOP(events_clear_records
);
119 DEFINE_MAILBOX_HANDLER_ZEROED(events_get_interrupt_policy
, 4);
120 DEFINE_MAILBOX_HANDLER_NOP(events_set_interrupt_policy
);
123 static ret_code
cmd_firmware_update_get_info(struct cxl_cmd
*cmd
,
124 CXLDeviceState
*cxl_dstate
,
128 uint8_t slots_supported
;
136 } QEMU_PACKED
*fw_info
;
137 QEMU_BUILD_BUG_ON(sizeof(*fw_info
) != 0x50);
139 if (cxl_dstate
->pmem_size
< (256 << 20)) {
140 return CXL_MBOX_INTERNAL_ERROR
;
143 fw_info
= (void *)cmd
->payload
;
144 memset(fw_info
, 0, sizeof(*fw_info
));
146 fw_info
->slots_supported
= 2;
147 fw_info
->slot_info
= BIT(0) | BIT(3);
149 pstrcpy(fw_info
->fw_rev1
, sizeof(fw_info
->fw_rev1
), "BWFW VERSION 0");
151 *len
= sizeof(*fw_info
);
152 return CXL_MBOX_SUCCESS
;
156 static ret_code
cmd_timestamp_get(struct cxl_cmd
*cmd
,
157 CXLDeviceState
*cxl_dstate
,
160 uint64_t time
, delta
;
161 uint64_t final_time
= 0;
163 if (cxl_dstate
->timestamp
.set
) {
164 /* First find the delta from the last time the host set the time. */
165 time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
166 delta
= time
- cxl_dstate
->timestamp
.last_set
;
167 final_time
= cxl_dstate
->timestamp
.host_set
+ delta
;
170 /* Then adjust the actual time */
171 stq_le_p(cmd
->payload
, final_time
);
174 return CXL_MBOX_SUCCESS
;
178 static ret_code
cmd_timestamp_set(struct cxl_cmd
*cmd
,
179 CXLDeviceState
*cxl_dstate
,
182 cxl_dstate
->timestamp
.set
= true;
183 cxl_dstate
->timestamp
.last_set
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
185 cxl_dstate
->timestamp
.host_set
= le64_to_cpu(*(uint64_t *)cmd
->payload
);
188 return CXL_MBOX_SUCCESS
;
191 static QemuUUID cel_uuid
;
194 static ret_code
cmd_logs_get_supported(struct cxl_cmd
*cmd
,
195 CXLDeviceState
*cxl_dstate
,
205 } QEMU_PACKED
*supported_logs
= (void *)cmd
->payload
;
206 QEMU_BUILD_BUG_ON(sizeof(*supported_logs
) != 0x1c);
208 supported_logs
->entries
= 1;
209 supported_logs
->log_entries
[0].uuid
= cel_uuid
;
210 supported_logs
->log_entries
[0].size
= 4 * cxl_dstate
->cel_size
;
212 *len
= sizeof(*supported_logs
);
213 return CXL_MBOX_SUCCESS
;
217 static ret_code
cmd_logs_get_log(struct cxl_cmd
*cmd
,
218 CXLDeviceState
*cxl_dstate
,
225 } QEMU_PACKED
QEMU_ALIGNED(16) *get_log
= (void *)cmd
->payload
;
229 * The device shall return Invalid Parameter if the Offset or Length
230 * fields attempt to access beyond the size of the log as reported by Get
233 * XXX: Spec is wrong, "Invalid Parameter" isn't a thing.
234 * XXX: Spec doesn't address incorrect UUID incorrectness.
236 * The CEL buffer is large enough to fit all commands in the emulation, so
237 * the only possible failure would be if the mailbox itself isn't big
240 if (get_log
->offset
+ get_log
->length
> cxl_dstate
->payload_size
) {
241 return CXL_MBOX_INVALID_INPUT
;
244 if (!qemu_uuid_is_equal(&get_log
->uuid
, &cel_uuid
)) {
245 return CXL_MBOX_UNSUPPORTED
;
248 /* Store off everything to local variables so we can wipe out the payload */
249 *len
= get_log
->length
;
251 memmove(cmd
->payload
, cxl_dstate
->cel_log
+ get_log
->offset
,
254 return CXL_MBOX_SUCCESS
;
258 static ret_code
cmd_identify_memory_device(struct cxl_cmd
*cmd
,
259 CXLDeviceState
*cxl_dstate
,
263 char fw_revision
[0x10];
264 uint64_t total_capacity
;
265 uint64_t volatile_capacity
;
266 uint64_t persistent_capacity
;
267 uint64_t partition_align
;
268 uint16_t info_event_log_size
;
269 uint16_t warning_event_log_size
;
270 uint16_t failure_event_log_size
;
271 uint16_t fatal_event_log_size
;
273 uint8_t poison_list_max_mer
[3];
274 uint16_t inject_poison_limit
;
276 uint8_t qos_telemetry_caps
;
278 QEMU_BUILD_BUG_ON(sizeof(*id
) != 0x43);
280 CXLType3Dev
*ct3d
= container_of(cxl_dstate
, CXLType3Dev
, cxl_dstate
);
281 CXLType3Class
*cvc
= CXL_TYPE3_GET_CLASS(ct3d
);
282 uint64_t size
= cxl_dstate
->pmem_size
;
284 if (!QEMU_IS_ALIGNED(size
, 256 << 20)) {
285 return CXL_MBOX_INTERNAL_ERROR
;
288 id
= (void *)cmd
->payload
;
289 memset(id
, 0, sizeof(*id
));
292 snprintf(id
->fw_revision
, 0x10, "BWFW VERSION %02d", 0);
294 id
->total_capacity
= size
/ (256 << 20);
295 id
->persistent_capacity
= size
/ (256 << 20);
296 id
->lsa_size
= cvc
->get_lsa_size(ct3d
);
299 return CXL_MBOX_SUCCESS
;
302 static ret_code
cmd_ccls_get_partition_info(struct cxl_cmd
*cmd
,
303 CXLDeviceState
*cxl_dstate
,
307 uint64_t active_vmem
;
308 uint64_t active_pmem
;
311 } QEMU_PACKED
*part_info
= (void *)cmd
->payload
;
312 QEMU_BUILD_BUG_ON(sizeof(*part_info
) != 0x20);
313 uint64_t size
= cxl_dstate
->pmem_size
;
315 if (!QEMU_IS_ALIGNED(size
, 256 << 20)) {
316 return CXL_MBOX_INTERNAL_ERROR
;
320 part_info
->active_vmem
= 0;
321 part_info
->next_vmem
= 0;
322 part_info
->active_pmem
= size
/ (256 << 20);
323 part_info
->next_pmem
= 0;
325 *len
= sizeof(*part_info
);
326 return CXL_MBOX_SUCCESS
;
329 #define IMMEDIATE_CONFIG_CHANGE (1 << 1)
330 #define IMMEDIATE_POLICY_CHANGE (1 << 3)
331 #define IMMEDIATE_LOG_CHANGE (1 << 4)
333 static struct cxl_cmd cxl_cmd_set
[256][256] = {
334 [EVENTS
][GET_RECORDS
] = { "EVENTS_GET_RECORDS",
335 cmd_events_get_records
, 1, 0 },
336 [EVENTS
][CLEAR_RECORDS
] = { "EVENTS_CLEAR_RECORDS",
337 cmd_events_clear_records
, ~0, IMMEDIATE_LOG_CHANGE
},
338 [EVENTS
][GET_INTERRUPT_POLICY
] = { "EVENTS_GET_INTERRUPT_POLICY",
339 cmd_events_get_interrupt_policy
, 0, 0 },
340 [EVENTS
][SET_INTERRUPT_POLICY
] = { "EVENTS_SET_INTERRUPT_POLICY",
341 cmd_events_set_interrupt_policy
, 4, IMMEDIATE_CONFIG_CHANGE
},
342 [FIRMWARE_UPDATE
][GET_INFO
] = { "FIRMWARE_UPDATE_GET_INFO",
343 cmd_firmware_update_get_info
, 0, 0 },
344 [TIMESTAMP
][GET
] = { "TIMESTAMP_GET", cmd_timestamp_get
, 0, 0 },
345 [TIMESTAMP
][SET
] = { "TIMESTAMP_SET", cmd_timestamp_set
, 8, IMMEDIATE_POLICY_CHANGE
},
346 [LOGS
][GET_SUPPORTED
] = { "LOGS_GET_SUPPORTED", cmd_logs_get_supported
, 0, 0 },
347 [LOGS
][GET_LOG
] = { "LOGS_GET_LOG", cmd_logs_get_log
, 0x18, 0 },
348 [IDENTIFY
][MEMORY_DEVICE
] = { "IDENTIFY_MEMORY_DEVICE",
349 cmd_identify_memory_device
, 0, 0 },
350 [CCLS
][GET_PARTITION_INFO
] = { "CCLS_GET_PARTITION_INFO",
351 cmd_ccls_get_partition_info
, 0, 0 },
354 void cxl_process_mailbox(CXLDeviceState
*cxl_dstate
)
356 uint16_t ret
= CXL_MBOX_SUCCESS
;
357 struct cxl_cmd
*cxl_cmd
;
360 uint64_t command_reg
= cxl_dstate
->mbox_reg_state64
[R_CXL_DEV_MAILBOX_CMD
];
362 uint8_t set
= FIELD_EX64(command_reg
, CXL_DEV_MAILBOX_CMD
, COMMAND_SET
);
363 uint8_t cmd
= FIELD_EX64(command_reg
, CXL_DEV_MAILBOX_CMD
, COMMAND
);
364 uint16_t len
= FIELD_EX64(command_reg
, CXL_DEV_MAILBOX_CMD
, LENGTH
);
365 cxl_cmd
= &cxl_cmd_set
[set
][cmd
];
366 h
= cxl_cmd
->handler
;
368 if (len
== cxl_cmd
->in
) {
369 cxl_cmd
->payload
= cxl_dstate
->mbox_reg_state
+
370 A_CXL_DEV_CMD_PAYLOAD
;
371 ret
= (*h
)(cxl_cmd
, cxl_dstate
, &len
);
372 assert(len
<= cxl_dstate
->payload_size
);
374 ret
= CXL_MBOX_INVALID_PAYLOAD_LENGTH
;
377 qemu_log_mask(LOG_UNIMP
, "Command %04xh not implemented\n",
379 ret
= CXL_MBOX_UNSUPPORTED
;
382 /* Set the return code */
383 status_reg
= FIELD_DP64(0, CXL_DEV_MAILBOX_STS
, ERRNO
, ret
);
385 /* Set the return length */
386 command_reg
= FIELD_DP64(command_reg
, CXL_DEV_MAILBOX_CMD
, COMMAND_SET
, 0);
387 command_reg
= FIELD_DP64(command_reg
, CXL_DEV_MAILBOX_CMD
, COMMAND
, 0);
388 command_reg
= FIELD_DP64(command_reg
, CXL_DEV_MAILBOX_CMD
, LENGTH
, len
);
390 cxl_dstate
->mbox_reg_state64
[R_CXL_DEV_MAILBOX_CMD
] = command_reg
;
391 cxl_dstate
->mbox_reg_state64
[R_CXL_DEV_MAILBOX_STS
] = status_reg
;
393 /* Tell the host we're done */
394 ARRAY_FIELD_DP32(cxl_dstate
->mbox_reg_state32
, CXL_DEV_MAILBOX_CTRL
,
398 int cxl_initialize_mailbox(CXLDeviceState
*cxl_dstate
)
400 /* CXL 2.0: Table 169 Get Supported Logs Log Entry */
401 const char *cel_uuidstr
= "0da9c0b5-bf41-4b78-8f79-96b1623b3f17";
403 for (int set
= 0; set
< 256; set
++) {
404 for (int cmd
= 0; cmd
< 256; cmd
++) {
405 if (cxl_cmd_set
[set
][cmd
].handler
) {
406 struct cxl_cmd
*c
= &cxl_cmd_set
[set
][cmd
];
407 struct cel_log
*log
=
408 &cxl_dstate
->cel_log
[cxl_dstate
->cel_size
];
410 log
->opcode
= (set
<< 8) | cmd
;
411 log
->effect
= c
->effect
;
412 cxl_dstate
->cel_size
++;
417 return qemu_uuid_parse(cel_uuidstr
, &cel_uuid
);