Skip to contentSkip to conversation

i2c: designware: Introduce cooldown timer to AMDPSP driver

PATCH Superseded Jan Dabros 2 participants 4 messages Jul 25, 2022 – Aug 12, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Dabros

author
#0 lore raw

PATCH 1/1 i2c: designware: Introduce cooldown timer to AMDPSP driver

In order to optimize performance, limit amount of back and forth transactions between x86 and PSP. This is done by introduction of cooldown period - that is window in which x86 isn't releasing the bus immediately after each I2C transaction.

In order to protect PSP from being starved while waiting for arbitration, after a programmed time bus is automatically released by a deferred function.

---
 drivers/i2c/busses/i2c-designware-amdpsp.c | 68 +++++++++++++++++-----
 1 file changed, 53 insertions(+), 15 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +53 -15
index b624356c945f..2e1bb5ae72c3 100644
@@ -6,6 +6,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/psp-sev.h>
 #include <linux/types.h>
+#include <linux/workqueue.h>
 
 #include <asm/msr.h>
 
@@ -15,6 +16,8 @@
 #define PSP_MBOX_OFFSET		0x10570
 #define PSP_CMD_TIMEOUT_US	(500 * USEC_PER_MSEC)
 
+#define PSP_I2C_COOLDOWN_TIME_MS 100
+
 #define PSP_I2C_REQ_BUS_CMD		0x64
 #define PSP_I2C_REQ_RETRY_CNT		400
 #define PSP_I2C_REQ_RETRY_DELAY_US	(25 * USEC_PER_MSEC)
@@ -240,6 +243,42 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
 	return ret;
 }
 
+static void release_bus_now(void)
+{
+	int status;
+
+	if (!psp_i2c_sem_acquired)
+		return;
+
+	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
+	if (status)
+		return;
+
+	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
+		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
+
+	psp_i2c_sem_acquired = 0;
+}
+
+static void psp_release_i2c_bus_deferred(struct work_struct *work)
+{
+
+	mutex_lock(&psp_i2c_access_mutex);
+
+	/*
+	 * If there is any pending transaction, cannot release the bus here.
+	 * psp_release_i2c_bus will take care of this later.
+	 */
+	if (psp_i2c_access_count)
+		goto cleanup;
+
+	release_bus_now();
+
+cleanup:
+	mutex_unlock(&psp_i2c_access_mutex);
+}
+static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
+
 static int psp_acquire_i2c_bus(void)
 {
 	int status;
@@ -250,21 +289,23 @@ static int psp_acquire_i2c_bus(void)
 	if (psp_i2c_mbox_fail)
 		goto cleanup;
 
+	psp_i2c_access_count++;
+
 	/*
-	 * Simply increment usage counter and return if PSP semaphore was
-	 * already taken by kernel.
+	 * No need to request bus arbitration once we are inside cooldown
+	 * period.
 	 */
-	if (psp_i2c_access_count) {
-		psp_i2c_access_count++;
+	if (psp_i2c_sem_acquired)
 		goto cleanup;
-	}
 
 	status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
 	if (status)
 		goto cleanup;
 
 	psp_i2c_sem_acquired = jiffies;
-	psp_i2c_access_count++;
+
+	schedule_delayed_work(&release_queue,
+			      msecs_to_jiffies(PSP_I2C_COOLDOWN_TIME_MS));
 
 	/*
 	 * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
@@ -279,8 +320,6 @@ static int psp_acquire_i2c_bus(void)
 
 static void psp_release_i2c_bus(void)
 {
-	int status;
-
 	mutex_lock(&psp_i2c_access_mutex);
 
 	/* Return early if mailbox was malfunctional */
@@ -295,13 +334,12 @@ static void psp_release_i2c_bus(void)
 	if (psp_i2c_access_count)
 		goto cleanup;
 
-	/* Send a release command to PSP */
-	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
-	if (status)
-		goto cleanup;
-
-	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
-		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
+	/*
+	 * Send a release command to PSP if the cooldown timeout elapsed but x86 still
+	 * owns the ctrlr.
+	 */
+	if (!delayed_work_pending(&release_queue))
+		release_bus_now();
 
 cleanup:
 	mutex_unlock(&psp_i2c_access_mutex);
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
#1 lore raw

Hi Jarko, Andriy,

Could you please take a look?

Best Regards,
Jan

pon., 25 lip 2022 o 10:03 Jan Dabros <[email protected]> napisał(a):

Quoted 141 lines

In order to optimize performance, limit amount of back and forth transactions between x86 and PSP. This is done by introduction of cooldown period - that is window in which x86 isn't releasing the bus immediately after each I2C transaction.

In order to protect PSP from being starved while waiting for arbitration, after a programmed time bus is automatically released by a deferred function.

---
 drivers/i2c/busses/i2c-designware-amdpsp.c | 68 +++++++++++++++++-----
 1 file changed, 53 insertions(+), 15 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +53 -16
index b624356c945f..2e1bb5ae72c3 100644
@@ -6,6 +6,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/psp-sev.h>
 #include <linux/types.h>
+#include <linux/workqueue.h>
 
 #include <asm/msr.h>
 
@@ -15,6 +16,8 @@
 #define PSP_MBOX_OFFSET                0x10570
 #define PSP_CMD_TIMEOUT_US     (500 * USEC_PER_MSEC)
 
+#define PSP_I2C_COOLDOWN_TIME_MS 100
+
 #define PSP_I2C_REQ_BUS_CMD            0x64
 #define PSP_I2C_REQ_RETRY_CNT          400
 #define PSP_I2C_REQ_RETRY_DELAY_US     (25 * USEC_PER_MSEC)
@@ -240,6 +243,42 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
        return ret;
 }
 
