2 * Copyright (c) 2004 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Device model implementation for an IDE disk
38 #include "arch/alpha/pmap.h"
39 #include "base/cprintf.hh" // csprintf
40 #include "base/trace.hh"
41 #include "dev/disk_image.hh"
42 #include "dev/ide_disk.hh"
43 #include "dev/ide_ctrl.hh"
44 #include "dev/tsunami.hh"
45 #include "dev/tsunami_pchip.hh"
46 #include "mem/functional_mem/physical_memory.hh"
47 #include "mem/bus/bus.hh"
48 #include "mem/bus/dma_interface.hh"
49 #include "mem/bus/pio_interface.hh"
50 #include "mem/bus/pio_interface_impl.hh"
51 #include "sim/builder.hh"
52 #include "sim/sim_object.hh"
53 #include "sim/universe.hh"
57 IdeDisk::IdeDisk(const string
&name
, DiskImage
*img
, PhysicalMemory
*phys
,
59 : SimObject(name
), ctrl(NULL
), image(img
), physmem(phys
),
60 dmaTransferEvent(this), dmaReadWaitEvent(this),
61 dmaWriteWaitEvent(this), dmaPrdReadEvent(this),
62 dmaReadEvent(this), dmaWriteEvent(this)
64 // Reset the device state
67 // calculate disk delay in microseconds
68 diskDelay
= (delay
* ticksPerSecond
/ 100000);
70 // fill out the drive ID structure
71 memset(&driveID
, 0, sizeof(struct hd_driveid
));
73 // Calculate LBA and C/H/S values
78 uint32_t lba_size
= image
->size();
79 if (lba_size
>= 16383*16*63) {
89 if ((lba_size
/ sectors
) >= 16)
92 heads
= (lba_size
/ sectors
);
94 cylinders
= lba_size
/ (heads
* sectors
);
97 // Setup the model name
98 sprintf((char *)driveID
.model
, "5MI EDD si k");
99 // Set the maximum multisector transfer size
100 driveID
.max_multsect
= MAX_MULTSECT
;
101 // IORDY supported, IORDY disabled, LBA enabled, DMA enabled
102 driveID
.capability
= 0x7;
103 // UDMA support, EIDE support
104 driveID
.field_valid
= 0x6;
105 // Setup default C/H/S settings
106 driveID
.cyls
= cylinders
;
107 driveID
.sectors
= sectors
;
108 driveID
.heads
= heads
;
109 // Setup the current multisector transfer size
110 driveID
.multsect
= MAX_MULTSECT
;
111 driveID
.multsect_valid
= 0x1;
112 // Number of sectors on disk
113 driveID
.lba_capacity
= lba_size
;
114 // Multiword DMA mode 2 and below supported
115 driveID
.dma_mword
= 0x400;
116 // Set PIO mode 4 and 3 supported
117 driveID
.eide_pio_modes
= 0x3;
118 // Set DMA mode 4 and below supported
119 driveID
.dma_ultra
= 0x10;
120 // Statically set hardware config word
121 driveID
.hw_config
= 0x4001;
126 // destroy the data buffer
127 delete [] dataBuffer
;
131 IdeDisk::reset(int id
)
133 // initialize the data buffer and shadow registers
134 dataBuffer
= new uint8_t[MAX_DMA_SIZE
];
136 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
137 memset(&cmdReg
, 0, sizeof(CommandReg_t
));
138 memset(&curPrd
.entry
, 0, sizeof(PrdEntry_t
));
140 dmaInterfaceBytes
= 0;
149 // set the device state to idle
153 devState
= Device_Idle_S
;
155 } else if (id
== DEV1
) {
156 devState
= Device_Idle_NS
;
159 panic("Invalid device ID: %#x\n", id
);
162 // set the device ready bit
163 status
= STATUS_DRDY_BIT
;
171 IdeDisk::pciToDma(Addr pciAddr
)
174 return ctrl
->tsunami
->pchip
->translatePciToDma(pciAddr
);
176 panic("Access to unset controller!\n");
180 IdeDisk::bytesInDmaPage(Addr curAddr
, uint32_t bytesLeft
)
182 uint32_t bytesInPage
= 0;
184 // First calculate how many bytes could be in the page
185 if (bytesLeft
> ALPHA_PGBYTES
)
186 bytesInPage
= ALPHA_PGBYTES
;
188 bytesInPage
= bytesLeft
;
190 // Next, see if we have crossed a page boundary, and adjust
191 Addr upperBound
= curAddr
+ bytesInPage
;
192 Addr pageBound
= alpha_trunc_page(curAddr
) + ALPHA_PGBYTES
;
194 assert(upperBound
>= curAddr
&& "DMA read wraps around address space!\n");
196 if (upperBound
>= pageBound
)
197 bytesInPage
= pageBound
- curAddr
;
203 // Device registers read/write
207 IdeDisk::read(const Addr
&offset
, bool byte
, bool cmdBlk
, uint8_t *data
)
209 DevAction_t action
= ACT_NONE
;
212 if (offset
< 0 || offset
> sizeof(CommandReg_t
))
213 panic("Invalid disk command register offset: %#x\n", offset
);
215 if (!byte
&& offset
!= DATA_OFFSET
)
216 panic("Invalid 16-bit read, only allowed on data reg\n");
219 *(uint16_t *)data
= *(uint16_t *)&cmdReg
.data0
;
221 *data
= ((uint8_t *)&cmdReg
)[offset
];
223 // determine if an action needs to be taken on the state machine
224 if (offset
== STATUS_OFFSET
) {
225 action
= ACT_STAT_READ
;
226 *data
= status
; // status is in a shadow, explicity copy
227 } else if (offset
== DATA_OFFSET
) {
229 action
= ACT_DATA_READ_BYTE
;
231 action
= ACT_DATA_READ_SHORT
;
235 if (offset
!= ALTSTAT_OFFSET
)
236 panic("Invalid disk control register offset: %#x\n", offset
);
239 panic("Invalid 16-bit read from control block\n");
244 if (action
!= ACT_NONE
)
249 IdeDisk::write(const Addr
&offset
, bool byte
, bool cmdBlk
, const uint8_t *data
)
251 DevAction_t action
= ACT_NONE
;
254 if (offset
< 0 || offset
> sizeof(CommandReg_t
))
255 panic("Invalid disk command register offset: %#x\n", offset
);
257 if (!byte
&& offset
!= DATA_OFFSET
)
258 panic("Invalid 16-bit write, only allowed on data reg\n");
261 *((uint16_t *)&cmdReg
.data0
) = *(uint16_t *)data
;
263 ((uint8_t *)&cmdReg
)[offset
] = *data
;
265 // determine if an action needs to be taken on the state machine
266 if (offset
== COMMAND_OFFSET
) {
267 action
= ACT_CMD_WRITE
;
268 } else if (offset
== DATA_OFFSET
) {
270 action
= ACT_DATA_WRITE_BYTE
;
272 action
= ACT_DATA_WRITE_SHORT
;
273 } else if (offset
== SELECT_OFFSET
) {
274 action
= ACT_SELECT_WRITE
;
278 if (offset
!= CONTROL_OFFSET
)
279 panic("Invalid disk control register offset: %#x\n", offset
);
282 panic("Invalid 16-bit write to control block\n");
284 if (*data
& CONTROL_RST_BIT
) {
285 // force the device into the reset state
286 devState
= Device_Srst
;
287 action
= ACT_SRST_SET
;
288 } else if (devState
== Device_Srst
&& !(*data
& CONTROL_RST_BIT
)) {
289 action
= ACT_SRST_CLEAR
;
292 nIENBit
= (*data
& CONTROL_IEN_BIT
) ? true : false;
295 if (action
!= ACT_NONE
)
300 // Perform DMA transactions
304 IdeDisk::doDmaTransfer()
306 if (dmaState
!= Dma_Transfer
|| devState
!= Transfer_Data_Dma
)
307 panic("Inconsistent DMA transfer state: dmaState = %d devState = %d\n",
310 // first read the current PRD
312 if (dmaInterface
->busy()) {
313 // reschedule after waiting period
314 dmaTransferEvent
.schedule(curTick
+ DMA_BACKOFF_PERIOD
);
318 dmaInterface
->doDMA(Read
, curPrdAddr
, sizeof(PrdEntry_t
), curTick
,
326 IdeDisk::dmaPrdReadDone()
328 // actually copy the PRD from physical memory
329 memcpy((void *)&curPrd
.entry
,
330 physmem
->dma_addr(curPrdAddr
, sizeof(PrdEntry_t
)),
333 DPRINTF(IdeDisk
, "PRD: baseAddr:%#x (%#x) byteCount:%d (%d) eot:%#x sector:%d\n",
334 curPrd
.getBaseAddr(), pciToDma(curPrd
.getBaseAddr()),
335 curPrd
.getByteCount(), (cmdBytesLeft
/SectorSize
),
336 curPrd
.getEOT(), curSector
);
338 // make sure the new curPrdAddr is properly translated from PCI to system
339 curPrdAddr
= pciToDma(curPrdAddr
+ sizeof(PrdEntry_t
));
350 Tick totalDiskDelay
= diskDelay
+ (curPrd
.getByteCount() / SectorSize
);
353 if (dmaInterface
->busy()) {
354 // reschedule after waiting period
355 dmaReadWaitEvent
.schedule(curTick
+ DMA_BACKOFF_PERIOD
);
359 Addr dmaAddr
= pciToDma(curPrd
.getBaseAddr());
361 uint32_t bytesInPage
= bytesInDmaPage(curPrd
.getBaseAddr(),
362 (uint32_t)curPrd
.getByteCount());
364 dmaInterfaceBytes
= bytesInPage
;
366 dmaInterface
->doDMA(Read
, dmaAddr
, bytesInPage
,
367 curTick
+ totalDiskDelay
, &dmaReadEvent
);
369 // schedule dmaReadEvent with sectorDelay (dmaReadDone)
370 dmaReadEvent
.schedule(curTick
+ totalDiskDelay
);
375 IdeDisk::dmaReadDone()
378 Addr curAddr
= 0, dmaAddr
= 0;
379 uint32_t bytesWritten
= 0, bytesInPage
= 0, bytesLeft
= 0;
381 // continue to use the DMA interface until all pages are read
382 if (dmaInterface
&& (dmaInterfaceBytes
< curPrd
.getByteCount())) {
383 // see if the interface is busy
384 if (dmaInterface
->busy()) {
385 // reschedule after waiting period
386 dmaReadEvent
.schedule(curTick
+ DMA_BACKOFF_PERIOD
);
390 uint32_t bytesLeft
= curPrd
.getByteCount() - dmaInterfaceBytes
;
391 curAddr
= curPrd
.getBaseAddr() + dmaInterfaceBytes
;
392 dmaAddr
= pciToDma(curAddr
);
394 bytesInPage
= bytesInDmaPage(curAddr
, bytesLeft
);
395 dmaInterfaceBytes
+= bytesInPage
;
397 dmaInterface
->doDMA(Read
, dmaAddr
, bytesInPage
,
398 curTick
, &dmaReadEvent
);
403 // set initial address
404 curAddr
= curPrd
.getBaseAddr();
406 // clear out the data buffer
407 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
409 // read the data from memory via DMA into a data buffer
410 while (bytesWritten
< curPrd
.getByteCount()) {
411 if (cmdBytesLeft
<= 0)
412 panic("DMA data is larger than # of sectors specified\n");
414 dmaAddr
= pciToDma(curAddr
);
416 // calculate how many bytes are in the current page
417 bytesLeft
= curPrd
.getByteCount() - bytesWritten
;
418 bytesInPage
= bytesInDmaPage(curAddr
, bytesLeft
);
420 // copy the data from memory into the data buffer
421 memcpy((void *)(dataBuffer
+ bytesWritten
),
422 physmem
->dma_addr(dmaAddr
, bytesInPage
),
425 curAddr
+= bytesInPage
;
426 bytesWritten
+= bytesInPage
;
427 cmdBytesLeft
-= bytesInPage
;
430 // write the data to the disk image
431 for (bytesWritten
= 0;
432 bytesWritten
< curPrd
.getByteCount();
433 bytesWritten
+= SectorSize
) {
435 writeDisk(curSector
++, (uint8_t *)(dataBuffer
+ bytesWritten
));
439 if (curPrd
.getEOT()) {
440 assert(cmdBytesLeft
== 0);
442 updateState(ACT_DMA_DONE
);
449 IdeDisk::doDmaWrite()
451 Tick totalDiskDelay
= diskDelay
+ (curPrd
.getByteCount() / SectorSize
);
454 if (dmaInterface
->busy()) {
455 // reschedule after waiting period
456 dmaWriteWaitEvent
.schedule(curTick
+ DMA_BACKOFF_PERIOD
);
460 Addr dmaAddr
= pciToDma(curPrd
.getBaseAddr());
462 uint32_t bytesInPage
= bytesInDmaPage(curPrd
.getBaseAddr(),
463 (uint32_t)curPrd
.getByteCount());
465 dmaInterfaceBytes
= bytesInPage
;
467 dmaInterface
->doDMA(WriteInvalidate
, dmaAddr
,
468 bytesInPage
, curTick
+ totalDiskDelay
,
471 // schedule event with disk delay (dmaWriteDone)
472 dmaWriteEvent
.schedule(curTick
+ totalDiskDelay
);
477 IdeDisk::dmaWriteDone()
479 Addr curAddr
= 0, pageAddr
= 0, dmaAddr
= 0;
480 uint32_t bytesRead
= 0, bytesInPage
= 0;
482 // continue to use the DMA interface until all pages are read
483 if (dmaInterface
&& (dmaInterfaceBytes
< curPrd
.getByteCount())) {
484 // see if the interface is busy
485 if (dmaInterface
->busy()) {
486 // reschedule after waiting period
487 dmaWriteEvent
.schedule(curTick
+ DMA_BACKOFF_PERIOD
);
491 uint32_t bytesLeft
= curPrd
.getByteCount() - dmaInterfaceBytes
;
492 curAddr
= curPrd
.getBaseAddr() + dmaInterfaceBytes
;
493 dmaAddr
= pciToDma(curAddr
);
495 bytesInPage
= bytesInDmaPage(curAddr
, bytesLeft
);
496 dmaInterfaceBytes
+= bytesInPage
;
498 dmaInterface
->doDMA(WriteInvalidate
, dmaAddr
,
499 bytesInPage
, curTick
,
505 // setup the initial page and DMA address
506 curAddr
= curPrd
.getBaseAddr();
507 pageAddr
= alpha_trunc_page(curAddr
);
508 dmaAddr
= pciToDma(curAddr
);
510 // clear out the data buffer
511 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
513 while (bytesRead
< curPrd
.getByteCount()) {
514 // see if we have crossed into a new page
515 if (pageAddr
!= alpha_trunc_page(curAddr
)) {
516 // write the data to memory
517 memcpy(physmem
->dma_addr(dmaAddr
, bytesInPage
),
518 (void *)(dataBuffer
+ (bytesRead
- bytesInPage
)),
521 // update the DMA address and page address
522 pageAddr
= alpha_trunc_page(curAddr
);
523 dmaAddr
= pciToDma(curAddr
);
528 if (cmdBytesLeft
<= 0)
529 panic("DMA requested data is larger than # sectors specified\n");
531 readDisk(curSector
++, (uint8_t *)(dataBuffer
+ bytesRead
));
533 curAddr
+= SectorSize
;
534 bytesRead
+= SectorSize
;
535 bytesInPage
+= SectorSize
;
536 cmdBytesLeft
-= SectorSize
;
539 // write the last page worth read to memory
540 if (bytesInPage
!= 0) {
541 memcpy(physmem
->dma_addr(dmaAddr
, bytesInPage
),
542 (void *)(dataBuffer
+ (bytesRead
- bytesInPage
)),
547 if (curPrd
.getEOT()) {
548 assert(cmdBytesLeft
== 0);
550 updateState(ACT_DMA_DONE
);
557 // Disk utility routines
561 IdeDisk::readDisk(uint32_t sector
, uint8_t *data
)
563 uint32_t bytesRead
= image
->read(data
, sector
);
565 if (bytesRead
!= SectorSize
)
566 panic("Can't read from %s. Only %d of %d read. errno=%d\n",
567 name(), bytesRead
, SectorSize
, errno
);
571 IdeDisk::writeDisk(uint32_t sector
, uint8_t *data
)
573 uint32_t bytesWritten
= image
->write(data
, sector
);
575 if (bytesWritten
!= SectorSize
)
576 panic("Can't write to %s. Only %d of %d written. errno=%d\n",
577 name(), bytesWritten
, SectorSize
, errno
);
581 // Setup and handle commands
585 IdeDisk::startDma(const uint32_t &prdTableBase
)
587 if (dmaState
!= Dma_Start
)
588 panic("Inconsistent DMA state, should be in Dma_Start!\n");
590 if (devState
!= Transfer_Data_Dma
)
591 panic("Inconsistent device state for DMA start!\n");
593 // PRD base address is given by bits 31:2
594 curPrdAddr
= pciToDma((Addr
)(prdTableBase
& ~ULL(0x3)));
596 dmaState
= Dma_Transfer
;
598 // schedule dma transfer (doDmaTransfer)
599 dmaTransferEvent
.schedule(curTick
+ 1);
605 if (dmaState
== Dma_Idle
)
606 panic("Inconsistent DMA state, should be in Dma_Start or Dma_Transfer!\n");
608 if (devState
!= Transfer_Data_Dma
&& devState
!= Prepare_Data_Dma
)
609 panic("Inconsistent device state, should be in Transfer or Prepare!\n");
611 updateState(ACT_CMD_ERROR
);
615 IdeDisk::startCommand()
617 DevAction_t action
= ACT_NONE
;
622 switch (cmdReg
.command
) {
623 // Supported non-data commands
624 case WIN_READ_NATIVE_MAX
:
625 size
= image
->size() - 1;
626 cmdReg
.sec_num
= (size
& 0xff);
627 cmdReg
.cyl_low
= ((size
& 0xff00) >> 8);
628 cmdReg
.cyl_high
= ((size
& 0xff0000) >> 16);
629 cmdReg
.head
= ((size
& 0xf000000) >> 24);
631 devState
= Command_Execution
;
632 action
= ACT_CMD_COMPLETE
;
637 case WIN_STANDBYNOW1
:
638 case WIN_FLUSH_CACHE
:
641 case WIN_SETFEATURES
:
643 devState
= Command_Execution
;
644 action
= ACT_CMD_COMPLETE
;
647 // Supported PIO data-in commands
649 cmdBytes
= cmdBytesLeft
= sizeof(struct hd_driveid
);
650 devState
= Prepare_Data_In
;
651 action
= ACT_DATA_READY
;
656 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
657 panic("Attempt to perform CHS access, only supports LBA\n");
659 if (cmdReg
.sec_count
== 0)
660 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
662 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
664 curSector
= getLBABase();
666 /** @todo make this a scheduled event to simulate disk delay */
667 devState
= Prepare_Data_In
;
668 action
= ACT_DATA_READY
;
671 // Supported PIO data-out commands
674 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
675 panic("Attempt to perform CHS access, only supports LBA\n");
677 if (cmdReg
.sec_count
== 0)
678 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
680 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
682 curSector
= getLBABase();
684 devState
= Prepare_Data_Out
;
685 action
= ACT_DATA_READY
;
688 // Supported DMA commands
690 dmaRead
= true; // a write to the disk is a DMA read from memory
692 if (!(cmdReg
.drive
& DRIVE_LBA_BIT
))
693 panic("Attempt to perform CHS access, only supports LBA\n");
695 if (cmdReg
.sec_count
== 0)
696 cmdBytes
= cmdBytesLeft
= (256 * SectorSize
);
698 cmdBytes
= cmdBytesLeft
= (cmdReg
.sec_count
* SectorSize
);
700 curSector
= getLBABase();
702 devState
= Prepare_Data_Dma
;
703 action
= ACT_DMA_READY
;
707 panic("Unsupported ATA command: %#x\n", cmdReg
.command
);
710 if (action
!= ACT_NONE
) {
712 status
|= STATUS_BSY_BIT
;
714 status
&= ~STATUS_DRQ_BIT
;
716 status
&= ~STATUS_DF_BIT
;
723 // Handle setting and clearing interrupts
730 panic("Attempt to post an interrupt with one pending\n");
734 // talk to controller to set interrupt
743 panic("Attempt to clear a non-pending interrupt\n");
747 // talk to controller to clear interrupt
753 // Manage the device internal state machine
757 IdeDisk::updateState(DevAction_t action
)
761 if (action
== ACT_SRST_SET
) {
763 status
|= STATUS_BSY_BIT
;
764 } else if (action
== ACT_SRST_CLEAR
) {
766 status
&= ~STATUS_BSY_BIT
;
768 // reset the device state
774 if (action
== ACT_SELECT_WRITE
&& !isDEVSelect()) {
775 devState
= Device_Idle_NS
;
776 } else if (action
== ACT_CMD_WRITE
) {
783 if (action
== ACT_SELECT_WRITE
&& !isDEVSelect()) {
784 devState
= Device_Idle_NS
;
786 } else if (action
== ACT_STAT_READ
|| isIENSet()) {
787 devState
= Device_Idle_S
;
789 } else if (action
== ACT_CMD_WRITE
) {
797 if (action
== ACT_SELECT_WRITE
&& isDEVSelect()) {
798 if (!isIENSet() && intrPending
) {
799 devState
= Device_Idle_SI
;
802 if (isIENSet() || !intrPending
) {
803 devState
= Device_Idle_S
;
808 case Command_Execution
:
809 if (action
== ACT_CMD_COMPLETE
) {
814 devState
= Device_Idle_SI
;
817 devState
= Device_Idle_S
;
822 case Prepare_Data_In
:
823 if (action
== ACT_CMD_ERROR
) {
828 devState
= Device_Idle_SI
;
831 devState
= Device_Idle_S
;
833 } else if (action
== ACT_DATA_READY
) {
835 status
&= ~STATUS_BSY_BIT
;
837 status
|= STATUS_DRQ_BIT
;
839 // copy the data into the data buffer
840 if (cmdReg
.command
== WIN_IDENTIFY
) {
841 // Reset the drqBytes for this block
842 drqBytesLeft
= sizeof(struct hd_driveid
);
844 memcpy((void *)dataBuffer
, (void *)&driveID
,
845 sizeof(struct hd_driveid
));
847 // Reset the drqBytes for this block
848 drqBytesLeft
= SectorSize
;
850 readDisk(curSector
++, dataBuffer
);
853 // put the first two bytes into the data register
854 memcpy((void *)&cmdReg
.data0
, (void *)dataBuffer
,
858 devState
= Data_Ready_INTRQ_In
;
861 devState
= Transfer_Data_In
;
866 case Data_Ready_INTRQ_In
:
867 if (action
== ACT_STAT_READ
) {
868 devState
= Transfer_Data_In
;
873 case Transfer_Data_In
:
874 if (action
== ACT_DATA_READ_BYTE
|| action
== ACT_DATA_READ_SHORT
) {
875 if (action
== ACT_DATA_READ_BYTE
) {
876 panic("DEBUG: READING DATA ONE BYTE AT A TIME!\n");
881 // copy next short into data registers
883 memcpy((void *)&cmdReg
.data0
,
884 (void *)&dataBuffer
[SectorSize
- drqBytesLeft
],
888 if (drqBytesLeft
== 0) {
889 if (cmdBytesLeft
== 0) {
892 devState
= Device_Idle_S
;
894 devState
= Prepare_Data_In
;
896 status
|= STATUS_BSY_BIT
;
898 status
&= ~STATUS_DRQ_BIT
;
900 /** @todo change this to a scheduled event to simulate
902 updateState(ACT_DATA_READY
);
908 case Prepare_Data_Out
:
909 if (action
== ACT_CMD_ERROR
|| cmdBytesLeft
== 0) {
914 devState
= Device_Idle_SI
;
917 devState
= Device_Idle_S
;
919 } else if (action
== ACT_DATA_READY
&& cmdBytesLeft
!= 0) {
921 status
&= ~STATUS_BSY_BIT
;
923 status
|= STATUS_DRQ_BIT
;
925 // clear the data buffer to get it ready for writes
926 memset(dataBuffer
, 0, MAX_DMA_SIZE
);
928 // reset the drqBytes for this block
929 drqBytesLeft
= SectorSize
;
931 if (cmdBytesLeft
== cmdBytes
|| isIENSet()) {
932 devState
= Transfer_Data_Out
;
934 devState
= Data_Ready_INTRQ_Out
;
940 case Data_Ready_INTRQ_Out
:
941 if (action
== ACT_STAT_READ
) {
942 devState
= Transfer_Data_Out
;
947 case Transfer_Data_Out
:
948 if (action
== ACT_DATA_WRITE_BYTE
||
949 action
== ACT_DATA_WRITE_SHORT
) {
951 if (action
== ACT_DATA_READ_BYTE
) {
952 panic("DEBUG: WRITING DATA ONE BYTE AT A TIME!\n");
954 // copy the latest short into the data buffer
955 memcpy((void *)&dataBuffer
[SectorSize
- drqBytesLeft
],
956 (void *)&cmdReg
.data0
,
963 if (drqBytesLeft
== 0) {
964 // copy the block to the disk
965 writeDisk(curSector
++, dataBuffer
);
968 status
|= STATUS_BSY_BIT
;
970 status
|= STATUS_SEEK_BIT
;
972 status
&= ~STATUS_DRQ_BIT
;
974 devState
= Prepare_Data_Out
;
976 /** @todo change this to a scheduled event to simulate
978 updateState(ACT_DATA_READY
);
983 case Prepare_Data_Dma
:
984 if (action
== ACT_CMD_ERROR
) {
989 devState
= Device_Idle_SI
;
992 devState
= Device_Idle_S
;
994 } else if (action
== ACT_DMA_READY
) {
996 status
&= ~STATUS_BSY_BIT
;
998 status
|= STATUS_DRQ_BIT
;
1000 devState
= Transfer_Data_Dma
;
1002 if (dmaState
!= Dma_Idle
)
1003 panic("Inconsistent DMA state, should be Dma_Idle\n");
1005 dmaState
= Dma_Start
;
1006 // wait for the write to the DMA start bit
1010 case Transfer_Data_Dma
:
1011 if (action
== ACT_CMD_ERROR
|| action
== ACT_DMA_DONE
) {
1012 // clear the BSY bit
1015 status
|= STATUS_SEEK_BIT
;
1016 // clear the controller state for DMA transfer
1017 ctrl
->setDmaComplete(this);
1020 devState
= Device_Idle_SI
;
1023 devState
= Device_Idle_S
;
1029 panic("Unknown IDE device state: %#x\n", devState
);
1034 IdeDisk::serialize(ostream
&os
)
1036 // Check all outstanding events to see if they are scheduled
1037 // these are all mutually exclusive
1038 Tick reschedule
= 0;
1039 Events_t event
= None
;
1043 if (dmaTransferEvent
.scheduled()) {
1044 reschedule
= dmaTransferEvent
.when();
1048 if (dmaReadWaitEvent
.scheduled()) {
1049 reschedule
= dmaReadWaitEvent
.when();
1053 if (dmaWriteWaitEvent
.scheduled()) {
1054 reschedule
= dmaWriteWaitEvent
.when();
1058 if (dmaPrdReadEvent
.scheduled()) {
1059 reschedule
= dmaPrdReadEvent
.when();
1063 if (dmaReadEvent
.scheduled()) {
1064 reschedule
= dmaReadEvent
.when();
1068 if (dmaWriteEvent
.scheduled()) {
1069 reschedule
= dmaWriteEvent
.when();
1074 assert(eventCount
<= 1);
1076 SERIALIZE_SCALAR(reschedule
);
1077 SERIALIZE_ENUM(event
);
1079 // Serialize device registers
1080 SERIALIZE_SCALAR(cmdReg
.data0
);
1081 SERIALIZE_SCALAR(cmdReg
.data1
);
1082 SERIALIZE_SCALAR(cmdReg
.sec_count
);
1083 SERIALIZE_SCALAR(cmdReg
.sec_num
);
1084 SERIALIZE_SCALAR(cmdReg
.cyl_low
);
1085 SERIALIZE_SCALAR(cmdReg
.cyl_high
);
1086 SERIALIZE_SCALAR(cmdReg
.drive
);
1087 SERIALIZE_SCALAR(cmdReg
.command
);
1088 SERIALIZE_SCALAR(status
);
1089 SERIALIZE_SCALAR(nIENBit
);
1090 SERIALIZE_SCALAR(devID
);
1092 // Serialize the PRD related information
1093 SERIALIZE_SCALAR(curPrd
.entry
.baseAddr
);
1094 SERIALIZE_SCALAR(curPrd
.entry
.byteCount
);
1095 SERIALIZE_SCALAR(curPrd
.entry
.endOfTable
);
1096 SERIALIZE_SCALAR(curPrdAddr
);
1098 // Serialize current transfer related information
1099 SERIALIZE_SCALAR(cmdBytesLeft
);
1100 SERIALIZE_SCALAR(cmdBytes
);
1101 SERIALIZE_SCALAR(drqBytesLeft
);
1102 SERIALIZE_SCALAR(curSector
);
1103 SERIALIZE_SCALAR(dmaRead
);
1104 SERIALIZE_SCALAR(dmaInterfaceBytes
);
1105 SERIALIZE_SCALAR(intrPending
);
1106 SERIALIZE_ENUM(devState
);
1107 SERIALIZE_ENUM(dmaState
);
1108 SERIALIZE_ARRAY(dataBuffer
, MAX_DMA_SIZE
);
1112 IdeDisk::unserialize(Checkpoint
*cp
, const string
§ion
)
1114 // Reschedule events that were outstanding
1115 // these are all mutually exclusive
1116 Tick reschedule
= 0;
1117 Events_t event
= None
;
1119 UNSERIALIZE_SCALAR(reschedule
);
1120 UNSERIALIZE_ENUM(event
);
1124 case Transfer
: dmaTransferEvent
.schedule(reschedule
); break;
1125 case ReadWait
: dmaReadWaitEvent
.schedule(reschedule
); break;
1126 case WriteWait
: dmaWriteWaitEvent
.schedule(reschedule
); break;
1127 case PrdRead
: dmaPrdReadEvent
.schedule(reschedule
); break;
1128 case DmaRead
: dmaReadEvent
.schedule(reschedule
); break;
1129 case DmaWrite
: dmaWriteEvent
.schedule(reschedule
); break;
1132 // Unserialize device registers
1133 UNSERIALIZE_SCALAR(cmdReg
.data0
);
1134 UNSERIALIZE_SCALAR(cmdReg
.data1
);
1135 UNSERIALIZE_SCALAR(cmdReg
.sec_count
);
1136 UNSERIALIZE_SCALAR(cmdReg
.sec_num
);
1137 UNSERIALIZE_SCALAR(cmdReg
.cyl_low
);
1138 UNSERIALIZE_SCALAR(cmdReg
.cyl_high
);
1139 UNSERIALIZE_SCALAR(cmdReg
.drive
);
1140 UNSERIALIZE_SCALAR(cmdReg
.command
);
1141 UNSERIALIZE_SCALAR(status
);
1142 UNSERIALIZE_SCALAR(nIENBit
);
1143 UNSERIALIZE_SCALAR(devID
);
1145 // Unserialize the PRD related information
1146 UNSERIALIZE_SCALAR(curPrd
.entry
.baseAddr
);
1147 UNSERIALIZE_SCALAR(curPrd
.entry
.byteCount
);
1148 UNSERIALIZE_SCALAR(curPrd
.entry
.endOfTable
);
1149 UNSERIALIZE_SCALAR(curPrdAddr
);
1151 // Unserialize current transfer related information
1152 UNSERIALIZE_SCALAR(cmdBytes
);
1153 UNSERIALIZE_SCALAR(cmdBytesLeft
);
1154 UNSERIALIZE_SCALAR(drqBytesLeft
);
1155 UNSERIALIZE_SCALAR(curSector
);
1156 UNSERIALIZE_SCALAR(dmaRead
);
1157 UNSERIALIZE_SCALAR(dmaInterfaceBytes
);
1158 UNSERIALIZE_SCALAR(intrPending
);
1159 UNSERIALIZE_ENUM(devState
);
1160 UNSERIALIZE_ENUM(dmaState
);
1161 UNSERIALIZE_ARRAY(dataBuffer
, MAX_DMA_SIZE
);
1164 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1166 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeDisk
)
1168 SimObjectParam
<DiskImage
*> image
;
1169 SimObjectParam
<PhysicalMemory
*> physmem
;
1171 Param
<int> disk_delay
;
1173 END_DECLARE_SIM_OBJECT_PARAMS(IdeDisk
)
1175 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeDisk
)
1177 INIT_PARAM(image
, "Disk image"),
1178 INIT_PARAM(physmem
, "Physical memory"),
1179 INIT_PARAM(driveID
, "Drive ID (0=master 1=slave)"),
1180 INIT_PARAM_DFLT(disk_delay
, "Fixed disk delay in microseconds", 1)
1182 END_INIT_SIM_OBJECT_PARAMS(IdeDisk
)
1185 CREATE_SIM_OBJECT(IdeDisk
)
1187 return new IdeDisk(getInstanceName(), image
, physmem
, driveID
,
1191 REGISTER_SIM_OBJECT("IdeDisk", IdeDisk
)
1193 #endif //DOXYGEN_SHOULD_SKIP_THIS