public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: dwc2: drd: fix clock gating on USB role switch
@ 2024-09-03  9:41 Tomas Marek
  2024-09-03 10:00 ` Greg KH
  2024-09-03 13:20 ` Sergey Shtylyov
  0 siblings, 2 replies; 4+ messages in thread
From: Tomas Marek @ 2024-09-03  9:41 UTC (permalink / raw)
  To: hminas, gregkh; +Cc: Arthur.Petrosyan, linux-usb, linux-kernel, oleg.karfich

The dwc2_handle_usb_suspend_intr() function disables gadget clocks in USB
peripheral mode when no other power-down mode is available (introduced by
commit 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")).
However, the dwc2_drd_role_sw_set() USB role update handler attempts to
read DWC2 registers if the USB role has changed while the USB is in suspend
mode (when the clocks are gated). This causes the system to hang.

Release gadget clocks before handling the USB role update.

Fixes: 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")

Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
---
 drivers/usb/dwc2/drd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c
index a8605b02115b..ccb33cd1f04b 100644
--- a/drivers/usb/dwc2/drd.c
+++ b/drivers/usb/dwc2/drd.c
@@ -127,6 +127,18 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
 			role = USB_ROLE_DEVICE;
 	}
 
+#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
+	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
+	if (dwc2_is_device_mode(hsotg)) {
+		if (hsotg->lx_state == DWC2_L2) {
+			if (hsotg->params.power_down ==
+			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
+			    !hsotg->params.no_clock_gating)
+				dwc2_gadget_exit_clock_gating(hsotg, 0);
+		}
+	}
+#endif
+
 	if (role == USB_ROLE_HOST) {
 		already = dwc2_ovr_avalid(hsotg, true);
 	} else if (role == USB_ROLE_DEVICE) {
-- 
2.25.1


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

* Re: [PATCH] usb: dwc2: drd: fix clock gating on USB role switch
  2024-09-03  9:41 [PATCH] usb: dwc2: drd: fix clock gating on USB role switch Tomas Marek
@ 2024-09-03 10:00 ` Greg KH
  2024-09-03 13:20 ` Sergey Shtylyov
  1 sibling, 0 replies; 4+ messages in thread
From: Greg KH @ 2024-09-03 10:00 UTC (permalink / raw)
  To: Tomas Marek
  Cc: hminas, Arthur.Petrosyan, linux-usb, linux-kernel, oleg.karfich

On Tue, Sep 03, 2024 at 11:41:56AM +0200, Tomas Marek wrote:
> The dwc2_handle_usb_suspend_intr() function disables gadget clocks in USB
> peripheral mode when no other power-down mode is available (introduced by
> commit 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")).
> However, the dwc2_drd_role_sw_set() USB role update handler attempts to
> read DWC2 registers if the USB role has changed while the USB is in suspend
> mode (when the clocks are gated). This causes the system to hang.
> 
> Release gadget clocks before handling the USB role update.
> 
> Fixes: 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")
> 
> Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
> ---
>  drivers/usb/dwc2/drd.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c
> index a8605b02115b..ccb33cd1f04b 100644
> --- a/drivers/usb/dwc2/drd.c
> +++ b/drivers/usb/dwc2/drd.c
> @@ -127,6 +127,18 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
>  			role = USB_ROLE_DEVICE;
>  	}
>  
> +#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
> +	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
> +	if (dwc2_is_device_mode(hsotg)) {
> +		if (hsotg->lx_state == DWC2_L2) {
> +			if (hsotg->params.power_down ==
> +			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
> +			    !hsotg->params.no_clock_gating)
> +				dwc2_gadget_exit_clock_gating(hsotg, 0);
> +		}
> +	}
> +#endif
> +
>  	if (role == USB_ROLE_HOST) {
>  		already = dwc2_ovr_avalid(hsotg, true);
>  	} else if (role == USB_ROLE_DEVICE) {
> -- 
> 2.25.1
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an
  older released kernel, yet you do not have a cc: stable line in the
  signed-off-by area at all, which means that the patch will not be
  applied to any older kernel releases.  To properly fix this, please
  follow the documented rules in the
  Documentation/process/stable-kernel-rules.rst file for how to resolve
  this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

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

* Re: [PATCH] usb: dwc2: drd: fix clock gating on USB role switch
  2024-09-03  9:41 [PATCH] usb: dwc2: drd: fix clock gating on USB role switch Tomas Marek
  2024-09-03 10:00 ` Greg KH
@ 2024-09-03 13:20 ` Sergey Shtylyov
  2024-09-04  5:30   ` Tomas Marek
  1 sibling, 1 reply; 4+ messages in thread
From: Sergey Shtylyov @ 2024-09-03 13:20 UTC (permalink / raw)
  To: Tomas Marek, hminas, gregkh
  Cc: Arthur.Petrosyan, linux-usb, linux-kernel, oleg.karfich

On 9/3/24 12:41 PM, Tomas Marek wrote:

> The dwc2_handle_usb_suspend_intr() function disables gadget clocks in USB
> peripheral mode when no other power-down mode is available (introduced by
> commit 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")).
> However, the dwc2_drd_role_sw_set() USB role update handler attempts to
> read DWC2 registers if the USB role has changed while the USB is in suspend
> mode (when the clocks are gated). This causes the system to hang.
> 
> Release gadget clocks before handling the USB role update.
> 
> Fixes: 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")
> 
> Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
> ---
>  drivers/usb/dwc2/drd.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c
> index a8605b02115b..ccb33cd1f04b 100644
> --- a/drivers/usb/dwc2/drd.c
> +++ b/drivers/usb/dwc2/drd.c
> @@ -127,6 +127,18 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
>  			role = USB_ROLE_DEVICE;
>  	}
>  
> +#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
> +	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
> +	if (dwc2_is_device_mode(hsotg)) {

   Why not avoid #ifdef by doing e.g.:

	if ((IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
	     IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) &&
	     dwc2_is_device_mode(hsotg)) {

> +		if (hsotg->lx_state == DWC2_L2) {
> +			if (hsotg->params.power_down ==
> +			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
> +			    !hsotg->params.no_clock_gating)

   Well, these *if*s are mergeable too... :-)

> +				dwc2_gadget_exit_clock_gating(hsotg, 0);
> +		}
> +	}
> +#endif
> +
[...]

MBR, Sergey

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

* Re: [PATCH] usb: dwc2: drd: fix clock gating on USB role switch
  2024-09-03 13:20 ` Sergey Shtylyov
@ 2024-09-04  5:30   ` Tomas Marek
  0 siblings, 0 replies; 4+ messages in thread
From: Tomas Marek @ 2024-09-04  5:30 UTC (permalink / raw)
  To: Sergey Shtylyov
  Cc: hminas, gregkh, Arthur.Petrosyan, linux-usb, linux-kernel,
	oleg.karfich

Hi Sergey,

thank you for your review.

On Tue, Sep 03, 2024 at 04:20:26PM +0300, Sergey Shtylyov wrote:
> On 9/3/24 12:41 PM, Tomas Marek wrote:
> 
> > The dwc2_handle_usb_suspend_intr() function disables gadget clocks in USB
> > peripheral mode when no other power-down mode is available (introduced by
> > commit 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")).
> > However, the dwc2_drd_role_sw_set() USB role update handler attempts to
> > read DWC2 registers if the USB role has changed while the USB is in suspend
> > mode (when the clocks are gated). This causes the system to hang.
> > 
> > Release gadget clocks before handling the USB role update.
> > 
> > Fixes: 0112b7ce68ea ("usb: dwc2: Update dwc2_handle_usb_suspend_intr function.")
> > 
> > Signed-off-by: Tomas Marek <tomas.marek@elrest.cz>
> > ---
> >  drivers/usb/dwc2/drd.c | 12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> > 
> > diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c
> > index a8605b02115b..ccb33cd1f04b 100644
> > --- a/drivers/usb/dwc2/drd.c
> > +++ b/drivers/usb/dwc2/drd.c
> > @@ -127,6 +127,18 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
> >  			role = USB_ROLE_DEVICE;
> >  	}
> >  
> > +#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
> > +	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
> > +	if (dwc2_is_device_mode(hsotg)) {
> 
>    Why not avoid #ifdef by doing e.g.:
> 
> 	if ((IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
> 	     IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) &&
> 	     dwc2_is_device_mode(hsotg)) {
> 
> > +		if (hsotg->lx_state == DWC2_L2) {
> > +			if (hsotg->params.power_down ==
> > +			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
> > +			    !hsotg->params.no_clock_gating)
> 
>    Well, these *if*s are mergeable too... :-)
> 

OK, why not. In the end all conditions could be merged into one if statement like:

	if ((IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) ||
	     IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)) &&
	     dwc2_is_device_mode(hsotg) &&
	     hsotg->lx_state == DWC2_L2 &&
	     hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
	     hsotg->bus_suspended &&
	     !hsotg->params.no_clock_gating)
		dwc2_gadget_exit_clock_gating(hsotg, 0);

I’ll submit a new patch version with this modification.

Best regards

Tomas

> > +				dwc2_gadget_exit_clock_gating(hsotg, 0);
> > +		}
> > +	}
> > +#endif
> > +
> [...]
> 
> MBR, Sergey

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

end of thread, other threads:[~2024-09-04  5:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-03  9:41 [PATCH] usb: dwc2: drd: fix clock gating on USB role switch Tomas Marek
2024-09-03 10:00 ` Greg KH
2024-09-03 13:20 ` Sergey Shtylyov
2024-09-04  5:30   ` Tomas Marek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox