Add support for PMBus in QEMU
cover Add support for PMBus in QEMU
Hello,
This patch series adds an interface to start supporting PMBus devices in QEMU. I’ve included two PMBus devices: MAX34451 and ADM1272.
PMBus is a variant of SMBus meant for digital management of power supplies. PMBus adds to the SMBus standard by defining a number of constants and commands used by compliant devices. The specification for PMBus can be found at:
https://pmbus.org/specification-archives/
Currently, the goal for these devices is to emulate basic functionality by reading and writing registers. Timing, and some logical operation is not implemented. This implementation supports nearly all available registers for PMBus including:
- Voltage inputs and outputs
- Current inputs and outputs
- Temperature sensors
Unimplimented registers get passed through to the device model, and device models can opt out of using the standard registers with flags. The included devices make use of these fields and illustrate how to interface with the pmbus class.
Datasheets for sensors:
https://datasheets.maximintegrated.com/en/ds/MAX34451.pdf
https://www.analog.com/media/en/technical-documentation/data-sheets/ADM1272.pdf
Thanks for reviewing,
Titus Rwantare
Titus Rwantare (3):
hw/i2c: add support for PMBus
hw/misc: add ADM1272 device
hw/misc: add MAX34451 device
hw/arm/Kconfig | 3 + hw/i2c/Kconfig | 4 + hw/i2c/meson.build | 1 + hw/i2c/pmbus_device.c | 1611 +++++++++++++++++++++++++++++++++ hw/misc/Kconfig | 8 + hw/misc/adm1272.c | 551 +++++++++++ hw/misc/max34451.c | 727 +++++++++++++++ hw/misc/meson.build | 2 + include/hw/i2c/pmbus_device.h | 520 +++++++++++ tests/qtest/adm1272-test.c | 453 +++++++++ tests/qtest/max34451-test.c | 344 +++++++ tests/qtest/meson.build | 2 + 12 files changed, 4226 insertions(+) create mode 100644 hw/i2c/pmbus_device.c create mode 100644 hw/misc/adm1272.c create mode 100644 hw/misc/max34451.c create mode 100644 include/hw/i2c/pmbus_device.h create mode 100644 tests/qtest/adm1272-test.c create mode 100644 tests/qtest/max34451-test.c
Signature · 1 line
2.31.1.527.g47e6f16901-goog
PATCH 1/3 hw/i2c: add support for PMBus
QEMU has support for SMBus devices, and PMBus is a more specific implementation of SMBus. The additions made in this commit makes it easier to add new PMBus devices to QEMU.
https://pmbus.org/specification-archives/
--- hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/meson.build | 1 + hw/i2c/pmbus_device.c | 1611 +++++++++++++++++++++++++++++++++ include/hw/i2c/pmbus_device.h | 520 +++++++++++ 5 files changed, 2137 insertions(+) create mode 100644 hw/i2c/pmbus_device.c create mode 100644 include/hw/i2c/pmbus_device.h
hw/arm/Kconfig +1 -0
@@ -373,6 +373,7 @@ config NPCM7XX select ARM_GIC select AT24C # EEPROM select PL310 # cache controller + select PMBUS select SERIAL select SSI select UNIMP
hw/i2c/Kconfig +4 -0
@@ -28,3 +28,7 @@ config IMX_I2C config MPC_I2C bool select I2C + +config PMBUS + bool + select SMBUS
hw/i2c/meson.build +1 -0
@@ -14,4 +14,5 @@ i2c_ss.add(when: 'CONFIG_SMBUS_EEPROM', if_true: files('smbus_eeprom.c')) i2c_ss.add(when: 'CONFIG_VERSATILE_I2C', if_true: files('versatile_i2c.c')) i2c_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_i2c.c')) i2c_ss.add(when: 'CONFIG_PPC4XX', if_true: files('ppc4xx_i2c.c')) +i2c_ss.add(when: 'CONFIG_PMBUS', if_true: files('pmbus_device.c')) softmmu_ss.add_all(when: 'CONFIG_I2C', if_true: i2c_ss)
hw/i2c/pmbus_device.c new file +1611 -0
1,611 lines not on this page · raw message
include/hw/i2c/pmbus_device.h new file +520 -0
520 lines not on this page · raw message
Signature · 1 line
2.31.1.527.g47e6f16901-goog
Re: [PATCH 1/3] hw/i2c: add support for PMBus
Hi Titus,
Quoted 8 lines · Titus Rwantare
QEMU has support for SMBus devices, and PMBus is a more specific implementation of SMBus. The additions made in this commit makes it easier to add new PMBus devices to QEMU.
https://pmbus.org/specification-archives/
Reviewed-by Hao Wu <[email protected]>Signed-off-by Titus Rwantare <[email protected]>
Quoted 7 lines
--- hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/meson.build | 1 + hw/i2c/pmbus_device.c | 1611 +++++++++++++++++++++++++++++++++ include/hw/i2c/pmbus_device.h | 520 +++++++++++ 5 files changed, 2137 insertions(+)
TBH this is quite a big patch to digest.
Any chance you could split it?
Thanks,
Phil.
Re: [PATCH 1/3] hw/i2c: add support for PMBus
Hi Phil,
It looks big but the bulk of it is simple switch statements and definitions. Unless you mean you'd like it split despite that.
Regards,
Titus
Quoted 27 lines · Philippe Mathieu-Daudé
Hi Titus,
Quoted 8 lines · Titus Rwantare
QEMU has support for SMBus devices, and PMBus is a more specific implementation of SMBus. The additions made in this commit makes it easier to add new PMBus devices to QEMU.
https://pmbus.org/specification-archives/
Reviewed-by Hao Wu <[email protected]>Signed-off-by Titus Rwantare <[email protected]>Quoted 7 lines
--- hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/meson.build | 1 + hw/i2c/pmbus_device.c | 1611 +++++++++++++++++++++++++++++++++ include/hw/i2c/pmbus_device.h | 520 +++++++++++ 5 files changed, 2137 insertions(+)TBH this is quite a big patch to digest.
Any chance you could split it?
Thanks,
Phil.
Re: [PATCH 1/3] hw/i2c: add support for PMBus
In reply to Titus RwantareQuoted 6 lines · Titus Rwantare
QEMU has support for SMBus devices, and PMBus is a more specific implementation of SMBus. The additions made in this commit makes it easier to add new PMBus devices to QEMU.
I'm not a pmbus expert, but I am happy that someone has created a framework to model it. I've given them a read and some minor comments below.
Quoted 1 line
Reviewed-by Hao Wu <[email protected]>
Did this review happen on the mailing list? if not, I recommend doing your review on the public lists, so we can see what comments Hao made.
Quoted 8 lines
Signed-off-by Titus Rwantare <[email protected]>+++
b/hw/i2c/pmbus_device.chw/i2c/pmbus_device.c new file +4
-0@@ -0,0 +1,1611 @@ +/* +* PMBus wrapper over SMBus +* +* Copyright 2021 Google LLC
hw/i2c/pmbus_device.c line 14
Quoted 10 lines
hw/i2c/pmbus_device.c new file +10
-0+* +* 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.
You can replace these two paragraphs with a SDPX line:
SPDX-License-Identifier: GPL-2.0-or-later
In addition, add this to your git config to put the .h files on top :
git config diff.orderFile = /some/path/qemu.git/scripts/git.orderfile
It's easier for review.
I'd also suggest putting your tests in a separate patch, following the addition of the model, again for easier review.
Quoted 30 lines
hw/i2c/pmbus_device.c new file +30
-0+*/ +#include "qemu/osdep.h" +#include <math.h> +#include <string.h> +#include "hw/i2c/pmbus_device.h" +#include "qemu/module.h" +#include "qemu/log.h" + +uint16_t pmbus_data2direct_mode (PMBusCoefficients c,uint32_t value) +{ +/* R is usually negative to fit large readings into 16 bits */ +uint16_t y = (c.m * value + c.b) *pow (10 , c.R); +return y; +} + +uint32_t pmbus_direct_mode2data (PMBusCoefficients c,uint16_t value) +{ +/* X = (Y * 10^-R - b) / m */ +uint32_t x = (value /pow (10 , c.R) - c.b) / c.m; +return x; +} + +static void pmbus_send (PMBusDevice *pmdev,const uint8_t *data,uint16_t len) +{ +if (pmdev->out_buf_len + len >SMBUS_DATA_MAX_LEN ) { +qemu_log_mask (LOG_GUEST_ERROR , +"PMBus device tried to send too much data" ); + len =0 ; + } +
hw/i2c/pmbus_device.c line 54
Quoted 10 lines
hw/i2c/pmbus_device.c new file +10
-0+for (int i = len -1 ; i >=0 ; i--) { + pmdev->out_buf[i + pmdev->out_buf_len] = data[len - i -1 ]; + } + pmdev->out_buf_len += len; +} + +/* Internal only, convert unsigned ints to the little endian bus */ +static void pmbus_send_uint (PMBusDevice *pmdev,uint64_t data,uint8_t size) +{ +uint8_t bytes[8 ];
Do you need to assert that size is less than the array size? Probably not as all the callers are local.
Quoted 25 lines
hw/i2c/pmbus_device.c new file +25
-0+for (int i =0 ; i < size; i++) { + bytes[i] = data &0xFF ; + data = data >>8 ; + } +pmbus_send (pmdev, bytes, size); +} + +void pmbus_send_block (PMBusDevice *pmdev,PMBusBlock block) +{ +pmbus_send (pmdev, block.buf, block.len); +} + +void pmbus_send8 (PMBusDevice *pmdev,uint8_t data) +{ +pmbus_send_uint (pmdev, data,1 ); +} + +void pmbus_send16 (PMBusDevice *pmdev,uint16_t data) +{ +pmbus_send_uint (pmdev, data,2 ); +} + +void pmbus_send32 (PMBusDevice *pmdev,uint32_t data) +{ +pmbus_send_uint (pmdev, data,4 );
hw/i2c/pmbus_device.c line 89
Quoted 10 lines
hw/i2c/pmbus_device.c new file +10
-0+} + +void pmbus_send64 (PMBusDevice *pmdev,uint64_t data) +{ +pmbus_send_uint (pmdev, data,8 ); +} + +void pmbus_send_string (PMBusDevice *pmdev,const char *data) +{ +size_t len =strlen (data);
Do you need to assert that len is > 0?
Quoted 22 lines
hw/i2c/pmbus_device.c new file +22
-0+g_assert (len + pmdev->out_buf_len <SMBUS_DATA_MAX_LEN ); + pmdev->out_buf[len + pmdev->out_buf_len] = len; + +for (int i = len -1 ; i >=0 ; i--) { + pmdev->out_buf[i + pmdev->out_buf_len] = data[len -1 - i]; + } + pmdev->out_buf_len += len +1 ; +} + + +static uint64_t pmbus_receive_uint (const uint8_t *buf,uint8_t len) +{ +uint64_t ret =0 ; + +/* Exclude command code from return value */ + buf++; + len--; + +for (int i = len -1 ; i >=0 ; i--) { + ret = ret <<8 | buf[i]; + } +return ret;
hw/i2c/pmbus_device.c line 121
Quoted 10 lines
hw/i2c/pmbus_device.c new file +10
-0+} + +uint8_t pmbus_receive8 (PMBusDevice *pmdev) +{ +if (pmdev->in_buf_len -1 !=1 ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: length mismatch. Expected 1 byte, got %d bytes\n" , + __func__, pmdev->in_buf_len -1 ); + } +return (uint8_t )pmbus_receive_uint (pmdev->in_buf, pmdev->in_buf_len);
The casts are implicit in C, so you do not need to explicitly cast the return types in this and the functions below.
Quoted 26 lines
hw/i2c/pmbus_device.c new file +26
-0+} + +uint16_t pmbus_receive16 (PMBusDevice *pmdev) +{ +if (pmdev->in_buf_len -1 !=2 ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: length mismatch. Expected 2 bytes, got %d bytes\n" , + __func__, pmdev->in_buf_len -1 ); + } +return (uint16_t )pmbus_receive_uint (pmdev->in_buf, pmdev->in_buf_len); +} + +uint32_t pmbus_receive32 (PMBusDevice *pmdev) +{ +if (pmdev->in_buf_len -1 !=4 ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: length mismatch. Expected 4 bytes, got %d bytes\n" , + __func__, pmdev->in_buf_len -1 ); + } +return (uint32_t )pmbus_receive_uint (pmdev->in_buf, pmdev->in_buf_len); +} + +uint64_t pmbus_receive64 (PMBusDevice *pmdev) +{ +if (pmdev->in_buf_len -1 !=8 ) { + qemu_log_mask(LOG_GUEST_ERROR,
hw/i2c/pmbus_device.c line 157
Quoted 10 lines
hw/i2c/pmbus_device.c new file +10
-0+"%s: length mismatch. Expected 8 bytes, got %d bytes\n" , + __func__, pmdev->in_buf_len - 1); + } +return (uint64_t )pmbus_receive_uint (pmdev->in_buf, pmdev->in_buf_len); +} + +PMBusBlock pmbus_receive_block (PMBusDevice *pmdev) +{ +PMBusBlock data = { pmdev->in_buf, pmdev->in_buf_len }; +return data;
Returning the local variable seems like a dangerous function to have. You don't have any users of this function in the models you have submitted.
hw/i2c/pmbus_device.c line 1025
Quoted 11 lines
hw/i2c/pmbus_device.c new file +11
-0+static int pmbus_write_data (SMBusDevice *smd,uint8_t *buf,uint8_t len) +{ +PMBusDevice *pmdev =PMBUS_DEVICE (smd); +PMBusDeviceClass *pmdc =PMBUS_DEVICE_GET_CLASS (pmdev); +int ret =0 ; +uint8_t index; + +if (len ==0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: writing empty data\n" , __func__); +return -1 ; + }
I was going to suggest you find a meaningful error code to return. Poking around at this callback, it seems like there's not much error checking going on at the call sites, so it's hard to make a suggestion.
Cheers,
Joel
PATCH 2/3 hw/misc: add ADM1272 device
In reply to Titus RwantareThe ADM1272 is a PMBus compliant Hot Swap Controller and Digital Power Monitor by Analog Devices.
This commit adds support for interfacing with it, and support for setting and monitoring sensor limits.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADM1272.pdf
--- hw/arm/Kconfig | 1 + hw/misc/Kconfig | 4 + hw/misc/adm1272.c | 551 +++++++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + tests/qtest/adm1272-test.c | 453 ++++++++++++++++++++++++++++++ tests/qtest/meson.build | 1 + 6 files changed, 1011 insertions(+) create mode 100644 hw/misc/adm1272.c create mode 100644 tests/qtest/adm1272-test.c
hw/arm/Kconfig +1 -0
@@ -370,6 +370,7 @@ config XLNX_VERSAL config NPCM7XX bool select A9MPCORE + select ADM1272 select ARM_GIC select AT24C # EEPROM select PL310 # cache controller
hw/misc/Kconfig +4 -0
@@ -14,6 +14,10 @@ config ARMSSE_CPU_PWRCTRL config MAX111X bool +config ADM1272 + bool + depends on I2C + config TMP105 bool depends on I2C
hw/misc/adm1272.c new file +551 -0
551 lines not on this page · raw message
hw/misc/meson.build +1 -0
@@ -10,6 +10,7 @@ softmmu_ss.add(when: 'CONFIG_SGA', if_true: files('sga.c')) softmmu_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c')) softmmu_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.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_UNIMP', if_true: files('unimp.c')) softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c')) softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
tests/qtest/adm1272-test.c new file +453 -0
453 lines not on this page · raw message
tests/qtest/meson.build +1 -0
@@ -200,6 +200,7 @@ qtests_s390x = \ qos_test_ss = ss.source_set() qos_test_ss.add( 'ac97-test.c', + 'adm1272-test.c', 'ds1338-test.c', 'e1000-test.c', 'e1000e-test.c',
Signature · 1 line
2.31.1.527.g47e6f16901-goog
PATCH 3/3 hw/misc: add MAX34451 device
In reply to Titus RwantareThe MAX34451 is a Maxim power-supply system manager that can monitor up to 16 voltage rails or currents. It also contains a temperature sensor and supports up to four external temperature sensors.
This commit adds support for interfacing with it, and setting limits on the supported sensors.
--- hw/arm/Kconfig | 1 + hw/misc/Kconfig | 4 + hw/misc/max34451.c | 727 ++++++++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + tests/qtest/max34451-test.c | 344 +++++++++++++++++ tests/qtest/meson.build | 1 + 6 files changed, 1078 insertions(+) create mode 100644 hw/misc/max34451.c create mode 100644 tests/qtest/max34451-test.c
hw/arm/Kconfig +1 -0
@@ -373,6 +373,7 @@ config NPCM7XX select ADM1272 select ARM_GIC select AT24C # EEPROM + select MAX34451 select PL310 # cache controller select PMBUS select SERIAL
hw/misc/Kconfig +4 -0
@@ -30,6 +30,10 @@ config EMC141X bool depends on I2C +config MAX34451 + bool + depends on I2C + config ISA_DEBUG bool depends on ISA_BUS
hw/misc/max34451.c new file +727 -0
727 lines not on this page · raw message
hw/misc/meson.build +1 -0
@@ -11,6 +11,7 @@ softmmu_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c')) softmmu_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.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_UNIMP', if_true: files('unimp.c')) softmmu_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c')) softmmu_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
tests/qtest/max34451-test.c new file +344 -0
344 lines not on this page · raw message
tests/qtest/meson.build +1 -0
@@ -207,6 +207,7 @@ qos_test_ss.add( 'eepro100-test.c', 'es1370-test.c', 'ipoctal232-test.c', + 'max34451-test.c', 'megasas-test.c', 'ne2000-test.c', 'tulip-test.c',
Signature · 1 line
2.31.1.527.g47e6f16901-goog