io_device.hh revision 8796:a2ae5c378d0a
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 the addressRanges() 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 void getDeviceAddressRanges(AddrRangeList &resp,
65                                        bool &snoop);
66
67  public:
68
69    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
70};
71
72
73class DmaPort : public Port
74{
75  protected:
76    struct DmaReqState : public Packet::SenderState, public FastAlloc
77    {
78        /** Event to call on the device when this transaction (all packets)
79         * complete. */
80        Event *completionEvent;
81
82        /** Where we came from for some sanity checking. */
83        Port *outPort;
84
85        /** Total number of bytes that this transaction involves. */
86        Addr totBytes;
87
88        /** Number of bytes that have been acked for this transaction. */
89        Addr numBytes;
90
91        /** Amount to delay completion of dma by */
92        Tick delay;
93
94
95        DmaReqState(Event *ce, Port *p, Addr tb, Tick _delay)
96            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0),
97              delay(_delay)
98        {}
99    };
100
101    MemObject *device;
102    std::list<PacketPtr> transmitList;
103
104    /** The system that device/port are in. This is used to select which mode
105     * we are currently operating in. */
106    System *sys;
107
108    /** Number of outstanding packets the dma port has. */
109    int pendingCount;
110
111    /** If a dmaAction is in progress. */
112    int actionInProgress;
113
114    /** If we need to drain, keep the drain event around until we're done
115     * here.*/
116    Event *drainEvent;
117
118    /** time to wait between sending another packet, increases as NACKs are
119     * recived, decreases as responses are recived. */
120    Tick backoffTime;
121
122    /** Minimum time that device should back off for after failed sendTiming */
123    Tick minBackoffDelay;
124
125    /** Maximum time that device should back off for after failed sendTiming */
126    Tick maxBackoffDelay;
127
128    /** If the port is currently waiting for a retry before it can send whatever
129     * it is that it's sending. */
130    bool inRetry;
131
132    /** Port accesses a cache which requires snooping */
133    bool recvSnoops;
134
135    /** Records snoop response so we only reply once to a status change */
136    bool snoopRangeSent;
137
138    virtual bool recvTiming(PacketPtr pkt);
139    virtual Tick recvAtomic(PacketPtr pkt)
140    {
141        if (recvSnoops) return 0;
142
143        panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN
144    }
145    virtual void recvFunctional(PacketPtr pkt)
146    {
147        if (recvSnoops) return;
148
149        panic("dma port shouldn't be used for pio access.");
150    }
151
152    virtual void recvStatusChange(Status status)
153    {
154        if (recvSnoops) {
155            if (status == RangeChange) {
156                if (!snoopRangeSent) {
157                    snoopRangeSent = true;
158                    sendStatusChange(Port::RangeChange);
159                }
160                return;
161            }
162            panic("Unexpected recvStatusChange\n");
163        }
164    }
165
166    virtual void recvRetry() ;
167
168    virtual void getDeviceAddressRanges(AddrRangeList &resp,
169                                        bool &snoop)
170    { resp.clear(); snoop = recvSnoops; }
171
172    void queueDma(PacketPtr pkt, bool front = false);
173    void sendDma();
174
175    /** event to give us a kick every time we backoff time is reached. */
176    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
177
178  public:
179    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
180            bool recv_snoops = false);
181
182    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
183                   uint8_t *data, Tick delay, Request::Flags flag = 0);
184
185    bool dmaPending() { return pendingCount > 0; }
186
187    unsigned cacheBlockSize() const { return peerBlockSize(); }
188    unsigned int drain(Event *de);
189};
190
191/**
192 * This device is the base class which all devices senstive to an address range
193 * inherit from. There are three pure virtual functions which all devices must
194 * implement addressRanges(), read(), and write(). The magic do choose which
195 * mode we are in, etc is handled by the PioPort so the device doesn't have to
196 * bother.
197 */
198class PioDevice : public MemObject
199{
200  protected:
201    System *sys;
202
203    /** The pioPort that handles the requests for us and provides us requests
204     * that it sees. */
205    PioPort *pioPort;
206
207    virtual void addressRanges(AddrRangeList &range_list) = 0;
208
209    /** Pure virtual function that the device must implement. Called
210     * when a read 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 read(PacketPtr pkt) = 0;
215
216    /** Pure virtual function that the device must implement. Called when a
217     * write command is recieved by the port.
218     * @param pkt Packet describing this request
219     * @return number of ticks it took to complete
220     */
221    virtual Tick write(PacketPtr pkt) = 0;
222
223  public:
224    typedef PioDeviceParams Params;
225    PioDevice(const Params *p);
226    virtual ~PioDevice();
227
228    const Params *
229    params() const
230    {
231        return dynamic_cast<const Params *>(_params);
232    }
233
234    virtual void init();
235
236    virtual unsigned int drain(Event *de);
237
238    virtual Port *getPort(const std::string &if_name, int idx = -1);
239
240    friend class PioPort;
241
242};
243
244class BasicPioDevice : public PioDevice
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    typedef BasicPioDeviceParams Params;
258    BasicPioDevice(const Params *p);
259
260    const Params *
261    params() const
262    {
263        return dynamic_cast<const Params *>(_params);
264    }
265
266    /** return the address ranges that this device responds to.
267     * @param range_list range list to populate with ranges
268     */
269    void addressRanges(AddrRangeList &range_list);
270
271};
272
273class DmaDevice : public PioDevice
274{
275   protected:
276    DmaPort *dmaPort;
277
278  public:
279    typedef DmaDeviceParams Params;
280    DmaDevice(const Params *p);
281    virtual ~DmaDevice();
282
283    const Params *
284    params() const
285    {
286        return dynamic_cast<const Params *>(_params);
287    }
288
289    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
290    {
291        dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
292    }
293
294    void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
295    {
296        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
297    }
298
299    bool dmaPending() { return dmaPort->dmaPending(); }
300
301    virtual unsigned int drain(Event *de);
302
303    unsigned cacheBlockSize() const { return dmaPort->cacheBlockSize(); }
304
305    virtual Port *getPort(const std::string &if_name, int idx = -1);
306
307    friend class DmaPort;
308};
309
310
311#endif // __DEV_IO_DEVICE_HH__
312