diff mbox series

[v3,04/15] pwm: lpss: Add range limit check for the base_unit register value

Message ID 20200620121758.14836-5-hdegoede@redhat.com
State Superseded
Headers show
Series acpi/pwm/i915: Convert pwm-crc and i915 driver's PWM code to use the atomic PWM API | expand

Commit Message

Hans de Goede June 20, 2020, 12:17 p.m. UTC
When the user requests a high enough period ns value, then the
calculations in pwm_lpss_prepare() might result in a base_unit value of 0.

But according to the data-sheet the way the PWM controller works is that
each input clock-cycle the base_unit gets added to a N bit counter and
that counter overflowing determines the PWM output frequency. Adding 0
to the counter is a no-op. The data-sheet even explicitly states that
writing 0 to the base_unit bits will result in the PWM outputting a
continuous 0 signal.

When the user requestes a low enough period ns value, then the
calculations in pwm_lpss_prepare() might result in a base_unit value
which is bigger then base_unit_range - 1. Currently the codes for this
deals with this by applying a mask:

	base_unit &= (base_unit_range - 1);

But this means that we let the value overflow the range, we throw away the
higher bits and store whatever value is left in the lower bits into the
register leading to a random output frequency, rather then clamping the
output frequency to the highest frequency which the hardware can do.

This commit fixes both issues by clamping the base_unit value to be
between 1 and (base_unit_range - 1).

Fixes: 684309e5043e ("pwm: lpss: Avoid potential overflow of base_unit")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
Changes in v3:
- Change upper limit of clamp to (base_unit_range - 1)
- Add Fixes tag
---
 drivers/pwm/pwm-lpss.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Hans de Goede July 7, 2020, 8:04 a.m. UTC | #1
Hi,

On 7/7/20 9:34 AM, Uwe Kleine-König wrote:
> On Mon, Jul 06, 2020 at 10:53:08PM +0200, Hans de Goede wrote:

>> Hi,

>>

>> Thank you for your review and sorry for the slow reply.

> 

> No problem for me, I didn't hold my breath :-)

>   

>>>> diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c

>>>> index 43b1fc634af1..80d0f9c64f9d 100644

>>>> --- a/drivers/pwm/pwm-lpss.c

>>>> +++ b/drivers/pwm/pwm-lpss.c

>>>> @@ -97,6 +97,9 @@ static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,

>>>>    	freq *= base_unit_range;

>>>>    	base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);

>>>

>>> DIV_ROUND_CLOSEST_ULL is most probably wrong, too. But I didn't spend

>>> the time to actually confirm that.

>>

>> Yes I saw your comment elsewhere that the PWM API defines rounding

>> in a certain direction, but fixing that falls outside of this patch.

> 

> Yeah, sure.

> 

>> [...]

>> I hope this helps to explain what is going on a bit.

> 

> I will try to make sense of that and reply to the patch directly when I

> succeeded.


In case it helps here is the datasheet for the LPSS PWM controller
(somewhat hard to find if you don't know what you are looking for):

https://cdrdv2.intel.com/v1/dl/getcontent/332065
https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/atom-z8000-datasheet-vol-2.pdf

The first link contains a description about how the PWM controller works in
section 17.5  "SIO - Pulse Width Modulation (PWM)", the second link contains
all register definitions for the SoC and is not all that interesting other
then for verifying the existing register bits defines.

Regards,

Hans



> 

>> ###

>>

>> As for the behavior on base_unit==0 in the get_state method,

>> as mentioned above I wrote that when I did not fully understood

>> how the controller works.

>>

>> We really should never encounter this.

>>

>> But if we do then I think closest to the truth would be:

>>

>> state->period     = UINT_MAX;

>> state->duty_cycle = 0;

> 

> I'd say state->period = 1 & state->duty_cycle = 0 is a better

> representation.

> 

> Best regards

> Uwe

>
Hans de Goede July 7, 2020, 5:31 p.m. UTC | #2
Hi,

On 7/7/20 9:34 AM, Uwe Kleine-König wrote:
> On Mon, Jul 06, 2020 at 10:53:08PM +0200, Hans de Goede wrote:

>> Hi,

>>

>> Thank you for your review and sorry for the slow reply.

> 

> No problem for me, I didn't hold my breath :-)

>   

>>>> diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c

>>>> index 43b1fc634af1..80d0f9c64f9d 100644

>>>> --- a/drivers/pwm/pwm-lpss.c

>>>> +++ b/drivers/pwm/pwm-lpss.c

>>>> @@ -97,6 +97,9 @@ static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,

>>>>    	freq *= base_unit_range;

>>>>    	base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);

