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.
42 * Device model for an IDE disk
45 #ifndef __DEV_STORAGE_IDE_DISK_HH__
46 #define __DEV_STORAGE_IDE_DISK_HH__
48 #include "base/statistics.hh"
49 #include "dev/io_device.hh"
50 #include "dev/storage/disk_image.hh"
51 #include "dev/storage/ide_atareg.h"
52 #include "dev/storage/ide_ctrl.hh"
53 #include "dev/storage/ide_wdcreg.h"
54 #include "params/IdeDisk.hh"
55 #include "sim/eventq.hh"
59 #define DMA_BACKOFF_PERIOD 200
61 #define MAX_DMA_SIZE 0x20000 // 128K
62 #define MAX_SINGLE_DMA_SIZE 0x10000
63 #define MAX_MULTSECT (128)
65 #define PRD_BASE_MASK 0xfffffffe
66 #define PRD_COUNT_MASK 0xfffe
67 #define PRD_EOT_MASK 0x8000
69 typedef struct PrdEntry {
79 uint32_t getBaseAddr()
81 return (entry.baseAddr & PRD_BASE_MASK);
84 uint32_t getByteCount()
86 return ((entry.byteCount == 0) ? MAX_SINGLE_DMA_SIZE :
87 (entry.byteCount & PRD_COUNT_MASK));
92 return (entry.endOfTable & PRD_EOT_MASK);
96 #define DATA_OFFSET (0)
97 #define ERROR_OFFSET (1)
98 #define FEATURES_OFFSET (1)
99 #define NSECTOR_OFFSET (2)
100 #define SECTOR_OFFSET (3)
101 #define LCYL_OFFSET (4)
102 #define HCYL_OFFSET (5)
103 #define SELECT_OFFSET (6)
104 #define DRIVE_OFFSET (6)
105 #define STATUS_OFFSET (7)
106 #define COMMAND_OFFSET (7)
108 #define CONTROL_OFFSET (2)
109 #define ALTSTAT_OFFSET (2)
111 #define SELECT_DEV_BIT 0x10
112 #define CONTROL_RST_BIT 0x04
113 #define CONTROL_IEN_BIT 0x02
114 #define STATUS_BSY_BIT 0x80
115 #define STATUS_DRDY_BIT 0x40
116 #define STATUS_DRQ_BIT 0x08
117 #define STATUS_SEEK_BIT 0x10
118 #define STATUS_DF_BIT 0x20
119 #define DRIVE_LBA_BIT 0x40
124 typedef struct CommandReg {
138 typedef enum Events {
148 typedef enum DevAction {
159 ACT_DATA_WRITE_SHORT,
166 typedef enum DevState {
178 // PIO data-in (data to host)
183 // PIO data-out (data from host)
185 Data_Ready_INTRQ_Out,
194 typedef enum DmaState {
203 * IDE Disk device model
205 class IdeDisk : public SimObject
208 /** The IDE controller for this disk. */
210 /** The image that contains the data of this disk. */
214 /** The disk delay in microseconds. */
218 /** Drive identification structure for this disk */
219 struct ataparams driveID;
220 /** Data buffer for transfers */
222 /** Number of bytes in command data transfer */
224 /** Number of bytes left in command data transfer */
225 uint32_t cmdBytesLeft;
226 /** Number of bytes left in DRQ block */
227 uint32_t drqBytesLeft;
228 /** Current sector in access */
230 /** Command block registers */
232 /** Status register */
234 /** Interrupt enable bit */
240 /** Dma transaction is a read */
242 /** Size of OS pages. */
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
286 setController(IdeController *c, Addr page_bytes)
288 panic_if(ctrl, "Cannot change the controller once set!\n");
290 pageBytes = page_bytes;
293 // Device register read/write
294 void readCommand(const Addr offset, int size, uint8_t *data);
295 void readControl(const Addr offset, int size, uint8_t *data);
296 void writeCommand(const Addr offset, int size, const uint8_t *data);
297 void writeControl(const Addr offset, int size, const uint8_t *data);
299 // Start/abort functions
300 void startDma(const uint32_t &prdTableBase);
306 // Interrupt management
311 void doDmaTransfer();
312 EventFunctionWrapper dmaTransferEvent;
314 void doDmaDataRead();
317 ChunkGenerator *dmaReadCG;
318 EventFunctionWrapper dmaReadWaitEvent;
320 void doDmaDataWrite();
323 ChunkGenerator *dmaWriteCG;
324 EventFunctionWrapper dmaWriteWaitEvent;
326 void dmaPrdReadDone();
327 EventFunctionWrapper dmaPrdReadEvent;
330 EventFunctionWrapper dmaReadEvent;
333 EventFunctionWrapper dmaWriteEvent;
335 // Disk image read/write
336 void readDisk(uint32_t sector, uint8_t *data);
337 void writeDisk(uint32_t sector, uint8_t *data);
339 // State machine management
340 void updateState(DevAction_t action);
343 bool isBSYSet() { return (status & STATUS_BSY_BIT); }
344 bool isIENSet() { return nIENBit; }
349 // clear out the status byte
352 status |= STATUS_DRDY_BIT;
354 status |= STATUS_SEEK_BIT;
357 uint32_t getLBABase()
359 return (Addr)(((cmdReg.head & 0xf) << 24) | (cmdReg.cyl_high << 16) |
360 (cmdReg.cyl_low << 8) | (cmdReg.sec_num));
363 inline Addr pciToDma(Addr pciAddr);
365 void serialize(CheckpointOut &cp) const override;
366 void unserialize(CheckpointIn &cp) override;
370 #endif // __DEV_STORAGE_IDE_DISK_HH__