From nobody Thu Nov 28 08:34:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693818488223888.4135497806698; Mon, 4 Sep 2023 02:08:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd5YB-0001tZ-K0; Mon, 04 Sep 2023 05:07:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd5Y9-0001pE-7t; Mon, 04 Sep 2023 05:07:13 -0400 Received: from gandalf.ozlabs.org ([150.107.74.76]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd5Y6-0003gg-Hl; Mon, 04 Sep 2023 05:07:12 -0400 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4RfN7J1PQkz4x2Z; Mon, 4 Sep 2023 19:07:08 +1000 (AEST) Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4RfN7G2XhFz4wy8; Mon, 4 Sep 2023 19:07:06 +1000 (AEST) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: qemu-ppc@nongnu.org, qemu-devel@nongnu.org Cc: Daniel Henrique Barboza , Nicholas Piggin , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PULL 12/35] hw/ppc: Introduce functions for conversion between timebase and nanoseconds Date: Mon, 4 Sep 2023 11:06:07 +0200 Message-ID: <20230904090630.725952-13-clg@kaod.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230904090630.725952-1-clg@kaod.org> References: <20230904090630.725952-1-clg@kaod.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=150.107.74.76; envelope-from=SRS0=kZzc=EU=kaod.org=clg@ozlabs.org; helo=gandalf.ozlabs.org X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1693818488481100001 From: Nicholas Piggin These calculations are repeated several times, and they will become a little more complicated with subsequent changes. Signed-off-by: Nicholas Piggin Signed-off-by: C=C3=A9dric Le Goater --- hw/ppc/ppc.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c index 09b82f68a84e..423a3a117ae2 100644 --- a/hw/ppc/ppc.c +++ b/hw/ppc/ppc.c @@ -482,10 +482,20 @@ void ppce500_set_mpic_proxy(bool enabled) /*************************************************************************= ****/ /* PowerPC time base and decrementer emulation */ =20 +static uint64_t ns_to_tb(uint32_t freq, int64_t clock) +{ + return muldiv64(clock, freq, NANOSECONDS_PER_SECOND); +} + +static int64_t tb_to_ns(uint32_t freq, uint64_t tb) +{ + return muldiv64(tb, NANOSECONDS_PER_SECOND, freq); +} + uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offse= t) { /* TB time in tb periods */ - return muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND) + tb_o= ffset; + return ns_to_tb(tb_env->tb_freq, vmclk) + tb_offset; } =20 uint64_t cpu_ppc_load_tbl (CPUPPCState *env) @@ -528,8 +538,7 @@ uint32_t cpu_ppc_load_tbu (CPUPPCState *env) static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t *tb_offsetp, uint64_t value) { - *tb_offsetp =3D value - - muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND); + *tb_offsetp =3D value - ns_to_tb(tb_env->tb_freq, vmclk); =20 trace_ppc_tb_store(value, *tb_offsetp); } @@ -694,11 +703,11 @@ static inline int64_t _cpu_ppc_load_decr(CPUPPCState = *env, uint64_t next) =20 diff =3D next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); if (diff >=3D 0) { - decr =3D muldiv64(diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND); + decr =3D ns_to_tb(tb_env->decr_freq, diff); } else if (tb_env->flags & PPC_TIMER_BOOKE) { decr =3D 0; } else { - decr =3D -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECON= D); + decr =3D -ns_to_tb(tb_env->decr_freq, -diff); } trace_ppc_decr_load(decr); =20 @@ -838,7 +847,7 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint6= 4_t *nextp, =20 /* Calculate the next timer event */ now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - next =3D now + muldiv64(value, NANOSECONDS_PER_SECOND, tb_env->decr_fr= eq); + next =3D now + tb_to_ns(tb_env->decr_freq, value); *nextp =3D next; =20 /* Adjust timer */ @@ -1130,7 +1139,7 @@ static void cpu_4xx_fit_cb (void *opaque) /* Cannot occur, but makes gcc happy */ return; } - next =3D now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->tb_freq); + next =3D now + tb_to_ns(tb_env->tb_freq, next); if (next =3D=3D now) next++; timer_mod(ppc40x_timer->fit_timer, next); @@ -1158,8 +1167,7 @@ static void start_stop_pit (CPUPPCState *env, ppc_tb_= t *tb_env, int is_excp) } else { trace_ppc4xx_pit_start(ppc40x_timer->pit_reload); now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - next =3D now + muldiv64(ppc40x_timer->pit_reload, - NANOSECONDS_PER_SECOND, tb_env->decr_freq); + next =3D now + tb_to_ns(tb_env->decr_freq, ppc40x_timer->pit_reloa= d); if (is_excp) next +=3D tb_env->decr_next - now; if (next =3D=3D now) @@ -1218,7 +1226,7 @@ static void cpu_4xx_wdt_cb (void *opaque) /* Cannot occur, but makes gcc happy */ return; } - next =3D now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->decr_fre= q); + next =3D now + tb_to_ns(tb_env->decr_freq, next); if (next =3D=3D now) next++; trace_ppc4xx_wdt(env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); --=20 2.41.0