direct-io.hg

changeset 15279:80eb95dc0dd9

hvm: Timer device model cleanups.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Jun 14 18:01:42 2007 +0100 (2007-06-14)
parents a0c0c7efffca
children 80577631fb87
files xen/arch/x86/hvm/hvm.c xen/arch/x86/hvm/i8254.c xen/arch/x86/hvm/rtc.c xen/arch/x86/hvm/vlapic.c xen/arch/x86/hvm/vpt.c xen/include/asm-x86/hvm/vpt.h
line diff
     1.1 --- a/xen/arch/x86/hvm/hvm.c	Thu Jun 14 18:01:07 2007 +0100
     1.2 +++ b/xen/arch/x86/hvm/hvm.c	Thu Jun 14 18:01:42 2007 +0100
     1.3 @@ -109,11 +109,8 @@ u64 hvm_get_guest_time(struct vcpu *v)
     1.4  
     1.5  void hvm_migrate_timers(struct vcpu *v)
     1.6  {
     1.7 -    pit_migrate_timers(v);
     1.8      rtc_migrate_timers(v);
     1.9 -    hpet_migrate_timers(v);
    1.10 -    if ( vcpu_vlapic(v)->pt.enabled )
    1.11 -        migrate_timer(&vcpu_vlapic(v)->pt.timer, v->processor);
    1.12 +    pt_migrate(v);
    1.13  }
    1.14  
    1.15  void hvm_do_resume(struct vcpu *v)
     2.1 --- a/xen/arch/x86/hvm/i8254.c	Thu Jun 14 18:01:07 2007 +0100
     2.2 +++ b/xen/arch/x86/hvm/i8254.c	Thu Jun 14 18:01:42 2007 +0100
     2.3 @@ -3,12 +3,13 @@
     2.4   * 
     2.5   * Copyright (c) 2003-2004 Fabrice Bellard
     2.6   * Copyright (c) 2006 Intel Corperation
     2.7 + * Copyright (c) 2007 Keir Fraser, XenSource Inc.
     2.8   * 
     2.9   * Permission is hereby granted, free of charge, to any person obtaining a copy
    2.10 - * of this software and associated documentation files (the "Software"), to deal
    2.11 - * in the Software without restriction, including without limitation the rights
    2.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    2.13 - * copies of the Software, and to permit persons to whom the Software is
    2.14 + * of this software and associated documentation files (the "Software"), to
    2.15 + * deal in the Software without restriction, including without limitation the
    2.16 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    2.17 + * sell copies of the Software, and to permit persons to whom the Software is
    2.18   * furnished to do so, subject to the following conditions:
    2.19   *
    2.20   * The above copyright notice and this permission notice shall be included in
    2.21 @@ -18,14 +19,9 @@
    2.22   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    2.23   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    2.24   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    2.25 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    2.26 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    2.27 - * THE SOFTWARE.
    2.28 - */
    2.29 -/* Edwin Zhai <edwin.zhai@intel.com>, Eddie Dong <eddie.dong@intel.com>
    2.30 - * Ported to xen:
    2.31 - * Add a new layer of periodic time on top of PIT;
    2.32 - * move speaker io access to hypervisor;
    2.33 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    2.34 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    2.35 + * IN THE SOFTWARE.
    2.36   */
    2.37  
    2.38  #include <xen/config.h>
    2.39 @@ -41,9 +37,6 @@
    2.40  #include <asm/hvm/vpt.h>
    2.41  #include <asm/current.h>
    2.42  
    2.43 -/* Enable DEBUG_PIT may cause guest calibration inaccuracy */
    2.44 -/* #define DEBUG_PIT */
    2.45 -
    2.46  #define RW_STATE_LSB 1
    2.47  #define RW_STATE_MSB 2
    2.48  #define RW_STATE_WORD0 3
    2.49 @@ -85,7 +78,8 @@ static int pit_get_count(PITState *s, in
    2.50  
    2.51      d = muldiv64(hvm_get_guest_time(pt->vcpu) - s->count_load_time[channel],
    2.52                   PIT_FREQ, ticks_per_sec(pt->vcpu));
    2.53 -    switch(c->mode) {
    2.54 +    switch ( c->mode )
    2.55 +    {
    2.56      case 0:
    2.57      case 1:
    2.58      case 4:
    2.59 @@ -103,16 +97,18 @@ static int pit_get_count(PITState *s, in
    2.60      return counter;
    2.61  }
    2.62  
    2.63 -/* get pit output bit */
    2.64 -int pit_get_out(PITState *pit, int channel, int64_t current_time)
    2.65 +int pit_get_out(PITState *pit, int channel)
    2.66  {
    2.67      struct hvm_hw_pit_channel *s = &pit->hw.channels[channel];
    2.68 -    uint64_t d;
    2.69 +    uint64_t d, current_time;
    2.70      int out;
    2.71  
    2.72 +    current_time = hvm_get_guest_time(pit->pt[channel].vcpu);
    2.73      d = muldiv64(current_time - pit->count_load_time[channel], 
    2.74                   PIT_FREQ, ticks_per_sec(pit->pt[channel].vcpu));
    2.75 -    switch(s->mode) {
    2.76 +
    2.77 +    switch ( s->mode )
    2.78 +    {
    2.79      default:
    2.80      case 0:
    2.81          out = (d >= s->count);
    2.82 @@ -121,19 +117,17 @@ int pit_get_out(PITState *pit, int chann
    2.83          out = (d < s->count);
    2.84          break;
    2.85      case 2:
    2.86 -        if ((d % s->count) == 0 && d != 0)
    2.87 -            out = 1;
    2.88 -        else
    2.89 -            out = 0;
    2.90 +        out = (((d % s->count) == 0) && (d != 0));
    2.91          break;
    2.92      case 3:
    2.93 -        out = (d % s->count) < ((s->count + 1) >> 1);
    2.94 +        out = ((d % s->count) < ((s->count + 1) >> 1));
    2.95          break;
    2.96      case 4:
    2.97      case 5:
    2.98          out = (d == s->count);
    2.99          break;
   2.100      }
   2.101 +
   2.102      return out;
   2.103  }
   2.104  
   2.105 @@ -143,7 +137,8 @@ void pit_set_gate(PITState *pit, int cha
   2.106      struct hvm_hw_pit_channel *s = &pit->hw.channels[channel];
   2.107      struct periodic_time *pt = &pit->pt[channel];
   2.108  
   2.109 -    switch(s->mode) {
   2.110 +    switch ( s->mode )
   2.111 +    {
   2.112      default:
   2.113      case 0:
   2.114      case 4:
   2.115 @@ -151,22 +146,14 @@ void pit_set_gate(PITState *pit, int cha
   2.116          break;
   2.117      case 1:
   2.118      case 5:
   2.119 -        if (s->gate < val) {
   2.120 -            /* restart counting on rising edge */
   2.121 -            pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu);
   2.122 -//            pit_irq_timer_update(s, s->count_load_time);
   2.123 -        }
   2.124 -        break;
   2.125      case 2:
   2.126      case 3:
   2.127 -        if (s->gate < val) {
   2.128 -            /* restart counting on rising edge */
   2.129 +        /* Restart counting on rising edge. */
   2.130 +        if ( s->gate < val )
   2.131              pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu);
   2.132 -//            pit_irq_timer_update(s, s->count_load_time);
   2.133 -        }
   2.134 -        /* XXX: disable/enable counting */
   2.135          break;
   2.136      }
   2.137 +
   2.138      s->gate = val;
   2.139  }
   2.140  
   2.141 @@ -181,51 +168,42 @@ void pit_time_fired(struct vcpu *v, void
   2.142      *count_load_time = hvm_get_guest_time(v);
   2.143  }
   2.144  
   2.145 -static inline void pit_load_count(PITState *pit, int channel, int val)
   2.146 +static void pit_load_count(PITState *pit, int channel, int val)
   2.147  {
   2.148      u32 period;
   2.149      struct hvm_hw_pit_channel *s = &pit->hw.channels[channel];
   2.150      struct periodic_time *pt = &pit->pt[channel];
   2.151 +    struct domain *d = container_of(pit, struct domain,
   2.152 +                                    arch.hvm_domain.pl_time.vpit);
   2.153      struct vcpu *v;
   2.154  
   2.155 -    if (val == 0)
   2.156 +    if ( val == 0 )
   2.157          val = 0x10000;
   2.158 +
   2.159      pit->count_load_time[channel] = hvm_get_guest_time(pt->vcpu);
   2.160      s->count = val;
   2.161      period = DIV_ROUND((val * 1000000000ULL), PIT_FREQ);
   2.162  
   2.163 -    if (channel != 0)
   2.164 +    if ( !is_hvm_domain(d) || (channel != 0) )
   2.165          return;
   2.166  
   2.167 -#ifdef DEBUG_PIT
   2.168 -    printk("HVM_PIT: pit-load-counter(%p), count=0x%x, period=%uns mode=%d, load_time=%lld\n",
   2.169 -            s,
   2.170 -            val,
   2.171 -            period,
   2.172 -            s->mode,
   2.173 -            (long long)pit->count_load_time[channel]);
   2.174 -#endif
   2.175 -
   2.176      /* Choose a vcpu to set the timer on: current if appropriate else vcpu 0 */
   2.177 -    if ( likely(pit == &current->domain->arch.hvm_domain.pl_time.vpit) )
   2.178 +    if ( pit == &current->domain->arch.hvm_domain.pl_time.vpit )
   2.179          v = current;
   2.180      else 
   2.181 -        v = container_of(pit, struct domain, 
   2.182 -                         arch.hvm_domain.pl_time.vpit)->vcpu[0];
   2.183 +        v = d->vcpu[0];
   2.184  
   2.185 -    switch (s->mode) {
   2.186 +    switch ( s->mode )
   2.187 +    {
   2.188          case 2:
   2.189 -            /* create periodic time */
   2.190 +            /* Periodic timer. */
   2.191              create_periodic_time(v, pt, period, 0, 0, pit_time_fired, 
   2.192                                   &pit->count_load_time[channel]);
   2.193              break;
   2.194          case 1:
   2.195 -            /* create one shot time */
   2.196 +            /* One-shot timer. */
   2.197              create_periodic_time(v, pt, period, 0, 1, pit_time_fired,
   2.198                                   &pit->count_load_time[channel]);
   2.199 -#ifdef DEBUG_PIT
   2.200 -            printk("HVM_PIT: create one shot time.\n");
   2.201 -#endif
   2.202              break;
   2.203          default:
   2.204              destroy_periodic_time(pt);
   2.205 @@ -233,63 +211,85 @@ static inline void pit_load_count(PITSta
   2.206      }
   2.207  }
   2.208  
   2.209 -/* if already latched, do not latch again */
   2.210  static void pit_latch_count(PITState *s, int channel)
   2.211  {
   2.212      struct hvm_hw_pit_channel *c = &s->hw.channels[channel];
   2.213 -    if (!c->count_latched) {
   2.214 +    if ( !c->count_latched )
   2.215 +    {
   2.216          c->latched_count = pit_get_count(s, channel);
   2.217          c->count_latched = c->rw_mode;
   2.218      }
   2.219  }
   2.220  
   2.221 +static void pit_latch_status(PITState *s, int channel)
   2.222 +{
   2.223 +    struct hvm_hw_pit_channel *c = &s->hw.channels[channel];
   2.224 +    if ( !c->status_latched )
   2.225 +    {
   2.226 +        /* TODO: Return NULL COUNT (bit 6). */
   2.227 +        c->status = ((pit_get_out(s, channel) << 7) |
   2.228 +                     (c->rw_mode << 4) |
   2.229 +                     (c->mode << 1) |
   2.230 +                     c->bcd);
   2.231 +        c->status_latched = 1;
   2.232 +    }
   2.233 +}
   2.234 +
   2.235  static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val)
   2.236  {
   2.237      PITState *pit = opaque;
   2.238      int channel, access;
   2.239      struct hvm_hw_pit_channel *s;
   2.240 -    val &= 0xff;
   2.241  
   2.242 +    val  &= 0xff;
   2.243      addr &= 3;
   2.244 -    if (addr == 3) {
   2.245 +
   2.246 +    if ( addr == 3 )
   2.247 +    {
   2.248          channel = val >> 6;
   2.249 -        if (channel == 3) {
   2.250 -            /* read back command */
   2.251 -            for(channel = 0; channel < 3; channel++) {
   2.252 +        if ( channel == 3 )
   2.253 +        {
   2.254 +            /* Read-Back Command. */
   2.255 +            for ( channel = 0; channel < 3; channel++ )
   2.256 +            {
   2.257                  s = &pit->hw.channels[channel];
   2.258 -                if (val & (2 << channel)) {
   2.259 -                    if (!(val & 0x20)) {
   2.260 +                if ( val & (2 << channel) )
   2.261 +                {
   2.262 +                    if ( !(val & 0x20) )
   2.263                          pit_latch_count(pit, channel);
   2.264 -                    }
   2.265 -                    if (!(val & 0x10) && !s->status_latched) {
   2.266 -                        /* status latch */
   2.267 -                        /* XXX: add BCD and null count */
   2.268 -                        s->status = (pit_get_out(pit, channel, hvm_get_guest_time(pit->pt[channel].vcpu)) << 7) |
   2.269 -                            (s->rw_mode << 4) |
   2.270 -                            (s->mode << 1) |
   2.271 -                            s->bcd;
   2.272 -                        s->status_latched = 1;
   2.273 -                    }
   2.274 +                    if ( !(val & 0x10) )
   2.275 +                        pit_latch_status(pit, channel);
   2.276                  }
   2.277              }
   2.278 -        } else {
   2.279 +        }
   2.280 +        else
   2.281 +        {
   2.282 +            /* Select Counter <channel>. */
   2.283              s = &pit->hw.channels[channel];
   2.284              access = (val >> 4) & 3;
   2.285 -            if (access == 0) {
   2.286 +            if ( access == 0 )
   2.287 +            {
   2.288                  pit_latch_count(pit, channel);
   2.289 -            } else {
   2.290 +            }
   2.291 +            else
   2.292 +            {
   2.293                  s->rw_mode = access;
   2.294                  s->read_state = access;
   2.295                  s->write_state = access;
   2.296 -
   2.297                  s->mode = (val >> 1) & 7;
   2.298 +                if ( s->mode > 5 )
   2.299 +                    s->mode -= 4;
   2.300                  s->bcd = val & 1;
   2.301                  /* XXX: update irq timer ? */
   2.302              }
   2.303          }
   2.304 -    } else {
   2.305 +    }
   2.306 +    else
   2.307 +    {
   2.308 +        /* Write Count. */
   2.309          s = &pit->hw.channels[addr];
   2.310 -        switch(s->write_state) {
   2.311 +        switch ( s->write_state )
   2.312 +        {
   2.313          default:
   2.314          case RW_STATE_LSB:
   2.315              pit_load_count(pit, addr, val);
   2.316 @@ -317,11 +317,16 @@ static uint32_t pit_ioport_read(void *op
   2.317      
   2.318      addr &= 3;
   2.319      s = &pit->hw.channels[addr];
   2.320 -    if (s->status_latched) {
   2.321 +
   2.322 +    if ( s->status_latched )
   2.323 +    {
   2.324          s->status_latched = 0;
   2.325          ret = s->status;
   2.326 -    } else if (s->count_latched) {
   2.327 -        switch(s->count_latched) {
   2.328 +    }
   2.329 +    else if ( s->count_latched )
   2.330 +    {
   2.331 +        switch ( s->count_latched )
   2.332 +        {
   2.333          default:
   2.334          case RW_STATE_LSB:
   2.335              ret = s->latched_count & 0xff;
   2.336 @@ -336,8 +341,11 @@ static uint32_t pit_ioport_read(void *op
   2.337              s->count_latched = RW_STATE_MSB;
   2.338              break;
   2.339          }
   2.340 -    } else {
   2.341 -        switch(s->read_state) {
   2.342 +    }
   2.343 +    else
   2.344 +    {
   2.345 +        switch ( s->read_state )
   2.346 +        {
   2.347          default:
   2.348          case RW_STATE_LSB:
   2.349              count = pit_get_count(pit, addr);
   2.350 @@ -359,6 +367,7 @@ static uint32_t pit_ioport_read(void *op
   2.351              break;
   2.352          }
   2.353      }
   2.354 +
   2.355      return ret;
   2.356  }
   2.357  
   2.358 @@ -374,7 +383,8 @@ static void pit_info(PITState *pit)
   2.359      struct periodic_time *pt;
   2.360      int i;
   2.361  
   2.362 -    for(i = 0; i < 3; i++) {
   2.363 +    for ( i = 0; i < 3; i++ )
   2.364 +    {
   2.365          printk("*****pit channel %d's state:*****\n", i);
   2.366          s = &pit->hw.channels[i];
   2.367          printk("pit 0x%x.\n", s->count);
   2.368 @@ -392,7 +402,8 @@ static void pit_info(PITState *pit)
   2.369          printk("pit %"PRId64"\n", pit->count_load_time[i]);
   2.370  
   2.371          pt = &pit->pt[i];
   2.372 -        if (pt) {
   2.373 +        if ( pt )
   2.374 +        {
   2.375              printk("pit channel %d has a periodic timer:\n", i);
   2.376              printk("pt %d.\n", pt->enabled);
   2.377              printk("pt %d.\n", pt->one_shot);
   2.378 @@ -405,7 +416,6 @@ static void pit_info(PITState *pit)
   2.379              printk("pt %"PRId64"\n", pt->last_plt_gtime);
   2.380          }
   2.381      }
   2.382 -
   2.383  }
   2.384  #else
   2.385  static void pit_info(PITState *pit)
   2.386 @@ -435,8 +445,8 @@ static int pit_load(struct domain *d, hv
   2.387      /* Recreate platform timers from hardware state.  There will be some 
   2.388       * time jitter here, but the wall-clock will have jumped massively, so 
   2.389       * we hope the guest can handle it. */
   2.390 -
   2.391 -    for(i = 0; i < 3; i++) {
   2.392 +    for ( i = 0; i < 3; i++ )
   2.393 +    {
   2.394          pit_load_count(pit, i, pit->hw.channels[i].count);
   2.395          pit->pt[i].last_plt_gtime = hvm_get_guest_time(d->vcpu[0]);
   2.396      }
   2.397 @@ -453,7 +463,8 @@ static void pit_reset(void *opaque)
   2.398      struct hvm_hw_pit_channel *s;
   2.399      int i;
   2.400  
   2.401 -    for(i = 0;i < 3; i++) {
   2.402 +    for ( i = 0; i < 3; i++ )
   2.403 +    {
   2.404          s = &pit->hw.channels[i];
   2.405          destroy_periodic_time(&pit->pt[i]);
   2.406          s->mode = 0xff; /* the init mode */
   2.407 @@ -468,38 +479,21 @@ void pit_init(struct vcpu *v, unsigned l
   2.408      struct periodic_time *pt;
   2.409  
   2.410      pt = &pit->pt[0];  
   2.411 -    pt->vcpu = v;
   2.412 -    /* the timer 0 is connected to an IRQ */
   2.413 -    init_timer(&pt->timer, pt_timer_fn, pt, v->processor);
   2.414 -    pt++; pt->vcpu = v;
   2.415 -    pt++; pt->vcpu = v;
   2.416 +    pt[0].vcpu = v;
   2.417 +    pt[1].vcpu = v;
   2.418 +    pt[2].vcpu = v;
   2.419  
   2.420      register_portio_handler(v->domain, PIT_BASE, 4, handle_pit_io);
   2.421      /* register the speaker port */
   2.422      register_portio_handler(v->domain, 0x61, 1, handle_speaker_io);
   2.423      ticks_per_sec(v) = cpu_khz * (int64_t)1000;
   2.424 -#ifdef DEBUG_PIT
   2.425 -    printk("HVM_PIT: guest frequency =%lld\n", (long long)ticks_per_sec(v));
   2.426 -#endif
   2.427      pit_reset(pit);
   2.428 -    return;
   2.429 -}
   2.430 -
   2.431 -void pit_migrate_timers(struct vcpu *v)
   2.432 -{
   2.433 -    PITState *pit = &v->domain->arch.hvm_domain.pl_time.vpit;
   2.434 -    struct periodic_time *pt;
   2.435 -
   2.436 -    pt = &pit->pt[0];
   2.437 -    if ( pt->vcpu == v && pt->enabled )
   2.438 -        migrate_timer(&pt->timer, v->processor);
   2.439  }
   2.440  
   2.441  void pit_deinit(struct domain *d)
   2.442  {
   2.443      PITState *pit = &d->arch.hvm_domain.pl_time.vpit;
   2.444 -
   2.445 -    kill_timer(&pit->pt[0].timer);
   2.446 +    destroy_periodic_time(&pit->pt[0]);
   2.447  }
   2.448  
   2.449  /* the intercept action for PIT DM retval:0--not handled; 1--handled */  
   2.450 @@ -508,22 +502,24 @@ static int handle_pit_io(ioreq_t *p)
   2.451      struct vcpu *v = current;
   2.452      struct PITState *vpit = &(v->domain->arch.hvm_domain.pl_time.vpit);
   2.453  
   2.454 -    if (p->size != 1 ||
   2.455 -        p->data_is_ptr ||
   2.456 -        p->type != IOREQ_TYPE_PIO){
   2.457 -        printk("HVM_PIT:wrong PIT IO!\n");
   2.458 +    if ( (p->size != 1) || p->data_is_ptr || (p->type != IOREQ_TYPE_PIO) )
   2.459 +    {
   2.460 +        gdprintk(XENLOG_WARNING, "HVM_PIT bad access\n");
   2.461          return 1;
   2.462      }
   2.463      
   2.464 -    if (p->dir == 0) {/* write */
   2.465 +    if ( p->dir == IOREQ_WRITE )
   2.466 +    {
   2.467          pit_ioport_write(vpit, p->addr, p->data);
   2.468 -    } else if (p->dir == 1) { /* read */
   2.469 -        if ( (p->addr & 3) != 3 ) {
   2.470 +    }
   2.471 +    else
   2.472 +    {
   2.473 +        if ( (p->addr & 3) != 3 )
   2.474              p->data = pit_ioport_read(vpit, p->addr);
   2.475 -        } else {
   2.476 -            printk("HVM_PIT: read A1:A0=3!\n");
   2.477 -        }
   2.478 +        else
   2.479 +            gdprintk(XENLOG_WARNING, "HVM_PIT: read A1:A0=3!\n");
   2.480      }
   2.481 +
   2.482      return 1;
   2.483  }
   2.484  
   2.485 @@ -537,12 +533,10 @@ static void speaker_ioport_write(void *o
   2.486  static uint32_t speaker_ioport_read(void *opaque, uint32_t addr)
   2.487  {
   2.488      PITState *pit = opaque;
   2.489 -    int out = pit_get_out(pit, 2,
   2.490 -                          hvm_get_guest_time(pit->pt[2].vcpu));
   2.491      /* Refresh clock toggles at about 15us. We approximate as 2^14ns. */
   2.492      unsigned int refresh_clock = ((unsigned int)NOW() >> 14) & 1;
   2.493      return ((pit->hw.speaker_data_on << 1) | pit_get_gate(pit, 2) |
   2.494 -            (out << 5) | refresh_clock << 4);
   2.495 +            (pit_get_out(pit, 2) << 5) | (refresh_clock << 4));
   2.496  }
   2.497  
   2.498  static int handle_speaker_io(ioreq_t *p)
   2.499 @@ -550,18 +544,16 @@ static int handle_speaker_io(ioreq_t *p)
   2.500      struct vcpu *v = current;
   2.501      struct PITState *vpit = &(v->domain->arch.hvm_domain.pl_time.vpit);
   2.502  
   2.503 -    if (p->size != 1 ||
   2.504 -        p->data_is_ptr ||
   2.505 -        p->type != IOREQ_TYPE_PIO){
   2.506 -        printk("HVM_SPEAKER:wrong SPEAKER IO!\n");
   2.507 +    if ( (p->size != 1) || p->data_is_ptr || (p->type != IOREQ_TYPE_PIO) )
   2.508 +    {
   2.509 +        gdprintk(XENLOG_WARNING, "HVM_SPEAKER bad access\n");
   2.510          return 1;
   2.511      }
   2.512  
   2.513 -    if (p->dir == 0) {/* write */
   2.514 +    if ( p->dir == IOREQ_WRITE )
   2.515          speaker_ioport_write(vpit, p->addr, p->data);
   2.516 -    } else if (p->dir == 1) {/* read */
   2.517 +    else
   2.518          p->data = speaker_ioport_read(vpit, p->addr);
   2.519 -    }
   2.520  
   2.521      return 1;
   2.522  }
   2.523 @@ -576,7 +568,7 @@ int pv_pit_handler(int port, int data, i
   2.524          .data = write ? data : 0,
   2.525      };
   2.526  
   2.527 -    if (port == 0x61)
   2.528 +    if ( port == 0x61 )
   2.529          handle_speaker_io(&ioreq);
   2.530      else
   2.531          handle_pit_io(&ioreq);
     3.1 --- a/xen/arch/x86/hvm/rtc.c	Thu Jun 14 18:01:07 2007 +0100
     3.2 +++ b/xen/arch/x86/hvm/rtc.c	Thu Jun 14 18:01:42 2007 +0100
     3.3 @@ -28,8 +28,6 @@
     3.4  #include <asm/hvm/support.h>
     3.5  #include <asm/current.h>
     3.6  
     3.7 -/* #define DEBUG_RTC */
     3.8 -
     3.9  void rtc_periodic_cb(struct vcpu *v, void *opaque)
    3.10  {
    3.11      RTCState *s = opaque;
    3.12 @@ -56,16 +54,16 @@ static void rtc_timer_update(RTCState *s
    3.13      {
    3.14          if ( period_code <= 2 )
    3.15              period_code += 7;
    3.16 -        
    3.17 +
    3.18          period = 1 << (period_code - 1); /* period in 32 Khz cycles */
    3.19          period = DIV_ROUND((period * 1000000000ULL), 32768); /* period in ns */
    3.20 -#ifdef DEBUG_RTC
    3.21 -        printk("HVM_RTC: period = %uns\n", period);
    3.22 -#endif
    3.23 -        create_periodic_time(v, &s->pt, period, RTC_IRQ, 0, rtc_periodic_cb, s);
    3.24 -    } 
    3.25 +        create_periodic_time(v, &s->pt, period, RTC_IRQ,
    3.26 +                             0, rtc_periodic_cb, s);
    3.27 +    }
    3.28      else
    3.29 +    {
    3.30          destroy_periodic_time(&s->pt);
    3.31 +    }
    3.32  }
    3.33  
    3.34  static void rtc_set_time(RTCState *s);
    3.35 @@ -80,14 +78,9 @@ static int rtc_ioport_write(void *opaque
    3.36          return (s->hw.cmos_index < RTC_CMOS_SIZE);
    3.37      }
    3.38  
    3.39 -    if (s->hw.cmos_index >= RTC_CMOS_SIZE)
    3.40 +    if ( s->hw.cmos_index >= RTC_CMOS_SIZE )
    3.41          return 0;
    3.42  
    3.43 -#ifdef DEBUG_RTC
    3.44 -    printk("HVM_RTC: write index=0x%02x val=0x%02x\n",
    3.45 -           s->hw.cmos_index, data);
    3.46 -#endif
    3.47 -
    3.48      switch ( s->hw.cmos_index )
    3.49      {
    3.50      case RTC_SECONDS_ALARM:
    3.51 @@ -182,7 +175,8 @@ static void rtc_copy_date(RTCState *s)
    3.52  {
    3.53      const struct tm *tm = &s->current_tm;
    3.54  
    3.55 -    if (s->time_offset_seconds != s->pt.vcpu->domain->time_offset_seconds) {
    3.56 +    if ( s->time_offset_seconds != s->pt.vcpu->domain->time_offset_seconds )
    3.57 +    {
    3.58          s->current_tm = gmtime(get_localtime(s->pt.vcpu->domain));
    3.59          s->time_offset_seconds = s->pt.vcpu->domain->time_offset_seconds;
    3.60      }
    3.61 @@ -229,33 +223,41 @@ static void rtc_next_second(RTCState *s)
    3.62      struct tm *tm = &s->current_tm;
    3.63      int days_in_month;
    3.64  
    3.65 -    if (s->time_offset_seconds != s->pt.vcpu->domain->time_offset_seconds) {
    3.66 +    if ( s->time_offset_seconds != s->pt.vcpu->domain->time_offset_seconds )
    3.67 +    {
    3.68          s->current_tm = gmtime(get_localtime(s->pt.vcpu->domain));
    3.69          s->time_offset_seconds = s->pt.vcpu->domain->time_offset_seconds;
    3.70      }
    3.71  
    3.72      tm->tm_sec++;
    3.73 -    if ((unsigned)tm->tm_sec >= 60) {
    3.74 +    if ( (unsigned)tm->tm_sec >= 60 )
    3.75 +    {
    3.76          tm->tm_sec = 0;
    3.77          tm->tm_min++;
    3.78 -        if ((unsigned)tm->tm_min >= 60) {
    3.79 +        if ( (unsigned)tm->tm_min >= 60 )
    3.80 +        {
    3.81              tm->tm_min = 0;
    3.82              tm->tm_hour++;
    3.83 -            if ((unsigned)tm->tm_hour >= 24) {
    3.84 +            if ( (unsigned)tm->tm_hour >= 24 )
    3.85 +            {
    3.86                  tm->tm_hour = 0;
    3.87                  /* next day */
    3.88                  tm->tm_wday++;
    3.89 -                if ((unsigned)tm->tm_wday >= 7)
    3.90 +                if ( (unsigned)tm->tm_wday >= 7 )
    3.91                      tm->tm_wday = 0;
    3.92                  days_in_month = get_days_in_month(tm->tm_mon, 
    3.93                                                    tm->tm_year + 1900);
    3.94                  tm->tm_mday++;
    3.95 -                if (tm->tm_mday < 1) {
    3.96 +                if ( tm->tm_mday < 1 )
    3.97 +                {
    3.98                      tm->tm_mday = 1;
    3.99 -                } else if (tm->tm_mday > days_in_month) {
   3.100 +                }
   3.101 +                else if ( tm->tm_mday > days_in_month )
   3.102 +                {
   3.103                      tm->tm_mday = 1;
   3.104                      tm->tm_mon++;
   3.105 -                    if (tm->tm_mon >= 12) {
   3.106 +                    if ( tm->tm_mon >= 12 )
   3.107 +                    {
   3.108                          tm->tm_mon = 0;
   3.109                          tm->tm_year++;
   3.110                      }
   3.111 @@ -360,11 +362,6 @@ static uint32_t rtc_ioport_read(void *op
   3.112          break;
   3.113      }
   3.114  
   3.115 -#ifdef DEBUG_RTC
   3.116 -    printk("HVM_RTC: read index=0x%02x val=0x%02x\n",
   3.117 -           s->hw.cmos_index, ret);
   3.118 -#endif
   3.119 -
   3.120      return ret;
   3.121  }
   3.122  
   3.123 @@ -375,16 +372,16 @@ static int handle_rtc_io(ioreq_t *p)
   3.124  
   3.125      if ( (p->size != 1) || p->data_is_ptr || (p->type != IOREQ_TYPE_PIO) )
   3.126      {
   3.127 -        printk("HVM_RTC: wrong RTC IO!\n");
   3.128 +        gdprintk(XENLOG_WARNING, "HVM_RTC bas access\n");
   3.129          return 1;
   3.130      }
   3.131      
   3.132 -    if ( p->dir == 0 ) /* write */
   3.133 +    if ( p->dir == IOREQ_WRITE )
   3.134      {
   3.135          if ( rtc_ioport_write(vrtc, p->addr, p->data & 0xFF) )
   3.136              return 1;
   3.137      }
   3.138 -    else if ( (p->dir == 1) && (vrtc->hw.cmos_index < RTC_CMOS_SIZE) ) /* read */
   3.139 +    else if ( vrtc->hw.cmos_index < RTC_CMOS_SIZE )
   3.140      {
   3.141          p->data = rtc_ioport_read(vrtc, p->addr);
   3.142          return 1;
   3.143 @@ -393,15 +390,12 @@ static int handle_rtc_io(ioreq_t *p)
   3.144      return 0;
   3.145  }
   3.146  
   3.147 -/* Move the RTC timers on to this vcpu's current cpu */
   3.148  void rtc_migrate_timers(struct vcpu *v)
   3.149  {
   3.150      RTCState *s = &v->domain->arch.hvm_domain.pl_time.vrtc;
   3.151  
   3.152 -    if ( s->pt.vcpu == v )
   3.153 +    if ( v->vcpu_id == 0 )
   3.154      {
   3.155 -        if ( s->pt.enabled )
   3.156 -            migrate_timer(&s->pt.timer, v->processor);
   3.157          migrate_timer(&s->second_timer, v->processor);
   3.158          migrate_timer(&s->second_timer2, v->processor);
   3.159      }
   3.160 @@ -452,7 +446,6 @@ void rtc_init(struct vcpu *v, int base)
   3.161      s->current_tm = gmtime(get_localtime(v->domain));
   3.162      rtc_copy_date(s);
   3.163  
   3.164 -    init_timer(&s->pt.timer, pt_timer_fn, &s->pt, v->processor);
   3.165      init_timer(&s->second_timer, rtc_update_second, s, v->processor);
   3.166      init_timer(&s->second_timer2, rtc_update_second2, s, v->processor);
   3.167  
   3.168 @@ -466,7 +459,7 @@ void rtc_deinit(struct domain *d)
   3.169  {
   3.170      RTCState *s = &d->arch.hvm_domain.pl_time.vrtc;
   3.171  
   3.172 -    kill_timer(&s->pt.timer);
   3.173 +    destroy_periodic_time(&s->pt);
   3.174      kill_timer(&s->second_timer);
   3.175      kill_timer(&s->second_timer2);
   3.176  }
     4.1 --- a/xen/arch/x86/hvm/vlapic.c	Thu Jun 14 18:01:07 2007 +0100
     4.2 +++ b/xen/arch/x86/hvm/vlapic.c	Thu Jun 14 18:01:42 2007 +0100
     4.3 @@ -944,8 +944,6 @@ int vlapic_init(struct vcpu *v)
     4.4      if ( v->vcpu_id == 0 )
     4.5          vlapic->hw.apic_base_msr |= MSR_IA32_APICBASE_BSP;
     4.6  
     4.7 -    init_timer(&vlapic->pt.timer, pt_timer_fn, &vlapic->pt, v->processor);
     4.8 -
     4.9      return 0;
    4.10  }
    4.11  
    4.12 @@ -953,7 +951,7 @@ void vlapic_destroy(struct vcpu *v)
    4.13  {
    4.14      struct vlapic *vlapic = vcpu_vlapic(v);
    4.15  
    4.16 -    kill_timer(&vlapic->pt.timer);
    4.17 +    destroy_periodic_time(&vlapic->pt);
    4.18      unmap_domain_page_global(vlapic->regs);
    4.19      free_domheap_page(vlapic->regs_page);
    4.20  }
     5.1 --- a/xen/arch/x86/hvm/vpt.c	Thu Jun 14 18:01:07 2007 +0100
     5.2 +++ b/xen/arch/x86/hvm/vpt.c	Thu Jun 14 18:01:42 2007 +0100
     5.3 @@ -22,31 +22,31 @@
     5.4  #include <asm/hvm/vpt.h>
     5.5  #include <asm/event.h>
     5.6  
     5.7 -static __inline__ void missed_ticks(struct periodic_time *pt)
     5.8 +static void missed_ticks(struct periodic_time *pt)
     5.9  {
    5.10      s_time_t missed_ticks;
    5.11  
    5.12      missed_ticks = NOW() - pt->scheduled;
    5.13 -    if ( missed_ticks > 0 ) 
    5.14 +    if ( missed_ticks <= 0 )
    5.15 +        return;
    5.16 +
    5.17 +    missed_ticks = missed_ticks / (s_time_t) pt->period + 1;
    5.18 +    if ( missed_ticks > 1000 )
    5.19      {
    5.20 -        missed_ticks = missed_ticks / (s_time_t) pt->period + 1;
    5.21 -        if ( missed_ticks > 1000 )
    5.22 -        {
    5.23 -            /* TODO: Adjust guest time together */
    5.24 -            pt->pending_intr_nr++;
    5.25 -        }
    5.26 -        else
    5.27 -        {
    5.28 -            pt->pending_intr_nr += missed_ticks;
    5.29 -        }
    5.30 -        pt->scheduled += missed_ticks * pt->period;
    5.31 +        /* TODO: Adjust guest time together */
    5.32 +        pt->pending_intr_nr++;
    5.33      }
    5.34 +    else
    5.35 +    {
    5.36 +        pt->pending_intr_nr += missed_ticks;
    5.37 +    }
    5.38 +
    5.39 +    pt->scheduled += missed_ticks * pt->period;
    5.40  }
    5.41  
    5.42  void pt_freeze_time(struct vcpu *v)
    5.43  {
    5.44      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
    5.45 -    struct list_head *list;
    5.46      struct periodic_time *pt;
    5.47  
    5.48      if ( test_bit(_VPF_blocked, &v->pause_flags) )
    5.49 @@ -54,17 +54,13 @@ void pt_freeze_time(struct vcpu *v)
    5.50  
    5.51      v->arch.hvm_vcpu.guest_time = hvm_get_guest_time(v);
    5.52  
    5.53 -    list_for_each( list, head )
    5.54 -    {
    5.55 -        pt = list_entry(list, struct periodic_time, list);
    5.56 +    list_for_each_entry ( pt, head, list )
    5.57          stop_timer(&pt->timer);
    5.58 -    }
    5.59  }
    5.60  
    5.61  void pt_thaw_time(struct vcpu *v)
    5.62  {
    5.63      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
    5.64 -    struct list_head *list;
    5.65      struct periodic_time *pt;
    5.66  
    5.67      if ( v->arch.hvm_vcpu.guest_time )
    5.68 @@ -72,17 +68,15 @@ void pt_thaw_time(struct vcpu *v)
    5.69          hvm_set_guest_time(v, v->arch.hvm_vcpu.guest_time);
    5.70          v->arch.hvm_vcpu.guest_time = 0;
    5.71  
    5.72 -        list_for_each( list, head )
    5.73 +        list_for_each_entry ( pt, head, list )
    5.74          {
    5.75 -            pt = list_entry(list, struct periodic_time, list);
    5.76              missed_ticks(pt);
    5.77              set_timer(&pt->timer, pt->scheduled);
    5.78          }
    5.79      }
    5.80  }
    5.81  
    5.82 -/* Hook function for the platform periodic time */
    5.83 -void pt_timer_fn(void *data)
    5.84 +static void pt_timer_fn(void *data)
    5.85  {
    5.86      struct periodic_time *pt = data;
    5.87  
    5.88 @@ -100,14 +94,12 @@ void pt_timer_fn(void *data)
    5.89  void pt_update_irq(struct vcpu *v)
    5.90  {
    5.91      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
    5.92 -    struct list_head *list;
    5.93      struct periodic_time *pt;
    5.94      uint64_t max_lag = -1ULL;
    5.95      int irq = -1;
    5.96  
    5.97 -    list_for_each( list, head )
    5.98 +    list_for_each_entry ( pt, head, list )
    5.99      {
   5.100 -        pt = list_entry(list, struct periodic_time, list);
   5.101          if ( !is_isa_irq_masked(v, pt->irq) && pt->pending_intr_nr &&
   5.102               ((pt->last_plt_gtime + pt->period_cycles) < max_lag) )
   5.103          {
   5.104 @@ -130,14 +122,12 @@ void pt_update_irq(struct vcpu *v)
   5.105  struct periodic_time *is_pt_irq(struct vcpu *v, int vector, int type)
   5.106  {
   5.107      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
   5.108 -    struct list_head *list;
   5.109      struct periodic_time *pt;
   5.110      struct RTCState *rtc = &v->domain->arch.hvm_domain.pl_time.vrtc;
   5.111      int vec;
   5.112  
   5.113 -    list_for_each( list, head )
   5.114 +    list_for_each_entry ( pt, head, list )
   5.115      {
   5.116 -        pt = list_entry(list, struct periodic_time, list);
   5.117          if ( !pt->pending_intr_nr )
   5.118              continue;
   5.119  
   5.120 @@ -177,17 +167,14 @@ void pt_intr_post(struct vcpu *v, int ve
   5.121          pt->cb(pt->vcpu, pt->priv);
   5.122  }
   5.123  
   5.124 -/* If pt is enabled, discard pending intr */
   5.125  void pt_reset(struct vcpu *v)
   5.126  {
   5.127      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
   5.128 -    struct list_head *list;
   5.129      struct periodic_time *pt;
   5.130  
   5.131 -    list_for_each( list, head )
   5.132 +    list_for_each_entry ( pt, head, list )
   5.133      {
   5.134 -	pt = list_entry(list, struct periodic_time, list);
   5.135 -	if ( pt->enabled )
   5.136 +        if ( pt->enabled )
   5.137          {
   5.138              pt->pending_intr_nr = 0;
   5.139              pt->last_plt_gtime = hvm_get_guest_time(pt->vcpu);
   5.140 @@ -197,11 +184,25 @@ void pt_reset(struct vcpu *v)
   5.141      }
   5.142  }
   5.143  
   5.144 -void create_periodic_time(struct vcpu *v, struct periodic_time *pt, uint64_t period,
   5.145 -                          uint8_t irq, char one_shot, time_cb *cb, void *data)
   5.146 +void pt_migrate(struct vcpu *v)
   5.147 +{
   5.148 +    struct list_head *head = &v->arch.hvm_vcpu.tm_list;
   5.149 +    struct periodic_time *pt;
   5.150 +
   5.151 +    list_for_each_entry ( pt, head, list )
   5.152 +    {
   5.153 +        if ( pt->enabled )
   5.154 +            migrate_timer(&pt->timer, v->processor);
   5.155 +    }
   5.156 +}
   5.157 +
   5.158 +void create_periodic_time(
   5.159 +    struct vcpu *v, struct periodic_time *pt, uint64_t period,
   5.160 +    uint8_t irq, char one_shot, time_cb *cb, void *data)
   5.161  {
   5.162      destroy_periodic_time(pt);
   5.163  
   5.164 +    init_timer(&pt->timer, pt_timer_fn, pt, v->processor);
   5.165      pt->enabled = 1;
   5.166      if ( period < 900000 ) /* < 0.9 ms */
   5.167      {
   5.168 @@ -226,11 +227,11 @@ void create_periodic_time(struct vcpu *v
   5.169  
   5.170  void destroy_periodic_time(struct periodic_time *pt)
   5.171  {
   5.172 -    if ( pt->enabled )
   5.173 -    {
   5.174 -        pt->enabled = 0;
   5.175 -        pt->pending_intr_nr = 0;
   5.176 -        list_del(&pt->list);
   5.177 -        stop_timer(&pt->timer);
   5.178 -    }
   5.179 +    if ( !pt->enabled )
   5.180 +        return;
   5.181 +
   5.182 +    pt->enabled = 0;
   5.183 +    pt->pending_intr_nr = 0;
   5.184 +    list_del(&pt->list);
   5.185 +    kill_timer(&pt->timer);
   5.186  }
     6.1 --- a/xen/include/asm-x86/hvm/vpt.h	Thu Jun 14 18:01:07 2007 +0100
     6.2 +++ b/xen/include/asm-x86/hvm/vpt.h	Thu Jun 14 18:01:42 2007 +0100
     6.3 @@ -115,19 +115,19 @@ struct pl_time {    /* platform time */
     6.4  
     6.5  void pt_freeze_time(struct vcpu *v);
     6.6  void pt_thaw_time(struct vcpu *v);
     6.7 -void pt_timer_fn(void *data);
     6.8  void pt_update_irq(struct vcpu *v);
     6.9  struct periodic_time *is_pt_irq(struct vcpu *v, int vector, int type);
    6.10  void pt_intr_post(struct vcpu *v, int vector, int type);
    6.11  void pt_reset(struct vcpu *v);
    6.12 -void create_periodic_time(struct vcpu *v, struct periodic_time *pt, uint64_t period,
    6.13 -                          uint8_t irq, char one_shot, time_cb *cb, void *data);
    6.14 +void pt_migrate(struct vcpu *v);
    6.15 +void create_periodic_time(
    6.16 +    struct vcpu *v, struct periodic_time *pt, uint64_t period,
    6.17 +    uint8_t irq, char one_shot, time_cb *cb, void *data);
    6.18  void destroy_periodic_time(struct periodic_time *pt);
    6.19  
    6.20  int pv_pit_handler(int port, int data, int write);
    6.21  void pit_init(struct vcpu *v, unsigned long cpu_khz);
    6.22  void pit_stop_channel0_irq(PITState * pit);
    6.23 -void pit_migrate_timers(struct vcpu *v);
    6.24  void pit_deinit(struct domain *d);
    6.25  void rtc_init(struct vcpu *v, int base);
    6.26  void rtc_migrate_timers(struct vcpu *v);