From mboxrd@z Thu Jan 1 00:00:00 1970 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 37595CE7A81 for ; Mon, 25 Sep 2023 09:21:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkhlp-0006Kn-1K; Mon, 25 Sep 2023 05:20:49 -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 1qkhlj-0006Im-0D; Mon, 25 Sep 2023 05:20:45 -0400 Received: from mail.ozlabs.org ([2404:9400:2221:ea00::3] helo=gandalf.ozlabs.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qkhld-0005t3-MU; Mon, 25 Sep 2023 05:20:42 -0400 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4RvHR35p2Yz4xPR; Mon, 25 Sep 2023 19:20:31 +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 4RvHR20Hsrz4xPQ; Mon, 25 Sep 2023 19:20:29 +1000 (AEST) Message-ID: Date: Mon, 25 Sep 2023 11:20:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [Qemu-devel] [PATCH 08/19] aspeed/timer: Fix behaviour running Linux Content-Language: en-US To: Andrew Jeffery , Peter Maydell Cc: =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9_via?= , Cameron Esfahani via , Joel Stanley References: <20190525151241.5017-1-clg@kaod.org> <20190525151241.5017-9-clg@kaod.org> From: =?UTF-8?Q?C=c3=a9dric_Le_Goater?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2404:9400:2221:ea00::3; envelope-from=SRS0=n5Cv=FJ=kaod.org=clg@ozlabs.org; helo=gandalf.ozlabs.org X-Spam_score_int: -53 X-Spam_score: -5.4 X-Spam_bar: ----- X-Spam_report: (-5.4 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, NICE_REPLY_A=-1.473, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On 9/25/23 09:54, Andrew Jeffery wrote: > > > On Fri, 22 Sep 2023, at 22:51, Cédric Le Goater wrote: >> Joel, Andrew, >> >> On 5/25/19 17:12, Cédric Le Goater wrote: >>> 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édric 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 AspeedTimer *t, uint32_t ticks) >>> >>> static uint64_t calculate_next(struct AspeedTimer *t) >>> { >>> - uint64_t next = 0; >>> - uint32_t rate = calculate_rate(t); >>> + uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); >>> + uint64_t next; >>> >>> - 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[] = { >>> - 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 = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); >>> - >>> - if (now < seq[0]) { >>> - next = seq[0]; >>> - } else if (now < seq[1]) { >>> - next = seq[1]; >>> - } else if (now < seq[2]) { >>> - next = seq[2]; >>> - } else if (t->reload) { >>> - reload_ns = muldiv64(t->reload, NANOSECONDS_PER_SECOND, rate); >>> - t->start = 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 = calculate_time(t, MAX(t->match[0], t->match[1])); >>> + if (now < next) { >>> + return next; >>> + } >>> + >>> + next = calculate_time(t, MIN(t->match[0], t->match[1])); >>> + if (now < next) { >>> + return next; >>> + } >>> + >>> + next = 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 = !t->level; >>> + qemu_set_irq(t->irq, t->level); >>> } >>> >>> - return next; >>> + t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); >>> + return calculate_time(t, MAX(MAX(t->match[0], t->match[1]), 0)); >> >> This MAX(MAX(x, y), 0) looks strange to me. Would you remember where it comes >> from ? Thanks, > > The inner MAX() deals with the lack of ordering constraints between the match values. I think the outer MAX() is redundant. We should probably remove it. The match member is type uint32_t so it can't be negative. You did steal that from an RFC patch :D I did ! Fixed there : https://patchwork.ozlabs.org/project/qemu-devel/patch/20230922155924.1172019-5-clg@kaod.org/ Cheers, C.