2 * Copyright (c) 2013 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Andrew Schultz
44 * Device model for an IDE disk
47 #ifndef __IDE_DISK_HH__
48 #define __IDE_DISK_HH__
50 #include "base/statistics.hh"
51 #include "dev/disk_image.hh"
52 #include "dev/ide_atareg.h"
53 #include "dev/ide_ctrl.hh"
54 #include "dev/ide_wdcreg.h"
55 #include "dev/io_device.hh"
56 #include "params/IdeDisk.hh"
57 #include "sim/eventq.hh"
61 #define DMA_BACKOFF_PERIOD 200
63 #define MAX_DMA_SIZE 0x20000 // 128K
64 #define MAX_SINGLE_DMA_SIZE 0x10000
65 #define MAX_MULTSECT (128)
67 #define PRD_BASE_MASK 0xfffffffe
68 #define PRD_COUNT_MASK 0xfffe
69 #define PRD_EOT_MASK 0x8000
71 typedef struct PrdEntry {
81 uint32_t getBaseAddr()
83 return (entry.baseAddr & PRD_BASE_MASK);
86 uint32_t getByteCount()
88 return ((entry.byteCount == 0) ? MAX_SINGLE_DMA_SIZE :
89 (entry.byteCount & PRD_COUNT_MASK));
94 return (entry.endOfTable & PRD_EOT_MASK);
98 #define DATA_OFFSET (0)
99 #define ERROR_OFFSET (1)
100 #define FEATURES_OFFSET (1)
101 #define NSECTOR_OFFSET (2)
102 #define SECTOR_OFFSET (3)
103 #define LCYL_OFFSET (4)
104 #define HCYL_OFFSET (5)
105 #define SELECT_OFFSET (6)
106 #define DRIVE_OFFSET (6)
107 #define STATUS_OFFSET (7)
108 #define COMMAND_OFFSET (7)
110 #define CONTROL_OFFSET (2)
111 #define ALTSTAT_OFFSET (2)
113 #define SELECT_DEV_BIT 0x10
114 #define CONTROL_RST_BIT 0x04
115 #define CONTROL_IEN_BIT 0x02
116 #define STATUS_BSY_BIT 0x80
117 #define STATUS_DRDY_BIT 0x40
118 #define STATUS_DRQ_BIT 0x08
119 #define STATUS_SEEK_BIT 0x10
120 #define STATUS_DF_BIT 0x20
121 #define DRIVE_LBA_BIT 0x40
126 typedef struct CommandReg {
140 typedef enum Events {
150 typedef enum DevAction {
161 ACT_DATA_WRITE_SHORT,
168 typedef enum DevState {
180 // PIO data-in (data to host)
185 // PIO data-out (data from host)
187 Data_Ready_INTRQ_Out,
196 typedef enum DmaState {
205 * IDE Disk device model
207 class IdeDisk : public SimObject
210 /** The IDE controller for this disk. */
212 /** The image that contains the data of this disk. */
216 /** The disk delay in microseconds. */
220 /** Drive identification structure for this disk */
221 struct ataparams driveID;
222 /** Data buffer for transfers */
224 /** Number of bytes in command data transfer */
226 /** Number of bytes left in command data transfer */
227 uint32_t cmdBytesLeft;
228 /** Number of bytes left in DRQ block */
229 uint32_t drqBytesLeft;
230 /** Current sector in access */
232 /** Command block registers */
234 /** Status register */
236 /** Interrupt enable bit */
242 /** Dma transaction is a read */
244 /** PRD table base address */
247 PrdTableEntry curPrd;
248 /** Device ID (master=0/slave=1) */
250 /** Interrupt pending */
255 Stats::Scalar dmaReadFullPages;
256 Stats::Scalar dmaReadBytes;
257 Stats::Scalar dmaReadTxs;
258 Stats::Scalar dmaWriteFullPages;
259 Stats::Scalar dmaWriteBytes;
260 Stats::Scalar dmaWriteTxs;
263 typedef IdeDiskParams Params;
264 IdeDisk(const Params *p);
267 * Delete the data buffer.
272 * Reset the device state
277 * Register Statistics
279 void regStats() override;
282 * Set the controller for this device
283 * @param c The IDE controller
285 void setController(IdeController *c) {
286 if (ctrl) panic("Cannot change the controller once set!\n");
290 // Device register read/write
291 void readCommand(const Addr offset, int size, uint8_t *data);
292 void readControl(const Addr offset, int size, uint8_t *data);
293 void writeCommand(const Addr offset, int size, const uint8_t *data);
294 void writeControl(const Addr offset, int size, const uint8_t *data);
296 // Start/abort functions
297 void startDma(const uint32_t &prdTableBase);
303 // Interrupt management
308 void doDmaTransfer();
309 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer>;
310 EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer> dmaTransferEvent;
312 void doDmaDataRead();
315 ChunkGenerator *dmaReadCG;
316 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaRead>;
317 EventWrapper<IdeDisk, &IdeDisk::doDmaRead> dmaReadWaitEvent;
319 void doDmaDataWrite();
322 ChunkGenerator *dmaWriteCG;
323 friend class EventWrapper<IdeDisk, &IdeDisk::doDmaWrite>;
324 EventWrapper<IdeDisk, &IdeDisk::doDmaWrite> dmaWriteWaitEvent;
326 void dmaPrdReadDone();
327 friend class EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone>;
328 EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone> dmaPrdReadEvent;
331 friend class EventWrapper<IdeDisk, &IdeDisk::dmaReadDone>;
332 EventWrapper<IdeDisk, &IdeDisk::dmaReadDone> dmaReadEvent;
335 friend class EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone>;
336 EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone> dmaWriteEvent;
338 // Disk image read/write
339 void readDisk(uint32_t sector, uint8_t *data);
340 void writeDisk(uint32_t sector, uint8_t *data);
342 // State machine management
343 void updateState(DevAction_t action);
346 bool isBSYSet() { return (status & STATUS_BSY_BIT); }
347 bool isIENSet() { return nIENBit; }
352 // clear out the status byte
355 status |= STATUS_DRDY_BIT;
357 status |= STATUS_SEEK_BIT;
360 uint32_t getLBABase()
362 return (Addr)(((cmdReg.head & 0xf) << 24) | (cmdReg.cyl_high << 16) |
363 (cmdReg.cyl_low << 8) | (cmdReg.sec_num));
366 inline Addr pciToDma(Addr pciAddr);
368 void serialize(CheckpointOut &cp) const override;
369 void unserialize(CheckpointIn &cp) override;
373 #endif // __IDE_DISK_HH__