io_device.hh revision 8799:dac1e33e07b0
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    /** Number of outstanding packets the dma port has. */
108    int pendingCount;
109
110    /** If a dmaAction is in progress. */
111    int actionInProgress;
112
113    /** If we need to drain, keep the drain event around until we're done
114     * here.*/
115    Event *drainEvent;
116
117    /** time to wait between sending another packet, increases as NACKs are
118     * recived, decreases as responses are recived. */
119    Tick backoffTime;
120
121    /** Minimum time that device should back off for after failed sendTiming */
122    Tick minBackoffDelay;
123
124    /** Maximum time that device should back off for after failed sendTiming */
125    Tick maxBackoffDelay;
126
127    /** If the port is currently waiting for a retry before it can send whatever
128     * it is that it's sending. */
129    bool inRetry;
130
131    /** Port accesses a cache which requires snooping */
132    bool recvSnoops;
133
134    virtual bool recvTiming(PacketPtr pkt);
135    virtual Tick recvAtomic(PacketPtr pkt)
136    {
137        if (recvSnoops) return 0;
138
139        panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN
140    }
141    virtual void recvFunctional(PacketPtr pkt)
142    {
143        if (recvSnoops) return;
144
145        panic("dma port shouldn't be used for pio access.");
146    }
147
148    virtual void recvRangeChange()
149    {
150        // DMA port is a master with a single slave so there is no choice and
151        // thus no need to worry about any address changes
152    }
153
154    virtual void recvRetry() ;
155
156    virtual bool isSnooping()
157    { return recvSnoops; }
158
159    void queueDma(PacketPtr pkt, bool front = false);
160    void sendDma();
161
162    /** event to give us a kick every time we backoff time is reached. */
163    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
164
165  public:
166    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
167            bool recv_snoops = false);
168
169    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
170                   uint8_t *data, Tick delay, Request::Flags flag = 0);
171
172    bool dmaPending() { return pendingCount > 0; }
173
174    unsigned cacheBlockSize() const { return peerBlockSize(); }
175    unsigned int drain(Event *de);
176};
177
178/**
179 * This device is the base class which all devices senstive to an address range
180 * inherit from. There are three pure virtual functions which all devices must
181 * implement getAddrRanges(), read(), and write(). The magic do choose which
182 * mode we are in, etc is handled by the PioPort so the device doesn't have to
183 * bother.
184 */
185class PioDevice : public MemObject
186{
187  protected:
188    System *sys;
189
190    /** The pioPort that handles the requests for us and provides us requests
191     * that it sees. */
192    PioPort *pioPort;
193
194    /**
195     * Every PIO device is obliged to provide an implementation that
196     * returns the address ranges the device responds to.
197     *
198     * @return a list of non-overlapping address ranges
199     */
200    virtual AddrRangeList getAddrRanges() = 0;
201
202    /** Pure virtual function that the device must implement. Called
203     * when a read command is recieved by the port.
204     * @param pkt Packet describing this request
205     * @return number of ticks it took to complete
206     */
207    virtual Tick read(PacketPtr pkt) = 0;
208
209    /** Pure virtual function that the device must implement. Called when a
210     * write command is recieved by the port.
211     * @param pkt Packet describing this request
212     * @return number of ticks it took to complete
213     */
214    virtual Tick write(PacketPtr pkt) = 0;
215
216  public:
217    typedef PioDeviceParams Params;
218    PioDevice(const Params *p);
219    virtual ~PioDevice();
220
221    const Params *
222    params() const
223    {
224        return dynamic_cast<const Params *>(_params);
225    }
226
227    virtual void init();
228
229    virtual unsigned int drain(Event *de);
230
231    virtual Port *getPort(const std::string &if_name, int idx = -1);
232
233    friend class PioPort;
234
235};
236
237class BasicPioDevice : public PioDevice
238{
239  protected:
240    /** Address that the device listens to. */
241    Addr pioAddr;
242
243    /** Size that the device's address range. */
244    Addr pioSize;
245
246    /** Delay that the device experinces on an access. */
247    Tick pioDelay;
248
249  public:
250    typedef BasicPioDeviceParams Params;
251    BasicPioDevice(const Params *p);
252
253    const Params *
254    params() const
255    {
256        return dynamic_cast<const Params *>(_params);
257    }
258
259    /**
260     * Determine the address ranges that this device responds to.
261     *
262     * @return a list of non-overlapping address ranges
263     */
264    virtual AddrRangeList getAddrRanges();
265
266};
267
268class DmaDevice : public PioDevice
269{
270   protected:
271    DmaPort *dmaPort;
272
273  public:
274    typedef DmaDeviceParams Params;
275    DmaDevice(const Params *p);
276    virtual ~DmaDevice();
277
278    const Params *
279    params() const
280    {
281        return dynamic_cast<const Params *>(_params);
282    }
283
284    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
285    {
286        dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
287    }
288
289    void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
290    {
291        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
292    }
293
294    bool dmaPending() { return dmaPort->dmaPending(); }
295
296    virtual unsigned int drain(Event *de);
297
298    unsigned cacheBlockSize() const { return dmaPort->cacheBlockSize(); }
299
300    virtual Port *getPort(const std::string &if_name, int idx = -1);
301
302    friend class DmaPort;
303};
304
305
306#endif // __DEV_IO_DEVICE_HH__
307