Received: by 2002:a19:6911:0:0:0:0:0 with SMTP id e17csp1006915lfc; Mon, 2 Dec 2019 20:16:40 -0800 (PST) X-Google-Smtp-Source: APXvYqxKwyV2uG7XkwNV8U3pvckvXYLmSVJp/SqDQCq4ic6XphqnqXOXJbpY25r6UXBLJypK63If X-Received: by 2002:a37:a685:: with SMTP id p127mr3248030qke.449.1575346600570; Mon, 02 Dec 2019 20:16:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575346600; cv=none; d=google.com; s=arc-20160816; b=jbpxWtzrE8NbPDVv/sjo+BNXkmG4vnRvEA0+LUWx1O4umT/0VheTHqL3IU3IIkN7Gh YywFQnZlQy+zh2M72fVt4UtumgRlBjwvO8K11vjDo01vDiXncy2J/IdCy/dXBTem0/Vy br6Pe2lBuR6z9O1xW5HiJrfuPLuFiDTI+OysE+fTr0nsgeG6leZrUG9E6XqwRA0zvHAh vBiMTza6EZYYWkh86DjIHGSPpxZoT0uj2jjjkfGeGlF4p/a0O/H2rc4CgJEGs7um87fv 4wDlDTDKy6cpic17LTpfzIXAIIRNz4VpjaVy7EfquWognI6uvOM0mtacfqU00wZHcTJs 5mSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:dkim-signature; bh=UnMHwPPfQ4KRN61OUf/u4EXG5kQhV6gU2lUxKArzxDo=; b=IP8kZsDLsKnfFt+wLArfdRJIucLqFRbhJfY4JTS5+zkr6EXgPyZXhsZLWrAZCsJqaB JJj4JR9XUyUkl9QEi3dHrrs8mSMBXzKShZzc3qiN2wA6sOwd+MJpmFrgQEGl8bpRCpir s5yvnAB3M72ftoU9SULIsF6FyZ3O5OFIBLKQzyh1kN27jTUSPZz0PyhHVhhwUGRObyMH 3EYYmOlz8V8ePZzLbljGV5+wU4QoJ2Jsv585UOzZBNooXbR4UGt51LZcnP0uu+r//FWP xh6FKT3yiy5xg1j/xY8VNjd21F7h1OH7XJuIAy3Z0Xey0D1O8EMLabdHWmVrchbBPBHy bt3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@aj.id.au header.s=fm1 header.b=jv6kpy63; dkim=fail header.i=@messagingengine.com header.s=fm1 header.b=uT9tE2oL; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id f56si1209200qtk.272.2019.12.02.20.16.40 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 02 Dec 2019 20:16:40 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@aj.id.au header.s=fm1 header.b=jv6kpy63; dkim=fail header.i=@messagingengine.com header.s=fm1 header.b=uT9tE2oL; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org" Received: from localhost ([::1]:48242 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ibzc7-0000QU-Ku for alex.bennee@linaro.org; Mon, 02 Dec 2019 23:16:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34016) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ibzZ3-0005hV-FJ for qemu-arm@nongnu.org; Mon, 02 Dec 2019 23:13:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ibzZ1-0001Fs-G5 for qemu-arm@nongnu.org; Mon, 02 Dec 2019 23:13:29 -0500 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]:56489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ibzZ1-0001El-7c; Mon, 02 Dec 2019 23:13:27 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 08B10ED2; Mon, 2 Dec 2019 23:13:25 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 02 Dec 2019 23:13:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aj.id.au; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=UnMHwPPfQ4KRN 61OUf/u4EXG5kQhV6gU2lUxKArzxDo=; b=jv6kpy63xqnWAhau27nV+ZCposTPf kiQ61fjwkzCXyx/Nk8bWtxMAnkHfKVViOVY6PuS4AA0EoEMDAdlaNL4FsmyLGDtJ rt/WrlC9YEZy+fSFRIOMgQOImGseiVyLrxYGTc3JUtFUzf1vNQTYHVHK9sltf/i8 6nxyFr72oG1+huUPdAydtc5eV/JL5uvxTze4u3sRc13hrUlTAYXewTBO+SiZmRLY txueFKm1YRVYmq44VXzQnLBq3qeSAAQ/c8ELJNsLyLLmKOqgWtbzNfuVvZ0Pxzsk a/EA/R3XwU+weeGsAnc9R7JDTQyZ9GiuV7VNtwQUybiR8qVBQeTE6mJTg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=UnMHwPPfQ4KRN61OUf/u4EXG5kQhV6gU2lUxKArzxDo=; b=uT9tE2oL Y9GYFM8myJifEkSq33gIoLUwOb63A9AWdaSmY1Qmn8x/WnFmmIv8d2cOsTRTb3DR vxqo3/C/xn4ssizCYw1XEJDyprvPyirB7GT2NcgRk7wOIvSwNI83cwTRH/Nxe7or BYo3kIMWkXQPrk+fmrUIBm4nsy4zWRCwnBJ+L1X+WaWQXCtDWpLFywlYDwXPQzBv X1NYDo/mjfaQcKzapjqDZLWYfiY1LPY4MWO275HE2o09CKkHC55M9oGxdPBbxMGH oHxRkK4sbptx0G5IrmZVJJVK1IFmoIfU3R5uZDSdq1iOelYt/coWxTVov1vzyMd3 ta+nnviQ9FhXFg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudejiedgieeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeetnhgurhgv ficulfgvfhhfvghrhicuoegrnhgurhgvfiesrghjrdhiugdrrghuqeenucfkphepvddtvd drkedurddukedrfedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrnhgurhgvfiesrghj rdhiugdrrghunecuvehluhhsthgvrhfuihiivgepud X-ME-Proxy: Received: from mistburn.au.ibm.com (bh02i525f01.au.ibm.com [202.81.18.30]) by mail.messagingengine.com (Postfix) with ESMTPA id EE4028005A; Mon, 2 Dec 2019 23:13:22 -0500 (EST) From: Andrew Jeffery To: qemu-arm@nongnu.org Subject: [PATCH v2 3/4] target/arm: Prepare generic timer for per-platform CNTFRQ Date: Tue, 3 Dec 2019 14:44:39 +1030 Message-Id: <20191203041440.6275-4-andrew@aj.id.au> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191203041440.6275-1-andrew@aj.id.au> References: <20191203041440.6275-1-andrew@aj.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 64.147.123.24 X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, Richard Henderson , clg@kaod.org, joel@jms.id.au Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: "Qemu-arm" X-TUID: GL3gHXPGUQAv The ASPEED AST2600 clocks the generic timer at the rate of HPLL. On recent firmwares this is at 1125MHz, which is considerably quicker than the assumed 62.5MHz of the current generic timer implementation. The delta between the value as read from CNTFRQ and the true rate of the underlying QEMUTimer leads to sticky behaviour in AST2600 guests. Add a feature-gated property exposing CNTFRQ for ARM CPUs providing the generic timer. This allows platforms to configure CNTFRQ (and the associated QEMUTimer) to the appropriate frequency prior to starting the guest. As the platform can now determine the rate of CNTFRQ we're exposed to limitations of QEMUTimer that didn't previously materialise: In the course of emulation we need to arbitrarily and accurately convert between guest ticks and time, but we're constrained by QEMUTimer's use of an integer scaling factor. The effect is QEMUTimer cannot exactly capture the period of frequencies that do not cleanly divide NANOSECONDS_PER_SECOND for scaling ticks to time. As such, provide an equally inaccurate scaling factor for scaling time to ticks so at least a self-consistent inverse relationship holds. Signed-off-by: Andrew Jeffery Reviewed-by: Richard Henderson --- target/arm/cpu.c | 43 +++++++++++++++++++++++++++++++++---------- target/arm/cpu.h | 18 ++++++++++++++++++ target/arm/helper.c | 9 ++++++++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/target/arm/cpu.c b/target/arm/cpu.c index 5698a74061bb..f186019a77fd 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -974,10 +974,12 @@ static void arm_cpu_initfn(Object *obj) if (tcg_enabled()) { cpu->psci_version = 2; /* TCG implements PSCI 0.2 */ } - - cpu->gt_cntfrq = NANOSECONDS_PER_SECOND / GTIMER_SCALE; } +static Property arm_cpu_gt_cntfrq_property = + DEFINE_PROP_UINT64("cntfrq", ARMCPU, gt_cntfrq, + NANOSECONDS_PER_SECOND / GTIMER_SCALE); + static Property arm_cpu_reset_cbar_property = DEFINE_PROP_UINT64("reset-cbar", ARMCPU, reset_cbar, 0); @@ -1174,6 +1176,11 @@ void arm_cpu_post_init(Object *obj) qdev_property_add_static(DEVICE(obj), &arm_cpu_cfgend_property, &error_abort); + + if (arm_feature(&cpu->env, ARM_FEATURE_GENERIC_TIMER)) { + qdev_property_add_static(DEVICE(cpu), &arm_cpu_gt_cntfrq_property, + &error_abort); + } } static void arm_cpu_finalizefn(Object *obj) @@ -1253,14 +1260,30 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp) } } - cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, - arm_gt_ptimer_cb, cpu); - cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, - arm_gt_vtimer_cb, cpu); - cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, - arm_gt_htimer_cb, cpu); - cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, GTIMER_SCALE, - arm_gt_stimer_cb, cpu); + + { + uint64_t scale; + + if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { + if (!cpu->gt_cntfrq) { + error_setg(errp, "Invalid CNTFRQ: %"PRId64"Hz", + cpu->gt_cntfrq); + return; + } + scale = gt_cntfrq_period_ns(cpu); + } else { + scale = GTIMER_SCALE; + } + + cpu->gt_timer[GTIMER_PHYS] = timer_new(QEMU_CLOCK_VIRTUAL, scale, + arm_gt_ptimer_cb, cpu); + cpu->gt_timer[GTIMER_VIRT] = timer_new(QEMU_CLOCK_VIRTUAL, scale, + arm_gt_vtimer_cb, cpu); + cpu->gt_timer[GTIMER_HYP] = timer_new(QEMU_CLOCK_VIRTUAL, scale, + arm_gt_htimer_cb, cpu); + cpu->gt_timer[GTIMER_SEC] = timer_new(QEMU_CLOCK_VIRTUAL, scale, + arm_gt_stimer_cb, cpu); + } #endif cpu_exec_realizefn(cs, &local_err); diff --git a/target/arm/cpu.h b/target/arm/cpu.h index 666c03871fdf..0bcd13dcac81 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -939,6 +939,24 @@ struct ARMCPU { static inline unsigned int gt_cntfrq_period_ns(ARMCPU *cpu) { + /* + * The exact approach to calculating guest ticks is: + * + * muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), cpu->gt_cntfrq, + * NANOSECONDS_PER_SECOND); + * + * We don't do that. Rather we intentionally use integer division + * truncation below and in the caller for the conversion of host monotonic + * time to guest ticks to provide the exact inverse for the semantics of + * the QEMUTimer scale factor. QEMUTimer's scale facter is an integer, so + * it loses precision when representing frequencies where + * `(NANOSECONDS_PER_SECOND % cpu->gt_cntfrq) > 0` holds. Failing to + * provide an exact inverse leads to scheduling timers with negative + * periods, which in turn leads to sticky behaviour in the guest. + * + * Finally, CNTFRQ is effectively capped at 1GHz to ensure our scale factor + * cannot become zero. + */ /* XXX: Could include qemu/timer.h to get NANOSECONDS_PER_SECOND? */ const unsigned int ns_per_s = 1000 * 1000 * 1000; return ns_per_s > cpu->gt_cntfrq ? ns_per_s / cpu->gt_cntfrq : 1; diff --git a/target/arm/helper.c b/target/arm/helper.c index 2622a9a8d02f..da960d17040b 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -2683,6 +2683,13 @@ void arm_gt_stimer_cb(void *opaque) gt_recalc_timer(cpu, GTIMER_SEC); } +static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) +{ + ARMCPU *cpu = env_archcpu(env); + + cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq; +} + static const ARMCPRegInfo generic_timer_cp_reginfo[] = { /* Note that CNTFRQ is purely reads-as-written for the benefit * of software; writing it doesn't actually change the timer frequency. @@ -2697,7 +2704,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = { .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), - .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, + .resetfn = arm_gt_cntfrq_reset, }, /* overall control: mostly access permissions */ { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, -- 2.20.1