Skip to contentSkip to conversation

hw/sensor: add stub for ADM1266 blackbox

PATCH New Titus Rwantare 1 participant 2 messages Sep 8, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 hw/sensor: add stub for ADM1266 blackbox

This patch fixes a bug that causes a kernel panic when calling ADM1266_BLACKBOX_INFORMATION.

Log Message:

adm1266 5-0042: Could not read blackbox!

Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: adm1266_nvmem_read+0x168/0x170
---
 hw/sensor/adm1266.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
hw/sensor/adm1266.c +20 -0
index 9017ce6116..1112f78d19 100644
@@ -75,6 +75,12 @@ typedef struct ADM1266State {
 static const uint8_t adm1266_ic_device_id[] = {0x03, 0x41, 0x12, 0x66};
 static const uint8_t adm1266_ic_device_rev[] = {0x08, 0x01, 0x08, 0x07, 0x0,
                                                 0x0, 0x07, 0x41, 0x30};
+static const uint8_t adm1266_blackbox_info[] = {
+    0x04,       /* length */
+    0x00, 0x00, /* latest ID */
+    0x00,       /* latest record index */
+    0x00        /* latest record count */
+};
 
 static void adm1266_exit_reset(Object *obj, ResetType type)
 {
@@ -125,6 +131,17 @@ static uint8_t adm1266_read_byte(PMBusDevice *pmdev)
         pmbus_send(pmdev, adm1266_ic_device_rev, sizeof(adm1266_ic_device_rev));
         break;
 
+    case ADM1266_BLACKBOX_CONFIG:
+        break;
+
+    case ADM1266_READ_BLACKBOX:
+        break;
+
+    case ADM1266_BLACKBOX_INFORMATION:
+        pmbus_send(pmdev, adm1266_blackbox_info,
+                   sizeof(adm1266_blackbox_info));
+        break;
+
     default:
         qemu_log_mask(LOG_UNIMP,
                       "%s: reading from unimplemented register: 0x%02x\n",
@@ -154,6 +171,9 @@ static int adm1266_write_data(PMBusDevice *pmdev, const uint8_t *buf,
         pmbus_receive_block(pmdev, (uint8_t *)s->mfr_rev, sizeof(s->mfr_rev));
         break;
 
+    case ADM1266_BLACKBOX_CONFIG:
+    case ADM1266_READ_BLACKBOX:
+    case ADM1266_BLACKBOX_INFORMATION:
     case ADM1266_SET_RTC:   /* do nothing */
         break;
 
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
#1 lore raw

PATCH 2/2 hw/sensor: adm1266: add blackbox read and clear support

This commit adds some support for blackbox reads and clearing, triggering writes to the blackbox based on the sensor status is not yet implemented.

---
 hw/sensor/adm1266.c         | 139 ++++++++++++++++++++----------------
 include/hw/sensor/adm1266.h | 102 ++++++++++++++++++++++++++
 tests/qtest/adm1266-test.c  |  90 +++++++++++++++++------
 3 files changed, 247 insertions(+), 84 deletions(-)
 create mode 100644 include/hw/sensor/adm1266.h
hw/sensor/adm1266.c +77 -62
index 1112f78d19..e259bb4d67 100644
@@ -10,77 +10,31 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/i2c/pmbus_device.h"
+#include "hw/sensor/adm1266.h"
+
 #include "hw/irq.h"
 #include "migration/vmstate.h"
 #include "qapi/error.h"
 #include "qapi/visitor.h"
+#include "qemu/bswap.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
+#include "qemu/timer.h"
 
-#define TYPE_ADM1266 "adm1266"
-OBJECT_DECLARE_SIMPLE_TYPE(ADM1266State, ADM1266)
-
-#define ADM1266_BLACKBOX_CONFIG                 0xD3
-#define ADM1266_PDIO_CONFIG                     0xD4
-#define ADM1266_READ_STATE                      0xD9
-#define ADM1266_READ_BLACKBOX                   0xDE
-#define ADM1266_SET_RTC                         0xDF
-#define ADM1266_GPIO_SYNC_CONFIGURATION         0xE1
-#define ADM1266_BLACKBOX_INFORMATION            0xE6
-#define ADM1266_PDIO_STATUS                     0xE9
-#define ADM1266_GPIO_STATUS                     0xEA
-
-/* Defaults */
-#define ADM1266_OPERATION_DEFAULT               0x80
-#define ADM1266_CAPABILITY_DEFAULT              0xA0
-#define ADM1266_CAPABILITY_NO_PEC               0x20
-#define ADM1266_PMBUS_REVISION_DEFAULT          0x22
-#define ADM1266_MFR_ID_DEFAULT                  "ADI"
-#define ADM1266_MFR_ID_DEFAULT_LEN              32
-#define ADM1266_MFR_MODEL_DEFAULT               "ADM1266-A1"
-#define ADM1266_MFR_MODEL_DEFAULT_LEN           32
-#define ADM1266_MFR_REVISION_DEFAULT            "25"
-#define ADM1266_MFR_REVISION_DEFAULT_LEN        8
-
-#define ADM1266_NUM_PAGES               17
-/**
- * PAGE Index
- * Page 0 VH1.
- * Page 1 VH2.
- * Page 2 VH3.
- * Page 3 VH4.
- * Page 4 VP1.
- * Page 5 VP2.
- * Page 6 VP3.
- * Page 7 VP4.
- * Page 8 VP5.
- * Page 9 VP6.
- * Page 10 VP7.
- * Page 11 VP8.
- * Page 12 VP9.
- * Page 13 VP10.
- * Page 14 VP11.
- * Page 15 VP12.
- * Page 16 VP13.
- */
-typedef struct ADM1266State {
-    PMBusDevice parent;
-
-    char mfr_id[32];
-    char mfr_model[32];
-    char mfr_rev[8];
-} ADM1266State;
 
 static const uint8_t adm1266_ic_device_id[] = {0x03, 0x41, 0x12, 0x66};
 static const uint8_t adm1266_ic_device_rev[] = {0x08, 0x01, 0x08, 0x07, 0x0,
                                                 0x0, 0x07, 0x41, 0x30};
-static const uint8_t adm1266_blackbox_info[] = {
-    0x04,       /* length */
-    0x00, 0x00, /* latest ID */
-    0x00,       /* latest record index */
-    0x00        /* latest record count */
-};
+
+static void adm1266_clear_blackbox(ADM1266State *s)
+{
+    memset(s->blackbox_records, 0, sizeof(s->blackbox_records));
+    for (int i = 0; i < ADM1266_BLACKBOX_RECORD_NUM_MAX; i++) {
+        s->blackbox_records[i].flags = ADM1266_RECORD_EMPTY;
+    }
+    s->blackbox_info.record_count = 0;
+    s->blackbox_info.logic_index = 0;
+}
 
 static void adm1266_exit_reset(Object *obj, ResetType type)
 {
@@ -101,6 +55,18 @@ static void adm1266_exit_reset(Object *obj, ResetType type)
         pmdev->pages[i].revision = ADM1266_PMBUS_REVISION_DEFAULT;
     }
 
+    adm1266_clear_blackbox(s);
+    for (int i = 0; i < ADM1266_RECORD_COUNT_DEFAULT; i++) {
+        s->blackbox_records[i].id = cpu_to_le16(i);
+        s->blackbox_records[i].flags = ADM1266_RECORD_USED;
+        s->blackbox_records[i].powerup_counter = cpu_to_le16(i);
+        s->blackbox_records[i].timestamp =
+            cpu_to_le64((qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000) << 8);
+        s->blackbox_info.logic_index = i;
+    }
+    s->partial_read = false;
+    s->blackbox_info.record_count = ADM1266_RECORD_COUNT_DEFAULT;
+
     strncpy(s->mfr_id, ADM1266_MFR_ID_DEFAULT, 4);
     strncpy(s->mfr_model, ADM1266_MFR_MODEL_DEFAULT, 11);
     strncpy(s->mfr_rev, ADM1266_MFR_REVISION_DEFAULT, 3);
@@ -109,6 +75,7 @@ static void adm1266_exit_reset(Object *obj, ResetType type)
 static uint8_t adm1266_read_byte(PMBusDevice *pmdev)
 {
     ADM1266State *s = ADM1266(pmdev);
+    uint8_t *raw_record;
 
     switch (pmdev->code) {
     case PMBUS_MFR_ID:                    /* R/W block */
@@ -132,14 +99,29 @@ static uint8_t adm1266_read_byte(PMBusDevice *pmdev)
         break;
 
     case ADM1266_BLACKBOX_CONFIG:
+        pmbus_send16(pmdev, s->blackbox_config &
+                     ADM1266_BLACKBOX_CONFIG_CYCLIC);
         break;
 
     case ADM1266_READ_BLACKBOX:
+        raw_record =
+                (uint8_t *)&s->blackbox_records[s->blackbox_info.logic_index];
+        /* The full black box record exceeds SMBUS_DATA_MAX_LEN */
+        if (!s->partial_read) {
+            s->partial_read = true;
+        } else {
+            raw_record += sizeof(ADM1266BlackboxRecord) / 2;
+            s->partial_read = false;
+        }
+        pmbus_send(pmdev, raw_record,
+                   sizeof(ADM1266BlackboxRecord) / 2);         /* data */
+        pmbus_send8(pmdev, sizeof(ADM1266BlackboxRecord) / 2); /* len */
         break;
 
     case ADM1266_BLACKBOX_INFORMATION:
-        pmbus_send(pmdev, adm1266_blackbox_info,
-                   sizeof(adm1266_blackbox_info));
+        pmbus_send(pmdev, (const uint8_t *)&s->blackbox_info,
+                   sizeof(s->blackbox_info));            /* data */
+        pmbus_send8(pmdev, sizeof(ADM1266BlackboxInfo)); /* len */
         break;
 
     default:
@@ -156,6 +138,7 @@ static int adm1266_write_data(PMBusDevice *pmdev, const uint8_t *buf,
                               uint8_t len)
 {
     ADM1266State *s = ADM1266(pmdev);
+    uint8_t blackbox_request[2];
 
     switch (pmdev->code) {
     case PMBUS_MFR_ID:                    /* R/W block */
@@ -172,8 +155,40 @@ static int adm1266_write_data(PMBusDevice *pmdev, const uint8_t *buf,
         break;
 
     case ADM1266_BLACKBOX_CONFIG:
+        s->blackbox_config = pmbus_receive16(pmdev) &
+                                ADM1266_BLACKBOX_CONFIG_CYCLIC;
+        break;
+
+    /*
+     * Writes to READ_BLACKBOX come in two forms.
+     * Read requests are a two byte block write, {0x1, record_index}
+     * Clearing records is a three byte block write, {0x2, 0xFE, 0x00}
+     */
     case ADM1266_READ_BLACKBOX:
+        blackbox_request[0] = pmbus_receive_block(pmdev,
+                                (uint8_t *)&blackbox_request[1],
+                                sizeof(blackbox_request));
+        if (blackbox_request[0] == 1) {
+            s->blackbox_info.logic_index = blackbox_request[1];
+            s->partial_read = false;
+            break;
+        }
+        /* erase blackbox records */
+        if (blackbox_request[0] == 2 && blackbox_request[1] == 0xFE) {
+            adm1266_clear_blackbox(s);
+            break;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: invalid write to READ_BLACKBOX: 0x%02x%02x",
+                      __func__, blackbox_request[0], blackbox_request[1]);
+        break;
+
     case ADM1266_BLACKBOX_INFORMATION:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: write to read-only register",
+                      __func__);
+        break;
+
     case ADM1266_SET_RTC:   /* do nothing */
         break;
 
include/hw/sensor/adm1266.h new file +102 -0

102 lines not on this page · raw message

tests/qtest/adm1266-test.c +68 -22

118 lines not on this page · raw message

Signature · 1 line
2.51.0.384.g4c02a37b29-goog