io_device.hh revision 2738:5d7a31c7fa29
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/chunk_generator.hh"
36#include "mem/mem_object.hh"
37#include "mem/packet_impl.hh"
38#include "sim/eventq.hh"
39#include "sim/sim_object.hh"
40
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. An extra
52 * sendTiming() function is implemented which takes an delay. In this way the
53 * device can immediatly call sendTiming(pkt, time) after processing a request
54 * and the request will be handled by the port even if the port bus the device
55 * connects to is blocked.
56 */
57class PioPort : public Port
58{
59  protected:
60    /** The device that this port serves. */
61    PioDevice *device;
62
63    /** The platform that device/port are in. This is used to select which mode
64     * we are currently operating in. */
65    Platform *platform;
66
67    /** A list of outgoing timing response packets that haven't been serviced
68     * yet. */
69    std::list<Packet*> transmitList;
70
71    /** The current status of the peer(bus) that we are connected to. */
72    Status peerStatus;
73
74    virtual bool recvTiming(Packet *pkt);
75
76    virtual Tick recvAtomic(Packet *pkt);
77
78    virtual void recvFunctional(Packet *pkt) ;
79
80    virtual void recvStatusChange(Status status)
81    { peerStatus = status; }
82
83    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
84
85    /**
86     * This class is used to implemented sendTiming() with a delay. When a delay
87     * is requested a new event is created. When the event time expires it
88     * attempts to send the packet. If it cannot, the packet is pushed onto the
89     * transmit list to be sent when recvRetry() is called. */
90    class SendEvent : public Event
91    {
92        PioPort *port;
93        Packet *packet;
94
95        SendEvent(PioPort *p, Packet *pkt, Tick t)
96            : Event(&mainEventQueue), port(p), packet(pkt)
97        { schedule(curTick + t); }
98
99        virtual void process();
100
101        virtual const char *description()
102        { return "Future scheduled sendTiming event"; }
103
104        friend class PioPort;
105    };
106
107    /** Schedule a sendTiming() event to be called in the future. */
108    void sendTiming(Packet *pkt, Tick time)
109    { new PioPort::SendEvent(this, pkt, time); }
110
111    /** This function is notification that the device should attempt to send a
112     * packet again. */
113    virtual void recvRetry();
114
115  public:
116    PioPort(PioDevice *dev, Platform *p);
117
118  friend class PioPort::SendEvent;
119};
120
121
122struct DmaReqState : public Packet::SenderState
123{
124    /** Event to call on the device when this transaction (all packets)
125     * complete. */
126    Event *completionEvent;
127
128    /** Where we came from for some sanity checking. */
129    Port *outPort;
130
131    /** Total number of bytes that this transaction involves. */
132    Addr totBytes;
133
134    /** Number of bytes that have been acked for this transaction. */
135    Addr numBytes;
136
137    bool final;
138    DmaReqState(Event *ce, Port *p, Addr tb)
139        : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0)
140    {}
141};
142
143class DmaPort : public Port
144{
145  protected:
146    DmaDevice *device;
147    std::list<Packet*> transmitList;
148
149    /** The platform that device/port are in. This is used to select which mode
150     * we are currently operating in. */
151    Platform *platform;
152
153    /** Number of outstanding packets the dma port has. */
154    int pendingCount;
155
156    virtual bool recvTiming(Packet *pkt);
157    virtual Tick recvAtomic(Packet *pkt)
158    { panic("dma port shouldn't be used for pio access."); }
159    virtual void recvFunctional(Packet *pkt)
160    { panic("dma port shouldn't be used for pio access."); }
161
162    virtual void recvStatusChange(Status status)
163    { ; }
164
165    virtual void recvRetry() ;
166
167    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
168    { resp.clear(); snoop.clear(); }
169
170    void sendDma(Packet *pkt, bool front = false);
171
172  public:
173    DmaPort(DmaDevice *dev, Platform *p);
174
175    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
176                   uint8_t *data = NULL);
177
178    bool dmaPending() { return pendingCount > 0; }
179
180};
181
182/**
183 * This device is the base class which all devices senstive to an address range
184 * inherit from. There are three pure virtual functions which all devices must
185 * implement addressRanges(), read(), and write(). The magic do choose which
186 * mode we are in, etc is handled by the PioPort so the device doesn't have to
187 * bother.
188 */
189
190class PioDevice : public MemObject
191{
192  protected:
193
194    /** The platform we are in. This is used to decide what type of memory
195     * transaction we should perform. */
196    Platform *platform;
197
198    /** The pioPort that handles the requests for us and provides us requests
199     * that it sees. */
200    PioPort *pioPort;
201
202    virtual void addressRanges(AddrRangeList &range_list) = 0;
203
204    /** As far as the devices are concerned they only accept atomic transactions
205     * which are converted to either a write or a read. */
206    Tick recvAtomic(Packet *pkt)
207    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
208
209    /** Pure virtual function that the device must implement. Called when a read
210     * 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(Packet *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(Packet *pkt) = 0;
222
223  public:
224    /** Params struct which is extended through each device based on the
225     * parameters it needs. Since we are re-writing everything, we might as well
226     * start from the bottom this time. */
227
228    struct Params
229    {
230        std::string name;
231        Platform *platform;
232        System *system;
233    };
234
235  protected:
236    Params *_params;
237
238  public:
239    const Params *params() const { return _params; }
240
241    PioDevice(Params *p)
242              : MemObject(p->name),  platform(p->platform), pioPort(NULL),
243                _params(p)
244              {}
245
246    virtual ~PioDevice();
247
248    virtual void init();
249
250    virtual Port *getPort(const std::string &if_name, int idx = -1)
251    {
252        if (if_name == "pio") {
253            if (pioPort != NULL)
254                panic("pio port already connected to.");
255            pioPort = new PioPort(this, params()->platform);
256            return pioPort;
257        } else
258            return NULL;
259    }
260    friend class PioPort;
261
262};
263
264class BasicPioDevice : public PioDevice
265{
266  public:
267    struct Params :  public PioDevice::Params
268    {
269        Addr pio_addr;
270        Tick pio_delay;
271    };
272
273  protected:
274    /** Address that the device listens to. */
275    Addr pioAddr;
276
277    /** Size that the device's address range. */
278    Addr pioSize;
279
280    /** Delay that the device experinces on an access. */
281    Tick pioDelay;
282
283  public:
284    BasicPioDevice(Params *p)
285        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
286    {}
287
288    /** return the address ranges that this device responds to.
289     * @params range_list range list to populate with ranges
290     */
291    void addressRanges(AddrRangeList &range_list);
292
293};
294
295class DmaDevice : public PioDevice
296{
297  protected:
298    DmaPort *dmaPort;
299
300  public:
301    DmaDevice(Params *p);
302    virtual ~DmaDevice();
303
304    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
305    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
306
307    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
308    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
309
310    bool dmaPending() { return dmaPort->dmaPending(); }
311
312    virtual Port *getPort(const std::string &if_name, int idx = -1)
313    {
314        if (if_name == "pio") {
315            if (pioPort != NULL)
316                panic("pio port already connected to.");
317            pioPort = new PioPort(this, params()->platform);
318            return pioPort;
319        } else if (if_name == "dma") {
320            if (dmaPort != NULL)
321                panic("dma port already connected to.");
322            dmaPort = new DmaPort(this, params()->platform);
323            return dmaPort;
324        } else
325            return NULL;
326    }
327
328    friend class DmaPort;
329};
330
331
332#endif // __DEV_IO_DEVICE_HH__
333