From patchwork Sat May 25 15:12:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?C=C3=A9dric_Le_Goater?= X-Patchwork-Id: 165180 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp4949645ili; Sat, 25 May 2019 08:18:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqzUNcCFOGfn/5xN0mQY9BHgwI993q+MnSblowKeY0XSfBjBnz6YcCE+XbQJCwraCgV2wbIH X-Received: by 2002:a67:ff85:: with SMTP id v5mr33909014vsq.10.1558797532845; Sat, 25 May 2019 08:18:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558797532; cv=none; d=google.com; s=arc-20160816; b=R689VGK/4nr8n8bcKivhaVpo4OmapHG4YDrx3PeNbumWz3vZXbVS1xRXgKvHI3sizk eK+xslhcqtACa2OprhFUm8i0qSVyRvukG1IsFmbhcpMFOlOf0/9Emg5DcWITys4us1zd 86PTcPMX1UMtwcQZ1d52mSl6c3dbwWL7VddMwnznn44FdeDFsSqlUhU+b0MWOxXhD8nY Vim9HmuK/63Rtjt+kBGu6a/DqGTbbccWG2Kz6kQLxaDUq0xhsC2R4uilCP13WPHRySW0 MRNQgYkytF1j5Q/dFWMdv4TkXd+qC6/+NDXACtg8jTV1c4fdFec65kZCm1u4eTbtj8ki kiwQ== 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:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from; bh=2VRFZzseqkasJR9ZEx928Vijh3xcsQhytLYJ3MVsijo=; b=kFoR4yUxgA8NoyjTnBe2vb5gc5AB6U2S8s8pq0x6LvdbHzCb8jrihCMIYC1cyFdsrO 6U5HqpEjPeClIGv/PLsceN6fU7u6IerhVdYG/mDOYGYftWFWH6FyknIGE6pMWmPWeAGY Q0e8mR84IqNhhzahJcJIi1hviJ0VfNOsMQ6Le2DsuDXAmuWy8SvXZJ/mlIJPup8HVWD1 bo+fSzPUzE8XudJQGI9Wm3FvYUtvt0m02tD9NLwQ5IM5JKR18Ah3ucMOfIh7FVDo92Eu g7lOeheJzwco5A6EOsM0rCeqxs6ciCMDQKXDaLqYCO9RuPU2MCGTHP3m+y1zQ1n7S12R hY+A== ARC-Authentication-Results: i=1; mx.google.com; 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" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a8si1950476vsn.257.2019.05.25.08.18.52 for (version=TLS1 cipher=AES128-SHA bits=128/128); Sat, 25 May 2019 08:18:52 -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; 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" Received: from localhost ([127.0.0.1]:43250 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hUYRg-0001Zq-CP for patch@linaro.org; Sat, 25 May 2019 11:18:52 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33035) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hUYMr-0005wi-If for qemu-devel@nongnu.org; Sat, 25 May 2019 11:13:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hUYMq-0001pn-AN for qemu-devel@nongnu.org; Sat, 25 May 2019 11:13:53 -0400 Received: from 12.mo5.mail-out.ovh.net ([46.105.39.65]:46307) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hUYMq-0001oW-5G for qemu-devel@nongnu.org; Sat, 25 May 2019 11:13:52 -0400 Received: from player698.ha.ovh.net (unknown [10.108.35.185]) by mo5.mail-out.ovh.net (Postfix) with ESMTP id 81890237847 for ; Sat, 25 May 2019 17:13:50 +0200 (CEST) Received: from kaod.org (lfbn-1-10649-41.w90-89.abo.wanadoo.fr [90.89.235.41]) (Authenticated sender: clg@kaod.org) by player698.ha.ovh.net (Postfix) with ESMTPSA id E09BB625A7BA; Sat, 25 May 2019 15:13:43 +0000 (UTC) From: =?utf-8?q?C=C3=A9dric_Le_Goater?= To: Peter Maydell Date: Sat, 25 May 2019 17:12:30 +0200 Message-Id: <20190525151241.5017-9-clg@kaod.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190525151241.5017-1-clg@kaod.org> References: <20190525151241.5017-1-clg@kaod.org> MIME-Version: 1.0 X-Ovh-Tracer-Id: 6606217703496780561 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduuddruddukedgkeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddm X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 46.105.39.65 Subject: [Qemu-devel] [PATCH 08/19] 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_Le_Goater?= , qemu-arm@nongnu.org, qemu-devel@nongnu.org, Joel Stanley Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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(-) -- 2.20.1 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)); } static void aspeed_timer_mod(AspeedTimer *t)