Skip to contentSkip to conversation

cpuidle: add riscv cpuidle driver

PATCH Superseded liush 5 participants 9 messages Sep 14, 2020 – Sep 28, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

liush

author
#0 lore raw

PATCH 1/1 cpuidle: add riscv cpuidle driver

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
arch/riscv/Kconfig +7 -0
index df18372..c7ddb9d 100644
@@ -86,6 +86,7 @@ config RISCV
 	select SPARSE_IRQ
 	select SYSCTL_EXCEPTION_TRACE
 	select THREAD_INFO_IN_TASK
+	select CPU_IDLE
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
 	depends on RISCV_M_MODE
 	depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +7 -0
new file mode 100644
index 00000000..2599d2f
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+
+extern void cpu_do_idle(void);
+
+#endif
arch/riscv/kernel/Makefile +1 -0
index dc93710..396ba9c 100644
@@ -29,6 +29,7 @@ obj-y	+= riscv_ksyms.o
 obj-y	+= stacktrace.o
 obj-y	+= cacheinfo.o
 obj-y	+= patch.o
+obj-y	+= cpuidle.o
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE)	+= traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +8 -0
new file mode 100644
index 00000000..a3289e7
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+	__asm__ __volatile__ ("wfi");
+
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
 	tristate "Halt poll cpuidle driver"
 	depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +11 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+	select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+	  Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)		+= cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)		+= cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)		+= cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)		+= cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 6 lines · liush

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---

[ ... ]

Quoted 8 lines

obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o

arch/riscv/kernel/cpuidle.c new file +0 -0
new file mode 100644
index 00000000..a3289e7

arch/riscv/kernel/cpuidle.c line 7

Quoted 7 lines
arch/riscv/kernel/cpuidle.c new file +7 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+	__asm__ __volatile__ ("wfi");
+

extra line

arch/riscv/kernel/cpuidle.c line 8

Quoted 1 line
arch/riscv/kernel/cpuidle.c new file +1 -0
+}

As for the next deeper states should end up with the cpu_do_idle function, isn't there an extra operation with the wfi() like flushing the l1 cache?

Quoted 23 lines
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
 	tristate "Halt poll cpuidle driver"
 	depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +1 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only

drivers/cpuidle/Kconfig.riscv line 11

Quoted 10 lines
drivers/cpuidle/Kconfig.riscv new file +10 -0
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+	select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+	  Now only support C0 State.

Identation

Rest looks ok for me.

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#4 lore raw
In reply to liush
Quoted 4 lines · liush

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

First of all thanks for taking up the CPUIDLE efforts for RISC-V.

The commit description can be bit simplified as follows:
"This patch adds a simple cpuidle driver for RISC-V systems using the WFI state. Other states will be supported in the future."

Quoted 22 lines
---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
arch/riscv/Kconfig +0 -0
index df18372..c7ddb9d 100644

arch/riscv/Kconfig config RISCV · line 89

Quoted 4 lines
arch/riscv/Kconfig +1 -0
@@ -86,6 +86,7 @@ config RISCV
        select SPARSE_IRQ
        select SYSCTL_EXCEPTION_TRACE
        select THREAD_INFO_IN_TASK
+       select CPU_IDLE

Place "select CPU_IDLE" in alphabetical order under "config RISCV".

Quoted 47 lines
arch/riscv/Kconfig +6 -0
 config ARCH_MMAP_RND_BITS_MIN
        default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
        depends on RISCV_M_MODE
        depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +7 -0
new file mode 100644
index 00000000..2599d2f
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+
+extern void cpu_do_idle(void);
+
+#endif
arch/riscv/kernel/Makefile +1 -0
index dc93710..396ba9c 100644
@@ -29,6 +29,7 @@ obj-y += riscv_ksyms.o
 obj-y  += stacktrace.o
 obj-y  += cacheinfo.o
 obj-y  += patch.o
+obj-y  += cpuidle.o
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE)     += traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +0 -0
new file mode 100644
index 00000000..a3289e7

arch/riscv/kernel/cpuidle.c line 7

Quoted 7 lines
arch/riscv/kernel/cpuidle.c new file +7 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+       __asm__ __volatile__ ("wfi");
+

You should directly use the wait_for_interrupt() macro defined in asm/processor.h.

I think we don't need a separate kernel/cpuidle.c source file as of now. Maybe in-future we can add if required.

I suggest making cpu_do_idle() as "static inline" in asm/cpuidle.h

This way you will only need asm/cpuidle.h for the current changes.

