public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] i2c: nvidia-gpu: Handle timeout correctly in gpu_i2c_check_status()
@ 2020-03-24 13:07 Kai-Heng Feng
  2020-03-24 13:41 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Kai-Heng Feng @ 2020-03-24 13:07 UTC (permalink / raw)
  To: ajayg
  Cc: Kai-Heng Feng, Andy Shevchenko, Wolfram Sang,
	open list:I2C CONTROLLER DRIVER FOR NVIDIA GPU, open list

Nvidia card may come with a "phantom" UCSI device, and its driver gets
stuck in probe routine, prevents any system PM operations like suspend.

When the target time equals to jiffies, it's not included by
time_is_before_jiffies(). So let's use a boolean to make sure the
operation is done or timeout.

Fixes: c71bcdcb42a7 ("i2c: add i2c bus driver for NVIDIA GPU")
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
v2:
- Use a boolean to make sure the operation is timeout or not.

 drivers/i2c/busses/i2c-nvidia-gpu.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/busses/i2c-nvidia-gpu.c b/drivers/i2c/busses/i2c-nvidia-gpu.c
index 62e18b4db0ed..bdb48600af0e 100644
--- a/drivers/i2c/busses/i2c-nvidia-gpu.c
+++ b/drivers/i2c/busses/i2c-nvidia-gpu.c
@@ -77,18 +77,20 @@ static int gpu_i2c_check_status(struct gpu_i2c_dev *i2cd)
 {
 	unsigned long target = jiffies + msecs_to_jiffies(1000);
 	u32 val;
+	bool done = false;
 
 	do {
 		val = readl(i2cd->regs + I2C_MST_CNTL);
-		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER))
-			break;
-		if ((val & I2C_MST_CNTL_STATUS) !=
-				I2C_MST_CNTL_STATUS_BUS_BUSY)
+		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER)
+		    || (val & I2C_MST_CNTL_STATUS) !=
+				I2C_MST_CNTL_STATUS_BUS_BUSY) {
+			done = true;
 			break;
+		}
 		usleep_range(500, 600);
 	} while (time_is_after_jiffies(target));
 
-	if (time_is_before_jiffies(target)) {
+	if (!done) {
 		dev_err(i2cd->dev, "i2c timeout error %x\n", val);
 		return -ETIMEDOUT;
 	}
-- 
2.17.1

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

* Re: [PATCH v2] i2c: nvidia-gpu: Handle timeout correctly in gpu_i2c_check_status()
  2020-03-24 13:07 [PATCH v2] i2c: nvidia-gpu: Handle timeout correctly in gpu_i2c_check_status() Kai-Heng Feng
@ 2020-03-24 13:41 ` Andy Shevchenko
  2020-03-24 14:39   ` Kai-Heng Feng
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2020-03-24 13:41 UTC (permalink / raw)
  To: Kai-Heng Feng
  Cc: ajayg, Wolfram Sang,
	open list:I2C CONTROLLER DRIVER FOR NVIDIA GPU, open list

On Tue, Mar 24, 2020 at 09:07:12PM +0800, Kai-Heng Feng wrote:
> Nvidia card may come with a "phantom" UCSI device, and its driver gets
> stuck in probe routine, prevents any system PM operations like suspend.
> 
> When the target time equals to jiffies, it's not included by
> time_is_before_jiffies(). So let's use a boolean to make sure the
> operation is done or timeout.

Thank you for an update, my comments below.

>  	unsigned long target = jiffies + msecs_to_jiffies(1000);
>  	u32 val;
> +	bool done = false;
>  
>  	do {
>  		val = readl(i2cd->regs + I2C_MST_CNTL);
> -		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER))
> -			break;
> -		if ((val & I2C_MST_CNTL_STATUS) !=
> -				I2C_MST_CNTL_STATUS_BUS_BUSY)

> +		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER)
> +		    || (val & I2C_MST_CNTL_STATUS) !=
> +				I2C_MST_CNTL_STATUS_BUS_BUSY) {

Bad formatting. But see below.

> +			done = true;
>  			break;
> +		}
>  		usleep_range(500, 600);
>  	} while (time_is_after_jiffies(target));
>  
> -	if (time_is_before_jiffies(target)) {
> +	if (!done) {
>  		dev_err(i2cd->dev, "i2c timeout error %x\n", val);
>  		return -ETIMEDOUT;
>  	}


Overall it can use simple tries since you already have sleep inside, but
moreover, you may simple switch to readl_poll_timeout() this entire
loop.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v2] i2c: nvidia-gpu: Handle timeout correctly in gpu_i2c_check_status()
  2020-03-24 13:41 ` Andy Shevchenko
@ 2020-03-24 14:39   ` Kai-Heng Feng
  0 siblings, 0 replies; 3+ messages in thread
From: Kai-Heng Feng @ 2020-03-24 14:39 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: ajayg, Wolfram Sang,
	open list:I2C CONTROLLER DRIVER FOR NVIDIA GPU, open list

Hi Andy,

> On Mar 24, 2020, at 21:41, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> 
> On Tue, Mar 24, 2020 at 09:07:12PM +0800, Kai-Heng Feng wrote:
>> Nvidia card may come with a "phantom" UCSI device, and its driver gets
>> stuck in probe routine, prevents any system PM operations like suspend.
>> 
>> When the target time equals to jiffies, it's not included by
>> time_is_before_jiffies(). So let's use a boolean to make sure the
>> operation is done or timeout.
> 
> Thank you for an update, my comments below.
> 
>> 	unsigned long target = jiffies + msecs_to_jiffies(1000);
>> 	u32 val;
>> +	bool done = false;
>> 
>> 	do {
>> 		val = readl(i2cd->regs + I2C_MST_CNTL);
>> -		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER))
>> -			break;
>> -		if ((val & I2C_MST_CNTL_STATUS) !=
>> -				I2C_MST_CNTL_STATUS_BUS_BUSY)
> 
>> +		if (!(val & I2C_MST_CNTL_CYCLE_TRIGGER)
>> +		    || (val & I2C_MST_CNTL_STATUS) !=
>> +				I2C_MST_CNTL_STATUS_BUS_BUSY) {
> 
> Bad formatting. But see below.
> 
>> +			done = true;
>> 			break;
>> +		}
>> 		usleep_range(500, 600);
>> 	} while (time_is_after_jiffies(target));
>> 
>> -	if (time_is_before_jiffies(target)) {
>> +	if (!done) {
>> 		dev_err(i2cd->dev, "i2c timeout error %x\n", val);
>> 		return -ETIMEDOUT;
>> 	}
> 
> 
> Overall it can use simple tries since you already have sleep inside, but
> moreover, you may simple switch to readl_poll_timeout() this entire
> loop.

Yes that can make the retry loop much simpler.
I'll send a v3 based on your suggestion.

Kai-Heng

> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 
> 

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

end of thread, other threads:[~2020-03-24 14:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-24 13:07 [PATCH v2] i2c: nvidia-gpu: Handle timeout correctly in gpu_i2c_check_status() Kai-Heng Feng
2020-03-24 13:41 ` Andy Shevchenko
2020-03-24 14:39   ` Kai-Heng Feng

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