diff options
author | kfraser@localhost.localdomain <kfraser@localhost.localdomain> | 2007-06-14 18:01:42 +0100 |
---|---|---|
committer | kfraser@localhost.localdomain <kfraser@localhost.localdomain> | 2007-06-14 18:01:42 +0100 |
commit | 4f3741b26466d7e1a7712f9e1695a8de5ccf8abf (patch) | |
tree | 2d34d6b2698a2b0d3cec8ff1eade1031192ba0d4 /xen/arch/x86/hvm/i8254.c | |
parent | c2c3a1c11e46fe18f390ee9b89519e596dde5306 (diff) | |
download | xen-4f3741b26466d7e1a7712f9e1695a8de5ccf8abf.tar.gz xen-4f3741b26466d7e1a7712f9e1695a8de5ccf8abf.tar.bz2 xen-4f3741b26466d7e1a7712f9e1695a8de5ccf8abf.zip |
hvm: Timer device model cleanups.
Signed-off-by: Keir Fraser <keir@xensource.com>
Diffstat (limited to 'xen/arch/x86/hvm/i8254.c')
-rw-r--r-- | xen/arch/x86/hvm/i8254.c | 264 |
1 files changed, 128 insertions, 136 deletions
diff --git a/xen/arch/x86/hvm/i8254.c b/xen/arch/x86/hvm/i8254.c index a3a21809fe..cca3a51b5c 100644 --- a/xen/arch/x86/hvm/i8254.c +++ b/xen/arch/x86/hvm/i8254.c @@ -3,12 +3,13 @@ * * Copyright (c) 2003-2004 Fabrice Bellard * Copyright (c) 2006 Intel Corperation + * Copyright (c) 2007 Keir Fraser, XenSource Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in @@ -18,14 +19,9 @@ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -/* Edwin Zhai <edwin.zhai@intel.com>, Eddie Dong <eddie.dong@intel.com> - * Ported to xen: - * Add a new layer of periodic time on top of PIT; - * move speaker io access to hypervisor; + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. */ #include <xen/config.h> @@ -41,9 +37,6 @@ #include <asm/hvm/vpt.h> #include <asm/current.h> -/* Enable DEBUG_PIT may cause guest calibration inaccuracy */ -/* #define DEBUG_PIT */ - #define RW_STATE_LSB 1 #define RW_STATE_MSB 2 #define RW_STATE_WORD0 3 @@ -85,7 +78,8 @@ static int pit_get_count(PITState *s, int channel) d = muldiv64(hvm_get_guest_time(pt->vcpu) - s->count_load_time[channel], PIT_FREQ, ticks_per_sec(pt->vcpu)); - switch(c->mode) { + switch ( c->mode ) + { case 0: case 1: case 4: @@ -103,16 +97,18 @@ static int pit_get_count(PITState *s, int channel) return counter; } -/* get pit output bit */ -int pit_get_out(PITState *pit, int channel, int64_t current_time) +int pit_get_out(PITState *pit, int channel) { struct hvm_hw_pit_channel *s = &pit->hw.channels[channel]; - uint64_t d; + uint64_t d, current_time; int out; + current_time = hvm_get_guest_time(pit->pt[channel].vcpu); d = muldiv64(current_time - pit->count_load_time[channel], PIT_FREQ, ticks_per_sec(pit->pt[channel].vcpu)); - switch(s->mode) { + + switch ( s->mode ) + { default: case 0: out = (d >= s->count); @@ -121,19 +117,17 @@ int pit_get_out(PITState *pit, int channel, int64_t current_time) out = (d < s->count); break; case 2: - if ((d % s->count) == 0 && d != 0) - out = 1; - else - out = 0; + out = (((d % s->count) == 0) && (d != 0)); break; case 3: - out = (d % s->count) < ((s->count + 1) >> 1); + out = ((d % s->count) < ((s->count + 1) >> 1)); break; case 4: case 5: out = (d == s->count); break; } + return out; } @@ -143,7 +137,8 @@ void pit_set_gate(PITState *pit, int channel, int val) struct hvm_hw_pit_channel *s = &pit->hw.channels[channel]; struct periodic_time *pt = &pit->pt[channel]; - switch(s->mode) { + switch ( s->mode ) + { default: case 0: case 4: @@ -151,22 +146,14 @@ void pit_set_gate(PITState *pit, int channel, int val) break; case 1: case 5: - if (s->gate < val) { - /* restart counting on rising edge */ - pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu); -// pit_irq_timer_update(s, s->count_load_time); - } - break; case 2: case 3: - if (s->gate < val) { - /* restart counting on rising edge */ + /* Restart counting on rising edge. */ + if ( s->gate < val ) pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu); -// pit_irq_timer_update(s, s->count_load_time); - } - /* XXX: disable/enable counting */ break; } + s->gate = val; } @@ -181,51 +168,42 @@ void pit_time_fired(struct vcpu *v, void *priv) *count_load_time = hvm_get_guest_time(v); } -static inline void pit_load_count(PITState *pit, int channel, int val) +static void pit_load_count(PITState *pit, int channel, int val) { u32 period; struct hvm_hw_pit_channel *s = &pit->hw.channels[channel]; struct periodic_time *pt = &pit->pt[channel]; + struct domain *d = container_of(pit, struct domain, + arch.hvm_domain.pl_time.vpit); struct vcpu *v; - if (val == 0) + if ( val == 0 ) val = 0x10000; + pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu); s->count = val; period = DIV_ROUND((val * 1000000000ULL), PIT_FREQ); - if (channel != 0) + if ( !is_hvm_domain(d) || (channel != 0) ) return; -#ifdef DEBUG_PIT - printk("HVM_PIT: pit-load-counter(%p), count=0x%x, period=%uns mode=%d, load_time=%lld\n", - s, - val, - period, - s->mode, - (long long)pit->count_load_time[channel]); -#endif - /* Choose a vcpu to set the timer on: current if appropriate else vcpu 0 */ - if ( likely(pit == ¤t->domain->arch.hvm_domain.pl_time.vpit) ) + if ( pit == ¤t->domain->arch.hvm_domain.pl_time.vpit ) v = current; else - v = container_of(pit, struct domain, - arch.hvm_domain.pl_time.vpit)->vcpu[0]; + v = d->vcpu[0]; - switch (s->mode) { + switch ( s->mode ) + { case 2: - /* create periodic time */ + /* Periodic timer. */ create_periodic_time(v, pt, period, 0, 0, pit_time_fired, &pit->count_load_time[channel]); break; case 1: - /* create one shot time */ + /* One-shot timer. */ create_periodic_time(v, pt, period, 0, 1, pit_time_fired, &pit->count_load_time[channel]); -#ifdef DEBUG_PIT - printk("HVM_PIT: create one shot time.\n"); -#endif break; default: destroy_periodic_time(pt); @@ -233,63 +211,85 @@ static inline void pit_load_count(PITState *pit, int channel, int val) } } -/* if already latched, do not latch again */ static void pit_latch_count(PITState *s, int channel) { struct hvm_hw_pit_channel *c = &s->hw.channels[channel]; - if (!c->count_latched) { + if ( !c->count_latched ) + { c->latched_count = pit_get_count(s, channel); c->count_latched = c->rw_mode; } } +static void pit_latch_status(PITState *s, int channel) +{ + struct hvm_hw_pit_channel *c = &s->hw.channels[channel]; + if ( !c->status_latched ) + { + /* TODO: Return NULL COUNT (bit 6). */ + c->status = ((pit_get_out(s, channel) << 7) | + (c->rw_mode << 4) | + (c->mode << 1) | + c->bcd); + c->status_latched = 1; + } +} + static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val) { PITState *pit = opaque; int channel, access; struct hvm_hw_pit_channel *s; - val &= 0xff; + val &= 0xff; addr &= 3; - if (addr == 3) { + + if ( addr == 3 ) + { channel = val >> 6; - if (channel == 3) { - /* read back command */ - for(channel = 0; channel < 3; channel++) { + if ( channel == 3 ) + { + /* Read-Back Command. */ + for ( channel = 0; channel < 3; channel++ ) + { s = &pit->hw.channels[channel]; - if (val & (2 << channel)) { - if (!(val & 0x20)) { + if ( val & (2 << channel) ) + { + if ( !(val & 0x20) ) pit_latch_count(pit, channel); - } - if (!(val & 0x10) && !s->status_latched) { - /* status latch */ - /* XXX: add BCD and null count */ - s->status = (pit_get_out(pit, channel, hvm_get_guest_time(pit->pt[channel].vcpu)) << 7) | - (s->rw_mode << 4) | - (s->mode << 1) | - s->bcd; - s->status_latched = 1; - } + if ( !(val & 0x10) ) + pit_latch_status(pit, channel); } } - } else { + } + else + { + /* Select Counter <channel>. */ s = &pit->hw.channels[channel]; access = (val >> 4) & 3; - if (access == 0) { + if ( access == 0 ) + { pit_latch_count(pit, channel); - } else { + } + else + { s->rw_mode = access; s->read_state = access; s->write_state = access; - s->mode = (val >> 1) & 7; + if ( s->mode > 5 ) + s->mode -= 4; s->bcd = val & 1; /* XXX: update irq timer ? */ } } - } else { + } + else + { + /* Write Count. */ s = &pit->hw.channels[addr]; - switch(s->write_state) { + switch ( s->write_state ) + { default: case RW_STATE_LSB: pit_load_count(pit, addr, val); @@ -317,11 +317,16 @@ static uint32_t pit_ioport_read(void *opaque, uint32_t addr) addr &= 3; s = &pit->hw.channels[addr]; - if (s->status_latched) { + + if ( s->status_latched ) + { s->status_latched = 0; ret = s->status; - } else if (s->count_latched) { - switch(s->count_latched) { + } + else if ( s->count_latched ) + { + switch ( s->count_latched ) + { default: case RW_STATE_LSB: ret = s->latched_count & 0xff; @@ -336,8 +341,11 @@ static uint32_t pit_ioport_read(void *opaque, uint32_t addr) s->count_latched = RW_STATE_MSB; break; } - } else { - switch(s->read_state) { + } + else + { + switch ( s->read_state ) + { default: case RW_STATE_LSB: count = pit_get_count(pit, addr); @@ -359,6 +367,7 @@ static uint32_t pit_ioport_read(void *opaque, uint32_t addr) break; } } + return ret; } @@ -374,7 +383,8 @@ static void pit_info(PITState *pit) struct periodic_time *pt; int i; - for(i = 0; i < 3; i++) { + for ( i = 0; i < 3; i++ ) + { printk("*****pit channel %d's state:*****\n", i); s = &pit->hw.channels[i]; printk("pit 0x%x.\n", s->count); @@ -392,7 +402,8 @@ static void pit_info(PITState *pit) printk("pit %"PRId64"\n", pit->count_load_time[i]); pt = &pit->pt[i]; - if (pt) { + if ( pt ) + { printk("pit channel %d has a periodic timer:\n", i); printk("pt %d.\n", pt->enabled); printk("pt %d.\n", pt->one_shot); @@ -405,7 +416,6 @@ static void pit_info(PITState *pit) printk("pt %"PRId64"\n", pt->last_plt_gtime); } } - } #else static void pit_info(PITState *pit) @@ -435,8 +445,8 @@ static int pit_load(struct domain *d, hvm_domain_context_t *h) /* Recreate platform timers from hardware state. There will be some * time jitter here, but the wall-clock will have jumped massively, so * we hope the guest can handle it. */ - - for(i = 0; i < 3; i++) { + for ( i = 0; i < 3; i++ ) + { pit_load_count(pit, i, pit->hw.channels[i].count); pit->pt[i].last_plt_gtime = hvm_get_guest_time(d->vcpu[0]); } @@ -453,7 +463,8 @@ static void pit_reset(void *opaque) struct hvm_hw_pit_channel *s; int i; - for(i = 0;i < 3; i++) { + for ( i = 0; i < 3; i++ ) + { s = &pit->hw.channels[i]; destroy_periodic_time(&pit->pt[i]); s->mode = 0xff; /* the init mode */ @@ -468,38 +479,21 @@ void pit_init(struct vcpu *v, unsigned long cpu_khz) struct periodic_time *pt; pt = &pit->pt[0]; - pt->vcpu = v; - /* the timer 0 is connected to an IRQ */ - init_timer(&pt->timer, pt_timer_fn, pt, v->processor); - pt++; pt->vcpu = v; - pt++; pt->vcpu = v; + pt[0].vcpu = v; + pt[1].vcpu = v; + pt[2].vcpu = v; register_portio_handler(v->domain, PIT_BASE, 4, handle_pit_io); /* register the speaker port */ register_portio_handler(v->domain, 0x61, 1, handle_speaker_io); ticks_per_sec(v) = cpu_khz * (int64_t)1000; -#ifdef DEBUG_PIT - printk("HVM_PIT: guest frequency =%lld\n", (long long)ticks_per_sec(v)); -#endif pit_reset(pit); - return; -} - -void pit_migrate_timers(struct vcpu *v) -{ - PITState *pit = &v->domain->arch.hvm_domain.pl_time.vpit; - struct periodic_time *pt; - - pt = &pit->pt[0]; - if ( pt->vcpu == v && pt->enabled ) - migrate_timer(&pt->timer, v->processor); } void pit_deinit(struct domain *d) { PITState *pit = &d->arch.hvm_domain.pl_time.vpit; - - kill_timer(&pit->pt[0].timer); + destroy_periodic_time(&pit->pt[0]); } /* the intercept action for PIT DM retval:0--not handled; 1--handled */ @@ -508,22 +502,24 @@ static int handle_pit_io(ioreq_t *p) struct vcpu *v = current; struct PITState *vpit = &(v->domain->arch.hvm_domain.pl_time.vpit); - if (p->size != 1 || - p->data_is_ptr || - p->type != IOREQ_TYPE_PIO){ - printk("HVM_PIT:wrong PIT IO!\n"); + if ( (p->size != 1) || p->data_is_ptr || (p->type != IOREQ_TYPE_PIO) ) + { + gdprintk(XENLOG_WARNING, "HVM_PIT bad access\n"); return 1; } - if (p->dir == 0) {/* write */ + if ( p->dir == IOREQ_WRITE ) + { pit_ioport_write(vpit, p->addr, p->data); - } else if (p->dir == 1) { /* read */ - if ( (p->addr & 3) != 3 ) { + } + else + { + if ( (p->addr & 3) != 3 ) p->data = pit_ioport_read(vpit, p->addr); - } else { - printk("HVM_PIT: read A1:A0=3!\n"); - } + else + gdprintk(XENLOG_WARNING, "HVM_PIT: read A1:A0=3!\n"); } + return 1; } @@ -537,12 +533,10 @@ static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val) static uint32_t speaker_ioport_read(void *opaque, uint32_t addr) { PITState *pit = opaque; - int out = pit_get_out(pit, 2, - hvm_get_guest_time(pit->pt[2].vcpu)); /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */ unsigned int refresh_clock = ((unsigned int)NOW() >> 14) & 1; return ((pit->hw.speaker_data_on << 1) | pit_get_gate(pit, 2) | - (out << 5) | refresh_clock << 4); + (pit_get_out(pit, 2) << 5) | (refresh_clock << 4)); } static int handle_speaker_io(ioreq_t *p) @@ -550,18 +544,16 @@ static int handle_speaker_io(ioreq_t *p) struct vcpu *v = current; struct PITState *vpit = &(v->domain->arch.hvm_domain.pl_time.vpit); - if (p->size != 1 || - p->data_is_ptr || - p->type != IOREQ_TYPE_PIO){ - printk("HVM_SPEAKER:wrong SPEAKER IO!\n"); + if ( (p->size != 1) || p->data_is_ptr || (p->type != IOREQ_TYPE_PIO) ) + { + gdprintk(XENLOG_WARNING, "HVM_SPEAKER bad access\n"); return 1; } - if (p->dir == 0) {/* write */ + if ( p->dir == IOREQ_WRITE ) speaker_ioport_write(vpit, p->addr, p->data); - } else if (p->dir == 1) {/* read */ + else p->data = speaker_ioport_read(vpit, p->addr); - } return 1; } @@ -576,7 +568,7 @@ int pv_pit_handler(int port, int data, int write) .data = write ? data : 0, }; - if (port == 0x61) + if ( port == 0x61 ) handle_speaker_io(&ioreq); else handle_pit_io(&ioreq); |