Quoted 66 lines
arch/riscv/kernel/cpuidle.c new file +1 -0
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
        tristate "Halt poll cpuidle driver"
        depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +11 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+       select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+         Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)                += cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)          += cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)          += cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)            += cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +14 -0
new file mode 100644
index 00000000..5dddcfa
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RISC-V CPU idle driver.
+ *
+ * Copyright (C) 2020-2022 Allwinner Ltd
+ *
+ * Based on code - driver/cpuidle/cpuidle-at91.c
+ *
+ */
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_pm.h>
+#include <linux/kernel.h>
+#include <linux/module.h>

drivers/cpuidle/cpuidle-riscv.c line 24

Quoted 10 lines
drivers/cpuidle/cpuidle-riscv.c new file +10 -0
+#include <linux/of.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <asm/cpuidle.h>
+
+#define MAX_IDLE_STATES        1
+
+/* TODO: Implement deeper idle states */
+static int riscv_low_level_suspend_enter(int state)
+{

Shouldn't we call cpu_do_idle() here ???

Quoted 32 lines
drivers/cpuidle/cpuidle-riscv.c new file +31 -1
+       return 0;
+}
+
+/* Actual code that puts the SoC in different idle states */
+static int riscv_enter_idle(struct cpuidle_device *dev,
+                       struct cpuidle_driver *drv,
+                              int index)
+{
+       return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_low_level_suspend_enter,
+                                          index, 0);
+}
+
+static struct cpuidle_driver riscv_idle_driver = {
+       .name                   = "riscv_idle",
+       .owner                  = THIS_MODULE,
+       .states[0]              = {
+               .enter                  = riscv_enter_idle,
+               .exit_latency           = 1,
+               .target_residency       = 1,
+               .name                   = "WFI",
+               .desc                   = "RISCV WFI",
+       },
+       .state_count = MAX_IDLE_STATES,
+};
+
+static int __init riscv_cpuidle_init(void)
+{
+       return cpuidle_register(&riscv_idle_driver, NULL);
+}
+
+device_initcall(riscv_cpuidle_init);
--
Quoted 2 lines

2.7.4

As a separate topic, I suggest you propose the
SBI_EXT_HSM_HART_SUSPEND call for SBI spec.

The generic RISC-V cpuidle driver can detect SBI_EXT_HSM_HART_SUSPEND availability in riscv_cpuidle_init(). The riscv_low_level_suspend_enter() will do SBI_EXT_HSM_HART_SUSPEND call whenever available otherwise it can simply call cpu_do_idle().

Regards,
Anup

#2 lore raw
In reply to Daniel Lezcano

Hi Daniel,

Quoted 27 lines
Quoted 6 lines

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---

[ ... ]

Quoted 8 lines

obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o

arch/riscv/kernel/cpuidle.c new file +0 -0
new file mode 100644
index 00000000..a3289e7

arch/riscv/kernel/cpuidle.c line 7

Quoted 7 lines
arch/riscv/kernel/cpuidle.c new file +7 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+ __asm__ __volatile__ ("wfi");
+

extra line

Quoted 5 lines

arch/riscv/kernel/cpuidle.c line 8

Quoted 1 line
arch/riscv/kernel/cpuidle.c new file +1 -0
+}

As for the next deeper states should end up with the cpu_do_idle function, isn't there an extra operation with the wfi() like flushing the l1 cache?

Data cache consistency is mainly ensured by hardware in riscv, and there is no implementation of flushing data cache in kernel. Before wfi(),add an memory barrier operation - mb(). Is this feasible?

//arch/riscv/include/asm/barrier.h

17 #define RISCV_FENCE(p, s) \
18 __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
19
20 /* These barriers need to enforce ordering on both devices or memory. */
21 #define mb() RISCV_FENCE(iorw,iorw)

After modification, the codes is as follows.

81 @@ -0,0 +1,8 @@
82 +// SPDX-License-Identifier: GPL-2.0
83 +#include <asm/cpuidle.h>
84 +
85 +void cpu_do_idle(void)
86 +{
87 + mb();
88 + __asm__ __volatile__ ("wfi");
89 +
90 +}

Quoted 27 lines
Quoted 27 lines
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
  tristate "Halt poll cpuidle driver"
  depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +5 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
Quoted 8 lines

drivers/cpuidle/Kconfig.riscv line 11

Quoted 6 lines
drivers/cpuidle/Kconfig.riscv new file +6 -0
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+ select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+   Now only support C0 State.

Identation

I'll fix it. Thank you!

#5 lore raw
In reply to Anup Patel

Hi Anup,

Quoted 11 lines · liush
Quoted 4 lines

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

First of all thanks for taking up the CPUIDLE efforts for RISC-V.

The commit description can be bit simplified as follows:
"This patch adds a simple cpuidle driver for RISC-V systems using the WFI state. Other states will be supported in the future."

Thank you for your advice!

Agree.

Quoted 17 lines
Quoted 17 lines
---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
Quoted 12 lines

arch/riscv/Kconfig config RISCV · line 89

Quoted 9 lines
arch/riscv/Kconfig +1 -0
index df18372..c7ddb9d 100644
@@ -86,6 +86,7 @@ config RISCV
        select SPARSE_IRQ
        select SYSCTL_EXCEPTION_TRACE
        select THREAD_INFO_IN_TASK
+       select CPU_IDLE

Place "select CPU_IDLE" in alphabetical order under "config RISCV".

Agree, I'll fix it.

Quoted 49 lines
Quoted 49 lines
arch/riscv/Kconfig +6 -0
 config ARCH_MMAP_RND_BITS_MIN
        default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
        depends on RISCV_M_MODE
        depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +7 -0
new file mode 100644
index 00000000..2599d2f
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+
+extern void cpu_do_idle(void);
+
+#endif
arch/riscv/kernel/Makefile +1 -0
index dc93710..396ba9c 100644
@@ -29,6 +29,7 @@ obj-y += riscv_ksyms.o
 obj-y  += stacktrace.o
 obj-y  += cacheinfo.o
 obj-y  += patch.o
+obj-y  += cpuidle.o
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE)     += traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +2 -0
new file mode 100644
index 00000000..a3289e7
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
Quoted 8 lines

arch/riscv/kernel/cpuidle.c line 7

Quoted 5 lines
arch/riscv/kernel/cpuidle.c new file +5 -0
+
+void cpu_do_idle(void)
+{
+       __asm__ __volatile__ ("wfi");
+

You should directly use the wait_for_interrupt() macro defined in asm/processor.h.

Agree, I'll fix it. In addition, I want to call the fence interface mb() before call wait_for_interrupt().

Quoted 6 lines

I think we don't need a separate kernel/cpuidle.c source file as of now. Maybe in-future we can add if required.

I suggest making cpu_do_idle() as "static inline" in asm/cpuidle.h

This way you will only need asm/cpuidle.h for the current changes.

Agree, I'll fix it.

Quoted 70 lines
Quoted 70 lines
arch/riscv/kernel/cpuidle.c new file +1 -0
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
        tristate "Halt poll cpuidle driver"
        depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +11 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+       select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+         Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)                += cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)          += cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)          += cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)            += cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +18 -0
new file mode 100644
index 00000000..5dddcfa
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RISC-V CPU idle driver.
+ *
+ * Copyright (C) 2020-2022 Allwinner Ltd
+ *
+ * Based on code - driver/cpuidle/cpuidle-at91.c
+ *
+ */
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_pm.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <asm/cpuidle.h>
Quoted 8 lines

drivers/cpuidle/cpuidle-riscv.c line 24

Quoted 6 lines
drivers/cpuidle/cpuidle-riscv.c new file +6 -0
+
+#define MAX_IDLE_STATES        1
+
+/* TODO: Implement deeper idle states */
+static int riscv_low_level_suspend_enter(int state)
+{

Shouldn't we call cpu_do_idle() here ???

I think riscv_low_level_suspend_enter is suitable for the scenario of C1, C2. In riscv_low_level_suspend_enter, SBI is called to jump to runtime firmware (opensbi or bbl)to deal with platform related tasks.

Quoted 38 lines
Quoted 32 lines
drivers/cpuidle/cpuidle-riscv.c new file +31 -1
+       return 0;
+}
+
+/* Actual code that puts the SoC in different idle states */
+static int riscv_enter_idle(struct cpuidle_device *dev,
+                       struct cpuidle_driver *drv,
+                              int index)
+{
+       return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_low_level_suspend_enter,
+                                          index, 0);
+}
+
+static struct cpuidle_driver riscv_idle_driver = {
+       .name                   = "riscv_idle",
+       .owner                  = THIS_MODULE,
+       .states[0]              = {
+               .enter                  = riscv_enter_idle,
+               .exit_latency           = 1,
+               .target_residency       = 1,
+               .name                   = "WFI",
+               .desc                   = "RISCV WFI",
+       },
+       .state_count = MAX_IDLE_STATES,
+};
+
+static int __init riscv_cpuidle_init(void)
+{
+       return cpuidle_register(&riscv_idle_driver, NULL);
+}
+
+device_initcall(riscv_cpuidle_init);
--
Quoted 2 lines

2.7.4

As a separate topic, I suggest you propose the
SBI_EXT_HSM_HART_SUSPEND call for SBI spec.

Quoted 5 lines

The generic RISC-V cpuidle driver can detect SBI_EXT_HSM_HART_SUSPEND availability in riscv_cpuidle_init(). The riscv_low_level_suspend_enter() will do SBI_EXT_HSM_HART_SUSPEND call whenever available otherwise it can simply call cpu_do_idle().

Thank you. Your suggestion is really appreciated. In my opinion, we may propose a new SBI ID and discuss about it later.

maintainer
#6 lore raw
In reply to liush
Quoted 63 lines · On Sun, 13 Sep 2020 18:52:03 PDT (-0700), [email protected] wrote:

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
arch/riscv/Kconfig +7 -0
index df18372..c7ddb9d 100644
@@ -86,6 +86,7 @@ config RISCV
 	select SPARSE_IRQ
 	select SYSCTL_EXCEPTION_TRACE
 	select THREAD_INFO_IN_TASK
+	select CPU_IDLE
 
 config ARCH_MMAP_RND_BITS_MIN
 	default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
 	depends on RISCV_M_MODE
 	depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +7 -0
new file mode 100644
index 00000000..2599d2f
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+
+extern void cpu_do_idle(void);
+
+#endif
arch/riscv/kernel/Makefile +0 -0
index dc93710..396ba9c 100644

arch/riscv/kernel/Makefile obj-y += riscv_ksyms.o · line 32

Quoted 4 lines
arch/riscv/kernel/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-y	+= riscv_ksyms.o
 obj-y	+= stacktrace.o
 obj-y	+= cacheinfo.o
 obj-y	+= patch.o
+obj-y	+= cpuidle.o

Presumably we want this to be a Kconfig option, if only to avoid excess size on the smaller systems?

Quoted 9 lines
arch/riscv/kernel/Makefile +0 -0
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE)	+= traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +0 -0
new file mode 100644
index 00000000..a3289e7

arch/riscv/kernel/cpuidle.c line 7

Quoted 7 lines
arch/riscv/kernel/cpuidle.c new file +7 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+	__asm__ __volatile__ ("wfi");
+

We have wait_for_interrupt() that does this already, but it's one line so it doesn't really matter. Either way, there's an extra newline here.

Additionally, we have arch_cpu_idle() which is calling cpu_do_idle() on other platforms. Presumably we should be doing something similar, under the assumption that we will eventually have cpu_do_idle() hook into CPU idle drivers here?

Quoted 23 lines
arch/riscv/kernel/cpuidle.c new file +1 -0
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
 	tristate "Halt poll cpuidle driver"
 	depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +0 -0
new file mode 100644
index 00000000..e86d36b

drivers/cpuidle/Kconfig.riscv line 8

Quoted 8 lines
drivers/cpuidle/Kconfig.riscv new file +8 -0
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+	select CPU_IDLE_MULTIPLE_DRIVERS

Looks like there's some space/tab issues here. IIRC checkpatch will catch this sort of thing.

Quoted 36 lines
drivers/cpuidle/Kconfig.riscv new file +3 -0
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+	  Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)		+= cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)		+= cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)		+= cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)		+= cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +15 -0
new file mode 100644
index 00000000..5dddcfa
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RISC-V CPU idle driver.
+ *
+ * Copyright (C) 2020-2022 Allwinner Ltd
+ *
+ * Based on code - driver/cpuidle/cpuidle-at91.c
+ *
+ */
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_pm.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>

drivers/cpuidle/cpuidle-riscv.c line 25

Quoted 10 lines
drivers/cpuidle/cpuidle-riscv.c new file +10 -0
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <asm/cpuidle.h>
+
+#define MAX_IDLE_STATES	1
+
+/* TODO: Implement deeper idle states */
+static int riscv_low_level_suspend_enter(int state)
+{
+	return 0;

As far as I can tell, this driver just doesn't do anything? Assuming that's the case, it's probably best to just drop everything but cpu_do_idle() until we have something to exercise this.

Quoted 30 lines
drivers/cpuidle/cpuidle-riscv.c new file +30 -0
+}
+
+/* Actual code that puts the SoC in different idle states */
+static int riscv_enter_idle(struct cpuidle_device *dev,
+			struct cpuidle_driver *drv,
+			       int index)
+{
+	return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_low_level_suspend_enter,
+					   index, 0);
+}
+
+static struct cpuidle_driver riscv_idle_driver = {
+	.name			= "riscv_idle",
+	.owner			= THIS_MODULE,
+	.states[0]		= {
+		.enter			= riscv_enter_idle,
+		.exit_latency		= 1,
+		.target_residency	= 1,
+		.name			= "WFI",
+		.desc			= "RISCV WFI",
+	},
+	.state_count = MAX_IDLE_STATES,
+};
+
+static int __init riscv_cpuidle_init(void)
+{
+	return cpuidle_register(&riscv_idle_driver, NULL);
+}
+
+device_initcall(riscv_cpuidle_init);
#7 lore raw

Hi Palmer,

Quoted 56 lines
Quoted 51 lines

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
arch/riscv/Kconfig +7 -0
index df18372..c7ddb9d 100644
@@ -86,6 +86,7 @@ config RISCV
  select SPARSE_IRQ
  select SYSCTL_EXCEPTION_TRACE
  select THREAD_INFO_IN_TASK
+ select CPU_IDLE
 
 config ARCH_MMAP_RND_BITS_MIN
  default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
  depends on RISCV_M_MODE
  depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +0 -0
new file mode 100644
index 00000000..2599d2f

arch/riscv/include/asm/cpuidle.h line 4

Quoted 4 lines
arch/riscv/include/asm/cpuidle.h new file +4 -0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+

+extern void cpu_do_idle(void);

Quoted 14 lines

arch/riscv/kernel/Makefile obj-y += riscv_ksyms.o · line 32

Quoted 11 lines
arch/riscv/include/asm/cpuidle.h new file +2 -0
+
+#endif
arch/riscv/kernel/Makefile +1 -0
index dc93710..396ba9c 100644
@@ -29,6 +29,7 @@ obj-y += riscv_ksyms.o
 obj-y += stacktrace.o
 obj-y += cacheinfo.o
 obj-y += patch.o
+obj-y += cpuidle.o

Presumably we want this to be a Kconfig option, if only to avoid excess size on the smaller systems?

Thank you for your suggestions.
I am considering following Anup's suggestion - delete cpuidle.c and implement cpu_do_idle in cpuidle.h.

Quoted 11 lines
Quoted 11 lines
arch/riscv/kernel/Makefile +0 -0
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +2 -0
new file mode 100644
index 00000000..a3289e7
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
Quoted 8 lines

arch/riscv/kernel/cpuidle.c line 7

Quoted 5 lines
arch/riscv/kernel/cpuidle.c new file +5 -0
+
+void cpu_do_idle(void)
+{
+ __asm__ __volatile__ ("wfi");
+

We have wait_for_interrupt() that does this already, but it's one line so it doesn't really matter. Either way, there's an extra newline here.

Thanks, i'll modify it

Quoted 4 lines

Additionally, we have arch_cpu_idle() which is calling cpu_do_idle() on other platforms. Presumably we should be doing something similar, under the assumption that we will eventually have cpu_do_idle() hook into CPU idle drivers here?

Based on your comments, I run a test and found that arch_cpu_idle could be executed normally without enabling the idle driver.Part of the code of the experiment:

arch/riscv/include/asm/cpuidle.h new file +11 -0
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+
+static inline void cpu_do_idle(void)
+{
+       mb();
+       wait_for_interrupt();
+}
+
+#endif

+++ b/arch/riscv/kernel/process.c
@@ -21,6 +21,7 @@
#include <asm/string.h>
#include <asm/switch_to.h>
#include <asm/thread_info.h>
+#include <asm/cpuidle.h>

register unsigned long gp_in_global __asm__("gp");

@@ -35,7 +36,7 @@ extern asmlinkage void ret_from_kernel_thread(void);

void arch_cpu_idle(void)
{
- wait_for_interrupt();

+       cpu_do_idle();
        local_irq_enable();

}

Quoted 26 lines
Quoted 26 lines
arch/riscv/kernel/cpuidle.c new file +1 -0
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
  tristate "Halt poll cpuidle driver"
  depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +3 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
Quoted 8 lines

drivers/cpuidle/Kconfig.riscv line 8

Quoted 5 lines
drivers/cpuidle/Kconfig.riscv new file +5 -0
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+ select CPU_IDLE_MULTIPLE_DRIVERS

Looks like there's some space/tab issues here. IIRC checkpatch will catch this sort of thing.

yes, i'll modify it

Quoted 42 lines
Quoted 42 lines
drivers/cpuidle/Kconfig.riscv new file +3 -0
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+   Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)  += cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)  += cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)  += cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)  += cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +21 -0
new file mode 100644
index 00000000..5dddcfa
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RISC-V CPU idle driver.
+ *
+ * Copyright (C) 2020-2022 Allwinner Ltd
+ *
+ * Based on code - driver/cpuidle/cpuidle-at91.c
+ *
+ */
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_pm.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <asm/cpuidle.h>
+
+#define MAX_IDLE_STATES 1
+
Quoted 8 lines

drivers/cpuidle/cpuidle-riscv.c line 25

Quoted 4 lines
drivers/cpuidle/cpuidle-riscv.c new file +4 -0
+/* TODO: Implement deeper idle states */
+static int riscv_low_level_suspend_enter(int state)
+{
+ return 0;

As far as I can tell, this driver just doesn't do anything? Assuming that's the case, it's probably best to just drop everything but cpu_do_idle() until we have something to exercise this.

Although, the driver in this case is not necessary, it is essential when work in the macro CPU_PM_CPU_IDLE_ENTER_PARAM.And it could also be used for supporting other deeper states in the future. So I think it would be better to keep riscv_low_level_suspend_enter but change it to __weak function.

Quoted 30 lines
Quoted 30 lines
drivers/cpuidle/cpuidle-riscv.c new file +30 -0
+}
+
+/* Actual code that puts the SoC in different idle states */
+static int riscv_enter_idle(struct cpuidle_device *dev,
+   struct cpuidle_driver *drv,
+          int index)
+{
+ return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_low_level_suspend_enter,
+        index, 0);
+}
+
+static struct cpuidle_driver riscv_idle_driver = {
+ .name   = "riscv_idle",
+ .owner   = THIS_MODULE,
+ .states[0]  = {
+  .enter   = riscv_enter_idle,
+  .exit_latency  = 1,
+  .target_residency = 1,
+  .name   = "WFI",
+  .desc   = "RISCV WFI",
+ },
+ .state_count = MAX_IDLE_STATES,
+};
+
+static int __init riscv_cpuidle_init(void)
+{
+ return cpuidle_register(&riscv_idle_driver, NULL);
+}
+
+device_initcall(riscv_cpuidle_init);
author
#8 lore raw
In reply to Palmer Dabbelt

Hi Palmer,

Quoted 56 lines
Quoted 51 lines

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---
 arch/riscv/Kconfig               |  7 +++++
 arch/riscv/include/asm/cpuidle.h |  7 +++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpuidle.c      |  8 ++++++
 drivers/cpuidle/Kconfig          |  5 ++++
 drivers/cpuidle/Kconfig.riscv    | 11 ++++++++
 drivers/cpuidle/Makefile         |  4 +++
 drivers/cpuidle/cpuidle-riscv.c  | 55 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 98 insertions(+)
 create mode 100644 arch/riscv/include/asm/cpuidle.h
 create mode 100644 arch/riscv/kernel/cpuidle.c
 create mode 100644 drivers/cpuidle/Kconfig.riscv
 create mode 100644 drivers/cpuidle/cpuidle-riscv.c
arch/riscv/Kconfig +7 -0
index df18372..c7ddb9d 100644
@@ -86,6 +86,7 @@ config RISCV
  select SPARSE_IRQ
  select SYSCTL_EXCEPTION_TRACE
  select THREAD_INFO_IN_TASK
+ select CPU_IDLE
 
 config ARCH_MMAP_RND_BITS_MIN
  default 18 if 64BIT
@@ -407,6 +408,12 @@ config BUILTIN_DTB
  depends on RISCV_M_MODE
  depends on OF
 
+menu "CPU Power Management"
+
+source "drivers/cpuidle/Kconfig"
+
+endmenu
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/cpuidle.h new file +0 -0
new file mode 100644
index 00000000..2599d2f

arch/riscv/include/asm/cpuidle.h line 4

Quoted 4 lines
arch/riscv/include/asm/cpuidle.h new file +4 -0
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __RISCV_CPUIDLE_H
+#define __RISCV_CPUIDLE_H
+

+extern void cpu_do_idle(void);

Quoted 14 lines

arch/riscv/kernel/Makefile obj-y += riscv_ksyms.o · line 32

Quoted 11 lines
arch/riscv/include/asm/cpuidle.h new file +2 -0
+
+#endif
arch/riscv/kernel/Makefile +1 -0
index dc93710..396ba9c 100644
@@ -29,6 +29,7 @@ obj-y += riscv_ksyms.o
 obj-y += stacktrace.o
 obj-y += cacheinfo.o
 obj-y += patch.o
+obj-y += cpuidle.o

Presumably we want this to be a Kconfig option, if only to avoid excess size on the smaller systems?

Thank you for your suggestions.
I am considering following Anup's suggestion - delete cpuidle.c and implement cpu_do_idle in cpuidle.h.

Quoted 11 lines
Quoted 11 lines
arch/riscv/kernel/Makefile +0 -0
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o
arch/riscv/kernel/cpuidle.c new file +2 -0
new file mode 100644
index 00000000..a3289e7
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
Quoted 8 lines

arch/riscv/kernel/cpuidle.c line 7

Quoted 5 lines
arch/riscv/kernel/cpuidle.c new file +5 -0
+
+void cpu_do_idle(void)
+{
+ __asm__ __volatile__ ("wfi");
+

We have wait_for_interrupt() that does this already, but it's one line so it doesn't really matter. Either way, there's an extra newline here.

Thanks, i'll modify it

Quoted 4 lines

Additionally, we have arch_cpu_idle() which is calling cpu_do_idle() on other platforms. Presumably we should be doing something similar, under the assumption that we will eventually have cpu_do_idle() hook into CPU idle drivers here?

Based on your comments, I run a test and found that arch_cpu_idle could be executed normally without enabling the idle driver.Part of the code of the experiment:

+static inline void cpu_do_idle(void)
+{
+ mb();
+ wait_for_interrupt();
+}

void arch_cpu_idle(void)
{
- wait_for_interrupt();

+       cpu_do_idle();
        local_irq_enable();

}

Quoted 26 lines
Quoted 26 lines
arch/riscv/kernel/cpuidle.c new file +1 -0
+}
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
  tristate "Halt poll cpuidle driver"
  depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +3 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
Quoted 8 lines

drivers/cpuidle/Kconfig.riscv line 8

Quoted 5 lines
drivers/cpuidle/Kconfig.riscv new file +5 -0
+#
+config RISCV_CPUIDLE
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+ select CPU_IDLE_MULTIPLE_DRIVERS

Looks like there's some space/tab issues here. IIRC checkpatch will catch this sort of thing.

yes, i'll modify it

Quoted 42 lines
Quoted 42 lines
drivers/cpuidle/Kconfig.riscv new file +3 -0
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+   Now only support C0 State.
drivers/cpuidle/Makefile +4 -0
index 26bbc5e..4c83c4e 100644
@@ -34,3 +34,7 @@ obj-$(CONFIG_MIPS_CPS_CPUIDLE)  += cpuidle-cps.o
 # POWERPC drivers
 obj-$(CONFIG_PSERIES_CPUIDLE)  += cpuidle-pseries.o
 obj-$(CONFIG_POWERNV_CPUIDLE)  += cpuidle-powernv.o
+
+###############################################################################
+# RISCV drivers
+obj-$(CONFIG_RISCV_CPUIDLE)  += cpuidle-riscv.o
drivers/cpuidle/cpuidle-riscv.c new file +21 -0
new file mode 100644
index 00000000..5dddcfa
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RISC-V CPU idle driver.
+ *
+ * Copyright (C) 2020-2022 Allwinner Ltd
+ *
+ * Based on code - driver/cpuidle/cpuidle-at91.c
+ *
+ */
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_pm.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <asm/cpuidle.h>
+
+#define MAX_IDLE_STATES 1
+
Quoted 8 lines

drivers/cpuidle/cpuidle-riscv.c line 25

