Skip to contentSkip to conversation

hw/sensor: Add SB-TSI Temperature Sensor Interface

PATCH Superseded Patrick Venture 1 participant 3 messages Jan 13, 2022 Arm/Amlogic Meson SoC support

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover hw/sensor: Add SB-TSI Temperature Sensor Interface

v3:
* typofix where I accidentally embedded 'wq' into a string
* moved the type sbtsi definition back into the source file
* renamed the qtest file to use hyphens only

v2:
* Split the commit into a separate patch for the qtest
* Moved the common registers into the new header
* Introduced a new header

Hao Wu (2):
hw/sensor: Add SB-TSI Temperature Sensor Interface
tests: add qtest for hw/sensor/sbtsi

 meson.build                  |   1 +
 hw/sensor/trace.h            |   1 +
 include/hw/sensor/sbtsi.h    |  45 +++++
 hw/sensor/tmp_sbtsi.c        | 369 +++++++++++++++++++++++++++++++++++
 tests/qtest/tmp-sbtsi-test.c | 161 +++++++++++++++
 hw/sensor/Kconfig            |   4 +
 hw/sensor/meson.build        |   1 +
 hw/sensor/trace-events       |   5 +
 tests/qtest/meson.build      |   1 +
 9 files changed, 588 insertions(+)
 create mode 100644 hw/sensor/trace.h
 create mode 100644 include/hw/sensor/sbtsi.h
 create mode 100644 hw/sensor/tmp_sbtsi.c
 create mode 100644 tests/qtest/tmp-sbtsi-test.c
 create mode 100644 hw/sensor/trace-events
Signature · 1 line
2.34.1.575.g55b058a8bb-goog
#1 lore raw

PATCH v3 1/2 hw/sensor: Add SB-TSI Temperature Sensor Interface

From: Hao Wu <[email protected]>

SB Temperature Sensor Interface (SB-TSI) is an SMBus compatible interface that reports AMD SoC's Ttcl (normalized temperature), and resembles a typical 8-pin remote temperature sensor's I2C interface to BMC.

This patch implements a basic AMD SB-TSI sensor that is compatible with the open-source data sheet from AMD and Linux kernel driver.

Reference:
Linux kernel driver:
https://lkml.org/lkml/2020/12/11/968
Register Map:
https://developer.amd.com/wp-content/resources/56255_3_03.PDF
(Chapter 6)

---
 meson.build               |   1 +
 hw/sensor/trace.h         |   1 +
 include/hw/sensor/sbtsi.h |  45 +++++
 hw/sensor/tmp_sbtsi.c     | 369 ++++++++++++++++++++++++++++++++++++++
 hw/sensor/Kconfig         |   4 +
 hw/sensor/meson.build     |   1 +
 hw/sensor/trace-events    |   5 +
 7 files changed, 426 insertions(+)
 create mode 100644 hw/sensor/trace.h
 create mode 100644 include/hw/sensor/sbtsi.h
 create mode 100644 hw/sensor/tmp_sbtsi.c
 create mode 100644 hw/sensor/trace-events
meson.build +1 -0
index c1b1db1e28..3634214546 100644
@@ -2494,6 +2494,7 @@ if have_system
     'hw/rtc',
     'hw/s390x',
     'hw/scsi',
+    'hw/sensor',
     'hw/sd',
     'hw/sh4',
     'hw/sparc',
hw/sensor/trace.h new file +1 -0
new file mode 100644
index 0000000000..e4721560b0
@@ -0,0 +1 @@
+#include "trace/trace-hw_sensor.h"
include/hw/sensor/sbtsi.h new file +45 -0

45 lines not on this page · raw message

hw/sensor/tmp_sbtsi.c new file +369 -0

369 lines not on this page · raw message

hw/sensor/Kconfig +4 -0
index 9c8a049b06..27f6f79c84 100644
@@ -21,3 +21,7 @@ config ADM1272
 config MAX34451
     bool
     depends on I2C
+
+config AMDSBTSI
+    bool
+    depends on I2C
hw/sensor/meson.build +1 -0
index 059c4ca935..f7b0e645eb 100644
@@ -4,3 +4,4 @@ softmmu_ss.add(when: 'CONFIG_DPS310', if_true: files('dps310.c'))
 softmmu_ss.add(when: 'CONFIG_EMC141X', if_true: files('emc141x.c'))
 softmmu_ss.add(when: 'CONFIG_ADM1272', if_true: files('adm1272.c'))
 softmmu_ss.add(when: 'CONFIG_MAX34451', if_true: files('max34451.c'))
