io_device.hh revision 2914:2c524dc023d2
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Ali Saidi
29 *          Nathan Binkert
30 */
31
32#ifndef __DEV_IO_DEVICE_HH__
33#define __DEV_IO_DEVICE_HH__
34
35#include "base/chunk_generator.hh"
36#include "mem/mem_object.hh"
37#include "mem/packet_impl.hh"
38#include "sim/eventq.hh"
39#include "sim/sim_object.hh"
40#include "mem/tport.hh"
41
42class Platform;
43class PioDevice;
44class DmaDevice;
45class System;
46
47/**
48 * The PioPort class is a programmed i/o port that all devices that are
49 * sensitive to an address range use. The port takes all the memory
50 * access types and roles them into one read() and write() call that the device
51 * must respond to. The device must also provide the addressRanges() function
52 * with which it returns the address ranges it is interested in. */
53
54class PioPort : public SimpleTimingPort
55{
56  protected:
57    /** The device that this port serves. */
58    PioDevice *device;
59
60    /** The system that device/port are in. This is used to select which mode
61     * we are currently operating in. */
62    System *sys;
63
64    /** The current status of the peer(bus) that we are connected to. */
65    Status peerStatus;
66
67    virtual bool recvTiming(Packet *pkt);
68
69    virtual Tick recvAtomic(Packet *pkt);
70
71    virtual void recvFunctional(Packet *pkt) ;
72
73    virtual void recvStatusChange(Status status)
74    { peerStatus = status; }
75
76    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
77
78  public:
79    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
80
81};
82
83
84class DmaPort : public Port
85{
86  protected:
87    struct DmaReqState : public Packet::SenderState
88    {
89        /** Event to call on the device when this transaction (all packets)
90         * complete. */
91        Event *completionEvent;
92
93        /** Where we came from for some sanity checking. */
94        Port *outPort;
95
96        /** Total number of bytes that this transaction involves. */
97        Addr totBytes;
98
99        /** Number of bytes that have been acked for this transaction. */
100        Addr numBytes;
101
102        DmaReqState(Event *ce, Port *p, Addr tb)
103            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0)
104        {}
105    };
106
107    DmaDevice *device;
108    std::list<Packet*> transmitList;
109
110    /** The system that device/port are in. This is used to select which mode
111     * we are currently operating in. */
112    System *sys;
113
114    /** Number of outstanding packets the dma port has. */
115    int pendingCount;
116
117    /** If a dmaAction is in progress. */
118    int actionInProgress;
119
120    /** If we need to drain, keep the drain event around until we're done
121     * here.*/
122    Event *drainEvent;
123
124    virtual bool recvTiming(Packet *pkt);
125    virtual Tick recvAtomic(Packet *pkt)
126    { panic("dma port shouldn't be used for pio access."); }
127    virtual void recvFunctional(Packet *pkt)
128    { panic("dma port shouldn't be used for pio access."); }
129
130    virtual void recvStatusChange(Status status)
131    { ; }
132
133    virtual void recvRetry() ;
134
135    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
136    { resp.clear(); snoop.clear(); }
137
138    void sendDma(Packet *pkt, bool front = false);
139
140  public:
141    DmaPort(DmaDevice *dev, System *s);
142
143    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
144                   uint8_t *data = NULL);
145
146    bool dmaPending() { return pendingCount > 0; }
147
148    unsigned int drain(Event *de);
149};
150
151/**
152 * This device is the base class which all devices senstive to an address range
153 * inherit from. There are three pure virtual functions which all devices must
154 * implement addressRanges(), read(), and write(). The magic do choose which
155 * mode we are in, etc is handled by the PioPort so the device doesn't have to
156 * bother.
157 */
158
159class PioDevice : public MemObject
160{
161  protected:
162
163    /** The platform we are in. This is used to decide what type of memory
164     * transaction we should perform. */
165    Platform *platform;
166
167    System *sys;
168
169    /** The pioPort that handles the requests for us and provides us requests
170     * that it sees. */
171    PioPort *pioPort;
172
173    virtual void addressRanges(AddrRangeList &range_list) = 0;
174
175    /** As far as the devices are concerned they only accept atomic transactions
176     * which are converted to either a write or a read. */
177    Tick recvAtomic(Packet *pkt)
178    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
179
180    /** Pure virtual function that the device must implement. Called when a read
181     * command is recieved by the port.
182     * @param pkt Packet describing this request
183     * @return number of ticks it took to complete
184     */
185    virtual Tick read(Packet *pkt) = 0;
186
187    /** Pure virtual function that the device must implement. Called when a
188     * write command is recieved by the port.
189     * @param pkt Packet describing this request
190     * @return number of ticks it took to complete
191     */
192    virtual Tick write(Packet *pkt) = 0;
193
194  public:
195    /** Params struct which is extended through each device based on the
196     * parameters it needs. Since we are re-writing everything, we might as well
197     * start from the bottom this time. */
198
199    struct Params
200    {
201        std::string name;
202        Platform *platform;
203        System *system;
204    };
205
206  protected:
207    Params *_params;
208
209  public:
210    const Params *params() const { return _params; }
211
212    PioDevice(Params *p)
213              : MemObject(p->name),  platform(p->platform), sys(p->system),
214              pioPort(NULL), _params(p)
215              {}
216
217    virtual ~PioDevice();
218
219    virtual void init();
220
221    virtual unsigned int drain(Event *de);
222
223    virtual Port *getPort(const std::string &if_name, int idx = -1)
224    {
225        if (if_name == "pio") {
226            if (pioPort != NULL)
227                panic("pio port already connected to.");
228            pioPort = new PioPort(this, sys);
229            return pioPort;
230        } else
231            return NULL;
232    }
233    friend class PioPort;
234
235};
236
237class BasicPioDevice : public PioDevice
238{
239  public:
240    struct Params :  public PioDevice::Params
241    {
242        Addr pio_addr;
243        Tick pio_delay;
244    };
245
246  protected:
247    /** Address that the device listens to. */
248    Addr pioAddr;
249
250    /** Size that the device's address range. */
251    Addr pioSize;
252
253    /** Delay that the device experinces on an access. */
254    Tick pioDelay;
255
256  public:
257    BasicPioDevice(Params *p)
258        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
259    {}
260
261    /** return the address ranges that this device responds to.
262     * @params range_list range list to populate with ranges
263     */
264    void addressRanges(AddrRangeList &range_list);
265
266};
267
268class DmaDevice : public PioDevice
269{
270  protected:
271    DmaPort *dmaPort;
272
273  public:
274    DmaDevice(Params *p);
275    virtual ~DmaDevice();
276
277    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
278    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
279
280    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
281    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
282
283    bool dmaPending() { return dmaPort->dmaPending(); }
284
285    virtual unsigned int drain(Event *de);
286
287    virtual Port *getPort(const std::string &if_name, int idx = -1)
288    {
289        if (if_name == "pio") {
290            if (pioPort != NULL)
291                panic("pio port already connected to.");
292            pioPort = new PioPort(this, sys);
293            return pioPort;
294        } else if (if_name == "dma") {
295            if (dmaPort != NULL)
296                panic("dma port already connected to.");
297            dmaPort = new DmaPort(this, sys);
298            return dmaPort;
299        } else
300            return NULL;
301    }
302
303    friend class DmaPort;
304};
305
306
307#endif // __DEV_IO_DEVICE_HH__
308