From patchwork Fri Mar 29 09:26:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Hsu X-Patchwork-Id: 13610486 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87234C6FD1F for ; Fri, 29 Mar 2024 09:28:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SJpqgwVj7o5wGjHyPaJmhNapPwpSpjua1ERkbbGuTIQ=; b=4cIie5B7yQNMNC zhr9jOnCCx6o4WVWqhF9qX8g9U+yBIAyK2vYylp84e5dAIwtvnRPEne+NaD35GUwJjE1zSk/KGw6m 2sNSZrbOmxk+etXBdctfo/ESQijsrwfpYNWRX4FJ+vgq3k4ctfkRRMPhOJVWioXY6ByzQwpsae77d EozLkWFk3VTuLPhRbJZdsRV9kWz4u+wlTONmyhH6tyjHI6pwFctkVTnH2kAcz3MKBkI0zJEFO6A8y R41nMUpTVgt1ogzQyuoQGXaumDsYuoCJ3J4gSUec6KN42baKlzR23B4//MOielJqyHjyE85xxb/8V 3mdQQDSbYOfD41uzD2NA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rq8XM-0000000HUub-2exG; Fri, 29 Mar 2024 09:28:36 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rq8Wt-0000000HUZy-3Q86 for linux-riscv@lists.infradead.org; Fri, 29 Mar 2024 09:28:12 +0000 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-29f9af2e0b7so1305694a91.1 for ; Fri, 29 Mar 2024 02:28:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1711704485; x=1712309285; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2me2ctA/QJ+WDbqGcKl2bMmK6E0Eh2HMfIwB//6qG84=; b=PpcHqt0ImXIBGu+HOT7f7mPmfVh8B0m6IaPcEJq7nD21LO8Uf7B2JxCx4goRXLgeqf Hv2oBim5Fy9lRQdBjLZyzRZt2JGMIrxvFjMF9wYAxhgQ5qCeRrIzSm7mJRC6QGG7xN2u lcfWx5RYcp71xPGuSGX1xoaSA8jp8UjHPePsT3B9WZ0OayU10PH8t6Cq603qNn0l0CS/ AdFRGLj7fdmBXqjj1WZr56t5VDrFUuLpnvk2B68f71SwK/mdgnT4rV6Ef/dmQvR2zI8J Tq+SkEzumkyv9hMx71TCOjDTuLtln//0bP/fE4dDpCP6BD0uvFN+yrznthZq2+EHJir1 qrRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711704485; x=1712309285; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2me2ctA/QJ+WDbqGcKl2bMmK6E0Eh2HMfIwB//6qG84=; b=ZvbFPLqJ8ZCori+WdLbNXgFYJO8RVU1Y5PVRIzQU2t323pqyzp9G+Me2CTYe1jRGhY GlG+LcS0vvW+2cIMP9K8ucQp22JoXS+zbgTSZtwzbssfqMESfnCwEYTSE0X834rwO/Ke w8ihPJb3FaiLIOd2hyDlaTXJLX6nsvz2QH6U/goGnlNp8LpX51854/ZJIlW0ik6zf+wc razjfbLC3hUGM9e1qvbW60vIwfXSSgcOkC9LLjRdcedYLk65Gvq7i/hsWGDm2NSm4Mzl k/b/D4hZBWYI+TEHsEhtBvllqm1jx8Mjr3j36LByTGQs/A8+DyP9Jo7MTjhrodVVDB0z iCwg== X-Forwarded-Encrypted: i=1; AJvYcCVjnZqhQ54DL68jZWURRIyTY4gi4JfRdJ2DU7RRZvDvcfqz/S6q1Cm3GhqCi7b2VMPNGuw8xwECkD9DAyHzoG1kddr0MIpPmz2hBRDNhwzq X-Gm-Message-State: AOJu0Yy/Nvyk2X9aeUK5zCNTsFQVFG6KCBEMzktuuFavd9VTjZ4psX0d vQikCy8yD3sHPCd70mwuQCqdKD4sS/8GZeQcGTBgBqs+5LbilT58C8DOLB0VhqnugScgc3KvHSC m9/eXZL/HeWDRu+Am/bV14uN7Jl6AN4yQmRNK1gfQlysQDocDyPhBdoDsqB2W3lBa/5Z5nMPuvY 2x4QJu1CYxYXp6EenqRcfEy1w2c0zqdaWxOgAWVCNxEnf0wNdOUw== X-Google-Smtp-Source: AGHT+IGdY03xWXQByMBRxphQC1Vc3BZVaeHeBgA9jftNMkoc3op5kWSNSp3jA7sJf0LUKMcaom5H1Q== X-Received: by 2002:a17:90a:7f84:b0:2a2:d48:9d50 with SMTP id m4-20020a17090a7f8400b002a20d489d50mr1744971pjl.44.1711704485323; Fri, 29 Mar 2024 02:28:05 -0700 (PDT) Received: from [127.0.1.1] (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id cv17-20020a17090afd1100b002a02f8d350fsm2628830pjb.53.2024.03.29.02.28.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 02:28:05 -0700 (PDT) From: Max Hsu Date: Fri, 29 Mar 2024 17:26:23 +0800 Subject: [PATCH RFC 07/11] riscv: Add task switch support for scontext CSR MIME-Version: 1.0 Message-Id: <20240329-dev-maxh-lin-452-6-9-v1-7-1534f93b94a7@sifive.com> References: <20240329-dev-maxh-lin-452-6-9-v1-0-1534f93b94a7@sifive.com> In-Reply-To: <20240329-dev-maxh-lin-452-6-9-v1-0-1534f93b94a7@sifive.com> To: Conor Dooley , Rob Herring , Krzysztof Kozlowski , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Rafael J. Wysocki" , Pavel Machek , Anup Patel , Atish Patra , Paolo Bonzini , Shuah Khan Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, Max Hsu , Nick Hu X-Mailer: b4 0.13.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240329_022808_048093_8292A0D2 X-CRM114-Status: GOOD ( 12.91 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Write the next task PID to the scontext CSR if the use_scontext static branch is enabled by the detection of the cpufeature.c The scontext CSR needs to be saved and restored when entering a non-retentive idle state so that when resuming the CPU, the task's PID on the scontext CSR will be correct. Co-developed-by: Nick Hu Signed-off-by: Nick Hu Signed-off-by: Max Hsu --- arch/riscv/include/asm/suspend.h | 1 + arch/riscv/include/asm/switch_to.h | 9 +++++++++ arch/riscv/kernel/suspend.c | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/arch/riscv/include/asm/suspend.h b/arch/riscv/include/asm/suspend.h index 2ecace073869..5021cad7e815 100644 --- a/arch/riscv/include/asm/suspend.h +++ b/arch/riscv/include/asm/suspend.h @@ -13,6 +13,7 @@ struct suspend_context { /* Saved and restored by low-level functions */ struct pt_regs regs; /* Saved and restored by high-level functions */ + unsigned long scontext; unsigned long scratch; unsigned long envcfg; unsigned long tvec; diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 07432550ed54..289cd6b60978 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -75,6 +76,12 @@ static __always_inline bool has_scontext(void) return static_branch_likely(&use_scontext); } +static __always_inline void __switch_to_scontext(struct task_struct *__prev, + struct task_struct *__next) +{ + csr_write(CSR_SCONTEXT, task_pid_nr(__next)); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -86,6 +93,8 @@ do { \ __switch_to_fpu(__prev, __next); \ if (has_vector()) \ __switch_to_vector(__prev, __next); \ + if (has_scontext()) \ + __switch_to_scontext(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c index a086da222872..6b403a1f75c3 100644 --- a/arch/riscv/kernel/suspend.c +++ b/arch/riscv/kernel/suspend.c @@ -11,9 +11,13 @@ #include #include #include +#include void suspend_save_csrs(struct suspend_context *context) { + if (has_scontext()) + context->scontext = csr_read(CSR_SCONTEXT); + context->scratch = csr_read(CSR_SCRATCH); if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) context->envcfg = csr_read(CSR_ENVCFG); @@ -46,6 +50,9 @@ void suspend_save_csrs(struct suspend_context *context) void suspend_restore_csrs(struct suspend_context *context) { + if (has_scontext()) + csr_write(CSR_SCONTEXT, context->scontext); + csr_write(CSR_SCRATCH, context->scratch); if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) csr_write(CSR_ENVCFG, context->envcfg);