All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag
@ 2022-02-23 13:48 Hans de Goede
  2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Hans de Goede @ 2022-02-23 13:48 UTC (permalink / raw)
  To: Jarkko Nikula, Wolfram Sang, Andy Shevchenko, Mika Westerberg
  Cc: Hans de Goede, linux-i2c

Lock the adapter while setting the suspended flag, to ensure that other
locked code always sees the change immediately, rather then possibly using
a stale value.

This involves splitting the suspend/resume callbacks into separate runtime
and normal suspend/resume calls. This is necessary because i2c_dw_xfer()
will get called by the i2c-core with the adapter locked and it in turn
calls the runtime-resume callback through pm_runtime_get_sync().

So the runtime versions of the suspend/resume callbacks cannot take
the adapter-lock. Note this patch simply makes the runtime suspend/resume
callbacks not deal with the suspended flag at all. During runtime the
pm_runtime_get_sync() from i2c_dw_xfer() will always ensure that the
adapter is resumed when necessary.

The suspended flag check is only necessary to check proper suspend/resume
ordering during normal suspend/resume which makes the pm_runtime_get_sync()
call a no-op.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/i2c/busses/i2c-designware-pcidrv.c  | 31 +++++++++++++++++----
 drivers/i2c/busses/i2c-designware-platdrv.c | 31 +++++++++++++++++----
 2 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index ef4250f8852b..9553d7075223 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -213,14 +213,30 @@ static struct dw_pci_controller dw_pci_controllers[] = {
 	},
 };
 
+static int __maybe_unused i2c_dw_pci_runtime_suspend(struct device *dev)
+{
+	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
+
+	i_dev->disable(i_dev);
+	return 0;
+}
+
 static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
+	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 	i_dev->suspended = true;
-	i_dev->disable(i_dev);
+	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 
-	return 0;
+	return i2c_dw_pci_runtime_suspend(dev);
+}
+
+static int __maybe_unused i2c_dw_pci_runtime_resume(struct device *dev)
+{
+	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
+
+	return i_dev->init(i_dev);
 }
 
 static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
@@ -228,14 +244,19 @@ static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 	int ret;
 
-	ret = i_dev->init(i_dev);
+	ret = i2c_dw_pci_runtime_resume(dev);
+
+	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 	i_dev->suspended = false;
+	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 
 	return ret;
 }
 
-static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
-			    i2c_dw_pci_resume, NULL);
+static const struct dev_pm_ops i2c_dw_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume)
+	SET_RUNTIME_PM_OPS(i2c_dw_pci_runtime_suspend, i2c_dw_pci_runtime_resume, NULL)
+};
 
 static int i2c_dw_pci_probe(struct pci_dev *pdev,
 			    const struct pci_device_id *id)
diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 2bd81abc86f6..8e45f65bab73 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -368,12 +368,10 @@ static void dw_i2c_plat_complete(struct device *dev)
 #endif
 
 #ifdef CONFIG_PM
-static int dw_i2c_plat_suspend(struct device *dev)
+static int dw_i2c_plat_runtime_suspend(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
-	i_dev->suspended = true;
-
 	if (i_dev->shared_with_punit)
 		return 0;
 
@@ -383,7 +381,18 @@ static int dw_i2c_plat_suspend(struct device *dev)
 	return 0;
 }
 
-static int dw_i2c_plat_resume(struct device *dev)
+static int dw_i2c_plat_suspend(struct device *dev)
+{
+	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
+
+	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+	i_dev->suspended = true;
+	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+
+	return dw_i2c_plat_runtime_suspend(dev);
+}
+
+static int dw_i2c_plat_runtime_resume(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
@@ -391,7 +400,19 @@ static int dw_i2c_plat_resume(struct device *dev)
 		i2c_dw_prepare_clk(i_dev, true);
 
 	i_dev->init(i_dev);
+
+	return 0;
+}
+
+static int dw_i2c_plat_resume(struct device *dev)
+{
+	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
+
+	dw_i2c_plat_runtime_resume(dev);
+
+	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 	i_dev->suspended = false;
+	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
 
 	return 0;
 }
@@ -400,7 +421,7 @@ static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
 	.prepare = dw_i2c_plat_prepare,
 	.complete = dw_i2c_plat_complete,
 	SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
