Skip to contentSkip to conversation

Add PIIX4 SMBus to qtest and related changes

PATCH In review Marc Marí 2 participants 5 messages Jun 9, 2014

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

cover Add PIIX4 SMBus to qtest and related changes

Add PIIX4 SMBus to testing libraries, add a test case for those libraries, and solve problems in SMBus and PIIX4 codes.

Marc Marí (2):
x86 piix4: Correct SMBus base address
qtest: Add PIIX4 SMBus support for libqos

Paolo Bonzini (1):
smbus: fix writes

 hw/acpi/piix4.c            |    2 +-
 hw/i2c/smbus.c             |    7 +-
 tests/Makefile             |    5 +
 tests/eeprom-test.c        |   58 ++++++++++++
 tests/libqos/i2c.h         |    5 +-
 tests/libqos/smbus-piix4.c |  220 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 293 insertions(+), 4 deletions(-)
 create mode 100644 tests/eeprom-test.c
 create mode 100644 tests/libqos/smbus-piix4.c
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 1/3 smbus: fix writes

From: Paolo Bonzini <[email protected]>

SMBus protocol sends offset and length before the actual data that is transferred. So we need to skip two bytes rather than one.

---
 hw/i2c/smbus.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/i2c/smbus.c +5 -2
index 6e27ae8..173a533 100644
@@ -59,9 +59,12 @@ static void smbus_do_write(SMBusDevice *dev)
     } else {
         dev->command = dev->data_buf[0];
         DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
+        if (dev->data_buf[1] > dev->data_len - 2) {
+            fprintf(stderr, "SMBus data transfer overrun!\n");
+        }
         if (sc->write_data) {
-            sc->write_data(dev, dev->command, dev->data_buf + 1,
-                           dev->data_len - 1);
+            sc->write_data(dev, dev->command, dev->data_buf + 2,
+                           MIN(dev->data_buf[1], dev->data_len - 2));
         }
     }
 }
Signature · 1 line
1.7.10.4
author
#2 lore raw

PATCH 2/3 x86 piix4: Correct SMBus base address

In reply to Marc Marí

As defined in the PIIX4 datasheet in page 135, the base address is set on bits [15:4]. The mask was changed to match those bits.

Datasheet: http://www.intel.com/assets/pdf/datasheet/290562.pdf

Found by Stefan Hajnoczi

---
 hw/acpi/piix4.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/piix4.c +1 -1
index 67dc075..5f263da 100644
@@ -133,7 +133,7 @@ static void smbus_io_space_update(PIIX4PMState *s)
     PCIDevice *d = PCI_DEVICE(s);
 
     s->smb_io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x90));
-    s->smb_io_base &= 0xffc0;
+    s->smb_io_base &= 0xfff0;
 
     memory_region_transaction_begin();
     memory_region_set_enabled(&s->smb.io, d->config[0xd2] & 1);
Signature · 1 line
1.7.10.4
author
#3 lore raw

PATCH 3/3 qtest: Add PIIX4 SMBus support for libqos

In reply to Marc Marí

Add support for the SMBus protocol on the PIIX4 chipset. Add a test for EEPROMs using this interface.

---
 tests/Makefile             |    5 +
 tests/eeprom-test.c        |   58 ++++++++++++
 tests/libqos/i2c.h         |    5 +-
 tests/libqos/smbus-piix4.c |  220 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 287 insertions(+), 1 deletion(-)
 create mode 100644 tests/eeprom-test.c
 create mode 100644 tests/libqos/smbus-piix4.c
tests/Makefile +5 -0
index 361bb7b..e49c779 100644
@@ -152,6 +152,8 @@ gcov-files-i386-y += hw/pci-bridge/i82801b11.c
 check-qtest-i386-y += tests/ioh3420-test$(EXESUF)
 gcov-files-i386-y += hw/pci-bridge/ioh3420.c
 check-qtest-i386-y += tests/usb-hcd-ehci-test$(EXESUF)
+gcov-files-i386-y += hw/i2c/smbus_eeprom.c
+check-qtest-i386-y += tests/eeprom-test$(EXESUF)
 gcov-files-i386-y += hw/usb/hcd-ehci.c
 gcov-files-i386-y += hw/usb/hcd-uhci.c
 gcov-files-i386-y += hw/usb/dev-hid.c
