1 /* 2 * CXL Event processing 3 * 4 * Copyright(C) 2023 Intel Corporation. 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 #include <stdint.h> 11 12 #include "qemu/osdep.h" 13 #include "qemu/bswap.h" 14 #include "qemu/typedefs.h" 15 #include "qemu/error-report.h" 16 #include "hw/cxl/cxl.h" 17 #include "hw/cxl/cxl_events.h" 18 19 /* Artificial limit on the number of events a log can hold */ 20 #define CXL_TEST_EVENT_OVERFLOW 8 21 22 static void reset_overflow(CXLEventLog *log) 23 { 24 log->overflow_err_count = 0; 25 log->first_overflow_timestamp = 0; 26 log->last_overflow_timestamp = 0; 27 } 28 29 void cxl_event_init(CXLDeviceState *cxlds) 30 { 31 CXLEventLog *log; 32 int i; 33 34 for (i = 0; i < CXL_EVENT_TYPE_MAX; i++) { 35 log = &cxlds->event_logs[i]; 36 log->next_handle = 1; 37 log->overflow_err_count = 0; 38 log->first_overflow_timestamp = 0; 39 log->last_overflow_timestamp = 0; 40 qemu_mutex_init(&log->lock); 41 QSIMPLEQ_INIT(&log->events); 42 } 43 } 44 45 static CXLEvent *cxl_event_get_head(CXLEventLog *log) 46 { 47 return QSIMPLEQ_FIRST(&log->events); 48 } 49 50 static CXLEvent *cxl_event_get_next(CXLEvent *entry) 51 { 52 return QSIMPLEQ_NEXT(entry, node); 53 } 54 55 static int cxl_event_count(CXLEventLog *log) 56 { 57 CXLEvent *event; 58 int rc = 0; 59 60 QSIMPLEQ_FOREACH(event, &log->events, node) { 61 rc++; 62 } 63 64 return rc; 65 } 66 67 static bool cxl_event_empty(CXLEventLog *log) 68 { 69 return QSIMPLEQ_EMPTY(&log->events); 70 } 71 72 static void cxl_event_delete_head(CXLDeviceState *cxlds, 73 CXLEventLogType log_type, 74 CXLEventLog *log) 75 { 76 CXLEvent *entry = cxl_event_get_head(log); 77 78 reset_overflow(log); 79 QSIMPLEQ_REMOVE_HEAD(&log->events, node); 80 if (cxl_event_empty(log)) { 81 cxl_event_set_status(cxlds, log_type, false); 82 } 83 g_free(entry); 84 } 85 86 /* 87 * return true if an interrupt should be generated as a result 88 * of inserting this event. 89 */ 90 bool cxl_event_insert(CXLDeviceState *cxlds, CXLEventLogType log_type, 91 CXLEventRecordRaw *event) 92 { 93 uint64_t time; 94 CXLEventLog *log; 95 CXLEvent *entry; 96 97 if (log_type >= CXL_EVENT_TYPE_MAX) { 98 return false; 99 } 100 101 time = cxl_device_get_timestamp(cxlds); 102 103 log = &cxlds->event_logs[log_type]; 104 105 QEMU_LOCK_GUARD(&log->lock); 106 107 if (cxl_event_count(log) >= CXL_TEST_EVENT_OVERFLOW) { 108 if (log->overflow_err_count == 0) { 109 log->first_overflow_timestamp = time; 110 } 111 log->overflow_err_count++; 112 log->last_overflow_timestamp = time; 113 return false; 114 } 115 116 entry = g_new0(CXLEvent, 1); 117 118 memcpy(&entry->data, event, sizeof(*event)); 119 120 entry->data.hdr.handle = cpu_to_le16(log->next_handle); 121 log->next_handle++; 122 /* 0 handle is never valid */ 123 if (log->next_handle == 0) { 124 log->next_handle++; 125 } 126 entry->data.hdr.timestamp = cpu_to_le64(time); 127 128 QSIMPLEQ_INSERT_TAIL(&log->events, entry, node); 129 cxl_event_set_status(cxlds, log_type, true); 130 131 /* Count went from 0 to 1 */ 132 return cxl_event_count(log) == 1; 133 } 134 135 CXLRetCode cxl_event_get_records(CXLDeviceState *cxlds, CXLGetEventPayload *pl, 136 uint8_t log_type, int max_recs, 137 uint16_t *len) 138 { 139 CXLEventLog *log; 140 CXLEvent *entry; 141 uint16_t nr; 142 143 if (log_type >= CXL_EVENT_TYPE_MAX) { 144 return CXL_MBOX_INVALID_INPUT; 145 } 146 147 log = &cxlds->event_logs[log_type]; 148 149 QEMU_LOCK_GUARD(&log->lock); 150 151 entry = cxl_event_get_head(log); 152 for (nr = 0; entry && nr < max_recs; nr++) { 153 memcpy(&pl->records[nr], &entry->data, CXL_EVENT_RECORD_SIZE); 154 entry = cxl_event_get_next(entry); 155 } 156 157 if (!cxl_event_empty(log)) { 158 pl->flags |= CXL_GET_EVENT_FLAG_MORE_RECORDS; 159 } 160 161 if (log->overflow_err_count) { 162 pl->flags |= CXL_GET_EVENT_FLAG_OVERFLOW; 163 pl->overflow_err_count = cpu_to_le16(log->overflow_err_count); 164 pl->first_overflow_timestamp = cpu_to_le64(log->first_overflow_timestamp); 165 pl->last_overflow_timestamp = cpu_to_le64(log->last_overflow_timestamp); 166 } 167 168 pl->record_count = cpu_to_le16(nr); 169 *len = CXL_EVENT_PAYLOAD_HDR_SIZE + (CXL_EVENT_RECORD_SIZE * nr); 170 171 return CXL_MBOX_SUCCESS; 172 } 173 174 CXLRetCode cxl_event_clear_records(CXLDeviceState *cxlds, CXLClearEventPayload *pl) 175 { 176 CXLEventLog *log; 177 uint8_t log_type; 178 CXLEvent *entry; 179 int nr; 180 181 log_type = pl->event_log; 182 183 if (log_type >= CXL_EVENT_TYPE_MAX) { 184 return CXL_MBOX_INVALID_INPUT; 185 } 186 187 log = &cxlds->event_logs[log_type]; 188 189 QEMU_LOCK_GUARD(&log->lock); 190 /* 191 * Must itterate the queue twice. 192 * "The device shall verify the event record handles specified in the input 193 * payload are in temporal order. If the device detects an older event 194 * record that will not be cleared when Clear Event Records is executed, 195 * the device shall return the Invalid Handle return code and shall not 196 * clear any of the specified event records." 197 * -- CXL 3.0 8.2.9.2.3 198 */ 199 entry = cxl_event_get_head(log); 200 for (nr = 0; entry && nr < pl->nr_recs; nr++) { 201 uint16_t handle = pl->handle[nr]; 202 203 /* NOTE: Both handles are little endian. */ 204 if (handle == 0 || entry->data.hdr.handle != handle) { 205 return CXL_MBOX_INVALID_INPUT; 206 } 207 entry = cxl_event_get_next(entry); 208 } 209 210 entry = cxl_event_get_head(log); 211 for (nr = 0; entry && nr < pl->nr_recs; nr++) { 212 cxl_event_delete_head(cxlds, log_type, log); 213 entry = cxl_event_get_head(log); 214 } 215 216 return CXL_MBOX_SUCCESS; 217 } 218