i2c: aspeed: Handle master/slave combined irq events properly
PATCH v6 1/1 i2c: aspeed: Handle master/slave combined irq events properly
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a NORMAL_STOP interrupt in its MASTER_STOP state, SLAVE_MATCH and RX_DONE interrupts could come along with the NORMAL_STOP in case of an another master immediately sends data just after acquiring the bus. In this case, the NORMAL_STOP interrupt should be handled by master_irq and the SLAVE_MATCH and RX_DONE interrupts should be handled by slave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.
---
Changes since v5:
- Changed variable names in irq hanlders to represent proper meaning.
- Fixed an error printing message again to make it use the irq_handled variable.
Changes since v4:
- Fixed an error printing message that handlers didn't handle all interrupts.
Changes since v3:
- Fixed typos in a comment.
Changes since v2:
- Changed the name of ASPEED_I2CD_INTR_ERRORS to ASPEED_I2CD_INTR_MASTER_ERRORS
- Removed a member irq_status from the struct aspeed_i2c_bus and changed master_irq and slave_irq handlers to make them return status_ack.
- Added a comment to explain why it needs to try both irq handlers.
Changes since v1:
- Fixed a grammar issue in commit message.
- Added a missing line feed character into a message printing.
drivers/i2c/busses/i2c-aspeed.c | 131 ++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 55 deletions(-)
drivers/i2c/busses/i2c-aspeed.c +76 -55
@@ -82,6 +82,11 @@#define ASPEED_I2CD_INTR_RX_DONE BIT(2)#define ASPEED_I2CD_INTR_TX_NAK BIT(1)#define ASPEED_I2CD_INTR_TX_ACK BIT(0) +#define ASPEED_I2CD_INTR_MASTER_ERRORS \ + (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ + ASPEED_I2CD_INTR_SCL_TIMEOUT | \ + ASPEED_I2CD_INTR_ABNORMAL | \ + ASPEED_I2CD_INTR_ARBIT_LOSS)#define ASPEED_I2CD_INTR_ALL \ (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \
@@ -227,32 +232,26 @@ static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) }#if IS_ENABLED (CONFIG_I2C_SLAVE ) -static bool aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 command, irq_status, status_ack =0 ; +u32 command, irq_handled =0 ;struct i2c_client *slave = bus->slave; -bool irq_handled = true;u8 value; -if (!slave) { - irq_handled = false; - goto out; - } +if (!slave) +return 0 ; command =readl (bus->base +ASPEED_I2C_CMD_REG ); - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG );/* Slave was requested, restart state machine. */ if (irq_status &ASPEED_I2CD_INTR_SLAVE_MATCH ) { - status_ack |=ASPEED_I2CD_INTR_SLAVE_MATCH ; + irq_handled |=ASPEED_I2CD_INTR_SLAVE_MATCH ; bus->slave_state =ASPEED_I2C_SLAVE_START ; }/* Slave is not currently active, irq was for someone else. */ -if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) { - irq_handled = false; - goto out; - } +if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) +return irq_handled;dev_dbg (bus->dev,"slave irq status 0x%08x, cmd 0x%08x\n" , irq_status, command);
@@ -269,31 +268,31 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) bus->slave_state =ASPEED_I2C_SLAVE_WRITE_REQUESTED ; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; }/* Slave was asked to stop. */ if (irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ) { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; }if (irq_status &ASPEED_I2CD_INTR_TX_NAK ) { - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; } +if (irq_status &ASPEED_I2CD_INTR_TX_ACK ) + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;switch (bus->slave_state) {case ASPEED_I2C_SLAVE_READ_REQUESTED :if (irq_status &ASPEED_I2CD_INTR_TX_ACK )dev_err (bus->dev,"Unexpected ACK on read request.\n" ); bus->slave_state =ASPEED_I2C_SLAVE_READ_PROCESSED ; -i2c_slave_event (slave,I2C_SLAVE_READ_REQUESTED , &value);writel (value, bus->base +ASPEED_I2C_BYTE_BUF_REG );writel (ASPEED_I2CD_S_TX_CMD , bus->base +ASPEED_I2C_CMD_REG );break ;case ASPEED_I2C_SLAVE_READ_PROCESSED : - status_ack |=ASPEED_I2CD_INTR_TX_ACK ;if (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))dev_err (bus->dev,"Expected ACK after processed read.\n" );
@@ -317,13 +316,6 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) break; } -if (status_ack != irq_status) -dev_err (bus->dev, -"irq handled != irq. expected %x, but was %x\n" , - irq_status, status_ack); -writel (status_ack, bus->base +ASPEED_I2C_INTR_STS_REG ); - -out:return irq_handled; }#endif /* CONFIG_I2C_SLAVE */
@@ -380,21 +372,21 @@ static int aspeed_i2c_is_irq_error(u32 irq_status)return 0 ; } -static bool aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 irq_status, status_ack =0 , command =0 ; +u32 irq_handled =0 , command =0 ;struct i2c_msg *msg;u8 recv_byte;int ret; - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); -/* Ack all interrupt bits. */ -writel (irq_status, bus->base +ASPEED_I2C_INTR_STS_REG ); -if (irq_status &ASPEED_I2CD_INTR_BUS_RECOVER_DONE ) { bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - status_ack |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; + irq_handled |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; goto out_complete; + }else { +/* Master is not currently active, irq was for someone else. */ +if (bus->master_state ==ASPEED_I2C_MASTER_INACTIVE ) + goto out_no_complete; }/*
@@ -403,19 +395,22 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)* INACTIVE state. */ ret =aspeed_i2c_is_irq_error (irq_status); -if (ret <0 ) { +if (ret) {dev_dbg (bus->dev,"received error interrupt: 0x%08x\n" , irq_status); bus->cmd_err = ret; bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + irq_handled |= (irq_status &ASPEED_I2CD_INTR_MASTER_ERRORS ); goto out_complete; }/* We are in an invalid state; reset bus to a known state. */ if (!bus->msgs) { -dev_err (bus->dev,"bus in unknown state\n" ); +dev_err (bus->dev,"bus in unknown state. irq_status: 0x%x\n" , + irq_status); bus->cmd_err = -EIO ; -if (bus->master_state !=ASPEED_I2C_MASTER_STOP ) +if (bus->master_state !=ASPEED_I2C_MASTER_STOP && + bus->master_state !=ASPEED_I2C_MASTER_INACTIVE )aspeed_i2c_do_stop (bus); goto out_no_complete; }
@@ -428,13 +423,18 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)*/ if (bus->master_state ==ASPEED_I2C_MASTER_START ) {if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) { +if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_NAK ))) { + bus->cmd_err = -ENXIO ; + bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + goto out_complete; + }pr_devel ("no slave present at %02x\n" , msg->addr); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->cmd_err = -ENXIO ;aspeed_i2c_do_stop (bus); goto out_no_complete; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;if (msg->len ==0 ) {/* SMBUS_QUICK */ aspeed_i2c_do_stop (bus); goto out_no_complete;
@@ -449,13 +449,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)case ASPEED_I2C_MASTER_TX :if (unlikely (irq_status &ASPEED_I2CD_INTR_TX_NAK )) {dev_dbg (bus->dev,"slave NACKed TX\n" ); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; goto error_and_stop; }else if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) {dev_err (bus->dev,"slave failed to ACK TX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;/* fallthrough intended */ case ASPEED_I2C_MASTER_TX_FIRST :if (bus->buf_index < msg->len) {
@@ -478,7 +478,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)dev_err (bus->dev,"master failed to RX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; recv_byte =readl (bus->base +ASPEED_I2C_BYTE_BUF_REG ) >>8 ; msg->buf[bus->buf_index++] = recv_byte;
@@ -506,11 +506,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) goto out_no_complete;case ASPEED_I2C_MASTER_STOP :if (unlikely (!(irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ))) { -dev_err (bus->dev,"master failed to STOP\n" ); +dev_err (bus->dev, +"master failed to STOP. irq_status:0x%x\n" , + irq_status); bus->cmd_err = -EIO ;/* Do not STOP as we have already tried. */ }else { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; } bus->master_state =ASPEED_I2C_MASTER_INACTIVE ;
@@ -540,33 +542,52 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) bus->master_xfer_result = bus->msgs_index +1 ;complete (&bus->cmd_complete); out_no_complete: -if (irq_status != status_ack) -dev_err (bus->dev, -"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , - irq_status, status_ack); -return !!irq_status; +return irq_handled; }static irqreturn_t aspeed_i2c_bus_irq (int irq,void *dev_id) {struct aspeed_i2c_bus *bus = dev_id; -bool ret; +u32 irq_received, irq_remaining, irq_handled;spin_lock (&bus->lock); + irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); + irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE ) -if (aspeed_i2c_slave_irq (bus)) { -dev_dbg (bus->dev,"irq handled by slave.\n" ); - ret = true; - goto out; +/* +* In most cases, interrupt bits will be set one by one, although +* multiple interrupt bits could be set at the same time. It's also +* possible that master interrupt bits could be set along with slave +* interrupt bits. Each case needs to be handled using corresponding +* handlers depending on the current state. +*/ +if (bus->master_state !=ASPEED_I2C_MASTER_INACTIVE ) { + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_slave_irq (bus, irq_remaining); + }else { + irq_handled =aspeed_i2c_slave_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_master_irq (bus, + irq_remaining); } +#else + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining);#endif /* CONFIG_I2C_SLAVE */ - ret =aspeed_i2c_master_irq (bus); + irq_remaining &= ~irq_handled; +if (irq_remaining) +dev_err (bus->dev, +"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , + irq_received, irq_handled); -out: +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock); -return ret ?IRQ_HANDLED :IRQ_NONE ; +return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; } static int aspeed_i2c_master_xfer(struct i2c_adapter *adap,
Signature · 1 line
2.18.0
Quoted 331 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Signed-off-by Jae Hyun Yoo <[email protected]>---
Changes since v5:
- Changed variable names in irq hanlders to represent proper meaning.
- Fixed an error printing message again to make it use theirq_handledvariable.Changes since v4:
- Fixed an error printing message that handlers didn't handle all interrupts.Changes since v3:
- Fixed typos in a comment.Changes since v2:
- Changed the name ofASPEED_I2CD_INTR_ERRORStoASPEED_I2CD_INTR_MASTER_ERRORS
- Removed a memberirq_statusfrom the structaspeed_i2c_busand changedmaster_irqandslave_irqhandlers to make them returnstatus_ack.
- Added a comment to explain why it needs to try both irq handlers.Changes since v1:
- Fixed a grammar issue in commit message.
- Added a missing line feed character into a message printing.drivers/i2c/busses/i2c-aspeed.c | 131 ++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 55 deletions(-)drivers/i2c/busses/i2c-aspeed.c +76
-56@@ -82,6 +82,11 @@#define ASPEED_I2CD_INTR_RX_DONE BIT(2)#define ASPEED_I2CD_INTR_TX_NAK BIT(1)#define ASPEED_I2CD_INTR_TX_ACK BIT(0) +#define ASPEED_I2CD_INTR_MASTER_ERRORS \ + (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ + ASPEED_I2CD_INTR_SCL_TIMEOUT | \ + ASPEED_I2CD_INTR_ABNORMAL | \ + ASPEED_I2CD_INTR_ARBIT_LOSS)#define ASPEED_I2CD_INTR_ALL \ (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \@@ -227,32 +232,26 @@ static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) }#if IS_ENABLED (CONFIG_I2C_SLAVE ) -static bool aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 command, irq_status, status_ack =0 ; +u32 command, irq_handled =0 ;struct i2c_client *slave = bus->slave; -bool irq_handled = true;u8 value; -if (!slave) { - irq_handled = false; - goto out; - } +if (!slave) +return 0 ; command =readl (bus->base +ASPEED_I2C_CMD_REG ); - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG );/* Slave was requested, restart state machine. */ if (irq_status &ASPEED_I2CD_INTR_SLAVE_MATCH ) { - status_ack |=ASPEED_I2CD_INTR_SLAVE_MATCH ; + irq_handled |=ASPEED_I2CD_INTR_SLAVE_MATCH ; bus->slave_state =ASPEED_I2C_SLAVE_START ; }/* Slave is not currently active, irq was for someone else. */ -if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) { - irq_handled = false; - goto out; - } +if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) +return irq_handled;dev_dbg (bus->dev,"slave irq status 0x%08x, cmd 0x%08x\n" , irq_status, command);@@ -269,31 +268,31 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) bus->slave_state =ASPEED_I2C_SLAVE_WRITE_REQUESTED ; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; }/* Slave was asked to stop. */ if (irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ) { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; }if (irq_status &ASPEED_I2CD_INTR_TX_NAK ) { - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; } +if (irq_status &ASPEED_I2CD_INTR_TX_ACK ) + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;switch (bus->slave_state) {case ASPEED_I2C_SLAVE_READ_REQUESTED :if (irq_status &ASPEED_I2CD_INTR_TX_ACK )dev_err (bus->dev,"Unexpected ACK on read request.\n" ); bus->slave_state =ASPEED_I2C_SLAVE_READ_PROCESSED ; -i2c_slave_event (slave,I2C_SLAVE_READ_REQUESTED , &value);writel (value, bus->base +ASPEED_I2C_BYTE_BUF_REG );writel (ASPEED_I2CD_S_TX_CMD , bus->base +ASPEED_I2C_CMD_REG );break ;case ASPEED_I2C_SLAVE_READ_PROCESSED : - status_ack |=ASPEED_I2CD_INTR_TX_ACK ;if (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))dev_err (bus->dev,"Expected ACK after processed read.\n" );@@ -317,13 +316,6 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) break; } -if (status_ack != irq_status) -dev_err (bus->dev, -"irq handled != irq. expected %x, but was %x\n" , - irq_status, status_ack); -writel (status_ack, bus->base +ASPEED_I2C_INTR_STS_REG ); - -out:return irq_handled; }#endif /* CONFIG_I2C_SLAVE */ @@ -380,21 +372,21 @@ static int aspeed_i2c_is_irq_error(u32 irq_status)return 0 ; } -static bool aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 irq_status, status_ack =0 , command =0 ; +u32 irq_handled =0 , command =0 ;struct i2c_msg *msg;u8 recv_byte;int ret; - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); -/* Ack all interrupt bits. */ -writel (irq_status, bus->base +ASPEED_I2C_INTR_STS_REG ); -if (irq_status &ASPEED_I2CD_INTR_BUS_RECOVER_DONE ) { bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - status_ack |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; + irq_handled |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; goto out_complete; + }else { +/* Master is not currently active, irq was for someone else. */ +if (bus->master_state ==ASPEED_I2C_MASTER_INACTIVE ) + goto out_no_complete; }/* @@ -403,19 +395,22 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)* INACTIVE state. */ ret =aspeed_i2c_is_irq_error (irq_status); -if (ret <0 ) { +if (ret) {dev_dbg (bus->dev,"received error interrupt: 0x%08x\n" , irq_status); bus->cmd_err = ret; bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + irq_handled |= (irq_status &ASPEED_I2CD_INTR_MASTER_ERRORS ); goto out_complete; }/* We are in an invalid state; reset bus to a known state. */ if (!bus->msgs) { -dev_err (bus->dev,"bus in unknown state\n" ); +dev_err (bus->dev,"bus in unknown state. irq_status: 0x%x\n" , + irq_status); bus->cmd_err = -EIO ; -if (bus->master_state !=ASPEED_I2C_MASTER_STOP ) +if (bus->master_state !=ASPEED_I2C_MASTER_STOP && + bus->master_state !=ASPEED_I2C_MASTER_INACTIVE )aspeed_i2c_do_stop (bus); goto out_no_complete; }@@ -428,13 +423,18 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)*/ if (bus->master_state ==ASPEED_I2C_MASTER_START ) {if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) { +if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_NAK ))) { + bus->cmd_err = -ENXIO ; + bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + goto out_complete; + }pr_devel ("no slave present at %02x\n" , msg->addr); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->cmd_err = -ENXIO ;aspeed_i2c_do_stop (bus); goto out_no_complete; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;if (msg->len ==0 ) {/* SMBUS_QUICK */ aspeed_i2c_do_stop (bus); goto out_no_complete;@@ -449,13 +449,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)case ASPEED_I2C_MASTER_TX :if (unlikely (irq_status &ASPEED_I2CD_INTR_TX_NAK )) {dev_dbg (bus->dev,"slave NACKed TX\n" ); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; goto error_and_stop; }else if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) {dev_err (bus->dev,"slave failed to ACK TX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;/* fallthrough intended */ case ASPEED_I2C_MASTER_TX_FIRST :if (bus->buf_index < msg->len) {@@ -478,7 +478,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)dev_err (bus->dev,"master failed to RX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; recv_byte =readl (bus->base +ASPEED_I2C_BYTE_BUF_REG ) >>8 ; msg->buf[bus->buf_index++] = recv_byte;@@ -506,11 +506,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) goto out_no_complete;case ASPEED_I2C_MASTER_STOP :if (unlikely (!(irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ))) { -dev_err (bus->dev,"master failed to STOP\n" ); +dev_err (bus->dev, +"master failed to STOP. irq_status:0x%x\n" , + irq_status); bus->cmd_err = -EIO ;/* Do not STOP as we have already tried. */ }else { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; } bus->master_state =ASPEED_I2C_MASTER_INACTIVE ;@@ -540,33 +542,52 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) bus->master_xfer_result = bus->msgs_index +1 ;complete (&bus->cmd_complete); out_no_complete: -if (irq_status != status_ack) -dev_err (bus->dev, -"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , - irq_status, status_ack); -return !!irq_status; +return irq_handled; }static irqreturn_t aspeed_i2c_bus_irq (int irq,void *dev_id) {struct aspeed_i2c_bus *bus = dev_id; -bool ret; +u32 irq_received, irq_remaining, irq_handled;spin_lock (&bus->lock); + irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); + irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE ) -if (aspeed_i2c_slave_irq (bus)) { -dev_dbg (bus->dev,"irq handled by slave.\n" ); - ret = true; - goto out; +/* +* In most cases, interrupt bits will be set one by one, although +* multiple interrupt bits could be set at the same time. It's also +* possible that master interrupt bits could be set along with slave +* interrupt bits. Each case needs to be handled using corresponding +* handlers depending on the current state. +*/ +if (bus->master_state !=ASPEED_I2C_MASTER_INACTIVE ) { + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_slave_irq (bus, irq_remaining); + }else { + irq_handled =aspeed_i2c_slave_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_master_irq (bus, + irq_remaining); } +#else + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining);#endif /* CONFIG_I2C_SLAVE */ - ret =aspeed_i2c_master_irq (bus); + irq_remaining &= ~irq_handled; +if (irq_remaining) +dev_err (bus->dev, +"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , + irq_received, irq_handled); -out: +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock); -return ret ?IRQ_HANDLED :IRQ_NONE ; +return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; } static int aspeed_i2c_master_xfer(struct i2c_adapter *adap, --
Quoted 2 lines
2.18.0
Looks awesome! Thanks!
Quoted 331 lines · Brendan Higgins
Quoted 329 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Signed-off-by Jae Hyun Yoo <[email protected]>---
Changes since v5:
- Changed variable names in irq hanlders to represent proper meaning.
- Fixed an error printing message again to make it use theirq_handledvariable.Changes since v4:
- Fixed an error printing message that handlers didn't handle all interrupts.Changes since v3:
- Fixed typos in a comment.Changes since v2:
- Changed the name ofASPEED_I2CD_INTR_ERRORStoASPEED_I2CD_INTR_MASTER_ERRORS
- Removed a memberirq_statusfrom the structaspeed_i2c_busand changedmaster_irqandslave_irqhandlers to make them returnstatus_ack.
- Added a comment to explain why it needs to try both irq handlers.Changes since v1:
- Fixed a grammar issue in commit message.
- Added a missing line feed character into a message printing.drivers/i2c/busses/i2c-aspeed.c | 131 ++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 55 deletions(-)drivers/i2c/busses/i2c-aspeed.c +76
-55@@ -82,6 +82,11 @@#define ASPEED_I2CD_INTR_RX_DONE BIT(2)#define ASPEED_I2CD_INTR_TX_NAK BIT(1)#define ASPEED_I2CD_INTR_TX_ACK BIT(0) +#define ASPEED_I2CD_INTR_MASTER_ERRORS \ + (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ + ASPEED_I2CD_INTR_SCL_TIMEOUT | \ + ASPEED_I2CD_INTR_ABNORMAL | \ + ASPEED_I2CD_INTR_ARBIT_LOSS)#define ASPEED_I2CD_INTR_ALL \ (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \@@ -227,32 +232,26 @@ static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) }#if IS_ENABLED (CONFIG_I2C_SLAVE ) -static bool aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 command, irq_status, status_ack =0 ; +u32 command, irq_handled =0 ;struct i2c_client *slave = bus->slave; -bool irq_handled = true;u8 value; -if (!slave) { - irq_handled = false; - goto out; - } +if (!slave) +return 0 ; command =readl (bus->base +ASPEED_I2C_CMD_REG ); - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG );/* Slave was requested, restart state machine. */ if (irq_status &ASPEED_I2CD_INTR_SLAVE_MATCH ) { - status_ack |=ASPEED_I2CD_INTR_SLAVE_MATCH ; + irq_handled |=ASPEED_I2CD_INTR_SLAVE_MATCH ; bus->slave_state =ASPEED_I2C_SLAVE_START ; }/* Slave is not currently active, irq was for someone else. */ -if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) { - irq_handled = false; - goto out; - } +if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) +return irq_handled;dev_dbg (bus->dev,"slave irq status 0x%08x, cmd 0x%08x\n" , irq_status, command);@@ -269,31 +268,31 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) bus->slave_state =ASPEED_I2C_SLAVE_WRITE_REQUESTED ; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; }/* Slave was asked to stop. */ if (irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ) { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; }if (irq_status &ASPEED_I2CD_INTR_TX_NAK ) { - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; } +if (irq_status &ASPEED_I2CD_INTR_TX_ACK ) + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;switch (bus->slave_state) {case ASPEED_I2C_SLAVE_READ_REQUESTED :if (irq_status &ASPEED_I2CD_INTR_TX_ACK )dev_err (bus->dev,"Unexpected ACK on read request.\n" ); bus->slave_state =ASPEED_I2C_SLAVE_READ_PROCESSED ; -i2c_slave_event (slave,I2C_SLAVE_READ_REQUESTED , &value);writel (value, bus->base +ASPEED_I2C_BYTE_BUF_REG );writel (ASPEED_I2CD_S_TX_CMD , bus->base +ASPEED_I2C_CMD_REG );break ;case ASPEED_I2C_SLAVE_READ_PROCESSED : - status_ack |=ASPEED_I2CD_INTR_TX_ACK ;if (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))dev_err (bus->dev,"Expected ACK after processed read.\n" );@@ -317,13 +316,6 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) break; } -if (status_ack != irq_status) -dev_err (bus->dev, -"irq handled != irq. expected %x, but was %x\n" , - irq_status, status_ack); -writel (status_ack, bus->base +ASPEED_I2C_INTR_STS_REG ); - -out:return irq_handled; }#endif /* CONFIG_I2C_SLAVE */ @@ -380,21 +372,21 @@ static int aspeed_i2c_is_irq_error(u32 irq_status)return 0 ; } -static bool aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus) +static u32 aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus,u32 irq_status) { -u32 irq_status, status_ack =0 , command =0 ; +u32 irq_handled =0 , command =0 ;struct i2c_msg *msg;u8 recv_byte;int ret; - irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); -/* Ack all interrupt bits. */ -writel (irq_status, bus->base +ASPEED_I2C_INTR_STS_REG ); -if (irq_status &ASPEED_I2CD_INTR_BUS_RECOVER_DONE ) { bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - status_ack |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; + irq_handled |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; goto out_complete; + }else { +/* Master is not currently active, irq was for someone else. */ +if (bus->master_state ==ASPEED_I2C_MASTER_INACTIVE ) + goto out_no_complete; }/* @@ -403,19 +395,22 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)* INACTIVE state. */ ret =aspeed_i2c_is_irq_error (irq_status); -if (ret <0 ) { +if (ret) {dev_dbg (bus->dev,"received error interrupt: 0x%08x\n" , irq_status); bus->cmd_err = ret; bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + irq_handled |= (irq_status &ASPEED_I2CD_INTR_MASTER_ERRORS ); goto out_complete; }/* We are in an invalid state; reset bus to a known state. */ if (!bus->msgs) { -dev_err (bus->dev,"bus in unknown state\n" ); +dev_err (bus->dev,"bus in unknown state. irq_status: 0x%x\n" , + irq_status); bus->cmd_err = -EIO ; -if (bus->master_state !=ASPEED_I2C_MASTER_STOP ) +if (bus->master_state !=ASPEED_I2C_MASTER_STOP && + bus->master_state !=ASPEED_I2C_MASTER_INACTIVE )aspeed_i2c_do_stop (bus); goto out_no_complete; }@@ -428,13 +423,18 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)*/ if (bus->master_state ==ASPEED_I2C_MASTER_START ) {if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) { +if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_NAK ))) { + bus->cmd_err = -ENXIO ; + bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; + goto out_complete; + }pr_devel ("no slave present at %02x\n" , msg->addr); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; bus->cmd_err = -ENXIO ;aspeed_i2c_do_stop (bus); goto out_no_complete; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;if (msg->len ==0 ) {/* SMBUS_QUICK */ aspeed_i2c_do_stop (bus); goto out_no_complete;@@ -449,13 +449,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)case ASPEED_I2C_MASTER_TX :if (unlikely (irq_status &ASPEED_I2CD_INTR_TX_NAK )) {dev_dbg (bus->dev,"slave NACKed TX\n" ); - status_ack |=ASPEED_I2CD_INTR_TX_NAK ; + irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; goto error_and_stop; }else if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) {dev_err (bus->dev,"slave failed to ACK TX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_TX_ACK ; + irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;/* fallthrough intended */ case ASPEED_I2C_MASTER_TX_FIRST :if (bus->buf_index < msg->len) {@@ -478,7 +478,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)dev_err (bus->dev,"master failed to RX\n" ); goto error_and_stop; } - status_ack |=ASPEED_I2CD_INTR_RX_DONE ; + irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; recv_byte =readl (bus->base +ASPEED_I2C_BYTE_BUF_REG ) >>8 ; msg->buf[bus->buf_index++] = recv_byte;@@ -506,11 +506,13 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) goto out_no_complete;case ASPEED_I2C_MASTER_STOP :if (unlikely (!(irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ))) { -dev_err (bus->dev,"master failed to STOP\n" ); +dev_err (bus->dev, +"master failed to STOP. irq_status:0x%x\n" , + irq_status); bus->cmd_err = -EIO ;/* Do not STOP as we have already tried. */ }else { - status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; + irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; } bus->master_state =ASPEED_I2C_MASTER_INACTIVE ;@@ -540,33 +542,52 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) bus->master_xfer_result = bus->msgs_index +1 ;complete (&bus->cmd_complete); out_no_complete: -if (irq_status != status_ack) -dev_err (bus->dev, -"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , - irq_status, status_ack); -return !!irq_status; +return irq_handled; }static irqreturn_t aspeed_i2c_bus_irq (int irq,void *dev_id) {struct aspeed_i2c_bus *bus = dev_id; -bool ret; +u32 irq_received, irq_remaining, irq_handled;spin_lock (&bus->lock); + irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); + irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE ) -if (aspeed_i2c_slave_irq (bus)) { -dev_dbg (bus->dev,"irq handled by slave.\n" ); - ret = true; - goto out; +/* +* In most cases, interrupt bits will be set one by one, although +* multiple interrupt bits could be set at the same time. It's also +* possible that master interrupt bits could be set along with slave +* interrupt bits. Each case needs to be handled using corresponding +* handlers depending on the current state. +*/ +if (bus->master_state !=ASPEED_I2C_MASTER_INACTIVE ) { + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_slave_irq (bus, irq_remaining); + }else { + irq_handled =aspeed_i2c_slave_irq (bus, irq_remaining); + irq_remaining &= ~irq_handled; +if (irq_remaining) + irq_handled |=aspeed_i2c_master_irq (bus, + irq_remaining); } +#else + irq_handled =aspeed_i2c_master_irq (bus, irq_remaining);#endif /* CONFIG_I2C_SLAVE */ - ret =aspeed_i2c_master_irq (bus); + irq_remaining &= ~irq_handled; +if (irq_remaining) +dev_err (bus->dev, +"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , + irq_received, irq_handled); -out: +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock); -return ret ?IRQ_HANDLED :IRQ_NONE ; +return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }
Quoted 9 lines
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-1static int aspeed_i2c_master_xfer(struct i2c_adapter *adap, --2.18.0
Looks awesome! Thanks!
Reviewed-by Brendan Higgins <[email protected]>
Thanks a lot for your review Brendan!
-Jae
Quoted 7 lines
Quoted 5 lines
Looks awesome! Thanks!
Reviewed-by Brendan Higgins <[email protected]>Thanks a lot for your review Brendan!
Please don't quote the whole message. Everyone else has to scroll down a lot to find the relevant information. Thanks!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180906/997d89f1/attachment.sig>
Quoted 4 lines
Please don't quote the whole message. Everyone else has to scroll down a lot to find the relevant information. Thanks!
Will keep that in mind. Thanks Wolfram!
Wolfram Sang
AppliedQuoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.
Quoted 1 line
Signed-off-by Jae Hyun Yoo <[email protected]>
Applied to for-next, thanks!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180906/e639f9f3/attachment.sig>
Hi,
Quoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.
Quoted 2 lines
Signed-off-by Jae Hyun Yoo <[email protected]>Reviewed-by Brendan Higgins <[email protected]>
This patch causes a boot stall when booting witherspoon-bmc with qemu v3.0, and all i2c device probes fail with error -110 (timeout). Bisect log is attached for reference.
With the same kernel configuration (aspeed_g5_defconfig), ast2500-evb and romulus-bmc are still able to boot. palmetto-bmc with aspeed_g4_defconfig also appears to work.
Is this a problem with qemu ? Should I drop the qemu test for witherspoon-bmc starting with the next kernel release ?
Thanks,
Guenter
---
# bad: [09c0888767529cdb382f34452819e42d1a66a114] Add linux-next specific files for 20180911 # good: [11da3a7f84f19c26da6f86af878298694ede0804] Linux 4.19-rc3
git bisect start 'HEAD' 'v4.19-rc3'
# bad: [a2ebc71cf97bed9b453318418e4a281434565e8b] Merge remote-tracking branch 'nfc-next/master' git bisect bad a2ebc71cf97bed9b453318418e4a281434565e8b
# good: [6fde463b32bf4105c28c0a297a5b66aca5d6ecd4] Merge remote-tracking branch 's390/features' git bisect good 6fde463b32bf4105c28c0a297a5b66aca5d6ecd4
# bad: [136fd6d530a3ae0dd003984f683345cfe88c01f3] Merge remote-tracking branch 'v4l-dvb/master' git bisect bad 136fd6d530a3ae0dd003984f683345cfe88c01f3
# good: [c7ae95368af43c08f5f615b00f2f7bf2e9c45788] Merge remote-tracking branch 'v9fs/9p-next' git bisect good c7ae95368af43c08f5f615b00f2f7bf2e9c45788
# good: [4c640c41381e47b328c6507bcf534812761256cd] Merge branch 'for-4.19/fixes' into for-next git bisect good 4c640c41381e47b328c6507bcf534812761256cd
# good: [5bc91f70c5ecc2bc5967b98ce7fa4e55ad230d99] Merge remote-tracking branch 'hid/for-next' git bisect good 5bc91f70c5ecc2bc5967b98ce7fa4e55ad230d99
# bad: [657b9d37406ed1625d469db0fd356e364dc75dd8] Merge remote-tracking branch 'hwmon-staging/hwmon-next' git bisect bad 657b9d37406ed1625d469db0fd356e364dc75dd8
# bad: [fc9f90ddace238716cfcbd00d51428ee8baa12c7] Merge branch 'i2c/for-current' into i2c/for-next git bisect bad fc9f90ddace238716cfcbd00d51428ee8baa12c7
# good: [34b7be301d4c5d85d1d093d2faf856f3d727416f] Merge branch 'i2c/for-current' into i2c/for-next git bisect good 34b7be301d4c5d85d1d093d2faf856f3d727416f
# bad: [3e9efc3299dd78a0fa96515f0a453fab1ed4a1bd] i2c: aspeed: Handle master/slave combined irq events properly git bisect bad 3e9efc3299dd78a0fa96515f0a453fab1ed4a1bd
# good: [fc66b39fe36acfd06f716e338de7cd8f9550fad2] i2c: mediatek: Use DMA safe buffers for i2c transactions git bisect good fc66b39fe36acfd06f716e338de7cd8f9550fad2
# first bad commit: [3e9efc3299dd78a0fa96515f0a453fab1ed4a1bd] i2c: aspeed: Handle master/slave combined irq events properly
Quoted 24 lines · Guenter Roeck
Hi,
Quoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Quoted 2 lines
Signed-off-by Jae Hyun Yoo <[email protected]>Reviewed-by Brendan Higgins <[email protected]>This patch causes a boot stall when booting witherspoon-bmc with qemu v3.0, and all i2c device probes fail with error -110 (timeout). Bisect log is attached for reference.
Quoted 6 lines
With the same kernel configuration (
aspeed_g5_defconfig), ast2500-evb and romulus-bmc are still able to boot. palmetto-bmc withaspeed_g4_defconfigalso appears to work.Is this a problem with qemu ? Should I drop the qemu test for witherspoon-bmc starting with the next kernel release ?
The QEMU model for the Aspeed I2C controller does not support slave mode or DMA registers. That might be the issue.
I will check what this patch does when I have sometime.
Thanks,
C.
Quoted 28 lines · Guenter Roeck
Hi,
Quoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Quoted 2 lines
Signed-off-by Jae Hyun Yoo <[email protected]>Reviewed-by Brendan Higgins <[email protected]>This patch causes a boot stall when booting witherspoon-bmc with qemu v3.0, and all i2c device probes fail with error -110 (timeout). Bisect log is attached for reference.
With the same kernel configuration (
aspeed_g5_defconfig), ast2500-evb and romulus-bmc are still able to boot. palmetto-bmc withaspeed_g4_defconfigalso appears to work.
Quoted 6 lines
Is this a problem with qemu ? Should I drop the qemu test for witherspoon-bmc starting with the next kernel release ?
Thanks,
Guenter
Hi Guenter,
Thanks for your report.
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
Thanks,
Jae
Quoted 36 lines · Jae Hyun Yoo
Quoted 28 lines · Guenter Roeck
Hi,
Quoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Quoted 2 lines
Signed-off-by Jae Hyun Yoo <[email protected]>Reviewed-by Brendan Higgins <[email protected]>This patch causes a boot stall when booting witherspoon-bmc with qemu v3.0, and all i2c device probes fail with error -110 (timeout). Bisect log is attached for reference.
With the same kernel configuration (
aspeed_g5_defconfig), ast2500-evb and romulus-bmc are still able to boot. palmetto-bmc withaspeed_g4_defconfigalso appears to work.Quoted 6 lines
Is this a problem with qemu ? Should I drop the qemu test for witherspoon-bmc starting with the next kernel release ?
Thanks,
Guenter
Quoted 8 lines
Hi Guenter,
Thanks for your report.
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
The problem may be that qemu and the new code disagree how interrupts should be generated and handled, and the new code does not handle the interrupts it receives from the simulated hardware. This will result in i2c device probe failure, which in turn can cause all kinds of problems.
Thanks,
Guenter
Quoted 45 lines · Guenter Roeck
Quoted 36 lines · Jae Hyun Yoo
Quoted 28 lines · Guenter Roeck
Hi,
Quoted 14 lines · Jae Hyun Yoo
In most of cases, interrupt bits are set one by one but there are also a lot of other cases that Aspeed I2C IP sends multiple interrupt bits with combining master and slave events using a single interrupt call. It happens much more in multi-master environment than single-master. For an example, when master is waiting for a
NORMAL_STOPinterrupt in itsMASTER_STOPstate,SLAVE_MATCHandRX_DONEinterrupts could come along with theNORMAL_STOPin case of an another master immediately sends data just after acquiring the bus. In this case, theNORMAL_STOPinterrupt should be handled bymaster_irqand theSLAVE_MATCHandRX_DONEinterrupts should be handled byslave_irq. This commit modifies irq hadling logic to handle the master/slave combined events properly.Quoted 2 lines
Signed-off-by Jae Hyun Yoo <[email protected]>Reviewed-by Brendan Higgins <[email protected]>This patch causes a boot stall when booting witherspoon-bmc with qemu v3.0, and all i2c device probes fail with error -110 (timeout). Bisect log is attached for reference.
With the same kernel configuration (
aspeed_g5_defconfig), ast2500-evb and romulus-bmc are still able to boot. palmetto-bmc withaspeed_g4_defconfigalso appears to work.Quoted 6 lines
Is this a problem with qemu ? Should I drop the qemu test for witherspoon-bmc starting with the next kernel release ?
Thanks,
GuenterQuoted 8 lines
Hi Guenter,
Thanks for your report.
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
Quoted 6 lines
The problem may be that qemu and the new code disagree how interrupts should be generated and handled, and the new code does not handle the interrupts it receives from the simulated hardware. This will result in i2c device probe failure, which in turn can cause all kinds of problems.
Yes, that makes sense. Looks like it should be reverted until the issue is fixed. Will submit a patch to revert it.
Thanks,
Jae
PATCH 1/1 Revert "i2c: aspeed: Handle master/slave combined irq events properly"
This reverts commit 3e9efc3299dd "i2c: aspeed: Handle master/slave combined irq events properly" to fix an issue which is reported from QEMU v3.0 environment.
--- drivers/i2c/busses/i2c-aspeed.c | 131 ++++++++++++++------------------ 1 file changed, 55 insertions(+), 76 deletions(-)
drivers/i2c/busses/i2c-aspeed.c +55 -76
@@ -82,11 +82,6 @@#define ASPEED_I2CD_INTR_RX_DONE BIT(2)#define ASPEED_I2CD_INTR_TX_NAK BIT(1)#define ASPEED_I2CD_INTR_TX_ACK BIT(0) -#define ASPEED_I2CD_INTR_MASTER_ERRORS \ - (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ - ASPEED_I2CD_INTR_SCL_TIMEOUT | \ - ASPEED_I2CD_INTR_ABNORMAL | \ - ASPEED_I2CD_INTR_ARBIT_LOSS)#define ASPEED_I2CD_INTR_ALL \ (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \
@@ -232,26 +227,32 @@ static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) }#if IS_ENABLED (CONFIG_I2C_SLAVE ) -static u32 aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus,u32 irq_status) +static bool aspeed_i2c_slave_irq (struct aspeed_i2c_bus *bus) { -u32 command, irq_handled =0 ; +u32 command, irq_status, status_ack =0 ;struct i2c_client *slave = bus->slave; +bool irq_handled = true;u8 value; -if (!slave) -return 0 ; +if (!slave) { + irq_handled = false; + goto out; + } command =readl (bus->base +ASPEED_I2C_CMD_REG ); + irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG );/* Slave was requested, restart state machine. */ if (irq_status &ASPEED_I2CD_INTR_SLAVE_MATCH ) { - irq_handled |=ASPEED_I2CD_INTR_SLAVE_MATCH ; + status_ack |=ASPEED_I2CD_INTR_SLAVE_MATCH ; bus->slave_state =ASPEED_I2C_SLAVE_START ; }/* Slave is not currently active, irq was for someone else. */ -if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) -return irq_handled; +if (bus->slave_state ==ASPEED_I2C_SLAVE_STOP ) { + irq_handled = false; + goto out; + }dev_dbg (bus->dev,"slave irq status 0x%08x, cmd 0x%08x\n" , irq_status, command);
@@ -268,31 +269,31 @@ static u32 aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus, u32 irq_status) bus->slave_state =ASPEED_I2C_SLAVE_WRITE_REQUESTED ; } - irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; + status_ack |=ASPEED_I2CD_INTR_RX_DONE ; }/* Slave was asked to stop. */ if (irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ) { - irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; + status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; }if (irq_status &ASPEED_I2CD_INTR_TX_NAK ) { - irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; + status_ack |=ASPEED_I2CD_INTR_TX_NAK ; bus->slave_state =ASPEED_I2C_SLAVE_STOP ; } -if (irq_status &ASPEED_I2CD_INTR_TX_ACK ) - irq_handled |=ASPEED_I2CD_INTR_TX_ACK ;switch (bus->slave_state) {case ASPEED_I2C_SLAVE_READ_REQUESTED :if (irq_status &ASPEED_I2CD_INTR_TX_ACK )dev_err (bus->dev,"Unexpected ACK on read request.\n" ); bus->slave_state =ASPEED_I2C_SLAVE_READ_PROCESSED ; +i2c_slave_event (slave,I2C_SLAVE_READ_REQUESTED , &value);writel (value, bus->base +ASPEED_I2C_BYTE_BUF_REG );writel (ASPEED_I2CD_S_TX_CMD , bus->base +ASPEED_I2C_CMD_REG );break ;case ASPEED_I2C_SLAVE_READ_PROCESSED : + status_ack |=ASPEED_I2CD_INTR_TX_ACK ;if (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))dev_err (bus->dev,"Expected ACK after processed read.\n" );
@@ -316,6 +317,13 @@ static u32 aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus, u32 irq_status) break; } +if (status_ack != irq_status) +dev_err (bus->dev, +"irq handled != irq. expected %x, but was %x\n" , + irq_status, status_ack); +writel (status_ack, bus->base +ASPEED_I2C_INTR_STS_REG ); + +out:return irq_handled; }#endif /* CONFIG_I2C_SLAVE */
@@ -372,21 +380,21 @@ static int aspeed_i2c_is_irq_error(u32 irq_status)return 0 ; } -static u32 aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus,u32 irq_status) +static bool aspeed_i2c_master_irq (struct aspeed_i2c_bus *bus) { -u32 irq_handled =0 , command =0 ; +u32 irq_status, status_ack =0 , command =0 ;struct i2c_msg *msg;u8 recv_byte;int ret; + irq_status =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_status, bus->base +ASPEED_I2C_INTR_STS_REG ); +if (irq_status &ASPEED_I2CD_INTR_BUS_RECOVER_DONE ) { bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - irq_handled |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; + status_ack |=ASPEED_I2CD_INTR_BUS_RECOVER_DONE ; goto out_complete; - }else { -/* Master is not currently active, irq was for someone else. */ -if (bus->master_state ==ASPEED_I2C_MASTER_INACTIVE ) - goto out_no_complete; }/*
@@ -395,22 +403,19 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)* INACTIVE state. */ ret =aspeed_i2c_is_irq_error (irq_status); -if (ret) { +if (ret <0 ) {dev_dbg (bus->dev,"received error interrupt: 0x%08x\n" , irq_status); bus->cmd_err = ret; bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - irq_handled |= (irq_status &ASPEED_I2CD_INTR_MASTER_ERRORS ); goto out_complete; }/* We are in an invalid state; reset bus to a known state. */ if (!bus->msgs) { -dev_err (bus->dev,"bus in unknown state. irq_status: 0x%x\n" , - irq_status); +dev_err (bus->dev,"bus in unknown state\n" ); bus->cmd_err = -EIO ; -if (bus->master_state !=ASPEED_I2C_MASTER_STOP && - bus->master_state !=ASPEED_I2C_MASTER_INACTIVE ) +if (bus->master_state !=ASPEED_I2C_MASTER_STOP )aspeed_i2c_do_stop (bus); goto out_no_complete; }
@@ -423,18 +428,13 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)*/ if (bus->master_state ==ASPEED_I2C_MASTER_START ) {if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) { -if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_NAK ))) { - bus->cmd_err = -ENXIO ; - bus->master_state =ASPEED_I2C_MASTER_INACTIVE ; - goto out_complete; - }pr_devel ("no slave present at %02x\n" , msg->addr); - irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; + status_ack |=ASPEED_I2CD_INTR_TX_NAK ; bus->cmd_err = -ENXIO ;aspeed_i2c_do_stop (bus); goto out_no_complete; } - irq_handled |=ASPEED_I2CD_INTR_TX_ACK ; + status_ack |=ASPEED_I2CD_INTR_TX_ACK ;if (msg->len ==0 ) {/* SMBUS_QUICK */ aspeed_i2c_do_stop (bus); goto out_no_complete;
@@ -449,13 +449,13 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)case ASPEED_I2C_MASTER_TX :if (unlikely (irq_status &ASPEED_I2CD_INTR_TX_NAK )) {dev_dbg (bus->dev,"slave NACKed TX\n" ); - irq_handled |=ASPEED_I2CD_INTR_TX_NAK ; + status_ack |=ASPEED_I2CD_INTR_TX_NAK ; goto error_and_stop; }else if (unlikely (!(irq_status &ASPEED_I2CD_INTR_TX_ACK ))) {dev_err (bus->dev,"slave failed to ACK TX\n" ); goto error_and_stop; } - irq_handled |=ASPEED_I2CD_INTR_TX_ACK ; + status_ack |=ASPEED_I2CD_INTR_TX_ACK ;/* fallthrough intended */ case ASPEED_I2C_MASTER_TX_FIRST :if (bus->buf_index < msg->len) {
@@ -478,7 +478,7 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status)dev_err (bus->dev,"master failed to RX\n" ); goto error_and_stop; } - irq_handled |=ASPEED_I2CD_INTR_RX_DONE ; + status_ack |=ASPEED_I2CD_INTR_RX_DONE ; recv_byte =readl (bus->base +ASPEED_I2C_BYTE_BUF_REG ) >>8 ; msg->buf[bus->buf_index++] = recv_byte;
@@ -506,13 +506,11 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status) goto out_no_complete;case ASPEED_I2C_MASTER_STOP :if (unlikely (!(irq_status &ASPEED_I2CD_INTR_NORMAL_STOP ))) { -dev_err (bus->dev, -"master failed to STOP. irq_status:0x%x\n" , - irq_status); +dev_err (bus->dev,"master failed to STOP\n" ); bus->cmd_err = -EIO ;/* Do not STOP as we have already tried. */ }else { - irq_handled |=ASPEED_I2CD_INTR_NORMAL_STOP ; + status_ack |=ASPEED_I2CD_INTR_NORMAL_STOP ; } bus->master_state =ASPEED_I2C_MASTER_INACTIVE ;
@@ -542,52 +540,33 @@ static u32 aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus, u32 irq_status) bus->master_xfer_result = bus->msgs_index +1 ;complete (&bus->cmd_complete); out_no_complete: -return irq_handled; +if (irq_status != status_ack) +dev_err (bus->dev, +"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , + irq_status, status_ack); +return !!irq_status; }static irqreturn_t aspeed_i2c_bus_irq (int irq,void *dev_id) {struct aspeed_i2c_bus *bus = dev_id; -u32 irq_received, irq_remaining, irq_handled; +bool ret;spin_lock (&bus->lock); - irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); - irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE ) -/* -* In most cases, interrupt bits will be set one by one, although -* multiple interrupt bits could be set at the same time. It's also -* possible that master interrupt bits could be set along with slave -* interrupt bits. Each case needs to be handled using corresponding -* handlers depending on the current state. -*/ -if (bus->master_state !=ASPEED_I2C_MASTER_INACTIVE ) { - irq_handled =aspeed_i2c_master_irq (bus, irq_remaining); - irq_remaining &= ~irq_handled; -if (irq_remaining) - irq_handled |=aspeed_i2c_slave_irq (bus, irq_remaining); - }else { - irq_handled =aspeed_i2c_slave_irq (bus, irq_remaining); - irq_remaining &= ~irq_handled; -if (irq_remaining) - irq_handled |=aspeed_i2c_master_irq (bus, - irq_remaining); +if (aspeed_i2c_slave_irq (bus)) { +dev_dbg (bus->dev,"irq handled by slave.\n" ); + ret = true; + goto out; } -#else - irq_handled =aspeed_i2c_master_irq (bus, irq_remaining);#endif /* CONFIG_I2C_SLAVE */ - irq_remaining &= ~irq_handled; -if (irq_remaining) -dev_err (bus->dev, -"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , - irq_received, irq_handled); + ret =aspeed_i2c_master_irq (bus); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); +out:spin_unlock (&bus->lock); -return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; +return ret ?IRQ_HANDLED :IRQ_NONE ; } static int aspeed_i2c_master_xfer(struct i2c_adapter *adap,
Signature · 1 line
2.18.0
Joel Stanley
maintainerQuoted 15 lines · Jae Hyun Yoo
Quoted 6 lines · Guenter Roeck
Quoted 4 lines · Jae Hyun Yoo
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
Quoted 6 lines
The problem may be that qemu and the new code disagree how interrupts should be generated and handled, and the new code does not handle the interrupts it receives from the simulated hardware. This will result in i2c device probe failure, which in turn can cause all kinds of problems.
Quoted 2 lines
Yes, that makes sense. Looks like it should be reverted until the issue is fixed. Will submit a patch to revert it.
Let's not rush. The qemu model was written in order to allow us to test the kernel code, and was validated by the kernel driver we have. We've had situations in the past (with the i2c driver in fact) where a change in the driver required an update of the model to be more accurate.
I suggest we wait until Cedric has a chance to look at the issue before reverting the patch.
Cheers,
Joel
Quoted 19 lines · Joel Stanley
Quoted 15 lines · Jae Hyun Yoo
Quoted 6 lines · Guenter Roeck
Quoted 4 lines · Jae Hyun Yoo
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
Quoted 6 lines
The problem may be that qemu and the new code disagree how interrupts should be generated and handled, and the new code does not handle the interrupts it receives from the simulated hardware. This will result in i2c device probe failure, which in turn can cause all kinds of problems.
Quoted 2 lines
Yes, that makes sense. Looks like it should be reverted until the issue is fixed. Will submit a patch to revert it.
Quoted 9 lines
Let's not rush. The qemu model was written in order to allow us to test the kernel code, and was validated by the kernel driver we have. We've had situations in the past (with the i2c driver in fact) where a change in the driver required an update of the model to be more accurate.
I suggest we wait until Cedric has a chance to look at the issue before reverting the patch.
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2 -2
@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )
@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }
Quoted 26 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-0@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c +0
-2@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }
My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race condition as you pointed out. I tested your code change and checked that it works well. Let me take more sufficient test on real H/W. Will share the test result.
Thanks a lot!
Jae
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );
Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Thanks,
Guenter
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.
It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Let me share my test result. Your code change works on 100KHz bus speed but doesn't work well on 1MHz bus speed. Investigated that interrupt handling is fast enough in 100KHz test but in 1MHz, most of data is corrupted because the bit is cleared at the beginning of interrupt handler so it allows receiving of the next data but the interrupt handler isn't fast enough to read the data buffer on time. I checked this problem on BMC-ME channel which ME sends lots of IPMB packets to BMC at 1MHz speed. You could simply check the data corruption problem on the BMC-ME channel.
My thought is, the current code is right for real Aspeed I2C hardware. It seems that QEMU 3.0 model for witherspoon-bmc doesn't simulate the actual Aspeed I2C hardware correctly.
Thanks,
Jae
Quoted 54 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
Quoted 9 lines
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
That makes sense. Does that apply to the other status bits as well ? Reason for asking is that the current code actually gets stuck in transmit, not receive.
Thanks,
Guenter
Quoted 60 lines · Guenter Roeck
Quoted 59 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.
Quoted 8 lines
Quoted 4 lines
It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
That makes sense. Does that apply to the other status bits as well ? Reason for asking is that the current code actually gets stuck in transmit, not receive.
Only bit 2 has that description in datasheet. Is slave config enabled for QEMU build? Does that get stuck in master sending or slave receiving?
Thanks,
Jae
Quoted 65 lines · Jae Hyun Yoo
Quoted 64 lines · Guenter Roeck
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 8 lines
Quoted 4 lines
That makes sense. Does that apply to the other status bits as well ? Reason for asking is that the current code actually gets stuck in transmit, not receive.
Only bit 2 has that description in datasheet. Is slave config enabled for QEMU build? Does that get stuck in master sending or slave receiving?
qemu does not support slave mode. Linux gets stuck in master tx.
I played with the code on both sides. I had to make changes in both the linux kernel and in qemu to get the code to work again. See attached.
Guenter
---
Linux:
drivers/i2c/busses/i2c-aspeed.c +7 -2
@@ -552,6 +552,9 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupts except for Rx done */ +writel (irq_received & ~ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )
@@ -584,8 +587,10 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack Rx done */ +if (irq_received &ASPEED_I2CD_INTR_RX_DONE ) +writel (ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }
---
qemu:
hw/i2c/aspeed_i2c.c +39 -19
89 lines not on this page · raw message
Quoted 205 lines · Guenter Roeck
Quoted 65 lines · Jae Hyun Yoo
Quoted 64 lines · Guenter Roeck
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 8 lines
Quoted 4 lines
That makes sense. Does that apply to the other status bits as well ? Reason for asking is that the current code actually gets stuck in transmit, not receive.
Only bit 2 has that description in datasheet. Is slave config enabled for QEMU build? Does that get stuck in master sending or slave receiving?
qemu does not support slave mode. Linux gets stuck in master tx.
I played with the code on both sides. I had to make changes in both the linux kernel and in qemu to get the code to work again. See attached.
Guenter
---
Linux:drivers/i2c/busses/i2c-aspeed.c +7
-3@@ -552,6 +552,9 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupts except for Rx done */ +writel (irq_received & ~ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +587,10 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack Rx done */ +if (irq_received &ASPEED_I2CD_INTR_RX_DONE ) +writel (ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; } ---qemu:
hw/i2c/aspeed_i2c.c +32
-19@@ -180,6 +180,33 @@ static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)return (bus->cmd >>I2CD_TX_STATE_SHIFT ) &I2CD_TX_STATE_MASK ; } +static void aspeed_i2c_handle_rx_cmd (AspeedI2CBus *bus) +{ +int ret; + +if (!(bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ))) { +return ; + } +if (bus->intr_status &I2CD_INTR_RX_DONE ) { +return ; + } + +aspeed_i2c_set_state (bus,I2CD_MRXD ); + ret =i2c_recv (bus->bus); +if (ret <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); + ret =0xff ; + }else { + bus->intr_status |=I2CD_INTR_RX_DONE ; + } + bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; +if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { +i2c_nack (bus->bus); + } + bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); +aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +} +/* * The state machine needs some refinement. It is only used to track * invalid STOP commands for the moment. @@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) { bus->cmd &= ~0xFFFF ; bus->cmd |= value &0xFFFF ; - bus->intr_status =0 ; + bus->intr_status &=I2CD_INTR_RX_DONE ;if (bus->cmd &I2CD_M_START_CMD ) { uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?@@ -227,22 +254,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) }if (bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST )) { -int ret; - -aspeed_i2c_set_state (bus,I2CD_MRXD ); - ret =i2c_recv (bus->bus); -if (ret <0 ) { -qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); - ret =0xff ; - }else { - bus->intr_status |=I2CD_INTR_RX_DONE ; - } - bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; -if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { -i2c_nack (bus->bus); - } - bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); -aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +aspeed_i2c_handle_rx_cmd (bus); }if (bus->cmd &I2CD_M_STOP_CMD ) {@@ -263,6 +275,7 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) {AspeedI2CBus *bus = opaque; +int status;switch (offset) {case I2CD_FUN_CTRL_REG :@@ -283,9 +296,16 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, bus->intr_ctrl = value &0x7FFF ;break ;case I2CD_INTR_STS_REG : + status = bus->intr_status; bus->intr_status &= ~(value &0x7FFF ); - bus->controller->intr_status &= ~(1 << bus->id); -qemu_irq_lower (bus->controller->irq); +if (!bus->intr_status) {
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 311
Quoted 10 lines
hw/i2c/aspeed_i2c.c +7
-0+ bus->controller->intr_status &= ~(1 << bus->id); +qemu_irq_lower (bus->controller->irq); + } +if ((status &I2CD_INTR_RX_DONE ) && !(bus->intr_status &I2CD_INTR_RX_DONE )) { +aspeed_i2c_handle_rx_cmd (bus); +aspeed_i2c_bus_raise_interrupt (bus); + }break ;case I2CD_DEV_ADDR_REG : qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
Nice fix! LGTM. I've tested the new patch and checked that it works well on both low and high bus speed environments. Thanks a lot!
Can you please submit this patch?
Thanks,
Jae
Quoted 136 lines · Jae Hyun Yoo
Quoted 122 lines
I played with the code on both sides. I had to make changes in both the linux kernel and in qemu to get the code to work again. See attached.
Guenter
---
Linux:drivers/i2c/busses/i2c-aspeed.c +7
-3@@ -552,6 +552,9 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupts except for Rx done */ +writel (irq_received & ~ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +587,10 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack Rx done */ +if (irq_received &ASPEED_I2CD_INTR_RX_DONE ) +writel (ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; } ---qemu:
hw/i2c/aspeed_i2c.c +32
-19@@ -180,6 +180,33 @@ static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)return (bus->cmd >>I2CD_TX_STATE_SHIFT ) &I2CD_TX_STATE_MASK ; } +static void aspeed_i2c_handle_rx_cmd (AspeedI2CBus *bus) +{ +int ret; + +if (!(bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ))) { +return ; + } +if (bus->intr_status &I2CD_INTR_RX_DONE ) { +return ; + } + +aspeed_i2c_set_state (bus,I2CD_MRXD ); + ret =i2c_recv (bus->bus); +if (ret <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); + ret =0xff ; + }else { + bus->intr_status |=I2CD_INTR_RX_DONE ; + } + bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; +if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { +i2c_nack (bus->bus); + } + bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); +aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +} +/* * The state machine needs some refinement. It is only used to track * invalid STOP commands for the moment. @@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) { bus->cmd &= ~0xFFFF ; bus->cmd |= value &0xFFFF ; - bus->intr_status =0 ; + bus->intr_status &=I2CD_INTR_RX_DONE ;if (bus->cmd &I2CD_M_START_CMD ) { uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?@@ -227,22 +254,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) }if (bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST )) { -int ret; - -aspeed_i2c_set_state (bus,I2CD_MRXD ); - ret =i2c_recv (bus->bus); -if (ret <0 ) { -qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); - ret =0xff ; - }else { - bus->intr_status |=I2CD_INTR_RX_DONE ; - } - bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; -if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { -i2c_nack (bus->bus); - } - bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); -aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +aspeed_i2c_handle_rx_cmd (bus); }if (bus->cmd &I2CD_M_STOP_CMD ) {@@ -263,6 +275,7 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) {AspeedI2CBus *bus = opaque; +int status;switch (offset) {case I2CD_FUN_CTRL_REG :@@ -283,9 +296,16 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, bus->intr_ctrl = value &0x7FFF ;break ;case I2CD_INTR_STS_REG : + status = bus->intr_status; bus->intr_status &= ~(value &0x7FFF ); - bus->controller->intr_status &= ~(1 << bus->id); -qemu_irq_lower (bus->controller->irq); +if (!bus->intr_status) {hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 311
Quoted 10 lines
hw/i2c/aspeed_i2c.c +7
-0+ bus->controller->intr_status &= ~(1 << bus->id); +qemu_irq_lower (bus->controller->irq); + } +if ((status &I2CD_INTR_RX_DONE ) && !(bus->intr_status &I2CD_INTR_RX_DONE )) { +aspeed_i2c_handle_rx_cmd (bus); +aspeed_i2c_bus_raise_interrupt (bus); + }break ;case I2CD_DEV_ADDR_REG : qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",Nice fix! LGTM. I've tested the new patch and checked that it works well on both low and high bus speed environments. Thanks a lot!
Quoted 2 lines
Can you please submit this patch?
Assuming you mean both patches, sure, can do. I'll need to clean up the qemu patch a bit, though; it looks too messy for my liking.
Guenter
Quoted 121 lines · Guenter Roeck
Quoted 65 lines · Jae Hyun Yoo
Quoted 64 lines · Guenter Roeck
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
I2CD10 Interrupt Status Register
bit 2 Receive Done Interrupt status
S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 8 lines
Quoted 4 lines
That makes sense. Does that apply to the other status bits as well ? Reason for asking is that the current code actually gets stuck in transmit, not receive.
Only bit 2 has that description in datasheet. Is slave config enabled for QEMU build? Does that get stuck in master sending or slave receiving?
qemu does not support slave mode. Linux gets stuck in master tx.
I played with the code on both sides. I had to make changes in both the linux kernel and in qemu to get the code to work again. See attached.
Guenter
---
Linux:drivers/i2c/busses/i2c-aspeed.c +7
-3@@ -552,6 +552,9 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupts except for Rx done */ +writel (irq_received & ~ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +587,10 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack Rx done */ +if (irq_received &ASPEED_I2CD_INTR_RX_DONE ) +writel (ASPEED_I2CD_INTR_RX_DONE , + bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; } ---qemu:
hw/i2c/aspeed_i2c.c +0
-0
hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 189
Quoted 10 lines
hw/i2c/aspeed_i2c.c +7
-0@@ -180,6 +180,33 @@ static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)return (bus->cmd >>I2CD_TX_STATE_SHIFT ) &I2CD_TX_STATE_MASK ; } +static void aspeed_i2c_handle_rx_cmd (AspeedI2CBus *bus) +{ +int ret; + +if (!(bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ))) { +return ; + }
it deserves a comment to understand which scenario we are trying to handle.
hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 192
Quoted 3 lines
hw/i2c/aspeed_i2c.c +3
-0+if (bus->intr_status &I2CD_INTR_RX_DONE ) { +return ; + }
should be handled in aspeed_i2c_bus_handle_cmd() I think
Quoted 20 lines
hw/i2c/aspeed_i2c.c +16
-0+aspeed_i2c_set_state (bus,I2CD_MRXD ); + ret =i2c_recv (bus->bus); +if (ret <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); + ret =0xff ; + }else { + bus->intr_status |=I2CD_INTR_RX_DONE ; + } + bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; +if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { +i2c_nack (bus->bus); + } + bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); +aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +} +/* * The state machine needs some refinement. It is only used to track * invalid STOP commands for the moment.
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 4 lines
hw/i2c/aspeed_i2c.c +0
-1@@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) { bus->cmd &= ~0xFFFF ; bus->cmd |= value &0xFFFF ; - bus->intr_status = 0;> + bus->intr_status &= I2CD_INTR_RX_DONE;
it deserves a comment to understand which scenario we are trying to handle.
Quoted 36 lines
hw/i2c/aspeed_i2c.c +2
-16if (bus->cmd &I2CD_M_START_CMD ) { uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?@@ -227,22 +254,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) }if (bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST )) { -int ret; - -aspeed_i2c_set_state (bus,I2CD_MRXD ); - ret =i2c_recv (bus->bus); -if (ret <0 ) { -qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); - ret =0xff ; - }else { - bus->intr_status |=I2CD_INTR_RX_DONE ; - } - bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; -if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { -i2c_nack (bus->bus); - } - bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); -aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +aspeed_i2c_handle_rx_cmd (bus); }if (bus->cmd &I2CD_M_STOP_CMD ) {@@ -263,6 +275,7 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) {AspeedI2CBus *bus = opaque; +int status;switch (offset) {case I2CD_FUN_CTRL_REG :@@ -283,9 +296,16 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, bus->intr_ctrl = value &0x7FFF ;
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 304
Quoted 10 lines
hw/i2c/aspeed_i2c.c +5
-2break ;case I2CD_INTR_STS_REG : + status = bus->intr_status; bus->intr_status &= ~(value &0x7FFF ); - bus->controller->intr_status &= ~(1 << bus->id); -qemu_irq_lower (bus->controller->irq); +if (!bus->intr_status) { + bus->controller->intr_status &= ~(1 << bus->id); +qemu_irq_lower (bus->controller->irq); + }
That part below is indeed something to fix. I had a similar patch.
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 308
Quoted 4 lines
hw/i2c/aspeed_i2c.c +4
-0+if ((status &I2CD_INTR_RX_DONE ) && !(bus->intr_status &I2CD_INTR_RX_DONE )) { +aspeed_i2c_handle_rx_cmd (bus); +aspeed_i2c_bus_raise_interrupt (bus); + }
ok.
Thanks for looking into this.
C.
Quoted 4 lines
hw/i2c/aspeed_i2c.c +0
-0break ;case I2CD_DEV_ADDR_REG : qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
On 09/12/2018 10:45 PM, C?dric Le Goater wrote
[ ... ]
Quoted 21 lines
Quoted 8 lines
---
qemu:hw/i2c/aspeed_i2c.c +0
-0hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 189
Quoted 10 lines
hw/i2c/aspeed_i2c.c +7
-0@@ -180,6 +180,33 @@ static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)return (bus->cmd >>I2CD_TX_STATE_SHIFT ) &I2CD_TX_STATE_MASK ; } +static void aspeed_i2c_handle_rx_cmd (AspeedI2CBus *bus) +{ +int ret; + +if (!(bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ))) { +return ; + }it deserves a comment to understand which scenario we are trying to handle.
Quoted 6 lines
hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 192
Quoted 3 lines
hw/i2c/aspeed_i2c.c +3
-0+if (bus->intr_status &I2CD_INTR_RX_DONE ) { +return ; + }should be handled in
aspeed_i2c_bus_handle_cmd()I think
I moved those two checks into the calling code.
Quoted 18 lines
Quoted 18 lines
hw/i2c/aspeed_i2c.c +16
-0+aspeed_i2c_set_state (bus,I2CD_MRXD ); + ret =i2c_recv (bus->bus); +if (ret <0 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); + ret =0xff ; + }else { + bus->intr_status |=I2CD_INTR_RX_DONE ; + } + bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; +if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { +i2c_nack (bus->bus); + } + bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); +aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +} +/* * The state machine needs some refinement. It is only used to track
Quoted 9 lines
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 6 lines
hw/i2c/aspeed_i2c.c +0
-1* invalid STOP commands for the moment. @@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) { bus->cmd &= ~0xFFFF ; bus->cmd |= value &0xFFFF ; - bus->intr_status = 0;> + bus->intr_status &= I2CD_INTR_RX_DONE;it deserves a comment to understand which scenario we are trying to handle.
Ok. FWIW, I wonder if intr_status should be touched here in the first place, but I neither have the hardware nor a datasheet, so I don't know if any bits are auto-cleared.
Quoted 32 lines
Quoted 32 lines
hw/i2c/aspeed_i2c.c +2
-16if (bus->cmd &I2CD_M_START_CMD ) { uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?@@ -227,22 +254,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) }if (bus->cmd & (I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST )) { -int ret; - -aspeed_i2c_set_state (bus,I2CD_MRXD ); - ret =i2c_recv (bus->bus); -if (ret <0 ) { -qemu_log_mask (LOG_GUEST_ERROR ,"%s: read failed\n" , __func__); - ret =0xff ; - }else { - bus->intr_status |=I2CD_INTR_RX_DONE ; - } - bus->buf = (ret &I2CD_BYTE_BUF_RX_MASK ) <<I2CD_BYTE_BUF_RX_SHIFT ; -if (bus->cmd &I2CD_M_S_RX_CMD_LAST ) { -i2c_nack (bus->bus); - } - bus->cmd &= ~(I2CD_M_RX_CMD |I2CD_M_S_RX_CMD_LAST ); -aspeed_i2c_set_state (bus,I2CD_MACTIVE ); +aspeed_i2c_handle_rx_cmd (bus); }if (bus->cmd &I2CD_M_STOP_CMD ) {@@ -263,6 +275,7 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) {AspeedI2CBus *bus = opaque; +int status;
Quoted 17 lines
Quoted 4 lines
hw/i2c/aspeed_i2c.c +0
-0switch (offset) {case I2CD_FUN_CTRL_REG :@@ -283,9 +296,16 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, bus->intr_ctrl = value &0x7FFF ;hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 304
Quoted 10 lines
hw/i2c/aspeed_i2c.c +5
-2break ;case I2CD_INTR_STS_REG : + status = bus->intr_status; bus->intr_status &= ~(value &0x7FFF ); - bus->controller->intr_status &= ~(1 << bus->id); -qemu_irq_lower (bus->controller->irq); +if (!bus->intr_status) { + bus->controller->intr_status &= ~(1 << bus->id); +qemu_irq_lower (bus->controller->irq); + }That part below is indeed something to fix. I had a similar patch.
Should I split it out as separate patch ? Alternatively, if you submitted your patch already, I'll be happy to use it as base for mine.
Thanks,
Guenter
Quoted 18 lines
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, · line 308
Quoted 5 lines
hw/i2c/aspeed_i2c.c +4
-0+if ((status &I2CD_INTR_RX_DONE ) && !(bus->intr_status &I2CD_INTR_RX_DONE )) { +aspeed_i2c_handle_rx_cmd (bus); +aspeed_i2c_bus_raise_interrupt (bus); + }ok.
Thanks for looking into this.
C.
Quoted 4 lines
hw/i2c/aspeed_i2c.c +0
-0break ;case I2CD_DEV_ADDR_REG : qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
Quoted 24 lines · Guenter Roeck
On 09/12/2018 10:45 PM, C?dric Le Goater wrote
[ ... ]
Quoted 20 lines
Quoted 8 lines
---
qemu:hw/i2c/aspeed_i2c.c +0
-0hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 188
Quoted 9 lines
hw/i2c/aspeed_i2c.c +6
-0@@ -180,6 +180,33 @@ static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) ????? return (bus->cmd >> I2CD_TX_STATE_SHIFT) & I2CD_TX_STATE_MASK; ? } ? +static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus) +{ +??? int ret; + +??? if (!(bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST))) { +??????? return; +??? }it deserves a comment to understand which scenario we are trying to handle.
Quoted 8 lines
Quoted 6 lines
hw/i2c/aspeed_i2c.c static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus) · line 191
Quoted 3 lines
hw/i2c/aspeed_i2c.c +3
-0+??? if (bus->intr_status & I2CD_INTR_RX_DONE) { +??????? return; +??? }should be handled in
aspeed_i2c_bus_handle_cmd()I thinkI moved those two checks into the calling code.
ok
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
hw/i2c/aspeed_i2c.c +16
-0+??? aspeed_i2c_set_state(bus, I2CD_MRXD); +??? ret = i2c_recv(bus->bus); +??? if (ret < 0) { +??????? qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__); +??????? ret = 0xff; +??? } else { +??????? bus->intr_status |= I2CD_INTR_RX_DONE; +??? } +??? bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT; +??? if (bus->cmd & I2CD_M_S_RX_CMD_LAST) { +??????? i2c_nack(bus->bus); +??? } +??? bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST); +??? aspeed_i2c_set_state(bus, I2CD_MACTIVE); +} +? /*
?? * The state machine needs some refinement. It is only used to track ?? * invalid STOP commands for the moment.Quoted 8 lines
hw/i2c/aspeed_i2c.c static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 5 lines
hw/i2c/aspeed_i2c.c +0
-1@@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0;> +??? bus->intr_status &= I2CD_INTR_RX_DONE;it deserves a comment to understand which scenario we are trying to handle. ??
Quoted 3 lines
Ok. FWIW, I wonder if
intr_statusshould be touched here in the first place, but I neither have the hardware nor a datasheet, so I don't know if any bits are auto-cleared.
I just pushed a patch on my branch with some more explanation :
https://github.com/legoater/qemu/commits/aspeed-3.1
Quoted 48 lines
Quoted 38 lines
Quoted 38 lines
hw/i2c/aspeed_i2c.c +0
-0????? if (bus->cmd & I2CD_M_START_CMD) {?????????
uint8_tstate =aspeed_i2c_get_state(bus)&I2CD_MACTIVE?hw/i2c/aspeed_i2c.c +2
-16@@ -227,22 +254,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) ????? } ? ????? if (bus->cmd & (I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST)) { -??????? int ret; - -??????? aspeed_i2c_set_state(bus, I2CD_MRXD); -??????? ret = i2c_recv(bus->bus); -??????? if (ret < 0) { -??????????? qemu_log_mask(LOG_GUEST_ERROR, "%s: read failed\n", __func__); -??????????? ret = 0xff; -??????? } else { -??????????? bus->intr_status |= I2CD_INTR_RX_DONE; -??????? } -??????? bus->buf = (ret & I2CD_BYTE_BUF_RX_MASK) << I2CD_BYTE_BUF_RX_SHIFT; -??????? if (bus->cmd & I2CD_M_S_RX_CMD_LAST) { -??????????? i2c_nack(bus->bus); -??????? } -??????? bus->cmd &= ~(I2CD_M_RX_CMD | I2CD_M_S_RX_CMD_LAST); -??????? aspeed_i2c_set_state(bus, I2CD_MACTIVE); +??????? aspeed_i2c_handle_rx_cmd(bus); ????? } ? ????? if (bus->cmd & I2CD_M_STOP_CMD) {@@ -263,6 +275,7 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, ?????????????????????????????????? uint64_t value, unsigned size) ? { ????? AspeedI2CBus *bus = opaque; +??? int status; ? ????? switch (offset) {????? case
I2CD_FUN_CTRL_REG:hw/i2c/aspeed_i2c.c +0
-0@@ -283,9 +296,16 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset, ????????? bus->intr_ctrl = value & 0x7FFF; ????????? break;????? case
I2CD_INTR_STS_REG:
+??????? status =bus->intr_status;
?????????bus->intr_status&= ~(value &0x7FFF);Quoted 9 lines
Quoted 6 lines
-???????
bus->controller->intr_status&= ~(1 <<bus->id); -???????qemu_irq_lower(bus->controller->irq); +??????? if (!bus->intr_status) {
+???????????bus->controller->intr_status&= ~(1 <<bus->id); +???????????qemu_irq_lower(bus->controller->irq); +??????? }That part below is indeed something to fix. I had a similar patch.
Quoted 2 lines
Should I split it out as separate patch ? Alternatively, if you submitted your patch already, I'll be happy to use it as base for mine.
See below.
Thanks a lot,
C.
Quoted 1 line
From
dea796e8d35ba7a70e4b14fbc30ff970a347b195Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= <[email protected]>
Date: Thu, 13 Sep 2018 17:44:32 +0200
Subject: [PATCH] aspeed/i2c: lower bus interrupt when all interrupts have been
cleared
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Also include some documentation on the interrupt status bits and how they should be cleared. Also, the model does not implement correctly the RX_DONE bit behavior which should be cleared to allow more data to be received. Yet to be fixed.
--- hw/i2c/aspeed_i2c.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
hw/i2c/aspeed_i2c.c +17 -3
@@ -52,6 +52,13 @@#define I2CD_AC_TIMING_REG2 0x08/* Clock and AC Timing Control #1 */ #define I2CD_INTR_CTRL_REG 0x0c/* I2CD Interrupt Control */ #define I2CD_INTR_STS_REG 0x10/* I2CD Interrupt Status */ + +#define I2CD_INTR_SLAVE_ADDR_MATCH (0x1 << 31)/* 0: addr1 1: addr2 */ +#define I2CD_INTR_SLAVE_ADDR_RX_PENDING (0x1 << 30) +/* bits[19-16] Reserved */ + +/* All bits below are cleared by writing 1 */ +#define I2CD_INTR_SLAVE_INACTIVE_TIMEOUT (0x1 << 15)#define I2CD_INTR_SDA_DL_TIMEOUT (0x1 << 14)#define I2CD_INTR_BUS_RECOVER_DONE (0x1 << 13)#define I2CD_INTR_SMBUS_ALERT (0x1 << 12)/* Bus [0-3] only */
@@ -59,11 +66,16 @@#define I2CD_INTR_SMBUS_DEV_ALERT_ADDR (0x1 << 10)/* Removed */ #define I2CD_INTR_SMBUS_DEF_ADDR (0x1 << 9)/* Removed */ #define I2CD_INTR_GCALL_ADDR (0x1 << 8)/* Removed */ -#define I2CD_INTR_SLAVE_MATCH (0x1 << 7)/* use RX_DONE */ +#define I2CD_INTR_SLAVE_ADDR_RX_MATCH (0x1 << 7)/* use RX_DONE */ #define I2CD_INTR_SCL_TIMEOUT (0x1 << 6)#define I2CD_INTR_ABNORMAL (0x1 << 5)#define I2CD_INTR_NORMAL_STOP (0x1 << 4)#define I2CD_INTR_ARBIT_LOSS (0x1 << 3) + +/* +* TODO: handle correctly I2CD_INTR_RX_DONE which needs to be cleared +* to allow next data to be received. +*/ #define I2CD_INTR_RX_DONE (0x1 << 2)#define I2CD_INTR_TX_NAK (0x1 << 1)#define I2CD_INTR_TX_ACK (0x1 << 0)
@@ -284,8 +296,10 @@ static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,break ;case I2CD_INTR_STS_REG : bus->intr_status &= ~(value &0x7FFF ); - bus->controller->intr_status &= ~(1 << bus->id); -qemu_irq_lower (bus->controller->irq); +if (!bus->intr_status) { + bus->controller->intr_status &= ~(1 << bus->id); +qemu_irq_lower (bus->controller->irq); + }break ;case I2CD_DEV_ADDR_REG : qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
Signature · 1 line
2.17.1
Quoted 1 line · C?dric Le Goater
On 09/13/2018 03:33 PM, Guenter Roeck wrote:
[ ... ]
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Review static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 8 lines
? /*
?? * The state machine needs some refinement. It is only used to track ?? * invalid STOP commands for the moment.(quoted diff) +0
-1@@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0;> +??? bus->intr_status &= I2CD_INTR_RX_DONE;it deserves a comment to understand which scenario we are trying to handle. ??
Quoted 8 lines
Quoted 3 lines
Ok. FWIW, I wonder if
intr_statusshould be touched here in the first place, but I neither have the hardware nor a datasheet, so I don't know if any bits are auto-cleared.I just pushed a patch on my branch with some more explanation :
That seems to suggest that none of the status bits auto-clears, and that the above code clearing intr_status should be removed entirely. Am I missing something ?
Thanks,
Guenter
Quoted 19 lines · Guenter Roeck
Quoted 1 line · C?dric Le Goater
On 09/13/2018 03:33 PM, Guenter Roeck wrote:
[ ... ]
Quoted 16 lines
Quoted 15 lines
Quoted 11 lines
Review static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 8 lines
? /*
?? * The state machine needs some refinement. It is only used to track ?? * invalid STOP commands for the moment.(quoted diff) +0
-1@@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0;> +??? bus->intr_status &= I2CD_INTR_RX_DONE;it deserves a comment to understand which scenario we are trying to handle. ??
Ok. FWIW, I wonder if
intr_statusshould be touched here in the first place, but I neither have the hardware nor a datasheet, so I don't know if any bits are auto-cleared.
Quoted 7 lines
Quoted 4 lines
I just pushed a patch on my branch with some more explanation :
That seems to suggest that none of the status bits auto-clears, and that the above code clearing
intr_statusshould be removed entirely. Am I missing something ?
You are right. I just pushed another version of the previous patch with this new hunk :
@@ -188,7 +200,6 @@ static void aspeed_i2c_bus_handle_cmd(As
{
bus->cmd &= ~0xFFFF;
bus->cmd |= value & 0xFFFF;- bus->intr_status = 0;
if (bus->cmd & I2CD_M_START_CMD) {
uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
The QEMU palmetto and witherspoon machines seem to behave fine. Can you give it a try ?
Thanks,
C.
Quoted 41 lines · C?dric Le Goater
Quoted 19 lines · Guenter Roeck
Quoted 1 line · C?dric Le Goater
On 09/13/2018 03:33 PM, Guenter Roeck wrote:
[ ... ]
Quoted 16 lines
Quoted 15 lines
Quoted 11 lines
Review static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) · line 191
Quoted 8 lines
? /*
?? * The state machine needs some refinement. It is only used to track ?? * invalid STOP commands for the moment.(quoted diff) +0
-1@@ -188,7 +215,7 @@ static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value) ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0;> +??? bus->intr_status &= I2CD_INTR_RX_DONE;it deserves a comment to understand which scenario we are trying to handle.
Ok. FWIW, I wonder if
intr_statusshould be touched here in the first place, but I neither have the hardware nor a datasheet, so I don't know if any bits are auto-cleared.Quoted 7 lines
Quoted 4 lines
I just pushed a patch on my branch with some more explanation :
That seems to suggest that none of the status bits auto-clears, and that the above code clearing
intr_statusshould be removed entirely. Am I missing something ?You are right. I just pushed another version of the previous patch with this new hunk :
(quoted diff) +0
-1@@ -188,7 +200,6 @@ static void aspeed_i2c_bus_handle_cmd(As { bus->cmd &= ~0xFFFF; bus->cmd |= value & 0xFFFF; - bus->intr_status = 0; if (bus->cmd & I2CD_M_START_CMD) { uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
Quoted 3 lines
The QEMU palmetto and witherspoon machines seem to behave fine. Can you give it a try ?
Works fine for me for all affected qemu platforms.
How do you want to proceed with the qemu patches ? I attached my patches for reference. Maybe you can add them to your tree if they are ok and submit the entire series together to the qemu mailing list ?
Thanks,
Guenter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-aspeed-i2c-Handle-receive-command-in-separate-functi.patch
Type: text/x-patch
Size: 2454 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180913/57727d84/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-aspeed-i2c-Fix-receive-done-interrupt-handling.patch
Type: text/x-patch
Size: 2348 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180913/57727d84/attachment-0001.bin>
Quoted 22 lines
Quoted 16 lines
Quoted 3 lines
That seems to suggest that none of the status bits auto-clears, and that the above code clearing
intr_statusshould be removed entirely. Am I missing something ?You are right. I just pushed another version of the previous patch with this new hunk :
(quoted diff) +0
-1@@ -188,7 +200,6 @@ static void aspeed_i2c_bus_handle_cmd(As ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0; ? ????? if (bus->cmd & I2CD_M_START_CMD) {?????????
uint8_tstate =aspeed_i2c_get_state(bus)&I2CD_MACTIVE?Quoted 3 lines
The QEMU palmetto and witherspoon machines seem to behave fine. Can you give it a try ?
Works fine for me for all affected qemu platforms.
Quoted 3 lines
How do you want to proceed with the qemu patches ? I attached my patches for reference. Maybe you can add them to your tree if they are ok and submit the entire series together to the qemu mailing list ?
yes. They are pushed in my aspeed-3.1 branch. I will send the series on the list.
Thanks,
C.
Quoted 22 lines · C?dric Le Goater
Quoted 22 lines
Quoted 16 lines
Quoted 3 lines
That seems to suggest that none of the status bits auto-clears, and that the above code clearing
intr_statusshould be removed entirely. Am I missing something ?You are right. I just pushed another version of the previous patch with this new hunk :
(quoted diff) +0
-1@@ -188,7 +200,6 @@ static void aspeed_i2c_bus_handle_cmd(As ? { ????? bus->cmd &= ~0xFFFF; ????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0; ? ????? if (bus->cmd & I2CD_M_START_CMD) { ????????? uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?Quoted 3 lines
The QEMU palmetto and witherspoon machines seem to behave fine. Can you give it a try ?
Works fine for me for all affected qemu platforms.
Quoted 7 lines
Quoted 3 lines
How do you want to proceed with the qemu patches ? I attached my patches for reference. Maybe you can add them to your tree if they are ok and submit the entire series together to the qemu mailing list ?
yes. They are pushed in my aspeed-3.1 branch. I will send the series on the list.
Excellent. Thanks a lot!
Guenter
Quoted 31 lines · Guenter Roeck
Quoted 30 lines · C?dric Le Goater
Quoted 29 lines
Quoted 18 lines
Quoted 4 lines
That seems to suggest that none of the status bits auto-clears, and that
the above code clearingintr_statusshould be removed entirely. Am I missing something ?You are right. I just pushed another version of the previous patch with this
new hunk :(quoted diff) +0
-1@@ -188,7 +200,6 @@ static void aspeed_i2c_bus_handle_cmd(As ?? { ?????? bus->cmd &= ~0xFFFF; ?????? bus->cmd |= value & 0xFFFF; -??? bus->intr_status = 0; ?? ????? if (bus->cmd & I2CD_M_START_CMD) {??????????
uint8_tstate =aspeed_i2c_get_state(bus)&I2CD_MACTIVE?Quoted 4 lines
The QEMU palmetto and witherspoon machines seem to behave fine. Can you give
it a try ?Works fine for me for all affected qemu platforms.
How do you want to proceed with the qemu patches ? I attached my patches for reference. Maybe you can add them to your tree if they are ok and submit
the entire series together to the qemu mailing list ?
Quoted 8 lines
Quoted 3 lines
yes. They are pushed in my aspeed-3.1 branch. I will send the series on the list.
Excellent. Thanks a lot!
Guenter
Awesome! Many thanks to Guenter, C?dric and Joel. I really appreciate it.
Jae
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ????? spin_lock(&bus->lock); ????? irq_received = readl(bus->base + ASPEED_I2C_INTR_STS_REG); +??? /* Ack all interrupt bits. */ +??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG); ????? irq_remaining = irq_received; ? #if IS_ENABLED(CONFIG_I2C_SLAVE)@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ????????????? "irq handled != irq. expected 0x%08x, but was 0x%08x\n", ????????????? irq_received, irq_handled); -??? /* Ack all interrupt bits. */ -??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG);Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0????? spin_unlock(&bus->lock); ????? return irq_remaining ? IRQ_NONE : IRQ_HANDLED; ? }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
? I2CD10 Interrupt Status Register
? bit 2 Receive Done Interrupt status
??????? S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 9 lines
Let me share my test result. Your code change works on 100KHz bus speed but doesn't work well on 1MHz bus speed. Investigated that interrupt handling is fast enough in 100KHz test but in 1MHz, most of data is corrupted because the bit is cleared at the beginning of interrupt handler so it allows receiving of the next data but the interrupt handler isn't fast enough to read the data buffer on time. I checked this problem on BMC-ME channel which ME sends lots of IPMB packets to BMC at 1MHz speed. You could simply check the data corruption problem on the BMC-ME channel.
OK.
Quoted 3 lines
My thought is, the current code is right for real Aspeed I2C hardware. It seems that QEMU 3.0 model for witherspoon-bmc doesn't simulate the actual Aspeed I2C hardware correctly.
That might be very well possible yes. it also misses support for the slave mode and the DMA registers.
Thanks for the info,
C.
Hi C?dric,
Quoted 74 lines · C?dric Le Goater
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ????? spin_lock(&bus->lock); ????? irq_received = readl(bus->base + ASPEED_I2C_INTR_STS_REG); +??? /* Ack all interrupt bits. */ +??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG); ????? irq_remaining = irq_received; ? #if IS_ENABLED(CONFIG_I2C_SLAVE)@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ????????????? "irq handled != irq. expected 0x%08x, but was 0x%08x\n", ????????????? irq_received, irq_handled); -??? /* Ack all interrupt bits. */ -??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG);Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0????? spin_unlock(&bus->lock); ????? return irq_remaining ? IRQ_NONE : IRQ_HANDLED; ? }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
? I2CD10 Interrupt Status Register
? bit 2 Receive Done Interrupt status
??????? S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 9 lines
Let me share my test result. Your code change works on 100KHz bus speed but doesn't work well on 1MHz bus speed. Investigated that interrupt handling is fast enough in 100KHz test but in 1MHz, most of data is corrupted because the bit is cleared at the beginning of interrupt handler so it allows receiving of the next data but the interrupt handler isn't fast enough to read the data buffer on time. I checked this problem on BMC-ME channel which ME sends lots of IPMB packets to BMC at 1MHz speed. You could simply check the data corruption problem on the BMC-ME channel.
Quoted 9 lines
OK.
Quoted 3 lines
My thought is, the current code is right for real Aspeed I2C hardware. It seems that QEMU 3.0 model for witherspoon-bmc doesn't simulate the actual Aspeed I2C hardware correctly.
That might be very well possible yes. it also misses support for the slave mode and the DMA registers.
Yes, it would be good if qemu's Aspeed I2C model supports slave mode. Since the current linux Aspeed I2C driver supports byte transfer mode only, so DMA transfer mode support in qemu could be considered later. Implementing pool mode and DMA mode for linux Aspeed I2C are in my backlog at this moment.
Thanks,
Jae
Quoted 4 lines
Thanks for the info,
C.
Hello !
Quoted 83 lines · Jae Hyun Yoo
Hi C?dric,
Quoted 80 lines · C?dric Le Goater
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ?????? spin_lock(&bus->lock); ?????? irq_received = readl(bus->base + ASPEED_I2C_INTR_STS_REG); +??? /* Ack all interrupt bits. */ +??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG); ?????? irq_remaining = irq_received; ?? #if IS_ENABLED(CONFIG_I2C_SLAVE)@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ?????????????? "irq handled != irq. expected 0x%08x, but was 0x%08x\n", ?????????????? irq_received, irq_handled); -??? /* Ack all interrupt bits. */ -??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG);Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0?????? spin_unlock(&bus->lock); ?????? return irq_remaining ? IRQ_NONE : IRQ_HANDLED; ?? }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
?? I2CD10 Interrupt Status Register
?? bit 2 Receive Done Interrupt status
???????? S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 9 lines
Let me share my test result. Your code change works on 100KHz bus speed but doesn't work well on 1MHz bus speed. Investigated that interrupt handling is fast enough in 100KHz test but in 1MHz, most of data is corrupted because the bit is cleared at the beginning of interrupt handler so it allows receiving of the next data but the interrupt handler isn't fast enough to read the data buffer on time. I checked this problem on BMC-ME channel which ME sends lots of IPMB packets to BMC at 1MHz speed. You could simply check the data corruption problem on the BMC-ME channel.
OK.
?Quoted 3 lines
My thought is, the current code is right for real Aspeed I2C hardware. It seems that QEMU 3.0 model for witherspoon-bmc doesn't simulate the actual Aspeed I2C hardware correctly.
Quoted 7 lines
Quoted 3 lines
That might be very well possible yes. it also misses support for the slave mode and the DMA registers.
Yes, it would be good if qemu's Aspeed I2C model supports slave mode. Since the current linux Aspeed I2C driver supports byte transfer mode only, so DMA transfer mode support in qemu could be considered later.
The Aspeed SDK already does, so yes, we will need to consider it.
Quoted 2 lines
Implementing pool mode and DMA mode for linux Aspeed I2C are in my backlog at this moment.
Is there a QEMU model for an I2C/IPMB device ?
There is already a large IPMI framework in QEMU, that would be interesting. to extend.
Thanks,
C.
Quoted 90 lines · C?dric Le Goater
Hello !
Quoted 87 lines · Jae Hyun Yoo
Hi C?dric,
Quoted 83 lines · C?dric Le Goater
Quoted 63 lines · Jae Hyun Yoo
Quoted 43 lines · Guenter Roeck
Quoted 28 lines · Jae Hyun Yoo
Quoted 27 lines · Guenter Roeck
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ?????? spin_lock(&bus->lock); ?????? irq_received = readl(bus->base + ASPEED_I2C_INTR_STS_REG); +??? /* Ack all interrupt bits. */ +??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG); ?????? irq_remaining = irq_received; ?? #if IS_ENABLED(CONFIG_I2C_SLAVE)@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) ?????????????? "irq handled != irq. expected 0x%08x, but was 0x%08x\n", ?????????????? irq_received, irq_handled); -??? /* Ack all interrupt bits. */ -??? writel(irq_received, bus->base + ASPEED_I2C_INTR_STS_REG);Quoted 8 lines
drivers/i2c/busses/i2c-aspeed.c static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) · line 591
Quoted 4 lines
drivers/i2c/busses/i2c-aspeed.c +0
-0?????? spin_unlock(&bus->lock); ?????? return irq_remaining ? IRQ_NONE : IRQ_HANDLED; ?? }My intention of putting the code at the end of interrupt handler was, to reduce possibility of combined irq calls which is explained in this patch. But YES, I agree with you. It could make a potential race
Hmm, yes, but that doesn't explain why it would make sense to acknowledge the interrupt late. The interrupt ack only means "I am going to handle these interrupts". If additional interrupts arrive while the interrupt handler is active, those will have to be acknowledged separately.
Quoted 6 lines
Sure, there is a risk that an interrupt arrives while the handler is running, and that it is handled but not acknowledged. That can happen with pretty much all interrupt handlers, and there are mitigations to limit the impact (for example, read the interrupt status register in a loop until no more interrupts are pending). But acknowledging an interrupt that was possibly not handled is always bad idea.
Well, that's generally right but not always. Sometimes that depends on hardware and Aspeed I2C is the case.
This is a description from Aspeed AST2500 datasheet:
?? I2CD10 Interrupt Status Register
?? bit 2 Receive Done Interrupt status
???????? S/W needs to clear this status bit to allow next data receiving.It means, driver should hold this bit to prevent transition of hardware state machine until the driver handles received data, so the bit should be cleared at the end of interrupt handler.
Quoted 9 lines
Let me share my test result. Your code change works on 100KHz bus speed but doesn't work well on 1MHz bus speed. Investigated that interrupt handling is fast enough in 100KHz test but in 1MHz, most of data is corrupted because the bit is cleared at the beginning of interrupt handler so it allows receiving of the next data but the interrupt handler isn't fast enough to read the data buffer on time. I checked this problem on BMC-ME channel which ME sends lots of IPMB packets to BMC at 1MHz speed. You could simply check the data corruption problem on the BMC-ME channel.
OK.
Quoted 3 lines
My thought is, the current code is right for real Aspeed I2C hardware. It seems that QEMU 3.0 model for witherspoon-bmc doesn't simulate the actual Aspeed I2C hardware correctly.
That might be very well possible yes. it also misses support for the slave mode and the DMA registers.
Quoted 6 lines
Quoted 3 lines
Yes, it would be good if qemu's Aspeed I2C model supports slave mode. Since the current linux Aspeed I2C driver supports byte transfer mode only, so DMA transfer mode support in qemu could be considered later.
The Aspeed SDK already does, so yes, we will need to consider it.
Yes, Aspeed SDK does but the driver in upstream doesn't at this time. So we will need to consider it because byte mode makes too many interrupt calls which is not good for performance.
Quoted 5 lines
Quoted 2 lines
Implementing pool mode and DMA mode for linux Aspeed I2C are in my backlog at this moment.
Is there a QEMU model for an I2C/IPMB device ?
Not sure because I'm not familiar with QEMU. Need to check.
Thanks,
Jae
Quoted 8 lines
There is already a large IPMI framework in QEMU, that would be interesting. to extend.
Thanks,
C.
Cédric Le Goater
Acked-byQuoted 26 lines · Guenter Roeck
Quoted 25 lines · Joel Stanley
Quoted 15 lines · Jae Hyun Yoo
Quoted 6 lines · Guenter Roeck
Quoted 4 lines · Jae Hyun Yoo
I checked this patch again but it doesn't have any change that could affect to the probing flow. I'll debug the issue on qemu 3.0 environment and will share if I find something.
Quoted 6 lines
The problem may be that qemu and the new code disagree how interrupts should be generated and handled, and the new code does not handle the interrupts it receives from the simulated hardware. This will result in i2c device probe failure, which in turn can cause all kinds of problems.
Quoted 2 lines
Yes, that makes sense. Looks like it should be reverted until the issue is fixed. Will submit a patch to revert it.
Let's not rush. The qemu model was written in order to allow us to test the kernel code, and was validated by the kernel driver we have. We've had situations in the past (with the i2c driver in fact) where a change in the driver required an update of the model to be more accurate.
Quoted 6 lines
Quoted 3 lines
I suggest we wait until Cedric has a chance to look at the issue before reverting the patch.
Looking into the patch, clearing the interrupt status at the end of an interrupt handler is always suspicious and tends to result in race
yes. That happened in the past with the I2C aspeed driver. I can not find the thread anymore but we had to move up the ack of the interrupts.
QEMU tends to be much faster to fire interrupts than real HW.
Quoted 4 lines
conditions (because additional interrupts may have arrived while handling the existing interrupts, or because interrupt handling itself may trigger another interrupt). With that in mind, the following patch fixes the problem for me.
Thanks,
C.
Quoted 27 lines
Guenter
---
drivers/i2c/busses/i2c-aspeed.c +2
-2@@ -552,6 +552,8 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)spin_lock (&bus->lock); irq_received =readl (bus->base +ASPEED_I2C_INTR_STS_REG ); +/* Ack all interrupt bits. */ +writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG ); irq_remaining = irq_received;#if IS_ENABLED (CONFIG_I2C_SLAVE )@@ -584,8 +586,6 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)"irq handled != irq. expected 0x%08x, but was 0x%08x\n" , irq_received, irq_handled); -/* Ack all interrupt bits. */ -writel (irq_received, bus->base +ASPEED_I2C_INTR_STS_REG );spin_unlock (&bus->lock);return irq_remaining ?IRQ_NONE :IRQ_HANDLED ; }