From patchwork Wed Jun 9 12:27:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 458175 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47EFCC48BD1 for ; Wed, 9 Jun 2021 12:28:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2A45C61351 for ; Wed, 9 Jun 2021 12:28:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232285AbhFIM3x (ORCPT ); Wed, 9 Jun 2021 08:29:53 -0400 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:16039 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233036AbhFIM3u (ORCPT ); Wed, 9 Jun 2021 08:29:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1623241674; x=1654777674; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=WRiii23Z6QzfuX16+FZ4tI7GR0aXkmDaWpjIGuDJwLM=; b=Av3DdaGW7DbYKigufHY4W2g3nOXyM67ftSO9EAIZvM1ClN8KVmDlZ5qD KOGYfbtDUAR9+2oUF2FjX5Kzh8fYqVmIv2Gx+0oqb83Z8EQVG4VdNTDmx hQ5ZWL2ftWpFmVlSgTlHYGHYxdmwpZW1YoX1gh7pAhFVmVQRas1nJUOdj RtdadMG1wIxT48vnCLNgZPOlig8yu9U+9keI6H/VsDxHpXEr/Wmfcmgiu XMbANmf5uZkvxtzbZ/Crfmgac9akP+bTLYsdaCI0KipGGN1AnsNO9mdGV JMyGwK7anROoZQMiPIFaCbdY2O2Y1ddgnPJtBQO+EbCeTQIydEMgY8l0b g==; IronPort-SDR: qfRcQNAnNiKjER39EdDhUMI7UE339klv/JWf0nKDc8HjYluBFQ+K13iHH6q7U/fLs9Xkqm+j6K wavr9Aop/z4C/WweYjJvwLzqXAQn8SLwrUh1FFiTjlTYBasAoGe9OQlj+ElazbwTKEBKrTfXZm qKMowLAQRiJMewXrshe2fEy4tC8P2hipEVFc3n2PnDYgGmPS1P8K245mS3KzNdtfV6arQcn6z1 XkJzow6sZbdXLgoFU47Obx/BuHnU7JhYMN33ox1RPhDi3V7QhrBR4W4+xmCMQPQo+LEVBOOfHp I2s= X-IronPort-AV: E=Sophos;i="5.83,260,1616428800"; d="scan'208";a="176057726" Received: from mail-mw2nam10lp2103.outbound.protection.outlook.com (HELO NAM10-MW2-obe.outbound.protection.outlook.com) ([104.47.55.103]) by ob1.hgst.iphmx.com with ESMTP; 09 Jun 2021 20:27:54 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Deh9z1I4mNys8hX1zj1OLzNYix4/Eeh9unWbML4e6tu4HUla6KEjOmIC0NLwLGa7li+tJ+VhD7TFn48JqEpXKkZtOUzeqnQ2rH+t3GuE232K9yR2tNJ8gM3s8fOfXFd196z+v+/5pv/XbCZtScB1yQHbq3zosoF2xyxwpKJpqlhyqv96hs8ywhiVz4/P5lfF+iglLfKEvYVsbuG6xQ//x7Og8bkKSjGEdmAg4lAwaIpJj09WUbcP8q0nQ2mxHaAP/hmNIkWf31JDODztcsERvocHjDNuaipAzRYpy2uRlnYwzoxYePoCPLScftvDYcBnq8IAgamYOSjhsgZVcUIFhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lUYD4VxNiVhQdYFN5kCIwzDGeWn5RGAjIKEex8OoBz4=; b=hSQEaWGPb1eYy5E7oUwSESqablv/acCoyfy/uUslCLA6FKxOsD5zo9+ZzegmEKdsfIerOyDwUIAz5EKz3PfnPQymTZOcKtHLULEOAT0vQz2mmaxp1PiTKG60moCCHJlI9PZJYt295cD1lPv+rgUB52/KqZ9Qr0MoCDBGpel5Q1/tcx04/Bwysbvfqp3FsaCLJodtwNNk3qfSLkJ3oQOCDcGepqy8YWSHnQ89jAP7Zpdoi5ornGW+gK9xuO/mvnEi7ke0XbIx8dP4BVzrldSCAMDBq3jEdkfEuLfm2HmXRRkeookpIGqGhsvye1ugEtyv+ScDWerNk0gSTeQMoaFctQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lUYD4VxNiVhQdYFN5kCIwzDGeWn5RGAjIKEex8OoBz4=; b=b1J4+pbuMaYXA5jFPU13UpuJSdfdTjsUpdjd9hPgOK7Q35OkNtIUF4BC0K+cy2w4l3syFmDoIboaMW7XV1LSvu48+cuWEXE6ktX99Lh20uYWtR6MxLU7nqVUb7qveg+/SP/3iFpQVt6zCj1MJ77gr8wB+pcNcZHKV9lG27O7Jvw= Authentication-Results: dabbelt.com; dkim=none (message not signed) header.d=none; dabbelt.com; dmarc=none action=none header.from=wdc.com; Received: from CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) by CO6PR04MB7843.namprd04.prod.outlook.com (2603:10b6:5:35f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.21; Wed, 9 Jun 2021 12:27:54 +0000 Received: from CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::a153:b7f8:c87f:89f8]) by CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::a153:b7f8:c87f:89f8%9]) with mapi id 15.20.4219.021; Wed, 9 Jun 2021 12:27:54 +0000 From: Anup Patel To: Palmer Dabbelt , Palmer Dabbelt , Paul Walmsley , Albert Ou , Daniel Lezcano , Ulf Hansson , "Rafael J . Wysocki" , Pavel Machek , Rob Herring Cc: Sandeep Tripathy , Atish Patra , Alistair Francis , Liush , Anup Patel , devicetree@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Anup Patel Subject: [PATCH v6 3/8] RISC-V: Add arch functions for non-retentive suspend entry/exit Date: Wed, 9 Jun 2021 17:57:10 +0530 Message-Id: <20210609122715.3389-4-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210609122715.3389-1-anup.patel@wdc.com> References: <20210609122715.3389-1-anup.patel@wdc.com> X-Originating-IP: [122.172.176.125] X-ClientProxiedBy: MA1PR0101CA0033.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:22::19) To CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (122.172.176.125) by MA1PR0101CA0033.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:22::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.21 via Frontend Transport; Wed, 9 Jun 2021 12:27:49 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e1e411c5-b1e4-4c7a-839d-08d92b42019c X-MS-TrafficTypeDiagnostic: CO6PR04MB7843: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mbX21YGzuQI4QqqHYHX8iW2O3TvNDhW3VvKPS2CUUwIBG+Fhc2bGORPunzOvV3t8IfxLSdawCmL0trxk9OQUPROjUP4/VloYpudI7pjdHkfoTSd8r9b+pHvCMBi1XWbP3gVKaWgp8+JD465Ct40crUtyxMjlBscz3Sb2SHpCMg68bbkj65IWHq5w1C3ytmwxDwmBV4pgWkO8ggaRsB+0at0/hO3h1SusLxux/p6cg+jSwUYSHaKJZl7KYwX/aAnUawwVqcA+7dW+f4eGQMW9Ze66WUYE8gWr1zBk9jE+6ITWVSmpndShgfIQL5JjCz4yS2vF38ZcUY8Zp8EWpsDwBl5cE310z+NWD6yP94SBQgwZ2sPAvTymLssaopw2GkpZrWbjkEuAxmiGt+XKBkLvmkg8ya2pK0lhzjoj5PxUvxrEu+rr6VJOYd0SwOZmZPAg5T0OJq7KepmT4mTSZurjtstJMmJuW500DFmjAWkuDXcYZvBrGinCbb9N+G89YdCJhgq7YNTFRalsIWRm+ME9feVjVLdHas8kE8gaEmTutCZx/cP3olgczU2bvm+4uVzGpJhwL0DHfhJABDMSiAhCpNC/XzAlwlrc5ylwQG5++qyRdY4ywSZdmn6UBVH7d01lH5LOvpqYoywPIcL9+i0uRw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CO6PR04MB7812.namprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(346002)(39860400002)(396003)(366004)(136003)(376002)(7416002)(4326008)(8676002)(8886007)(83380400001)(478600001)(30864003)(110136005)(956004)(316002)(86362001)(6666004)(44832011)(2616005)(38350700002)(52116002)(2906002)(55236004)(7696005)(15650500001)(26005)(8936002)(38100700002)(1076003)(55016002)(66476007)(66556008)(66946007)(36756003)(16526019)(186003)(5660300002)(54906003); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gZazl/rDpGhPmbJu7Ke4td7IWlRjUkZ6LWH+7XLK7NemJXEVBCBvDyRGXDlaysv0h9yeQJilDA8CCA9y8vczO9EwR7c0krWdPRRuBMt+TLeIL6R+zaURtXXmY2v7wEj54/Yf/E2n1qRB2wn6NNNmjRQX5kyp9ZGUy+1RrvcTyW2+5abVefBarmwEUFF3VYSaKALfsimz+A1/MUIs6lab33hxSaPLLWPRNDP8lKcwkUZ3AEr4U2z9bszW/VUf7uI9+knUIenjXAM9TAtm9qOC0aPIFQrpWUTSkaMIHppqErpQ2KeUObBjlEtTAeWuwjBKveyAmLaFwEjpnfRjz4H9HXgp31TGoogcyJwV1XHrm9fDjxoifPnkOoHtAKzMzSjerns3j5Z7nLb6N83QA9yPaHNQbtVe4aIZnGGUGn0mnNSpeF+xSsWIyOTYhhJ5PZXRl2jwt7ebbDGVofHhZYX0LUoCcPBkv1FAORNxdH+poMfIvT3Ie2qos4eIUIvl6WV55xDqLcxU87nq9k44i7xJezcCTyekgmEtvacd6VTsDlYZ+S8Lyd466F0uvdjUVOaXZVJMIeRcglHaJgMWMbYRUeJyzw+jsqClnQc046FVsFu2AmxgxyAojGa/xUGCTrjHoGgGVLaOlPUQioEMvQtkj2Z8EggciThdkHTaarjxfj1FKzGDyhdjHZnfUKphV8K63LhwkLkK7SQ9VnndDHTgaXK+1GvAABsICLD4pARX5s5YhwPPmSVXVfMrijuyOt7MsnMYGkei/d6IZPhUNo6S5nwgv9atOLMxdd8l7YJCh5/vDkhW8bbFt1Icpp+vVKR0DrrEotCdFcw5lQjiIFpsEPScqigtUGWGSYeMR6Po8+1To6XYxPhN6OIPjCA45bfZ3EuX7PnAsbHRbIx1WChbduZ4QYWP5RLnP+4FRPUcXyLgjoYzAnkOwBfYuyTqHyHiYDzFjnaC2vyf2FWNT8jnWVW27XGZlTv4U4HgS50Ot5/CaJ244gPbiuSEYm9tGLEw6pjfou/oT3xmNPnWGuAIRggv9opE8+GJf4iaThx1Et0LTU09oCIo/YeuUAyHIosOEarnAsOtgpoEpxxeHF+cAaj48Y6+SOqhwGd6yKNBfkxcfKY27YdsxCgWLA0PJ1XsKMzzfJrdlhJ+eGqVKFxF2mUMsBFy/qitGWaJD114wSiOFgxNbKRIKUMVRRJXt9AwKILtO7Yf62bLxybQfEvZgeJ7H/0rwGBwhXoJ2GIabL9gQ296r8ua1MXzBguu9kyTaf/FzoPDBYpaje1UsnlksZoJ47lUqj+4/FWevaZko9JkOPAbWg/+XpABF9AkU7mc X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1e411c5-b1e4-4c7a-839d-08d92b42019c X-MS-Exchange-CrossTenant-AuthSource: CO6PR04MB7812.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2021 12:27:54.6417 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4gWuSFbhnHn75lVoxxmg9OjXtpJu2c/EXuowKVfVOvxBo4uK9W67e4Qbs+nbO9pl0AOGouRPJEDHwcJKGcXT+A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR04MB7843 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The hart registers and CSRs are not preserved in non-retentative suspend state so we provide arch specific helper functions which will save/restore hart context upon entry/exit to non-retentive suspend state. These helper functions can be used by cpuidle drivers for non-retentive suspend entry/exit. Signed-off-by: Anup Patel --- arch/riscv/include/asm/asm.h | 17 +++++ arch/riscv/include/asm/suspend.h | 35 +++++++++ arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/asm-offsets.c | 3 + arch/riscv/kernel/head.S | 11 --- arch/riscv/kernel/suspend.c | 86 +++++++++++++++++++++ arch/riscv/kernel/suspend_entry.S | 123 ++++++++++++++++++++++++++++++ 7 files changed, 266 insertions(+), 11 deletions(-) create mode 100644 arch/riscv/include/asm/suspend.h create mode 100644 arch/riscv/kernel/suspend.c create mode 100644 arch/riscv/kernel/suspend_entry.S diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h index 618d7c5af1a2..6c93f2806eb7 100644 --- a/arch/riscv/include/asm/asm.h +++ b/arch/riscv/include/asm/asm.h @@ -67,4 +67,21 @@ #error "Unexpected __SIZEOF_SHORT__" #endif +#ifdef __ASSEMBLY__ + +/* Common assembly source macros */ + +#ifdef CONFIG_XIP_KERNEL +.macro XIP_FIXUP_OFFSET reg + REG_L t0, _xip_fixup + add \reg, \reg, t0 +.endm +_xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET +#else +.macro XIP_FIXUP_OFFSET reg +.endm +#endif /* CONFIG_XIP_KERNEL */ + +#endif + #endif /* _ASM_RISCV_ASM_H */ diff --git a/arch/riscv/include/asm/suspend.h b/arch/riscv/include/asm/suspend.h new file mode 100644 index 000000000000..63e9f434fb89 --- /dev/null +++ b/arch/riscv/include/asm/suspend.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2021 Western Digital Corporation or its affiliates. + */ + +#ifndef _ASM_RISCV_SUSPEND_H +#define _ASM_RISCV_SUSPEND_H + +#include + +struct suspend_context { + /* Saved and restored by low-level functions */ + struct pt_regs regs; + /* Saved and restored by high-level functions */ + unsigned long scratch; + unsigned long tvec; + unsigned long ie; +#ifdef CONFIG_MMU + unsigned long satp; +#endif +}; + +/* Low-level CPU suspend entry function */ +int __cpu_suspend_enter(struct suspend_context *context); + +/* High-level CPU suspend which will save context and call finish() */ +int cpu_suspend(unsigned long arg, + int (*finish)(unsigned long arg, + unsigned long entry, + unsigned long context)); + +/* Low-level CPU resume entry function */ +int __cpu_resume_enter(unsigned long hartid, unsigned long context); + +#endif diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index d3081e4d9600..5a2fc649ad11 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -47,6 +47,8 @@ obj-$(CONFIG_SMP) += cpu_ops_spinwait.o obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o +obj-$(CONFIG_CPU_PM) += suspend_entry.o suspend.o + obj-$(CONFIG_FUNCTION_TRACER) += mcount.o ftrace.o obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index 9ef33346853c..2628dfd0f77d 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -10,6 +10,7 @@ #include #include #include +#include void asm_offsets(void); @@ -111,6 +112,8 @@ void asm_offsets(void) OFFSET(PT_BADADDR, pt_regs, badaddr); OFFSET(PT_CAUSE, pt_regs, cause); + OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs); + /* * THREAD_{F,X}* might be larger than a S-type offset can handle, but * these are used in performance-sensitive assembly so we can't resort diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index a44c0bc9c2f3..03bf2edfe9b2 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -15,17 +15,6 @@ #include #include "efi-header.S" -#ifdef CONFIG_XIP_KERNEL -.macro XIP_FIXUP_OFFSET reg - REG_L t0, _xip_fixup - add \reg, \reg, t0 -.endm -_xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET -#else -.macro XIP_FIXUP_OFFSET reg -.endm -#endif /* CONFIG_XIP_KERNEL */ - __HEAD ENTRY(_start) /* diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c new file mode 100644 index 000000000000..49dddec30e99 --- /dev/null +++ b/arch/riscv/kernel/suspend.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2021 Western Digital Corporation or its affiliates. + */ + +#include +#include +#include + +static void suspend_save_csrs(struct suspend_context *context) +{ + context->scratch = csr_read(CSR_SCRATCH); + context->tvec = csr_read(CSR_TVEC); + context->ie = csr_read(CSR_IE); + + /* + * No need to save/restore IP CSR (i.e. MIP or SIP) because: + * + * 1. For no-MMU (M-mode) kernel, the bits in MIP are set by + * external devices (such as interrupt controller, timer, etc). + * 2. For MMU (S-mode) kernel, the bits in SIP are set by + * M-mode firmware and external devices (such as interrupt + * controller, etc). + */ + +#ifdef CONFIG_MMU + context->satp = csr_read(CSR_SATP); +#endif +} + +static void suspend_restore_csrs(struct suspend_context *context) +{ + csr_write(CSR_SCRATCH, context->scratch); + csr_write(CSR_TVEC, context->tvec); + csr_write(CSR_IE, context->ie); + +#ifdef CONFIG_MMU + csr_write(CSR_SATP, context->satp); +#endif +} + +int cpu_suspend(unsigned long arg, + int (*finish)(unsigned long arg, + unsigned long entry, + unsigned long context)) +{ + int rc = 0; + struct suspend_context context = { 0 }; + + /* Finisher should be non-NULL */ + if (!finish) + return -EINVAL; + + /* Save additional CSRs*/ + suspend_save_csrs(&context); + + /* + * Function graph tracer state gets incosistent when the kernel + * calls functions that never return (aka finishers) hence disable + * graph tracing during their execution. + */ + pause_graph_tracing(); + + /* Save context on stack */ + if (__cpu_suspend_enter(&context)) { + /* Call the finisher */ + rc = finish(arg, __pa_symbol(__cpu_resume_enter), + (ulong)&context); + + /* + * Should never reach here, unless the suspend finisher + * fails. Successful cpu_suspend() should return from + * __cpu_resume_entry() + */ + if (!rc) + rc = -EOPNOTSUPP; + } + + /* Enable function graph tracer */ + unpause_graph_tracing(); + + /* Restore additional CSRs */ + suspend_restore_csrs(&context); + + return rc; +} diff --git a/arch/riscv/kernel/suspend_entry.S b/arch/riscv/kernel/suspend_entry.S new file mode 100644 index 000000000000..b8d20decfc28 --- /dev/null +++ b/arch/riscv/kernel/suspend_entry.S @@ -0,0 +1,123 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2021 Western Digital Corporation or its affiliates. + */ + +#include +#include +#include +#include + + .text + .altmacro + .option norelax + +ENTRY(__cpu_suspend_enter) + /* Save registers (except A0 and T0-T6) */ + REG_S ra, (SUSPEND_CONTEXT_REGS + PT_RA)(a0) + REG_S sp, (SUSPEND_CONTEXT_REGS + PT_SP)(a0) + REG_S gp, (SUSPEND_CONTEXT_REGS + PT_GP)(a0) + REG_S tp, (SUSPEND_CONTEXT_REGS + PT_TP)(a0) + REG_S s0, (SUSPEND_CONTEXT_REGS + PT_S0)(a0) + REG_S s1, (SUSPEND_CONTEXT_REGS + PT_S1)(a0) + REG_S a1, (SUSPEND_CONTEXT_REGS + PT_A1)(a0) + REG_S a2, (SUSPEND_CONTEXT_REGS + PT_A2)(a0) + REG_S a3, (SUSPEND_CONTEXT_REGS + PT_A3)(a0) + REG_S a4, (SUSPEND_CONTEXT_REGS + PT_A4)(a0) + REG_S a5, (SUSPEND_CONTEXT_REGS + PT_A5)(a0) + REG_S a6, (SUSPEND_CONTEXT_REGS + PT_A6)(a0) + REG_S a7, (SUSPEND_CONTEXT_REGS + PT_A7)(a0) + REG_S s2, (SUSPEND_CONTEXT_REGS + PT_S2)(a0) + REG_S s3, (SUSPEND_CONTEXT_REGS + PT_S3)(a0) + REG_S s4, (SUSPEND_CONTEXT_REGS + PT_S4)(a0) + REG_S s5, (SUSPEND_CONTEXT_REGS + PT_S5)(a0) + REG_S s6, (SUSPEND_CONTEXT_REGS + PT_S6)(a0) + REG_S s7, (SUSPEND_CONTEXT_REGS + PT_S7)(a0) + REG_S s8, (SUSPEND_CONTEXT_REGS + PT_S8)(a0) + REG_S s9, (SUSPEND_CONTEXT_REGS + PT_S9)(a0) + REG_S s10, (SUSPEND_CONTEXT_REGS + PT_S10)(a0) + REG_S s11, (SUSPEND_CONTEXT_REGS + PT_S11)(a0) + + /* Save CSRs */ + csrr t0, CSR_EPC + REG_S t0, (SUSPEND_CONTEXT_REGS + PT_EPC)(a0) + csrr t0, CSR_STATUS + REG_S t0, (SUSPEND_CONTEXT_REGS + PT_STATUS)(a0) + csrr t0, CSR_TVAL + REG_S t0, (SUSPEND_CONTEXT_REGS + PT_BADADDR)(a0) + csrr t0, CSR_CAUSE + REG_S t0, (SUSPEND_CONTEXT_REGS + PT_CAUSE)(a0) + + /* Return non-zero value */ + li a0, 1 + + /* Return to C code */ + ret +END(__cpu_suspend_enter) + +ENTRY(__cpu_resume_enter) + /* Load the global pointer */ + .option push + .option norelax + la gp, __global_pointer$ + .option pop + +#ifdef CONFIG_MMU + /* Save A0 and A1 */ + add t0, a0, zero + add t1, a1, zero + + /* Enable MMU */ + la a0, swapper_pg_dir + XIP_FIXUP_OFFSET a0 + call relocate_enable_mmu + + /* Restore A0 and A1 */ + add a0, t0, zero + add a1, t1, zero +#endif + + /* Make A0 point to suspend context */ + add a0, a1, zero + + /* Restore CSRs */ + REG_L t0, (SUSPEND_CONTEXT_REGS + PT_EPC)(a0) + csrw CSR_EPC, t0 + REG_L t0, (SUSPEND_CONTEXT_REGS + PT_STATUS)(a0) + csrw CSR_STATUS, t0 + REG_L t0, (SUSPEND_CONTEXT_REGS + PT_BADADDR)(a0) + csrw CSR_TVAL, t0 + REG_L t0, (SUSPEND_CONTEXT_REGS + PT_CAUSE)(a0) + csrw CSR_CAUSE, t0 + + /* Restore registers (except A0 and T0-T6) */ + REG_L ra, (SUSPEND_CONTEXT_REGS + PT_RA)(a0) + REG_L sp, (SUSPEND_CONTEXT_REGS + PT_SP)(a0) + REG_L gp, (SUSPEND_CONTEXT_REGS + PT_GP)(a0) + REG_L tp, (SUSPEND_CONTEXT_REGS + PT_TP)(a0) + REG_L s0, (SUSPEND_CONTEXT_REGS + PT_S0)(a0) + REG_L s1, (SUSPEND_CONTEXT_REGS + PT_S1)(a0) + REG_L a1, (SUSPEND_CONTEXT_REGS + PT_A1)(a0) + REG_L a2, (SUSPEND_CONTEXT_REGS + PT_A2)(a0) + REG_L a3, (SUSPEND_CONTEXT_REGS + PT_A3)(a0) + REG_L a4, (SUSPEND_CONTEXT_REGS + PT_A4)(a0) + REG_L a5, (SUSPEND_CONTEXT_REGS + PT_A5)(a0) + REG_L a6, (SUSPEND_CONTEXT_REGS + PT_A6)(a0) + REG_L a7, (SUSPEND_CONTEXT_REGS + PT_A7)(a0) + REG_L s2, (SUSPEND_CONTEXT_REGS + PT_S2)(a0) + REG_L s3, (SUSPEND_CONTEXT_REGS + PT_S3)(a0) + REG_L s4, (SUSPEND_CONTEXT_REGS + PT_S4)(a0) + REG_L s5, (SUSPEND_CONTEXT_REGS + PT_S5)(a0) + REG_L s6, (SUSPEND_CONTEXT_REGS + PT_S6)(a0) + REG_L s7, (SUSPEND_CONTEXT_REGS + PT_S7)(a0) + REG_L s8, (SUSPEND_CONTEXT_REGS + PT_S8)(a0) + REG_L s9, (SUSPEND_CONTEXT_REGS + PT_S9)(a0) + REG_L s10, (SUSPEND_CONTEXT_REGS + PT_S10)(a0) + REG_L s11, (SUSPEND_CONTEXT_REGS + PT_S11)(a0) + + /* Return zero value */ + add a0, zero, zero + + /* Return to C code */ + ret +END(__cpu_resume_enter)