From patchwork Fri Apr 19 18:46:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 790218 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp1223104wrm; Fri, 19 Apr 2024 11:47:20 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVAZbdK6FnZgqLBL6mqomwhf1pdhi6+RjpNGulrtoGeQLYenVWMRzLjxPzTW4TW+FuYlhPS724QLaqw/TOtEHZv X-Google-Smtp-Source: AGHT+IGnkSuZDJzBQ810SEFIzgjT33BG9lqLK4ZS3sU8J1AjmBs+fXeges9ao5MKWlK1VVEc6tCE X-Received: by 2002:ad4:424f:0:b0:6a0:5aaf:7486 with SMTP id l15-20020ad4424f000000b006a05aaf7486mr3152174qvq.56.1713552440632; Fri, 19 Apr 2024 11:47:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713552440; cv=none; d=google.com; s=arc-20160816; b=ghClc+jkkwvzrbXVpVjbLzh4uwRJmjrPdPZlhQ/pbDVMGRKJf4qrM+R45oBmvIKVNm 5ihd1DQdy2QYcTmFEGp+E/Mnx//9Pj3A7iC4hMv5J2jwBUhuPyURWaDt4fcSb4cOBKda DsCvQhESdnw8B6TGmtXwLwybtn5LikcisbK25yw/woLvqlhbm8PXKU7BxVfP5DEzatXa S4k8cibF/1ZHKCoEHg0cgChcG0jE56ov5ahdjzhLTqMuIBZYJqYbSWsCg8sryG6M47m9 PJcekkM2IRASUoCaQhF9vbhei2USG9PyFNybwEBDkTAhT0B6TsrYHnlgUgK2hlOPPx6X /1sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=cD15Yu4sqNz4RqnI7YL4Ofu26geh4vOxHGToCiBD3jE=; fh=SVGO+SuXg4wE7dsT0XUn1VYyD7EkZJ3zB9pdlXjL0xU=; b=PZv3kvGskL9GDFaZjzl+NQZ4yPQVx20vqmKPi/iXP0UTojfwe1pd2n3uhTgKzFYpIK lwP5bRz/CkX8X6lbStgYeX4XjLnFHOW9yqHBHDpc9FKDP3QwGZ6yFajgvCSWkXgKw+Mi DHbBN7QeCqvHbsnhpD+BDax4fNshPVddOCtPm5qsctzSTSl2C1MC9/Qi3pVDyGPn3kn+ 9/u7NP2EVoBi0uY7PymGZirw6KPpfpIRvOuLa3QnK7kE91VePzZqwqP9ArCYLZocmAS+ PTEgG8YmG8aW6YY1Cz7ViMiq0FqEIKwEfb7SqcQw539B7jplrJwFaj+w0zbI9FXLmLPY B2GQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c04mI1x0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 10-20020a05621420ea00b0069f9faf39f4si4395203qvk.584.2024.04.19.11.47.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 19 Apr 2024 11:47:20 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c04mI1x0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rxtFc-00086z-Ad; Fri, 19 Apr 2024 14:46:20 -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 1rxtFX-00085c-IQ for qemu-devel@nongnu.org; Fri, 19 Apr 2024 14:46:15 -0400 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rxtFU-00011z-K3 for qemu-devel@nongnu.org; Fri, 19 Apr 2024 14:46:15 -0400 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-347c197a464so1623041f8f.2 for ; Fri, 19 Apr 2024 11:46:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713552371; x=1714157171; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=cD15Yu4sqNz4RqnI7YL4Ofu26geh4vOxHGToCiBD3jE=; b=c04mI1x0RibMi2WGYX6R0ljpHHK217XENKr/ySn3LWJlZAwhzZGeAHdeOVmjnenIp0 23xSWR5IBEQlco5NDZdqAckocQpK5AkQGUAhK07L/BoUriF4mqfYKefax7knKD2hYdM1 IGpnC5WzIlM/tFgTlQr8Xq54ud603QWgLO2/CT0w75WHThi3XrL60gdaF29w3P4lOKro Kz33lK2FYKvUsR/tagNXEduyZ7lpeiqh6zur1VK5SdKxqEGvzqY/x616v4hq6X5r+rxx 1QN2BA6eoDSB7tOv7KDREUwTV1XHW/CGTqcn9jn8mq3qIV2wy/toLcy0OG//RECw9c+8 uKeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713552371; x=1714157171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cD15Yu4sqNz4RqnI7YL4Ofu26geh4vOxHGToCiBD3jE=; b=WXCd9ND7DSBjgE81QXKo4ue58CUlE11vujE5FFNp6zaUpmRuHEn1fFt0lXPf7sfL64 PaKpf8lgQnOBi3vzY10QD1PIK/61kZ6BUZg5XjR8BwGnOXvd+Pg6snZpV2zdUUhoP5r2 PnWPFEJpe+xa5ctymS76grJ5r+hUgHVDWIXmBExH7f246q067p5PNg2/9zaGreUrxIH+ sT99V8/SXvq5ZeJ0EDWj3zK7p3oTYtmJjLVJT4VhtvOgmmbNokSjyLljz0AXgxW/auEL Cr29oUPBIofXISYvYmm08NpJJ8U/wHBjaBdW7CeCRFWvR73w1u1/EzFlQY0HUzQJ4t9J iOhw== X-Forwarded-Encrypted: i=1; AJvYcCXoDhAOAHh3IathcwxFESlMFoBLucnJ9F1MMR5wt5I5e0gS59p6rd9DjEEPstILTcsWgetFlt4CP1KJnPWm7IGNJDs7fE4= X-Gm-Message-State: AOJu0YxUakRxy5EM9BGitMVfXkzsQdLEjK+1OWSZzd7JABxf4Mi5B/5U B9mqH1lPDOfUsZcFm1+sxUII2/MWV81vMWk8t5g0jbP2aJjSa25pkphs1Jsf6QaipBnCLnaAvFW o X-Received: by 2002:adf:f6c3:0:b0:343:71c2:287e with SMTP id y3-20020adff6c3000000b0034371c2287emr1771485wrp.59.1713552370825; Fri, 19 Apr 2024 11:46:10 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id cs18-20020a056000089200b003437799a373sm5108718wrb.83.2024.04.19.11.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 11:46:10 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH 2/3] target/arm: Refactor default generic timer frequency handling Date: Fri, 19 Apr 2024 19:46:07 +0100 Message-Id: <20240419184608.2675213-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240419184608.2675213-1-peter.maydell@linaro.org> References: <20240419184608.2675213-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42b.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The generic timer frequency is settable by board code via a QOM property "cntfrq", but otherwise defaults to 62.5MHz. The way this is done includes some complication resulting from how this was originally a fixed value with no QOM property. Clean it up: * always set cpu->gt_cntfrq_hz to some sensible value, whether the CPU has the generic timer or not, and whether it's system or user-only emulation * this means we can always use gt_cntfrq_hz, and never need the old GTIMER_SCALE define * set the default value in exactly one place, in the realize fn The aim here is to pave the way for handling the ARMv8.6 requirement that the generic timer frequency is always 1GHz. We're going to do that by having old CPU types keep their legacy-in-QEMU behaviour and having the default for any new CPU types be a 1GHz rather han 62.5MHz cntfrq, so we want the point where the default is decided to be in one place, and in code, not in a DEFINE_PROP_UINT64() initializer. This commit should have no behavioural changes. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- target/arm/internals.h | 7 ++++--- target/arm/cpu.c | 31 +++++++++++++++++-------------- target/arm/helper.c | 16 ++++++++-------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/target/arm/internals.h b/target/arm/internals.h index dd3da211a3f..74d4b1b0990 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -59,10 +59,11 @@ static inline bool excp_is_internal(int excp) || excp == EXCP_SEMIHOST; } -/* Scale factor for generic timers, ie number of ns per tick. - * This gives a 62.5MHz timer. +/* + * Default frequency for the generic timer, in Hz. + * This is 62.5MHz, which gives a 16 ns tick period. */ -#define GTIMER_SCALE 16 +#define GTIMER_DEFAULT_HZ 62500000 /* Bit definitions for the v7M CONTROL register */ FIELD(V7M_CONTROL, NPRIV, 0, 1) diff --git a/target/arm/cpu.c b/target/arm/cpu.c index ab8d007a86c..b248b283423 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -1381,9 +1381,12 @@ static void arm_cpu_initfn(Object *obj) } } +/* + * 0 means "unset, use the default value". That default might vary depending + * on the CPU type, and is set in the realize fn. + */ static Property arm_cpu_gt_cntfrq_property = - DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, - NANOSECONDS_PER_SECOND / GTIMER_SCALE); + DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq_hz, 0); static Property arm_cpu_reset_cbar_property = DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0); @@ -1829,6 +1832,17 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp) return; } + if (!cpu->gt_cntfrq_hz) { + /* + * 0 means "the board didn't set a value, use the default". + * The default value of the generic timer frequency (as seen in + * CNTFRQ_EL0) is 62.5MHz, which corresponds to a period of 16ns. + * This is what you get (a) for a CONFIG_USER_ONLY CPU (b) if the + * board doesn't set it. + */ + cpu->gt_cntfrq_hz = GTIMER_DEFAULT_HZ; + } + #ifndef CONFIG_USER_ONLY /* The NVIC and M-profile CPU are two halves of a single piece of * hardware; trying to use one without the other is a command line @@ -1877,18 +1891,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp) } { - uint64_t scale; - - if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { - if (!cpu->gt_cntfrq_hz) { - error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz", - cpu->gt_cntfrq_hz); - return; - } - scale = gt_cntfrq_period_ns(cpu); - } else { - scale = GTIMER_SCALE; - } + uint64_t scale = gt_cntfrq_period_ns(cpu); cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale, arm_gt_ptimer_cb, cpu); diff --git a/target/arm/helper.c b/target/arm/helper.c index 8bdbb404195..01cf231a861 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -2461,6 +2461,13 @@ static const ARMCPRegInfo v6k_cp_reginfo[] = { .resetvalue = 0 }, }; +static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) +{ + ARMCPU *cpu = env_archcpu(env); + + cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz; +} + #ifndef CONFIG_USER_ONLY static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, @@ -3215,13 +3222,6 @@ void arm_gt_hvtimer_cb(void *opaque) gt_recalc_timer(cpu, GTIMER_HYPVIRT); } -static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) -{ - ARMCPU *cpu = env_archcpu(env); - - cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz; -} - static const ARMCPRegInfo generic_timer_cp_reginfo[] = { /* * Note that CNTFRQ is purely reads-as-written for the benefit @@ -3501,7 +3501,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = { .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), - .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, + .resetfn = arm_gt_cntfrq_reset, }, { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,