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