io_device.hh revision 8832:247fee427324
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/fast_alloc.hh"
36#include "mem/mem_object.hh"
37#include "mem/packet.hh"
38#include "mem/tport.hh"
39#include "params/BasicPioDevice.hh"
40#include "params/DmaDevice.hh"
41#include "params/PioDevice.hh"
42#include "sim/sim_object.hh"
43
44class Event;
45class PioDevice;
46class DmaDevice;
47class System;
48
49/**
50 * The PioPort class is a programmed i/o port that all devices that are
51 * sensitive to an address range use. The port takes all the memory
52 * access types and roles them into one read() and write() call that the device
53 * must respond to. The device must also provide getAddrRanges() function
54 * with which it returns the address ranges it is interested in.
55 */
56class PioPort : public SimpleTimingPort
57{
58  protected:
59    /** The device that this port serves. */
60    PioDevice *device;
61
62    virtual Tick recvAtomic(PacketPtr pkt);
63
64    virtual AddrRangeList getAddrRanges();
65
66  public:
67
68    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
69};
70
71
72class DmaPort : public Port
73{
74  protected:
75    struct DmaReqState : public Packet::SenderState, public FastAlloc
76    {
77        /** Event to call on the device when this transaction (all packets)
78         * complete. */
79        Event *completionEvent;
80
81        /** Where we came from for some sanity checking. */
82        Port *outPort;
83
84        /** Total number of bytes that this transaction involves. */
85        Addr totBytes;
86
87        /** Number of bytes that have been acked for this transaction. */
88        Addr numBytes;
89
90        /** Amount to delay completion of dma by */
91        Tick delay;
92
93
94        DmaReqState(Event *ce, Port *p, Addr tb, Tick _delay)
95            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0),
96              delay(_delay)
97        {}
98    };
99
100    MemObject *device;
101    std::list<PacketPtr> transmitList;
102
103    /** The system that device/port are in. This is used to select which mode
104     * we are currently operating in. */
105    System *sys;
106
107    /** Id for all requests */
108    MasterID masterId;
109
110    /** Number of outstanding packets the dma port has. */
111    int pendingCount;
112
113    /** If a dmaAction is in progress. */
114    int actionInProgress;
115
116    /** If we need to drain, keep the drain event around until we're done
117     * here.*/
118    Event *drainEvent;
119
120    /** time to wait between sending another packet, increases as NACKs are
121     * recived, decreases as responses are recived. */
122    Tick backoffTime;
123
124    /** Minimum time that device should back off for after failed sendTiming */
125    Tick minBackoffDelay;
126
127    /** Maximum time that device should back off for after failed sendTiming */
128    Tick maxBackoffDelay;
129
130    /** If the port is currently waiting for a retry before it can send whatever
131     * it is that it's sending. */
132    bool inRetry;
133
134    /** Port accesses a cache which requires snooping */
135    bool recvSnoops;
136
137    virtual bool recvTiming(PacketPtr pkt);
138    virtual Tick recvAtomic(PacketPtr pkt)
139    {
140        if (recvSnoops) return 0;
141
142        panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN
143    }
144    virtual void recvFunctional(PacketPtr pkt)
145    {
146        if (recvSnoops) return;
147
148        panic("dma port shouldn't be used for pio access.");
149    }
150
151    virtual void recvRangeChange()
152    {
153        // DMA port is a master with a single slave so there is no choice and
154        // thus no need to worry about any address changes
155    }
156
157    virtual void recvRetry() ;
158
159    virtual bool isSnooping()
160    { return recvSnoops; }
161
162    void queueDma(PacketPtr pkt, bool front = false);
163    void sendDma();
164
165    /** event to give us a kick every time we backoff time is reached. */
166    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
167
168  public:
169    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
170            bool recv_snoops = false);
171
172    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
173                   uint8_t *data, Tick delay, Request::Flags flag = 0);
174
175    bool dmaPending() { return pendingCount > 0; }
176
177    unsigned cacheBlockSize() const { return peerBlockSize(); }
178    unsigned int drain(Event *de);
179};
180
181/**
182 * This device is the base class which all devices senstive to an address range
183 * inherit from. There are three pure virtual functions which all devices must
184 * implement getAddrRanges(), read(), and write(). The magic do choose which
185 * mode we are in, etc is handled by the PioPort so the device doesn't have to
186 * bother.
187 */
188class PioDevice : public MemObject
189{
190  protected:
191    System *sys;
192
193    /** The pioPort that handles the requests for us and provides us requests
194     * that it sees. */
195    PioPort *pioPort;
196
197    /**
198     * Every PIO device is obliged to provide an implementation that
199     * returns the address ranges the device responds to.
200     *
201     * @return a list of non-overlapping address ranges
202     */
203    virtual AddrRangeList getAddrRanges() = 0;
204
205    /** Pure virtual function that the device must implement. Called
206     * when a read command is recieved by the port.
207     * @param pkt Packet describing this request
208     * @return number of ticks it took to complete
209     */
210    virtual Tick read(PacketPtr pkt) = 0;
211
212    /** Pure virtual function that the device must implement. Called when a
213     * write command is recieved by the port.
214     * @param pkt Packet describing this request
215     * @return number of ticks it took to complete
216     */
217    virtual Tick write(PacketPtr pkt) = 0;
218
219  public:
220    typedef PioDeviceParams Params;
221    PioDevice(const Params *p);
222    virtual ~PioDevice();
223
224    const Params *
225    params() const
226    {
227        return dynamic_cast<const Params *>(_params);
228    }
229
230    virtual void init();
231
232    virtual unsigned int drain(Event *de);
233
234    virtual Port *getPort(const std::string &if_name, int idx = -1);
235
236    friend class PioPort;
237
238};
239
240class BasicPioDevice : public PioDevice
241{
242  protected:
243    /** Address that the device listens to. */
244    Addr pioAddr;
245
246    /** Size that the device's address range. */
247    Addr pioSize;
248
249    /** Delay that the device experinces on an access. */
250    Tick pioDelay;
251
252  public:
253    typedef BasicPioDeviceParams Params;
254    BasicPioDevice(const Params *p);
255
256    const Params *
257    params() const
258    {
259        return dynamic_cast<const Params *>(_params);
260    }
261
262    /**
263     * Determine the address ranges that this device responds to.
264     *
265     * @return a list of non-overlapping address ranges
266     */
267    virtual AddrRangeList getAddrRanges();
268
269};
270
271class DmaDevice : public PioDevice
272{
273   protected:
274    DmaPort *dmaPort;
275
276  public:
277    typedef DmaDeviceParams Params;
278    DmaDevice(const Params *p);
279    virtual ~DmaDevice();
280
281    const Params *
282    params() const
283    {
284        return dynamic_cast<const Params *>(_params);
285    }
286
287    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
288    {
289        dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
290    }
291
292    void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
293    {
294        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
295    }
296
297    bool dmaPending() { return dmaPort->dmaPending(); }
298
299    virtual unsigned int drain(Event *de);
300
301    unsigned cacheBlockSize() const { return dmaPort->cacheBlockSize(); }
302
303    virtual Port *getPort(const std::string &if_name, int idx = -1);
304
305    friend class DmaPort;
306};
307
308
309#endif // __DEV_IO_DEVICE_HH__
310