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