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