@@ -281,6 +283,7 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
+libqos-piix4-obj-y = $(libqos-obj-y) tests/libqos/smbus-piix4.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
@@ -322,9 +325,11 @@ tests/es1370-test$(EXESUF): tests/es1370-test.o
 tests/intel-hda-test$(EXESUF): tests/intel-hda-test.o
 tests/ioh3420-test$(EXESUF): tests/ioh3420-test.o
 tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-pc-obj-y)
+tests/eeprom-test$(EXESUF): tests/eeprom-test.o $(libqos-piix4-obj-y)
 tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
 tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
 
+
 # QTest rules
 
 TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
tests/eeprom-test.c new file +58 -0

58 lines not on this page · raw message

tests/libqos/i2c.h +4 -1
index 1ce9af4..9fd3a1c 100644
@@ -24,7 +24,10 @@ void i2c_send(I2CAdapter *i2c, uint8_t addr,
 void i2c_recv(I2CAdapter *i2c, uint8_t addr,
               uint8_t *buf, uint16_t len);
 
-/* libi2c-omap.c */
+/* i2c-omap.c */
 I2CAdapter *omap_i2c_create(uint64_t addr);
 
+/* smbus-piix4.c */
+I2CAdapter *piix4_smbus_create(uint64_t addr);
+
 #endif
tests/libqos/smbus-piix4.c new file +220 -0

220 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#4 lore raw

Re: [Qemu-devel] [PATCH 3/3] qtest: Add PIIX4 SMBus support for libqos

Il 09/06/2014 10:55, Marc Marí ha scritto:

tests/eeprom-test.c line 27

Quoted 6 lines
tests/eeprom-test.c new file +5 -0
+static void send_and_receive(void)
+{
+    uint8_t i;
+    uint8_t buf = 0;
+    uint64_t big_buf = 0;

Probably uint32_t since you only read 4 bytes.

tests/eeprom-test.c line 35

Quoted 7 lines
tests/eeprom-test.c new file +7 -0
+    for (i = EEPROM_TEST_ADDR; i < EEPROM_TEST_ADDR+8; ++i) {
+        i2c_recv(i2c, i, &buf, 1);
+        g_assert_cmphex(buf, ==, 0);
+
+        i2c_recv(i2c, i, (uint8_t *)&big_buf, 4);
+        g_assert_cmphex(big_buf, ==, 0);
+    }

Here you should be sending something before. The protocol is that you send the address, then send the offset, then receive bytes.

In fact, right now you're sending a zero here:

+ /* Clear data */
+ outb(s->addr + PIIX4_SMBUS_DAT0, 0);

in libqos.

The problem is that the piix4 smbus interface includes more "magic" than the i2c interface that is currently part of libqos. Basically libqos would have to implement the same state machine as hw/i2c/smbus.c in order to convert i2c commands (from the test case) to smbus commands (for the device). The opposite also makes sense if you want to have an smbus testcase API and you want to make it talk to "basic" i2c devices.

So this testcase by itself is not really meaningful. This is not your fault---I and Stefan aren't 100% comfortable with I2C either. :) Let's discuss it today on the chat.

tests/libqos/smbus-piix4.c line 138

Quoted 6 lines
tests/libqos/smbus-piix4.c new file +6 -0
+        while (len > 0) {
+            size = inb(s->addr + PIIX4_SMBUS_DAT0);
+            if (size == 0) {
+                break;
+            }
+            g_assert_cmpuint((len-size), <, 0);

Asserting len < size does not make much sense. Should you be asserting instead that size <= len here? Or even len == size (without the outer "while (len > 0)" loop)?

tests/libqos/smbus-piix4.c line 146

Quoted 8 lines
tests/libqos/smbus-piix4.c new file +8 -0
+            while (size > 0) {
+                buf[0] = readb(s->addr + PIIX4_SMBUS_BLKDAT);
+                ++buf;
+                --size;
+            }
+
+            len -= size;
+        }

size is zero here; the decrement should be before the inner while loop.

Paolo