Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp956738lqh; Fri, 29 Mar 2024 02:30:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVpRHeB162N/mjvKNCrt7Jz6SmKLG8FBTBChpKj7qcf1Im+IxZVrjfk3eWqv9iKzSu4UFOtbxOTSsR9v7MCp833NKJgFRKEHQklDIBkmg== X-Google-Smtp-Source: AGHT+IEkvbOoQnUPgK9ffiCiWNRVgGrDXWfRrRqH0oZN3sEEPJBjTalnnjyYnIw77o/+eZT3ZRwO X-Received: by 2002:a0c:e9c2:0:b0:696:42f8:a99c with SMTP id q2-20020a0ce9c2000000b0069642f8a99cmr1521296qvo.32.1711704645294; Fri, 29 Mar 2024 02:30:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711704645; cv=pass; d=google.com; s=arc-20160816; b=p80MFf544aQIi3nKL3rge516nc3Y9XwEDAlujQsyahygMGqSj4qiSj/vetyM2nD4lY JiFp/NXIkHJ1TXxwh2teN2NLeKMfJNSCpTodEGmp1XG4q2BBd+e/8y9kfoyY/jsvEzd1 Y2Vif+gixDwQSWn8y3qwfr6oaaXnEQllZbiR+aJ78kzoJZB8SKPMQFpimk70AF9aWeX5 JEF/z7uTvOX2Ua7jJDiu9reVOwdAjV6vEl2EMiH2Stf/uLWIG0ltWm0MQXG408KtbZxD KqGLyvyJfNkzBADYeSLmbRO4tWOGDqiHah7Ga1iHM8woB5kznFu3G4TjHsZcBzPSLDBN 1/lg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=2me2ctA/QJ+WDbqGcKl2bMmK6E0Eh2HMfIwB//6qG84=; fh=ZDvgACD65ewSMzEji/cHOTQ088y0agarbpRYqIRWqUg=; b=UT0AeMa8BZ6MaiN/HFzW+0CJOFcMHWWU9zfqWe4cKki642bJ5tlr57O3lIqaRKQVT8 bDRb6SxXqbB3CJl4VeuuLHPgOpv220jeNB3CNo2xQG0FKF3lXWmG+0fRsTwy12Wcnjdz zjNHQ+MYHM29vcBhCk/nDaR3doFdIV7M8BMbPy/RYHKdvP3fe14ryMGxi5RQZtoOPUJb 5aFgYxqql+j8es2f5VJDqH14v9OGrnfGAWwT7AcZSBxMiJGLW6rD8RY0UxWoeknyAVzO dksMcjHmwdAIveOZ2OhnJJ9IvvxsPEw2zTbGxw/WO53eTZrF2EMIhhTZ0mNrlhpm3y3j +jqw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=Wv3Se+ip; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-124339-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124339-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id l14-20020ad44d0e000000b00696a20b29a8si3312026qvl.84.2024.03.29.02.30.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 02:30:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124339-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=Wv3Se+ip; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-124339-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124339-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AF4661C230AD for ; Fri, 29 Mar 2024 09:30:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94C6E6A356; Fri, 29 Mar 2024 09:28:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="Wv3Se+ip" Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBFBB45971 for ; Fri, 29 Mar 2024 09:28:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711704487; cv=none; b=jQK344xhfZ8Kdtgt5vmQC3ApV0j2NH6nWA9GIF7EHcWtehfDQwCSX2S9mmkeS8ipAyylAMFdh4jNVNc+9iaC1kdp+C7c4D2Y1udLfa3UhtliZNtUx/4e18/+CcF6O7hmebFO4Hzky+kEIlHopUQpSJ0LfFKR3Ev5i+6Js3EK/f4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711704487; c=relaxed/simple; bh=aYxwIoa5oYGQg8jCIXD+qDStt+bScyurvWfoZf9ca1o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=f+UyhIeksc0bEEbx3ccAtw5c5M4QeXUxeixm0iacIk2RvUg2xX7XkYsRNn7hJ2niX7z0d3OV3i0DGjNcLwWEMvmhg7BkMGcZcg3sILGwK+J3UOD0UDyWo+KsmZ0L1VUlb0Pn4KJq5lfuwAgxMQLg95EYMcR7Jpt7i61lzKz/G9c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=Wv3Se+ip; arc=none smtp.client-ip=209.85.216.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-29f9af2e0b7so1305687a91.1 for ; Fri, 29 Mar 2024 02:28:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1711704485; x=1712309285; darn=vger.kernel.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=Wv3Se+ipLwX7BaXC8mc9OVgZLMWWwl2aQ5g0wKk7JFUVqgpbxaIkAz4jAIQvr5zPR+ wJI6gc1xj5e45Le+xmGkj8FRc5cSMqpYGdPlnF1GXJUqniLGUunR02g7KHc+JzvRv4N6 Xdt1BN9vHfz/gujH+Ls7JrlWKjlHhVGBMx/IlOFioZpORbH3km2aUylELqEVdx4BKFEh OXroAsuI1Ru7h1ry6MKwusQ1K/UlCJ/hjrF6UFr1n+e2XOCjGYid9x7vQ4wRL7bCOll9 ockv9yKI6dWdk3xXwOeIMINnH+vCgzJPiWwtABBZOc32d9CcOWRjL7y6toOgQjsQo/o/ tdHg== 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=iIGSaPKJn/wOOFtAbWGl8RHAoso1SkFteXIXiGYiO7uKcTTrB3txC39uVqRZROxpCT PbFatYumZG5+ZkQRP/ICOqRw5Y95s6muiubU9vvC9w0iKauhPB0vQjDsSI0z3I8ju5Zb cqN4FXGUxSWWT7ajR6qRqmDzyGbCYCGYPLQw+MmRNcomYQMv7FrEgHnMJooNfpI42lWY AZS4cPIN0L1yqkFjza66Fa33TZmPJOLbMfw534UhaJi7eqBXuGHG1FwruMne3dDrBEwk wYh6LVtbH3AL3nQ8diERmRPsAcpnW9wyfpkPx061WgoVT3ulL+p5PElpThRgyvX+4Sov VFDw== X-Forwarded-Encrypted: i=1; AJvYcCWDaXU3idNFMSpeURD4/GpQRUE6w08nuE/EiW9Jtee4scLS4PRfq+UhRg4WTd6ZmczFZENlbV+SwxH5TcKutddr22u48q841EojRr+4 X-Gm-Message-State: AOJu0YzcelD76YVn02mXyuvjEUGDVDfPxMnpQrXBeCIYFoIEbbAuuayQ rqpnIlTpLkSZpwXm/cb0LoG03n5VywrP7iGR6qZu2EL52nz8fgcEaH0frk+P0hg= 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 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); -- 2.43.2