+softmmu_ss.add(when: 'CONFIG_AMDSBTSI', if_true: files('tmp_sbtsi.c'))
hw/sensor/trace-events new file +5 -0
new file mode 100644
index 0000000000..21ab3809e2
@@ -0,0 +1,5 @@
+# See docs/devel/tracing.rst for syntax documentation.
+
+# tmp_sbtsi.c
+tmp_sbtsi_write_data(uint8_t addr, uint8_t value) "SBTSI write addr:0x%02x data: 0x%02x"
+tmp_sbtsi_read_data(uint8_t addr, uint8_t value) "SBTSI read addr:0x%02x data: 0x%02x"
Signature · 1 line
2.34.1.575.g55b058a8bb-goog
#2 lore raw

PATCH v3 2/2 tests: add qtest for hw/sensor/sbtsi

In reply to Patrick Venture

From: Hao Wu <[email protected]>

---
 tests/qtest/tmp-sbtsi-test.c | 161 +++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build      |   1 +
 2 files changed, 162 insertions(+)
 create mode 100644 tests/qtest/tmp-sbtsi-test.c
tests/qtest/tmp-sbtsi-test.c new file +161 -0
new file mode 100644
index 0000000000..ff1e193739
@@ -0,0 +1,161 @@
+/*
+ * QTests for the SBTSI temperature sensor
+ *
+ * Copyright 2020 Google LLC
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include "qemu/osdep.h"
+
+#include "libqtest-single.h"
+#include "libqos/qgraph.h"
+#include "libqos/i2c.h"
+#include "qapi/qmp/qdict.h"
+#include "qemu/bitops.h"
+#include "hw/sensor/sbtsi.h"
+
+#define TEST_ID   "sbtsi-test"
+#define TEST_ADDR (0x4c)
+
+/* The temperature stored are in units of 0.125 degrees. */
+#define LIMIT_LOW_IN_MILLIDEGREE (10500)
+#define LIMIT_HIGH_IN_MILLIDEGREE (55125)
+
+static uint32_t qmp_sbtsi_get_temperature(const char *id)
+{
+    QDict *response;
+    int ret;
+
+    response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
+                   "'property': 'temperature' } }", id);
+    g_assert(qdict_haskey(response, "return"));
+    ret = (uint32_t)qdict_get_int(response, "return");
+    qobject_unref(response);
+    return ret;
+}
+
+static void qmp_sbtsi_set_temperature(const char *id, uint32_t value)
+{
+    QDict *response;
+
+    response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
+                   "'property': 'temperature', 'value': %d } }", id, value);
+    g_assert(qdict_haskey(response, "return"));
+    qobject_unref(response);
+}
+
+/*
+ * Compute the temperature using the integer and decimal part and return
+ * millidegrees. The decimal part are only the top 3 bits so we shift it by
+ * 5 here.
+ */
+static uint32_t regs_to_temp(uint8_t integer, uint8_t decimal)
+{
+    return ((integer << 3) + (decimal >> 5)) * SBTSI_TEMP_UNIT_IN_MILLIDEGREE;
+}
+
+/*
+ * Compute the integer and decimal parts of the temperature in millidegrees.
+ * H/W store the decimal in the top 3 bits so we shift it by 5.
+ */
+static void temp_to_regs(uint32_t temp, uint8_t *integer, uint8_t *decimal)
+{
+    temp /= SBTSI_TEMP_UNIT_IN_MILLIDEGREE;
+    *integer = temp >> 3;
+    *decimal = (temp & 0x7) << 5;
+}
+
+static void tx_rx(void *obj, void *data, QGuestAllocator *alloc)
+{
+    uint16_t value;
+    uint8_t integer, decimal;
+    QI2CDevice *i2cdev = (QI2CDevice *)obj;
+
+    /* Test default values */
+    value = qmp_sbtsi_get_temperature(TEST_ID);
+    g_assert_cmpuint(value, ==, 0);
+
+    integer = i2c_get8(i2cdev, SBTSI_REG_TEMP_INT);
+    decimal = i2c_get8(i2cdev, SBTSI_REG_TEMP_DEC);
+    g_assert_cmpuint(regs_to_temp(integer, decimal), ==, 0);
+
+    /* Test setting temperature */
+    qmp_sbtsi_set_temperature(TEST_ID, 20000);
+    value = qmp_sbtsi_get_temperature(TEST_ID);
+    g_assert_cmpuint(value, ==, 20000);
+
+    integer = i2c_get8(i2cdev, SBTSI_REG_TEMP_INT);
+    decimal = i2c_get8(i2cdev, SBTSI_REG_TEMP_DEC);
+    g_assert_cmpuint(regs_to_temp(integer, decimal), ==, 20000);
+
+    /* Set alert mask in config */
+    i2c_set8(i2cdev, SBTSI_REG_CONFIG_WR, SBTSI_CONFIG_ALERT_MASK);
+    value = i2c_get8(i2cdev, SBTSI_REG_CONFIG);
+    g_assert_cmphex(value, ==, SBTSI_CONFIG_ALERT_MASK);
+    /* Enable alarm_en */
+    i2c_set8(i2cdev, SBTSI_REG_ALERT_CONFIG, SBTSI_ALARM_EN);
+    value = i2c_get8(i2cdev, SBTSI_REG_ALERT_CONFIG);
+    g_assert_cmphex(value, ==, SBTSI_ALARM_EN);
+
+    /* Test setting limits */
+    /* Limit low = 10.500 */
+    temp_to_regs(LIMIT_LOW_IN_MILLIDEGREE, &integer, &decimal);
+    i2c_set8(i2cdev, SBTSI_REG_TEMP_LOW_INT, integer);
+    i2c_set8(i2cdev, SBTSI_REG_TEMP_LOW_DEC, decimal);
+    integer = i2c_get8(i2cdev, SBTSI_REG_TEMP_LOW_INT);
+    decimal = i2c_get8(i2cdev, SBTSI_REG_TEMP_LOW_DEC);
+    g_assert_cmpuint(
+        regs_to_temp(integer, decimal), ==, LIMIT_LOW_IN_MILLIDEGREE);
+    /* Limit high = 55.125 */
+    temp_to_regs(LIMIT_HIGH_IN_MILLIDEGREE, &integer, &decimal);
+    i2c_set8(i2cdev, SBTSI_REG_TEMP_HIGH_INT, integer);
+    i2c_set8(i2cdev, SBTSI_REG_TEMP_HIGH_DEC, decimal);
+    integer = i2c_get8(i2cdev, SBTSI_REG_TEMP_HIGH_INT);
+    decimal = i2c_get8(i2cdev, SBTSI_REG_TEMP_HIGH_DEC);
+    g_assert_cmpuint(
+        regs_to_temp(integer, decimal), ==, LIMIT_HIGH_IN_MILLIDEGREE);
+    /* No alert is generated. */
+    value = i2c_get8(i2cdev, SBTSI_REG_STATUS);
+    g_assert_cmphex(value, ==, 0);
+
+    /* Test alert for low temperature */
+    qmp_sbtsi_set_temperature(TEST_ID, LIMIT_LOW_IN_MILLIDEGREE);
+    value = i2c_get8(i2cdev, SBTSI_REG_STATUS);
+    g_assert_cmphex(value, ==, SBTSI_STATUS_LOW_ALERT);
+
+    /* Test alert for high temperature */
+    qmp_sbtsi_set_temperature(TEST_ID, LIMIT_HIGH_IN_MILLIDEGREE);
+    value = i2c_get8(i2cdev, SBTSI_REG_STATUS);
+    g_assert_cmphex(value, ==, SBTSI_STATUS_HIGH_ALERT);
+
+    /* Disable alarm_en */
+    i2c_set8(i2cdev, SBTSI_REG_ALERT_CONFIG, 0);
+    value = i2c_get8(i2cdev, SBTSI_REG_ALERT_CONFIG);
+    g_assert_cmphex(value, ==, 0);
+    /* No alert when alarm_en is false. */
+    value = i2c_get8(i2cdev, SBTSI_REG_STATUS);
+    g_assert_cmphex(value, ==, 0);
+}
+
+static void sbtsi_register_nodes(void)
+{
+    QOSGraphEdgeOptions opts = {
+        .extra_device_opts = "id=" TEST_ID ",address=0x4c"
+    };
+    add_qi2c_address(&opts, &(QI2CAddress) { TEST_ADDR });
+
+    qos_node_create_driver("sbtsi", i2c_device_create);
+    qos_node_consumes("sbtsi", "i2c-bus", &opts);
+
+    qos_add_test("tx-rx", "sbtsi", tx_rx, NULL);
+}
+libqos_init(sbtsi_register_nodes);
tests/qtest/meson.build +1 -0
index 37e1eaa449..21e6152949 100644
@@ -254,6 +254,7 @@ qos_test_ss.add(
   'spapr-phb-test.c',
   'tmp105-test.c',
   'emc141x-test.c',
+  'tmp-sbtsi-test.c',
   'usb-hcd-ohci-test.c',
   'virtio-test.c',
   'virtio-blk-test.c',
Signature · 1 line
2.34.1.575.g55b058a8bb-goog