From patchwork Mon Feb 23 13:57:40 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 8406 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n1NDxrGb006133 for ; Mon, 23 Feb 2009 13:59:54 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754750AbZBWN7t (ORCPT ); Mon, 23 Feb 2009 08:59:49 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1754721AbZBWN7t (ORCPT ); Mon, 23 Feb 2009 08:59:49 -0500 Received: from mx2.redhat.com ([66.187.237.31]:53645 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754647AbZBWN7r (ORCPT ); Mon, 23 Feb 2009 08:59:47 -0500 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n1NDxlrh025568; Mon, 23 Feb 2009 08:59:47 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n1NDxkAd023856; Mon, 23 Feb 2009 08:59:47 -0500 Received: from amt.cnet (vpn-10-87.str.redhat.com [10.32.10.87]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n1NDxjrV028717; Mon, 23 Feb 2009 08:59:46 -0500 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id DFDEF588007; Mon, 23 Feb 2009 10:59:13 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id n1NDxBXN004484; Mon, 23 Feb 2009 10:59:11 -0300 Message-Id: <20090223135910.406346757@emt.localdomain> References: <20090223135738.355208932@emt.localdomain> User-Agent: quilt/0.46-1 Date: Mon, 23 Feb 2009 10:57:40 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org Cc: avi@redhat.com, sheng@linux.intel.com, Marcelo Tosatti Subject: [patch 2/3] KVM: PIT: remove usage of count_load_time for channel 0 Content-Disposition: inline; filename=i8254-get-rid-of-count-load-time X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We can infer elapsed time from hrtimer_expires_remaining. Signed-off-by: Marcelo Tosatti Index: kvm/arch/x86/kvm/i8254.c =================================================================== --- kvm.orig/arch/x86/kvm/i8254.c +++ kvm/arch/x86/kvm/i8254.c @@ -98,6 +98,32 @@ static int pit_get_gate(struct kvm *kvm, return kvm->arch.vpit->pit_state.channels[channel].gate; } +static s64 __kpit_elapsed(struct kvm *kvm) +{ + s64 elapsed; + ktime_t remaining; + struct kvm_kpit_state *ps = &kvm->arch.vpit->pit_state; + + remaining = hrtimer_expires_remaining(&ps->pit_timer.timer); + if (ktime_to_ns(remaining) < 0) + remaining = ktime_set(0, 0); + + elapsed = ps->pit_timer.period; + if (ktime_to_ns(remaining) <= ps->pit_timer.period) + elapsed = ps->pit_timer.period - ktime_to_ns(remaining); + + return elapsed; +} + +static s64 kpit_elapsed(struct kvm *kvm, struct kvm_kpit_channel_state *c, + int channel) +{ + if (channel == 0) + return __kpit_elapsed(kvm); + + return ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time)); +} + static int pit_get_count(struct kvm *kvm, int channel) { struct kvm_kpit_channel_state *c = @@ -107,7 +133,7 @@ static int pit_get_count(struct kvm *kvm WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); - t = ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time)); + t = kpit_elapsed(kvm, c, channel); d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC); switch (c->mode) { @@ -137,7 +163,7 @@ static int pit_get_out(struct kvm *kvm, WARN_ON(!mutex_is_locked(&kvm->arch.vpit->pit_state.lock)); - t = ktime_to_ns(ktime_sub(ktime_get(), c->count_load_time)); + t = kpit_elapsed(kvm, c, channel); d = muldiv64(t, KVM_PIT_FREQ, NSEC_PER_SEC); switch (c->mode) { @@ -208,8 +234,6 @@ static int __pit_timer_fn(struct kvm_kpi wake_up_interruptible(&vcpu0->wq); hrtimer_add_expires_ns(&pt->timer, pt->period); - if (pt->period) - ps->channels[0].count_load_time = ktime_get(); return (pt->period == 0 ? 0 : 1); } @@ -305,11 +329,12 @@ static void pit_load_count(struct kvm *k if (val == 0) val = 0x10000; - ps->channels[channel].count_load_time = ktime_get(); ps->channels[channel].count = val; - if (channel != 0) + if (channel != 0) { + ps->channels[channel].count_load_time = ktime_get(); return; + } /* Two types of timer * mode 1 is one shot, mode 2 is period, otherwise del timer */