Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1556311

[PATCH v2] i2c: piix4: Avoid race conditions with IMC

From Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
Newsgroups linux.kernel
Subject [PATCH v2] i2c: piix4: Avoid race conditions with IMC
Date 2017-01-11 10:20 +0100
Message-ID <sYnex-565-17@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


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 <alex@qtec.com>
Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---

v2: Suggestions by Andy Shevchenko <andy.shevchenko@gmail.com>:
 -Rename timeout to retries
 -Use do {} while(--retries) pattern
 -Group new variables


 drivers/i2c/busses/i2c-piix4.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/i2c/busses/i2c-piix4.c b/drivers/i2c/busses/i2c-piix4.c
index f0563f7ce01b..81d06be0a72d 100644
--- a/drivers/i2c/busses/i2c-piix4.c
+++ b/drivers/i2c/busses/i2c-piix4.c
@@ -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

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH v2] i2c: piix4: Avoid race conditions with IMC Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com> - 2017-01-11 10:20 +0100
  Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Andy Shevchenko <andy.shevchenko@gmail.com> - 2017-01-11 17:40 +0100
    Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Wolfram Sang <wsa@the-dreams.de> - 2017-01-12 20:40 +0100
      Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Andy Shevchenko <andy.shevchenko@gmail.com> - 2017-01-12 20:40 +0100
        Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Wolfram Sang <wsa@the-dreams.de> - 2017-01-12 21:00 +0100
          Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com> - 2017-01-12 21:10 +0100
            Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Wolfram Sang <wsa@the-dreams.de> - 2017-01-12 21:30 +0100
            Re: [PATCH v2] i2c: piix4: Avoid race conditions with IMC Andy Shevchenko <andy.shevchenko@gmail.com> - 2017-01-12 21:50 +0100

csiph-web