io_device.hh revision 2665
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 *completionEvent;
125    bool final;
126    DmaReqState(Event *ce, bool f)
127        : completionEvent(ce), final(f)
128    {}
129};
130
131class DmaPort : public Port
132{
133  protected:
134    DmaDevice *device;
135    std::list<Packet*> transmitList;
136
137    /** The platform that device/port are in. This is used to select which mode
138     * we are currently operating in. */
139    Platform *platform;
140
141    /** Number of outstanding packets the dma port has. */
142    int pendingCount;
143
144    virtual bool recvTiming(Packet *pkt);
145    virtual Tick recvAtomic(Packet *pkt)
146    { panic("dma port shouldn't be used for pio access."); }
147    virtual void recvFunctional(Packet *pkt)
148    { panic("dma port shouldn't be used for pio access."); }
149
150    virtual void recvStatusChange(Status status)
151    { ; }
152
153    virtual void recvRetry() ;
154
155    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
156    { resp.clear(); snoop.clear(); }
157
158    void sendDma(Packet *pkt);
159
160  public:
161    DmaPort(DmaDevice *dev, Platform *p);
162
163    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
164                   uint8_t *data = NULL);
165
166    bool dmaPending() { return pendingCount > 0; }
167
168};
169
170/**
171 * This device is the base class which all devices senstive to an address range
172 * inherit from. There are three pure virtual functions which all devices must
173 * implement addressRanges(), read(), and write(). The magic do choose which
174 * mode we are in, etc is handled by the PioPort so the device doesn't have to
175 * bother.
176 */
177
178class PioDevice : public MemObject
179{
180  protected:
181
182    /** The platform we are in. This is used to decide what type of memory
183     * transaction we should perform. */
184    Platform *platform;
185
186    /** The pioPort that handles the requests for us and provides us requests
187     * that it sees. */
188    PioPort *pioPort;
189
190    virtual void addressRanges(AddrRangeList &range_list) = 0;
191
192    /** As far as the devices are concerned they only accept atomic transactions
193     * which are converted to either a write or a read. */
194    Tick recvAtomic(Packet *pkt)
195    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
196
197    /** Pure virtual function that the device must implement. Called when a read
198     * command is recieved by the port.
199     * @param pkt Packet describing this request
200     * @return number of ticks it took to complete
201     */
202    virtual Tick read(Packet *pkt) = 0;
203
204    /** Pure virtual function that the device must implement. Called when a
205     * write command is recieved by the port.
206     * @param pkt Packet describing this request
207     * @return number of ticks it took to complete
208     */
209    virtual Tick write(Packet *pkt) = 0;
210
211  public:
212    /** Params struct which is extended through each device based on the
213     * parameters it needs. Since we are re-writing everything, we might as well
214     * start from the bottom this time. */
215
216    struct Params
217    {
218        std::string name;
219        Platform *platform;
220        System *system;
221    };
222
223  protected:
224    Params *_params;
225
226  public:
227    const Params *params() const { return _params; }
228
229    PioDevice(Params *p)
230              : MemObject(p->name),  platform(p->platform), pioPort(NULL),
231                _params(p)
232              {}
233
234    virtual ~PioDevice();
235
236    virtual void init();
237
238    virtual Port *getPort(const std::string &if_name)
239    {
240        if (if_name == "pio") {
241            if (pioPort != NULL)
242                panic("pio port already connected to.");
243            pioPort = new PioPort(this, params()->platform);
244            return pioPort;
245        } else
246            return NULL;
247    }
248    friend class PioPort;
249
250};
251
252class BasicPioDevice : public PioDevice
253{
254  public:
255    struct Params :  public PioDevice::Params
256    {
257        Addr pio_addr;
258        Tick pio_delay;
259    };
260
261  protected:
262    /** Address that the device listens to. */
263    Addr pioAddr;
264
265    /** Size that the device's address range. */
266    Addr pioSize;
267
268    /** Delay that the device experinces on an access. */
269    Tick pioDelay;
270
271  public:
272    BasicPioDevice(Params *p)
273        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
274    {}
275
276    /** return the address ranges that this device responds to.
277     * @params range_list range list to populate with ranges
278     */
279    void addressRanges(AddrRangeList &range_list);
280
281};
282
283class DmaDevice : public PioDevice
284{
285  protected:
286    DmaPort *dmaPort;
287
288  public:
289    DmaDevice(Params *p);
290    virtual ~DmaDevice();
291
292    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
293    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
294
295    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
296    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
297
298    bool dmaPending() { return dmaPort->dmaPending(); }
299
300    virtual Port *getPort(const std::string &if_name)
301    {
302        if (if_name == "pio") {
303            if (pioPort != NULL)
304                panic("pio port already connected to.");
305            pioPort = new PioPort(this, params()->platform);
306            return pioPort;
307        } else if (if_name == "dma") {
308            if (dmaPort != NULL)
309                panic("dma port already connected to.");
310            dmaPort = new DmaPort(this, params()->platform);
311            return dmaPort;
312        } else
313            return NULL;
314    }
315
316    friend class DmaPort;
317};
318
319
320#endif // __DEV_IO_DEVICE_HH__
321