From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 655C0606A33D for ; Wed, 2 Dec 2026 14:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=1wUCmHpk; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=GKUKm1BM DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type: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-Unsubscribe-Post: List-Owner; bh=L45cBruxBkzDq+SNIqv1gcoEu6CVL8Zib18C73Zo9HI=; b=1wUCmHpkndWQ+U XfmhjiAFiE+V0J3OjNaVwH0zX9yXSC5ohCT62cqPDZrT3AhSxyQ1/uszAExSOUAARBHkqiGsUip+K uYwexPZVKtBRSScwJTyF9I5uLFWGYtaIoYOsME2O3v2ISVVnFIrcGIifrUwyK8KahaGbDqd0XtFOu 1MTHlxdw0NGC4i3ghWFnrK6ZYDTShpmNw3RDrTtc4eqPSI1DXpC15fJkgcfrUkCpTCLntZ6j4kyrt QE6z3p1ovvemkd4e711cEIAzsKAxCrwpW1XtrAN/gC/q+QHwwKzxJoTPF/jVW3ze1yiSYx7ECSIyX sAnXg9CdGz0FWCo0H53A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYl4d-0000000BhBP-0m3c; Wed, 02 Dec 2026 14:12:43 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYl4b-0000000Bh9l-1jxq for linux-arm-kernel@lists.infradead.org; Wed, 02 Dec 2026 14:12:42 +0000 Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B2AM349237459; Wed, 2 Dec 2026 14:12:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=L45cBr uxBkzDq+SNIqv1gcoEu6CVL8Zib18C73Zo9HI=; b=GKUKm1BMBgRFLdZu5SCs33 FjfVJl/SyOMfuko0yyiTpgWmdH1On9xuv7kfkFD/yfUQrCypsbWFLgce4WZD59f7 K8At7BAuMPIg4p1O3xW8T5OoAwNGaZilIcIkdgnEOOOmyWL67Th0XK5xmalrQMcH /VXnjZOFmv+C4D5ip0NxgKobf90MJxAiA7hCB8RnPtOyi+3auSKuiQB74naNTeH3 wMQMiCU3z4wLcjBzhji2aGMz/K0FThgUgi9aX8GGTqXJ/QV/Tm6VLLj/yQz85JNS qHiSmG2Y4oF9k6+25I8PqS2LwuRlhj7NHJMCBMskJQXoL2obJj0wfmLR6rJo6TuA == Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4j7pp624sv-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 02 Dec 2026 14:12:11 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B2BYRR83858231; Wed, 2 Dec 2026 14:12:10 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4ja2uv3rwr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 02 Dec 2026 14:12:09 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (smtpav03.fra02v.mail.ibm.com [10.20.54.102]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B2EC6nX48693608 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 2 Dec 2026 14:12:06 GMT Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EEAB620040; Wed, 2 Dec 2026 14:12:05 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9AC3620043; Wed, 2 Dec 2026 14:12:05 +0000 (GMT) Received: from a46lp73.lnxne.boe (unknown [9.87.84.240]) by smtpav03.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 2 Dec 2026 14:12:05 +0000 (GMT) From: Steffen Eiden Date: Wed, 02 Dec 2026 15:11:47 +0100 Subject: [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261202-smccc-v1-9-48340e2737b6@linux.ibm.com> References: <20261202-smccc-v1-0-48340e2737b6@linux.ibm.com> In-Reply-To: <20261202-smccc-v1-0-48340e2737b6@linux.ibm.com> To: Marc Zyngier , Oliver Upton , Fuad Tabba , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Andreas Grapentin , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Friedrich Welter , Gautam Gala , Hariharan Mari , Hendrik Brueckner , Nico Boehr , Ulrich Weigand , Arnd Bergmann , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-s390@vger.kernel.org, Steffen Eiden X-Mailer: b4 0.14.3 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: Dwdltr-lPTMezKtD5XTZjEQ-kCvlcv2S X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAyMDA1NiBTYWx0ZWRfXwQRlwcMvjSqH Qp0dzGLoT77FHb9ay9U1Vl1GVy/ge5m6/A4XgD6nNbugi8+Zg24u/O9ZEYF0BqUQB1eZ9h1FgcU Dga/ltU3BBukA18hk3yI6pC8K+0m8Xk= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAyMDA1NiBTYWx0ZWRfX42TdZH+bo6X1 EQo2vtRrTCId6DX4IkuuDyOHi8Wx7RwGZNzsXBDvbqKAjLIiEq/BBc61HmU3Em6Jy51KgdzoQbZ s4ApZGQKIoBTmwBxRjYgrY535F6xLm+oUnOEKqjQFWC0b855eTCDj+mSp61ekt5HiYAUSFjN5ao cG2DY7QnkAwn55iVaK9W7fMggU7/nRd0f+/abWmYgy9EIk+LRrP6tWd1CRKHtEd/6kIoI4UXJOM aZPOgJqiLzUXm6C2TIhDW3H0bFTeFnT9dVjnSWAHi6oDmkdPoNaaRDOXKp/jPdiEb7ezWsA5fou MnxyA2OUJkM30seK28UBT+5SjKK2OUfkriT+uRLjLH9FzopBAWgQId1mPOXEhPqSFDgXMlgSo/g /kccDS8qEfs01uvzI619RISIhThKOM50yRHOomeJeobqaEQPvodq/+s7ebcx8gPlStQp7cV3a1r sZ2l+lz/2RDipLLy3HQ== X-Authority-Analysis: v=2.4 cv=Ae8kjHXG c=1 sm=1 tr=0 ts=6b10273b cx=c_pps a=3Bg1Hr4SwmMryq2xdFQyZA==:117 a=3Bg1Hr4SwmMryq2xdFQyZA==:17 a=IkcTkHD0fZMA:10 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VnNF1IyMAAAA:8 a=U2evYRUgxbVsejY69UAA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-ORIG-GUID: Dwdltr-lPTMezKtD5XTZjEQ-kCvlcv2S X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-02_02,2026-12-01_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 spamscore=0 malwarescore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612020056 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261202_061241_482464_2055DAAD X-CRM114-Status: GOOD ( 28.43 ) X-Spam-Score: -2.6 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Gautam Gala Add support for the arm64 architected timers (virtual and physical) of an arm64 guest. The SAE block holds the timer control registers, and ISTATUS in CTL is set when a timer condition is met. Build the shared arm64 arch_timer.c with s390-specific backends: CTL and CVAL are accessed through vcpu_{read,write}_sys_reg(), and the guest counter is obtained with PTFF QAGPT from the guest physical time offset (GPTO) in the SAE block. Co-developed-by: Steffen Eiden Signed-off-by: Gautam Gala Signed-off-by: Steffen Eiden --- arch/s390/include/arm64/kvm_emulate.h | 5 + arch/s390/include/asm/kvm_host_arm64.h | 2 + arch/s390/kvm/arm64/Makefile | 1 + arch/s390/kvm/arm64/Makefile.gen | 1 + arch/s390/kvm/arm64/arch_timer.c | 224 +++++++++++++++++++++++++++++++++ arch/s390/kvm/arm64/arm.c | 5 - 6 files changed, 233 insertions(+), 5 deletions(-) diff --git a/arch/s390/include/arm64/kvm_emulate.h b/arch/s390/include/arm64/kvm_emulate.h index dbaef40a2ac7..bb2ddfe22b95 100644 --- a/arch/s390/include/arm64/kvm_emulate.h +++ b/arch/s390/include/arm64/kvm_emulate.h @@ -127,6 +127,11 @@ static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu) void vcpu_reset_hcrx(struct kvm_vcpu *vcpu); +static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu) +{ + return false; +} + #include #endif /* __S390_ARM64_KVM_EMULATE_H__ */ diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h index 9bde7c56f08a..8367d34990a9 100644 --- a/arch/s390/include/asm/kvm_host_arm64.h +++ b/arch/s390/include/asm/kvm_host_arm64.h @@ -135,6 +135,8 @@ struct kvm_vcpu_arch { u64 last_steal; gpa_t base; } steal; + + struct arch_timer_cpu timer_cpu; }; struct kvm_vcpu_stat { diff --git a/arch/s390/kvm/arm64/Makefile b/arch/s390/kvm/arm64/Makefile index dc3d991fb4e2..e22037a7c498 100644 --- a/arch/s390/kvm/arm64/Makefile +++ b/arch/s390/kvm/arm64/Makefile @@ -11,6 +11,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/$(SRCARCH)/kvm/gma kvm-arm64-local := \ arm.o \ + arch_timer.o \ exception.o \ feature.o \ guest.o \ diff --git a/arch/s390/kvm/arm64/Makefile.gen b/arch/s390/kvm/arm64/Makefile.gen index 6faf2e66ac8d..5175b4b5c688 100644 --- a/arch/s390/kvm/arm64/Makefile.gen +++ b/arch/s390/kvm/arm64/Makefile.gen @@ -5,6 +5,7 @@ # List of ARM64 C files to extract ARM64_CFILES := \ + arch_timer.c \ arm.c \ guest.c \ hypercalls.c \ diff --git a/arch/s390/kvm/arm64/arch_timer.c b/arch/s390/kvm/arm64/arch_timer.c new file mode 100644 index 000000000000..f5b486afc2ad --- /dev/null +++ b/arch/s390/kvm/arm64/arch_timer.c @@ -0,0 +1,224 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +#include +#include +#include + +#include + +#include + +#include "qaaf.h" + +static bool kvm_timer_enabled(struct arch_timer_context *timer_ctx); +static bool kvm_timer_pending(struct arch_timer_context *timer_ctx); +static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx, u64 val); +static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu); + +#define __INCL_GEN_ARM_FILE +#include "generated/arch_timer.inc" +#undef __INCL_GEN_ARM_FILE + +/* + * This code assumes CNTRFRQ_EL0 == 1GHz and will break if not the case. + * A check during vcpu_create triggering a WARN_ONCE is done. + */ + +/* Clamp the delta: >2^63 ns overflows ktime_t and livelocks the bg hrtimer */ +#define COUNTER_DELTA_MAX (1ULL << 42) + +u32 timer_get_ctl(struct arch_timer_context *ctxt) +{ + struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt); + + switch (arch_timer_ctx_index(ctxt)) { + case TIMER_VTIMER: + return vcpu_read_sys_reg(vcpu, CNTV_CTL_EL0); + case TIMER_PTIMER: + return vcpu_read_sys_reg(vcpu, CNTP_CTL_EL0); + default: + WARN_ON(1); + return 0; + } +} + +static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl) +{ + struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt); + + switch (arch_timer_ctx_index(ctxt)) { + case TIMER_VTIMER: + vcpu_write_sys_reg(vcpu, ctl, CNTV_CTL_EL0); + break; + case TIMER_PTIMER: + vcpu_write_sys_reg(vcpu, ctl, CNTP_CTL_EL0); + break; + default: + WARN_ON(1); + } +} + +u64 timer_get_cval(struct arch_timer_context *ctxt) +{ + struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt); + + switch (arch_timer_ctx_index(ctxt)) { + case TIMER_VTIMER: + return vcpu_read_sys_reg(vcpu, CNTV_CVAL_EL0); + case TIMER_PTIMER: + return vcpu_read_sys_reg(vcpu, CNTP_CVAL_EL0); + default: + WARN_ON(1); + return 0; + } +} + +static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu) +{ + return vcpu_get_flag(vcpu, IN_WFIT); +} + +static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx, u64 val) +{ + struct kvm_vcpu *vcpu = timer_context_to_vcpu(timer_ctx); + u64 now = ptff_qagpt(vcpu->arch.sae_block.gpto); + + /* + * Bound the delta to a sane value. KVM/arm64 gets an equivalent bound + * from cyclecounter_cyc2ns() scaled math. + */ + if (now < val) + return min(val - now, COUNTER_DELTA_MAX); + + return 0; +} + +static bool kvm_timer_enabled(struct arch_timer_context *timer_ctx) +{ + return timer_ctx && + ((timer_get_ctl(timer_ctx) & + (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == + ARCH_TIMER_CTRL_ENABLE); +} + +static bool kvm_timer_pending(struct arch_timer_context *timer_ctx) +{ + if (!kvm_timer_enabled(timer_ctx)) + return false; + + if (timer_get_ctl(timer_ctx) & ARCH_TIMER_CTRL_IT_STAT) + return true; + + return !kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx)); +} + +static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt) +{ + struct arch_timer_cpu *timer; + struct kvm_vcpu *vcpu; + u64 ns; + + timer = container_of(hrt, struct arch_timer_cpu, bg_timer); + vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); + + /* + * Check that the timer has really expired from the guest's + * PoV (NTP on the host may have forced it to expire + * early). If we should have slept longer, restart it. + */ + ns = kvm_timer_earliest_exp(vcpu); + if (unlikely(ns)) { + hrtimer_forward_now(hrt, ns_to_ktime(ns)); + return HRTIMER_RESTART; + } + + kvm_vcpu_wake_up(vcpu); + + return HRTIMER_NORESTART; +} + +/* + * Schedule the background timer before calling kvm_vcpu_halt, so that this + * thread is removed from its waitqueue and made runnable when there's a timer + * interrupt to handle or when a WFxT times out. + */ +static void kvm_timer_blocking(struct kvm_vcpu *vcpu) +{ + struct arch_timer_cpu *timer = vcpu_timer(vcpu); + + /* + * If no timers are capable of raising interrupts (disabled or + * masked), then there's no more work for us to do. + */ + if (!kvm_timer_enabled(vcpu_vtimer(vcpu)) && + !kvm_timer_enabled(vcpu_ptimer(vcpu)) && + !vcpu_has_wfit_active(vcpu)) + return; + + /* + * At least one guest timer will expire or a WFxT will time out. + * Schedule a background timer. + * Set the earliest expiration time among the guest timers. + */ + soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu)); +} + +void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) +{ + struct arch_timer_context *ctxt; + + kvm_timer_unblocking(vcpu); + + for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) { + ctxt = vcpu_get_timer(vcpu, timerid); + ctxt->loaded = true; + } +} + +void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) +{ + struct arch_timer_context *ctxt; + + for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) { + ctxt = vcpu_get_timer(vcpu, timerid); + ctxt->loaded = false; + } + + if (kvm_vcpu_is_blocking(vcpu)) + kvm_timer_blocking(vcpu); +} + +static void timer_context_init(struct kvm_vcpu *vcpu, int timerid) +{ + struct arch_timer_context *ctxt = vcpu_get_timer(vcpu, timerid); + + ctxt->timer_id = timerid; +} + +void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) +{ + u64 cntfrq_el0 = kvm_qaaf_read_ftr_reg(SYS_CNTFRQ_EL0); + struct arch_timer_cpu *timer = vcpu_timer(vcpu); + + cntfrq_el0 = kvm_qaaf_read_ftr_reg(SYS_CNTFRQ_EL0); + if (cntfrq_el0 != HZ_PER_GHZ) + pr_warn_once( + "CNTFRQ_EL0 is %llu Hz. KVM expects 1GHz. Wakeups may arrive early or late.", + cntfrq_el0); + + for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) + timer_context_init(vcpu, timerid); + + hrtimer_setup(&timer->bg_timer, kvm_bg_timer_expire, CLOCK_MONOTONIC, + HRTIMER_MODE_ABS_HARD); +} + +void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu) +{ + timer_set_ctl(vcpu_vtimer(vcpu), 0); + timer_set_ctl(vcpu_ptimer(vcpu), 0); +} diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c index 7212ea1fc141..cbc760043a45 100644 --- a/arch/s390/kvm/arm64/arm.c +++ b/arch/s390/kvm/arm64/arm.c @@ -894,11 +894,6 @@ int kvm_set_routing_entry(struct kvm *kvm, return -EINVAL; } -int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) -{ - return 0; -} - long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { -- 2.56.1