Skip to contentSkip to conversation

hw/i2c: smbus: Reset fixes

PATCH Superseded Joe Komlodi 3 participants 10 messages Feb 2, 2024 – Feb 20, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover hw/i2c: smbus: Reset fixes

Changelog:

v2 -> v3
Patch 1
- Removed I3CBus class definition, since it was unneeded.
- whitespace fixes
- Changed enter_reset to hold_reset

Patch 2
- Moved pointer returned by object_get_canonical_path outside of printf so it can be freed

Patch 3
- Changed enter_reset to hold_reset

v1 -> v2
- Dropped 4th patch "hw/i2c: smbus: mux: Reset SMBusDevice state on reset". After more testing and Corey's comment, I realized it wasn't needed.

Original message:

Hi all,

This series adds some resets for SMBus and for the I2C core. Along with it, we make SMBus slave error printing a little more helpful.

These reset issues were very infrequent, they would maybe occur in 1 out of hundreds of resets in our testing, but the way they happen is pretty straightforward.

Basically as long as a reset happens in the middle of a transaction, the state of the old transaction would still partially be there after the reset. Once a new transaction comes in, the partial stale state can cause the new transaction to incorrectly fail.

Thanks,
Joe

Joe Komlodi (3):
hw/i2c: core: Add reset
hw/i2c/smbus_slave: Add object path on error prints hw/i2c: smbus_slave: Reset state on reset

 hw/i2c/core.c        | 19 +++++++++++++++++++
 hw/i2c/smbus_slave.c | 17 +++++++++++++++--
 include/hw/i2c/i2c.h |  2 +-
 3 files changed, 35 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
#1 lore raw

PATCH v3 1/3 hw/i2c: core: Add reset

It's possible for a reset to come in the middle of a transaction, which causes the bus to be in an old state when a new transaction comes in.

---
 hw/i2c/core.c        | 19 +++++++++++++++++++
 include/hw/i2c/i2c.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
hw/i2c/core.c +19 -0
index 4cf30b2c86..3128067bba 100644
@@ -23,10 +23,29 @@ static Property i2c_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void i2c_bus_hold_reset(Object *obj)
+{
+    I2CBus *bus = I2C_BUS(obj);
+    I2CNode *node, *next;
+
+    bus->broadcast = false;
+    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
+        QLIST_REMOVE(node, next);
+        g_free(node);
+    }
+}
+
+static void i2c_bus_class_init(ObjectClass *klass, void *data)
+{
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    rc->phases.hold = i2c_bus_hold_reset;
+}
+
 static const TypeInfo i2c_bus_info = {
     .name = TYPE_I2C_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(I2CBus),
+    .class_init = i2c_bus_class_init,
 };
 
 static int i2c_bus_pre_save(void *opaque)
include/hw/i2c/i2c.h +1 -1
index 2a3abacd1b..49580e30e2 100644
@@ -64,7 +64,7 @@ struct I2CSlave {
 };
 
 #define TYPE_I2C_BUS "i2c-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
+OBJECT_DECLARE_TYPE(I2CBus, I2CBusClass, I2C_BUS)
 
 typedef struct I2CNode I2CNode;
 
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
#2 lore raw

Re: [PATCH v3 1/3] hw/i2c: core: Add reset

Quoted 19 lines · Joe Komlodi

It's possible for a reset to come in the middle of a transaction, which causes the bus to be in an old state when a new transaction comes in.

---
 hw/i2c/core.c        | 19 +++++++++++++++++++
 include/hw/i2c/i2c.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
hw/i2c/core.c +1 -0
index 4cf30b2c86..3128067bba 100644
@@ -23,10 +23,29 @@ static Property i2c_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void i2c_bus_hold_reset(Object *obj)

hw/i2c/core.c static Property i2c_props[] = { · line 36

Quoted 10 lines
hw/i2c/core.c +10 -0
+{
+    I2CBus *bus = I2C_BUS(obj);
+    I2CNode *node, *next;
+
+    bus->broadcast = false;
+    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
+        QLIST_REMOVE(node, next);
+        g_free(node);
+    }
+}

This does what it says it's going to do; but I think it would be good to hear from Corey whether it's better to do this, or instead to call i2c_end_transfer() in the reset-enter phase.

