io_device.hh revision 2541
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
29#ifndef __DEV_IO_DEVICE_HH__
30#define __DEV_IO_DEVICE_HH__
31
32#include "base/chunk_generator.hh"
33#include "mem/port.hh"
34#include "sim/eventq.hh"
35#include "sim/sim_object.hh"
36
37class Platform;
38class PioDevice;
39class DmaDevice;
40class System;
41
42/**
43 * The PioPort class is a programmed i/o port that all devices that are
44 * sensitive to an address range use. The port takes all the memory
45 * access types and roles them into one read() and write() call that the device
46 * must respond to. The device must also provide the addressRanges() function
47 * with which it returns the address ranges it is interested in. An extra
48 * sendTiming() function is implemented which takes an delay. In this way the
49 * device can immediatly call sendTiming(pkt, time) after processing a request
50 * and the request will be handled by the port even if the port bus the device
51 * connects to is blocked.
52 */
53class PioPort : public Port
54{
55  protected:
56    /** The device that this port serves. */
57    PioDevice *device;
58
59    /** The platform that device/port are in. This is used to select which mode
60     * we are currently operating in. */
61    Platform *platform;
62
63    /** A list of outgoing timing response packets that haven't been serviced
64     * yet. */
65    std::list<Packet*> transmitList;
66
67    /** The current status of the peer(bus) that we are connected to. */
68    Status peerStatus;
69
70    virtual bool recvTiming(Packet &pkt);
71
72    virtual Tick recvAtomic(Packet &pkt);
73
74    virtual void recvFunctional(Packet &pkt) ;
75
76    virtual void recvStatusChange(Status status)
77    { peerStatus = status; }
78
79    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
80
81    /**
82     * This class is used to implemented sendTiming() with a delay. When a delay
83     * is requested a new event is created. When the event time expires it
84     * attempts to send the packet. If it cannot, the packet is pushed onto the
85     * transmit list to be sent when recvRetry() is called. */
86    class SendEvent : public Event
87    {
88        PioPort *port;
89        Packet packet;
90
91        SendEvent(PioPort *p, Packet &pkt, Tick t)
92            : Event(&mainEventQueue), packet(pkt)
93        { schedule(curTick + t); }
94
95        virtual void process();
96
97        virtual const char *description()
98        { return "Future scheduled sendTiming event"; }
99
100        friend class PioPort;
101    };
102
103    /** Schedule a sendTiming() event to be called in the future. */
104    void sendTiming(Packet &pkt, Tick time)
105    { new PioPort::SendEvent(this, pkt, time); }
106
107    /** This function pops the last element off the transmit list and sends it.*/
108    virtual Packet *recvRetry();
109
110  public:
111    PioPort(PioDevice *dev, Platform *p);
112
113  friend class PioPort::SendEvent;
114};
115
116class DmaPort : public Port
117{
118  protected:
119    PioDevice *device;
120    std::list<Packet*> transmitList;
121    Event *completionEvent;
122
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 Packet *recvRetry() ;
134
135    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
136    { resp.clear(); snoop.clear(); }
137
138    class SendEvent : public Event
139    {
140        DmaPort *port;
141        Packet packet;
142
143        SendEvent(PioPort *p, Packet &pkt, Tick t)
144            : Event(&mainEventQueue), packet(pkt)
145        { schedule(curTick + t); }
146
147        virtual void process();
148
149        virtual const char *description()
150        { return "Future scheduled sendTiming event"; }
151
152        friend class DmaPort;
153    };
154
155    void dmaAction(Command cmd, DmaPort port, Addr addr, int size,
156              Event *event, uint8_t *data = NULL);
157
158    void sendDma(Packet &pkt);
159
160  public:
161    DmaPort(DmaDevice *dev);
162
163  friend class DmaPort::SendEvent;
164
165};
166
167/**
168 * This device is the base class which all devices senstive to an address range
169 * inherit from. There are three pure virtual functions which all devices must
170 * implement addressRanges(), read(), and write(). The magic do choose which
171 * mode we are in, etc is handled by the PioPort so the device doesn't have to
172 * bother.
173 */
174
175class PioDevice : public SimObject
176{
177  protected:
178
179    /** The platform we are in. This is used to decide what type of memory
180     * transaction we should perform. */
181    Platform *platform;
182
183    /** The pioPort that handles the requests for us and provides us requests
184     * that it sees. */
185    PioPort *pioPort;
186
187    virtual void addressRanges(AddrRangeList &range_list) = 0;
188
189    /** As far as the devices are concerned they only accept atomic transactions
190     * which are converted to either a write or a read. */
191    Tick recvAtomic(Packet &pkt)
192    { return pkt.cmd == Read ? this->read(pkt) : this->write(pkt); }
193
194    /** Pure virtual function that the device must implement. Called when a read
195     * command is recieved by the port.
196     * @param pkt Packet describing this request
197     * @return number of ticks it took to complete
198     */
199    virtual Tick read(Packet &pkt) = 0;
200
201    /** Pure virtual function that the device must implement. Called when a
202     * write command is recieved by the port.
203     * @param pkt Packet describing this request
204     * @return number of ticks it took to complete
205     */
206    virtual Tick write(Packet &pkt) = 0;
207
208  public:
209    /** Params struct which is extended through each device based on the
210     * parameters it needs. Since we are re-writing everything, we might as well
211     * start from the bottom this time. */
212
213    struct Params
214    {
215        std::string name;
216        Platform *platform;
217        System *system;
218    };
219
220  protected:
221    Params *_params;
222
223  public:
224    const Params *params() const { return _params; }
225
226    PioDevice(Params *p)
227              : SimObject(params()->name), platform(p->platform), _params(p)
228              {}
229
230    virtual ~PioDevice();
231
232    virtual void init();
233
234    virtual Port *getPort(const std::string &if_name)
235    {
236        if (if_name == "pio") {
237            if (pioPort != NULL)
238                panic("pio port already connected to.");
239            pioPort = new PioPort(this, params()->platform);
240            return pioPort;
241        } else
242            return NULL;
243    }
244    friend class PioPort;
245
246};
247
248class BasicPioDevice : public PioDevice
249{
250  public:
251    struct Params :  public PioDevice::Params
252    {
253        Addr pio_addr;
254        Tick pio_delay;
255    };
256
257  protected:
258    /** Address that the device listens to. */
259    Addr pioAddr;
260
261    /** Size that the device's address range. */
262    Addr pioSize;
263
264    /** Delay that the device experinces on an access. */
265    Tick pioDelay;
266
267  public:
268    BasicPioDevice(Params *p)
269        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
270    {}
271
272    /** return the address ranges that this device responds to.
273     * @params range_list range list to populate with ranges
274     */
275    addressRanges(AddrRangeList &range_list);
276
277};
278
279class DmaDevice : public PioDevice
280{
281  protected:
282    DmaPort *dmaPort;
283
284  public:
285    DmaDevice(Params *p);
286    virtual ~DmaDevice();
287
288    virtual Port *getPort(const std::string &if_name)
289    {
290        if (if_name == "pio")
291            return pioPort;
292        else if (if_name == "dma")
293            return dmaPort;
294        else
295            return NULL;
296    }
297
298    friend class DmaPort;
299};
300
301
302#endif // __DEV_IO_DEVICE_HH__
303