From nobody Tue Feb 10 13:16:31 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 28DAF19E7D1; Fri, 31 Oct 2025 03:16:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761880617; cv=none; b=WKzV1M284fxlJ6v+wbc+sGEGku4xcRNT7X1VUGFwX7jW+CEmUT6Vt97D9eXkISThJEitmGjLt0cGHp5S2PEQY4LUh3FQVZPKaK1ZgytbM5hAwJCkBL/5GJrzBoLEVcWt0Az9iJQY0rAHgRjrK07DXKdQ/qrlyAJppjoAz52o2qY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761880617; c=relaxed/simple; bh=OupdCito8KXOWmdl+haqV9nwx66a8x9a5epBhc4k0x8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kPhaZX29NVxOl0nmSIfNLNVDJG0gFmgZ4ny/fM2xzFngB91M9Ml6Zqs7c1phbtwssOQz1DpsKcjAcp9HcI7FQ0luggk4jz2IOBqIR4BXzhKXj5vvK6Ze80wn5iAEqV6ML6znIrePrgqNvs2yh2jnOc8VwIOMlvphCUS7/e+FGfY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Dxb_AlKgRpWO0cAA--.63468S3; Fri, 31 Oct 2025 11:16:53 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJBxjcEkKgRpxMkeAQ--.48052S2; Fri, 31 Oct 2025 11:16:52 +0800 (CST) From: Bibo Mao To: Paolo Bonzini , Sean Christopherson , Shuah Khan , Tianrui Zhao , Huacai Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org Subject: [PATCH 6/6] KVM: LoongArch: selftests: Add SW emulated timer test Date: Fri, 31 Oct 2025 11:16:52 +0800 Message-Id: <20251031031652.4020879-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20251031031227.4020738-1-maobibo@loongson.cn> References: <20251031031227.4020738-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJBxjcEkKgRpxMkeAQ--.48052S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" This test case setup one-shot timer and execute idle instruction immediately to indicate giving up CPU, hypervisor will emulate SW hrtimer and wakeup vCPU when SW hrtimer is fired. Signed-off-by: Bibo Mao --- .../selftests/kvm/loongarch/arch_timer.c | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/testing/selftests/kvm/loongarch/arch_timer.c b/tools/tes= ting/selftests/kvm/loongarch/arch_timer.c index 1be42ef33bf3..a30dd0414a34 100644 --- a/tools/testing/selftests/kvm/loongarch/arch_timer.c +++ b/tools/testing/selftests/kvm/loongarch/arch_timer.c @@ -114,6 +114,45 @@ static void guest_time_count_test(uint32_t cpu) } } =20 +static void do_idle(void) +{ + unsigned int intid; + unsigned long estat; + + __asm__ __volatile__("idle 0" : : : "memory"); + + estat =3D csr_read(LOONGARCH_CSR_ESTAT); + intid =3D !!(estat & BIT(INT_TI)); + + /* Make sure pending timer IRQ arrived */ + GUEST_ASSERT_EQ(intid, 1); + csr_write(CSR_TINTCLR_TI, LOONGARCH_CSR_TINTCLR); +} + +static void guest_test_emulate_timer(uint32_t cpu) +{ + uint32_t config_iter; + uint64_t xcnt_diff_us, us; + struct test_vcpu_shared_data *shared_data =3D &vcpu_shared_data[cpu]; + + local_irq_disable(); + shared_data->nr_iter =3D 0; + us =3D msecs_to_usecs(test_args.timer_period_ms); + for (config_iter =3D 0; config_iter < test_args.nr_iter; config_iter++) { + shared_data->xcnt =3D timer_get_cycles(); + + /* Setup the next interrupt */ + timer_set_next_cmp_ms(test_args.timer_period_ms, false); + do_idle(); + + xcnt_diff_us =3D cycles_to_usec(timer_get_cycles() - shared_data->xcnt); + __GUEST_ASSERT(xcnt_diff_us >=3D us, + "xcnt_diff_us =3D 0x%lx, us =3D 0x%lx.\n", + xcnt_diff_us, us); + } + local_irq_enable(); +} + static void guest_code(void) { uint32_t cpu =3D guest_get_vcpuid(); @@ -123,6 +162,7 @@ static void guest_code(void) local_irq_enable(); guest_test_oneshot_timer(cpu); guest_test_period_timer(cpu); + guest_test_emulate_timer(cpu); =20 GUEST_DONE(); } --=20 2.39.3