Skip to contentSkip to conversation

PIT acceleration enhancement

PATCH In review Edwin Zhai 2 participants 2 messages Feb 22, 2006

lore
Share & tools
lore

Edwin Zhai

author
#0 lore raw

PATCH 1/1 PIT acceleration enhancement

PIT acceleration enhancement
add a new flag in HV to control whether re-arm the actimer and handle guest PIT io, by this we can
* stop the actimer and io interception if guest change previous periodic mode to other mode.
* safely handle reinitialization of periodic mode from guest

tools/ioemu/hw/i8254.c +34 -29
diff -r 13e9fdaeed27 tools/ioemu/hw/i8254.c
@@ -50,19 +50,18 @@ typedef struct PITChannelState {
     int64_t next_transition_time;
     QEMUTimer *irq_timer;
     int irq;
-    int hvm_channel; /* Is this accelerated by HVM ? */
 } PITChannelState;
 
 struct PITState {
     PITChannelState channels[3];
+    /* currently operate which channel for hvm use */
+    int hvm_channel;
 };
 
 static PITState pit_state;
 
 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
 
-/* currently operate which channel for hvm use */
-int hvm_channel = -1;
 extern FILE *logfile;
 static int pit_get_count(PITChannelState *s)
 {
@@ -215,56 +214,62 @@ int pit_get_gate(PITState *pit, int chan
     return s->gate;
 }
 
+/* change config for HVM accelerated pit channel */
 void pit_reset_hvm_vectors()
 {
     extern shared_iopage_t *shared_page;
     ioreq_t *req; 
-    int irq, i;
+    int irq;
     PITChannelState *s;
 
     irq = 0;
 
-    for(i = 0; i < 3; i++) {
-        if (pit_state.channels[i].hvm_channel)
-             break;
-    }
-    
-    if (i == 3)
-        return;
-
     /* Assumes just one HVM accelerated channel */
-    hvm_channel = i;
-    s = &pit_state.channels[hvm_channel];
+    s = &pit_state.channels[pit_state.hvm_channel];
     fprintf(logfile,
-    	"HVM_PIT:guest init pit channel %d!\n", hvm_channel);
+    	"HVM_PIT:guest init pit channel %d!\n", pit_state.hvm_channel);
     req = &shared_page->vcpu_iodata[0].vp_ioreq;
 
     req->state = STATE_IORESP_HOOK;
     /*
      * info passed to HV as following
      * -- init count:16 bit, timer vec:8 bit,
-     * PIT channel(0~2):2 bit, rw mode:2 bit
+     * PIT channel(0~2):2 bit, rw mode:2 bit,
+     * PIT mode(0~5):3 bit
      */
     req->u.data = s->count;
     req->u.data |= (irq << 16);
-    req->u.data |= (hvm_channel << 24);
+    req->u.data |= (pit_state.hvm_channel << 24);
     req->u.data |= ((s->rw_mode) << 26);
+    req->u.data |= ((s->mode) << 28);
+
+    /* HVM acceleration only for mode 2 */
+    if (s->mode != 2)
+        pit_state.hvm_channel = -1;
+
     fprintf(logfile, "HVM_PIT:pass info 0x%llx to HV!\n", req->u.data);
 }
 
-static inline void pit_load_count(PITChannelState *s, int val)
+static inline void pit_load_count(PITChannelState *s, int val, int chn)
 {
     if (val == 0)
         val = 0x10000;
     s->count_load_time = qemu_get_clock(vm_clock);
     s->count = val;
 
-    /* guest init this pit channel for periodic mode. we do not update related
-     * timer so the channel never send intr from device model*/
-    if (hvm_channel != -1 && s->mode == 2) {
+    /* first time for periodic mode, mark this channel as HVM accelerated */
+    if (s->mode == 2) {
+        if (pit_state.hvm_channel == -1)
+            pit_state.hvm_channel = chn;
+        else if (pit_state.hvm_channel != chn){
+            fprintf(logfile, "HVM_PIT:should not set another pit channel as mode 2!\n");
+            return;
+        }
+    }
+
+    /* change something for hvm channel, e.g the mode,freq or vec */
+    if (pit_state.hvm_channel == chn)
         pit_reset_hvm_vectors();
-        hvm_channel = -1;
-    }
 
 /*    pit_irq_timer_update(s, s->count_load_time);*/
 }
