2 * Copyright (c) 2004-2005 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 for an IDE disk
33 #ifndef __IDE_DISK_HH__
34 #define __IDE_DISK_HH__
36 #include "base/statistics.hh"
37 #include "dev/disk_image.hh"
38 #include "dev/ide_atareg.h"
39 #include "dev/ide_ctrl.hh"
40 #include "dev/ide_wdcreg.h"
41 #include "dev/io_device.hh"
42 #include "sim/eventq.hh"
44 #define DMA_BACKOFF_PERIOD 200
46 #define MAX_DMA_SIZE (131072) // 128K
47 #define MAX_MULTSECT (128)
49 #define PRD_BASE_MASK 0xfffffffe
50 #define PRD_COUNT_MASK 0xfffe
51 #define PRD_EOT_MASK 0x8000
53 typedef struct PrdEntry {
63 uint32_t getBaseAddr()
65 return (entry.baseAddr & PRD_BASE_MASK);
68 uint32_t getByteCount()
70 return ((entry.byteCount == 0) ? MAX_DMA_SIZE :
71 (entry.byteCount & PRD_COUNT_MASK));
76 return (entry.endOfTable & PRD_EOT_MASK);
80 #define DATA_OFFSET (0)
81 #define ERROR_OFFSET (1)
82 #define FEATURES_OFFSET (1)
83 #define NSECTOR_OFFSET (2)
84 #define SECTOR_OFFSET (3)
85 #define LCYL_OFFSET (4)
86 #define HCYL_OFFSET (5)
87 #define SELECT_OFFSET (6)
88 #define DRIVE_OFFSET (6)
89 #define STATUS_OFFSET (7)
90 #define COMMAND_OFFSET (7)
92 #define CONTROL_OFFSET (2)
93 #define ALTSTAT_OFFSET (2)
95 #define SELECT_DEV_BIT 0x10
96 #define CONTROL_RST_BIT 0x04
97 #define CONTROL_IEN_BIT 0x02
98 #define STATUS_BSY_BIT 0x80
99 #define STATUS_DRDY_BIT 0x40
100 #define STATUS_DRQ_BIT 0x08
101 #define STATUS_SEEK_BIT 0x10
102 #define STATUS_DF_BIT 0x20
103 #define DRIVE_LBA_BIT 0x40
108 typedef struct CommandReg {
122 typedef enum Events {
132 typedef enum DevAction {
143 ACT_DATA_WRITE_SHORT,
150 typedef enum DevState {
162 // PIO data-in (data to host)
167 // PIO data-out (data from host)
169 Data_Ready_INTRQ_Out,
177 typedef enum DmaState {
183 class PhysicalMemory;
187 * IDE Disk device model
189 class IdeDisk : public SimObject
192 /** The IDE controller for this disk. */
194 /** The image that contains the data of this disk. */
198 /** The disk delay in microseconds. */
202 /** Drive identification structure for this disk */
203 struct ataparams driveID;
204 /** Data buffer for transfers */
206 /** Number of bytes in command data transfer */
208 /** Number of bytes left in command data transfer */
209 uint32_t cmdBytesLeft;
210 /** Number of bytes left in DRQ block */
211 uint32_t drqBytesLeft;
212 /** Current sector in access */
214 /** Command block registers */
216 /** Status register */
218 /** Interrupt enable bit */
224 /** Dma transaction is a read */
226 /** PRD table base address */
229 PrdTableEntry curPrd;
230 /** Device ID (master=0/slave=1) */
232 /** Interrupt pending */
235 Stats::Scalar<> dmaReadFullPages;
236 Stats::Scalar<> dmaReadBytes;
237 Stats::Scalar<> dmaReadTxs;
238 Stats::Scalar<> dmaWriteFullPages;
239 Stats::Scalar<> dmaWriteBytes;
240 Stats::Scalar<> dmaWriteTxs;
244 * Create and initialize this Disk.
245 * @param name The name of this disk.
246 * @param img The disk image of this disk.
247 * @param id The disk ID (master=0/slave=1)
248 * @param disk_delay The disk delay in milliseconds
250 IdeDisk(const std::string &name, DiskImage *img, int id, Tick disk_delay);
253 * Delete the data buffer.
258 * Reset the device state
263 * Register Statistics
268 * Set the controller for this device
269 * @param c The IDE controller
271 void setController(IdeController *c) {
272 if (ctrl) panic("Cannot change the controller once set!\n");
276 // Device register read/write
277 void read(const Addr &offset, IdeRegType regtype, uint8_t *data);
278 void write(const Addr &offset, IdeRegType regtype, const uint8_t *data);
280 // Start/abort functions
281 void startDma(const uint32_t &prdTableBase);
287 // Interrupt management
292 void doDmaTransfer();
293 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer>;
294 EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer> dmaTransferEvent;
296 void doDmaDataRead();
299 ChunkGenerator *dmaReadCG;
300 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaRead>;
301 EventWrapper<IdeDisk, &IdeDisk::doDmaRead> dmaReadWaitEvent;
303 void doDmaDataWrite();
306 ChunkGenerator *dmaWriteCG;
307 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaWrite>;
308 EventWrapper<IdeDisk, &IdeDisk::doDmaWrite> dmaWriteWaitEvent;
310 void dmaPrdReadDone();
311 friend class EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone>;
312 EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone> dmaPrdReadEvent;
315 friend class EventWrapper<IdeDisk, &IdeDisk::dmaReadDone>;
316 EventWrapper<IdeDisk, &IdeDisk::dmaReadDone> dmaReadEvent;
319 friend class EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone>;
320 EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone> dmaWriteEvent;
322 // Disk image read/write
323 void readDisk(uint32_t sector, uint8_t *data);
324 void writeDisk(uint32_t sector, uint8_t *data);
326 // State machine management
327 void updateState(DevAction_t action);
330 bool isBSYSet() { return (status & STATUS_BSY_BIT); }
331 bool isIENSet() { return nIENBit; }
336 // clear out the status byte
339 status |= STATUS_DRDY_BIT;
341 status |= STATUS_SEEK_BIT;
344 uint32_t getLBABase()
346 return (Addr)(((cmdReg.head & 0xf) << 24) | (cmdReg.cyl_high << 16) |
347 (cmdReg.cyl_low << 8) | (cmdReg.sec_num));
350 inline Addr pciToDma(Addr pciAddr);
353 * Serialize this object to the given output stream.
354 * @param os The stream to serialize to.
356 void serialize(std::ostream &os);
359 * Reconstruct the state of this object from a checkpoint.
360 * @param cp The checkpoint to use.
361 * @param section The section name describing this object.
363 void unserialize(Checkpoint *cp, const std::string §ion);
367 #endif // __IDE_DISK_HH__