Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
hw/cxl/events: Wire up get/clear event mailbox commands
CXL testing is benefited from an artificial event log injection
mechanism.

Add an event log infrastructure to insert, get, and clear events from
the various logs available on a device.

Replace the stubbed out CXL Get/Clear Event mailbox commands with
commands that operate on the new infrastructure.

Signed-off-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Fan Ni <fan.ni@samsung.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Message-Id: <20230530133603.16934-4-Jonathan.Cameron@huawei.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
  • Loading branch information
weiny2 authored and mstsirkin committed Jun 22, 2023
1 parent 2f6b8c8 commit 22d7e3b
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 2 deletions.
217 changes: 217 additions & 0 deletions hw/cxl/cxl-events.c
@@ -0,0 +1,217 @@
/*
* CXL Event processing
*
* Copyright(C) 2023 Intel Corporation.
*
* This work is licensed under the terms of the GNU GPL, version 2. See the
* COPYING file in the top-level directory.
*/

#include <stdint.h>

#include "qemu/osdep.h"
#include "qemu/bswap.h"
#include "qemu/typedefs.h"
#include "qemu/error-report.h"
#include "hw/cxl/cxl.h"
#include "hw/cxl/cxl_events.h"

/* Artificial limit on the number of events a log can hold */
#define CXL_TEST_EVENT_OVERFLOW 8

static void reset_overflow(CXLEventLog *log)
{
log->overflow_err_count = 0;
log->first_overflow_timestamp = 0;
log->last_overflow_timestamp = 0;
}

void cxl_event_init(CXLDeviceState *cxlds)
{
CXLEventLog *log;
int i;

for (i = 0; i < CXL_EVENT_TYPE_MAX; i++) {
log = &cxlds->event_logs[i];
log->next_handle = 1;
log->overflow_err_count = 0;
log->first_overflow_timestamp = 0;
log->last_overflow_timestamp = 0;
qemu_mutex_init(&log->lock);
QSIMPLEQ_INIT(&log->events);
}
}

static CXLEvent *cxl_event_get_head(CXLEventLog *log)
{
return QSIMPLEQ_FIRST(&log->events);
}

static CXLEvent *cxl_event_get_next(CXLEvent *entry)
{
return QSIMPLEQ_NEXT(entry, node);
}

static int cxl_event_count(CXLEventLog *log)
{
CXLEvent *event;
int rc = 0;

QSIMPLEQ_FOREACH(event, &log->events, node) {
rc++;
}

return rc;
}

static bool cxl_event_empty(CXLEventLog *log)
{
return QSIMPLEQ_EMPTY(&log->events);
}

static void cxl_event_delete_head(CXLDeviceState *cxlds,
CXLEventLogType log_type,
CXLEventLog *log)
{
CXLEvent *entry = cxl_event_get_head(log);

reset_overflow(log);
QSIMPLEQ_REMOVE_HEAD(&log->events, node);
if (cxl_event_empty(log)) {
cxl_event_set_status(cxlds, log_type, false);
}
g_free(entry);
}

/*
* return true if an interrupt should be generated as a result
* of inserting this event.
*/
bool cxl_event_insert(CXLDeviceState *cxlds, CXLEventLogType log_type,
CXLEventRecordRaw *event)
{
uint64_t time;
CXLEventLog *log;
CXLEvent *entry;

if (log_type >= CXL_EVENT_TYPE_MAX) {
return false;
}

time = cxl_device_get_timestamp(cxlds);

log = &cxlds->event_logs[log_type];

QEMU_LOCK_GUARD(&log->lock);

if (cxl_event_count(log) >= CXL_TEST_EVENT_OVERFLOW) {
if (log->overflow_err_count == 0) {
log->first_overflow_timestamp = time;
}
log->overflow_err_count++;
log->last_overflow_timestamp = time;
return false;
}

entry = g_new0(CXLEvent, 1);

memcpy(&entry->data, event, sizeof(*event));

entry->data.hdr.handle = cpu_to_le16(log->next_handle);
log->next_handle++;
/* 0 handle is never valid */
if (log->next_handle == 0) {
log->next_handle++;
}
entry->data.hdr.timestamp = cpu_to_le64(time);

QSIMPLEQ_INSERT_TAIL(&log->events, entry, node);
cxl_event_set_status(cxlds, log_type, true);

/* Count went from 0 to 1 */
return cxl_event_count(log) == 1;
}

