2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_dev_mgr.h>
23 #include <palacios/vmm_time.h>
24 #include <palacios/vmm_util.h>
25 #include <palacios/vmm_intr.h>
26 #include <palacios/vmm_config.h>
27 #include <palacios/vmm_io.h>
30 #ifndef V3_CONFIG_DEBUG_PIT
32 #define PrintDebug(fmt, args...)
38 #define OSC_HZ 1193182
41 /* The 8254 has three counters and one control port */
42 #define CHANNEL0_PORT 0x40
43 #define CHANNEL1_PORT 0x41
44 #define CHANNEL2_PORT 0x42
45 #define COMMAND_PORT 0x43
46 #define SPEAKER_PORT 0x61
49 #define PIT_INTR_NUM 0
50 #define PIT_SPEAKER_GATE 0x01
52 /* The order of these typedefs is important because the numerical values correspond to the
53 * values coming from the io ports
55 typedef enum {NOT_RUNNING, PENDING, RUNNING} channel_run_state_t;
56 typedef enum {NOT_WAITING, WAITING_LOBYTE, WAITING_HIBYTE} channel_access_state_t;
57 typedef enum {LATCH_COUNT = 0, LOBYTE_ONLY = 1, HIBYTE_ONLY = 2, LOBYTE_HIBYTE = 3} channel_access_mode_t;
58 typedef enum {IRQ_ON_TERM_CNT = 0, ONE_SHOT = 1, RATE_GEN = 2, SQR_WAVE = 3, SW_STROBE = 4, HW_STROBE = 5} channel_op_mode_t;
62 channel_access_mode_t access_mode;
63 channel_access_state_t access_state;
64 channel_run_state_t run_state;
66 channel_op_mode_t op_mode;
69 // Time til interrupt trigger
72 ushort_t reload_value;
74 ushort_t latched_value;
76 enum {NOTLATCHED, LATCHED} latch_state;
78 enum {LSB, MSB} read_state;
80 uint_t output_pin : 1;
81 uint_t gate_input_pin : 1;
90 struct v3_timer * timer;
92 struct v3_vm_info * vm;
101 struct pit_cmd_word {
104 uint_t access_mode : 2;
108 struct pit_rdb_cmd_word {
109 uint_t rsvd : 1; // SBZ
113 uint_t latch_status : 1;
114 uint_t latch_count : 1;
115 uint_t readback_cmd : 2; // Must Be 0x3
118 struct pit_rdb_status_word {
121 uint_t access_mode : 2;
122 uint_t null_count : 1;
123 uint_t pin_state : 1;
129 * This should call out to handle_SQR_WAVE_tics, etc...
131 // Returns true if the the output signal changed state
132 static int handle_crystal_tics(struct pit * pit, struct channel * ch, uint_t oscillations) {
133 uint_t channel_cycles = 0;
134 uint_t output_changed = 0;
136 // PrintDebug(info->vm_info, info, "8254 PIT (channel %d): %d crystal tics\n",
137 // ch - pit->ch0, oscillations);
138 if (ch->run_state == PENDING) {
140 ch->counter = ch->reload_value;
142 if (ch->op_mode == SQR_WAVE) {
143 ch->counter -= ch->counter % 2;
146 ch->run_state = RUNNING;
147 } else if (ch->run_state != RUNNING) {
148 return output_changed;
152 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Channel Run State = %d, counter=", ch->run_state);
153 PrintTraceLL(ch->counter);
154 PrintDebug(VM_NONE, VCORE_NONE, "\n");
156 if (ch->op_mode == SQR_WAVE) {
160 if (ch->counter > oscillations) {
161 ch->counter -= oscillations;
162 //PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Counter at %u after %u oscillations.\n",
163 // (unsigned int)ch->counter, oscillations);
164 return output_changed;
166 ushort_t reload_val = ch->reload_value;
168 if ((ch->op_mode == SW_STROBE) || (ch->op_mode == IRQ_ON_TERM_CNT)) {
172 oscillations -= ch->counter;
176 if (ch->op_mode == SQR_WAVE) {
177 reload_val -= reload_val % 2;
180 if (reload_val == 0) {
181 // This means the value is being set to 0x10000
182 // but due to the tick after the reload, it wraps
187 channel_cycles += oscillations / reload_val;
188 oscillations = oscillations % reload_val;
190 ch->counter = reload_val - oscillations;
191 // PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Counter reset to %u.\n",
192 // (unsigned int)ch->counter);
196 //PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Channel %ld (mode = %u) Cycles: %d\n",
197 //(ch - &pit->ch_0), ch->op_mode, channel_cycles);
199 switch (ch->op_mode) {
200 case IRQ_ON_TERM_CNT:
201 if (channel_cycles > 0) {
202 if (ch->output_pin == 0) {
206 // PrintDebug(VM_NONE, VCORE_NONE, "8254: Output not changed in TERM_CNT mode.\n");
211 if (channel_cycles > 0) {
212 if ((ch->output_pin == 0)) {
216 // PrintDebug(VM_NONE, VCORE_NONE, "8254: Output not changed in ONE_SHOT mode.\n");
221 // See the data sheet: we ignore the output pin cycle...
222 if (channel_cycles > 0) {
227 ch->output_pin = (ch->output_pin + 1) % 2;
229 if (ch->output_pin == 1) {
237 if (channel_cycles > 0) {
238 if (ch->output_pin == 1) {
245 PrintError(VM_NONE, VCORE_NONE, "Hardware strobe not implemented\n");
252 return output_changed;
256 #include <palacios/vm_guest.h>
258 static void pit_update_timer(struct guest_info * info, ullong_t cpu_cycles, ullong_t cpu_freq, void * private_data) {
259 struct pit * state = (struct pit *)private_data;
260 // ullong_t tmp_ctr = state->pit_counter;
262 uint_t oscillations = 0;
266 PrintDebug(info->vm_info, info, "updating cpu_cycles=");
267 PrintTraceLL(cpu_cycles);
268 PrintDebug(info->vm_info, info, "\n");
270 PrintDebug(info->vm_info, info, "pit_counter=");
271 PrintTraceLL(state->pit_counter);
272 PrintDebug(info->vm_info, info, "\n");
274 PrintDebug(info->vm_info, info, "pit_reload=");
275 PrintTraceLL(state->pit_reload);
276 PrintDebug(info->vm_info, info, "\n");
279 if (state->pit_counter > cpu_cycles) {
281 state->pit_counter -= cpu_cycles;
283 ushort_t reload_val = state->pit_reload;
284 // Take off the first part
285 cpu_cycles -= state->pit_counter;
286 state->pit_counter = 0;
289 if (cpu_cycles > state->pit_reload) {
290 // how many full oscillations
292 //PrintError(info->vm_info, info, "cpu_cycles = %p, reload = %p...\n",
293 // (void *)(addr_t)cpu_cycles,
294 // (void *)(addr_t)state->pit_reload);
296 // How do we check for a one shot....
297 if (state->pit_reload == 0) {
301 tmp_cycles = cpu_cycles;
305 cpu_cycles = tmp_cycles % state->pit_reload;
306 tmp_cycles = tmp_cycles / state->pit_reload;
308 cpu_cycles = do_divll(tmp_cycles, state->pit_reload);
311 oscillations += tmp_cycles;
314 // update counter with remainder (mod reload)
315 state->pit_counter = state->pit_reload - cpu_cycles;
318 // PrintDebug(info->vm_info, info, "8254 PIT: Handling %d crystal tics\n", oscillations);
320 if (handle_crystal_tics(state, &(state->ch_0), oscillations) == 1) {
322 PrintDebug(info->vm_info, info, "8254 PIT: Injecting Timer interrupt to guest (run_state = %d)\n",
323 state->ch_0.run_state);
324 v3_raise_irq(info->vm_info, 0);
327 //handle_crystal_tics(state, &(state->ch_1), oscillations);
328 handle_crystal_tics(state, &(state->ch_2), oscillations);
335 /* This should call out to handle_SQR_WAVE_write, etc...
337 static int handle_channel_write(struct channel * ch, char val) {
339 switch (ch->access_state) {
342 ushort_t tmp_val = ((ushort_t)val) << 8;
343 ch->reload_value &= 0x00ff;
344 ch->reload_value |= tmp_val;
347 if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)){
348 ch->run_state = PENDING;
351 if (ch->access_mode == LOBYTE_HIBYTE) {
352 ch->access_state = WAITING_LOBYTE;
355 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: updated channel counter: %d\n", ch->reload_value);
356 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Channel Run State=%d\n", ch->run_state);
360 ch->reload_value &= 0xff00;
361 ch->reload_value |= val;
363 if (ch->access_mode == LOBYTE_HIBYTE) {
364 ch->access_state = WAITING_HIBYTE;
365 } else if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)) {
366 ch->run_state = PENDING;
369 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: updated channel counter: %d\n", ch->reload_value);
370 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Channel Run State=%d\n", ch->run_state);
373 PrintError(VM_NONE, VCORE_NONE, "Invalid Access state\n");
378 switch (ch->op_mode) {
379 case IRQ_ON_TERM_CNT:
395 PrintError(VM_NONE, VCORE_NONE, "Invalid OP_MODE: %d\n", ch->op_mode);
405 static int handle_channel_read(struct channel * ch, char * val) {
409 if (ch->latch_state == NOTLATCHED) {
410 myval = &(ch->counter);
412 myval = &(ch->latched_value);
415 if (ch->read_state == LSB) {
416 *val = ((char*)myval)[0]; // little endian
417 ch->read_state = MSB;
419 *val = ((char*)myval)[1];
420 ch->read_state = LSB;
421 if (ch->latch_state == LATCHED) {
422 ch->latch_state = NOTLATCHED;
430 static int handle_speaker_read(uint8_t *speaker, struct channel * ch, char * val) {
433 if ((*speaker & PIT_SPEAKER_GATE)) {
434 *val |= (ch->output_pin << 5);
440 static int handle_speaker_write(uint8_t *speaker, struct channel * ch, char val) {
441 *speaker = (val & ~0x20);
445 static int handle_channel_cmd(struct channel * ch, struct pit_cmd_word cmd) {
447 if (cmd.op_mode != ch->op_mode) {
448 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Changing channel from op mode %d to op mode %d.\n",
449 ch->op_mode, cmd.op_mode);
452 if (cmd.access_mode != 0) {
453 ch->op_mode = cmd.op_mode;
456 if (cmd.access_mode != ch->access_mode) {
457 PrintDebug(VM_NONE, VCORE_NONE, "8254 PIT: Changing channel from access mode %d to access mode %d.\n",
458 ch->access_mode, cmd.access_mode);
460 ch->access_mode = cmd.access_mode;
462 switch (cmd.access_mode) {
464 if (ch->latch_state == NOTLATCHED) {
465 ch->latched_value = ch->counter;
466 ch->latch_state = LATCHED;
470 ch->access_state = WAITING_HIBYTE;
474 ch->access_state = WAITING_LOBYTE;
479 switch (cmd.op_mode) {
480 case IRQ_ON_TERM_CNT:
496 PrintError(VM_NONE, VCORE_NONE, "Invalid OP_MODE: %d\n", cmd.op_mode);
507 static int pit_read_channel(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
508 struct pit * state = (struct pit *)priv_data;
509 char * val = (char *)dst;
512 PrintError(core->vm_info, core, "8254 PIT: Invalid Read Write length \n");
516 PrintDebug(core->vm_info, core, "8254 PIT: Read of PIT Channel %d\n", port - CHANNEL0_PORT);
520 if (handle_channel_read(&(state->ch_0), val) == -1) {
521 PrintError(core->vm_info, core, "CHANNEL0 read error\n");
526 if (handle_channel_read(&(state->ch_1), val) == -1) {
527 PrintError(core->vm_info, core, "CHANNEL1 read error\n");
532 if (handle_channel_read(&(state->ch_2), val) == -1) {
533 PrintError(core->vm_info, core, "CHANNEL2 read error\n");
538 if (handle_speaker_read(&state->speaker, &(state->ch_2), val) == -1) {
539 PrintError(core->vm_info, core, "SPEAKER read error\n");
544 PrintError(core->vm_info, core, "8254 PIT: Read from invalid port (%d)\n", port);
553 static int pit_write_channel(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
554 struct pit * state = (struct pit *)priv_data;
555 char val = *(char *)src;
558 PrintError(core->vm_info, core, "8254 PIT: Invalid Write Length\n");
562 PrintDebug(core->vm_info, core, "8254 PIT: Write to PIT Channel %d (%x)\n", port - CHANNEL0_PORT, *(char*)src);
567 if (handle_channel_write(&(state->ch_0), val) == -1) {
568 PrintError(core->vm_info, core, "CHANNEL0 write error\n");
573 if (handle_channel_write(&(state->ch_1), val) == -1) {
574 PrintError(core->vm_info, core, "CHANNEL1 write error\n");
579 if (handle_channel_write(&(state->ch_2), val) == -1) {
580 PrintError(core->vm_info, core, "CHANNEL2 write error\n");
585 if (handle_speaker_write(&state->speaker, &(state->ch_2), val) == -1) {
586 PrintError(core->vm_info, core, "SPEAKER write error\n");
591 PrintError(core->vm_info, core, "8254 PIT: Write to invalid port (%d)\n", port);
601 static int pit_write_command(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
602 struct pit * state = (struct pit *)priv_data;
603 struct pit_cmd_word * cmd = (struct pit_cmd_word *)src;
605 PrintDebug(core->vm_info, core, "8254 PIT: Write to PIT Command port\n");
606 PrintDebug(core->vm_info, core, "8254 PIT: Writing to channel %d (access_mode = %d, op_mode = %d)\n", cmd->channel, cmd->access_mode, cmd->op_mode);
608 PrintError(core->vm_info, core, "8254 PIT: Write of Invalid length to command port\n");
612 switch (cmd->channel) {
614 if (handle_channel_cmd(&(state->ch_0), *cmd) == -1) {
615 PrintError(core->vm_info, core, "CHANNEL0 command error\n");
620 if (handle_channel_cmd(&(state->ch_1), *cmd) == -1) {
621 PrintError(core->vm_info, core, "CHANNEL1 command error\n");
626 if (handle_channel_cmd(&(state->ch_2), *cmd) == -1) {
627 PrintError(core->vm_info, core, "CHANNEL2 command error\n");
633 PrintError(core->vm_info, core, "Read back command not implemented\n");
647 static struct v3_timer_ops timer_ops = {
648 .update_timer = pit_update_timer,
652 static void init_channel(struct channel * ch) {
653 ch->run_state = NOT_RUNNING;
654 ch->access_state = NOT_WAITING;
659 ch->reload_value = 0;
661 ch->gate_input_pin = 0;
663 ch->latched_value = 0;
664 ch->latch_state = NOTLATCHED;
665 ch->read_state = LSB;
673 static int pit_free(void * private_data) {
674 struct pit * state = (struct pit *)private_data;
675 struct guest_info * info = &(state->vm->cores[0]);
679 v3_remove_timer(info, state->timer);
686 #ifdef V3_CONFIG_CHECKPOINT
688 #include <palacios/vmm_sprintf.h>
691 #define MAKE_KEY(x) snprintf(key,KEY_MAX,"PIT_CH%d_%s",i,x)
693 static int pit_save(struct v3_chkpt_ctx * ctx, void * private_data) {
694 struct pit * pit_state = (struct pit *)private_data;
698 V3_CHKPT_SAVE(ctx, "PIT_COUNTER", pit_state->pit_counter,savefailout);
699 V3_CHKPT_SAVE(ctx, "PIT_RELOAD", pit_state->pit_reload,savefailout);
706 c=&(pit_state->ch_0);
708 c=&(pit_state->ch_1);
710 c=&(pit_state->ch_2);
713 MAKE_KEY("ACCESS_MODE");
714 V3_CHKPT_SAVE(ctx, key, c->access_mode, savefailout);
715 MAKE_KEY("ACCESS_STATE");
716 V3_CHKPT_SAVE(ctx, key, c->access_state, savefailout);
717 MAKE_KEY("RUN_STATE");
718 V3_CHKPT_SAVE(ctx, key, c->run_state, savefailout);
720 V3_CHKPT_SAVE(ctx, key, c->op_mode, savefailout);
722 V3_CHKPT_SAVE(ctx, key, c->counter, savefailout);
723 MAKE_KEY("RELOAD_VALUE");
724 V3_CHKPT_SAVE(ctx, key, c->reload_value, savefailout);
725 MAKE_KEY("LATCH_STATE");
726 V3_CHKPT_SAVE(ctx, key, c->latch_state, savefailout);
727 MAKE_KEY("READ_STATE");
728 V3_CHKPT_SAVE(ctx, key, c->read_state, savefailout);
730 pins = (c->output_pin) | (c->gate_input_pin << 1);
732 V3_CHKPT_SAVE(ctx, key, pins, savefailout);
735 V3_CHKPT_SAVE(ctx, "PIT_SPEAKER", pit_state->speaker,savefailout);
740 PrintError(VM_NONE, VCORE_NONE, "Failed to save pit\n");
744 static int pit_load(struct v3_chkpt_ctx * ctx, void * private_data) {
745 struct pit * pit_state = (struct pit *)private_data;
749 V3_CHKPT_LOAD(ctx, "PIT_COUNTER", pit_state->pit_counter,loadfailout);
750 V3_CHKPT_LOAD(ctx, "PIT_RELOAD", pit_state->pit_reload,loadfailout);
757 c=&(pit_state->ch_0);
759 c=&(pit_state->ch_1);
761 c=&(pit_state->ch_2);
764 MAKE_KEY("ACCESS_MODE");
765 V3_CHKPT_LOAD(ctx, key, c->access_mode, loadfailout);
766 MAKE_KEY("ACCESS_STATE");
767 V3_CHKPT_LOAD(ctx, key, c->access_state, loadfailout);
768 MAKE_KEY("RUN_STATE");
769 V3_CHKPT_LOAD(ctx, key, c->run_state, loadfailout);
771 V3_CHKPT_LOAD(ctx, key, c->op_mode, loadfailout);
773 V3_CHKPT_LOAD(ctx, key, c->counter, loadfailout);
774 MAKE_KEY("RELOAD_VALUE");
775 V3_CHKPT_LOAD(ctx, key, c->reload_value, loadfailout);
776 MAKE_KEY("LATCH_STATE");
777 V3_CHKPT_LOAD(ctx, key, c->latch_state, loadfailout);
778 MAKE_KEY("READ_STATE");
779 V3_CHKPT_LOAD(ctx, key, c->read_state, loadfailout);
781 pins = (c->output_pin) | (c->gate_input_pin << 1);
783 V3_CHKPT_LOAD(ctx, key, pins, loadfailout);
786 V3_CHKPT_LOAD(ctx, "PIT_SPEAKER", pit_state->speaker,loadfailout);
791 PrintError(VM_NONE, VCORE_NONE, "Failed to load pit\n");
796 static struct v3_device_ops dev_ops = {
797 .free = (int (*)(void *))pit_free,
798 #ifdef V3_CONFIG_CHECKPOINT
804 #include <palacios/vm_guest.h>
806 static int pit_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
807 struct pit * pit_state = NULL;
808 struct vm_device * dev = NULL;
809 char * dev_id = v3_cfg_val(cfg, "ID");
812 // PIT is only usable in non-multicore environments
813 // just hardcode the core context
814 struct guest_info * info = &(vm->cores[0]);
816 uint_t cpu_khz = info->time_state.guest_cpu_freq;
817 ullong_t reload_val = (ullong_t)cpu_khz * 1000;
819 pit_state = (struct pit *)V3_Malloc(sizeof(struct pit));
822 PrintError(info->vm_info, info, "Cannot allocate in init\n");
826 pit_state->speaker = 0;
829 dev = v3_add_device(vm, dev_id, &dev_ops, pit_state);
832 PrintError(info->vm_info, info, "Could not attach device %s\n", dev_id);
837 ret |= v3_dev_hook_io(dev, CHANNEL0_PORT, &pit_read_channel, &pit_write_channel);
838 ret |= v3_dev_hook_io(dev, CHANNEL1_PORT, &pit_read_channel, &pit_write_channel);
839 ret |= v3_dev_hook_io(dev, CHANNEL2_PORT, &pit_read_channel, &pit_write_channel);
840 ret |= v3_dev_hook_io(dev, COMMAND_PORT, NULL, &pit_write_command);
841 ret |= v3_dev_hook_io(dev, SPEAKER_PORT, &pit_read_channel, &pit_write_channel);
844 PrintError(info->vm_info, info, "8254 PIT: Failed to hook IO ports\n");
845 v3_remove_device(dev);
849 #ifdef V3_CONFIG_DEBUG_PIT
850 PrintDebug(info->vm_info, info, "8254 PIT: OSC_HZ=%d, reload_val=", OSC_HZ);
851 //PrintTrace(reload_val);
852 PrintDebug(info->vm_info, info, "\n");
857 pit_state->timer = v3_add_timer(info, &timer_ops, pit_state);
859 if (pit_state->timer == NULL) {
860 v3_remove_device(dev);
864 // Get cpu frequency and calculate the global pit oscilattor counter/cycle
866 do_divll(reload_val, OSC_HZ);
867 pit_state->pit_counter = reload_val;
868 pit_state->pit_reload = reload_val;
871 init_channel(&(pit_state->ch_0));
872 init_channel(&(pit_state->ch_1));
873 init_channel(&(pit_state->ch_2));
875 #ifdef V3_CONFIG_DEBUG_PIT
876 PrintDebug(info->vm_info, info, "8254 PIT: CPU MHZ=%d -- pit count=", cpu_khz / 1000);
877 //PrintTraceLL(pit_state->pit_counter);
878 PrintDebug(info->vm_info, info, "\n");
885 device_register("8254_PIT", pit_init);