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