+static void release_bus_now(void)
+{
+       int status;
+
+       if (!psp_i2c_sem_acquired)
+               return;
+
+       status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
+       if (status)
+               return;
+
+       dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
+               jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
+
+       psp_i2c_sem_acquired = 0;
+}
+
+static void psp_release_i2c_bus_deferred(struct work_struct *work)
+{
+
+       mutex_lock(&psp_i2c_access_mutex);
+
+       /*
+        * If there is any pending transaction, cannot release the bus here.
+        * psp_release_i2c_bus will take care of this later.
+        */
+       if (psp_i2c_access_count)
+               goto cleanup;
+
+       release_bus_now();
+
+cleanup:
+       mutex_unlock(&psp_i2c_access_mutex);
+}
+static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
+
 static int psp_acquire_i2c_bus(void)
 {
        int status;
@@ -250,21 +289,23 @@ static int psp_acquire_i2c_bus(void)
        if (psp_i2c_mbox_fail)
                goto cleanup;
 
+       psp_i2c_access_count++;
+
        /*
-        * Simply increment usage counter and return if PSP semaphore was
-        * already taken by kernel.
+        * No need to request bus arbitration once we are inside cooldown
+        * period.
         */
-       if (psp_i2c_access_count) {
-               psp_i2c_access_count++;
+       if (psp_i2c_sem_acquired)
                goto cleanup;
-       }
 
        status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
        if (status)
                goto cleanup;
 
        psp_i2c_sem_acquired = jiffies;
-       psp_i2c_access_count++;
+
+       schedule_delayed_work(&release_queue,
+                             msecs_to_jiffies(PSP_I2C_COOLDOWN_TIME_MS));
 
        /*
         * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
@@ -279,8 +320,6 @@ static int psp_acquire_i2c_bus(void)
 
 static void psp_release_i2c_bus(void)
 {
-       int status;
-
        mutex_lock(&psp_i2c_access_mutex);
 
        /* Return early if mailbox was malfunctional */
@@ -295,13 +334,12 @@ static void psp_release_i2c_bus(void)
        if (psp_i2c_access_count)
                goto cleanup;
 
-       /* Send a release command to PSP */
-       status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
-       if (status)
-               goto cleanup;
-
-       dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
-               jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
+       /*
+        * Send a release command to PSP if the cooldown timeout elapsed but x86 still
+        * owns the ctrlr.
+        */
+       if (!delayed_work_pending(&release_queue))
+               release_bus_now();
 
 cleanup:
        mutex_unlock(&psp_i2c_access_mutex);
--

2.37.1.359.gd136c6c3e2-goog

#2 lore raw
In reply to Jan Dabros

Hi

Sorry the delay, this slipped through my eyes during vacation. Couple minor comments below.

Quoted 27 lines · Jan Dabros

In order to optimize performance, limit amount of back and forth transactions between x86 and PSP. This is done by introduction of cooldown period - that is window in which x86 isn't releasing the bus immediately after each I2C transaction.

In order to protect PSP from being starved while waiting for arbitration, after a programmed time bus is automatically released by a deferred function.

---
  drivers/i2c/busses/i2c-designware-amdpsp.c | 68 +++++++++++++++++-----
  1 file changed, 53 insertions(+), 15 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +1 -0
index b624356c945f..2e1bb5ae72c3 100644
@@ -6,6 +6,7 @@
  #include <linux/io-64-nonatomic-lo-hi.h>
  #include <linux/psp-sev.h>
  #include <linux/types.h>
+#include <linux/workqueue.h>
 
  #include <asm/msr.h>
 

drivers/i2c/busses/i2c-designware-amdpsp.c line 20

Quoted 5 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +2 -0
@@ -15,6 +16,8 @@
  #define PSP_MBOX_OFFSET		0x10570
  #define PSP_CMD_TIMEOUT_US	(500 * USEC_PER_MSEC)
 
+#define PSP_I2C_COOLDOWN_TIME_MS 100
+

"cooldown" distract me thinking thermal management. Would semaphore reservation time/timer fit better?

drivers/i2c/busses/i2c-designware-amdpsp.c static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type) · line 281

