Skip to contentSkip to conversation

i2c: piix4: Avoid race conditions with IMC

PATCH Superseded Ricardo Ribalda Delgado 3 participants 8 messages Jan 11, 2017 – Jan 12, 2017

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 i2c: piix4: Avoid race conditions with IMC

On AMD's SB800 and upwards, the SMBus is shared with the Integrated Micro Controller (IMC).

The platform provides a hardware semaphore to avoid race conditions among them. (Check page 288 of the SB800-Series Southbridges Register Reference Guide http://support.amd.com/TechDocs/45482.pdf)

Without this patch, many access to the SMBus end with an invalid transaction or even with the bus stalled.

Credit-to: Alexandre Desnoyers <[email protected]>

---

v2: Suggestions by Andy Shevchenko <[email protected]>:
-Rename timeout to retries
-Use do {} while(--retries) pattern
-Group new variables

 drivers/i2c/busses/i2c-piix4.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/i2c/busses/i2c-piix4.c +22 -0
index f0563f7ce01b..81d06be0a72d 100644
@@ -585,10 +585,29 @@ static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
 		 u8 command, int size, union i2c_smbus_data *data)
 {
 	struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
+	unsigned short piix4_smba = adapdata->smba;
+	int retries = MAX_TIMEOUT;
+	int smbslvcnt;
 	u8 smba_en_lo;
 	u8 port;
 	int retval;
 
+	/* Request the SMBUS semaphore, avoid conflicts with the IMC */
+	smbslvcnt  = inb_p(SMBSLVCNT);
+	do {
+		outb_p(smbslvcnt | 0x10, SMBSLVCNT);
+
+		/* Check the semaphore status */
+		smbslvcnt  = inb_p(SMBSLVCNT);
+		if (smbslvcnt & 0x10)
+			break;
+
+		usleep_range(1000, 2000);
+	} while (--retries);
+	/* SMBus is still owned by the IMC, we give up */
+	if (!retries)
+		return -EBUSY;
+
 	mutex_lock(&piix4_mutex_sb800);
 
 	outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
@@ -606,6 +625,9 @@ static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
 
 	mutex_unlock(&piix4_mutex_sb800);
 
+	/* Release the semaphore */
+	outb_p(smbslvcnt | 0x20, SMBSLVCNT);
+
 	return retval;
 }
 
Signature · 1 line
2.11.0
#1 lore raw
Quoted 10 lines · Ricardo Ribalda Delgado

On AMD's SB800 and upwards, the SMBus is shared with the Integrated Micro Controller (IMC).

The platform provides a hardware semaphore to avoid race conditions among them. (Check page 288 of the SB800-Series Southbridges Register Reference Guide http://support.amd.com/TechDocs/45482.pdf)

Without this patch, many access to the SMBus end with an invalid transaction or even with the bus stalled.

After addressing below comment
FWIW: Reviewed-by: Andy Shevchenko <[email protected]>:

Quoted 1 line

Credit-to: Alexandre Desnoyers <[email protected]>

It would be nice to put in plain test what you tell in the discussion here instead of non-standard (see submitting-patches.rst) tag.

Quoted 59 lines

---

v2: Suggestions by Andy Shevchenko <[email protected]>:
-Rename timeout to retries
-Use do {} while(--retries) pattern
-Group new variables

 drivers/i2c/busses/i2c-piix4.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/i2c/busses/i2c-piix4.c +22 -1
index f0563f7ce01b..81d06be0a72d 100644
@@ -585,10 +585,29 @@ static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
                 u8 command, int size, union i2c_smbus_data *data)
 {
        struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
+       unsigned short piix4_smba = adapdata->smba;
+       int retries = MAX_TIMEOUT;
+       int smbslvcnt;
        u8 smba_en_lo;
        u8 port;
        int retval;
 
+       /* Request the SMBUS semaphore, avoid conflicts with the IMC */
+       smbslvcnt  = inb_p(SMBSLVCNT);
+       do {
+               outb_p(smbslvcnt | 0x10, SMBSLVCNT);
+
+               /* Check the semaphore status */
+               smbslvcnt  = inb_p(SMBSLVCNT);
+               if (smbslvcnt & 0x10)
+                       break;
+
+               usleep_range(1000, 2000);
+       } while (--retries);
+       /* SMBus is still owned by the IMC, we give up */
+       if (!retries)
+               return -EBUSY;
+
        mutex_lock(&piix4_mutex_sb800);
 
        outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
@@ -606,6 +625,9 @@ static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
 
        mutex_unlock(&piix4_mutex_sb800);
 
+       /* Release the semaphore */
+       outb_p(smbslvcnt | 0x20, SMBSLVCNT);
+
        return retval;
 }
 
