Skip to contentSkip to conversation

i2c: i2c-s3c2410: Remove recently introduced performance overheads

PATCH In review Mark Brown 2 participants 3 messages Nov 20, 2012 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 i2c: i2c-s3c2410: Remove recently introduced performance overheads

The changes in "i2c-s3c2410: use exponential back off while polling for bus idle" remove the initial busy wait for I2C transfers to complete and replace it with usleep_range() calls which will schedule.

Since for older SoCs I2C transfers would usually complete within an extremely small number of CPU cycles there is a win from not having to schedule. This happens because on the older SoCs the cores run at a smaller multiple of the speeds that the I2C bus is operating at; on more modern SoCs the busy wait is less likely to be effective.

Fix the issue by restoring the busy wait, reducing the number of spins from 20 to 3 which covers the overwhelming majority of I2C transfers on the SoCs where the busy wait is effective.

---
 drivers/i2c/busses/i2c-s3c2410.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-s3c2410.c +16 -5
index 31f802b..9050821 100644
@@ -530,6 +530,7 @@ static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
 	unsigned long iicstat;
 	ktime_t start, now;
 	unsigned long delay;
+	int spins;
 
 	/* ensure the stop has been through the bus */
 
@@ -542,12 +543,22 @@ static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
 	 * end of a transaction.  However, really slow i2c devices can stretch
 	 * the clock, delaying STOP generation.
 	 *
-	 * As a compromise between idle detection latency for the normal, fast
-	 * case, and system load in the slow device case, use an exponential
-	 * back off in the polling loop, up to 1/10th of the total timeout,
-	 * then continue to poll at a constant rate up to the timeout.
+	 * On slower SoCs this typically happens within a very small number of
+	 * instructions so busy wait briefly to avoid scheduling overhead.
+	 */
+	spins = 3;
+	do {
+		cpu_relax();
+		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
+	} while ((iicstat & S3C2410_IICSTAT_START) && --spins);
+
+	/*
+	 * If we do get an appreciable delay as a compromise between idle
+	 * detection latency for the normal, fast case, and system load in the
+	 * slow device case, use an exponential back off in the polling loop,
+	 * up to 1/10th of the total timeout, then continue to poll at a
+	 * constant rate up to the timeout.
 	 */
-	iicstat = readl(i2c->regs + S3C2410_IICSTAT);
 	delay = 1;
 	while ((iicstat & S3C2410_IICSTAT_START) &&
 	       ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to a message not in the archive
Quoted 37 lines · Mark Brown

The changes in "i2c-s3c2410: use exponential back off while polling for bus idle" remove the initial busy wait for I2C transfers to complete and replace it with usleep_range() calls which will schedule.

Since for older SoCs I2C transfers would usually complete within an extremely small number of CPU cycles there is a win from not having to schedule. This happens because on the older SoCs the cores run at a smaller multiple of the speeds that the I2C bus is operating at; on more modern SoCs the busy wait is less likely to be effective.

Fix the issue by restoring the busy wait, reducing the number of spins from 20 to 3 which covers the overwhelming majority of I2C transfers on the SoCs where the busy wait is effective.

---
 drivers/i2c/busses/i2c-s3c2410.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-s3c2410.c +1 -2
index 31f802b..9050821 100644
@@ -530,6 +530,7 @@ static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
        unsigned long iicstat;
        ktime_t start, now;
        unsigned long delay;
+       int spins;
 
        /* ensure the stop has been through the bus */
 
@@ -542,12 +543,22 @@ static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
         * end of a transaction.  However, really slow i2c devices can stretch
         * the clock, delaying STOP generation.
         *
-        * As a compromise between idle detection latency for the normal, fast
-        * case, and system load in the slow device case, use an exponential

drivers/i2c/busses/i2c-s3c2410.c static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) · line 553

Quoted 10 lines
drivers/i2c/busses/i2c-s3c2410.c +8 -2
-        * back off in the polling loop, up to 1/10th of the total timeout,
-        * then continue to poll at a constant rate up to the timeout.
+        * On slower SoCs this typically happens within a very small number of
+        * instructions so busy wait briefly to avoid scheduling overhead.
+        */
+       spins = 3;
+       do {
+               cpu_relax();
+               iicstat = readl(i2c->regs + S3C2410_IICSTAT);
+       } while ((iicstat & S3C2410_IICSTAT_START) && --spins);

Can you avoid one cpu_relax() by reading IICSTAT first, and switch to while { } instead of do { } while ()?

Quoted 14 lines
drivers/i2c/busses/i2c-s3c2410.c +7 -2
+
+       /*
+        * If we do get an appreciable delay as a compromise between idle
+        * detection latency for the normal, fast case, and system load in the
+        * slow device case, use an exponential back off in the polling loop,
+        * up to 1/10th of the total timeout, then continue to poll at a
+        * constant rate up to the timeout.
         */
-       iicstat = readl(i2c->regs + S3C2410_IICSTAT);
        delay = 1;
        while ((iicstat & S3C2410_IICSTAT_START) &&
               ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
--

1.7.10.4

author
#4 lore raw
In reply to a message not in the archive
Quoted 10 lines · Daniel Kurtz

On Tue, Nov 20, 2012 at 1:57 PM, Mark Brown

drivers/i2c/busses/i2c-s3c2410.c static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) · line 553

Quoted 5 lines
drivers/i2c/busses/i2c-s3c2410.c +5 -0
+       spins = 3;
+       do {
+               cpu_relax();
+               iicstat = readl(i2c->regs + S3C2410_IICSTAT);
+       } while ((iicstat & S3C2410_IICSTAT_START) && --spins);

Can you avoid one cpu_relax() by reading IICSTAT first, and switch to while { } instead of do { } while ()?

Of course. The CPUs that care about this are single core so the cpu_relax() has no effect on them anyway.