1 #include <devices/8254.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_time.h>
4 #include <palacios/vmm_util.h>
5 #include <palacios/vmm_intr.h>
14 #define OSC_HZ 1193182
17 /* The 8254 has three counters and one control port */
18 #define CHANNEL0_PORT 0x40
19 #define CHANNEL1_PORT 0x41
20 #define CHANNEL2_PORT 0x42
21 #define COMMAND_PORT 0x43
24 #define PIT_INTR_NUM 0
26 /* The order of these typedefs is important because the numerical values correspond to the
27 * values coming from the io ports
29 typedef enum {NOT_RUNNING, PENDING, RUNNING} channel_run_state_t;
30 typedef enum {NOT_WAITING, WAITING_LOBYTE, WAITING_HIBYTE} channel_access_state_t;
31 typedef enum {LATCH_COUNT, LOBYTE_ONLY, HIBYTE_ONLY, LOBYTE_HIBYTE} channel_access_mode_t;
32 typedef enum {IRQ_ON_TERM_CNT, ONE_SHOT, RATE_GEN, SQR_WAVE, SW_STROBE, HW_STROBE} channel_op_mode_t;
36 channel_access_mode_t access_mode;
37 channel_access_state_t access_state;
38 channel_run_state_t run_state;
40 channel_op_mode_t op_mode;
43 // Time til interrupt trigger
46 ushort_t reload_value;
48 ushort_t latched_value;
50 enum {NOTLATCHED, LATCHED} latch_state;
52 enum {LSB, MSB} read_state;
54 uint_t output_pin : 1;
55 uint_t gate_input_pin : 1;
74 uint_t access_mode : 2;
78 struct pit_rdb_cmd_word {
79 uint_t rsvd : 1; // SBZ
83 uint_t latch_status : 1;
84 uint_t latch_count : 1;
85 uint_t readback_cmd : 2; // Must Be 0x3
88 struct pit_rdb_status_word {
91 uint_t access_mode : 2;
92 uint_t null_count : 1;
99 * This should call out to handle_SQR_WAVE_tics, etc...
101 // Returns true if the the output signal changed state
102 static int handle_crystal_tics(struct vm_device * dev, struct channel * ch, uint_t oscillations) {
103 uint_t channel_cycles = 0;
104 uint_t output_changed = 0;
106 // PrintDebug("8254 PIT: %d crystal tics\n", oscillations);
107 if (ch->run_state == PENDING) {
109 ch->counter = ch->reload_value;
111 if (ch->op_mode == SQR_WAVE) {
112 ch->counter -= ch->counter % 2;
115 ch->run_state = RUNNING;
116 } else if (ch->run_state != RUNNING) {
117 return output_changed;
121 PrintDebug("8254 PIT: Channel Run State = %d, counter=", ch->run_state);
122 PrintTraceLL(ch->counter);
125 if (ch->op_mode == SQR_WAVE) {
129 if (ch->counter > oscillations) {
130 ch->counter -= oscillations;
131 return output_changed;
133 ushort_t reload_val = ch->reload_value;
134 oscillations -= ch->counter;
139 if (ch->op_mode == SQR_WAVE) {
140 reload_val -= reload_val % 2;
143 channel_cycles += oscillations / reload_val;
144 oscillations = oscillations % reload_val;
146 ch->counter = reload_val - oscillations;
149 // PrintDebug("8254 PIT: Channel Cycles: %d\n", channel_cycles);
153 switch (ch->op_mode) {
154 case IRQ_ON_TERM_CNT:
155 if ((channel_cycles > 0) && (ch->output_pin == 0)) {
161 if ((channel_cycles > 0) && (ch->output_pin == 0)) {
167 // See the data sheet: we ignore the output pin cycle...
168 if (channel_cycles > 0) {
173 ch->output_pin = (ch->output_pin + 1) % 2;
175 if (ch->output_pin == 1) {
190 return output_changed;
195 static void pit_update_time(ullong_t cpu_cycles, ullong_t cpu_freq, void * private_data) {
196 struct vm_device * dev = (struct vm_device *)private_data;
197 struct pit * state = (struct pit *)dev->private_data;
198 // ullong_t tmp_ctr = state->pit_counter;
200 uint_t oscillations = 0;
204 PrintDebug("updating cpu_cycles=");
205 PrintTraceLL(cpu_cycles);
208 PrintDebug("pit_counter=");
209 PrintTraceLL(state->pit_counter);
212 PrintDebug("pit_reload=");
213 PrintTraceLL(state->pit_reload);
217 if (state->pit_counter > cpu_cycles) {
219 state->pit_counter -= cpu_cycles;
222 // Take off the first part
223 cpu_cycles -= state->pit_counter;
224 state->pit_counter = 0;
227 if (cpu_cycles > state->pit_reload) {
228 // how many full oscillations
229 tmp_cycles = cpu_cycles;
231 cpu_cycles = do_divll(tmp_cycles, state->pit_reload);
233 oscillations += tmp_cycles;
236 // update counter with remainder (mod reload)
237 state->pit_counter = state->pit_reload - cpu_cycles;
239 //PrintDebug("8254 PIT: Handling %d crystal tics\n", oscillations);
240 if (handle_crystal_tics(dev, &(state->ch_0), oscillations) == 1) {
242 PrintDebug("8254 PIT: Injecting Timer interrupt to guest\n");
243 v3_raise_irq(dev->vm, 0);
246 //handle_crystal_tics(dev, &(state->ch_1), oscillations);
247 //handle_crystal_tics(dev, &(state->ch_2), oscillations);
258 /* This should call out to handle_SQR_WAVE_write, etc...
260 static int handle_channel_write(struct channel * ch, char val) {
262 switch (ch->access_state) {
265 ushort_t tmp_val = ((ushort_t)val) << 8;
266 ch->reload_value &= 0x00ff;
267 ch->reload_value |= tmp_val;
270 if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)){
271 ch->run_state = PENDING;
274 if (ch->access_mode == LOBYTE_HIBYTE) {
275 ch->access_state = WAITING_LOBYTE;
278 PrintDebug("8254 PIT: updated channel counter: %d\n", ch->reload_value);
279 PrintDebug("8254 PIT: Channel Run State=%d\n", ch->run_state);
283 ch->reload_value &= 0xff00;
284 ch->reload_value |= val;
286 if (ch->access_mode == LOBYTE_HIBYTE) {
287 ch->access_state = WAITING_HIBYTE;
288 } else if ((ch->op_mode != RATE_GEN) || (ch->run_state != RUNNING)) {
289 ch->run_state = PENDING;
292 PrintDebug("8254 PIT: updated channel counter: %d\n", ch->reload_value);
293 PrintDebug("8254 PIT: Channel Run State=%d\n", ch->run_state);
300 switch (ch->op_mode) {
301 case IRQ_ON_TERM_CNT:
323 static int handle_channel_read(struct channel * ch, char * val) {
327 if (ch->latch_state == NOTLATCHED) {
328 myval = &(ch->counter);
330 myval = &(ch->latched_value);
333 if (ch->read_state == LSB) {
334 *val = ((char*)myval)[0]; // little endian
335 ch->read_state = MSB;
337 *val = ((char*)myval)[1];
338 ch->read_state = LSB;
339 if (ch->latch_state == LATCHED) {
340 ch->latch_state = NOTLATCHED;
352 static int handle_channel_cmd(struct channel * ch, struct pit_cmd_word cmd) {
353 ch->op_mode = cmd.op_mode;
354 ch->access_mode = cmd.access_mode;
359 switch (cmd.access_mode) {
361 if (ch->latch_state == NOTLATCHED) {
362 ch->latched_value = ch->counter;
363 ch->latch_state = LATCHED;
367 ch->access_state = WAITING_HIBYTE;
371 ch->access_state = WAITING_LOBYTE;
376 switch (cmd.op_mode) {
377 case IRQ_ON_TERM_CNT:
400 static int pit_read_channel(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
401 struct pit * state = (struct pit *)dev->private_data;
402 char * val = (char *)dst;
405 PrintDebug("8254 PIT: Invalid Read Write length \n");
409 PrintDebug("8254 PIT: Read of PIT Channel %d\n", port - CHANNEL0_PORT);
413 if (handle_channel_read(&(state->ch_0), val) == -1) {
418 if (handle_channel_read(&(state->ch_1), val) == -1) {
423 if (handle_channel_read(&(state->ch_2), val) == -1) {
428 PrintDebug("8254 PIT: Read from invalid port (%d)\n", port);
437 static int pit_write_channel(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
438 struct pit * state = (struct pit *)dev->private_data;
439 char val = *(char *)src;
442 PrintDebug("8254 PIT: Invalid Write Length\n");
446 PrintDebug("8254 PIT: Write to PIT Channel %d (%x)\n", port - CHANNEL0_PORT, *(char*)src);
451 if (handle_channel_write(&(state->ch_0), val) == -1) {
456 if (handle_channel_write(&(state->ch_1), val) == -1) {
461 if (handle_channel_write(&(state->ch_2), val) == -1) {
466 PrintDebug("8254 PIT: Write to invalid port (%d)\n", port);
476 static int pit_write_command(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
477 struct pit * state = (struct pit *)dev->private_data;
478 struct pit_cmd_word * cmd = (struct pit_cmd_word *)src;
480 PrintDebug("8254 PIT: Write to PIT Command port\n");
481 PrintDebug("8254 PIT: Writing to channel %d (access_mode = %d, op_mode = %d)\n", cmd->channel, cmd->access_mode, cmd->op_mode);
483 PrintDebug("8254 PIT: Write of Invalid length to command port\n");
487 switch (cmd->channel) {
489 if (handle_channel_cmd(&(state->ch_0), *cmd) == -1) {
494 if (handle_channel_cmd(&(state->ch_1), *cmd) == -1) {
499 if (handle_channel_cmd(&(state->ch_2), *cmd) == -1) {
518 static struct vm_timer_ops timer_ops = {
519 .update_time = pit_update_time,
523 static void init_channel(struct channel * ch) {
524 ch->run_state = NOT_RUNNING;
525 ch->access_state = NOT_WAITING;
530 ch->reload_value = 0;
532 ch->gate_input_pin = 0;
534 ch->latched_value = 0;
535 ch->latch_state = NOTLATCHED;
536 ch->read_state = LSB;
542 static int pit_init(struct vm_device * dev) {
543 struct pit * state = (struct pit *)dev->private_data;
544 uint_t cpu_khz = V3_CPU_KHZ();
545 ullong_t reload_val = (ullong_t)cpu_khz * 1000;
547 dev_hook_io(dev, CHANNEL0_PORT, &pit_read_channel, &pit_write_channel);
548 dev_hook_io(dev, CHANNEL1_PORT, &pit_read_channel, &pit_write_channel);
549 dev_hook_io(dev, CHANNEL2_PORT, &pit_read_channel, &pit_write_channel);
550 dev_hook_io(dev, COMMAND_PORT, NULL, &pit_write_command);
552 PrintDebug("8254 PIT: OSC_HZ=%d, reload_val=", OSC_HZ);
553 PrintTraceLL(reload_val);
557 v3_add_timer(dev->vm, &timer_ops, dev);
559 // Get cpu frequency and calculate the global pit oscilattor counter/cycle
561 do_divll(reload_val, OSC_HZ);
562 state->pit_counter = reload_val;
563 state->pit_reload = reload_val;
567 init_channel(&(state->ch_0));
568 init_channel(&(state->ch_1));
569 init_channel(&(state->ch_2));
571 PrintDebug("8254 PIT: CPU MHZ=%d -- pit count=", cpu_khz / 1000);
572 PrintTraceLL(state->pit_counter);
578 static int pit_deinit(struct vm_device * dev) {
584 static struct vm_device_ops dev_ops = {
586 .deinit = pit_deinit,
594 struct vm_device * create_pit() {
595 struct pit * pit_state = NULL;
596 pit_state = (struct pit *)V3_Malloc(sizeof(struct pit));
597 V3_ASSERT(pit_state != NULL);
599 struct vm_device * dev = create_device("PIT", &dev_ops, pit_state);