From nobody Thu Sep 19 00:51:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1721744227; cv=none; d=zohomail.com; s=zohoarc; b=JwQU6XCw/3VMqUORdWTTuw/Nt/rUF25+sj2mx1pXmI65TWZGNRBn2lCyRZ1rVkR5hXD1vYdlT4qUq/m2tcNDcT3DpxIanWfCug4B1I6cXi9YS44/zmuiiM33dzX0budZ6lYFlQyEWddrpV9d9A/B/k/4zS8GuxzbQVywjgDLR9o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721744227; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=HJdZwCWA36rJai5fAzKET5M9D8dSbh2TfkiP88YY4YQ=; b=fwdN3D39H3k0ov5N0o9LmCDu4xKpiFUL0EQKZLQwx64DsWLwFVL2QiGXc3zVWC/8LcGlIF7HCNxQ79HjZARKFjYNePo8pALa+ZFcAzQL9rp4347sz42IWDNCdZH36/9gLmKUkgziFUNCZG3xTsqvJhUa9Z832t1UIHJ90EaBVa4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172174422712942.955025754578514; Tue, 23 Jul 2024 07:17:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWGJY-00071p-O5; Tue, 23 Jul 2024 10:16:28 -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 1sWGJE-0005RH-9k for qemu-devel@nongnu.org; Tue, 23 Jul 2024 10:16:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sWGJB-0007Eu-Nj for qemu-devel@nongnu.org; Tue, 23 Jul 2024 10:16:07 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-83-K3ZsObdWPpe7vYkbi9VpFg-1; Tue, 23 Jul 2024 10:16:00 -0400 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-367962f0cb0so3305586f8f.1 for ; Tue, 23 Jul 2024 07:16:00 -0700 (PDT) Received: from avogadro.local ([151.95.101.29]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36878811899sm11623162f8f.116.2024.07.23.07.15.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 07:15:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721744164; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HJdZwCWA36rJai5fAzKET5M9D8dSbh2TfkiP88YY4YQ=; b=duAs8omkOM2UuuIwXruilBqg1EwNQeLxD3dMaLf0QV1oCvAzyzgK+OzTq4b3/qC4wvm4LI 4nRkNNXpWiYCkrbIE0DhWdi22x9leKtOndfJE/hFdVWEpxPR2bjURGjT0ESD8cxM7EQ0hy yMRsDLhHv8VfWCnaG4BeckLLVle+eCo= X-MC-Unique: K3ZsObdWPpe7vYkbi9VpFg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721744159; x=1722348959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HJdZwCWA36rJai5fAzKET5M9D8dSbh2TfkiP88YY4YQ=; b=t+0MmZOgXV7myJokWMIGdWISVgOj5rS73itMClyvvOxzKR9cxapLC3RxhMUyEkX/my iQs3P3Wibu8R/3ZiLzM9b+snuBxUMXIkt9bCiHjdO5zvUR1sZ5x/Yh35n3Ovb7930kER WcH6FHbBlBa0rR3vCzWiRDgyn2re1NzsSXOxX6WH4E5LWLMqZ8lUrloejtI5wL+F4NR1 jh3gxQCrVdVy7ZZ399CYi7paQqXcGIdkMzkQtdNMv4EaJsPFkdV/J5OyLIvwPXxetBNC FAcUhP2tPYtW5usZdLo4gJos8lz8hDQoeN3sP1NwsqtDAop40rs4wRjvi/QPQpsovgdV vw4w== X-Gm-Message-State: AOJu0Yw8TL7IeynBT3TDo75+OmJgsGGVYg7ipQmRNjwXYc+VKtGHoPtV QIkrOV9qobdhD5cb/oMM+nOKVt8si0arhF6gNPEKbhQ/6916wK4r+KtfwI4XsnNUUUgJu421FeB pr+S4jWyhdiobp0TDIQ8EvpsfWCJEkjX4pihhyJpSfRUt207jEOOkEzX/cIEbjiXDTLexeHqh5T gVJxA+r26HacdEg22dWJ/xHw8ZcEhJR+XCPFLt X-Received: by 2002:a05:6000:18aa:b0:366:ef25:de51 with SMTP id ffacd0b85a97d-369bae707ecmr7393118f8f.49.1721744159083; Tue, 23 Jul 2024 07:15:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHj3HpwtcBQMJzvWUBI/P8uQSDZTfo5bXNJzluPQyep8jggzsM3lWrB8RL5LIeC7LMpMZ7nXA== X-Received: by 2002:a05:6000:18aa:b0:366:ef25:de51 with SMTP id ffacd0b85a97d-369bae707ecmr7393090f8f.49.1721744158441; Tue, 23 Jul 2024 07:15:58 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 10/11] hpet: store full 64-bit target value of the counter Date: Tue, 23 Jul 2024 16:15:28 +0200 Message-ID: <20240723141529.551737-11-pbonzini@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723141529.551737-1-pbonzini@redhat.com> References: <20240723141529.551737-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.133, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1721744228384116600 Content-Type: text/plain; charset="utf-8" Store the full 64-bit value at which the timer should fire. This makes it possible to skip the imprecise hpet_calculate_diff() step, and to remove the clamping of the period to 31 or 63 bits. Signed-off-by: Paolo Bonzini --- hw/timer/hpet.c | 111 +++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 62 deletions(-) diff --git a/hw/timer/hpet.c b/hw/timer/hpet.c index ac55dd1ebd6..1654b7cb8b8 100644 --- a/hw/timer/hpet.c +++ b/hw/timer/hpet.c @@ -54,6 +54,7 @@ typedef struct HPETTimer { /* timers */ uint64_t cmp; /* comparator */ uint64_t fsb; /* FSB route */ /* Hidden register state */ + uint64_t cmp64; /* comparator (extended to counter width) */ uint64_t period; /* Last value written to comparator */ uint8_t wrap_flag; /* timer pop will indicate wrap for one-shot 3= 2-bit * mode. Next pop will be actual timer expirat= ion. @@ -115,11 +116,6 @@ static uint32_t timer_enabled(HPETTimer *t) } =20 static uint32_t hpet_time_after(uint64_t a, uint64_t b) -{ - return ((int32_t)(b - a) < 0); -} - -static uint32_t hpet_time_after64(uint64_t a, uint64_t b) { return ((int64_t)(b - a) < 0); } @@ -156,29 +152,34 @@ static uint64_t hpet_get_ticks(HPETState *s) return ns_to_ticks(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hpet_off= set); } =20 -/* - * calculate diff between comparator value and current ticks - */ -static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current) +static uint64_t hpet_get_ns(HPETState *s, uint64_t tick) { + return ticks_to_ns(tick) - s->hpet_offset; +} =20 +/* + * calculate next value of the general counter that matches the + * target (either entirely, or the low 32-bit only depending on + * the timer mode). + */ +static uint64_t hpet_calculate_cmp64(HPETTimer *t, uint64_t cur_tick, uint= 64_t target) +{ if (t->config & HPET_TN_32BIT) { - uint32_t diff, cmp; - - cmp =3D (uint32_t)t->cmp; - diff =3D cmp - (uint32_t)current; - diff =3D (int32_t)diff > 0 ? diff : (uint32_t)1; - return (uint64_t)diff; + uint64_t result =3D deposit64(cur_tick, 0, 32, target); + if (result < cur_tick) { + result +=3D 0x100000000ULL; + } + return result; } else { - uint64_t diff, cmp; - - cmp =3D t->cmp; - diff =3D cmp - current; - diff =3D (int64_t)diff > 0 ? diff : (uint64_t)1; - return diff; + return target; } } =20 +static uint64_t hpet_next_wrap(uint64_t cur_tick) +{ + return (cur_tick | 0xffffffffU) + 1; +} + static void update_irq(struct HPETTimer *timer, int set) { uint64_t mask; @@ -260,7 +261,12 @@ static bool hpet_validate_num_timers(void *opaque, int= version_id) static int hpet_post_load(void *opaque, int version_id) { HPETState *s =3D opaque; + int i; =20 + for (i =3D 0; i < s->num_timers; i++) { + HPETTimer *t =3D &s->timer[i]; + t->cmp64 =3D hpet_calculate_cmp64(t, s->hpet_counter, t->cmp); + } /* Recalculate the offset between the main counter and guest time */ if (!s->hpet_offset_saved) { s->hpet_offset =3D ticks_to_ns(s->hpet_counter) @@ -356,14 +362,10 @@ static const VMStateDescription vmstate_hpet =3D { } }; =20 -static void hpet_arm(HPETTimer *t, uint64_t ticks) +static void hpet_arm(HPETTimer *t, uint64_t tick) { - if (ticks < ns_to_ticks(INT64_MAX / 2)) { - timer_mod(t->qemu_timer, - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ticks_to_ns(tick= s)); - } else { - timer_del(t->qemu_timer); - } + /* FIXME: Clamp period to reasonable min value? */ + timer_mod(t->qemu_timer, hpet_get_ns(t->state, tick)); } =20 /* @@ -372,54 +374,44 @@ static void hpet_arm(HPETTimer *t, uint64_t ticks) static void hpet_timer(void *opaque) { HPETTimer *t =3D opaque; - uint64_t diff; - uint64_t period =3D t->period; uint64_t cur_tick =3D hpet_get_ticks(t->state); =20 if (timer_is_periodic(t) && period !=3D 0) { + while (hpet_time_after(cur_tick, t->cmp64)) { + t->cmp64 +=3D period; + } if (t->config & HPET_TN_32BIT) { - while (hpet_time_after(cur_tick, t->cmp)) { - t->cmp =3D (uint32_t)(t->cmp + t->period); - } + t->cmp =3D (uint32_t)t->cmp64; } else { - while (hpet_time_after64(cur_tick, t->cmp)) { - t->cmp +=3D period; - } - } - diff =3D hpet_calculate_diff(t, cur_tick); - hpet_arm(t, diff); - } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { - if (t->wrap_flag) { - diff =3D hpet_calculate_diff(t, cur_tick); - hpet_arm(t, diff); - t->wrap_flag =3D 0; + t->cmp =3D t->cmp64; } + hpet_arm(t, t->cmp64); + } else if (t->wrap_flag) { + t->wrap_flag =3D 0; + hpet_arm(t, t->cmp64); } update_irq(t, 1); } =20 static void hpet_set_timer(HPETTimer *t) { - uint64_t diff; - uint32_t wrap_diff; /* how many ticks until we wrap? */ uint64_t cur_tick =3D hpet_get_ticks(t->state); =20 - /* whenever new timer is being set up, make sure wrap_flag is 0 */ t->wrap_flag =3D 0; - diff =3D hpet_calculate_diff(t, cur_tick); + t->cmp64 =3D hpet_calculate_cmp64(t, cur_tick, t->cmp); + if (t->config & HPET_TN_32BIT) { =20 - /* hpet spec says in one-shot 32-bit mode, generate an interrupt when - * counter wraps in addition to an interrupt with comparator match. - */ - if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { - wrap_diff =3D 0xffffffff - (uint32_t)cur_tick; - if (wrap_diff < (uint32_t)diff) { - diff =3D wrap_diff; + /* hpet spec says in one-shot 32-bit mode, generate an interrupt w= hen + * counter wraps in addition to an interrupt with comparator match. + */ + if (!timer_is_periodic(t) && t->cmp64 > hpet_next_wrap(cur_tick)) { t->wrap_flag =3D 1; + hpet_arm(t, hpet_next_wrap(cur_tick)); + return; } } - hpet_arm(t, diff); + hpet_arm(t, t->cmp64); } =20 static void hpet_del_timer(HPETTimer *t) @@ -550,12 +542,7 @@ static void hpet_ram_write(void *opaque, hwaddr addr, timer->cmp =3D deposit64(timer->cmp, shift, len, value); } if (timer_is_periodic(timer)) { - /* - * FIXME: Clamp period to reasonable min value? - * Clamp period to reasonable max value - */ - new_val =3D deposit64(timer->period, shift, len, value); - timer->period =3D MIN(new_val, (timer->config & HPET_TN_32= BIT ? ~0u : ~0ull) >> 1); + timer->period =3D deposit64(timer->period, shift, len, val= ue); } timer->config &=3D ~HPET_TN_SETVAL; if (hpet_enabled(s)) { --=20 2.45.2