Skip to contentSkip to conversation

i2c-designware: Add support for AMD PSP semaphore

RFC In review Jan Dabros 6 participants 16 messages Dec 22, 2021 – Dec 27, 2021

Subsystems (4)
lore
Share & tools
lore

Jan Dabros

author
#0 lore raw

cover i2c-designware: Add support for AMD PSP semaphore

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

Jan Dabros (2):
i2c: designware: Add missing locks
i2c: designware: Add AMD PSP I2C bus support

 MAINTAINERS                                 |   1 +
 drivers/acpi/acpi_apd.c                     |   1 +
 drivers/i2c/busses/Kconfig                  |  20 ++
 drivers/i2c/busses/Makefile                 |   1 +
 drivers/i2c/busses/i2c-designware-amdpsp.c  | 359 ++++++++++++++++++++
 drivers/i2c/busses/i2c-designware-common.c  |  12 +
 drivers/i2c/busses/i2c-designware-core.h    |   9 +-
 drivers/i2c/busses/i2c-designware-master.c  |   6 +
 drivers/i2c/busses/i2c-designware-platdrv.c |   3 +
 9 files changed, 411 insertions(+), 1 deletion(-)
 create mode 100644 drivers/i2c/busses/i2c-designware-amdpsp.c
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
author
#1 lore raw

RFC 1/2 i2c: designware: Add missing locks

All accesses to controller's registers should be protected on probe, disable and xfer paths. This is needed for i2c bus controllers that are shared with but not controlled by kernel.

---
 drivers/i2c/busses/i2c-designware-common.c | 12 ++++++++++++
 drivers/i2c/busses/i2c-designware-master.c |  6 ++++++
 2 files changed, 18 insertions(+)
drivers/i2c/busses/i2c-designware-common.c +12 -0
index bf2a4920638a..3e7ddae446a0 100644
@@ -578,9 +578,14 @@ int i2c_dw_set_fifo_size(struct dw_i2c_dev *dev)
 	 * Try to detect the FIFO depth if not set by interface driver,
 	 * the depth could be from 2 to 256 from HW spec.
 	 */
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		return ret;
+
 	ret = regmap_read(dev->map, DW_IC_COMP_PARAM_1, &param);
 	if (ret)
 		return ret;
+	i2c_dw_release_lock(dev);
 
 	tx_fifo_depth = ((param >> 16) & 0xff) + 1;
 	rx_fifo_depth = ((param >> 8)  & 0xff) + 1;
@@ -607,6 +612,11 @@ u32 i2c_dw_func(struct i2c_adapter *adap)
 void i2c_dw_disable(struct dw_i2c_dev *dev)
 {
 	u32 dummy;
+	int ret;
+
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		return;
 
 	/* Disable controller */
 	__i2c_dw_disable(dev);
@@ -614,6 +624,8 @@ void i2c_dw_disable(struct dw_i2c_dev *dev)
 	/* Disable all interrupts */
 	regmap_write(dev->map, DW_IC_INTR_MASK, 0);
 	regmap_read(dev->map, DW_IC_CLR_INTR, &dummy);
+
+	i2c_dw_release_lock(dev);
 }
 
 void i2c_dw_disable_int(struct dw_i2c_dev *dev)
drivers/i2c/busses/i2c-designware-master.c +6 -0
index 9b08bb5df38d..3faef28a9ba9 100644
@@ -902,7 +902,13 @@ int i2c_dw_probe_master(struct dw_i2c_dev *dev)
 		irq_flags = IRQF_SHARED | IRQF_COND_SUSPEND;
 	}
 
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		return ret;
+
 	i2c_dw_disable_int(dev);
