From nobody Tue Feb 10 23:13:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of _spf.google.com designates 209.85.221.53 as permitted sender) client-ip=209.85.221.53; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wr1-f53.google.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1601313353; cv=none; d=zohomail.com; s=zohoarc; b=D3sksiOD5CWEl9VVX35fHwvG/lwh+BpgagC9Fdw/1PXjIpXq/l/Rs/cUoUKrlb4FnQxapDx+dZmIC80mrCfP1l1j18gj3DTFrX5YfuiXcwp8QAovBCkT3byqhcywoePasPDEgGrbX4wKEouBZS1MZD6cAlHyIZNQYJdiI3OkKsc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601313353; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Sender:Subject:To; bh=nV1TKIg/u52Ts1S/ZitAGgYmEsezh0Rxsw6P1iX3beg=; b=mYScfbCmC4EEFbb+L6e/cls+9jIL5qKOcdbIoOj7BJMSLAyfCXnAIRJDWtJIulbh8bzjGT2sx8dwv4aVBsbOVgI1z4m57wgvWbPswJ5aOUop8ALNuNZsL04LK4Zd4Q2hS+57EtG32IPVQ8QvoSobSy5hhNuM+PazjO2ekVt2ifA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of _spf.google.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=philippe.mathieu.daude@gmail.com Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by mx.zohomail.com with SMTPS id 1601313353478153.17649968869546; Mon, 28 Sep 2020 10:15:53 -0700 (PDT) Received: by mail-wr1-f53.google.com with SMTP id t10so2234904wrv.1 for ; Mon, 28 Sep 2020 10:15:52 -0700 (PDT) Return-Path: Return-Path: Received: from localhost.localdomain (74.red-83-53-161.dynamicip.rima-tde.net. [83.53.161.74]) by smtp.gmail.com with ESMTPSA id z67sm464787wme.41.2020.09.28.10.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 10:15:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nV1TKIg/u52Ts1S/ZitAGgYmEsezh0Rxsw6P1iX3beg=; b=OWHMcctro7BRn0S27C3rvtw5JjfuqgAWWFL79rCa/ApfqceoP6gx7FTasT1meSsidu Lqjl4ZqjqUCNCBtZk8eEjgDZd7XADW9E2vFqxMlreJtNcejwSVLeKu2/nV2wgzFCeK9f 65xfBx+7fbOI3ivuvieusGZ9vYXmFXB3WRhC0CkGY2KKJp2ufx722mlJtKkFrPo83Jc6 wC1PB/RYf0NnLEv7l1v3HzCYr5ckuX366/30vDuFfeOZy9ysi2l+9nwDfEyqXyR37j9y JnR0K5Fcr6VPvFHWJoc835gaLf61N7+qyppCqH+Zh/Z8QtEFR7vk+OlWa3pZYZmRDhc8 d/Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=nV1TKIg/u52Ts1S/ZitAGgYmEsezh0Rxsw6P1iX3beg=; b=rPywtLl8MZDmhYBaxQmuisTJYi4S6MmYFKGDz6VD3cGu70vGFgjnbKA8nHjskpqWDh PRuQczeSrNdM28zO1xXwCrWbuHWuhZ1ycJvwPGVPUVNiWscMUX/jM9cmT8ymuebuURuF juOs54efHnMF/wgM1KhuFufsXyEKZTfBrk5m0ZHbJLUkbVRE3a3Ew1t7gBT1XFARydy9 XA6AEJGN6ivsLwabhmRW+H5RMh9kg7JevMWsRYxLOAJE1ZWu/JCep42P3ycjd5M1A6MI +k7JP7rHBTZPdHNIJ1/PAP5KCxek7NutAg3AYmOCRAvYL0P8Ihg0WMGZKM+gIhfBsCiI 1k9g== X-Gm-Message-State: AOAM533RiO5F86rsGqtb/W3+GPhhQ5j8wCVFPiKiGFX5Z7CZ0bA3eOGV 5sVGDDAmnmheAXTMrjaqoug= X-Google-Smtp-Source: ABdhPJw1/M1sUi+8nOmvW6mF+8Pc26aSVdgzrLZA4XyhMHJXpCfxq8cXMMZgQv/qd2aySb6Y0nRykQ== X-Received: by 2002:a5d:4c4c:: with SMTP id n12mr2756136wrt.162.1601313351531; Mon, 28 Sep 2020 10:15:51 -0700 (PDT) Sender: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Aleksandar Rikalo , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Aleksandar Markovic , Wainer dos Santos Moschetta , Richard Henderson , Paul Burton , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Herv=C3=A9=20Poussineau?= , Damien Hedde , Jiaxun Yang , Huacai Chen , Cleber Rosa , Marcel Apfelbaum , Huacai Chen , Eduardo Habkost , "Edgar E . Iglesias" , Aurelien Jarno Subject: [PATCH 06/16] target/mips: Keep CP0 counter in sync with the CPU frequency Date: Mon, 28 Sep 2020 19:15:29 +0200 Message-Id: <20200928171539.788309-7-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200928171539.788309-1-f4bug@amsat.org> References: <20200928171539.788309-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) Since commit 6af0bf9c7c3 the CP0 counter is running at half the frequency of a 200 MHz CPU: a static 100 MHz value is used. By using the Clock API we can change the CPU frequency at runtime, so the CP0 counter might run out of sync with the CPU clock. Avoid that by using the recently introduced CPUClass::clock_update() callback. If the CPU clock is dynamically changed, our CP0 counter will be updated correspondingly. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- target/mips/cpu.h | 9 +++++++++ target/mips/cp0_timer.c | 13 ++++++------- target/mips/cpu.c | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/target/mips/cpu.h b/target/mips/cpu.h index 7cf7f5239f7..66d6124ae02 100644 --- a/target/mips/cpu.h +++ b/target/mips/cpu.h @@ -1145,6 +1145,7 @@ struct CPUMIPSState { struct MIPSITUState *itu; MemoryRegion *itc_tag; /* ITC Configuration Tags */ target_ulong exception_base; /* ExceptionBase input to the core */ + unsigned cp0_count_ns; /* CP0_Count clock period (in nanoseconds) */ }; =20 /** @@ -1160,6 +1161,14 @@ struct MIPSCPU { =20 CPUNegativeOffsetState neg; CPUMIPSState env; + /* + * The Count register acts as a timer, incrementing at a constant rate, + * whether or not an instruction is executed, retired, or any forward + * progress is made through the pipeline. The rate at which the counter + * increments is implementation dependent, and is a function of the + * pipeline clock of the processor, not the issue width of the process= or. + */ + unsigned cp0_count_rate; }; =20 =20 diff --git a/target/mips/cp0_timer.c b/target/mips/cp0_timer.c index 5194c967ae3..5ec0d6249e9 100644 --- a/target/mips/cp0_timer.c +++ b/target/mips/cp0_timer.c @@ -27,8 +27,6 @@ #include "sysemu/kvm.h" #include "internal.h" =20 -#define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */ - /* MIPS R4K timer */ static void cpu_mips_timer_update(CPUMIPSState *env) { @@ -37,8 +35,8 @@ static void cpu_mips_timer_update(CPUMIPSState *env) =20 now_ns =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); wait =3D env->CP0_Compare - env->CP0_Count - - (uint32_t)(now_ns / TIMER_PERIOD); - next_ns =3D now_ns + (uint64_t)wait * TIMER_PERIOD; + (uint32_t)(now_ns / env->cp0_count_ns); + next_ns =3D now_ns + (uint64_t)wait * env->cp0_count_ns; timer_mod(env->timer, next_ns); } =20 @@ -66,7 +64,7 @@ uint32_t cpu_mips_get_count(CPUMIPSState *env) cpu_mips_timer_expire(env); } =20 - return env->CP0_Count + (uint32_t)(now_ns / TIMER_PERIOD); + return env->CP0_Count + (uint32_t)(now_ns / env->cp0_count_ns); } } =20 @@ -82,7 +80,8 @@ void cpu_mips_store_count(CPUMIPSState *env, uint32_t cou= nt) } else { /* Store new count register */ env->CP0_Count =3D count - - (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PE= RIOD); + (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / + env->cp0_count_ns); /* Update timer timer */ cpu_mips_timer_update(env); } @@ -109,7 +108,7 @@ void cpu_mips_stop_count(CPUMIPSState *env) { /* Store the current value */ env->CP0_Count +=3D (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / - TIMER_PERIOD); + env->cp0_count_ns); } =20 static void mips_timer_cb(void *opaque) diff --git a/target/mips/cpu.c b/target/mips/cpu.c index 8d07a293a2e..2f75216c324 100644 --- a/target/mips/cpu.c +++ b/target/mips/cpu.c @@ -26,6 +26,7 @@ #include "qemu/module.h" #include "sysemu/kvm.h" #include "exec/exec-all.h" +#include "hw/qdev-clock.h" #include "hw/qdev-properties.h" =20 static void mips_cpu_set_pc(CPUState *cs, vaddr value) @@ -134,6 +135,22 @@ static void mips_cpu_disas_set_info(CPUState *s, disas= semble_info *info) } } =20 +static void mips_cp0_period_set(MIPSCPU *cpu) +{ + CPUMIPSState *env =3D &cpu->env; + + /* Recompute CP0's period on clock change */ + env->cp0_count_ns =3D cpu->cp0_count_rate * clock_get_ns(CPU(cpu)->clo= ck); +} + +static void mips_cpu_clk_update(CPUState *cs) +{ + MIPSCPU *cpu =3D MIPS_CPU(cs); + + /* Recompute CP0's period on clock change */ + mips_cp0_period_set(cpu); +} + static void mips_cpu_realizefn(DeviceState *dev, Error **errp) { CPUState *cs =3D CPU(dev); @@ -148,6 +165,7 @@ static void mips_cpu_realizefn(DeviceState *dev, Error = **errp) */ clock_set_hz(cs->clock, 200000000); } + mips_cpu_clk_update(cs); =20 cpu_exec_realizefn(cs, &local_err); if (local_err !=3D NULL) { @@ -190,6 +208,7 @@ static ObjectClass *mips_cpu_class_by_name(const char *= cpu_model) } =20 static Property mips_cpu_properties[] =3D { + DEFINE_PROP_UINT32("CP0_Count-rate", MIPSCPU, cp0_count_rate, 2), DEFINE_PROP_END_OF_LIST() }; =20 @@ -224,6 +243,7 @@ static void mips_cpu_class_init(ObjectClass *c, void *d= ata) cc->tcg_initialize =3D mips_tcg_init; cc->tlb_fill =3D mips_cpu_tlb_fill; #endif + cc->clock_update =3D mips_cpu_clk_update; =20 cc->gdb_num_core_regs =3D 73; cc->gdb_stop_before_watchpoint =3D true; --=20 2.26.2