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("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("8254 PIT: Channel Run State = %d, counter=", ch->run_state);
153 PrintTraceLL(ch->counter);
156 if (ch->op_mode == SQR_WAVE) {
160 if (ch->counter > oscillations) {
161 ch->counter -= oscillations;
162 //PrintDebug("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 // TODO: Check this....
181 // Is this correct???
182 if (reload_val == 0) {
186 channel_cycles += oscillations / reload_val;
187 oscillations = oscillations % reload_val;
189 ch->counter = reload_val - oscillations;
190 // PrintDebug("8254 PIT: Counter reset to %u.\n",
191 // (unsigned int)ch->counter);
195 //PrintDebug("8254 PIT: Channel %ld (mode = %u) Cycles: %d\n",
196 //(ch - &pit->ch_0), ch->op_mode, channel_cycles);
198 switch (ch->op_mode) {
199 case IRQ_ON_TERM_CNT:
200 if (channel_cycles > 0) {
201 if (ch->output_pin == 0) {
205 // PrintDebug("8254: Output not changed in TERM_CNT mode.\n");
210 if (channel_cycles > 0) {
211 if ((ch->output_pin == 0)) {
215 // PrintDebug("8254: Output not changed in ONE_SHOT mode.\n");
220 // See the data sheet: we ignore the output pin cycle...
221 if (channel_cycles > 0) {
226 ch->output_pin = (ch->output_pin + 1) % 2;
228 if (ch->output_pin == 1) {
236 if (channel_cycles > 0) {
237 if (ch->output_pin == 1) {
244 PrintError("Hardware strobe not implemented\n");
251 return output_changed;
255 #include <palacios/vm_guest.h>
257 static void pit_update_timer(struct guest_info * info, ullong_t cpu_cycles, ullong_t cpu_freq, void * private_data) {
258 struct pit * state = (struct pit *)private_data;
259 // ullong_t tmp_ctr = state->pit_counter;
261 uint_t oscillations = 0;
265 PrintDebug("updating cpu_cycles=");
266 PrintTraceLL(cpu_cycles);
269 PrintDebug("pit_counter=");
270 PrintTraceLL(state->pit_counter);
273 PrintDebug("pit_reload=");
274 PrintTraceLL(state->pit_reload);
278 if (state->pit_counter > cpu_cycles) {
280 state->pit_counter -= cpu_cycles;
282 ushort_t reload_val = state->pit_reload;
283 // Take off the first part
284 cpu_cycles -= state->pit_counter;
285 state->pit_counter = 0;
288 if (cpu_cycles > state->pit_reload) {
289 // how many full oscillations
291 //PrintError("cpu_cycles = %p, reload = %p...\n",
292 // (void *)(addr_t)cpu_cycles,
293 // (void *)(addr_t)state->pit_reload);
295 // How do we check for a one shot....
296 if (state->pit_reload == 0) {
300 tmp_cycles = cpu_cycles;
304 cpu_cycles = tmp_cycles % state->pit_reload;
305 tmp_cycles = tmp_cycles / state->pit_reload;
307 cpu_cycles = do_divll(tmp_cycles, state->pit_reload);
310 oscillations += tmp_cycles;
313 // update counter with remainder (mod reload)
314 state->pit_counter = state->pit_reload - cpu_cycles;
317 // PrintDebug("8254 PIT: Handling %d crystal tics\n", oscillations);
319 if (handle_crystal_tics(state, &(state->ch_0), oscillations) == 1) {
321 PrintDebug("8254 PIT: Injecting Timer interrupt to guest (run_state = %d)\n",
322 state->ch_0.run_state);
323 v3_raise_irq(info->vm_info, 0);
326 //handle_crystal_tics(state, &(state->ch_1), oscillations);
327 handle_crystal_tics(state, &(state->ch_2), oscillations);
334 /* This should call out to handle_SQR_WAVE_write, etc...
336 static int handle_channel_write(struct channel * ch, char val) {
338 switch (ch->access_state) {
341 ushort_t tmp_val = ((ushort_t)val) << 8;
342 ch->reload_value &= 0x00ff;
343 ch->reload_value |= tmp_val;
346 if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)){
347 ch->run_state = PENDING;
350 if (ch->access_mode == LOBYTE_HIBYTE) {
351 ch->access_state = WAITING_LOBYTE;
354 PrintDebug("8254 PIT: updated channel counter: %d\n", ch->reload_value);
355 PrintDebug("8254 PIT: Channel Run State=%d\n", ch->run_state);
359 ch->reload_value &= 0xff00;
360 ch->reload_value |= val;
362 if (ch->access_mode == LOBYTE_HIBYTE) {
363 ch->access_state = WAITING_HIBYTE;
364 } else if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)) {
365 ch->run_state = PENDING;
368 PrintDebug("8254 PIT: updated channel counter: %d\n", ch->reload_value);
369 PrintDebug("8254 PIT: Channel Run State=%d\n", ch->run_state);
372 PrintError("Invalid Access state\n");
377 switch (ch->op_mode) {
378 case IRQ_ON_TERM_CNT:
394 PrintError("Invalid OP_MODE: %d\n", ch->op_mode);
404 static int handle_channel_read(struct channel * ch, char * val) {
408 if (ch->latch_state == NOTLATCHED) {
409 myval = &(ch->counter);
411 myval = &(ch->latched_value);
414 if (ch->read_state == LSB) {
415 *val = ((char*)myval)[0]; // little endian
416 ch->read_state = MSB;
418 *val = ((char*)myval)[1];
419 ch->read_state = LSB;
420 if (ch->latch_state == LATCHED) {
421 ch->latch_state = NOTLATCHED;
429 static int handle_speaker_read(uint8_t *speaker, struct channel * ch, char * val) {
432 if ((*speaker & PIT_SPEAKER_GATE)) {
433 *val |= (ch->output_pin << 5);
439 static int handle_speaker_write(uint8_t *speaker, struct channel * ch, char val) {
440 *speaker = (val & ~0x20);
444 static int handle_channel_cmd(struct channel * ch, struct pit_cmd_word cmd) {
446 if (cmd.op_mode != ch->op_mode) {
447 PrintDebug("8254 PIT: Changing channel from op mode %d to op mode %d.\n",
448 ch->op_mode, cmd.op_mode);
451 if (cmd.access_mode != 0) {
452 ch->op_mode = cmd.op_mode;
455 if (cmd.access_mode != ch->access_mode) {
456 PrintDebug("8254 PIT: Changing channel from access mode %d to access mode %d.\n",
457 ch->access_mode, cmd.access_mode);
459 ch->access_mode = cmd.access_mode;
461 switch (cmd.access_mode) {
463 if (ch->latch_state == NOTLATCHED) {
464 ch->latched_value = ch->counter;
465 ch->latch_state = LATCHED;
469 ch->access_state = WAITING_HIBYTE;
473 ch->access_state = WAITING_LOBYTE;
478 switch (cmd.op_mode) {
479 case IRQ_ON_TERM_CNT:
495 PrintError("Invalid OP_MODE: %d\n", cmd.op_mode);
506 static int pit_read_channel(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
507 struct pit * state = (struct pit *)priv_data;
508 char * val = (char *)dst;
511 PrintError("8254 PIT: Invalid Read Write length \n");
515 PrintDebug("8254 PIT: Read of PIT Channel %d\n", port - CHANNEL0_PORT);
519 if (handle_channel_read(&(state->ch_0), val) == -1) {
520 PrintError("CHANNEL0 read error\n");
525 if (handle_channel_read(&(state->ch_1), val) == -1) {
526 PrintError("CHANNEL1 read error\n");
531 if (handle_channel_read(&(state->ch_2), val) == -1) {
532 PrintError("CHANNEL2 read error\n");
537 if (handle_speaker_read(&state->speaker, &(state->ch_2), val) == -1) {
538 PrintError("SPEAKER read error\n");
543 PrintError("8254 PIT: Read from invalid port (%d)\n", port);
552 static int pit_write_channel(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
553 struct pit * state = (struct pit *)priv_data;
554 char val = *(char *)src;
557 PrintError("8254 PIT: Invalid Write Length\n");
561 PrintDebug("8254 PIT: Write to PIT Channel %d (%x)\n", port - CHANNEL0_PORT, *(char*)src);
566 if (handle_channel_write(&(state->ch_0), val) == -1) {
567 PrintError("CHANNEL0 write error\n");
572 if (handle_channel_write(&(state->ch_1), val) == -1) {
573 PrintError("CHANNEL1 write error\n");
578 if (handle_channel_write(&(state->ch_2), val) == -1) {
579 PrintError("CHANNEL2 write error\n");
584 if (handle_speaker_write(&state->speaker, &(state->ch_2), val) == -1) {
585 PrintError("SPEAKER write error\n");
590 PrintError("8254 PIT: Write to invalid port (%d)\n", port);
600 static int pit_write_command(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
601 struct pit * state = (struct pit *)priv_data;
602 struct pit_cmd_word * cmd = (struct pit_cmd_word *)src;
604 PrintDebug("8254 PIT: Write to PIT Command port\n");
605 PrintDebug("8254 PIT: Writing to channel %d (access_mode = %d, op_mode = %d)\n", cmd->channel, cmd->access_mode, cmd->op_mode);
607 PrintError("8254 PIT: Write of Invalid length to command port\n");
611 switch (cmd->channel) {
613 if (handle_channel_cmd(&(state->ch_0), *cmd) == -1) {
614 PrintError("CHANNEL0 command error\n");
619 if (handle_channel_cmd(&(state->ch_1), *cmd) == -1) {
620 PrintError("CHANNEL1 command error\n");
625 if (handle_channel_cmd(&(state->ch_2), *cmd) == -1) {
626 PrintError("CHANNEL2 command error\n");
632 PrintError("Read back command not implemented\n");
646 static struct v3_timer_ops timer_ops = {
647 .update_timer = pit_update_timer,
651 static void init_channel(struct channel * ch) {
652 ch->run_state = NOT_RUNNING;
653 ch->access_state = NOT_WAITING;
658 ch->reload_value = 0;
660 ch->gate_input_pin = 0;
662 ch->latched_value = 0;
663 ch->latch_state = NOTLATCHED;
664 ch->read_state = LSB;
672 static int pit_free(void * private_data) {
673 struct pit * state = (struct pit *)private_data;
674 struct guest_info * info = &(state->vm->cores[0]);
678 v3_remove_timer(info, state->timer);
685 #ifdef V3_CONFIG_CHECKPOINT
686 static int pit_save(struct v3_chkpt_ctx * ctx, void * private_data) {
687 struct pit * pit_state = (struct pit *)private_data;
689 V3_CHKPT_STD_SAVE(ctx, pit_state->pit_counter);
690 V3_CHKPT_STD_SAVE(ctx, pit_state->pit_reload);
691 V3_CHKPT_STD_SAVE(ctx, pit_state->ch_0);
692 V3_CHKPT_STD_SAVE(ctx, pit_state->ch_1);
693 V3_CHKPT_STD_SAVE(ctx, pit_state->ch_2);
694 V3_CHKPT_STD_SAVE(ctx, pit_state->speaker);
699 static int pit_load(struct v3_chkpt_ctx * ctx, void * private_data) {
700 struct pit * pit_state = (struct pit *)private_data;
702 V3_CHKPT_STD_LOAD(ctx, pit_state->pit_counter);
703 V3_CHKPT_STD_LOAD(ctx, pit_state->pit_reload);
704 V3_CHKPT_STD_LOAD(ctx, pit_state->ch_0);
705 V3_CHKPT_STD_LOAD(ctx, pit_state->ch_1);
706 V3_CHKPT_STD_LOAD(ctx, pit_state->ch_2);
707 V3_CHKPT_STD_LOAD(ctx, pit_state->speaker);
713 static struct v3_device_ops dev_ops = {
714 .free = (int (*)(void *))pit_free,
715 #ifdef V3_CONFIG_CHECKPOINT
721 #include <palacios/vm_guest.h>
723 static int pit_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
724 struct pit * pit_state = NULL;
725 struct vm_device * dev = NULL;
726 char * dev_id = v3_cfg_val(cfg, "ID");
729 // PIT is only usable in non-multicore environments
730 // just hardcode the core context
731 struct guest_info * info = &(vm->cores[0]);
733 uint_t cpu_khz = info->time_state.guest_cpu_freq;
734 ullong_t reload_val = (ullong_t)cpu_khz * 1000;
736 pit_state = (struct pit *)V3_Malloc(sizeof(struct pit));
738 V3_ASSERT(pit_state != NULL);
739 pit_state->speaker = 0;
742 dev = v3_add_device(vm, dev_id, &dev_ops, pit_state);
745 PrintError("Could not attach device %s\n", dev_id);
750 ret |= v3_dev_hook_io(dev, CHANNEL0_PORT, &pit_read_channel, &pit_write_channel);
751 ret |= v3_dev_hook_io(dev, CHANNEL1_PORT, &pit_read_channel, &pit_write_channel);
752 ret |= v3_dev_hook_io(dev, CHANNEL2_PORT, &pit_read_channel, &pit_write_channel);
753 ret |= v3_dev_hook_io(dev, COMMAND_PORT, NULL, &pit_write_command);
754 ret |= v3_dev_hook_io(dev, SPEAKER_PORT, &pit_read_channel, &pit_write_channel);
757 PrintError("8254 PIT: Failed to hook IO ports\n");
758 v3_remove_device(dev);
762 #ifdef V3_CONFIG_DEBUG_PIT
763 PrintDebug("8254 PIT: OSC_HZ=%d, reload_val=", OSC_HZ);
764 //PrintTrace(reload_val);
770 pit_state->timer = v3_add_timer(info, &timer_ops, pit_state);
772 if (pit_state->timer == NULL) {
773 v3_remove_device(dev);
777 // Get cpu frequency and calculate the global pit oscilattor counter/cycle
779 do_divll(reload_val, OSC_HZ);
780 pit_state->pit_counter = reload_val;
781 pit_state->pit_reload = reload_val;
784 init_channel(&(pit_state->ch_0));
785 init_channel(&(pit_state->ch_1));
786 init_channel(&(pit_state->ch_2));
788 #ifdef V3_CONFIG_DEBUG_PIT
789 PrintDebug("8254 PIT: CPU MHZ=%d -- pit count=", cpu_khz / 1000);
790 //PrintTraceLL(pit_state->pit_counter);
798 device_register("8254_PIT", pit_init);