CXLRetCode cxl_event_get_records(CXLDeviceState *cxlds, CXLGetEventPayload *pl,
uint8_t log_type, int max_recs,
uint16_t *len)
{
CXLEventLog *log;
CXLEvent *entry;
uint16_t nr;

if (log_type >= CXL_EVENT_TYPE_MAX) {
return CXL_MBOX_INVALID_INPUT;
}

log = &cxlds->event_logs[log_type];

QEMU_LOCK_GUARD(&log->lock);

entry = cxl_event_get_head(log);
for (nr = 0; entry && nr < max_recs; nr++) {
memcpy(&pl->records[nr], &entry->data, CXL_EVENT_RECORD_SIZE);
entry = cxl_event_get_next(entry);
}

if (!cxl_event_empty(log)) {
pl->flags |= CXL_GET_EVENT_FLAG_MORE_RECORDS;
}

if (log->overflow_err_count) {
pl->flags |= CXL_GET_EVENT_FLAG_OVERFLOW;
pl->overflow_err_count = cpu_to_le16(log->overflow_err_count);
pl->first_overflow_timestamp = cpu_to_le64(log->first_overflow_timestamp);
pl->last_overflow_timestamp = cpu_to_le64(log->last_overflow_timestamp);
}

pl->record_count = cpu_to_le16(nr);
*len = CXL_EVENT_PAYLOAD_HDR_SIZE + (CXL_EVENT_RECORD_SIZE * nr);

return CXL_MBOX_SUCCESS;
}

CXLRetCode cxl_event_clear_records(CXLDeviceState *cxlds, CXLClearEventPayload *pl)
{
CXLEventLog *log;
uint8_t log_type;
CXLEvent *entry;
int nr;

log_type = pl->event_log;

if (log_type >= CXL_EVENT_TYPE_MAX) {
return CXL_MBOX_INVALID_INPUT;
}

log = &cxlds->event_logs[log_type];

QEMU_LOCK_GUARD(&log->lock);
/*
* Must itterate the queue twice.
* "The device shall verify the event record handles specified in the input
* payload are in temporal order. If the device detects an older event
* record that will not be cleared when Clear Event Records is executed,
* the device shall return the Invalid Handle return code and shall not
* clear any of the specified event records."
* -- CXL 3.0 8.2.9.2.3
*/
entry = cxl_event_get_head(log);
for (nr = 0; entry && nr < pl->nr_recs; nr++) {
uint16_t handle = pl->handle[nr];

/* NOTE: Both handles are little endian. */
if (handle == 0 || entry->data.hdr.handle != handle) {
return CXL_MBOX_INVALID_INPUT;
}
entry = cxl_event_get_next(entry);
}

entry = cxl_event_get_head(log);
for (nr = 0; entry && nr < pl->nr_recs; nr++) {
cxl_event_delete_head(cxlds, log_type, log);
entry = cxl_event_get_head(log);
}

return CXL_MBOX_SUCCESS;
}
40 changes: 38 additions & 2 deletions hw/cxl/cxl-mailbox-utils.c
Expand Up @@ -9,6 +9,7 @@

#include "qemu/osdep.h"
#include "hw/cxl/cxl.h"
#include "hw/cxl/cxl_events.h"
#include "hw/pci/pci.h"
#include "qemu/cutils.h"
#include "qemu/log.h"
Expand Down Expand Up @@ -95,11 +96,46 @@ struct cxl_cmd {
return CXL_MBOX_SUCCESS; \
}

DEFINE_MAILBOX_HANDLER_ZEROED(events_get_records, 0x20);
DEFINE_MAILBOX_HANDLER_NOP(events_clear_records);
DEFINE_MAILBOX_HANDLER_ZEROED(events_get_interrupt_policy, 4);
DEFINE_MAILBOX_HANDLER_NOP(events_set_interrupt_policy);