-	SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
+	SET_RUNTIME_PM_OPS(dw_i2c_plat_runtime_suspend, dw_i2c_plat_runtime_resume, NULL)
 };
 
 #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers
  2022-02-23 13:48 [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Hans de Goede
@ 2022-02-23 13:48 ` Hans de Goede
  2022-02-24 13:03   ` Jarkko Nikula
  2022-03-01 15:33   ` Wolfram Sang
  2022-02-23 14:52 ` [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Andy Shevchenko
  2022-03-01 15:32 ` Wolfram Sang
  2 siblings, 2 replies; 8+ messages in thread
From: Hans de Goede @ 2022-02-23 13:48 UTC (permalink / raw)
  To: Jarkko Nikula, Wolfram Sang, Andy Shevchenko, Mika Westerberg
  Cc: Hans de Goede, linux-i2c

Use the i2c_mark_adapter_suspended/resumed() i2c-core helpers and rely
on the i2c-core's suspended checking instead of using DIY code.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/i2c/busses/i2c-designware-core.h    | 2 --
 drivers/i2c/busses/i2c-designware-master.c  | 5 -----
 drivers/i2c/busses/i2c-designware-pcidrv.c  | 8 ++------
 drivers/i2c/busses/i2c-designware-platdrv.c | 9 ++-------
 4 files changed, 4 insertions(+), 20 deletions(-)

diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h
index 4b26cba40139..d9ac302011f2 100644
--- a/drivers/i2c/busses/i2c-designware-core.h
+++ b/drivers/i2c/busses/i2c-designware-core.h
@@ -234,7 +234,6 @@ struct reset_control;
  * @set_sda_hold_time: callback to retrieve IP specific SDA hold timing
  * @mode: operation mode - DW_IC_MASTER or DW_IC_SLAVE
  * @rinfo: I²C GPIO recovery information
- * @suspended: set to true if the controller is suspended
  *
  * HCNT and LCNT parameters can be used if the platform knows more accurate
  * values than the one computed based only on the input clock frequency.
@@ -292,7 +291,6 @@ struct dw_i2c_dev {
 	int			(*set_sda_hold_time)(struct dw_i2c_dev *dev);
 	int			mode;
 	struct i2c_bus_recovery_info rinfo;
-	bool			suspended;
 };
 
 #define ACCESS_INTR_MASK	BIT(0)
diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c
index 1a4b23556db3..44a94b225ed8 100644
--- a/drivers/i2c/busses/i2c-designware-master.c
+++ b/drivers/i2c/busses/i2c-designware-master.c
@@ -567,11 +567,6 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 		goto done_nolock;
 	}
 
-	if (dev_WARN_ONCE(dev->dev, dev->suspended, "Transfer while suspended\n")) {
-		ret = -ESHUTDOWN;
-		goto done_nolock;
-	}
-
 	reinit_completion(&dev->cmd_complete);
 	dev->msgs = msgs;
 	dev->msgs_num = num;
diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index 9553d7075223..777cbac34982 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -225,9 +225,7 @@ static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
-	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
-	i_dev->suspended = true;
-	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+	i2c_mark_adapter_suspended(&i_dev->adapter);
 
 	return i2c_dw_pci_runtime_suspend(dev);
 }
@@ -246,9 +244,7 @@ static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
 
 	ret = i2c_dw_pci_runtime_resume(dev);
 
-	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
-	i_dev->suspended = false;
-	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+	i2c_mark_adapter_resumed(&i_dev->adapter);
 
 	return ret;
 }
diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 8e45f65bab73..43d2bee45ede 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -385,9 +385,7 @@ static int dw_i2c_plat_suspend(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
-	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
-	i_dev->suspended = true;
-	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+	i2c_mark_adapter_suspended(&i_dev->adapter);
 
 	return dw_i2c_plat_runtime_suspend(dev);
 }
@@ -409,10 +407,7 @@ static int dw_i2c_plat_resume(struct device *dev)
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
 	dw_i2c_plat_runtime_resume(dev);
-
-	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
-	i_dev->suspended = false;
-	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
+	i2c_mark_adapter_resumed(&i_dev->adapter);
 
 	return 0;
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag
  2022-02-23 13:48 [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Hans de Goede
  2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
@ 2022-02-23 14:52 ` Andy Shevchenko
  2022-02-23 15:15   ` Hans de Goede
  2022-03-01 15:32 ` Wolfram Sang
  2 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-02-23 14:52 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Jarkko Nikula, Wolfram Sang, Andy Shevchenko, Mika Westerberg, linux-i2c

On Wed, Feb 23, 2022 at 02:48:38PM +0100, Hans de Goede wrote:
> Lock the adapter while setting the suspended flag, to ensure that other
> locked code always sees the change immediately, rather then possibly using
> a stale value.
> 
> This involves splitting the suspend/resume callbacks into separate runtime
> and normal suspend/resume calls. This is necessary because i2c_dw_xfer()
> will get called by the i2c-core with the adapter locked and it in turn
> calls the runtime-resume callback through pm_runtime_get_sync().
> 
> So the runtime versions of the suspend/resume callbacks cannot take
> the adapter-lock. Note this patch simply makes the runtime suspend/resume
> callbacks not deal with the suspended flag at all. During runtime the
> pm_runtime_get_sync() from i2c_dw_xfer() will always ensure that the
> adapter is resumed when necessary.
> 
> The suspended flag check is only necessary to check proper suspend/resume
> ordering during normal suspend/resume which makes the pm_runtime_get_sync()
> call a no-op.

Both patches look good to me and seems like a nice clean up. Not sure if the
first one should be backported (and hence have a Fixes tag).

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>  drivers/i2c/busses/i2c-designware-pcidrv.c  | 31 +++++++++++++++++----
>  drivers/i2c/busses/i2c-designware-platdrv.c | 31 +++++++++++++++++----
>  2 files changed, 52 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
> index ef4250f8852b..9553d7075223 100644
> --- a/drivers/i2c/busses/i2c-designware-pcidrv.c
> +++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
> @@ -213,14 +213,30 @@ static struct dw_pci_controller dw_pci_controllers[] = {
>  	},
>  };
>  
> +static int __maybe_unused i2c_dw_pci_runtime_suspend(struct device *dev)
> +{
> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
> +
> +	i_dev->disable(i_dev);
> +	return 0;
> +}
> +
>  static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
>  {
>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>  
> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  	i_dev->suspended = true;
> -	i_dev->disable(i_dev);
> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  
> -	return 0;
> +	return i2c_dw_pci_runtime_suspend(dev);
> +}
> +
> +static int __maybe_unused i2c_dw_pci_runtime_resume(struct device *dev)
> +{
> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
> +
> +	return i_dev->init(i_dev);
>  }
>  
>  static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
> @@ -228,14 +244,19 @@ static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>  	int ret;
>  
> -	ret = i_dev->init(i_dev);
> +	ret = i2c_dw_pci_runtime_resume(dev);
> +
> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  	i_dev->suspended = false;
> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  
>  	return ret;
>  }
>  
> -static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
> -			    i2c_dw_pci_resume, NULL);
> +static const struct dev_pm_ops i2c_dw_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume)
> +	SET_RUNTIME_PM_OPS(i2c_dw_pci_runtime_suspend, i2c_dw_pci_runtime_resume, NULL)
> +};
>  
>  static int i2c_dw_pci_probe(struct pci_dev *pdev,
>  			    const struct pci_device_id *id)
> diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
> index 2bd81abc86f6..8e45f65bab73 100644
> --- a/drivers/i2c/busses/i2c-designware-platdrv.c
> +++ b/drivers/i2c/busses/i2c-designware-platdrv.c
> @@ -368,12 +368,10 @@ static void dw_i2c_plat_complete(struct device *dev)
>  #endif
>  
>  #ifdef CONFIG_PM
> -static int dw_i2c_plat_suspend(struct device *dev)
> +static int dw_i2c_plat_runtime_suspend(struct device *dev)
>  {
>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>  
> -	i_dev->suspended = true;
> -
>  	if (i_dev->shared_with_punit)
>  		return 0;
>  
> @@ -383,7 +381,18 @@ static int dw_i2c_plat_suspend(struct device *dev)
>  	return 0;
>  }
>  
> -static int dw_i2c_plat_resume(struct device *dev)
> +static int dw_i2c_plat_suspend(struct device *dev)
> +{
> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
> +
> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
> +	i_dev->suspended = true;
> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
> +
> +	return dw_i2c_plat_runtime_suspend(dev);
> +}
> +
> +static int dw_i2c_plat_runtime_resume(struct device *dev)
>  {
>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>  
> @@ -391,7 +400,19 @@ static int dw_i2c_plat_resume(struct device *dev)
>  		i2c_dw_prepare_clk(i_dev, true);
>  
>  	i_dev->init(i_dev);
> +
> +	return 0;
> +}
> +
> +static int dw_i2c_plat_resume(struct device *dev)
> +{
> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
> +
> +	dw_i2c_plat_runtime_resume(dev);
> +
> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  	i_dev->suspended = false;
> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>  
>  	return 0;
>  }
> @@ -400,7 +421,7 @@ static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
>  	.prepare = dw_i2c_plat_prepare,
>  	.complete = dw_i2c_plat_complete,
>  	SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
> -	SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
> +	SET_RUNTIME_PM_OPS(dw_i2c_plat_runtime_suspend, dw_i2c_plat_runtime_resume, NULL)
>  };
>  
>  #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
> -- 
> 2.35.1
> 

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag
  2022-02-23 14:52 ` [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Andy Shevchenko
@ 2022-02-23 15:15   ` Hans de Goede
  2022-02-24 13:02     ` Jarkko Nikula
  0 siblings, 1 reply; 8+ messages in thread
From: Hans de Goede @ 2022-02-23 15:15 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jarkko Nikula, Wolfram Sang, Andy Shevchenko, Mika Westerberg, linux-i2c

Hi Andy,

On 2/23/22 15:52, Andy Shevchenko wrote:
> On Wed, Feb 23, 2022 at 02:48:38PM +0100, Hans de Goede wrote:
>> Lock the adapter while setting the suspended flag, to ensure that other
>> locked code always sees the change immediately, rather then possibly using
>> a stale value.
>>
>> This involves splitting the suspend/resume callbacks into separate runtime
>> and normal suspend/resume calls. This is necessary because i2c_dw_xfer()
>> will get called by the i2c-core with the adapter locked and it in turn
>> calls the runtime-resume callback through pm_runtime_get_sync().
>>
>> So the runtime versions of the suspend/resume callbacks cannot take
>> the adapter-lock. Note this patch simply makes the runtime suspend/resume
>> callbacks not deal with the suspended flag at all. During runtime the
>> pm_runtime_get_sync() from i2c_dw_xfer() will always ensure that the
>> adapter is resumed when necessary.
>>
>> The suspended flag check is only necessary to check proper suspend/resume
>> ordering during normal suspend/resume which makes the pm_runtime_get_sync()
>> call a no-op.
> 
> Both patches look good to me and seems like a nice clean up. Not sure if the
> first one should be backported (and hence have a Fixes tag).
> 
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Thank you for the review. As for adding a Fixes: tag, this was inspired
by an actual bug-report, but it did not help in fixing the bug, so I left
the fixes tag out.

If you or Wolfram still want to add a Fixes tag, then it should be:

Fixes: 275154155538  ("i2c: designware: Do not allow i2c_dw_xfer() calls while suspended")

Regards,

Hans




> 
>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>> ---
>>  drivers/i2c/busses/i2c-designware-pcidrv.c  | 31 +++++++++++++++++----
>>  drivers/i2c/busses/i2c-designware-platdrv.c | 31 +++++++++++++++++----
>>  2 files changed, 52 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
>> index ef4250f8852b..9553d7075223 100644
>> --- a/drivers/i2c/busses/i2c-designware-pcidrv.c
>> +++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
>> @@ -213,14 +213,30 @@ static struct dw_pci_controller dw_pci_controllers[] = {
>>  	},
>>  };
>>  
>> +static int __maybe_unused i2c_dw_pci_runtime_suspend(struct device *dev)
>> +{
>> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>> +
>> +	i_dev->disable(i_dev);
>> +	return 0;
>> +}
>> +
>>  static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
>>  {
>>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>>  
>> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  	i_dev->suspended = true;
>> -	i_dev->disable(i_dev);
>> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  
>> -	return 0;
>> +	return i2c_dw_pci_runtime_suspend(dev);
>> +}
>> +
>> +static int __maybe_unused i2c_dw_pci_runtime_resume(struct device *dev)
>> +{
>> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>> +
>> +	return i_dev->init(i_dev);
>>  }
>>  
>>  static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
>> @@ -228,14 +244,19 @@ static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
>>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>>  	int ret;
>>  
>> -	ret = i_dev->init(i_dev);
>> +	ret = i2c_dw_pci_runtime_resume(dev);
>> +
>> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  	i_dev->suspended = false;
>> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  
>>  	return ret;
>>  }
>>  
>> -static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
>> -			    i2c_dw_pci_resume, NULL);
>> +static const struct dev_pm_ops i2c_dw_pm_ops = {
>> +	SET_SYSTEM_SLEEP_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume)
>> +	SET_RUNTIME_PM_OPS(i2c_dw_pci_runtime_suspend, i2c_dw_pci_runtime_resume, NULL)
>> +};
>>  
>>  static int i2c_dw_pci_probe(struct pci_dev *pdev,
>>  			    const struct pci_device_id *id)
>> diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
>> index 2bd81abc86f6..8e45f65bab73 100644
>> --- a/drivers/i2c/busses/i2c-designware-platdrv.c
>> +++ b/drivers/i2c/busses/i2c-designware-platdrv.c
>> @@ -368,12 +368,10 @@ static void dw_i2c_plat_complete(struct device *dev)
>>  #endif
>>  
>>  #ifdef CONFIG_PM
>> -static int dw_i2c_plat_suspend(struct device *dev)
>> +static int dw_i2c_plat_runtime_suspend(struct device *dev)
>>  {
>>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>>  
>> -	i_dev->suspended = true;
>> -
>>  	if (i_dev->shared_with_punit)
>>  		return 0;
>>  
>> @@ -383,7 +381,18 @@ static int dw_i2c_plat_suspend(struct device *dev)
>>  	return 0;
>>  }
>>  
>> -static int dw_i2c_plat_resume(struct device *dev)
>> +static int dw_i2c_plat_suspend(struct device *dev)
>> +{
>> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>> +
>> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>> +	i_dev->suspended = true;
>> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>> +
>> +	return dw_i2c_plat_runtime_suspend(dev);
>> +}
>> +
>> +static int dw_i2c_plat_runtime_resume(struct device *dev)
>>  {
>>  	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>>  
>> @@ -391,7 +400,19 @@ static int dw_i2c_plat_resume(struct device *dev)
>>  		i2c_dw_prepare_clk(i_dev, true);
>>  
>>  	i_dev->init(i_dev);
>> +
>> +	return 0;
>> +}
>> +
>> +static int dw_i2c_plat_resume(struct device *dev)
>> +{
>> +	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
>> +
>> +	dw_i2c_plat_runtime_resume(dev);
>> +
>> +	i2c_lock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  	i_dev->suspended = false;
>> +	i2c_unlock_bus(&i_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
>>  
>>  	return 0;
>>  }
>> @@ -400,7 +421,7 @@ static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
>>  	.prepare = dw_i2c_plat_prepare,
>>  	.complete = dw_i2c_plat_complete,
>>  	SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
>> -	SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
>> +	SET_RUNTIME_PM_OPS(dw_i2c_plat_runtime_suspend, dw_i2c_plat_runtime_resume, NULL)
>>  };
>>  
>>  #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
>> -- 
>> 2.35.1
>>
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag
  2022-02-23 15:15   ` Hans de Goede