Quoted 4 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +4 -0
+static void release_bus_now(void)
+static void psp_release_i2c_bus_deferred(struct work_struct *work)
+static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
+

I'd use the same namespace here. Perhaps _now can be dropped from the name since the release_bus and release_bus_deferred are near to each other and _deferred variant implies it's called after timeout.

drivers/i2c/busses/i2c-designware-amdpsp.c static void psp_release_i2c_bus(void) · line 340

Quoted 4 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +4 -0
+	/*
+	 * Send a release command to PSP if the cooldown timeout elapsed but x86 still
+	 * owns the ctrlr.
+	 */

Replace "ctrlr" -> "control" here since then it doesn't lead to think is't some technical object like register etc.

#3 lore raw

wt., 9 sie 2022 o 14:05 Jarkko Nikula <[email protected]> napisał(a):

Quoted 37 lines

Hi

Sorry the delay, this slipped through my eyes during vacation. Couple minor comments below.

Quoted 30 lines · Jan Dabros

In order to optimize performance, limit amount of back and forth transactions between x86 and PSP. This is done by introduction of cooldown period - that is window in which x86 isn't releasing the bus immediately after each I2C transaction.

In order to protect PSP from being starved while waiting for arbitration, after a programmed time bus is automatically released by a deferred function.

---
  drivers/i2c/busses/i2c-designware-amdpsp.c | 68 +++++++++++++++++-----
  1 file changed, 53 insertions(+), 15 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +1 -0
index b624356c945f..2e1bb5ae72c3 100644
@@ -6,6 +6,7 @@
  #include <linux/io-64-nonatomic-lo-hi.h>
  #include <linux/psp-sev.h>
  #include <linux/types.h>
+#include <linux/workqueue.h>
 
  #include <asm/msr.h>
 
@@ -15,6 +16,8 @@
  #define PSP_MBOX_OFFSET             0x10570
  #define PSP_CMD_TIMEOUT_US  (500 * USEC_PER_MSEC)
 
Quoted 5 lines

drivers/i2c/busses/i2c-designware-amdpsp.c line 20

Quoted 2 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +2 -0
+#define PSP_I2C_COOLDOWN_TIME_MS 100
+

"cooldown" distract me thinking thermal management. Would semaphore reservation time/timer fit better?

Yes, it makes sense. I will change this here and in the commit message to "semaphore reservation timer".

Quoted 9 lines

drivers/i2c/busses/i2c-designware-amdpsp.c static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type) · line 281

Quoted 5 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +4 -0
+static void release_bus_now(void)
+static void psp_release_i2c_bus_deferred(struct work_struct *work)
+static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
+

I'd use the same namespace here. Perhaps _now can be dropped from the name since the release_bus and release_bus_deferred are near to each other and _deferred variant implies it's called after timeout.

Right, release_bus_now -> release_bus.

Quoted 8 lines

drivers/i2c/busses/i2c-designware-amdpsp.c static void psp_release_i2c_bus(void) · line 340

Quoted 5 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +4 -0
+     /*
+      * Send a release command to PSP if the cooldown timeout elapsed but x86 still
+      * owns the ctrlr.
+      */

Replace "ctrlr" -> "control" here since then it doesn't lead to think is't some technical object like register etc.

This is about "controller" not "control", but I think your comment is still applicable.

Best Regards,
Jan