Mostly QEMU's "reset" is like power-cycling, in which case I guess that what we have here where we just forget about the in-progress transfer and assume the device on the other end is also going to reset back to a neutral state is what we want.

Does i2c have a concept of a bus-level "reset" operation?

Quoted 22 lines
hw/i2c/core.c +8 -0
+
+static void i2c_bus_class_init(ObjectClass *klass, void *data)
+{
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    rc->phases.hold = i2c_bus_hold_reset;
+}
+
 static const TypeInfo i2c_bus_info = {
     .name = TYPE_I2C_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(I2CBus),
+    .class_init = i2c_bus_class_init,
 };
 
 

static int i2c_bus_pre_save(void *opaque)

include/hw/i2c/i2c.h +0 -0
index 2a3abacd1b..49580e30e2 100644

include/hw/i2c/i2c.h struct I2CSlave { · line 67

Quoted 5 lines
include/hw/i2c/i2c.h +1 -1
@@ -64,7 +64,7 @@ struct I2CSlave {
 };
 
 #define TYPE_I2C_BUS "i2c-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
+OBJECT_DECLARE_TYPE(I2CBus, I2CBusClass, I2C_BUS)

I don't think you need this change any more ?

thanks
-- PMM

#3 lore raw

Re: [PATCH v3 1/3] hw/i2c: core: Add reset

Quoted 20 lines · Peter Maydell
Quoted 18 lines · Joe Komlodi

It's possible for a reset to come in the middle of a transaction, which causes the bus to be in an old state when a new transaction comes in.

---
 hw/i2c/core.c        | 19 +++++++++++++++++++
 include/hw/i2c/i2c.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
hw/i2c/core.c +0 -0
index 4cf30b2c86..3128067bba 100644
@@ -23,10 +23,29 @@ static Property i2c_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
Quoted 16 lines

hw/i2c/core.c static Property i2c_props[] = { · line 36

Quoted 11 lines
hw/i2c/core.c +11 -0
+static void i2c_bus_hold_reset(Object *obj)
+{
+    I2CBus *bus = I2C_BUS(obj);
+    I2CNode *node, *next;
+
+    bus->broadcast = false;
+    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
+        QLIST_REMOVE(node, next);
+        g_free(node);
+    }
+}

This does what it says it's going to do; but I think it would be good to hear from Corey whether it's better to do this, or instead to call i2c_end_transfer() in the reset-enter phase.

i2c_end_transfer() might actually make more sense (explained a little more below). I'll see what Corey says though.

Quoted 9 lines

Mostly QEMU's "reset" is like power-cycling, in which case I guess that what we have here where we just forget about the in-progress transfer and assume the device on the other end is also going to reset back to a neutral state is what we want.

Does i2c have a concept of a bus-level "reset" operation?

Not really, as far as I know.
On hardware I believe if a reset happened in the middle of a transaction it would just look like a transaction ending from the target's PoV.

Quoted 24 lines
Quoted 24 lines
hw/i2c/core.c +8 -0
+
+static void i2c_bus_class_init(ObjectClass *klass, void *data)
+{
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    rc->phases.hold = i2c_bus_hold_reset;
+}
+
 static const TypeInfo i2c_bus_info = {
     .name = TYPE_I2C_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(I2CBus),
+    .class_init = i2c_bus_class_init,
 };
 
 

static int i2c_bus_pre_save(void *opaque)

include/hw/i2c/i2c.h +0 -0
index 2a3abacd1b..49580e30e2 100644
@@ -64,7 +64,7 @@ struct I2CSlave {
 };
 
Quoted 5 lines

include/hw/i2c/i2c.h struct I2CSlave { · line 67

Quoted 3 lines
include/hw/i2c/i2c.h +1 -1
 #define TYPE_I2C_BUS "i2c-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
+OBJECT_DECLARE_TYPE(I2CBus, I2CBusClass, I2C_BUS)

I don't think you need this change any more ?

Oops, will fix in v4. I'll hold off on sending it until Corey gives input on the reset behavior.

Thanks,
Joe

Quoted 3 lines

thanks
-- PMM

#4 lore raw

Re: [PATCH v3 1/3] hw/i2c: core: Add reset

In reply to Peter Maydell
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Joe Komlodi

It's possible for a reset to come in the middle of a transaction, which causes the bus to be in an old state when a new transaction comes in.

---
 hw/i2c/core.c        | 19 +++++++++++++++++++
 include/hw/i2c/i2c.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
hw/i2c/core.c +0 -0
index 4cf30b2c86..3128067bba 100644
@@ -23,10 +23,29 @@ static Property i2c_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
Quoted 16 lines

hw/i2c/core.c static Property i2c_props[] = { · line 36

Quoted 11 lines
hw/i2c/core.c +11 -0
+static void i2c_bus_hold_reset(Object *obj)
+{
+    I2CBus *bus = I2C_BUS(obj);
+    I2CNode *node, *next;
+
+    bus->broadcast = false;
+    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
+        QLIST_REMOVE(node, next);
+        g_free(node);
+    }
+}

This does what it says it's going to do; but I think it would be good to hear from Corey whether it's better to do this, or instead to call i2c_end_transfer() in the reset-enter phase.

Sorry, I missed this, I'm having major chaos going on right now in my life.

I don't think i2c_end_transfer() is the right thing to do. The transfer has not cleanly ended, it is just forgotten.

Quoted 8 lines

Mostly QEMU's "reset" is like power-cycling, in which case I guess that what we have here where we just forget about the in-progress transfer and assume the device on the other end is also going to reset back to a neutral state is what we want.

Does i2c have a concept of a bus-level "reset" operation?

No, it does not. Most I2C devices don't even have a reset pin. In a reset situation in real hardware, the operation would be aborted by the lines drifting high after the bus master has been reset.

So I think this is fine as is.

-corey

Quoted 34 lines
Quoted 23 lines
hw/i2c/core.c +8 -0
+
+static void i2c_bus_class_init(ObjectClass *klass, void *data)
+{
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    rc->phases.hold = i2c_bus_hold_reset;
+}
+
 static const TypeInfo i2c_bus_info = {
     .name = TYPE_I2C_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(I2CBus),
+    .class_init = i2c_bus_class_init,
 };
 
 

static int i2c_bus_pre_save(void *opaque)

include/hw/i2c/i2c.h +0 -0
index 2a3abacd1b..49580e30e2 100644

include/hw/i2c/i2c.h struct I2CSlave { · line 67

Quoted 5 lines
include/hw/i2c/i2c.h +1 -1
@@ -64,7 +64,7 @@ struct I2CSlave {
 };
 
 #define TYPE_I2C_BUS "i2c-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
+OBJECT_DECLARE_TYPE(I2CBus, I2CBusClass, I2C_BUS)

I don't think you need this change any more ?

thanks
-- PMM

#5 lore raw

Re: [PATCH v3 1/3] hw/i2c: core: Add reset

Quoted 38 lines · Corey Minyard
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Joe Komlodi

It's possible for a reset to come in the middle of a transaction, which causes the bus to be in an old state when a new transaction comes in.

---
 hw/i2c/core.c        | 19 +++++++++++++++++++
 include/hw/i2c/i2c.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
hw/i2c/core.c +0 -0
index 4cf30b2c86..3128067bba 100644
@@ -23,10 +23,29 @@ static Property i2c_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
Quoted 16 lines

hw/i2c/core.c static Property i2c_props[] = { · line 36

Quoted 11 lines
hw/i2c/core.c +11 -0
+static void i2c_bus_hold_reset(Object *obj)
+{
+    I2CBus *bus = I2C_BUS(obj);
+    I2CNode *node, *next;
+
+    bus->broadcast = false;
+    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
+        QLIST_REMOVE(node, next);
+        g_free(node);
+    }
+}

This does what it says it's going to do; but I think it would be good to hear from Corey whether it's better to do this, or instead to call i2c_end_transfer() in the reset-enter phase.

Quoted 2 lines

Sorry, I missed this, I'm having major chaos going on right now in my life.

No worries! I also missed this for a bit.

Quoted 3 lines

I don't think i2c_end_transfer() is the right thing to do. The transfer has not cleanly ended, it is just forgotten.

Sounds good to me, I'll send up v4 with the change Peter pointed out.

Thanks,
Joe

Quoted 52 lines
Quoted 9 lines

Mostly QEMU's "reset" is like power-cycling, in which case I guess that what we have here where we just forget about the in-progress transfer and assume the device on the other end is also going to reset back to a neutral state is what we want.

Does i2c have a concept of a bus-level "reset" operation?

No, it does not. Most I2C devices don't even have a reset pin. In a reset situation in real hardware, the operation would be aborted by the lines drifting high after the bus master has been reset.

So I think this is fine as is.

-corey

Quoted 34 lines
Quoted 23 lines
hw/i2c/core.c +8 -0
+
+static void i2c_bus_class_init(ObjectClass *klass, void *data)
+{
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    rc->phases.hold = i2c_bus_hold_reset;
+}
+
 static const TypeInfo i2c_bus_info = {
     .name = TYPE_I2C_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(I2CBus),
+    .class_init = i2c_bus_class_init,
 };
 
 

static int i2c_bus_pre_save(void *opaque)

include/hw/i2c/i2c.h +0 -0
index 2a3abacd1b..49580e30e2 100644

include/hw/i2c/i2c.h struct I2CSlave { · line 67

Quoted 5 lines
include/hw/i2c/i2c.h +1 -1
@@ -64,7 +64,7 @@ struct I2CSlave {
 };
 
 #define TYPE_I2C_BUS "i2c-bus"
-OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
+OBJECT_DECLARE_TYPE(I2CBus, I2CBusClass, I2C_BUS)

I don't think you need this change any more ?

thanks
-- PMM

#6 lore raw

PATCH v3 2/3 hw/i2c/smbus_slave: Add object path on error prints

In reply to Joe Komlodi

The current logging doesn't tell us which specific smbus device is an error state.

---
 hw/i2c/smbus_slave.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/i2c/smbus_slave.c +6 -2
index 1300c9ec72..9f9afc25a4 100644
@@ -25,11 +25,15 @@
 #define DPRINTF(fmt, ...) \
 do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
 #define BADF(fmt, ...) \
-do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
+do { g_autofree char *qom_path = object_get_canonical_path(OBJECT(dev));  \
+    fprintf(stderr, "%s: smbus: error: " fmt , qom_path, ## __VA_ARGS__); \
+            exit(1); } while (0)
 #else
 #define DPRINTF(fmt, ...) do {} while(0)
 #define BADF(fmt, ...) \
-do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
+do { g_autofree char *qom_path = object_get_canonical_path(OBJECT(dev));  \
+    fprintf(stderr, "%s: smbus: error: " fmt , qom_path, ## __VA_ARGS__); \
+             } while (0)
 #endif
 
 enum {
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
#8 lore raw

PATCH v3 3/3 hw/i2c: smbus_slave: Reset state on reset

In reply to Joe Komlodi

If a reset comes while the SMBus device is not in its idle state, it's possible for it to get confused on valid transactions post-reset.

---
 hw/i2c/smbus_slave.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/i2c/smbus_slave.c +9 -0
index 9f9afc25a4..4615e8b097 100644
@@ -201,10 +201,19 @@ static int smbus_i2c_send(I2CSlave *s, uint8_t data)
     return 0;
 }
 
+static void smbus_device_hold_reset(Object *obj)
+{
+    SMBusDevice *dev = SMBUS_DEVICE(obj);
+    dev->mode = SMBUS_IDLE;
+    dev->data_len = 0;
+}
+
 static void smbus_device_class_init(ObjectClass *klass, void *data)
 {
     I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
 
+    rc->phases.hold = smbus_device_hold_reset;
     sc->event = smbus_i2c_event;
     sc->recv = smbus_i2c_recv;
     sc->send = smbus_i2c_send;
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Reviewed-by on v3 3/3
#9 lore raw

Re: [PATCH v3 3/3] hw/i2c: smbus_slave: Reset state on reset

Quoted 8 lines · Joe Komlodi

If a reset comes while the SMBus device is not in its idle state, it's possible for it to get confused on valid transactions post-reset.

---
 hw/i2c/smbus_slave.c | 9 +++++++++
 1 file changed, 9 insertions(+)

thanks
-- PMM