--

2.11.0

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 3 lines

After addressing below comment
FWIW: Reviewed-by: Andy Shevchenko <[email protected]>:

@andy: Please don't prefix tags. Patchwork doesn't seem to find them like this :/

Quoted 4 lines
Quoted 1 line

Credit-to: Alexandre Desnoyers <[email protected]>

It would be nice to put in plain test what you tell in the discussion here instead of non-standard (see submitting-patches.rst) tag.

@ricardo: I agree. Is this Reported-by maybe?

Reviewed-by
#3 lore raw
Quoted 3 lines · Wolfram Sang
Quoted 3 lines

After addressing below comment
FWIW:

Quoted 1 line

@andy: Please don't prefix tags. Patchwork doesn't seem to find them like this :/

Oops, sorry, didn't notice that. I usually use a new line for them.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Credit-to: Alexandre Desnoyers <[email protected]>

It would be nice to put in plain test what you tell in the discussion here instead of non-standard (see submitting-patches.rst) tag.

@ricardo: I agree. Is this Reported-by maybe?

Yeah, since it was mentioned IIRC that guy is hardware engineer I don't think Suggested-by would work here better.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 3 lines
Quoted 1 line

@andy: Please don't prefix tags. Patchwork doesn't seem to find them like this :/

Oops, sorry, didn't notice that. I usually use a new line for them.

Thanks for fixing it :)

Quoted 4 lines
Quoted 1 line

@ricardo: I agree. Is this Reported-by maybe?

Yeah, since it was mentioned IIRC that guy is hardware engineer I don't think Suggested-by would work here better.

So, I changed that and applied to for-current so it can be in my next pull-request.

Thanks!

#5 lore raw

Hi Wolfram and Andy,

I have already sent a v3 with the latest suggested changes.

Quoted 7 lines · Wolfram Sang
Quoted 3 lines
Quoted 1 line

@andy: Please don't prefix tags. Patchwork doesn't seem to find them like this :/

Oops, sorry, didn't notice that. I usually use a new line for them.

Thanks for fixing it :)

Quoted 1 line
Quoted 1 line

@ricardo: I agree. Is this Reported-by maybe?

I still believe that we miss a Credit-to: or Co-author: tag ;). Most times we do not give enough credit to the hardware engineers. I would have taken me a long time through dark AMD doc to figure this out. But for the meantime Reported-by will do the job.

Thanks all of you for your fast review!

Signature · 1 line
Ricardo Ribalda
#6 lore raw
Quoted 2 lines

I still believe that we miss a Credit-to: or Co-author: tag ;). Most

Co-author is just a second signed-off (in my book at least).

Quoted 1 line

times we do not give enough credit to the hardware engineers.

True. Yet again, I'd favor simply multiple SoB here.

Quoted 1 line

Thanks all of you for your fast review!

You are welcome!

#7 lore raw
In reply to Ricardo Ribalda Delgado
Quoted 3 lines · Ricardo Ribalda Delgado
Quoted 1 line
Quoted 1 line
Quoted 1 line

@ricardo: I agree. Is this Reported-by maybe?

I still believe that we miss a Credit-to: or Co-author: tag ;)

We have more than enough of tags.

Signature · 2 lines
With Best Regards,
Andy Shevchenko