ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time [Linux 3.16.72]

This Linux kernel change "ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time" is included in the Linux 3.16.72 release. This change is authored by Kohji Okuno <okuno.kohji [at]> on Tue Feb 26 11:34:13 2019 +0900. The commit for this change in Linux stable tree is 6eef8f1 (patch) which is from upstream commit 91740fc. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 91740fc.

ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time

commit 91740fc8242b4f260cfa4d4536d8551804777fae upstream.

In the current cpuidle implementation for i.MX6q, the CPU that sets
'WAIT_UNCLOCKED' and the CPU that returns to 'WAIT_CLOCKED' are always
the same. While the CPU that sets 'WAIT_UNCLOCKED' is in IDLE state of
"WAIT", if the other CPU wakes up and enters IDLE state of "WFI"
istead of "WAIT", this CPU can not wake up at expired time.
 Because, in the case of "WFI", the CPU must be waked up by the local
timer interrupt. But, while 'WAIT_UNCLOCKED' is set, the local timer
is stopped, when all CPUs execute "wfi" instruction. As a result, the
local timer interrupt is not fired.
 In this situation, this CPU will wake up by IRQ different from local
timer. (e.g. broacast timer)

So, this fix changes CPU to return to 'WAIT_CLOCKED'.

Signed-off-by: Kohji Okuno <[email protected]>
Fixes: e5f9dec8ff5f ("ARM: imx6q: support WAIT mode using cpuidle")
Signed-off-by: Shawn Guo <[email protected]>
[bwh: Backported to 3.16: use imx6q_set_lpm() instead of imx6_set_lpm()]
Signed-off-by: Ben Hutchings <[email protected]>

There are 27 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/arm/mach-imx/cpuidle-imx6q.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/arch/arm/mach-imx/cpuidle-imx6q.c b/arch/arm/mach-imx/cpuidle-imx6q.c
index 6bcae04..e338a6d 100644
--- a/arch/arm/mach-imx/cpuidle-imx6q.c
+++ b/arch/arm/mach-imx/cpuidle-imx6q.c
@@ -14,30 +14,23 @@
 #include "common.h"
 #include "cpuidle.h"

-static atomic_t master = ATOMIC_INIT(0);
-static DEFINE_SPINLOCK(master_lock);
+static int num_idle_cpus = 0;
+static DEFINE_SPINLOCK(cpuidle_lock);

 static int imx6q_enter_wait(struct cpuidle_device *dev,
                struct cpuidle_driver *drv, int index)
-   if (atomic_inc_return(&master) == num_online_cpus()) {
-       /*
-        * With this lock, we prevent other cpu to exit and enter
-        * this function again and become the master.
-        */
-       if (!spin_trylock(&master_lock))
-           goto idle;
+   spin_lock(&cpuidle_lock);
+   if (++num_idle_cpus == num_online_cpus())
-       cpu_do_idle();
-       imx6q_set_lpm(WAIT_CLOCKED);
-       spin_unlock(&master_lock);
-       goto done;
-   }
+   spin_unlock(&cpuidle_lock);

-   atomic_dec(&master);
+   spin_lock(&cpuidle_lock);
+   if (num_idle_cpus-- == num_online_cpus())
+       imx6q_set_lpm(WAIT_CLOCKED);
+   spin_unlock(&cpuidle_lock);

    return index;

Leave a Reply

Your email address will not be published. Required fields are marked *