static CXLRetCode cmd_events_get_records(struct cxl_cmd *cmd,
CXLDeviceState *cxlds,
uint16_t *len)
{
CXLGetEventPayload *pl;
uint8_t log_type;
int max_recs;

if (cmd->in < sizeof(log_type)) {
return CXL_MBOX_INVALID_INPUT;
}

log_type = *((uint8_t *)cmd->payload);

pl = (CXLGetEventPayload *)cmd->payload;
memset(pl, 0, sizeof(*pl));

max_recs = (cxlds->payload_size - CXL_EVENT_PAYLOAD_HDR_SIZE) /
CXL_EVENT_RECORD_SIZE;
if (max_recs > 0xFFFF) {
max_recs = 0xFFFF;
}

return cxl_event_get_records(cxlds, pl, log_type, max_recs, len);
}

static CXLRetCode cmd_events_clear_records(struct cxl_cmd *cmd,
CXLDeviceState *cxlds,
uint16_t *len)
{
CXLClearEventPayload *pl;

pl = (CXLClearEventPayload *)cmd->payload;
*len = 0;
return cxl_event_clear_records(cxlds, pl);
}

/* 8.2.9.2.1 */
static CXLRetCode cmd_firmware_update_get_info(struct cxl_cmd *cmd,
CXLDeviceState *cxl_dstate,
Expand Down
1 change: 1 addition & 0 deletions hw/cxl/meson.build
Expand Up @@ -5,6 +5,7 @@ system_ss.add(when: 'CONFIG_CXL',
'cxl-mailbox-utils.c',
'cxl-host.c',
'cxl-cdat.c',
'cxl-events.c',
),
if_false: files(
'cxl-host-stubs.c',
Expand Down
1 change: 1 addition & 0 deletions hw/mem/cxl_type3.c
Expand Up @@ -724,6 +724,7 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp)
goto err_release_cdat;
}

cxl_event_init(&ct3d->cxl_dstate);
return;

err_release_cdat:
Expand Down
25 changes: 25 additions & 0 deletions include/hw/cxl/cxl_device.h
Expand Up @@ -111,6 +111,20 @@ typedef enum {
CXL_MBOX_MAX = 0x17
} CXLRetCode;

typedef struct CXLEvent {
CXLEventRecordRaw data;
QSIMPLEQ_ENTRY(CXLEvent) node;
} CXLEvent;

typedef struct CXLEventLog {
uint16_t next_handle;
uint16_t overflow_err_count;
uint64_t first_overflow_timestamp;
uint64_t last_overflow_timestamp;
QemuMutex lock;
QSIMPLEQ_HEAD(, CXLEvent) events;
} CXLEventLog;

typedef struct cxl_device_state {
MemoryRegion device_registers;

Expand Down Expand Up @@ -161,6 +175,8 @@ typedef struct cxl_device_state {
uint64_t mem_size;
uint64_t pmem_size;
uint64_t vmem_size;

CXLEventLog event_logs[CXL_EVENT_TYPE_MAX];
} CXLDeviceState;

/* Initialize the register block for a device */
Expand Down Expand Up @@ -353,6 +369,15 @@ MemTxResult cxl_type3_write(PCIDevice *d, hwaddr host_addr, uint64_t data,

uint64_t cxl_device_get_timestamp(CXLDeviceState *cxlds);

void cxl_event_init(CXLDeviceState *cxlds);
bool cxl_event_insert(CXLDeviceState *cxlds, CXLEventLogType log_type,
CXLEventRecordRaw *event);
CXLRetCode cxl_event_get_records(CXLDeviceState *cxlds, CXLGetEventPayload *pl,
uint8_t log_type, int max_recs,
uint16_t *len);
CXLRetCode cxl_event_clear_records(CXLDeviceState *cxlds,
CXLClearEventPayload *pl);

void cxl_set_poison_list_overflowed(CXLType3Dev *ct3d);

#endif

0 comments on commit 22d7e3b

Please sign in to comment.