pwm: imx27: fix race condition .apply,.get_state
Subsystems (2)
PATCH v2 1/1 pwm: imx27: fix race condition .apply,.get_state
From: Leif Middelschulte <[email protected]>
With CONFIG_PWM_DEBUG=y after writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As the PWM_DEBUG code applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.
Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
--- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)
drivers/pwm/pwm-imx27.c +47 -3
@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe
@@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC ); +int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return ; +msleep (period_ms); + } +dev_warn (dev,"FIFO has been refilled concurrently\n" ); +} +static int pwm_imx27_get_state (struct pwm_chip *chip, -struct pwm_device *pwm,struct pwm_state *state) +struct pwm_device *pwm,struct pwm_state *state) {struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);u32 period, prescaler, pwm_clk, val;
@@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"! +* Finally: +* "The PWM will run at the last set duty-cycle setting if all the +* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is +* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl. +*/ +pwm_imx27_wait_fifo_empty (chip, pwm); val =readl (imx->mmio_base +MX3_PWMSAR ); -else + }else { val = imx->duty_cycle; + } tmp =NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle =DIV_ROUND_UP_ULL (tmp, pwm_clk);
Signature · 1 line
2.39.1
Quoted 36 lines · Leif Middelschulte
From: Leif Middelschulte <[email protected]>
With
CONFIG_PWM_DEBUG=yafter writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As thePWM_DEBUGcode applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
Signed-off-by Leif Middelschulte <[email protected]>--- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)drivers/pwm/pwm-imx27.c +1
-0@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 127
Quoted 9 lines
drivers/pwm/pwm-imx27.c +6
-0@@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC );
Please don't use pwm_get_period. This one is intended for PWM consumers only. Directly using pwm->state.period is fine however.
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 137
Quoted 10 lines
drivers/pwm/pwm-imx27.c +10
-0+int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return ; +msleep (period_ms);
This is a rather long sleep. Maybe check in each iteration that fifoav decreases as expected?
Quoted 28 lines
drivers/pwm/pwm-imx27.c +18
-2+ } +dev_warn (dev,"FIFO has been refilled concurrently\n" ); +} +static int pwm_imx27_get_state (struct pwm_chip *chip, -struct pwm_device *pwm,struct pwm_state *state) +struct pwm_device *pwm,struct pwm_state *state) {struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);u32 period, prescaler, pwm_clk, val;@@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"! +* Finally: +* "The PWM will run at the last set duty-cycle setting if all the
drivers/pwm/pwm-imx27.c static int pwm_imx27_get_state(struct pwm_chip *chip, · line 207
Quoted 10 lines
drivers/pwm/pwm-imx27.c +10
-0+* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is +* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl. +*/ +pwm_imx27_wait_fifo_empty (chip, pwm);
Instead of issuing a warning, I'd return an error code if wait_fifo_empty fails.
drivers/pwm/pwm-imx27.c static int pwm_imx27_get_state(struct pwm_chip *chip, · line 214
Quoted 8 lines
drivers/pwm/pwm-imx27.c +2
-1val =readl (imx->mmio_base +MX3_PWMSAR ); - else + }else { val = imx->duty_cycle; + } tmp =NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle =DIV_ROUND_UP_ULL (tmp, pwm_clk);
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 1/4 pwm: imx27: fix race condition .apply,.get_state
From: Leif Middelschulte <[email protected]>
With CONFIG_PWM_DEBUG=y after writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As the PWM_DEBUG code applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.
Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
--- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)
drivers/pwm/pwm-imx27.c +47 -3
@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe
@@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC ); +int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return ; +msleep (period_ms); + } +dev_warn (dev,"FIFO has been refilled concurrently\n" ); +} +static int pwm_imx27_get_state (struct pwm_chip *chip, -struct pwm_device *pwm,struct pwm_state *state) +struct pwm_device *pwm,struct pwm_state *state) {struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);u32 period, prescaler, pwm_clk, val;
@@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"! +* Finally: +* "The PWM will run at the last set duty-cycle setting if all the +* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is +* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl. +*/ +pwm_imx27_wait_fifo_empty (chip, pwm); val =readl (imx->mmio_base +MX3_PWMSAR ); -else + }else { val = imx->duty_cycle; + } tmp =NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle =DIV_ROUND_UP_ULL (tmp, pwm_clk);
Signature · 7 lines
2.39.2 (Apple Git-143) _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 2/4 pwm: imx27: avoid PWM consumer API
Access struct members directly as adviced[0].
[0] https://lore.kernel.org/lkml/[email protected]/T/#ec9560c1f613d9c0d7b77d72ad9051768812f80db
--- drivers/pwm/pwm-imx27.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pwm/pwm-imx27.c +2 -2
@@ -124,7 +124,7 @@ static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, {struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);struct device *dev = chip->dev; -unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC ); +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ;int fifoav;u32 sr;
@@ -248,7 +248,7 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, sr =readl (imx->mmio_base +MX3_PWMSR ); fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_4WORDS ) { - period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm), + period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );msleep (period_ms);
Signature · 7 lines
2.39.2 (Apple Git-143) _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 2/4] pwm: imx27: avoid PWM consumer API
Quoted 5 lines · Leif Middelschulte
Access struct members directly as adviced[0].
[0] https://lore.kernel.org/lkml/[email protected]/T/#ec9560c1f613d9c0d7b77d72ad9051768812f80db
Signed-off-by Leif Middelschulte <[email protected]>
Thanks,
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH v3 3/4 pwm: imx27: verify decreasing PWM FIFO av value
In reply to Leif MiddelschulteAvoid unnecessary sleeps as adviced[0], if the FIFO cannot be emptied.
[0] https://lore.kernel.org/lkml/[email protected]/T/#ec9560c1f613d9c0d7b77d72ad9051768812f80db
--- drivers/pwm/pwm-imx27.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pwm/pwm-imx27.c +5 -1
@@ -126,7 +126,7 @@ static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip,struct device *dev = chip->dev;unsigned int period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; -int fifoav; +int fifoav, previous_fifoav =INT_MAX ;u32 sr;while (tries--) {
@@ -134,6 +134,10 @@ static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY )return ; +/* if the FIFO value does not decrease, there is another problem */ +if (previous_fifoav == fifoav) +break ; + previous_fifoav = fifoav;msleep (period_ms); }dev_warn (dev,"FIFO has been refilled concurrently\n" );
Signature · 7 lines
2.39.2 (Apple Git-143) _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 4/4 pwm: imx27: return error, if clean PWM setup fails
In reply to Leif MiddelschulteInstead of issuing a warning, return an error (as adviced[0]), if the FIFO cannot be cleanly set up.
[0] https://lore.kernel.org/lkml/[email protected]/T/#ec9560c1f613d9c0d7b77d72ad9051768812f80db
--- drivers/pwm/pwm-imx27.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/pwm/pwm-imx27.c +6 -3
@@ -133,14 +133,15 @@ static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, sr =readl (imx->mmio_base +MX3_PWMSR ); fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) -return ; +return 0 ;/* if the FIFO value does not decrease, there is another problem */ if (previous_fifoav == fifoav)break ; previous_fifoav = fifoav;msleep (period_ms); } -dev_warn (dev,"FIFO has been refilled concurrently\n" ); + +return -EAGAIN ; } static int pwm_imx27_get_state(struct pwm_chip *chip,
@@ -208,7 +209,9 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* been supplied and a period expired between the call of the wait * function and the subsequent readl. */ -pwm_imx27_wait_fifo_empty (chip, pwm); + ret =pwm_imx27_wait_fifo_empty (chip, pwm); +if (ret) +return ret; val =readl (imx->mmio_base +MX3_PWMSAR ); }else { val = imx->duty_cycle;
Signature · 7 lines
2.39.2 (Apple Git-143) _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 4/4] pwm: imx27: return error, if clean PWM setup fails
Quoted 33 lines · Leif Middelschulte
Instead of issuing a warning, return an error (as adviced[0]), if the FIFO cannot be cleanly set up.
[0] https://lore.kernel.org/lkml/[email protected]/T/#ec9560c1f613d9c0d7b77d72ad9051768812f80db
Signed-off-by Leif Middelschulte <[email protected]>--- drivers/pwm/pwm-imx27.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)drivers/pwm/pwm-imx27.c +3
-2@@ -133,14 +133,15 @@ static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, sr =readl (imx->mmio_base +MX3_PWMSR ); fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) -return ; +return 0 ;/* if the FIFO value does not decrease, there is another problem */ if (previous_fifoav == fifoav)break ; previous_fifoav = fifoav;msleep (period_ms); } -dev_warn (dev,"FIFO has been refilled concurrently\n" ); + +return -EAGAIN ; } static int pwm_imx27_get_state(struct pwm_chip *chip,
drivers/pwm/pwm-imx27.c static int pwm_imx27_get_state(struct pwm_chip *chip, · line 217
Quoted 10 lines
drivers/pwm/pwm-imx27.c +3
-1@@ -208,7 +209,9 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* been supplied and a period expired between the call of the wait * function and the subsequent readl. */ -pwm_imx27_wait_fifo_empty (chip, pwm); + ret =pwm_imx27_wait_fifo_empty (chip, pwm); +if (ret) +return ret; val =readl (imx->mmio_base +MX3_PWMSAR ); }else { val = imx->duty_cycle;
I'd squash this into the patch introducing pwm_imx27_wait_fifo_empty, too.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Quoted 52 lines · Leif Middelschulte
From: Leif Middelschulte <[email protected]>
With
CONFIG_PWM_DEBUG=yafter writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As thePWM_DEBUGcode applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
Signed-off-by Leif Middelschulte <[email protected]>--- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)drivers/pwm/pwm-imx27.c +14
-0@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe@@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC ); +int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 143
Quoted 10 lines
drivers/pwm/pwm-imx27.c +8
-1+if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return ; +msleep (period_ms); + } +dev_warn (dev,"FIFO has been refilled concurrently\n" ); +} +static int pwm_imx27_get_state (struct pwm_chip *chip, -struct pwm_device *pwm,struct pwm_state *state) +struct pwm_device *pwm,struct pwm_state *state)
This looks wrong. Aligning at the opening ( was right.
Quoted 19 lines
drivers/pwm/pwm-imx27.c +11
-1{struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);u32 period, prescaler, pwm_clk, val;@@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"!
drivers/pwm/pwm-imx27.c static int pwm_imx27_get_state(struct pwm_chip *chip, · line 205
Quoted 10 lines
drivers/pwm/pwm-imx27.c +10
-0+* Finally: +* "The PWM will run at the last set duty-cycle setting if all the +* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is +* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl.
this would only happen if there are concurrent calls into the driver, wouldn't it? I think it's safe to assume this doesn't happen.
Patch 3 of this series improves the function that is only introduced here. I suggest to squash these together.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Quoted 60 lines
Am 06.09.2023 um 17:42 schrieb Uwe Kleine-König <[email protected]>:
Quoted 56 lines · Leif Middelschulte
From: Leif Middelschulte <[email protected]>
With
CONFIG_PWM_DEBUG=yafter writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As thePWM_DEBUGcode applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
Signed-off-by Leif Middelschulte <[email protected]>---
drivers/pwm/pwm-imx27.c| 50 ++++++++++++++++++++++++++++++++++++++---1 file changed, 47 insertions(+), 3 deletions(-)drivers/pwm/pwm-imx27.c +18
-0@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe@@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm),NSEC_PER_MSEC ); +int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return ; +msleep (period_ms); + }
Quoted 8 lines
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 143
Quoted 6 lines
drivers/pwm/pwm-imx27.c +4
-1+dev_warn (dev,"FIFO has been refilled concurrently\n" ); +} +static int pwm_imx27_get_state (struct pwm_chip *chip, -struct pwm_device *pwm,struct pwm_state *state) +struct pwm_device *pwm,struct pwm_state *state)This looks wrong. Aligning at the opening ( was right.
Good catch. That change as unintentional.
Quoted 25 lines
Quoted 25 lines
drivers/pwm/pwm-imx27.c +16
-1{struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip);u32 period, prescaler, pwm_clk, val;@@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"! +* Finally: +* "The PWM will run at the last set duty-cycle setting if all the +* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is
Quoted 8 lines
drivers/pwm/pwm-imx27.c static int pwm_imx27_get_state(struct pwm_chip *chip, · line 205
Quoted 5 lines
drivers/pwm/pwm-imx27.c +5
-0+* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl. this would only happen if there are concurrent calls into the driver, wouldn't it? I think it's safe to assume this doesn't happen.
This assessment seems correct at them moment. This comment is supposed to be a source of explanation to future developers if - for some reason - the driver/subsystem is changed and they trip over this behaviour (again), just as I did.
Quoted 3 lines
Patch 3 of this series improves the function that is only introduced here. I suggest to squash these together.
I will provide v4 with the changes squashed into the initial commit, that introduced the function.
Quoted 7 lines
Best regards
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Thank you,
Leif
PATCH v4 1/2 pwm: imx27: fix race condition .apply,.get_state
In reply to Uwe Kleine-KönigFrom: Leif Middelschulte <[email protected]>
With CONFIG_PWM_DEBUG=y after writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As the PWM_DEBUG code applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.
Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
--- drivers/pwm/pwm-imx27.c | 55 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)
drivers/pwm/pwm-imx27.c +53 -2
@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe
@@ -118,6 +119,31 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC ); +int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav, previous_fifoav =INT_MAX ; +u32 sr; + +while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return 0 ; +/* if the FIFO value does not decrease, there is another problem */ +if (previous_fifoav == fifoav) +break ; + previous_fifoav = fifoav; +msleep (period_ms); + } + +return -EAGAIN ; +} +static int pwm_imx27_get_state (struct pwm_chip *chip,struct pwm_device *pwm,struct pwm_state *state) {
@@ -161,10 +187,35 @@ static int pwm_imx27_get_state(struct pwm_chip *chip,* PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ -if (state->enabled) +if (state->enabled) { +/* +* From the i.MX PWM reference manual: +* "A read on the sample register yields the current FIFO value that +* is being used, or will be used, by the PWM for generation on the +* output signal. Therefore, a write and a subsequent read on the +* sample register may result in different values being obtained." +* Furthermore: +* "When a new value is written, the duty cycle changes after the +* current period is over." +* Note "changes" vs. "changes to the given value"! +* Finally: +* "The PWM will run at the last set duty-cycle setting if all the +* values of the FIFO has been utilized, until the FIFO is reloaded +* or the PWM is disabled." +* Try to be at least a bit more deterministic about which value is +* read by waiting until the FIFO is empty. In this state the last/most +* recently pushed sample (duty cycle) value is continuously applied. +* Beware that this approach is still racy, as a new value could have +* been supplied and a period expired between the call of the wait +* function and the subsequent readl. +*/ + ret =pwm_imx27_wait_fifo_empty (chip, pwm); +if (ret) +return ret; val =readl (imx->mmio_base +MX3_PWMSAR ); -else + }else { val = imx->duty_cycle; + } tmp =NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle =DIV_ROUND_UP_ULL (tmp, pwm_clk);
Signature · 1 line
2.39.3 (Apple Git-145)
PATCH v4 2/2 pwm: imx27: avoid PWM consumer API
Access struct members directly.
--- drivers/pwm/pwm-imx27.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-imx27.c +1 -1
@@ -255,7 +255,7 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, sr =readl (imx->mmio_base +MX3_PWMSR ); fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_4WORDS ) { - period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm), + period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );msleep (period_ms);
Signature · 1 line
2.39.3 (Apple Git-145)
Re: [PATCH v4 2/2] pwm: imx27: avoid PWM consumer API
Hello Leif,
drivers/pwm/pwm-imx27.c static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, · line 260
Quoted 12 lines · Leif Middelschulte
drivers/pwm/pwm-imx27.c +1
-1@@ -255,7 +255,7 @@ static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, sr =readl (imx->mmio_base +MX3_PWMSR ); fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr);if (fifoav ==MX3_PWMSR_FIFOAV_4WORDS ) { - period_ms =DIV_ROUND_UP_ULL (pwm_get_period (pwm), + period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );msleep (period_ms);
This doesn't apply anymore on top of pwm/for-next. See commit 1706175c682fdb3fbe0b70d50b137aaa270959db which includes this change (and more).
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Hello Leif,
thanks for this new round addressing the identified issues.
Quoted 28 lines · Leif Middelschulte
From: Leif Middelschulte <[email protected]>
With
CONFIG_PWM_DEBUG=yafter writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As thePWM_DEBUGcode applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
Signed-off-by Leif Middelschulte <[email protected]>--- drivers/pwm/pwm-imx27.c | 55 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)drivers/pwm/pwm-imx27.c +0
-0
drivers/pwm/pwm-imx27.c line 78
Quoted 4 lines
drivers/pwm/pwm-imx27.c +1
-0@@ -75,6 +75,7 @@ (x)) + 1)#define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4
This looks like a register definition, but it's only a number that defines the iterations waiting for the FIFO to empty. (The same critic applies to MX3_PWM_SWR_LOOP, though.)
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 127
Quoted 12 lines
drivers/pwm/pwm-imx27.c +6
-0/* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe@@ -118,6 +119,31 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)clk_disable_unprepare (imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty (struct pwm_chip *chip, +struct pwm_device *pwm) +{ +struct pwm_imx27_chip *imx =to_pwm_imx27_chip (chip); +struct device *dev = chip->dev; +unsigned int period_ms =DIV_ROUND_UP_ULL (pwm->state.period,NSEC_PER_MSEC );
Given that waiting here is unfortunate it would be nice so reduce the waiting as much as possible. So it might make sense to read the actual period from the hardware and use that as it might be smaller that pwm->state.period.
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 130
Quoted 3 lines
drivers/pwm/pwm-imx27.c +3
-0+int tries =MX3_PWM_FIFOAV_EMPTY_LOOP ; +int fifoav, previous_fifoav =INT_MAX ; +u32 sr;
Most variables can go into the while loop.
drivers/pwm/pwm-imx27.c static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) · line 142
Quoted 11 lines
drivers/pwm/pwm-imx27.c +11
-0+while (tries--) { + sr =readl (imx->mmio_base +MX3_PWMSR ); + fifoav =FIELD_GET (MX3_PWMSR_FIFOAV , sr); +if (fifoav ==MX3_PWMSR_FIFOAV_EMPTY ) +return 0 ; +/* if the FIFO value does not decrease, there is another problem */ +if (previous_fifoav == fifoav) +break ; + previous_fifoav = fifoav; +msleep (period_ms); + }
I wonder if a loop is necessary at all. Why not use msleep(FIELD_GET(MX3_PWMSR_FIFOAV, sr) * period_ms)?
Maybe take PWMCNR into account to shorten the sleep a bit.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Hello,
Quoted 18 lines · Leif Middelschulte
From: Leif Middelschulte <[email protected]>
With
CONFIG_PWM_DEBUG=yafter writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As thePWM_DEBUGcode applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state.Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register.
Quoted 1 line
Signed-off-by Leif Middelschulte <[email protected]>
Another few things I noticed only after replying to this patch and trying to apply #2:
- Please make sure you have a S-o-b line for the sender (i.e. you with your gmail identity).
- My reply to your klsmartin.com address couldn't be delivered, The MS mailserver told: "leif.middelschulte wasn't found at klsmartin.com."
- Please start a new thread if you send a v5, as applying patches from a thread containing many patches is a bit less trivial.
- Consider using git format-patch's --base parameter to document your patch base.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |