Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1995323pxk; Mon, 14 Sep 2020 01:41:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1fMyiaCzpjOjO9NcHS6/ukeyalW9tAsHkiSYoMhLy+Sqv4w0Q5YrrvPNwObgu3t+eKE5q X-Received: by 2002:a17:906:1b15:: with SMTP id o21mr13256325ejg.377.1600072893324; Mon, 14 Sep 2020 01:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600072893; cv=none; d=google.com; s=arc-20160816; b=rM3HhROZ8JNWJMXILcaeAbAG97Jube3fNARFv5PRhgWkMs3aR/eSlwqIXrhO6DNekc s+PifrTBsc0ZC0/NNISZBZjdcIBS00Ud6JnQeb4j7C/18h0XJfc/3omeTP96uviOXlaA mGkp4XH4EpI6aJ1Y61j72+BHvza3tBwnaFUitXbk/jrIO8SkJK1c3IkqJpQ2IkmHGJpU 0VkDTwb7zNtwb/i4Qi+sSlXC4pWi4F4bttNoNTscsxHfQvbeFlz+lrUdy3vvmdSPN/fx hHCG9Hanzup90ERLFIqpc+h7DFzle74dKt+T4WXDWTmcDF3g09U1ICFm1WBukRVxtyHo rxVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=JIHvV2acufNTdV2ZD5UVUE8IHhdO6zOGauywATo6f5M=; b=H8ZjbG13fktkRObXTYDTwGlJCMdKveGs3XouO0ynMZ1TQZytbUQ3YLB8Sghf2niEBw Ul/uVZqZbS1UCBD55l9BUjja6NkIY9SRN7rCYWW7vNXrFIoLL7LceJMtMfADE+I+aQmE 7s1/rbCmWbxLCeeOySC+5vDPxApukG/CJde4oTdTE0DlJioTGqyO2ptNqVCOiJ1WkCaE /0xfrKtj6RbPSnJ+a6hxU2Jx66FY4Jw7DblioYEbS1U4af7IxGDgnyhLAuSduwjo8Gf8 L0CQl97X+3C/fyqhfYE7VVrBOcYnXFdGT6Q42xPlHQzYCpKbRCGXSxMukjozJ211SQp8 fIPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=YWpaC93P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j24si6478750ejd.671.2020.09.14.01.41.11; Mon, 14 Sep 2020 01:41:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20150623.gappssmtp.com header.s=20150623 header.b=YWpaC93P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726241AbgINIkX (ORCPT + 99 others); Mon, 14 Sep 2020 04:40:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726220AbgINIkP (ORCPT ); Mon, 14 Sep 2020 04:40:15 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A722C06174A for ; Mon, 14 Sep 2020 01:40:15 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id z4so17746685wrr.4 for ; Mon, 14 Sep 2020 01:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=JIHvV2acufNTdV2ZD5UVUE8IHhdO6zOGauywATo6f5M=; b=YWpaC93PYgw87CMssVEGXLRFjRk4KgGY/F1FvrcEiBp4LR38i+yXTh1JADV48qqjAZ lu08OG3aCJptxCi00sK+VAmSKO59eWfiFONCeYEc1ssxf6q6TY96Y5OfRs6dDnP1koO8 L08c9pT50Zy4fXmQunPZE0eaZxMesoN4CIw3Do/LrEp4BTR71QGpSTpezDFlpLwC3NVX Yugw/Q0BhJegiIEUDch6sD1ER5cg/j2pX2eMoSyj8rb8v2M/0N4el4IQmJegFalV6CEz 0LFp9MiCLGoqRjdHyYoGT1vmMT4E1bHee0nTLv84HNKJTL2FZ7iWAE/OkRoNuFiVzGZh M5Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JIHvV2acufNTdV2ZD5UVUE8IHhdO6zOGauywATo6f5M=; b=PN2uCrW0a4NBWpv+6uNGVJv0sxVuyPNsK5P2/yr51K44SUgDY5KTQvBw10plwbZqzt J3v2aasgNiCdW4fZpQR6BoPqVfNpa7QmFeAumbknKtjdOwR6CI7R4M3gaiq69U39N9P2 W4Rz6whQHCG0wkqmVQuED6EAnhca+mfEL7vzCFUGLyaVqdD2EQaxk9p5RShKvi+zAnXy tO0N+LEVM8+MGQjt+kzpQKMZU+p8wKBOJL6OxRA93JoFpjn7J+eWCUxSXjPdyrwTHFjM cJDq2lC/5R5xvVEvINzIFV8hQPPN6NQM86IXrgjPYfKMmd6KKdb1V/jdfk5P93ywuBX6 Yljw== X-Gm-Message-State: AOAM530YBfmQ8z7YAIUU5jyRV9aihzK0W5WlGcXrot1eFDUEhwtdIsZK 1U7+vIMKdKAiGsDHAZSWnK6I+xtHxv673OmXiIRTQg== X-Received: by 2002:adf:eecb:: with SMTP id a11mr14874541wrp.356.1600072813698; Mon, 14 Sep 2020 01:40:13 -0700 (PDT) MIME-Version: 1.0 References: <1600048323-2964-1-git-send-email-liush@allwinnertech.com> In-Reply-To: <1600048323-2964-1-git-send-email-liush@allwinnertech.com> From: Anup Patel Date: Mon, 14 Sep 2020 14:10:02 +0530 Message-ID: Subject: Re: [PATCH] cpuidle: add riscv cpuidle driver To: liush Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , rjw@rjwysocki.net, Daniel Lezcano , Anup Patel , Atish Patra , Damien Le Moal , Kefeng Wang , Emil Renner Berhing , Zong Li , linux-riscv , "linux-kernel@vger.kernel.org List" , linux-pm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 14, 2020 at 7:22 AM liush 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. 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." > > Signed-off-by: liush > --- > 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 > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > index df18372..c7ddb9d 100644 > --- a/arch/riscv/Kconfig > +++ b/arch/riscv/Kconfig > @@ -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". > > 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" > diff --git a/arch/riscv/include/asm/cpuidle.h b/arch/riscv/include/asm/cpuidle.h > new file mode 100644 > index 00000000..2599d2f > --- /dev/null > +++ b/arch/riscv/include/asm/cpuidle.h > @@ -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 > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index dc93710..396ba9c 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -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 > diff --git a/arch/riscv/kernel/cpuidle.c b/arch/riscv/kernel/cpuidle.c > new file mode 100644 > index 00000000..a3289e7 > --- /dev/null > +++ b/arch/riscv/kernel/cpuidle.c > @@ -0,0 +1,8 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > + > +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. > +} > diff --git a/drivers/cpuidle/Kconfig b/drivers/cpuidle/Kconfig > index c0aeedd..f6be0fd 100644 > --- a/drivers/cpuidle/Kconfig > +++ b/drivers/cpuidle/Kconfig > @@ -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 > diff --git a/drivers/cpuidle/Kconfig.riscv b/drivers/cpuidle/Kconfig.riscv > new file mode 100644 > index 00000000..e86d36b > --- /dev/null > +++ b/drivers/cpuidle/Kconfig.riscv > @@ -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. > diff --git a/drivers/cpuidle/Makefile b/drivers/cpuidle/Makefile > index 26bbc5e..4c83c4e 100644 > --- a/drivers/cpuidle/Makefile > +++ b/drivers/cpuidle/Makefile > @@ -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 > diff --git a/drivers/cpuidle/cpuidle-riscv.c b/drivers/cpuidle/cpuidle-riscv.c > new file mode 100644 > index 00000000..5dddcfa > --- /dev/null > +++ b/drivers/cpuidle/cpuidle-riscv.c > @@ -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 > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#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 ??? > + 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); > -- > 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