@@ -323,22 +328,20 @@ static void pit_ioport_write(void *opaqu
         }
     } else {
         s = &pit->channels[addr];
-        s->hvm_channel = 1;
-        hvm_channel = addr;
         switch(s->write_state) {
         default:
         case RW_STATE_LSB:
-            pit_load_count(s, val);
+            pit_load_count(s, val, addr);
             break;
         case RW_STATE_MSB:
-            pit_load_count(s, val << 8);
+            pit_load_count(s, val << 8, addr);
             break;
         case RW_STATE_WORD0:
             s->write_latch = val;
             s->write_state = RW_STATE_WORD1;
             break;
         case RW_STATE_WORD1:
-            pit_load_count(s, s->write_latch | (val << 8));
+            pit_load_count(s, s->write_latch | (val << 8), addr);
             s->write_state = RW_STATE_WORD0;
             break;
         }
@@ -493,11 +496,12 @@ static void pit_reset(void *opaque)
     PITChannelState *s;
     int i;
 
+    pit->hvm_channel = -1;
     for(i = 0;i < 3; i++) {
         s = &pit->channels[i];
         s->mode = 3;
         s->gate = (i != 2);
-        pit_load_count(s, 0);
+        pit_load_count(s, 0, i);
     }
 }
 
@@ -506,6 +510,7 @@ PITState *pit_init(int base, int irq)
     PITState *pit = &pit_state;
     PITChannelState *s;
 
+    pit->hvm_channel = -1;
     s = &pit->channels[0];
     /* the timer 0 is connected to an IRQ */
     s->irq_timer = qemu_new_timer(vm_clock, pit_irq_timer, s);
xen/arch/x86/hvm/hvm.c +3 -0
diff -r 13e9fdaeed27 xen/arch/x86/hvm/hvm.c
@@ -186,6 +186,7 @@ static void hvm_get_info(struct domain *
     unmap_domain_page(p);
 }
 
+extern void pit_init(struct hvm_virpit *vpit, struct vcpu *v);
 void hvm_setup_platform(struct domain* d)
 {
     struct hvm_domain *platform;
@@ -204,6 +205,8 @@ void hvm_setup_platform(struct domain* d
         spin_lock_init(&d->arch.hvm_domain.round_robin_lock);
         hvm_vioapic_init(d);
     }
+
+    pit_init(&platform->vpit, current);
 }
 
 void pic_irq_request(int *interrupt_request, int level)
xen/arch/x86/hvm/intercept.c +33 -15

92 lines not on this page · raw message

xen/include/asm-x86/hvm/vpit.h +1 -0
diff -r 13e9fdaeed27 xen/include/asm-x86/hvm/vpit.h
@@ -43,6 +43,7 @@ struct hvm_virpit {
     s_time_t scheduled;         /* scheduled timer interrupt */
     struct timer pit_timer;     /* periodic timer for mode 2*/
     unsigned int channel;       /* the pit channel, counter 0~2 */
+    unsigned int mode;          /* the pit mode, only acceleration for mode 2 */
     unsigned int pending_intr_nr; /* the couner for pending timer interrupts */
     u32 period;                 /* pit frequency in ns */
     int first_injected;         /* flag to prevent shadow window */
#1 lore raw
Quoted 8 lines · Edwin Zhai

PIT acceleration enhancement
add a new flag in HV to control whether re-arm the actimer and handle guest PIT io, by this we can
* stop the actimer and io interception if guest change previous periodic mode to other mode.
* safely handle reinitialization of periodic mode from guest

Please can you just sync the PIT code with the current qemu tree and move the whole lot down into Xen? The current split between dom0 and xen is unnecessary and gross, and I hate this piecemeal ad hoc migration of PIT functionality from dom0 down to xen. Just move it all in one go.

The only things that should need implementing in qemu-dm are devices whose backend cannot be implemented in Xen (net, disc, keyboard, mouse, ....) -- generally real peripheral devices. Platform devices like PITs and PICs should be done entirely in Xen.

-- Keir