io_device.hh revision 2522
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    virtual Tick read(Packet &pkt) = 0;
197
198    /** Pure virtual function that the device must implement. Called when a
199     * write command is recieved by the port. */
200    virtual Tick write(Packet &pkt) = 0;
201
202  public:
203    /** Params struct which is extended through each device based on the
204     * parameters it needs. Since we are re-writing everything, we might as well
205     * start from the bottom this time. */
206
207    struct Params
208    {
209        std::string name;
210        Platform *platform;
211        System *system;
212    };
213
214  protected:
215    Params *_params;
216
217  public:
218    const Params *params() const { return _params; }
219
220    PioDevice(Params *p)
221              : SimObject(params()->name), platform(p->platform), _params(p)
222              {}
223
224    virtual ~PioDevice();
225
226    virtual Port *getPort(const std::string &if_name)
227    {
228        if (if_name == "pio") {
229            if (pioPort != NULL)
230                panic("pio port already connected to.");
231            pioPort = new PioPort(this, params()->platform);
232            return pioPort;
233        } else
234            return NULL;
235    }
236    friend class PioPort;
237
238};
239
240class BasicPioDevice : public PioDevice
241{
242  public:
243    struct Params :  public PioDevice::Params
244    {
245        Addr pio_addr;
246        Tick pio_delay;
247    };
248
249  protected:
250    /** Address that the device listens to. */
251    Addr pioAddr;
252
253    /** Size that the device's address range. */
254    Addr pioSize;
255
256    /** Delay that the device experinces on an access. */
257    Tick pioDelay;
258
259  public:
260    BasicPioDevice(Params *p)
261        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
262    {}
263
264};
265
266class DmaDevice : public PioDevice
267{
268  protected:
269    DmaPort *dmaPort;
270
271  public:
272    DmaDevice(Params *p);
273    virtual ~DmaDevice();
274
275    virtual Port *getPort(const std::string &if_name)
276    {
277        if (if_name == "pio")
278            return pioPort;
279        else if (if_name == "dma")
280            return dmaPort;
281        else
282            return NULL;
283    }
284
285    friend class DmaPort;
286};
287
288
289#endif // __DEV_IO_DEVICE_HH__
290