+	i2c_dw_release_lock(dev);
+
 	ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags,
 			       dev_name(dev->dev), dev);
 	if (ret) {
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
Acked-by on v1 1/2
#2 lore raw

Re: [RFC 1/2] i2c: designware: Add missing locks

Quoted 10 lines · Jan Dabros

All accesses to controller's registers should be protected on probe, disable and xfer paths. This is needed for i2c bus controllers that are shared with but not controlled by kernel.

---
  drivers/i2c/busses/i2c-designware-common.c | 12 ++++++++++++
  drivers/i2c/busses/i2c-designware-master.c |  6 ++++++
  2 files changed, 18 insertions(+)
#3 lore raw

Re: [RFC 1/2] i2c: designware: Add missing locks

In reply to Jan Dabros
Quoted 3 lines · Jan Dabros

All accesses to controller's registers should be protected on probe, disable and xfer paths. This is needed for i2c bus controllers that are shared with but not controlled by kernel.

...

drivers/i2c/busses/i2c-designware-common.c int i2c_dw_set_fifo_size(struct dw_i2c_dev *dev) · line 588

Quoted 8 lines
drivers/i2c/busses/i2c-designware-common.c +5 -0
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		return ret;
+
 	ret = regmap_read(dev->map, DW_IC_COMP_PARAM_1, &param);
 	if (ret)
 		return ret;
+	i2c_dw_release_lock(dev);

Not sure this part is fully correct. Please, fix the leakage.

...

drivers/i2c/busses/i2c-designware-common.c void i2c_dw_disable(struct dw_i2c_dev *dev) · line 628

Quoted 12 lines
drivers/i2c/busses/i2c-designware-common.c +4 -0
+	ret = i2c_dw_acquire_lock(dev);
+	if (ret)
+		return;
 
 	/* Disable controller */
 	__i2c_dw_disable(dev);
@@ -614,6 +624,8 @@ void i2c_dw_disable(struct dw_i2c_dev *dev)
 	/* Disable all interrupts */
 	regmap_write(dev->map, DW_IC_INTR_MASK, 0);
 	regmap_read(dev->map, DW_IC_CLR_INTR, &dummy);
 
+	i2c_dw_release_lock(dev);

Not enough context here, bu I believe there is the same issue(s).

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

Re: [RFC 1/2] i2c: designware: Add missing locks

czw., 23 gru 2021 o 16:51 Andy Shevchenko
<[email protected]> napisał(a):

Quoted 17 lines · Jan Dabros
Quoted 3 lines

All accesses to controller's registers should be protected on probe, disable and xfer paths. This is needed for i2c bus controllers that are shared with but not controlled by kernel.

...

drivers/i2c/busses/i2c-designware-common.c int i2c_dw_set_fifo_size(struct dw_i2c_dev *dev) · line 588

Quoted 8 lines
drivers/i2c/busses/i2c-designware-common.c +5 -0
+     ret = i2c_dw_acquire_lock(dev);
+     if (ret)
+             return ret;
+
      ret = regmap_read(dev->map, DW_IC_COMP_PARAM_1, &param);
      if (ret)
              return ret;
+     i2c_dw_release_lock(dev);
Quoted 1 line

Not sure this part is fully correct. Please, fix the leakage.

Correct, I will move release() right below regmap_read() call, before checking return code.

Quoted 14 lines

...

Quoted 10 lines
drivers/i2c/busses/i2c-designware-common.c +3 -0
+     ret = i2c_dw_acquire_lock(dev);
+     if (ret)
+             return;
 
      /* Disable controller */
      __i2c_dw_disable(dev);
@@ -614,6 +624,8 @@ void i2c_dw_disable(struct dw_i2c_dev *dev)
      /* Disable all interrupts */
      regmap_write(dev->map, DW_IC_INTR_MASK, 0);
      regmap_read(dev->map, DW_IC_CLR_INTR, &dummy);
Quoted 3 lines

Review void i2c_dw_disable(struct dw_i2c_dev *dev) · line 627

Quoted 1 line
(quoted diff) +1 -0
+     i2c_dw_release_lock(dev);

Not enough context here, bu I believe there is the same issue(s).

Since we are ignoring values returned by regmap_write()/read() and also __i2c_dw_disable() returns void I don't see a leakage here. Don't have more function calls here.

author
#5 lore raw

RFC 2/2 i2c: designware: Add AMD PSP I2C bus support

In reply to Jan Dabros

Implement an I2C controller sharing mechanism between the host (kernel) and PSP co-processor on some platforms equipped with AMD Cezanne SoC.

On these platforms we need to implement "software" i2c arbitration. Default arbitration owner is PSP and kernel asks for acquire as well as inform about release of the i2c bus via mailbox mechanism.

            +---------+
 <- ACQUIRE |         |
  +---------|   CPU   |\
  |         |         | \      +----------+  SDA
  |         +---------+  \     |          |-------
MAILBOX                   +--> |  I2C-DW  |  SCL
  |         +---------+        |          |-------
  |         |         |        +----------+
  +---------|   PSP   |
   <- ACK   |         |
            +---------+
            +---------+
 <- RELEASE |         |
  +---------|   CPU   |
  |         |         |        +----------+  SDA
  |         +---------+        |          |-------
MAILBOX                   +--> |  I2C-DW  |  SCL
  |         +---------+  /     |          |-------
  |         |         | /      +----------+
  +---------|   PSP   |/
   <- ACK   |         |
            +---------+

The solution is similar to i2c-designware-baytrail.c implementation, where we are using a generic i2c-designware-* driver with a small "wrapper".

In contrary to baytrail semaphore implementation, beside internal acquire_lock() and release_lock() methods we are also applying quirks to lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Configure new matching ACPI ID and register ARBITRATION_SEMAPHORE flag in order to distinguish setup with PSP arbitration.

Only one instance of a driver may be running at a time since there is only one PSP mailbox available. Add extra check to ensure this.

Introduce new CONFIG and add new menuconfig selection list in order to allow (optional) selection between baytrail and amdpsp semaphore.

Add new entry in MAINTAINERS file to cover new module.

---
 MAINTAINERS                                 |   1 +
 drivers/acpi/acpi_apd.c                     |   1 +
 drivers/i2c/busses/Kconfig                  |  20 ++
 drivers/i2c/busses/Makefile                 |   1 +
 drivers/i2c/busses/i2c-designware-amdpsp.c  | 359 ++++++++++++++++++++
 drivers/i2c/busses/i2c-designware-core.h    |   9 +-
 drivers/i2c/busses/i2c-designware-platdrv.c |   3 +
 7 files changed, 393 insertions(+), 1 deletion(-)
 create mode 100644 drivers/i2c/busses/i2c-designware-amdpsp.c
MAINTAINERS +1 -0
index 8912b2c1260c..8a75c355942f 100644
@@ -18410,6 +18410,7 @@ SYNOPSYS DESIGNWARE I2C DRIVER
 M:	Jarkko Nikula <[email protected]>
 R:	Andy Shevchenko <[email protected]>
 R:	Mika Westerberg <[email protected]>
+R:	Jan Dabros <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	drivers/i2c/busses/i2c-designware-*
drivers/acpi/acpi_apd.c +1 -0
index 6e02448d15d9..1d7a3bed8250 100644
@@ -227,6 +227,7 @@ static const struct acpi_device_id acpi_apd_device_ids[] = {
 	{ "AMD0020", APD_ADDR(cz_uart_desc) },
 	{ "AMDI0020", APD_ADDR(cz_uart_desc) },
 	{ "AMDI0022", APD_ADDR(cz_uart_desc) },
+	{ "AMDI9999", APD_ADDR(wt_i2c_desc) },
 	{ "AMD0030", },
 	{ "AMD0040", APD_ADDR(fch_misc_desc)},
 	{ "HYGO0010", APD_ADDR(wt_i2c_desc) },
drivers/i2c/busses/Kconfig +20 -0
index dce392839017..a54d592fc1a0 100644
@@ -553,6 +553,24 @@ config I2C_DESIGNWARE_PLATFORM
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-designware-platform.
 
+choice
+	prompt "I2C DesignWare semaphore support"
+	depends on I2C_DESIGNWARE_PLATFORM
+	optional
+	help
+	  Option to enable implementations of DesignWare I2C driver with
+	  support for semaphores.
+
+config I2C_DESIGNWARE_AMDPSP
+	bool "AMD PSP I2C semaphore support"
+	depends on ACPI
+	depends on I2C_DESIGNWARE_PLATFORM
+	help
+	  This driver enables managed host access to the selected I2C bus shared
+	  between AMD CPU and AMD PSP.
+
+	  You should say Y if running on an AMD system equipped with the PSP.
+
 config I2C_DESIGNWARE_BAYTRAIL
 	bool "Intel Baytrail I2C semaphore support"
 	depends on ACPI
@@ -566,6 +584,8 @@ config I2C_DESIGNWARE_BAYTRAIL
 	  the platform firmware controlling it. You should say Y if running on
 	  a BayTrail system using the AXP288.
 
+endchoice
+
 config I2C_DESIGNWARE_PCI
 	tristate "Synopsys DesignWare PCI"
 	depends on PCI
drivers/i2c/busses/Makefile +1 -0
index d85899fef8c7..eaacb31769eb 100644
@@ -54,6 +54,7 @@ i2c-designware-core-y					+= i2c-designware-master.o
 i2c-designware-core-$(CONFIG_I2C_DESIGNWARE_SLAVE) 	+= i2c-designware-slave.o
 obj-$(CONFIG_I2C_DESIGNWARE_PLATFORM)			+= i2c-designware-platform.o
 i2c-designware-platform-y 				:= i2c-designware-platdrv.o
+i2c-designware-platform-$(CONFIG_I2C_DESIGNWARE_AMDPSP)	+= i2c-designware-amdpsp.o
 i2c-designware-platform-$(CONFIG_I2C_DESIGNWARE_BAYTRAIL) += i2c-designware-baytrail.o
 obj-$(CONFIG_I2C_DESIGNWARE_PCI)			+= i2c-designware-pci.o
 i2c-designware-pci-y					:= i2c-designware-pcidrv.o
drivers/i2c/busses/i2c-designware-amdpsp.c new file +359 -0

359 lines not on this page · raw message

drivers/i2c/busses/i2c-designware-core.h +8 -1
index 60a2e750cee9..252931a3f552 100644
@@ -292,6 +292,7 @@ struct dw_i2c_dev {
 
 #define ACCESS_INTR_MASK	BIT(0)
 #define ACCESS_NO_IRQ_SUSPEND	BIT(1)
+#define ARBITRATION_SEMAPHORE	BIT(2)
 
 #define MODEL_MSCC_OCELOT	BIT(8)
 #define MODEL_BAIKAL_BT1	BIT(9)
@@ -364,12 +365,18 @@ static inline void i2c_dw_configure(struct dw_i2c_dev *dev)
 		i2c_dw_configure_master(dev);
 }
 
-#if IS_ENABLED(CONFIG_I2C_DESIGNWARE_BAYTRAIL)
+#if IS_ENABLED(CONFIG_I2C_DESIGNWARE_BAYTRAIL) || IS_ENABLED(CONFIG_I2C_DESIGNWARE_AMDPSP)
 extern int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev);
 #else
 static inline int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev) { return 0; }
 #endif
 
