Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9BB01CCD193 for ; Wed, 15 Oct 2025 09:20:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v8xeN-0001yH-9C; Wed, 15 Oct 2025 05:18:27 -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 1v8xe0-0001n8-3E for qemu-arm@nongnu.org; Wed, 15 Oct 2025 05:18:07 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1v8xdh-0007Bj-VH for qemu-arm@nongnu.org; Wed, 15 Oct 2025 05:17:58 -0400 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-b07d4d24d09so533345766b.2 for ; Wed, 15 Oct 2025 02:17:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760519862; x=1761124662; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dD9uX6PaBNvNuAFjXV9Q7DahJDHptOizy7QDhFL8Gts=; b=hMdCQWsNKd4NetYe7OEnS64akGFKWuBwmAo0rHa1o1HYeMjQy9u7AdmV9IdlvY7qdM m5uPQQQjmchXSF6a9reTaHLjrqosVupVj/xbP8/C7mG3AVOwsUlvdqOT+vHGabY1XotF /ZjF4e3eLY+hFa2Cvw6ZpJlBqzNZBn5uuRVx6uWHLcN/4YioLubCrXSOzKxUmBGznvhW uajAzqDHzAAcaygPjwuhr0PHD3DUyNyQLAiE9AJHjER2uYr8tKmsiHRKa5WL+X8yX8Ha xcOpfMEdeWooeW9kakOcvla0UqcZMX8X6YlzYGkS7t9IcouX6bw4v2rCBFyRlA1ZclOt CTUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760519862; x=1761124662; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dD9uX6PaBNvNuAFjXV9Q7DahJDHptOizy7QDhFL8Gts=; b=T540lH4FTBuvyjWXPVsLyavj6Fp+6dQmJwpURE/OgtRt3IEh5YGQ1XwM3MCsEbp2Rz ydBjoXooiJorsuWU6tpzcLpprmfijwlp6QcQgxfRO1luKuuYqgV3HB0mCoep18ASH/Az 9syCDAXYZj4qAcgm5mnqGJnv2QgWiOno7AWqkXCiqWFqtTP9rnBJP0OHLCmVm/V+GEtH nnuhZhoClOXADpjIkMCmWl/AjsGxDQ/jtrc7x/IB2mO1rcVrY9REB+CH8SjnXf5YB5Df Ootuprx/J2jpvhSBB5d4QerJd4yKeoYP5BRgNv8syKOaVzCCIQFhSOxmW8HjHfOqdbP0 GaNw== X-Forwarded-Encrypted: i=1; AJvYcCUbNcFVKD9QXGtEcBnr3fXn+nOghAlamUoZm3GPI8JnunslQQH6F7/8gp5DuFaOSES6G4E7HK9MwA==@nongnu.org X-Gm-Message-State: AOJu0YzzUx8igINhBuwWUUGoOMt96MbHH0HKDKYanSPGAhq77v4UzMrf spp3QK1UIzPyo8VbPgYleRfmWfyQbpUGCQin37ogO5lwucjpcG3jeWMPP376SGg7Y3g= X-Gm-Gg: ASbGncv6eN1pM2AFoKzhltLSno6s3yzT6O1ShS102/UlS/4fjXhDG0nLNwsnDJmv8Bb EmBSVreRmwuDnbaQQmTMRxrvMmKUyfjnfo9FpATOdA+4cr8xazd3l3k6DjJ40Bb70zEkaH4HrS7 OACOo+2Net7WlFtNetfJGpY6xFLpSVMfPEQdy6OZVeDHBTBPGw0uyrCRHh/jZ3vvwiPZFlbu541 gcEmaod0cHSBCTM5z5IcFy+SFxet2k74mnWSmE536EeN4zj6Oo0D9fg+OxQh5GT6xb50tKe9uN5 mfLmC+/sQ5Q0IUHVoB7ua1QmEU+29u7YvaRpEO/AE0Js0U9/wXvp4lTT/K3ixSV/KUOYpHtHEu7 zsixEInSM4dEsb7l39yVqlGYS2EZq4dkoYgoxexlDt5WXDo1OBEdL+VuHJHFnFXDQ0uzGdN7Q X-Google-Smtp-Source: AGHT+IFn+Jxc4SgpAXY72JjpWx+qqMtm+cS8pd7JnLXg7lsD9pjCYZCnrynqxVjYC2WqHyUIITBJIA== X-Received: by 2002:a17:907:7f0b:b0:b46:31be:e8fe with SMTP id a640c23a62f3a-b50aa48c4f0mr3042696566b.11.1760519861994; Wed, 15 Oct 2025 02:17:41 -0700 (PDT) Received: from PC-DA2D10.beckhoff.com ([195.226.174.194]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b5cb9e7a23dsm182069966b.23.2025.10.15.02.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Oct 2025 02:17:41 -0700 (PDT) From: =?UTF-8?q?Corvin=20K=C3=B6hne?= To: qemu-devel@nongnu.org Cc: Kevin Wolf , =?UTF-8?q?Yannick=20Vo=C3=9Fen?= , Alistair Francis , "Edgar E. Iglesias" , Paolo Bonzini , qemu-arm@nongnu.org, Hanna Reitz , Peter Maydell , qemu-block@nongnu.org, =?UTF-8?q?Corvin=20K=C3=B6hne?= , YannickV Subject: [PATCH v3 01/14] hw/timer: Make frequency configurable Date: Wed, 15 Oct 2025 11:17:16 +0200 Message-ID: <20251015091729.33761-2-corvin.koehne@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251015091729.33761-1-corvin.koehne@gmail.com> References: <20251015091729.33761-1-corvin.koehne@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=corvin.koehne@gmail.com; helo=mail-ej1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-arm@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-arm-bounces+qemu-arm=archiver.kernel.org@nongnu.org Sender: qemu-arm-bounces+qemu-arm=archiver.kernel.org@nongnu.org From: YannickV The a9 global timer and arm mp timers rely on the PERIPHCLK as their clock source. The current implementation does not take that into account. That causes problems for applications assuming other frequencies than 1 GHz. We can now configure frequencies for the a9 global timer and arm mp timer. By allowing these values to be set according to the application's needs, we ensure that the timers behave consistently with the expected system configuration. The SoC configures the device correctly. Information can be found in the Zynq 7000 SoC Technical Reference Manual under Timers. https://docs.amd.com/r/en-US/ug585-zynq-7000-SoC-TRM Signed-off-by: YannickV --- hw/timer/a9gtimer.c | 9 ++++++--- hw/timer/arm_mptimer.c | 15 +++++++++++---- include/hw/timer/a9gtimer.h | 1 + include/hw/timer/arm_mptimer.h | 2 ++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/hw/timer/a9gtimer.c b/hw/timer/a9gtimer.c index 690140f5a6..ad9abcb4bb 100644 --- a/hw/timer/a9gtimer.c +++ b/hw/timer/a9gtimer.c @@ -63,9 +63,9 @@ static inline int a9_gtimer_get_current_cpu(A9GTimerState *s) static inline uint64_t a9_gtimer_get_conv(A9GTimerState *s) { uint64_t prescale = extract32(s->control, R_CONTROL_PRESCALER_SHIFT, - R_CONTROL_PRESCALER_LEN); - - return (prescale + 1) * 10; + R_CONTROL_PRESCALER_LEN) + 1; + uint64_t scaled_prescaler = prescale * 10; + return muldiv64(scaled_prescaler, NANOSECONDS_PER_SECOND, s->freq_hz); } static A9GTimerUpdate a9_gtimer_get_update(A9GTimerState *s) @@ -374,6 +374,9 @@ static const VMStateDescription vmstate_a9_gtimer = { }; static const Property a9_gtimer_properties[] = { + /* Default clock-frequency is 1GHz (NANOSECONDS_PER_SECOND) */ + DEFINE_PROP_UINT64("clock-frequency", A9GTimerState, freq_hz, + NANOSECONDS_PER_SECOND), DEFINE_PROP_UINT32("num-cpu", A9GTimerState, num_cpu, 0), }; diff --git a/hw/timer/arm_mptimer.c b/hw/timer/arm_mptimer.c index 7cc5915e9e..342ca1276a 100644 --- a/hw/timer/arm_mptimer.c +++ b/hw/timer/arm_mptimer.c @@ -59,9 +59,11 @@ static inline void timerblock_update_irq(TimerBlock *tb) } /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */ -static inline uint32_t timerblock_scale(uint32_t control) +static inline uint32_t timerblock_scale(TimerBlock *tb, uint32_t control) { - return (((control >> 8) & 0xff) + 1) * 10; + uint64_t prescale = (((control >> 8) & 0xff) + 1); + uint64_t scaled_prescaler = prescale * 10; + return muldiv64(scaled_prescaler, NANOSECONDS_PER_SECOND, tb->freq_hz); } /* Must be called within a ptimer transaction block */ @@ -155,7 +157,7 @@ static void timerblock_write(void *opaque, hwaddr addr, ptimer_stop(tb->timer); } if ((control & 0xff00) != (value & 0xff00)) { - ptimer_set_period(tb->timer, timerblock_scale(value)); + ptimer_set_period(tb->timer, timerblock_scale(tb, value)); } if (value & 1) { uint64_t count = ptimer_get_count(tb->timer); @@ -222,7 +224,8 @@ static void timerblock_reset(TimerBlock *tb) ptimer_transaction_begin(tb->timer); ptimer_stop(tb->timer); ptimer_set_limit(tb->timer, 0, 1); - ptimer_set_period(tb->timer, timerblock_scale(0)); + ptimer_set_period(tb->timer, + timerblock_scale(tb, tb->control)); ptimer_transaction_commit(tb->timer); } } @@ -269,6 +272,7 @@ static void arm_mptimer_realize(DeviceState *dev, Error **errp) */ for (i = 0; i < s->num_cpu; i++) { TimerBlock *tb = &s->timerblock[i]; + tb->freq_hz = s->freq_hz; tb->timer = ptimer_init(timerblock_tick, tb, PTIMER_POLICY); sysbus_init_irq(sbd, &tb->irq); memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb, @@ -301,6 +305,9 @@ static const VMStateDescription vmstate_arm_mptimer = { }; static const Property arm_mptimer_properties[] = { + /* Default clock-frequency is 1GHz (NANOSECONDS_PER_SECOND) */ + DEFINE_PROP_UINT64("clock-frequency", ARMMPTimerState, freq_hz, + NANOSECONDS_PER_SECOND), DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0), }; diff --git a/include/hw/timer/a9gtimer.h b/include/hw/timer/a9gtimer.h index 6ae9122e4b..3b63d14927 100644 --- a/include/hw/timer/a9gtimer.h +++ b/include/hw/timer/a9gtimer.h @@ -76,6 +76,7 @@ struct A9GTimerState { MemoryRegion iomem; /* static props */ + uint64_t freq_hz; uint32_t num_cpu; QEMUTimer *timer; diff --git a/include/hw/timer/arm_mptimer.h b/include/hw/timer/arm_mptimer.h index 65a96e2a0d..da43a3d351 100644 --- a/include/hw/timer/arm_mptimer.h +++ b/include/hw/timer/arm_mptimer.h @@ -31,6 +31,7 @@ typedef struct { uint32_t control; uint32_t status; struct ptimer_state *timer; + uint64_t freq_hz; qemu_irq irq; MemoryRegion iomem; } TimerBlock; @@ -43,6 +44,7 @@ struct ARMMPTimerState { SysBusDevice parent_obj; /*< public >*/ + uint64_t freq_hz; uint32_t num_cpu; TimerBlock timerblock[ARM_MPTIMER_MAX_CPUS]; MemoryRegion iomem; -- 2.47.3