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