+#if IS_ENABLED(CONFIG_I2C_DESIGNWARE_AMDPSP)
+extern void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev);
+#else
+static inline void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev) { return; }
+#endif
+
 int i2c_dw_validate_speed(struct dw_i2c_dev *dev);
 void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev);
 
drivers/i2c/busses/i2c-designware-platdrv.c +3 -0
index 21113665ddea..2023da6b020f 100644
@@ -51,6 +51,7 @@ static const struct acpi_device_id dw_i2c_acpi_match[] = {
 	{ "AMD0010", ACCESS_INTR_MASK },
 	{ "AMDI0010", ACCESS_INTR_MASK },
 	{ "AMDI0510", 0 },
+	{ "AMDI9999", ACCESS_INTR_MASK | ARBITRATION_SEMAPHORE },
 	{ "APMC0D0F", 0 },
 	{ "HISI02A1", 0 },
 	{ "HISI02A2", 0 },
@@ -332,6 +333,8 @@ static int dw_i2c_plat_remove(struct platform_device *pdev)
 	pm_runtime_put_sync(&pdev->dev);
 	dw_i2c_plat_pm_cleanup(dev);
 
+	i2c_dw_remove_lock_support(dev);
+
 	reset_control_assert(dev->rst);
 
 	return 0;
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#6 lore raw

Re: [RFC 2/2] i2c: designware: Add AMD PSP I2C bus support

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Jan,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on wsa/i2c/for-next]
[also build test WARNING on linux/master rafael-pm/linux-next linus/master v5.16-rc6 next-20211222] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jan-Dabros/i2c-designware-Add-support-for-AMD-PSP-semaphore/20211222-174729
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: ia64-allyesconfig (https://download.01.org/0day-ci/archive/20211223/202112230116.Z8RNaMCY-lkp(a)intel.com/config)
compiler: ia64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/d0789fea89a6cfb42f237fd64068a7fbc9c7c283
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jan-Dabros/i2c-designware-Add-support-for-AMD-PSP-semaphore/20211222-174729 git checkout d0789fea89a6cfb42f237fd64068a7fbc9c7c283
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash drivers/i2c/busses/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_get_mbox_addr':
drivers/i2c/busses/i2c-designware-amdpsp.c:65:13: error: implicit declaration of function 'rdmsrl_safe' [-Werror=implicit-function-declaration]
65 | if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
| ^~~~~~~~~~~
drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_wait_cmd':

>> drivers/i2c/busses/i2c-designware-amdpsp.c:98:32: warning: variable 'and_mask' set but not used [-Wunused-but-set-variable]
      98 |         union psp_mbox_cmd_reg and_mask = { .val = ~0 };
         |                                ^~~~~~~~
   In file included from include/linux/printk.h:559,
                    from include/linux/kernel.h:20,
                    from include/linux/kernfs.h:9,
                    from include/linux/sysfs.h:16,
                    from include/linux/kobject.h:20,
                    from include/linux/of.h:17,
                    from include/linux/irqdomain.h:35,
                    from include/linux/acpi.h:13,
                    from include/linux/i2c.h:13,
                    from drivers/i2c/busses/i2c-designware-amdpsp.c:3:
   drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_send_i2c_req':
>> drivers/i2c/busses/i2c-designware-amdpsp.c:188:46: warning: too many arguments for format [-Wformat-extra-args]
     188 |                         dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
         |                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:134:29: note: in definition of macro '__dynamic_func_call'
     134 |                 func(&id, ##__VA_ARGS__);               \
         |                             ^~~~~~~~~~~
   include/linux/dynamic_debug.h:166:9: note: in expansion of macro '_dynamic_func_call'
     166 |         _dynamic_func_call(fmt,__dynamic_dev_dbg,               \
         |         ^~~~~~~~~~~~~~~~~~
   include/linux/dev_printk.h:155:9: note: in expansion of macro 'dynamic_dev_dbg'
     155 |         dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~~~~~~~~~~
   include/linux/dev_printk.h:155:30: note: in expansion of macro 'dev_fmt'
     155 |         dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
         |                              ^~~~~~~
   drivers/i2c/busses/i2c-designware-amdpsp.c:188:25: note: in expansion of macro 'dev_dbg'
     188 |                         dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
         |                         ^~~~~~~

cc1: some warnings being treated as errors

vim +/and_mask +98 drivers/i2c/busses/i2c-designware-amdpsp.c

    95	
    96	static int psp_wait_cmd(struct psp_mbox *mbox)
    97	{
  > 98		union psp_mbox_cmd_reg and_mask = { .val = ~0 };
    99		union psp_mbox_cmd_reg expected = { .val = 0 };
   100		u32 tmp;
   101	
   102		/* Expect mbox_cmd to be cleared and ready bit to be set by PSP */
   103		and_mask.fields.mbox_cmd = 0;
   104		and_mask.fields.ready = 0;
   105		expected.fields.ready = 1;
   106	
   107		return readl_poll_timeout(&mbox->fields.val, tmp, (tmp == expected.val),
   108					  0, 1000 * PSP_CMD_TIMEOUT_MS);
   109	}
   110	
   111	/* Status equal to 0 means that PSP succeed processing command */
   112	static int psp_check_mbox_sts(struct psp_mbox *mbox)
   113	{
   114		union psp_mbox_cmd_reg cmd_reg = {0};
   115	
   116		cmd_reg.val = readl(&mbox->fields.val);
   117		return cmd_reg.fields.mbox_status;
   118	}
   119	
   120	static int psp_send_cmd(struct psp_i2c_req *req)
   121	{
   122		struct psp_mbox *mbox = (struct psp_mbox *)mbox_iomem;
   123		union psp_mbox_cmd_reg cmd_reg = {0};
   124	
   125		if (psp_check_mbox_recovery(mbox))
   126			return -EIO;
   127	
   128		if (psp_wait_cmd(mbox))
   129			return -EBUSY;
   130	
   131		/* Fill address of command-response buffer */
   132		writeq((uintptr_t)__psp_pa((void *)req), &mbox->i2c_req_addr);
   133	
   134		/* Write command register to trigger processing */
   135		cmd_reg.fields.mbox_cmd = PSP_I2C_REQ_BUS_CMD;
   136		writel(cmd_reg.val, &mbox->fields.val);
   137	
   138		if (psp_wait_cmd(mbox))
   139			return -ETIMEDOUT;
   140	
   141		if (psp_check_mbox_sts(mbox))
   142			return -EIO;
   143	
   144		return 0;
   145	}
   146	
   147	/* Helper to verify status returned by PSP */
   148	static int check_i2c_req_sts(struct psp_i2c_req *req)
   149	{
   150		int status;
   151	
   152		status = readl(&req->hdr.status);
   153	
   154		switch (status) {
   155		case PSP_I2C_REQ_STS_OK:
   156			return 0;
   157		case PSP_I2C_REQ_STS_BUS_BUSY:
   158			return -EBUSY;
   159		case PSP_I2C_REQ_STS_INV_PARAM:
   160		default:
   161			return -EIO;
   162		};
   163	}
   164	
   165	static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
   166	{
   167		int status, ret, retry_cnt = PSP_I2C_REQ_RETRY_CNT;
   168		struct psp_i2c_req *req;
   169		unsigned long start;
   170	
   171		/* Allocate command-response buffer */
   172		req = kzalloc(sizeof(*req), GFP_KERNEL);
   173		if (!req)
   174			return -ENOMEM;
   175	
   176		req->hdr.total_size = sizeof(*req);
   177		req->type = i2c_req_type;
   178	
   179		start = jiffies;
   180		do {
   181			if (psp_send_cmd(req)) {
   182				ret = -EIO;
   183				goto cleanup;
   184			}
   185	
   186			status = check_i2c_req_sts(req);
   187			if (!status) {
 > 188				dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",

189 i2c_req_type, jiffies_to_msecs(jiffies - start));
190 ret = 0;
191 goto cleanup;
192 } else if (status == -EBUSY) {
193 retry_cnt--;
194 } else {
195 ret = -EIO;
196 goto cleanup;
197 };
198
199 /* IF EBUSY, give PSP time to finish its i2c activities */
200 mdelay(PSP_I2C_REQ_RETRY_DELAY_MSEC);
201 } while (retry_cnt);
202
203
204 ret = -ETIMEDOUT;
205
206 cleanup:
207 kfree(req);
208 return ret;
209 }
210

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [RFC 2/2] i2c: designware: Add AMD PSP I2C bus support

In reply to Jan Dabros
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Jan,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on linux/master rafael-pm/linux-next linus/master v5.16-rc6 next-20211222] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jan-Dabros/i2c-designware-Add-support-for-AMD-PSP-semaphore/20211222-174729
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20211223/202112230819.LF6yKZIf-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/d0789fea89a6cfb42f237fd64068a7fbc9c7c283
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jan-Dabros/i2c-designware-Add-support-for-AMD-PSP-semaphore/20211222-174729 git checkout d0789fea89a6cfb42f237fd64068a7fbc9c7c283
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_wait_cmd':
drivers/i2c/busses/i2c-designware-amdpsp.c:98:25: warning: variable 'and_mask' set but not used [-Wunused-but-set-variable]

      98 |  union psp_mbox_cmd_reg and_mask = { .val = ~0 };
         |                         ^~~~~~~~
   drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_send_cmd':
>> drivers/i2c/busses/i2c-designware-amdpsp.c:132:2: error: implicit declaration of function 'writeq'; did you mean 'writel'? [-Werror=implicit-function-declaration]
     132 |  writeq((uintptr_t)__psp_pa((void *)req), &mbox->i2c_req_addr);
         |  ^~~~~~
         |  writel
   In file included from include/linux/printk.h:559,
                    from include/linux/kernel.h:20,
                    from include/linux/kernfs.h:9,
                    from include/linux/sysfs.h:16,
                    from include/linux/kobject.h:20,
                    from include/linux/of.h:17,
                    from include/linux/irqdomain.h:35,
                    from include/linux/acpi.h:13,
                    from include/linux/i2c.h:13,
                    from drivers/i2c/busses/i2c-designware-amdpsp.c:3:
   drivers/i2c/busses/i2c-designware-amdpsp.c: In function 'psp_send_i2c_req':
   drivers/i2c/busses/i2c-designware-amdpsp.c:188:25: warning: too many arguments for format [-Wformat-extra-args]
     188 |    dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:134:15: note: in definition of macro '__dynamic_func_call'
     134 |   func(&id, ##__VA_ARGS__);  \
         |               ^~~~~~~~~~~
   include/linux/dynamic_debug.h:166:2: note: in expansion of macro '_dynamic_func_call'
     166 |  _dynamic_func_call(fmt,__dynamic_dev_dbg,   \
         |  ^~~~~~~~~~~~~~~~~~
   include/linux/dev_printk.h:155:2: note: in expansion of macro 'dynamic_dev_dbg'
     155 |  dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
         |  ^~~~~~~~~~~~~~~
   include/linux/dev_printk.h:155:23: note: in expansion of macro 'dev_fmt'
     155 |  dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
         |                       ^~~~~~~
   drivers/i2c/busses/i2c-designware-amdpsp.c:188:4: note: in expansion of macro 'dev_dbg'
     188 |    dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
         |    ^~~~~~~

cc1: some warnings being treated as errors

vim +132 drivers/i2c/busses/i2c-designware-amdpsp.c

119
120 static int psp_send_cmd(struct psp_i2c_req *req)
121 {
122 struct psp_mbox *mbox = (struct psp_mbox *)mbox_iomem;
123 union psp_mbox_cmd_reg cmd_reg = {0};
124
125 if (psp_check_mbox_recovery(mbox))
126 return -EIO;
127
128 if (psp_wait_cmd(mbox))
129 return -EBUSY;
130
131 /* Fill address of command-response buffer */

 > 132		writeq((uintptr_t)__psp_pa((void *)req), &mbox->i2c_req_addr);

133
134 /* Write command register to trigger processing */
135 cmd_reg.fields.mbox_cmd = PSP_I2C_REQ_BUS_CMD;
136 writel(cmd_reg.val, &mbox->fields.val);
137
138 if (psp_wait_cmd(mbox))
139 return -ETIMEDOUT;
140
141 if (psp_check_mbox_sts(mbox))
142 return -EIO;
143
144 return 0;
145 }
146

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#8 lore raw

Re: [RFC 2/2] i2c: designware: Add AMD PSP I2C bus support

In reply to Jan Dabros
Quoted 4 lines · Jan Dabros

Introduce new CONFIG and add new menuconfig selection list in order to allow (optional) selection between baytrail and amdpsp semaphore.

For the Linux distribution point of view selection must happen runtime. Otherwise either AMD PSP or Baytrail semaphore support is missing from the installation image.

Jarkko

#9 lore raw

Re: [RFC 2/2] i2c: designware: Add AMD PSP I2C bus support

czw., 23 gru 2021 o 15:37 Jarkko Nikula
<[email protected]> napisał(a):

Quoted 9 lines · Jan Dabros
Quoted 4 lines

Introduce new CONFIG and add new menuconfig selection list in order to allow (optional) selection between baytrail and amdpsp semaphore.

For the Linux distribution point of view selection must happen runtime. Otherwise either AMD PSP or Baytrail semaphore support is missing from the installation image.

I see, thanks for your input.
Will modify the driver so that it should work with both (Baytrail, AMDPSP and potential new ones) semaphore modules compiled in. Executing probe()'s at runtime will allow to select and install correct callbacks.

Best Regards,
Jan

#10 lore raw
In reply to Jan Dabros

+Serge

Quoted 21 lines · Jan Dabros

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

If I am not mistaken something similar happened in Baikal T1. Perhaps Serge has something to share.

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

Hi Andy, Jan

Quoted 24 lines · Andy Shevchenko

+Serge

Quoted 21 lines · Jan Dabros

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 8 lines
Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

If I am not mistaken something similar happened in Baikal T1. Perhaps Serge has something to share.

No, Baikal-T1 doesn't have such specific interface sharing since it doesn't have any co-processor (though a scenario of booting different kernels on each CPU core was at consideration by some our customers). So the only peculiar things the SoC has got are two interfaces with non-standard access:
1) DW SPI controller with memory mapped 16MB direct EEPROM access. DW SPI CSR/EEPROM mapping are switched by a multiplexer (basically just a flag) embedded into the system controller.
2) DW i2c controller with indirect registers access. Originally it was supposed to be used by the system bootloader for some bootloading stuff, but the actual usage scenario wasn't described by the SoC engineers. The chip initially loads the code from the SPI-flash only, which can be of up to 16MB size. It's more than enough to start pretty complex systems, so an additional i2c interface turned to be not really needed. Anyway other than having an indirectly accessible CSRs it's pretty much normal DW I2C controller.

But you are right in a reference to another BE-chip - Baikal-M1. The i2c/spi/gpio/uart interfaces sharing support might get to be needed for it since aside with four 2-cored Cortex-A57 clusters it has got an embedded SCP co-processor which can access the same SoC interfaces as the CPU cores. Though Baikal-M1 isn't supported by the mainline kernel at the moment. We are going to start working on it on the next year. Then we'll most likely need to implement the interface sharing feature like the one introduced in this RFC. But for now alas I can't be much helpful.

-Sergey

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko
#12 lore raw
Quoted 49 lines · Serge Semin
Quoted 22 lines · Andy Shevchenko
Quoted 21 lines · Jan Dabros

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 8 lines
Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

If I am not mistaken something similar happened in Baikal T1. Perhaps Serge has something to share.

No, Baikal-T1 doesn't have such specific interface sharing since it doesn't have any co-processor (though a scenario of booting different kernels on each CPU core was at consideration by some our customers). So the only peculiar things the SoC has got are two interfaces with non-standard access:
1) DW SPI controller with memory mapped 16MB direct EEPROM access. DW SPI CSR/EEPROM mapping are switched by a multiplexer (basically just a flag) embedded into the system controller.
2) DW i2c controller with indirect registers access. Originally it was supposed to be used by the system bootloader for some bootloading stuff, but the actual usage scenario wasn't described by the SoC engineers. The chip initially loads the code from the SPI-flash only, which can be of up to 16MB size. It's more than enough to start pretty complex systems, so an additional i2c interface turned to be not really needed. Anyway other than having an indirectly accessible CSRs it's pretty much normal DW I2C controller.

