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".
20 #include <palacios/vmm.h>
21 #include <devices/ide.h>
22 #include "ide-types.h"
24 #define PRI_DEFAULT_IRQ 14
25 #define SEC_DEFAULT_IRQ 15
27 #define PRI_DATA_PORT 0x1f0
28 #define PRI_FEATURES_PORT 0x1f1
29 #define PRI_SECT_CNT_PORT 0x1f2
30 #define PRI_SECT_NUM_PORT 0x1f3
31 #define PRI_CYL_LOW_PORT 0x1f4
32 #define PRI_CYL_HIGH_PORT 0x1f5
33 #define PRI_DRV_SEL_PORT 0x1f6
34 #define PRI_CMD_PORT 0x1f7
35 #define PRI_CTRL_PORT 0x3f6
36 #define PRI_ADDR_REG_PORT 0x3f7
38 #define SEC_DATA_PORT 0x170
39 #define SEC_FEATURES_PORT 0x171
40 #define SEC_SECT_CNT_PORT 0x172
41 #define SEC_SECT_NUM_PORT 0x173
42 #define SEC_CYL_LOW_PORT 0x174
43 #define SEC_CYL_HIGH_PORT 0x175
44 #define SEC_DRV_SEL_PORT 0x176
45 #define SEC_CMD_PORT 0x177
46 #define SEC_CTRL_PORT 0x376
47 #define SEC_ADDR_REG_PORT 0x377
51 static const char * ide_dev_type_strs[] = {"HARDDISK", "CDROM", "NONE"};
54 static inline const char * device_type_to_str(v3_ide_dev_type_t type) {
59 return ide_dev_type_strs[type];
69 v3_ide_dev_type_t drive_type;
72 struct v3_ide_cd_ops * cd_ops;
73 struct v3_ide_hd_ops * hd_ops;
86 uint8_t sector_count; // 0x1f2,0x172
87 uint8_t sector_num; // 0x1f3,0x173
91 uint8_t cylinder_low; // 0x1f4,0x174
92 uint8_t cylinder_high; // 0x1f5,0x175
93 } __attribute__((packed));
94 } __attribute__((packed));
102 struct ide_drive drives[2];
105 struct ide_error_reg error_reg; // [read] 0x1f1,0x171
107 struct ide_features_reg features;
109 struct ide_drive_head_reg drive_head; // 0x1f6,0x176
111 struct ide_status_reg status; // [read] 0x1f7,0x177
112 uint8_t command_reg; // [write] 0x1f7,0x177
114 int irq; // this is temporary until we add PCI support
117 struct ide_ctrl_reg ctrl_reg; // [write] 0x3f6,0x376
122 struct ide_internal {
123 struct ide_channel channels[2];
129 static inline int get_channel_index(ushort_t port) {
130 if (((port & 0xfff8) == 0x1f0) ||
131 ((port & 0xfffe) == 0x3f6)) {
133 } else if (((port & 0xfff8) == 0x170) ||
134 ((port & 0xfffe) == 0x376)) {
141 static inline struct ide_channel * get_selected_channel(struct ide_internal * ide, ushort_t port) {
142 int channel_idx = get_channel_index(port);
143 return &(ide->channels[channel_idx]);
146 static inline struct ide_drive * get_selected_drive(struct ide_channel * channel) {
147 return &(channel->drives[channel->drive_head.drive_sel]);
151 static inline int is_lba_enabled(struct ide_channel * channel) {
152 return channel->drive_head.lba_mode;
156 static void ide_raise_irq(struct vm_device * dev, struct ide_channel * channel) {
157 if (channel->ctrl_reg.irq_disable == 0) {
158 v3_raise_irq(dev->vm, channel->irq);
163 static void drive_reset(struct ide_drive * drive) {
164 drive->sector_count = 0x01;
165 drive->sector_num = 0x01;
167 if (drive->drive_type == IDE_CDROM) {
168 drive->cylinder = 0xeb14;
170 drive->cylinder = 0x0000;
173 // Send the reset signal to the connected device callbacks
174 // channel->drives[0].reset();
175 // channel->drives[1].reset();
178 static void channel_reset(struct ide_channel * channel) {
180 // set busy and seek complete flags
181 channel->status.val = 0x90;
184 channel->error_reg.val = 0x01;
187 channel->command_reg = 0x00;
189 channel->ctrl_reg.irq_disable = 0;
192 static void channel_reset_complete(struct ide_channel * channel) {
193 channel->status.busy = 0;
194 channel->status.ready = 1;
196 channel->drive_head.head_num = 0;
198 drive_reset(&(channel->drives[0]));
199 drive_reset(&(channel->drives[1]));
203 static void ide_abort_command(struct vm_device * dev, struct ide_channel * channel) {
204 channel->status.val = 0x41; // Error + ready
205 channel->error_reg.val = 0x04; // No idea...
207 ide_raise_irq(dev, channel);
213 // Include the ATAPI interface handlers
219 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
220 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
221 struct ide_channel * channel = get_selected_channel(ide, port);
222 struct ide_drive * drive = get_selected_drive(channel);
225 PrintError("Invalid Write Length on IDE command Port %x\n", port);
229 channel->command_reg = *(uint8_t *)src;
232 PrintDebug("IDE: Writing Command Port %x (val=%x)\n", port, *(uint8_t *)src);
234 switch (channel->command_reg) {
236 case 0xa0: // ATAPI Command Packet
237 if (drive->drive_type != IDE_CDROM) {
238 ide_abort_command(dev, channel);
241 drive->sector_count = 1;
243 channel->status.busy = 0;
244 channel->status.write_fault = 0;
245 channel->status.data_req = 1;
246 channel->status.error = 0;
248 drive->data_offset = 0;
250 case 0xa1: // ATAPI Identify Device Packet
251 atapi_identify_device(drive);
253 channel->error_reg.val = 0;
254 channel->status.val = 0x58; // ready, data_req, seek_complete
256 ide_raise_irq(dev, channel);
257 case 0xec: // Identify Device
258 if (drive->drive_type != IDE_DISK) {
261 // JRL: Should we abort here?
262 ide_abort_command(dev, channel);
264 PrintError("IDE Disks currently not implemented\n");
275 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
276 PrintDebug("IDE: Writing Data Port %x (val=%x)\n", port, *(uint8_t *)src);
281 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
282 PrintDebug("IDE: Reading Data Port %x\n", port);
286 static int write_port_std(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
287 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
288 struct ide_channel * channel = get_selected_channel(ide, port);
289 struct ide_drive * drive = get_selected_drive(channel);
292 PrintError("Invalid Write length on IDE port %x\n", port);
296 PrintDebug("IDE: Writing Standard Port %x (val=%x)\n", port, *(uint8_t *)src);
300 // reset and interrupt enable
302 case SEC_CTRL_PORT: {
303 struct ide_ctrl_reg * tmp_ctrl = (struct ide_ctrl_reg *)src;
305 // only reset channel on a 0->1 reset bit transition
306 if ((!channel->ctrl_reg.soft_reset) && (tmp_ctrl->soft_reset)) {
307 channel_reset(channel);
308 } else if ((channel->ctrl_reg.soft_reset) && (!tmp_ctrl->soft_reset)) {
309 channel_reset_complete(channel);
312 channel->ctrl_reg.val = tmp_ctrl->val;
315 case PRI_FEATURES_PORT:
316 case SEC_FEATURES_PORT:
317 channel->features.val = *(uint8_t *)src;
320 case PRI_SECT_CNT_PORT:
321 case SEC_SECT_CNT_PORT:
322 drive->sector_count = *(uint8_t *)src;
325 case PRI_SECT_NUM_PORT:
326 case SEC_SECT_NUM_PORT:
327 drive->sector_num = *(uint8_t *)src;
329 case PRI_CYL_LOW_PORT:
330 case SEC_CYL_LOW_PORT:
331 drive->cylinder_low = *(uint8_t *)src;
334 case PRI_CYL_HIGH_PORT:
335 case SEC_CYL_HIGH_PORT:
336 drive->cylinder_high = *(uint8_t *)src;
339 case PRI_DRV_SEL_PORT:
340 case SEC_DRV_SEL_PORT: {
341 channel->drive_head.val = *(uint8_t *)src;
343 // make sure the reserved bits are ok..
344 // JRL TODO: check with new ramdisk to make sure this is right...
345 channel->drive_head.val |= 0xa0;
347 drive = get_selected_drive(channel);
349 // Selecting a non-present device is a no-no
350 if (drive->drive_type == IDE_NONE) {
351 PrintDebug("Attempting to select a non-present drive\n");
352 channel->error_reg.abort = 1;
353 channel->status.error = 1;
359 PrintError("IDE: Write to unknown Port %x\n", port);
366 static int read_port_std(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
367 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
368 struct ide_channel * channel = get_selected_channel(ide, port);
369 struct ide_drive * drive = get_selected_drive(channel);
372 PrintError("Invalid Read length on IDE port %x\n", port);
376 PrintDebug("IDE: Reading Standard Port %x\n", port);
379 if ((port == PRI_ADDR_REG_PORT) ||
380 (port == SEC_ADDR_REG_PORT)) {
381 // unused, return 0xff
382 *(uint8_t *)dst = 0xff;
387 // if no drive is present just return 0 + reserved bits
388 if (drive->drive_type == IDE_NONE) {
389 if ((port == PRI_DRV_SEL_PORT) ||
390 (port == SEC_DRV_SEL_PORT)) {
391 *(uint8_t *)dst = 0xa0;
401 // This is really the error register.
402 case PRI_FEATURES_PORT:
403 case SEC_FEATURES_PORT:
404 *(uint8_t *)dst = channel->error_reg.val;
407 case PRI_SECT_CNT_PORT:
408 case SEC_SECT_CNT_PORT:
409 *(uint8_t *)dst = drive->sector_count;
412 case PRI_SECT_NUM_PORT:
413 case SEC_SECT_NUM_PORT:
414 *(uint8_t *)dst = drive->sector_num;
417 case PRI_CYL_LOW_PORT:
418 case SEC_CYL_LOW_PORT:
419 *(uint8_t *)dst = drive->cylinder_low;
423 case PRI_CYL_HIGH_PORT:
424 case SEC_CYL_HIGH_PORT:
425 *(uint8_t *)dst = drive->cylinder_high;
428 case PRI_DRV_SEL_PORT:
429 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
430 *(uint8_t *)dst = channel->drive_head.val;
437 // Something about lowering interrupts here....
438 *(uint8_t *)dst = channel->status.val;
442 PrintError("Invalid Port: %x\n", port);
446 PrintDebug("\tVal=%x\n", *(uint8_t *)dst);
453 static void init_drive(struct ide_drive * drive) {
455 drive->sector_count = 0x01;
456 drive->sector_num = 0x01;
457 drive->cylinder = 0x0000;
459 drive->drive_type = IDE_NONE;
461 drive->data_offset = 0;
462 memset(drive->model, 0, sizeof(drive->model));
463 memset(drive->buffer, 0, sizeof(drive->buffer));
465 drive->private_data = NULL;
466 drive->cd_ops = NULL;
469 static void init_channel(struct ide_channel * channel) {
472 channel->error_reg.val = 0x01;
473 channel->drive_head.val = 0x00;
474 channel->status.val = 0x00;
475 channel->command_reg = 0x00;
476 channel->ctrl_reg.val = 0x08;
479 for (i = 0; i < 2; i++) {
480 init_drive(&(channel->drives[i]));
485 static void init_ide_state(struct ide_internal * ide) {
488 for (i = 0; i < 2; i++) {
489 init_channel(&(ide->channels[i]));
491 // JRL: this is a terrible hack...
492 ide->channels[i].irq = PRI_DEFAULT_IRQ + i;
498 static int init_ide(struct vm_device * dev) {
499 struct ide_internal * ide = (struct ide_internal *)(dev->private_data);
501 PrintDebug("IDE: Initializing IDE\n");
506 v3_dev_hook_io(dev, PRI_CTRL_PORT,
507 &read_port_std, &write_port_std);
509 v3_dev_hook_io(dev, PRI_DATA_PORT,
510 &read_data_port, &write_data_port);
511 v3_dev_hook_io(dev, PRI_FEATURES_PORT,
512 &read_port_std, &write_port_std);
513 v3_dev_hook_io(dev, PRI_SECT_CNT_PORT,
514 &read_port_std, &write_port_std);
515 v3_dev_hook_io(dev, PRI_SECT_NUM_PORT,
516 &read_port_std, &write_port_std);
517 v3_dev_hook_io(dev, PRI_CYL_LOW_PORT,
518 &read_port_std, &write_port_std);
519 v3_dev_hook_io(dev, PRI_CYL_HIGH_PORT,
520 &read_port_std, &write_port_std);
521 v3_dev_hook_io(dev, PRI_DRV_SEL_PORT,
522 &read_port_std, &write_port_std);
523 v3_dev_hook_io(dev, PRI_CMD_PORT,
524 &read_port_std, &write_cmd_port);
527 v3_dev_hook_io(dev, SEC_CTRL_PORT,
528 &read_port_std, &write_port_std);
530 v3_dev_hook_io(dev, SEC_DATA_PORT,
531 &read_data_port, &write_data_port);
532 v3_dev_hook_io(dev, SEC_FEATURES_PORT,
533 &read_port_std, &write_port_std);
534 v3_dev_hook_io(dev, SEC_SECT_CNT_PORT,
535 &read_port_std, &write_port_std);
536 v3_dev_hook_io(dev, SEC_SECT_NUM_PORT,
537 &read_port_std, &write_port_std);
538 v3_dev_hook_io(dev, SEC_CYL_LOW_PORT,
539 &read_port_std, &write_port_std);
540 v3_dev_hook_io(dev, SEC_CYL_HIGH_PORT,
541 &read_port_std, &write_port_std);
542 v3_dev_hook_io(dev, SEC_DRV_SEL_PORT,
543 &read_port_std, &write_port_std);
544 v3_dev_hook_io(dev, SEC_CMD_PORT,
545 &read_port_std, &write_cmd_port);
549 v3_dev_hook_io(dev, SEC_ADDR_REG_PORT,
550 &read_port_std, &write_port_std);
552 v3_dev_hook_io(dev, PRI_ADDR_REG_PORT,
553 &read_port_std, &write_port_std);
559 static int deinit_ide(struct vm_device * dev) {
560 // unhook io ports....
561 // deregister from PCI?
566 static struct vm_device_ops dev_ops = {
568 .deinit = deinit_ide,
575 struct vm_device * v3_create_ide() {
576 struct ide_internal * ide = (struct ide_internal *)V3_Malloc(sizeof(struct ide_internal));
577 struct vm_device * device = v3_create_device("IDE", &dev_ops, ide);
581 PrintDebug("IDE: Creating IDE bus x 2\n");
590 int v3_ide_register_cdrom(struct vm_device * ide_dev,
594 struct v3_ide_cd_ops * ops,
595 void * private_data) {
597 struct ide_internal * ide = (struct ide_internal *)(ide_dev->private_data);
598 struct ide_channel * channel = NULL;
599 struct ide_drive * drive = NULL;
601 V3_ASSERT((bus_num >= 0) && (bus_num < 2));
602 V3_ASSERT((drive_num >= 0) && (drive_num < 2));
604 channel = &(ide->channels[bus_num]);
605 drive = &(channel->drives[drive_num]);
607 if (drive->drive_type != IDE_NONE) {
608 PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
612 strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
614 while (strlen((char *)(drive->model)) < 40) {
615 strcat((char*)(drive->model), " ");
619 drive->drive_type = IDE_CDROM;
623 drive->private_data = private_data;
629 int v3_ide_register_harddisk(struct vm_device * ide_dev,
633 struct v3_ide_hd_ops * ops,
634 void * private_data) {
636 struct ide_internal * ide = (struct ide_internal *)(ide_dev->private_data);
637 struct ide_channel * channel = NULL;
638 struct ide_drive * drive = NULL;
640 V3_ASSERT((bus_num >= 0) && (bus_num < 2));
641 V3_ASSERT((drive_num >= 0) && (drive_num < 2));
643 channel = &(ide->channels[bus_num]);
644 drive = &(channel->drives[drive_num]);
646 if (drive->drive_type != IDE_NONE) {
647 PrintError("Device slot (bus=%d, drive=%d) already occupied\n", bus_num, drive_num);
651 strncpy(drive->model, dev_name, sizeof(drive->model) - 1);
653 drive->drive_type = IDE_DISK;
657 drive->private_data = private_data;