From nobody Sun May 5 15:49:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491990869977764.1470123855781; Wed, 12 Apr 2017 02:54:29 -0700 (PDT) Received: from localhost ([::1]:43241 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEyq-0007FM-Q8 for importer@patchew.org; Wed, 12 Apr 2017 05:54:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51301) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEwO-0005Tl-MN for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyEwN-0004kx-JY for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:56 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:33938) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyEwN-0004kp-EM for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:55 -0400 Received: by mail-pf0-x242.google.com with SMTP id o126so4065486pfb.1 for ; Wed, 12 Apr 2017 02:51:55 -0700 (PDT) Received: from eric.tencent.com ([103.7.29.8]) by smtp.gmail.com with ESMTPSA id 4sm35445749pff.17.2017.04.12.02.51.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 02:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=IHG0eq7pYInf5jBkUXcHbbwYX0qxmNcW19UBVxRj8fk=; b=GzWzfgurlxCHX6H5A0EMxAbSTv4gcNyFknepyfaGmYvehFmlTxpWydqCenucv84NsS 4PNqfBeHpntACadMQ1mP5FBGjIDUwNtp/BxcU5LPjZxlG1hjeDtdFh7cVNJW7dDFLp9j ou0rNaV7cL4JXS01NF8qQrtXjbqlR/BL9Dq+9Homue/htIyYTn3BPxv8DEZ/E4s4MPHL GGtr6LBg37Jldby9PSLbTaIg3FjbJQPmemVNK3Xpkz6WVqKqlGlBGZfT9suzPtRaPcrE z4OhJsK6WRHhKH9Ev6O4qhHIigLZ+i6jAseTPkE/8L0v7XxREcFqPS0MGU5BP91oU8x8 8/cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=IHG0eq7pYInf5jBkUXcHbbwYX0qxmNcW19UBVxRj8fk=; b=hwM0vlerLmOhaFMoCufxjR0GZJsp1Dr6hiFdVPuNfZjt8tLTvqq/VOugkSZa7ar4Fv j8YiiANVaBEOem4o6+54iuZvGeHGlfUCoszCCTbwQiw+STXONNbyU2eKhYctk1Yy2jlz 7bHMhqiPPTFB+d8A6atSkaiAOf/zV+ZpYkLwIGC8I90lRK7X5DjgrNXlWAl+4bb1KUuX dyq4xh48BFch/sJBKLQvkZ3MA2TDXCRGv8byvYUepLhneEsk07c/GIr23cGld1vbJJVo 6vyVn8i4pY5hoFUt8CuxCcfdU0SM9sdFxchS2J/4UFXVS7GzjHiDpfQJji+1kntl9BiQ 75Lg== X-Gm-Message-State: AN3rC/7qvOGXp5ugLjGUq5choX6ggFomSPiM8SCjowZoBfV5MVMbu9z7ore5mDvtCdiCMg== X-Received: by 10.84.232.205 with SMTP id x13mr24058071plm.153.1491990714482; Wed, 12 Apr 2017 02:51:54 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Wed, 12 Apr 2017 17:51:07 +0800 Message-Id: <20170412095111.11728-2-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170412095111.11728-1-xiaoguangrong@tencent.com> References: <20170412095111.11728-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH 1/5] mc146818rtc: update periodic timer only if it is needed 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: Xiao Guangrong , yunfangtai@tencent.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong Currently, the timer is updated whenever RegA or RegB is written even if the periodic timer related configuration is not changed This patch optimizes it slightly to make the update happen only if its period or enable-status is changed, also later patches are depend on this optimization Signed-off-by: Xiao Guangrong --- hw/timer/mc146818rtc.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c index 4165450..749e206 100644 --- a/hw/timer/mc146818rtc.c +++ b/hw/timer/mc146818rtc.c @@ -387,10 +387,25 @@ static void rtc_update_timer(void *opaque) check_update_timer(s); } =20 +static bool rtc_periodic_timer_updated_by_regA(RTCState *s, uint64_t data) +{ + uint8_t orig =3D s->cmos_data[RTC_REG_A]; + + return (orig & 0x0f) !=3D (data & 0x0f); +} + +static bool rtc_periodic_timer_updated_by_regB(RTCState *s, uint64_t data) +{ + uint8_t orig =3D s->cmos_data[RTC_REG_B]; + + return (orig & REG_B_PIE) !=3D (data & REG_B_PIE); +} + static void cmos_ioport_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) { RTCState *s =3D opaque; + bool update_periodic_timer; =20 if ((addr & 1) =3D=3D 0) { s->cmos_index =3D data & 0x7f; @@ -423,6 +438,8 @@ static void cmos_ioport_write(void *opaque, hwaddr addr, } break; case RTC_REG_A: + update_periodic_timer =3D rtc_periodic_timer_updated_by_regA(s= , data); + if ((data & 0x60) =3D=3D 0x60) { if (rtc_running(s)) { rtc_update_time(s); @@ -445,10 +462,16 @@ static void cmos_ioport_write(void *opaque, hwaddr ad= dr, /* UIP bit is read only */ s->cmos_data[RTC_REG_A] =3D (data & ~REG_A_UIP) | (s->cmos_data[RTC_REG_A] & REG_A_UIP); - periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + + if (update_periodic_timer) { + periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + } + check_update_timer(s); break; case RTC_REG_B: + update_periodic_timer =3D rtc_periodic_timer_updated_by_regB(s= , data); + if (data & REG_B_SET) { /* update cmos to when the rtc was stopping */ if (rtc_running(s)) { @@ -475,7 +498,11 @@ static void cmos_ioport_write(void *opaque, hwaddr add= r, qemu_irq_lower(s->irq); } s->cmos_data[RTC_REG_B] =3D data; - periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + + if (update_periodic_timer) { + periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + } + check_update_timer(s); break; case RTC_REG_C: --=20 2.9.3 From nobody Sun May 5 15:49:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491990998136761.2727959965099; Wed, 12 Apr 2017 02:56:38 -0700 (PDT) Received: from localhost ([::1]:43255 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyF0u-0000nx-Vv for importer@patchew.org; Wed, 12 Apr 2017 05:56:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51327) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEwQ-0005VI-KO for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyEwP-0004ml-PM for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:58 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:36274) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyEwP-0004mI-Ir for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:51:57 -0400 Received: by mail-pg0-x242.google.com with SMTP id 34so1371576pgx.3 for ; Wed, 12 Apr 2017 02:51:57 -0700 (PDT) Received: from eric.tencent.com ([103.7.29.8]) by smtp.gmail.com with ESMTPSA id 4sm35445749pff.17.2017.04.12.02.51.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 02:51:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zjIOylt3BTVARZNDEomO5jE6fTM+VBcKcS1uMPoF3c0=; b=gViUsf2w7YgyylISlnqSvdcNTI6iGR1nG6gSISmvLZwxA21yquchYmfmsOWPcqiTjR 4HD9AC+ufYzhdlQ3G4hZ/jzpQeNI22T3ihCw2nkJxLwL1oV9bBjM37dUkWGAF4+LXtCT yRlR9VUC6xj7OU4JCi1XQBRekOe7+WRIQbMgrqjfYAoBJ3ZCPFAzJBWUJZyprkvn8ro9 VmNAfK3n3xk5tshWfiEsPLrf9jhKO0lFcK0mdvKn28pfGInCjtEcE8TQMd2vTtcbmDLU rud/wr4hI5wOLDP9GTbWH0RxLVsmRA3SiTbtCAMhlQz7KPyKpUlZdRWdUC7Z0p9Vb+AN uIVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zjIOylt3BTVARZNDEomO5jE6fTM+VBcKcS1uMPoF3c0=; b=NcI7kn9jvvxCuUVDReQn6CCIdSq99t3Q+jVpMX6ixNsT739rMUdULosy8DSU5GLPEr FnoX9BCm9AIpiRomOH1r7sMwvafzdPMWwxc0RFd3KR7aCiGc8TNMQorwMLOy3qeR67ZS 6M13GM4Bg/YQC3piAW9bu3/DRRNX5BqY8uJdiwz1Y7oCt7Jg31k3RyiEPPP34P4yFOjT WwQrIVqiue+tUJe4JpDJHLkGL4ZHg4VCX3O29+FWrZ9h2vX+xZOcfZwGF+de7TDDv6XT TbglqNTvYK6rcGgm0ohvx4hInyqLl+jSRFZHKj1E+uSlgy1GCjBIt7QO3V70bv5eFdMl H7pQ== X-Gm-Message-State: AN3rC/5B/Y5LkEUPnl8/IRZtoGZswRTyNgxFjco7r5fIIvk5JQgJs7djLcZTSUQNXZxt8w== X-Received: by 10.84.222.140 with SMTP id x12mr34786216pls.36.1491990716635; Wed, 12 Apr 2017 02:51:56 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Wed, 12 Apr 2017 17:51:08 +0800 Message-Id: <20170412095111.11728-3-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170412095111.11728-1-xiaoguangrong@tencent.com> References: <20170412095111.11728-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH 2/5] mc146818rtc: fix clock lost after scaling coalesced irq 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: Xiao Guangrong , yunfangtai@tencent.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong If the period is changed by re-configuring RegA, the coalesced irq will be scaled to reflect the new period, however, it calculates the new interrupt number like this: s->irq_coalesced =3D (s->irq_coalesced * s->period) / period; There are some clocks will be lost if they are not enough to be squeezed to a single new period that will cause the VM clock slower In order to fix the issue, we calculate the interrupt window based on the precise clock rather than period, then the clocks lost during period is scaled can be compensated properly Signed-off-by: Xiao Guangrong --- hw/timer/mc146818rtc.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c index 749e206..649678c 100644 --- a/hw/timer/mc146818rtc.c +++ b/hw/timer/mc146818rtc.c @@ -146,23 +146,46 @@ static void rtc_coalesced_timer(void *opaque) } #endif =20 +static int period_code_to_clock(int period_code) +{ + /* periodic timer is disabled. */ + if (!period_code) { + return 0; + } + + if (period_code <=3D 2) { + period_code +=3D 7; + } + + /* period in 32 Khz cycles */ + return 1 << (period_code - 1); +} + /* handle periodic timer */ static void periodic_timer_update(RTCState *s, int64_t current_time) { int period_code, period; - int64_t cur_clock, next_irq_clock; + int64_t cur_clock, next_irq_clock, lost_clock =3D 0; =20 period_code =3D s->cmos_data[RTC_REG_A] & 0x0f; if (period_code !=3D 0 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) { - if (period_code <=3D 2) - period_code +=3D 7; - /* period in 32 Khz cycles */ - period =3D 1 << (period_code - 1); + period =3D period_code_to_clock(period_code); #ifdef TARGET_I386 if (period !=3D s->period) { - s->irq_coalesced =3D (s->irq_coalesced * s->period) / period; - DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coales= ced); + int current_irq_coalesced =3D s->irq_coalesced; + + s->irq_coalesced =3D (current_irq_coalesced * s->period) / per= iod; + + /* + * calculate the lost clock after it is scaled which should be + * compensated in the next interrupt. + */ + lost_clock +=3D current_irq_coalesced * s->period - + s->irq_coalesced * period; + DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, %ld cloc= ks " + "are compensated.\n", + current_irq_coalesced, s->irq_coalesced, lost_clock); } s->period =3D period; #endif @@ -170,7 +193,7 @@ static void periodic_timer_update(RTCState *s, int64_t = current_time) cur_clock =3D muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND); =20 - next_irq_clock =3D (cur_clock & ~(period - 1)) + period; + next_irq_clock =3D cur_clock + period - lost_clock; s->next_periodic_time =3D muldiv64(next_irq_clock, NANOSECONDS_PER= _SECOND, RTC_CLOCK_RATE) + 1; timer_mod(s->periodic_timer, s->next_periodic_time); --=20 2.9.3 From nobody Sun May 5 15:49:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491990815116304.0073145730249; Wed, 12 Apr 2017 02:53:35 -0700 (PDT) Received: from localhost ([::1]:43238 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyExx-0006UR-UE for importer@patchew.org; Wed, 12 Apr 2017 05:53:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51355) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEwW-0005Zl-Gf for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyEwS-0004nB-4f for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:04 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:36282) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyEwR-0004n1-TB for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:00 -0400 Received: by mail-pg0-x241.google.com with SMTP id 34so1371706pgx.3 for ; Wed, 12 Apr 2017 02:51:59 -0700 (PDT) Received: from eric.tencent.com ([103.7.29.8]) by smtp.gmail.com with ESMTPSA id 4sm35445749pff.17.2017.04.12.02.51.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 02:51:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bxQe8PxCk+LUCTGLkOqW18q73Ul9EzM1BZdLG3YGtAo=; b=gTcz5n6dkwtqndxywqAkjft6m9npjy+h79LSFDUV2xm9AVnVUnCFUe4PzgVZ5XyJqw qe4VufKWG/XSOFt6veqGum2pwwJ38XV4Ywbd2M/MTuPI1bsQNEyBiOjTC56G1C95u4xk DXUEeOIWR9iaTFhpUmOUu1/3R/3ISknC7fwuTDK9PlmAg23UtDkaTC0uUkXhvtfzNNIQ Q36DTMKlY60hmRcPpY59iacyr7Bnu5gJ1mPivlcz/oQ10/LU6RrmLfxWnXq87cg8NRlR YuQRujoyv8vj3VbXUovz3aiMdS0RxNGO5RYd9zp047BVsFzB+gQwvGg6THExfDiE77tG 4sFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bxQe8PxCk+LUCTGLkOqW18q73Ul9EzM1BZdLG3YGtAo=; b=SeSgeMnAsPmTCSv1jCsWPbQW7NmCYsn+FYmpSBJZuO+0daTmHl9gmn88RK2AXN14sQ Nt6A3gUOK6SHDZvE9z2ozhVqaSJV3sxVDIS2tkYiw3Q8mmyXH6N3IJpOLiFtfkraqgD3 CfXQdDV04756IxkvVgbwbf2HdgmL6UrMDRwBzMjeAULe1YB23S7sBOWoe2wdA8qnr+NY uq/FqjDMjsJYdKPcZj/V+yGmegLVXAEO0JdfoiIMitvgcn3PRdIV9kYy39JTmhqQHhWK S2C6azg0+5NEwC2ec7sKlwXydx2IBs7G75P8Rotmjpw++fJ9jt2Lo3H4AmK97lNePrfE /UbA== X-Gm-Message-State: AN3rC/55ZjooSVk91EyJKf/GRXsloYCO31TurBx0u5i4m7PPObuFFyMaIBke31H7++I9KA== X-Received: by 10.84.222.138 with SMTP id x10mr34228730pls.3.1491990718890; Wed, 12 Apr 2017 02:51:58 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Wed, 12 Apr 2017 17:51:09 +0800 Message-Id: <20170412095111.11728-4-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170412095111.11728-1-xiaoguangrong@tencent.com> References: <20170412095111.11728-1-xiaoguangrong@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH 3/5] mc146818rtc: properly count the time for the next interrupt 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: Xiao Guangrong , yunfangtai@tencent.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: Tai Yunfang If periodic_timer_update() is called due to RegA reconfiguration, i.e, the period is updated, current time is not the start point for the next periodic timer, instead, which should start from the last interrupt, otherwise, the clock in VM will become slow This patch takes the clocks from last interrupt to current clock into account and compensates the clocks for the next interrupt, especially=EF=BC= =8C if a complete interrupt was lost in this window, the time can be caught up by LOST_TICK_POLICY_SLEW [ Xiao: redesign the algorithm based on Yunfang's original work. ] Signed-off-by: Tai Yunfang Signed-off-by: Xiao Guangrong --- hw/timer/mc146818rtc.c | 71 +++++++++++++++++++++++++++++++++++++++++++++-= ---- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c index 649678c..3bf559d 100644 --- a/hw/timer/mc146818rtc.c +++ b/hw/timer/mc146818rtc.c @@ -161,8 +161,12 @@ static int period_code_to_clock(int period_code) return 1 << (period_code - 1); } =20 -/* handle periodic timer */ -static void periodic_timer_update(RTCState *s, int64_t current_time) +/* + * handle periodic timer. @old_period indicates the periodic timer update + * is just due to period adjustment. + */ +static void +periodic_timer_update(RTCState *s, int64_t current_time, int old_period) { int period_code, period; int64_t cur_clock, next_irq_clock, lost_clock =3D 0; @@ -193,6 +197,56 @@ static void periodic_timer_update(RTCState *s, int64_t= current_time) cur_clock =3D muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND); =20 + /* + * if the periodic timer's update is due to period re-configuration, + * we should count the clock since last interrupt. + */ + if (old_period) { + int64_t last_periodic_clock; + + last_periodic_clock =3D muldiv64(s->next_periodic_time, + RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND= ); + /* + * if the next interrupt has not happened yet, we recall the l= ast + * interrupt based on the original period. + */ + if (last_periodic_clock > cur_clock) { + last_periodic_clock -=3D period_code_to_clock(old_period); + + /* the last interrupt must have happened. */ + assert(cur_clock >=3D last_periodic_clock); + } + + /* calculate the clock since last interrupt. */ + lost_clock +=3D cur_clock - last_periodic_clock; + +#ifdef TARGET_I386 + /* + * if more than period clocks were passed, i.e, the timer inte= rrupt + * has been lost, we should catch up the time. + */ + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW && + (lost_clock / period)) { + int lost_interrupt =3D lost_clock / period; + + s->irq_coalesced +=3D lost_interrupt; + lost_clock -=3D lost_interrupt * period; + if (lost_interrupt) { + DPRINTF_C("cmos: compensate %d interrupts, coalesced i= rqs " + "increased to %d\n", lost_interrupt, + s->irq_coalesced); + rtc_coalesced_timer_update(s); + } + } else +#endif + /* + * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW + * is not used, we should make the time progress anyway. + */ + lost_clock =3D MIN(lost_clock, period); + assert(lost_clock >=3D 0); + } + next_irq_clock =3D cur_clock + period - lost_clock; s->next_periodic_time =3D muldiv64(next_irq_clock, NANOSECONDS_PER= _SECOND, RTC_CLOCK_RATE) + 1; @@ -209,7 +263,7 @@ static void rtc_periodic_timer(void *opaque) { RTCState *s =3D opaque; =20 - periodic_timer_update(s, s->next_periodic_time); + periodic_timer_update(s, s->next_periodic_time, 0); s->cmos_data[RTC_REG_C] |=3D REG_C_PF; if (s->cmos_data[RTC_REG_B] & REG_B_PIE) { s->cmos_data[RTC_REG_C] |=3D REG_C_IRQF; @@ -428,6 +482,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr, uint64_t data, unsigned size) { RTCState *s =3D opaque; + int cur_period; bool update_periodic_timer; =20 if ((addr & 1) =3D=3D 0) { @@ -461,6 +516,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr, } break; case RTC_REG_A: + cur_period =3D s->cmos_data[RTC_REG_A] & 0xf; update_periodic_timer =3D rtc_periodic_timer_updated_by_regA(s= , data); =20 if ((data & 0x60) =3D=3D 0x60) { @@ -487,7 +543,8 @@ static void cmos_ioport_write(void *opaque, hwaddr addr, (s->cmos_data[RTC_REG_A] & REG_A_UIP); =20 if (update_periodic_timer) { - periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), + cur_period); } =20 check_update_timer(s); @@ -523,7 +580,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr, s->cmos_data[RTC_REG_B] =3D data; =20 if (update_periodic_timer) { - periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), 0); } =20 check_update_timer(s); @@ -793,7 +850,7 @@ static int rtc_post_load(void *opaque, int version_id) uint64_t now =3D qemu_clock_get_ns(rtc_clock); if (now < s->next_periodic_time || now > (s->next_periodic_time + get_max_clock_jump())) { - periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); + periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), 0); } } =20 @@ -858,7 +915,7 @@ static void rtc_notify_clock_reset(Notifier *notifier, = void *data) int64_t now =3D *(int64_t *)data; =20 rtc_set_date_from_host(ISA_DEVICE(s)); - periodic_timer_update(s, now); + periodic_timer_update(s, now, 0); check_update_timer(s); #ifdef TARGET_I386 if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { --=20 2.9.3 From nobody Sun May 5 15:49:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491990927313421.6217323603762; Wed, 12 Apr 2017 02:55:27 -0700 (PDT) Received: from localhost ([::1]:43244 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEzl-00080J-Jc for importer@patchew.org; Wed, 12 Apr 2017 05:55:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51369) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEwY-0005aQ-DX for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyEwU-0004nT-6g for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:06 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:34922) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyEwT-0004nN-VP for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:02 -0400 Received: by mail-pg0-x241.google.com with SMTP id g2so4225886pge.2 for ; Wed, 12 Apr 2017 02:52:01 -0700 (PDT) Received: from eric.tencent.com ([103.7.29.8]) by smtp.gmail.com with ESMTPSA id 4sm35445749pff.17.2017.04.12.02.51.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 02:52:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=95oGEhcSCSxA8dayWct3bpTBoVjwvh7b68XnyuUVb4Q=; b=vYq2noedglXEM3uYQsvJG8WCm1+yYBKoHae1m7p9wc+F04anFBgXJm2lg2YFGF5ZLu sQ+uc6PI+wgftl6b+mt2CGz+yQhFnDGDUjYzTmlCiXlWFTlWZ36+l5ZrHrpQd1n0qPiI f0+8eTZA8/yhKuoI3avQF6Vtiv6j9TL/+fsn1ROCYbbZ240RGkGGd4fbSYdD8avCr6Sf B+Qc+IiuDf1ppaAViQkAIa32/cRyCu4vdf8VPKHKwzQo5CSdpMFVKBbxkN8khfxUP82E YIKEdvMvY2/loRX4ohKtbzBTD2z82fXdnqp7IU5Td7S466ToZSYlFckwgfbCMtUAcJQM vYyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=95oGEhcSCSxA8dayWct3bpTBoVjwvh7b68XnyuUVb4Q=; b=Y4FzzPmF2XFiKosAGgTiW+SjSLWoKI+wNPvTcHawmj3S7qZ1hf8rmIuswHzSczeqEU okx3RU+IQeDhHsmioYuztqmFvSwqf/3s757fLS+NTYNT65gBFeVSxQ/jRSjIbJl5BVBu GpZw3wvcPiAIejs+/5BMIfU/9cIirDPRbLv/DPC5Po0XNsLhvYFyPNE2hOWNfUnS/81E dVGHt1cRMXJ6SVKs993kFsk33sLye4UoKWL+jhqVgGELPPOgU3peat/Djd+8T7iWTZWy F7k1PoQyQuU9AhFc4GNgHjlYYbpAwHPLhvNBxXPT3gwMav09aGoKP6ySuUoNnSWc6K9o zV2w== X-Gm-Message-State: AN3rC/6V/0A7IO7jP1sdYs6QRk//eI+SzUBcZ0gqm/4L4UN9MYLsgCoBSGs1ntPXcbI7Ew== X-Received: by 10.84.238.198 with SMTP id l6mr28302403pln.95.1491990721090; Wed, 12 Apr 2017 02:52:01 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Wed, 12 Apr 2017 17:51:10 +0800 Message-Id: <20170412095111.11728-5-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170412095111.11728-1-xiaoguangrong@tencent.com> References: <20170412095111.11728-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH 4/5] mc146818rtc: move x86 specific code out of periodic_timer_update 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: Xiao Guangrong , yunfangtai@tencent.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong Move the x86 specific code in periodic_timer_update() to a common place, the actual logic is not changed Signed-off-by: Xiao Guangrong --- hw/timer/mc146818rtc.c | 112 +++++++++++++++++++++++++++++----------------= ---- 1 file changed, 66 insertions(+), 46 deletions(-) diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c index 3bf559d..d7b7c56 100644 --- a/hw/timer/mc146818rtc.c +++ b/hw/timer/mc146818rtc.c @@ -144,6 +144,63 @@ static void rtc_coalesced_timer(void *opaque) =20 rtc_coalesced_timer_update(s); } + +static int64_t +arch_periodic_timer_update(RTCState *s, int period, int64_t lost_clock) +{ + if (period !=3D s->period) { + int64_t scale_lost_clock; + int current_irq_coalesced =3D s->irq_coalesced; + + s->irq_coalesced =3D (current_irq_coalesced * s->period) / period; + + /* + * calculate the lost clock after it is scaled which should be + * compensated in the next interrupt. + */ + scale_lost_clock =3D current_irq_coalesced * s->period - + s->irq_coalesced * period; + DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, %ld clocks " + "are compensated.\n", current_irq_coalesced, + s->irq_coalesced, scale_lost_clock); + lost_clock +=3D scale_lost_clock; + s->period =3D period; + } + + /* + * if more than period clocks were passed, i.e, the timer interrupt + * has been lost, we should catch up the time. + */ + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW && + (lost_clock / period)) { + int lost_interrupt =3D lost_clock / period; + + s->irq_coalesced +=3D lost_interrupt; + lost_clock -=3D lost_interrupt * period; + if (lost_interrupt) { + DPRINTF_C("cmos: compensate %d interrupts, coalesced irqs " + "increased to %d\n", lost_interrupt, s->irq_coalesce= d); + rtc_coalesced_timer_update(s); + } + } + + return lost_clock; +} + +static void arch_periodic_timer_disable(RTCState *s) +{ + s->irq_coalesced =3D 0; +} +#else +static int64_t +arch_periodic_timer_update(RTCState *s, int period, int64_t lost_clock) +{ + return lost_clock; +} + +static void arch_periodic_timer_disable(RTCState *s) +{ +} #endif =20 static int period_code_to_clock(int period_code) @@ -175,24 +232,7 @@ periodic_timer_update(RTCState *s, int64_t current_tim= e, int old_period) if (period_code !=3D 0 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) { period =3D period_code_to_clock(period_code); -#ifdef TARGET_I386 - if (period !=3D s->period) { - int current_irq_coalesced =3D s->irq_coalesced; - - s->irq_coalesced =3D (current_irq_coalesced * s->period) / per= iod; =20 - /* - * calculate the lost clock after it is scaled which should be - * compensated in the next interrupt. - */ - lost_clock +=3D current_irq_coalesced * s->period - - s->irq_coalesced * period; - DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, %ld cloc= ks " - "are compensated.\n", - current_irq_coalesced, s->irq_coalesced, lost_clock); - } - s->period =3D period; -#endif /* compute 32 khz clock */ cur_clock =3D muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND); @@ -219,42 +259,22 @@ periodic_timer_update(RTCState *s, int64_t current_ti= me, int old_period) =20 /* calculate the clock since last interrupt. */ lost_clock +=3D cur_clock - last_periodic_clock; - -#ifdef TARGET_I386 - /* - * if more than period clocks were passed, i.e, the timer inte= rrupt - * has been lost, we should catch up the time. - */ - if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW && - (lost_clock / period)) { - int lost_interrupt =3D lost_clock / period; - - s->irq_coalesced +=3D lost_interrupt; - lost_clock -=3D lost_interrupt * period; - if (lost_interrupt) { - DPRINTF_C("cmos: compensate %d interrupts, coalesced i= rqs " - "increased to %d\n", lost_interrupt, - s->irq_coalesced); - rtc_coalesced_timer_update(s); - } - } else -#endif - /* - * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW - * is not used, we should make the time progress anyway. - */ - lost_clock =3D MIN(lost_clock, period); - assert(lost_clock >=3D 0); } =20 + lost_clock =3D arch_periodic_timer_update(s, period, lost_clock); + + /* + * we should make the time progress anyway. + */ + lost_clock =3D MIN(lost_clock, period); + assert(lost_clock >=3D 0); + next_irq_clock =3D cur_clock + period - lost_clock; s->next_periodic_time =3D muldiv64(next_irq_clock, NANOSECONDS_PER= _SECOND, RTC_CLOCK_RATE) + 1; timer_mod(s->periodic_timer, s->next_periodic_time); } else { -#ifdef TARGET_I386 - s->irq_coalesced =3D 0; -#endif + arch_periodic_timer_disable(s); timer_del(s->periodic_timer); } } --=20 2.9.3 From nobody Sun May 5 15:49:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491990940379178.56522400149095; Wed, 12 Apr 2017 02:55:40 -0700 (PDT) Received: from localhost ([::1]:43250 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEzz-0008IH-5S for importer@patchew.org; Wed, 12 Apr 2017 05:55:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51367) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyEwX-0005aP-VG for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyEwW-0004oI-Ij for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:06 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:35286) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyEwW-0004nf-As for qemu-devel@nongnu.org; Wed, 12 Apr 2017 05:52:04 -0400 Received: by mail-pf0-x243.google.com with SMTP id a188so4059452pfa.2 for ; Wed, 12 Apr 2017 02:52:04 -0700 (PDT) Received: from eric.tencent.com ([103.7.29.8]) by smtp.gmail.com with ESMTPSA id 4sm35445749pff.17.2017.04.12.02.52.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 02:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CUt7N+WQwV29GnvX3YwvHLD7J/l2NBkYag8VuOslczw=; b=qRTA+MKr3ks6bJDNdySlIQhYDs49clEnIOfsttZK8ozJaiLOk6ouY/aDTYJS4TEhHH y5uaNi6KhhbY+OEjuN1kLym0Z9VbSlliuVgzbjCJbeoi0pthJIlTCT9GfISYdxUq9Wik ky70wm0JjUXqh3H4CXl2U7POeIhrDozeM8bE5XzbcJPPRnaNFnRwTdz5ymjS+YObCnYF ngRNOLeYKUmGBt7BgTRnewjp061MJIZOke0OMlUTGitNslGxczkfsKjVhoHDtLvM7wAh YNMjTBegDWbRf1gbIfyxVloIgWWf8VrnYr3aoytUQ6zpQhFiIfbZiktnx3DDcXfe+LVs 8/6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CUt7N+WQwV29GnvX3YwvHLD7J/l2NBkYag8VuOslczw=; b=GGcP2s8KnmDUuetfPWXr3VXLNKl2JCnlHKk3gyCKQllWuJOiTyJ2IborfiPGd2/WxP TgPLZMqcfiEz9a1p972cUiOxwPD1hc+6hyIvqA3SrYmwEMip1gzRG3AZw7lek95NQOXC BgqHzZ6g/vHhp4nrQDYAawSwtoR02/WijbbbWcO+p5FkSwhZ9n5OY5JUA9Jx49Y2twKR EOfCC+D+qhNi7tqRltSr+g3rf8GNqKfQVt+t5gx+vSivWg1+MWuvCqwMMLKnnKEKazbR PJ5PXY63J2IdB833fEbL9ioz/0v0X8Yx/Radu5q2amo9UPpA/aA51mJCQlKDCKaX8wP3 D2XA== X-Gm-Message-State: AFeK/H1IqMi9wGh7aN1GvZSJpmfzw/dRaA6kcwWMRY+AEgBYkhZKa1Iw+rXgCSmckgebow== X-Received: by 10.98.156.23 with SMTP id f23mr64892370pfe.3.1491990723339; Wed, 12 Apr 2017 02:52:03 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Wed, 12 Apr 2017 17:51:11 +0800 Message-Id: <20170412095111.11728-6-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170412095111.11728-1-xiaoguangrong@tencent.com> References: <20170412095111.11728-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH 5/5] mc146818rtc: embrace all x86 specific code 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: Xiao Guangrong , yunfangtai@tencent.com, qemu-devel@nongnu.org, kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong This patch introduces proper hooks in the common code then moves x86 specific code into a single '#ifdef TARGET_I386' The real logic is not touched Signed-off-by: Xiao Guangrong --- hw/timer/mc146818rtc.c | 197 ++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 122 insertions(+), 75 deletions(-) diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c index d7b7c56..5cb4621 100644 --- a/hw/timer/mc146818rtc.c +++ b/hw/timer/mc146818rtc.c @@ -112,6 +112,9 @@ static uint64_t get_guest_rtc_ns(RTCState *s) guest_clock - s->last_update + s->offset; } =20 +static QLIST_HEAD(, RTCState) rtc_devices =3D + QLIST_HEAD_INITIALIZER(rtc_devices); + #ifdef TARGET_I386 static void rtc_coalesced_timer_update(RTCState *s) { @@ -145,6 +148,15 @@ static void rtc_coalesced_timer(void *opaque) rtc_coalesced_timer_update(s); } =20 +void qmp_rtc_reset_reinjection(Error **errp) +{ + RTCState *s; + + QLIST_FOREACH(s, &rtc_devices, link) { + s->irq_coalesced =3D 0; + } +} + static int64_t arch_periodic_timer_update(RTCState *s, int period, int64_t lost_clock) { @@ -191,6 +203,83 @@ static void arch_periodic_timer_disable(RTCState *s) { s->irq_coalesced =3D 0; } + +static void arch_rtc_periodic_timer(RTCState *s) +{ + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { + if (s->irq_reinject_on_ack_count >=3D RTC_REINJECT_ON_ACK_COUNT) { + s->irq_reinject_on_ack_count =3D 0; + } + apic_reset_irq_delivered(); + qemu_irq_raise(s->irq); + if (!apic_get_irq_delivered()) { + s->irq_coalesced++; + rtc_coalesced_timer_update(s); + DPRINTF_C("cmos: coalesced irqs increased to %d\n", + s->irq_coalesced); + } + } else { + qemu_irq_raise(s->irq); + } +} + +static void arch_read_regC(RTCState *s) +{ + if (s->irq_coalesced && (s->cmos_data[RTC_REG_B] & REG_B_PIE) && + s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) { + s->irq_reinject_on_ack_count++; + s->cmos_data[RTC_REG_C] |=3D REG_C_IRQF | REG_C_PF; + apic_reset_irq_delivered(); + DPRINTF_C("cmos: injecting on ack\n"); + qemu_irq_raise(s->irq); + if (apic_get_irq_delivered()) { + s->irq_coalesced--; + DPRINTF_C("cmos: coalesced irqs decreased to %d\n", + s->irq_coalesced); + } + } +} + +static void arch_rtc_post_load(RTCState *s, int version_id) +{ + if (version_id >=3D 2) { + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { + rtc_coalesced_timer_update(s); + } + } +} + +static void arch_rtc_notify_clock_reset(RTCState *s) +{ + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { + rtc_coalesced_timer_update(s); + } +} + +static void arch_rtc_reset(RTCState *s) +{ + if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { + s->irq_coalesced =3D 0; + s->irq_reinject_on_ack_count =3D 0; + } +} + +static bool arch_rtc_realizefn(RTCState *s, Error **errp) +{ + switch (s->lost_tick_policy) { + case LOST_TICK_POLICY_SLEW: + s->coalesced_timer =3D + timer_new_ns(rtc_clock, rtc_coalesced_timer, s); + break; + case LOST_TICK_POLICY_DISCARD: + break; + default: + error_setg(errp, "Invalid lost tick policy."); + return false; + } + + return true; +} #else static int64_t arch_periodic_timer_update(RTCState *s, int period, int64_t lost_clock) @@ -201,6 +290,32 @@ arch_periodic_timer_update(RTCState *s, int period, in= t64_t lost_clock) static void arch_periodic_timer_disable(RTCState *s) { } + +static void arch_rtc_periodic_timer(RTCState *s) +{ + qemu_irq_raise(s->irq); +} + +static void arch_read_regC(RTCState *s) +{ +} + +static void arch_rtc_post_load(RTCState *s, int version_id) +{ +} + +static void arch_rtc_notify_clock_reset(RTCState *s) +{ +} + +static void arch_rtc_reset(RTCState *s) +{ +} + +static bool arch_rtc_realizefn(RTCState *s, Error **errp) +{ + return true; +} #endif =20 static int period_code_to_clock(int period_code) @@ -287,21 +402,7 @@ static void rtc_periodic_timer(void *opaque) s->cmos_data[RTC_REG_C] |=3D REG_C_PF; if (s->cmos_data[RTC_REG_B] & REG_B_PIE) { s->cmos_data[RTC_REG_C] |=3D REG_C_IRQF; -#ifdef TARGET_I386 - if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { - if (s->irq_reinject_on_ack_count >=3D RTC_REINJECT_ON_ACK_COUN= T) - s->irq_reinject_on_ack_count =3D 0; =09 - apic_reset_irq_delivered(); - qemu_irq_raise(s->irq); - if (!apic_get_irq_delivered()) { - s->irq_coalesced++; - rtc_coalesced_timer_update(s); - DPRINTF_C("cmos: coalesced irqs increased to %d\n", - s->irq_coalesced); - } - } else -#endif - qemu_irq_raise(s->irq); + arch_rtc_periodic_timer(s); } } =20 @@ -656,20 +757,6 @@ static void rtc_get_time(RTCState *s, struct tm *tm) rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900; } =20 -static QLIST_HEAD(, RTCState) rtc_devices =3D - QLIST_HEAD_INITIALIZER(rtc_devices); - -#ifdef TARGET_I386 -void qmp_rtc_reset_reinjection(Error **errp) -{ - RTCState *s; - - QLIST_FOREACH(s, &rtc_devices, link) { - s->irq_coalesced =3D 0; - } -} -#endif - static void rtc_set_time(RTCState *s) { struct tm tm; @@ -789,22 +876,8 @@ static uint64_t cmos_ioport_read(void *opaque, hwaddr = addr, if (ret & (REG_C_UF | REG_C_AF)) { check_update_timer(s); } -#ifdef TARGET_I386 - if(s->irq_coalesced && - (s->cmos_data[RTC_REG_B] & REG_B_PIE) && - s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COU= NT) { - s->irq_reinject_on_ack_count++; - s->cmos_data[RTC_REG_C] |=3D REG_C_IRQF | REG_C_PF; - apic_reset_irq_delivered(); - DPRINTF_C("cmos: injecting on ack\n"); - qemu_irq_raise(s->irq); - if (apic_get_irq_delivered()) { - s->irq_coalesced--; - DPRINTF_C("cmos: coalesced irqs decreased to %d\n", - s->irq_coalesced); - } - } -#endif + + arch_read_regC(s); break; default: ret =3D s->cmos_data[s->cmos_index]; @@ -874,13 +947,7 @@ static int rtc_post_load(void *opaque, int version_id) } } =20 -#ifdef TARGET_I386 - if (version_id >=3D 2) { - if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { - rtc_coalesced_timer_update(s); - } - } -#endif + arch_rtc_post_load(s, version_id); return 0; } =20 @@ -937,11 +1004,7 @@ static void rtc_notify_clock_reset(Notifier *notifier= , void *data) rtc_set_date_from_host(ISA_DEVICE(s)); periodic_timer_update(s, now, 0); check_update_timer(s); -#ifdef TARGET_I386 - if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { - rtc_coalesced_timer_update(s); - } -#endif + arch_rtc_notify_clock_reset(s); } =20 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE) @@ -961,13 +1024,7 @@ static void rtc_reset(void *opaque) check_update_timer(s); =20 qemu_irq_lower(s->irq); - -#ifdef TARGET_I386 - if (s->lost_tick_policy =3D=3D LOST_TICK_POLICY_SLEW) { - s->irq_coalesced =3D 0; - s->irq_reinject_on_ack_count =3D 0; =09 - } -#endif + arch_rtc_reset(s); } =20 static const MemoryRegionOps cmos_ops =3D { @@ -1013,19 +1070,9 @@ static void rtc_realizefn(DeviceState *dev, Error **= errp) =20 rtc_set_date_from_host(isadev); =20 -#ifdef TARGET_I386 - switch (s->lost_tick_policy) { - case LOST_TICK_POLICY_SLEW: - s->coalesced_timer =3D - timer_new_ns(rtc_clock, rtc_coalesced_timer, s); - break; - case LOST_TICK_POLICY_DISCARD: - break; - default: - error_setg(errp, "Invalid lost tick policy."); + if (!arch_rtc_realizefn(s, errp)) { return; } -#endif =20 s->periodic_timer =3D timer_new_ns(rtc_clock, rtc_periodic_timer, s); s->update_timer =3D timer_new_ns(rtc_clock, rtc_update_timer, s); --=20 2.9.3