Quoted 9 lines

But you are right in a reference to another BE-chip - Baikal-M1. The i2c/spi/gpio/uart interfaces sharing support might get to be needed for it since aside with four 2-cored Cortex-A57 clusters it has got an embedded SCP co-processor which can access the same SoC interfaces as the CPU cores. Though Baikal-M1 isn't supported by the mainline kernel at the moment. We are going to start working on it on the next year. Then we'll most likely need to implement the interface sharing feature like the one introduced in this RFC. But for now alas I can't be much helpful.

Anyway, thanks for your insights!

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

Hi Serge, Andy,

Thanks for your comments. Glad to hear that this work may (possibly) be helpful for even broader audience. For most of the stuff I tried to create generic code and actually these solutions are already meant to be shared with platforms using baytrail semaphore.

Best Regards,
Jan

śr., 22 gru 2021 o 19:22 Andy Shevchenko
<[email protected]> napisał(a):

Quoted 68 lines · Serge Semin
Quoted 49 lines · Andy Shevchenko
Quoted 22 lines · Jan Dabros
Quoted 21 lines

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 8 lines
Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

If I am not mistaken something similar happened in Baikal T1. Perhaps Serge has something to share.

No, Baikal-T1 doesn't have such specific interface sharing since it doesn't have any co-processor (though a scenario of booting different kernels on each CPU core was at consideration by some our customers). So the only peculiar things the SoC has got are two interfaces with non-standard access:
1) DW SPI controller with memory mapped 16MB direct EEPROM access. DW SPI CSR/EEPROM mapping are switched by a multiplexer (basically just a flag) embedded into the system controller.
2) DW i2c controller with indirect registers access. Originally it was supposed to be used by the system bootloader for some bootloading stuff, but the actual usage scenario wasn't described by the SoC engineers. The chip initially loads the code from the SPI-flash only, which can be of up to 16MB size. It's more than enough to start pretty complex systems, so an additional i2c interface turned to be not really needed. Anyway other than having an indirectly accessible CSRs it's pretty much normal DW I2C controller.

