io_device.cc (2902:695d4683916e) io_device.cc (2914:2c524dc023d2)
1/*
2 * Copyright (c) 2006 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#include "base/trace.hh"
33#include "dev/io_device.hh"
34#include "sim/builder.hh"
35#include "sim/system.hh"
36
37
38PioPort::PioPort(PioDevice *dev, System *s, std::string pname)
1/*
2 * Copyright (c) 2006 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#include "base/trace.hh"
33#include "dev/io_device.hh"
34#include "sim/builder.hh"
35#include "sim/system.hh"
36
37
38PioPort::PioPort(PioDevice *dev, System *s, std::string pname)
39 : Port(dev->name() + pname), device(dev), sys(s),
40 outTiming(0), drainEvent(NULL)
39 : SimpleTimingPort(dev->name() + pname), device(dev), sys(s)
41{ }
42
43
44Tick
45PioPort::recvAtomic(Packet *pkt)
46{
47 return device->recvAtomic(pkt);
48}
49
50void
51PioPort::recvFunctional(Packet *pkt)
52{
53 device->recvAtomic(pkt);
54}
55
56void
57PioPort::getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
58{
59 snoop.clear();
60 device->addressRanges(resp);
61}
62
63
40{ }
41
42
43Tick
44PioPort::recvAtomic(Packet *pkt)
45{
46 return device->recvAtomic(pkt);
47}
48
49void
50PioPort::recvFunctional(Packet *pkt)
51{
52 device->recvAtomic(pkt);
53}
54
55void
56PioPort::getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
57{
58 snoop.clear();
59 device->addressRanges(resp);
60}
61
62
64void
65PioPort::recvRetry()
66{
67 bool result = true;
68 while (result && transmitList.size()) {
69 result = Port::sendTiming(transmitList.front());
70 if (result)
71 transmitList.pop_front();
72 }
73 if (transmitList.size() == 0 && drainEvent) {
74 drainEvent->process();
75 drainEvent = NULL;
76 }
77}
78
79void
80PioPort::SendEvent::process()
81{
82 port->outTiming--;
83 assert(port->outTiming >= 0);
84 if (port->Port::sendTiming(packet))
85 if (port->transmitList.size() == 0 && port->drainEvent) {
86 port->drainEvent->process();
87 port->drainEvent = NULL;
88 }
89 return;
90
91 port->transmitList.push_back(packet);
92}
93
94void
95PioPort::resendNacked(Packet *pkt) {
96 pkt->reinitNacked();
97 if (transmitList.size()) {
98 transmitList.push_front(pkt);
99 } else {
100 if (!Port::sendTiming(pkt))
101 transmitList.push_front(pkt);
102 }
103};
104
105
106bool
107PioPort::recvTiming(Packet *pkt)
108{
109 if (pkt->result == Packet::Nacked) {
110 resendNacked(pkt);
111 } else {
112 Tick latency = device->recvAtomic(pkt);
113 // turn packet around to go back to requester
114 pkt->makeTimingResponse();
115 sendTiming(pkt, latency);
116 }
117 return true;
118}
119
63bool
64PioPort::recvTiming(Packet *pkt)
65{
66 if (pkt->result == Packet::Nacked) {
67 resendNacked(pkt);
68 } else {
69 Tick latency = device->recvAtomic(pkt);
70 // turn packet around to go back to requester
71 pkt->makeTimingResponse();
72 sendTiming(pkt, latency);
73 }
74 return true;
75}
76
120unsigned int
121PioPort::drain(Event *de)
122{
123 if (outTiming == 0 && transmitList.size() == 0)
124 return 0;
125 drainEvent = de;
126 return 1;
127}
128
129PioDevice::~PioDevice()
130{
131 if (pioPort)
132 delete pioPort;
133}
134
135void
136PioDevice::init()
137{
138 if (!pioPort)
139 panic("Pio port not connected to anything!");
140 pioPort->sendStatusChange(Port::RangeChange);
141}
142
143
144unsigned int
145PioDevice::drain(Event *de)
146{
147 unsigned int count;
148 count = pioPort->drain(de);
149 if (count)
150 changeState(Draining);
151 else
152 changeState(Drained);
153 return count;
154}
155
156void
157BasicPioDevice::addressRanges(AddrRangeList &range_list)
158{
159 assert(pioSize != 0);
160 range_list.clear();
161 range_list.push_back(RangeSize(pioAddr, pioSize));
162}
163
164
165DmaPort::DmaPort(DmaDevice *dev, System *s)
166 : Port(dev->name() + "-dmaport"), device(dev), sys(s), pendingCount(0),
167 actionInProgress(0), drainEvent(NULL)
168{ }
169
170bool
171DmaPort::recvTiming(Packet *pkt)
172{
173
174
175 if (pkt->result == Packet::Nacked) {
176 DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n",
177 pkt, pkt->senderState, pkt->getAddr());
178 pkt->reinitNacked();
179 sendDma(pkt, true);
180 } else if (pkt->senderState) {
181 DmaReqState *state;
182 DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x\n",
183 pkt, pkt->senderState, pkt->getAddr());
184 state = dynamic_cast<DmaReqState*>(pkt->senderState);
185 pendingCount--;
186
187 assert(pendingCount >= 0);
188 assert(state);
189
190 state->numBytes += pkt->req->getSize();
191 if (state->totBytes == state->numBytes) {
192 state->completionEvent->process();
193 delete state;
194 }
195 delete pkt->req;
196 delete pkt;
197
198 if (pendingCount == 0 && drainEvent) {
199 drainEvent->process();
200 drainEvent = NULL;
201 }
202 } else {
203 panic("Got packet without sender state... huh?\n");
204 }
205
206 return true;
207}
208
209DmaDevice::DmaDevice(Params *p)
210 : PioDevice(p), dmaPort(NULL)
211{ }
212
213
214unsigned int
215DmaDevice::drain(Event *de)
216{
217 unsigned int count;
218 count = pioPort->drain(de) + dmaPort->drain(de);
219 if (count)
220 changeState(Draining);
221 else
222 changeState(Drained);
223 return count;
224}
225
226unsigned int
227DmaPort::drain(Event *de)
228{
229 if (pendingCount == 0)
230 return 0;
231 drainEvent = de;
232 return 1;
233}
234
235
236void
237DmaPort::recvRetry()
238{
239 Packet* pkt = transmitList.front();
240 bool result = true;
241 while (result && transmitList.size()) {
242 DPRINTF(DMA, "Retry on Packet %#x with senderState: %#x\n",
243 pkt, pkt->senderState);
244 result = sendTiming(pkt);
245 if (result) {
246 DPRINTF(DMA, "-- Done\n");
247 transmitList.pop_front();
248 } else {
249 DPRINTF(DMA, "-- Failed, queued\n");
250 }
251 }
252}
253
254
255void
256DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
257 uint8_t *data)
258{
259 assert(event);
260
261 assert(device->getState() == SimObject::Running);
262
263 DmaReqState *reqState = new DmaReqState(event, this, size);
264
265 for (ChunkGenerator gen(addr, size, peerBlockSize());
266 !gen.done(); gen.next()) {
267 Request *req = new Request(gen.addr(), gen.size(), 0);
268 Packet *pkt = new Packet(req, cmd, Packet::Broadcast);
269
270 // Increment the data pointer on a write
271 if (data)
272 pkt->dataStatic(data + gen.complete());
273
274 pkt->senderState = reqState;
275
276 assert(pendingCount >= 0);
277 pendingCount++;
278 sendDma(pkt);
279 }
280
281}
282
283
284void
285DmaPort::sendDma(Packet *pkt, bool front)
286{
287 // some kind of selction between access methods
288 // more work is going to have to be done to make
289 // switching actually work
290
291 System::MemoryMode state = sys->getMemoryMode();
292 if (state == System::Timing) {
293 DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n",
294 pkt, pkt->getAddr());
295 if (transmitList.size() || !sendTiming(pkt)) {
296 if (front)
297 transmitList.push_front(pkt);
298 else
299 transmitList.push_back(pkt);
300 DPRINTF(DMA, "-- Failed: queued\n");
301 } else {
302 DPRINTF(DMA, "-- Done\n");
303 }
304 } else if (state == System::Atomic) {
305 Tick lat;
306 lat = sendAtomic(pkt);
307 assert(pkt->senderState);
308 DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
309 assert(state);
310
311 state->numBytes += pkt->req->getSize();
312 if (state->totBytes == state->numBytes) {
313 state->completionEvent->schedule(curTick + lat);
314 delete state;
315 delete pkt->req;
316 }
317 pendingCount--;
318 assert(pendingCount >= 0);
319 delete pkt;
320
321 if (pendingCount == 0 && drainEvent) {
322 drainEvent->process();
323 drainEvent = NULL;
324 }
325
326 } else
327 panic("Unknown memory command state.");
328}
329
330DmaDevice::~DmaDevice()
331{
332 if (dmaPort)
333 delete dmaPort;
334}
335
336
77PioDevice::~PioDevice()
78{
79 if (pioPort)
80 delete pioPort;
81}
82
83void
84PioDevice::init()
85{
86 if (!pioPort)
87 panic("Pio port not connected to anything!");
88 pioPort->sendStatusChange(Port::RangeChange);
89}
90
91
92unsigned int
93PioDevice::drain(Event *de)
94{
95 unsigned int count;
96 count = pioPort->drain(de);
97 if (count)
98 changeState(Draining);
99 else
100 changeState(Drained);
101 return count;
102}
103
104void
105BasicPioDevice::addressRanges(AddrRangeList &range_list)
106{
107 assert(pioSize != 0);
108 range_list.clear();
109 range_list.push_back(RangeSize(pioAddr, pioSize));
110}
111
112
113DmaPort::DmaPort(DmaDevice *dev, System *s)
114 : Port(dev->name() + "-dmaport"), device(dev), sys(s), pendingCount(0),
115 actionInProgress(0), drainEvent(NULL)
116{ }
117
118bool
119DmaPort::recvTiming(Packet *pkt)
120{
121
122
123 if (pkt->result == Packet::Nacked) {
124 DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n",
125 pkt, pkt->senderState, pkt->getAddr());
126 pkt->reinitNacked();
127 sendDma(pkt, true);
128 } else if (pkt->senderState) {
129 DmaReqState *state;
130 DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x\n",
131 pkt, pkt->senderState, pkt->getAddr());
132 state = dynamic_cast<DmaReqState*>(pkt->senderState);
133 pendingCount--;
134
135 assert(pendingCount >= 0);
136 assert(state);
137
138 state->numBytes += pkt->req->getSize();
139 if (state->totBytes == state->numBytes) {
140 state->completionEvent->process();
141 delete state;
142 }
143 delete pkt->req;
144 delete pkt;
145
146 if (pendingCount == 0 && drainEvent) {
147 drainEvent->process();
148 drainEvent = NULL;
149 }
150 } else {
151 panic("Got packet without sender state... huh?\n");
152 }
153
154 return true;
155}
156
157DmaDevice::DmaDevice(Params *p)
158 : PioDevice(p), dmaPort(NULL)
159{ }
160
161
162unsigned int
163DmaDevice::drain(Event *de)
164{
165 unsigned int count;
166 count = pioPort->drain(de) + dmaPort->drain(de);
167 if (count)
168 changeState(Draining);
169 else
170 changeState(Drained);
171 return count;
172}
173
174unsigned int
175DmaPort::drain(Event *de)
176{
177 if (pendingCount == 0)
178 return 0;
179 drainEvent = de;
180 return 1;
181}
182
183
184void
185DmaPort::recvRetry()
186{
187 Packet* pkt = transmitList.front();
188 bool result = true;
189 while (result && transmitList.size()) {
190 DPRINTF(DMA, "Retry on Packet %#x with senderState: %#x\n",
191 pkt, pkt->senderState);
192 result = sendTiming(pkt);
193 if (result) {
194 DPRINTF(DMA, "-- Done\n");
195 transmitList.pop_front();
196 } else {
197 DPRINTF(DMA, "-- Failed, queued\n");
198 }
199 }
200}
201
202
203void
204DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
205 uint8_t *data)
206{
207 assert(event);
208
209 assert(device->getState() == SimObject::Running);
210
211 DmaReqState *reqState = new DmaReqState(event, this, size);
212
213 for (ChunkGenerator gen(addr, size, peerBlockSize());
214 !gen.done(); gen.next()) {
215 Request *req = new Request(gen.addr(), gen.size(), 0);
216 Packet *pkt = new Packet(req, cmd, Packet::Broadcast);
217
218 // Increment the data pointer on a write
219 if (data)
220 pkt->dataStatic(data + gen.complete());
221
222 pkt->senderState = reqState;
223
224 assert(pendingCount >= 0);
225 pendingCount++;
226 sendDma(pkt);
227 }
228
229}
230
231
232void
233DmaPort::sendDma(Packet *pkt, bool front)
234{
235 // some kind of selction between access methods
236 // more work is going to have to be done to make
237 // switching actually work
238
239 System::MemoryMode state = sys->getMemoryMode();
240 if (state == System::Timing) {
241 DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n",
242 pkt, pkt->getAddr());
243 if (transmitList.size() || !sendTiming(pkt)) {
244 if (front)
245 transmitList.push_front(pkt);
246 else
247 transmitList.push_back(pkt);
248 DPRINTF(DMA, "-- Failed: queued\n");
249 } else {
250 DPRINTF(DMA, "-- Done\n");
251 }
252 } else if (state == System::Atomic) {
253 Tick lat;
254 lat = sendAtomic(pkt);
255 assert(pkt->senderState);
256 DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
257 assert(state);
258
259 state->numBytes += pkt->req->getSize();
260 if (state->totBytes == state->numBytes) {
261 state->completionEvent->schedule(curTick + lat);
262 delete state;
263 delete pkt->req;
264 }
265 pendingCount--;
266 assert(pendingCount >= 0);
267 delete pkt;
268
269 if (pendingCount == 0 && drainEvent) {
270 drainEvent->process();
271 drainEvent = NULL;
272 }
273
274 } else
275 panic("Unknown memory command state.");
276}
277
278DmaDevice::~DmaDevice()
279{
280 if (dmaPort)
281 delete dmaPort;
282}
283
284