>>>

>>> DIV_ROUND_CLOSEST_ULL is most probably wrong, too. But I didn't spend

>>> the time to actually confirm that.

>>

>> Yes I saw your comment elsewhere that the PWM API defines rounding

>> in a certain direction, but fixing that falls outside of this patch.

> 

> Yeah, sure.

> 

>> [...]

>> I hope this helps to explain what is going on a bit.

> 

> I will try to make sense of that and reply to the patch directly when I

> succeeded.

> 

>> ###

>>

>> As for the behavior on base_unit==0 in the get_state method,

>> as mentioned above I wrote that when I did not fully understood

>> how the controller works.

>>

>> We really should never encounter this.

>>

>> But if we do then I think closest to the truth would be:

>>

>> state->period     = UINT_MAX;

>> state->duty_cycle = 0;

> 

> I'd say state->period = 1 & state->duty_cycle = 0 is a better

> representation.


But that would suggest the output is configured for an
infinitely high output frequency, but the frequency is
actually 0, the reason why get_state needs to treat a
base_unit val of 0 special at all is to avoid a division
by 0, and in math dividing by 0 gives infinite, isn't
UINT_MAX a better way to represent infinity ?

Regards,

Hans
Hans de Goede July 7, 2020, 7:41 p.m. UTC | #3
Hi,

On 7/7/20 9:09 PM, Uwe Kleine-König wrote:
> Hello Hans,

> 

> On Tue, Jul 07, 2020 at 07:31:29PM +0200, Hans de Goede wrote:

>> On 7/7/20 9:34 AM, Uwe Kleine-König wrote:

>>> On Mon, Jul 06, 2020 at 10:53:08PM +0200, Hans de Goede wrote:

>>>> But if we do then I think closest to the truth would be:

>>>>

>>>> state->period     = UINT_MAX;

>>>> state->duty_cycle = 0;

>>>

>>> I'd say state->period = 1 & state->duty_cycle = 0 is a better

>>> representation.

>>

>> But that would suggest the output is configured for an

>> infinitely high output frequency, but the frequency is

>> actually 0, the reason why get_state needs to treat a

>> base_unit val of 0 special at all is to avoid a division

>> by 0, and in math dividing by 0 gives infinite, isn't

>> UINT_MAX a better way to represent infinity ?

> 

> Given that duty_cycle is 0, how can to tell anything about the period

> when only seeing the signal (= a constant low)?

> 

> Given that (ideally) a period is completed when pwm_apply_state() is

> called, a short period is much more sensible.


Ok, I will add a patch to v4 of the patch-set to adjust the pwm-lpss
driver's get_state method accordingly.

Regards,

Hans
diff mbox series

Patch

diff --git a/drivers/pwm/pwm-lpss.c b/drivers/pwm/pwm-lpss.c
index 43b1fc634af1..80d0f9c64f9d 100644
--- a/drivers/pwm/pwm-lpss.c
+++ b/drivers/pwm/pwm-lpss.c
@@ -97,6 +97,9 @@  static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,
 	freq *= base_unit_range;
 
 	base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
+	/* base_unit must not be 0 and we also want to avoid overflowing it */
+	base_unit = clamp_t(unsigned long long, base_unit, 1,
+			    base_unit_range - 1);
 
 	on_time_div = 255ULL * duty_ns;
 	do_div(on_time_div, period_ns);
@@ -105,7 +108,6 @@  static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,
 	orig_ctrl = ctrl = pwm_lpss_read(pwm);
 	ctrl &= ~PWM_ON_TIME_DIV_MASK;
 	ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
-	base_unit &= (base_unit_range - 1);
 	ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
 	ctrl |= on_time_div;