From patchwork Tue Jun 13 14:07:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 105358 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp432646qgd; Tue, 13 Jun 2017 07:25:35 -0700 (PDT) X-Received: by 10.200.54.152 with SMTP id a24mr200762qtc.66.1497363935649; Tue, 13 Jun 2017 07:25:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497363935; cv=none; d=google.com; s=arc-20160816; b=MisDp5lMditExUOd953B/wlojB847PjkNNEhi72D3eJsKUju8hn4atnPEvT+udkljp dNtnzpz/T7SrjgSVns7yH47X7hy0cuci8Gn7IqUiJP1rbAtgQCU9rSnJodM0wbrztQPA CLflGtxo9hQgLD/AKPtZQR3BuKWhDOWExr/cS4bX2w88K0BBTmQA+AfksZBC9yZD6BP2 4A1l7TJsWEVBQpRast1xeh8zr9NAZKeA1XYmAFc541PLwpVzn1/cwhgh6PToowqZlfft lR5AmDxEl9perueF76ZirnD2MTRty90IK4Ju5KKm1GaM3rxa0IFvKatl8hQ3SbF9/E2i oVMA== 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:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:arc-authentication-results; bh=tXMPNGT2vv5BX82hxTWW9hNlFdaHAS8QRDkFhu5ssak=; b=ZorPVMVsPzw06uqDHoF+zJ99VYhjTWYkORT2FQphsj0LZyNWz0mvayjCyTBGIrhE8X 1bpM1DtGr65Bpf175+FyBj4ArFFG264hwoMZ56PHl14KV7M0U7oDvhh/wYTtSeKq65ec AIu7YDHE8awPK5ADRjbdZiisWBzJ8vjBRfVYiILwee87Nmh3nEpHlQ2wCSpgbxEewYzZ hAJleHpvKnLmrlBLGdG8xSARJ0XYUjbP12KV6EpHDUdf4xXtnxcfJI6M0QMIxs/BT1rY XsTt6/7dNM7NQR5E3N7B9qh1zXQAh5dD7TF4KMpAddgg8SVSh2UlRYY1EXU2DsLocA2y TyjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [208.118.235.17]) by mx.google.com with ESMTPS id b128si11670199qkf.321.2017.06.13.07.25.35 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 13 Jun 2017 07:25:35 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:43560 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKmlA-0005Gl-VI for patch@linaro.org; Tue, 13 Jun 2017 10:25:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33241) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKmTf-0005LO-IA for qemu-devel@nongnu.org; Tue, 13 Jun 2017 10:07:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKmTZ-0006gg-Ar for qemu-devel@nongnu.org; Tue, 13 Jun 2017 10:07:27 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:37257) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dKmTZ-0006cz-2G for qemu-devel@nongnu.org; Tue, 13 Jun 2017 10:07:21 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1dKmTU-00014t-MH for qemu-devel@nongnu.org; Tue, 13 Jun 2017 15:07:16 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 13 Jun 2017 15:07:02 +0100 Message-Id: <1497362826-21125-14-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1497362826-21125-1-git-send-email-peter.maydell@linaro.org> References: <1497362826-21125-1-git-send-email-peter.maydell@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PULL 13/17] timer/aspeed: fix timer enablement when a reload is not set 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Cédric Le Goater When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid. Signed-off-by: Cédric Le Goater Reviewed-by: Andrew Jeffery Message-id: 1496739312-32304-1-git-send-email-clg@kaod.org Signed-off-by: Peter Maydell --- hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) -- 2.7.4 diff --git a/hw/timer/aspeed_timer.c b/hw/timer/aspeed_timer.c index 9b70ee0..50acbf5 100644 --- a/hw/timer/aspeed_timer.c +++ b/hw/timer/aspeed_timer.c @@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1]; } else if (now < seq[2]) { next = seq[2]; - } else { + } 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; } } return next; } +static void aspeed_timer_mod(AspeedTimer *t) +{ + uint64_t next = calculate_next(t); + if (next) { + timer_mod(&t->timer, next); + } +} + static void aspeed_timer_expire(void *opaque) { AspeedTimer *t = opaque; @@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque) qemu_set_irq(t->irq, t->level); } - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg) @@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) { AspeedTimer *t; + uint32_t old_reload; trace_aspeed_timer_set_value(timer, reg, value); t = &s->timers[timer]; switch (reg) { + case TIMER_REG_RELOAD: + old_reload = t->reload; + t->reload = value; + + /* If the reload value was not previously set, or zero, and + * the current value is valid, try to start the timer if it is + * enabled. + */ + if (old_reload || !t->reload) { + break; + } + case TIMER_REG_STATUS: if (timer_enabled(t)) { uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); @@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t rate = calculate_rate(t); t->start += muldiv64(delta, NANOSECONDS_PER_SECOND, rate); - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } break; - case TIMER_REG_RELOAD: - t->reload = value; - break; case TIMER_REG_MATCH_FIRST: case TIMER_REG_MATCH_SECOND: t->match[reg - 2] = value; if (timer_enabled(t)) { - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } break; default: @@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) trace_aspeed_timer_ctrl_enable(t->id, enable); if (enable) { t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } else { timer_del(&t->timer); }