@ 2022-02-24 13:02     ` Jarkko Nikula
  0 siblings, 0 replies; 8+ messages in thread
From: Jarkko Nikula @ 2022-02-24 13:02 UTC (permalink / raw)
  To: Hans de Goede, Andy Shevchenko
  Cc: Wolfram Sang, Andy Shevchenko, Mika Westerberg, linux-i2c

On 2/23/22 17:15, Hans de Goede wrote:
> Hi Andy,
> 
> On 2/23/22 15:52, Andy Shevchenko wrote:
>> On Wed, Feb 23, 2022 at 02:48:38PM +0100, Hans de Goede wrote:
>>> Lock the adapter while setting the suspended flag, to ensure that other
>>> locked code always sees the change immediately, rather then possibly using
>>> a stale value.
>>>
>>> This involves splitting the suspend/resume callbacks into separate runtime
>>> and normal suspend/resume calls. This is necessary because i2c_dw_xfer()
>>> will get called by the i2c-core with the adapter locked and it in turn
>>> calls the runtime-resume callback through pm_runtime_get_sync().
>>>
>>> So the runtime versions of the suspend/resume callbacks cannot take
>>> the adapter-lock. Note this patch simply makes the runtime suspend/resume
>>> callbacks not deal with the suspended flag at all. During runtime the
>>> pm_runtime_get_sync() from i2c_dw_xfer() will always ensure that the
>>> adapter is resumed when necessary.
>>>
>>> The suspended flag check is only necessary to check proper suspend/resume
>>> ordering during normal suspend/resume which makes the pm_runtime_get_sync()
>>> call a no-op.
>>
>> Both patches look good to me and seems like a nice clean up. Not sure if the
>> first one should be backported (and hence have a Fixes tag).
>>
>> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> Thank you for the review. As for adding a Fixes: tag, this was inspired
> by an actual bug-report, but it did not help in fixing the bug, so I left
> the fixes tag out.
> 
> If you or Wolfram still want to add a Fixes tag, then it should be:
> 
> Fixes: 275154155538  ("i2c: designware: Do not allow i2c_dw_xfer() calls while suspended")
> 
Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers
  2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
@ 2022-02-24 13:03   ` Jarkko Nikula
  2022-03-01 15:33   ` Wolfram Sang
  1 sibling, 0 replies; 8+ messages in thread
From: Jarkko Nikula @ 2022-02-24 13:03 UTC (permalink / raw)
  To: Hans de Goede, Wolfram Sang, Andy Shevchenko, Mika Westerberg; +Cc: linux-i2c

On 2/23/22 15:48, Hans de Goede wrote:
> Use the i2c_mark_adapter_suspended/resumed() i2c-core helpers and rely
> on the i2c-core's suspended checking instead of using DIY code.
> 
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>   drivers/i2c/busses/i2c-designware-core.h    | 2 --
>   drivers/i2c/busses/i2c-designware-master.c  | 5 -----
>   drivers/i2c/busses/i2c-designware-pcidrv.c  | 8 ++------
>   drivers/i2c/busses/i2c-designware-platdrv.c | 9 ++-------
>   4 files changed, 4 insertions(+), 20 deletions(-)
> 
I think Andy's Reviewed-by for 1/2 holds here too: "Both patches look 
good to me and seems like a nice clean up".

Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag
  2022-02-23 13:48 [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Hans de Goede
  2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
  2022-02-23 14:52 ` [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Andy Shevchenko
@ 2022-03-01 15:32 ` Wolfram Sang
  2 siblings, 0 replies; 8+ messages in thread
From: Wolfram Sang @ 2022-03-01 15:32 UTC (permalink / raw)
  To: Hans de Goede; +Cc: Jarkko Nikula, Andy Shevchenko, Mika Westerberg, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 1196 bytes --]

On Wed, Feb 23, 2022 at 02:48:38PM +0100, Hans de Goede wrote:
> Lock the adapter while setting the suspended flag, to ensure that other
> locked code always sees the change immediately, rather then possibly using
> a stale value.
> 
> This involves splitting the suspend/resume callbacks into separate runtime
> and normal suspend/resume calls. This is necessary because i2c_dw_xfer()
> will get called by the i2c-core with the adapter locked and it in turn
> calls the runtime-resume callback through pm_runtime_get_sync().
> 
> So the runtime versions of the suspend/resume callbacks cannot take
> the adapter-lock. Note this patch simply makes the runtime suspend/resume
> callbacks not deal with the suspended flag at all. During runtime the
> pm_runtime_get_sync() from i2c_dw_xfer() will always ensure that the
> adapter is resumed when necessary.
> 
> The suspended flag check is only necessary to check proper suspend/resume
> ordering during normal suspend/resume which makes the pm_runtime_get_sync()
> call a no-op.
> 
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>

Applied to for-next, thanks! We can still backport if we find out we
really need it...

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers
  2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
  2022-02-24 13:03   ` Jarkko Nikula
@ 2022-03-01 15:33   ` Wolfram Sang
  1 sibling, 0 replies; 8+ messages in thread
From: Wolfram Sang @ 2022-03-01 15:33 UTC (permalink / raw)
  To: Hans de Goede; +Cc: Jarkko Nikula, Andy Shevchenko, Mika Westerberg, linux-i2c

[-- Attachment #1: Type: text/plain, Size: 355 bytes --]

On Wed, Feb 23, 2022 at 02:48:39PM +0100, Hans de Goede wrote:
> Use the i2c_mark_adapter_suspended/resumed() i2c-core helpers and rely
> on the i2c-core's suspended checking instead of using DIY code.
> 
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>

Applied to for-next, thanks! Nice to see the core helpers getting used
more widely :)


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-03-01 15:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-23 13:48 [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Hans de Goede
2022-02-23 13:48 ` [PATCH 2/2] i2c: designware: Use the i2c_mark_adapter_suspended/resumed() helpers Hans de Goede
2022-02-24 13:03   ` Jarkko Nikula
2022-03-01 15:33   ` Wolfram Sang
2022-02-23 14:52 ` [PATCH 1/2] i2c: designware: Lock the adapter while setting the suspended flag Andy Shevchenko
2022-02-23 15:15   ` Hans de Goede
2022-02-24 13:02     ` Jarkko Nikula
2022-03-01 15:32 ` Wolfram Sang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.