Quoted 9 lines

But you are right in a reference to another BE-chip - Baikal-M1. The i2c/spi/gpio/uart interfaces sharing support might get to be needed for it since aside with four 2-cored Cortex-A57 clusters it has got an embedded SCP co-processor which can access the same SoC interfaces as the CPU cores. Though Baikal-M1 isn't supported by the mainline kernel at the moment. We are going to start working on it on the next year. Then we'll most likely need to implement the interface sharing feature like the one introduced in this RFC. But for now alas I can't be much helpful.

Anyway, thanks for your insights!

--
With Best Regards,
Andy Shevchenko

#14 lore raw
In reply to Jan Dabros
Quoted 22 lines · Jan Dabros

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

When you will be ready, CC a new version also to Hans, he may look at it from the Baytrail functionality perspective.

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

sob., 25 gru 2021 o 16:59 Andy Shevchenko <[email protected]> napisał(a):

Quoted 24 lines · Jan Dabros
Quoted 22 lines

This patchset comprises support for new i2c-designware controller setup on some AMD Cezanne SoCs, where x86 is sharing i2c bus with PSP. PSP uses the same controller and acts as an i2c arbitrator there (x86 is leasing bus from it).

First commit aims to improve generic i2c-designware code by adding extra locking on probe() and disable() paths. I would like to ask someone with access to boards which use Intel BayTrail(CONFIG_I2C_DESIGNWARE_BAYTRAIL) to verify behavior of my changes on such setup.

Second commit adds support for new PSP semaphore arbitration mechanism. Implementation is similar to the one from i2c-designware-baytrail.c however there are two main differences:
1) Add new ACPI ID in order to protect against silent binding of the old driver to the setup with PSP semaphore. Extra flag ARBITRATION_SEMAPHORE added to this new _HID allows to recognize setup with PSP.
2) Beside acquire_lock() and release_lock() methods we are also applying quirks to the lock_bus() and unlock_bus() global adapter methods. With this in place all i2c clients drivers may lock i2c bus for a desired number of i2c transactions (e.g. write-wait-read) without being aware of that such bus is shared with another entity.

Quoted 8 lines
Quoted 5 lines

Mark this patchset as RFC, since waiting for new ACPI ID value. As a temporary measure use "AMDI9999". Once proper one will be ready, will re-send this CL for review & merge.

Looking forward to some feedback.

When you will be ready, CC a new version also to Hans, he may look at it from the Baytrail functionality perspective.

Thanks for the hint, it will be very helpful to have this tested on Baytrail. From other comments on this RFC it seems that I would need to affect Baytrail-semaphore path even more than initially thought.

Best Regards,
Jan