cxl-events.c 6.9 KB

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