ide_disk.hh revision 11169
14202Sbinkertn@umich.edu/*
24202Sbinkertn@umich.edu * Copyright (c) 2013 ARM Limited
34202Sbinkertn@umich.edu * All rights reserved
44202Sbinkertn@umich.edu *
54202Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
64202Sbinkertn@umich.edu * not be construed as granting a license to any other intellectual
74202Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84202Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94202Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
104202Sbinkertn@umich.edu * terms below provided that you ensure that this notice is replicated
114202Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124202Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134202Sbinkertn@umich.edu *
144202Sbinkertn@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
154202Sbinkertn@umich.edu * All rights reserved.
164202Sbinkertn@umich.edu *
174202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
184202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
194202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
204202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
214202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
224202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
234202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
244202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
254202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
264202Sbinkertn@umich.edu * this software without specific prior written permission.
274202Sbinkertn@umich.edu *
284202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
294202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
304202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
314202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
324202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
339157Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3410259SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
354486Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
369793Sakash.bagdia@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
379827Sakash.bagdia@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
389850Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3910249Sstephan.diestelhorst@arm.com *
4010268SGeoffrey.Blake@arm.com * Authors: Andrew Schultz
414486Sbinkertn@umich.edu */
428774Sgblack@eecs.umich.edu
434202Sbinkertn@umich.edu/** @file
4411235Sandreas.sandberg@arm.com * Device model for an IDE disk
454202Sbinkertn@umich.edu */
4611077SCurtis.Dunham@arm.com
4710458Sandreas.hansson@arm.com#ifndef __IDE_DISK_HH__
4810458Sandreas.hansson@arm.com#define __IDE_DISK_HH__
4910458Sandreas.hansson@arm.com
504202Sbinkertn@umich.edu#include "base/statistics.hh"
5112302Sgabeblack@google.com#include "dev/disk_image.hh"
524202Sbinkertn@umich.edu#include "dev/ide_atareg.h"
539983Sstever@gmail.com#include "dev/ide_ctrl.hh"
5412302Sgabeblack@google.com#include "dev/ide_wdcreg.h"
5510453SAndrew.Bardsley@arm.com#include "dev/io_device.hh"
5612302Sgabeblack@google.com#include "params/IdeDisk.hh"
5713771Sgabeblack@google.com#include "sim/eventq.hh"
5813784Sgabeblack@google.com
594202Sbinkertn@umich.educlass ChunkGenerator;
604202Sbinkertn@umich.edu
619342SAndreas.Sandberg@arm.com#define DMA_BACKOFF_PERIOD      200
624202Sbinkertn@umich.edu
634202Sbinkertn@umich.edu#define MAX_DMA_SIZE            0x20000  // 128K
6410268SGeoffrey.Blake@arm.com#define MAX_SINGLE_DMA_SIZE     0x10000
6510259SAndrew.Bardsley@arm.com#define MAX_MULTSECT            (128)
664202Sbinkertn@umich.edu
674202Sbinkertn@umich.edu#define PRD_BASE_MASK  0xfffffffe
6812302Sgabeblack@google.com#define PRD_COUNT_MASK 0xfffe
699793Sakash.bagdia@arm.com#define PRD_EOT_MASK   0x8000
709827Sakash.bagdia@arm.com
7111909SBrandon.Potter@amd.comtypedef struct PrdEntry {
7211420Sdavid.guillen@arm.com    uint32_t baseAddr;
739850Sandreas.hansson@arm.com    uint16_t byteCount;
7410249Sstephan.diestelhorst@arm.com    uint16_t endOfTable;
7511524Sdavid.guillen@arm.com} PrdEntry_t;
7611527Sdavid.guillen@arm.com
777768SAli.Saidi@ARM.comclass PrdTableEntry {
789850Sandreas.hansson@arm.com  public:
799850Sandreas.hansson@arm.com    PrdEntry_t entry;
808766Sgblack@eecs.umich.edu
8111854Sbrandon.potter@amd.com    uint32_t getBaseAddr()
827768SAli.Saidi@ARM.com    {
838766Sgblack@eecs.umich.edu        return (entry.baseAddr & PRD_BASE_MASK);
8411856Sbrandon.potter@amd.com    }
8510930Sbrandon.potter@amd.com
867768SAli.Saidi@ARM.com    uint32_t getByteCount()
879850Sandreas.hansson@arm.com    {
8811794Sbrandon.potter@amd.com        return ((entry.byteCount == 0) ? MAX_SINGLE_DMA_SIZE :
894486Sbinkertn@umich.edu                (entry.byteCount & PRD_COUNT_MASK));
9011800Sbrandon.potter@amd.com    }
9111800Sbrandon.potter@amd.com
9211800Sbrandon.potter@amd.com    uint16_t getEOT()
938335Snate@binkert.org    {
948335Snate@binkert.org        return (entry.endOfTable & PRD_EOT_MASK);
9510458Sandreas.hansson@arm.com    }
969152Satgutier@umich.edu};
978335Snate@binkert.org
988335Snate@binkert.org#define DATA_OFFSET     (0)
998335Snate@binkert.org#define ERROR_OFFSET    (1)
1008335Snate@binkert.org#define FEATURES_OFFSET (1)
1018335Snate@binkert.org#define NSECTOR_OFFSET  (2)
1028335Snate@binkert.org#define SECTOR_OFFSET   (3)
1038335Snate@binkert.org#define LCYL_OFFSET     (4)
1049733Sandreas@sandberg.pp.se#define HCYL_OFFSET     (5)
1058335Snate@binkert.org#define SELECT_OFFSET   (6)
10611380Salexandru.dutu@amd.com#define DRIVE_OFFSET    (6)
1078335Snate@binkert.org#define STATUS_OFFSET   (7)
1088335Snate@binkert.org#define COMMAND_OFFSET  (7)
1098335Snate@binkert.org
1108335Snate@binkert.org#define CONTROL_OFFSET  (2)
1118335Snate@binkert.org#define ALTSTAT_OFFSET  (2)
1128335Snate@binkert.org
1139793Sakash.bagdia@arm.com#define SELECT_DEV_BIT  0x10
1149827Sakash.bagdia@arm.com#define CONTROL_RST_BIT 0x04
11510249Sstephan.diestelhorst@arm.com#define CONTROL_IEN_BIT 0x02
11611380Salexandru.dutu@amd.com#define STATUS_BSY_BIT  0x80
11711380Salexandru.dutu@amd.com#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
122
123#define DEV0 (0)
124#define DEV1 (1)
125
126typedef struct CommandReg {
127    uint16_t data;
128    uint8_t error;
129    uint8_t sec_count;
130    uint8_t sec_num;
131    uint8_t cyl_low;
132    uint8_t cyl_high;
133    union {
134        uint8_t drive;
135        uint8_t head;
136    };
137    uint8_t command;
138} CommandReg_t;
139
140typedef enum Events {
141    None = 0,
142    Transfer,
143    ReadWait,
144    WriteWait,
145    PrdRead,
146    DmaRead,
147    DmaWrite
148} Events_t;
149
150typedef enum DevAction {
151    ACT_NONE = 0,
152    ACT_CMD_WRITE,
153    ACT_CMD_COMPLETE,
154    ACT_CMD_ERROR,
155    ACT_SELECT_WRITE,
156    ACT_STAT_READ,
157    ACT_DATA_READY,
158    ACT_DATA_READ_BYTE,
159    ACT_DATA_READ_SHORT,
160    ACT_DATA_WRITE_BYTE,
161    ACT_DATA_WRITE_SHORT,
162    ACT_DMA_READY,
163    ACT_DMA_DONE,
164    ACT_SRST_SET,
165    ACT_SRST_CLEAR
166} DevAction_t;
167
168typedef enum DevState {
169    // Device idle
170    Device_Idle_S = 0,
171    Device_Idle_SI,
172    Device_Idle_NS,
173
174    // Software reset
175    Device_Srst,
176
177    // Non-data commands
178    Command_Execution,
179
180    // PIO data-in (data to host)
181    Prepare_Data_In,
182    Data_Ready_INTRQ_In,
183    Transfer_Data_In,
184
185    // PIO data-out (data from host)
186    Prepare_Data_Out,
187    Data_Ready_INTRQ_Out,
188    Transfer_Data_Out,
189
190    // DMA protocol
191    Prepare_Data_Dma,
192    Transfer_Data_Dma,
193    Device_Dma_Abort
194} DevState_t;
195
196typedef enum DmaState {
197    Dma_Idle = 0,
198    Dma_Start,
199    Dma_Transfer
200} DmaState_t;
201
202class IdeController;
203
204/**
205 * IDE Disk device model
206 */
207class IdeDisk : public SimObject
208{
209  protected:
210    /** The IDE controller for this disk. */
211    IdeController *ctrl;
212    /** The image that contains the data of this disk. */
213    DiskImage *image;
214
215  protected:
216    /** The disk delay in microseconds. */
217    int diskDelay;
218
219  private:
220    /** Drive identification structure for this disk */
221    struct ataparams driveID;
222    /** Data buffer for transfers */
223    uint8_t *dataBuffer;
224    /** Number of bytes in command data transfer */
225    uint32_t cmdBytes;
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 */
231    uint32_t curSector;
232    /** Command block registers */
233    CommandReg_t cmdReg;
234    /** Status register */
235    uint8_t status;
236    /** Interrupt enable bit */
237    bool nIENBit;
238    /** Device state */
239    DevState_t devState;
240    /** Dma state */
241    DmaState_t dmaState;
242    /** Dma transaction is a read */
243    bool dmaRead;
244    /** PRD table base address */
245    uint32_t curPrdAddr;
246    /** PRD entry */
247    PrdTableEntry curPrd;
248    /** Device ID (master=0/slave=1) */
249    int devID;
250    /** Interrupt pending */
251    bool intrPending;
252    /** DMA Aborted */
253    bool dmaAborted;
254
255    Stats::Scalar dmaReadFullPages;
256    Stats::Scalar dmaReadBytes;
257    Stats::Scalar dmaReadTxs;
258    Stats::Scalar dmaWriteFullPages;
259    Stats::Scalar dmaWriteBytes;
260    Stats::Scalar dmaWriteTxs;
261
262  public:
263    typedef IdeDiskParams Params;
264    IdeDisk(const Params *p);
265
266    /**
267     * Delete the data buffer.
268     */
269    ~IdeDisk();
270
271    /**
272     * Reset the device state
273     */
274    void reset(int id);
275
276    /**
277     * Register Statistics
278     */
279    void regStats() override;
280
281    /**
282     * Set the controller for this device
283     * @param c The IDE controller
284     */
285    void setController(IdeController *c) {
286        if (ctrl) panic("Cannot change the controller once set!\n");
287        ctrl = c;
288    }
289
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);
295
296    // Start/abort functions
297    void startDma(const uint32_t &prdTableBase);
298    void abortDma();
299
300  private:
301    void startCommand();
302
303    // Interrupt management
304    void intrPost();
305    void intrClear();
306
307    // DMA stuff
308    void doDmaTransfer();
309    friend class EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer>;
310    EventWrapper<IdeDisk, &IdeDisk::doDmaTransfer> dmaTransferEvent;
311
312    void doDmaDataRead();
313
314    void doDmaRead();
315    ChunkGenerator *dmaReadCG;
316    friend class EventWrapper<IdeDisk, &IdeDisk::doDmaRead>;
317    EventWrapper<IdeDisk, &IdeDisk::doDmaRead> dmaReadWaitEvent;
318
319    void doDmaDataWrite();
320
321    void doDmaWrite();
322    ChunkGenerator *dmaWriteCG;
323    friend class EventWrapper<IdeDisk, &IdeDisk::doDmaWrite>;
324    EventWrapper<IdeDisk, &IdeDisk::doDmaWrite> dmaWriteWaitEvent;
325
326    void dmaPrdReadDone();
327    friend class EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone>;
328    EventWrapper<IdeDisk, &IdeDisk::dmaPrdReadDone> dmaPrdReadEvent;
329
330    void dmaReadDone();
331    friend class EventWrapper<IdeDisk, &IdeDisk::dmaReadDone>;
332    EventWrapper<IdeDisk, &IdeDisk::dmaReadDone> dmaReadEvent;
333
334    void dmaWriteDone();
335    friend class EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone>;
336    EventWrapper<IdeDisk, &IdeDisk::dmaWriteDone> dmaWriteEvent;
337
338    // Disk image read/write
339    void readDisk(uint32_t sector, uint8_t *data);
340    void writeDisk(uint32_t sector, uint8_t *data);
341
342    // State machine management
343    void updateState(DevAction_t action);
344
345    // Utility functions
346    bool isBSYSet() { return (status & STATUS_BSY_BIT); }
347    bool isIENSet() { return nIENBit; }
348    bool isDEVSelect();
349
350    void setComplete()
351    {
352        // clear out the status byte
353        status = 0;
354        // set the DRDY bit
355        status |= STATUS_DRDY_BIT;
356        // set the SEEK bit
357        status |= STATUS_SEEK_BIT;
358    }
359
360    uint32_t getLBABase()
361    {
362        return  (Addr)(((cmdReg.head & 0xf) << 24) | (cmdReg.cyl_high << 16) |
363                       (cmdReg.cyl_low << 8) | (cmdReg.sec_num));
364    }
365
366    inline Addr pciToDma(Addr pciAddr);
367
368    void serialize(CheckpointOut &cp) const override;
369    void unserialize(CheckpointIn &cp) override;
370};
371
372
373#endif // __IDE_DISK_HH__
374