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