Quoted 4 lines
drivers/cpuidle/cpuidle-riscv.c new file +4 -0
+/* TODO: Implement deeper idle states */
+static int riscv_low_level_suspend_enter(int state)
+{
+ return 0;

As far as I can tell, this driver just doesn't do anything? Assuming that's the case, it's probably best to just drop everything but cpu_do_idle() until we have something to exercise this.

Although, the driver in this case is not necessary, it is essential when work in the macro CPU_PM_CPU_IDLE_ENTER_PARAM.And it could also be used for supporting other deeper states in the future. So I think it would be better to keep riscv_low_level_suspend_enter but change it to __weak function.

Quoted 22 lines
Quoted 22 lines
drivers/cpuidle/cpuidle-riscv.c new file +22 -0
+}
+
+/* Actual code that puts the SoC in different idle states */
+static int riscv_enter_idle(struct cpuidle_device *dev,
+   struct cpuidle_driver *drv,
+          int index)
+{
+ return CPU_PM_CPU_IDLE_ENTER_PARAM(riscv_low_level_suspend_enter,
+        index, 0);
+}
+
+static struct cpuidle_driver riscv_idle_driver = {
+ .name   = "riscv_idle",
+ .owner   = THIS_MODULE,
+ .states[0]  = {
+  .enter   = riscv_enter_idle,
+  .exit_latency  = 1,
+  .target_residency = 1,
+  .name   = "WFI",
+  .desc   = "RISCV WFI",
+ },
+ .state_count = MAX_IDLE_STATES,
Quoted 8 lines

drivers/cpuidle/cpuidle-riscv.c line 55

Quoted 8 lines
drivers/cpuidle/cpuidle-riscv.c new file +8 -0
+};
+
+static int __init riscv_cpuidle_init(void)
+{
+ return cpuidle_register(&riscv_idle_driver, NULL);
+}
+
+device_initcall(riscv_cpuidle_init);

Regards,
liush

#3 lore raw
In reply to 刘邵华BTD
Quoted 49 lines · 刘邵华BTD

Hi Daniel,

Quoted 27 lines
Quoted 6 lines

This patch adds a cpuidle driver for systems based RISCV architecture. This patch supports state WFI. Other states will be supported in the future.

---

[ ... ]

Quoted 8 lines

obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o

arch/riscv/kernel/cpuidle.c new file +0 -0
new file mode 100644
index 00000000..a3289e7

arch/riscv/kernel/cpuidle.c line 7

Quoted 7 lines
arch/riscv/kernel/cpuidle.c new file +7 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <asm/cpuidle.h>
+
+void cpu_do_idle(void)
+{
+ __asm__ __volatile__ ("wfi");
+

extra line

Quoted 5 lines

arch/riscv/kernel/cpuidle.c line 8

Quoted 1 line
arch/riscv/kernel/cpuidle.c new file +1 -0
+}

As for the next deeper states should end up with the cpu_do_idle function, isn't there an extra operation with the wfi() like flushing the l1 cache?

Data cache consistency is mainly ensured by hardware in riscv, and there is no implementation of flushing data cache in kernel. Before wfi(),add an memory barrier operation - mb(). Is this feasible?

//arch/riscv/include/asm/barrier.h

17 #define RISCV_FENCE(p, s) \
18 __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
19
20 /* These barriers need to enforce ordering on both devices or memory. */
21 #define mb() RISCV_FENCE(iorw,iorw)

After modification, the codes is as follows.

Quoted 7 lines

81 @@ -0,0 +1,8 @@
82 +// SPDX-License-Identifier: GPL-2.0
83 +#include <asm/cpuidle.h>
84 +
85 +void cpu_do_idle(void)
86 +{
87 + mb();

take a sceptical attibute with it , anyway mb() shout comment it with why

Quoted 41 lines

88 + __asm__ __volatile__ ("wfi");
89 +
90 +}

Quoted 27 lines
Quoted 27 lines
drivers/cpuidle/Kconfig +5 -0
index c0aeedd..f6be0fd 100644
@@ -62,6 +62,11 @@ depends on PPC
 source "drivers/cpuidle/Kconfig.powerpc"
 endmenu
 
+menu "RISCV CPU Idle Drivers"
+depends on RISCV
+source "drivers/cpuidle/Kconfig.riscv"
+endmenu
+
 config HALTPOLL_CPUIDLE
  tristate "Halt poll cpuidle driver"
  depends on X86 && KVM_GUEST
drivers/cpuidle/Kconfig.riscv new file +5 -0
new file mode 100644
index 00000000..e86d36b
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# RISCV CPU Idle drivers
+#
+config RISCV_CPUIDLE
Quoted 8 lines

drivers/cpuidle/Kconfig.riscv line 11

Quoted 6 lines
drivers/cpuidle/Kconfig.riscv new file +6 -0
+        bool "Generic RISCV CPU idle Driver"
+        select DT_IDLE_STATES
+ select CPU_IDLE_MULTIPLE_DRIVERS
+        help
+          Select this option to enable generic cpuidle driver for RISCV.
+   Now only support C0 State.

Identation

I'll fix it. Thank you!

Signature · 1 line
<http://www.allwinnertech.com> Allwinnertech Technology