From nobody Fri Apr 26 15:54:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 155255310618517.64611320331096; Thu, 14 Mar 2019 01:45:06 -0700 (PDT) Received: from localhost ([127.0.0.1]:59114 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4Lz0-0000rY-VP for importer@patchew.org; Thu, 14 Mar 2019 04:44:59 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54490) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4LxE-0008Ef-AU for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4LxC-0002QP-Nx for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:07 -0400 Received: from 4.mo179.mail-out.ovh.net ([46.105.36.149]:60018) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4LxC-0002LU-DY for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:06 -0400 Received: from player762.ha.ovh.net (unknown [10.109.143.225]) by mo179.mail-out.ovh.net (Postfix) with ESMTP id 50C88121745 for ; Thu, 14 Mar 2019 09:43:03 +0100 (CET) Received: from kaod.org (lfbn-1-2226-17.w90-76.abo.wanadoo.fr [90.76.48.17]) (Authenticated sender: clg@kaod.org) by player762.ha.ovh.net (Postfix) with ESMTPSA id D8AE73AA2DBB; Thu, 14 Mar 2019 08:42:54 +0000 (UTC) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: Peter Maydell Date: Thu, 14 Mar 2019 09:42:31 +0100 Message-Id: <20190314084235.9887-2-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314084235.9887-1-clg@kaod.org> References: <20190314084235.9887-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 1447625806499908369 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedutddrhedugdduvdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 46.105.36.149 Subject: [Qemu-devel] [PATCH 1/5] aspeed/timer: Fix behaviour running Linux X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , qemu-arm@nongnu.org, Joel Stanley , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Joel Stanley The Linux kernel driver was updated in commit 4451d3f59f2a ("clocksource/drivers/fttmr010: Fix set_next_event handler) to fix an issue observed on hardware: > RELOAD register is loaded into COUNT register when the aspeed timer > is enabled, which means the next event may be delayed because timer > interrupt won't be generated until <0xFFFFFFFF - current_count + > cycles>. When running under Qemu, the system appeared "laggy". The guest is now scheduling timer events too regularly, starving the host of CPU time. This patch modifies the timer model to attempt to schedule the timer expiry as the guest requests, but if we have missed the deadline we re interrupt and try again, which allows the guest to catch up. Provides expected behaviour with old and new guest code. Fixes: c04bd47db6b9 ("hw/timer: Add ASPEED timer device model") Signed-off-by: Joel Stanley [clg: - merged a fix from Andrew Jeffery "Fire interrupt on failure to meet deadline" https://lists.ozlabs.org/pipermail/openbmc/2019-January/014641.html - adapted commit log - checkpatch fixes ] Signed-off-by: C=C3=A9dric Le Goater --- hw/timer/aspeed_timer.c | 59 ++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 5c786e512815..9ffd8e09f670 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -109,37 +109,40 @@ static inline uint64_t calculate_time(struct AspeedTi= mer *t, uint32_t ticks) =20 static uint64_t calculate_next(struct AspeedTimer *t) { - uint64_t next =3D 0; - uint32_t rate =3D calculate_rate(t); + uint64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + uint64_t next; =20 - while (!next) { - /* We don't know the relationship between the values in the match - * registers, so sort using MAX/MIN/zero. We sort in that order as= the - * timer counts down to zero. */ - uint64_t seq[] =3D { - calculate_time(t, MAX(t->match[0], t->match[1])), - calculate_time(t, MIN(t->match[0], t->match[1])), - calculate_time(t, 0), - }; - uint64_t reload_ns; - uint64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - - if (now < seq[0]) { - next =3D seq[0]; - } else if (now < seq[1]) { - next =3D seq[1]; - } else if (now < seq[2]) { - next =3D seq[2]; - } else if (t->reload) { - reload_ns =3D muldiv64(t->reload, NANOSECONDS_PER_SECOND, rate= ); - t->start =3D now - ((now - t->start) % reload_ns); - } else { - /* no reload value, return 0 */ - break; - } + /* + * We don't know the relationship between the values in the match + * registers, so sort using MAX/MIN/zero. We sort in that order as + * the timer counts down to zero. + */ + + next =3D calculate_time(t, MAX(t->match[0], t->match[1])); + if (now < next) { + return next; + } + + next =3D calculate_time(t, MIN(t->match[0], t->match[1])); + if (now < next) { + return next; + } + + next =3D calculate_time(t, 0); + if (now < next) { + return next; + } + + /* We've missed all deadlines, fire interrupt and try again */ + timer_del(&t->timer); + + if (timer_overflow_interrupt(t)) { + t->level =3D !t->level; + qemu_set_irq(t->irq, t->level); } =20 - return next; + t->start =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + return calculate_time(t, MAX(MAX(t->match[0], t->match[1]), 0)); } =20 static void aspeed_timer_mod(AspeedTimer *t) --=20 2.20.1 From nobody Fri Apr 26 15:54:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 1552553114669561.8207235047232; Thu, 14 Mar 2019 01:45:14 -0700 (PDT) Received: from localhost ([127.0.0.1]:59118 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4Lz8-00010T-Vu for importer@patchew.org; Thu, 14 Mar 2019 04:45:07 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54509) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4LxM-0008LU-0a for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4LxL-0002aQ-4J for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:15 -0400 Received: from 5.mo7.mail-out.ovh.net ([178.32.120.239]:46402) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4LxK-0002X6-SZ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:15 -0400 Received: from player762.ha.ovh.net (unknown [10.109.160.226]) by mo7.mail-out.ovh.net (Postfix) with ESMTP id 19CE510863E for ; Thu, 14 Mar 2019 09:43:11 +0100 (CET) Received: from kaod.org (lfbn-1-2226-17.w90-76.abo.wanadoo.fr [90.76.48.17]) (Authenticated sender: clg@kaod.org) by player762.ha.ovh.net (Postfix) with ESMTPSA id 4166C3AA2E1C; Thu, 14 Mar 2019 08:43:03 +0000 (UTC) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: Peter Maydell Date: Thu, 14 Mar 2019 09:42:32 +0100 Message-Id: <20190314084235.9887-3-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314084235.9887-1-clg@kaod.org> References: <20190314084235.9887-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 1449877606907022097 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedutddrhedugdduvdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 178.32.120.239 Subject: [Qemu-devel] [PATCH 2/5] aspeed/timer: Status register contains reload for stopped timer X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , qemu-arm@nongnu.org, Joel Stanley , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Andrew Jeffery From the datasheet: This register stores the current status of counter #N. When timer enable bit TMC30[N * b] is disabled, the reload register will be loaded into this counter. When timer bit TMC30[N * b] is set, the counter will start to decrement. CPU can update this register value when enable bit is set. Signed-off-by: Andrew Jeffery Signed-off-by: C=C3=A9dric Le Goater --- hw/timer/aspeed_timer.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 9ffd8e09f670..2f8522a597d3 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -187,7 +187,11 @@ static uint64_t aspeed_timer_get_value(AspeedTimer *t,= int reg) =20 switch (reg) { case TIMER_REG_STATUS: - value =3D calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)= ); + if (timer_enabled(t)) { + value =3D calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRT= UAL)); + } else { + value =3D t->reload; + } break; case TIMER_REG_RELOAD: value =3D t->reload; --=20 2.20.1 From nobody Fri Apr 26 15:54:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 1552553255546896.6537851323154; Thu, 14 Mar 2019 01:47:35 -0700 (PDT) Received: from localhost ([127.0.0.1]:59172 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4M1W-0002hE-D5 for importer@patchew.org; Thu, 14 Mar 2019 04:47:34 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54534) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4LxV-0008SH-Ml for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4LxU-0002mP-LQ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:25 -0400 Received: from 8.mo69.mail-out.ovh.net ([46.105.56.233]:57817) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4LxU-0002i4-80 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:24 -0400 Received: from player762.ha.ovh.net (unknown [10.109.159.20]) by mo69.mail-out.ovh.net (Postfix) with ESMTP id C9DB744154 for ; Thu, 14 Mar 2019 09:43:19 +0100 (CET) Received: from kaod.org (lfbn-1-2226-17.w90-76.abo.wanadoo.fr [90.76.48.17]) (Authenticated sender: clg@kaod.org) by player762.ha.ovh.net (Postfix) with ESMTPSA id DFA0B3AA2E84; Thu, 14 Mar 2019 08:43:11 +0000 (UTC) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: Peter Maydell Date: Thu, 14 Mar 2019 09:42:33 +0100 Message-Id: <20190314084235.9887-4-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314084235.9887-1-clg@kaod.org> References: <20190314084235.9887-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 1452129406510271249 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedutddrhedugdduvdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 46.105.56.233 Subject: [Qemu-devel] [PATCH 3/5] aspeed/timer: Fix match calculations X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , qemu-arm@nongnu.org, Joel Stanley , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Andrew Jeffery If the match value exceeds reload then we don't want to include it in calculations for the next event. Signed-off-by: Andrew Jeffery Signed-off-by: C=C3=A9dric Le Goater --- hw/timer/aspeed_timer.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 2f8522a597d3..6c184572ea04 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -107,6 +107,11 @@ static inline uint64_t calculate_time(struct AspeedTim= er *t, uint32_t ticks) return t->start + delta_ns; } =20 +static inline uint32_t calculate_match(struct AspeedTimer *t, int i) +{ + return t->match[i] < t->reload ? t->match[i] : 0; +} + static uint64_t calculate_next(struct AspeedTimer *t) { uint64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); @@ -118,12 +123,12 @@ static uint64_t calculate_next(struct AspeedTimer *t) * the timer counts down to zero. */ =20 - next =3D calculate_time(t, MAX(t->match[0], t->match[1])); + next =3D calculate_time(t, MAX(calculate_match(t, 0), calculate_match(= t, 1))); if (now < next) { return next; } =20 - next =3D calculate_time(t, MIN(t->match[0], t->match[1])); + next =3D calculate_time(t, MIN(calculate_match(t, 0), calculate_match(= t, 1))); if (now < next) { return next; } @@ -141,8 +146,10 @@ static uint64_t calculate_next(struct AspeedTimer *t) qemu_set_irq(t->irq, t->level); } =20 + next =3D MAX(MAX(calculate_match(t, 0), calculate_match(t, 1)), 0); t->start =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - return calculate_time(t, MAX(MAX(t->match[0], t->match[1]), 0)); + + return calculate_time(t, next); } =20 static void aspeed_timer_mod(AspeedTimer *t) --=20 2.20.1 From nobody Fri Apr 26 15:54:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 155255327020365.13697649031246; Thu, 14 Mar 2019 01:47:50 -0700 (PDT) Received: from localhost ([127.0.0.1]:59174 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4M1l-0002qg-86 for importer@patchew.org; Thu, 14 Mar 2019 04:47:49 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54563) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4Lxj-0000BY-3Z for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4Lxh-00036p-BR for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:38 -0400 Received: from 10.mo68.mail-out.ovh.net ([46.105.79.203]:50000) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4Lxd-0002zC-MJ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:35 -0400 Received: from player762.ha.ovh.net (unknown [10.109.146.106]) by mo68.mail-out.ovh.net (Postfix) with ESMTP id ED91311D84E for ; Thu, 14 Mar 2019 09:43:30 +0100 (CET) Received: from kaod.org (lfbn-1-2226-17.w90-76.abo.wanadoo.fr [90.76.48.17]) (Authenticated sender: clg@kaod.org) by player762.ha.ovh.net (Postfix) with ESMTPSA id C653D3AA2F18; Thu, 14 Mar 2019 08:43:19 +0000 (UTC) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: Peter Maydell Date: Thu, 14 Mar 2019 09:42:34 +0100 Message-Id: <20190314084235.9887-5-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314084235.9887-1-clg@kaod.org> References: <20190314084235.9887-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 1455225630926932753 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedutddrhedugdduvdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 46.105.79.203 Subject: [Qemu-devel] [PATCH 4/5] aspeed/timer: Provide back-pressure information for short periods X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , qemu-arm@nongnu.org, Joel Stanley , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Andrew Jeffery First up: This is not the way the hardware behaves. However, it helps resolve real-world problems with short periods being used under Linux. Commit 4451d3f59f2a ("clocksource/drivers/fttmr010: Fix set_next_event handler") in Linux fixed the timer driver to correctly schedule the next event for the Aspeed controller, and in combination with 5daa8212c08e ("ARM: dts: aspeed: Describe random number device") Linux will now set a timer with a period as low as 1us. Configuring a qemu timer with such a short period results in spending time handling the interrupt in the model rather than executing guest code, leading to noticeable "sticky" behaviour in the guest. The behaviour of Linux is correct with respect to the hardware, so we need to improve our handling under emulation. The approach chosen is to provide back-pressure information by calculating an acceptable minimum number of ticks to be set on the model. Under Linux an additional read is added in the timer configuration path to detect back-pressure, which will never occur on hardware. However if back-pressure is observed, the driver alerts the clock event subsystem, which then performs its own next event dilation via a config option - d1748302f70b ("clockevents: Make minimum delay adjustments configurable") A minimum period of 5us was experimentally determined on a Lenovo T480s, which I've increased to 20us for "safety". Signed-off-by: Andrew Jeffery Signed-off-by: C=C3=A9dric Le Goater --- include/hw/timer/aspeed_timer.h | 1 + hw/misc/aspeed_scu.c | 6 ++++++ hw/timer/aspeed_timer.c | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/hw/timer/aspeed_timer.h b/include/hw/timer/aspeed_time= r.h index 1fb949e16710..10c851ebb6d7 100644 --- a/include/hw/timer/aspeed_timer.h +++ b/include/hw/timer/aspeed_timer.h @@ -41,6 +41,7 @@ typedef struct AspeedTimer { * interrupts, signalling with both the rising and falling edge. */ int32_t level; + uint32_t min_ticks; uint32_t reload; uint32_t match[2]; uint64_t start; diff --git a/hw/misc/aspeed_scu.c b/hw/misc/aspeed_scu.c index 51498ce70be6..797a07c2c45a 100644 --- a/hw/misc/aspeed_scu.c +++ b/hw/misc/aspeed_scu.c @@ -427,6 +427,12 @@ static void aspeed_scu_realize(DeviceState *dev, Error= **errp) TYPE_ASPEED_SCU, SCU_IO_REGION_SIZE); =20 sysbus_init_mmio(sbd, &s->iomem); + + /* + * Reset on realize to ensure the APB clock value is calculated in tim= e for + * use by the timer model, which is reset before the SCU. + */ + aspeed_scu_reset(dev); } =20 static const VMStateDescription vmstate_aspeed_scu =3D { diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 6c184572ea04..9988b8fbbf17 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -259,7 +259,7 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState= *s, int timer, int reg, switch (reg) { case TIMER_REG_RELOAD: old_reload =3D t->reload; - t->reload =3D value; + t->reload =3D value < t->min_ticks ? t->min_ticks : value; =20 /* If the reload value was not previously set, or zero, and * the current value is valid, try to start the timer if it is @@ -311,7 +311,11 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, b= ool enable) =20 static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable) { + AspeedTimerCtrlState *s =3D timer_to_ctrl(t); + uint32_t rate =3D enable ? TIMER_CLOCK_EXT_HZ : s->scu->apb_freq; + trace_aspeed_timer_ctrl_external_clock(t->id, enable); + t->min_ticks =3D muldiv64(20 * SCALE_US, rate, NANOSECONDS_PER_SECOND); } =20 static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enab= le) --=20 2.20.1 From nobody Fri Apr 26 15:54:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 1552553320629183.6668416572361; Thu, 14 Mar 2019 01:48:40 -0700 (PDT) Received: from localhost ([127.0.0.1]:59176 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4M2Z-0003Qt-K8 for importer@patchew.org; Thu, 14 Mar 2019 04:48:39 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54622) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4Lxy-0000Qf-10 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4Lxw-0003Wi-Mp for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:53 -0400 Received: from 11.mo5.mail-out.ovh.net ([46.105.47.167]:56000) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4Lxw-0003CK-Fc for qemu-devel@nongnu.org; Thu, 14 Mar 2019 04:43:52 -0400 Received: from player762.ha.ovh.net (unknown [10.109.146.50]) by mo5.mail-out.ovh.net (Postfix) with ESMTP id 2F18621E57B for ; Thu, 14 Mar 2019 09:43:41 +0100 (CET) Received: from kaod.org (lfbn-1-2226-17.w90-76.abo.wanadoo.fr [90.76.48.17]) (Authenticated sender: clg@kaod.org) by player762.ha.ovh.net (Postfix) with ESMTPSA id ED9083AA3031; Thu, 14 Mar 2019 08:43:30 +0000 (UTC) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: Peter Maydell Date: Thu, 14 Mar 2019 09:42:35 +0100 Message-Id: <20190314084235.9887-6-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190314084235.9887-1-clg@kaod.org> References: <20190314084235.9887-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 1458321854415735569 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedutddrhedugdduvdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 46.105.47.167 Subject: [Qemu-devel] [PATCH 5/5] aspeed/timer: Use signed muldiv for timer resets X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christian Svensson , Andrew Jeffery , qemu-devel@nongnu.org, qemu-arm@nongnu.org, =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Joel Stanley Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Christian Svensson If the host decrements the counter register that results in a negative delta. This is then passed to muldiv64 which only handles unsigned numbers resulting in bogus results. This fix ensures the data being operated on is signed before it is ultimately casted to the final unsigned value. Test case: kexec a kernel using aspeed_timer and it will freeze on the second bootup when the kernel initializes the timer. With this patch that no longer happens and the timer appears to run OK. Signed-off-by: Christian Svensson [clg: - checkpatch fixes ] Signed-off-by: C=C3=A9dric Le Goater --- hw/timer/aspeed_timer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 9988b8fbbf17..0b16eac8970c 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -275,7 +275,8 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState= *s, int timer, int reg, int64_t delta =3D (int64_t) value - (int64_t) calculate_ticks(= t, now); uint32_t rate =3D calculate_rate(t); =20 - t->start +=3D muldiv64(delta, NANOSECONDS_PER_SECOND, rate); + t->start =3D (int64_t)t->start + + ((__int128_t)delta * NANOSECONDS_PER_SECOND / rate); aspeed_timer_mod(t); } break; --=20 2.20.1