2 * Copyright (c) 2006 The Regents of The University of Michigan
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.
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.
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"
39 PioPort::PioPort(PioDevice
*dev
, System
*s
, std::string pname
)
40 : SimpleTimingPort(dev
->name() + pname
, dev
), device(dev
)
45 PioPort::recvAtomic(PacketPtr pkt
)
47 return pkt
->isRead() ? device
->read(pkt
) : device
->write(pkt
);
51 PioPort::getDeviceAddressRanges(AddrRangeList
&resp
, bool &snoop
)
54 device
->addressRanges(resp
);
58 PioDevice::~PioDevice()
68 panic("Pio port not connected to anything!");
69 pioPort
->sendStatusChange(Port::RangeChange
);
74 PioDevice::drain(Event
*de
)
77 count
= pioPort
->drain(de
);
79 changeState(Draining
);
86 BasicPioDevice::addressRanges(AddrRangeList
&range_list
)
90 range_list
.push_back(RangeSize(pioAddr
, pioSize
));
94 DmaPort::DmaPort(DmaDevice
*dev
, System
*s
)
95 : Port(dev
->name() + "-dmaport", dev
), device(dev
), sys(s
),
96 pendingCount(0), actionInProgress(0), drainEvent(NULL
),
97 backoffTime(0), inRetry(false), backoffEvent(this)
101 DmaPort::recvTiming(PacketPtr pkt
)
103 if (pkt
->wasNacked()) {
104 DPRINTF(DMA
, "Received nacked %s addr %#x\n",
105 pkt
->cmdString(), pkt
->getAddr());
107 if (backoffTime
< device
->minBackoffDelay
)
108 backoffTime
= device
->minBackoffDelay
;
109 else if (backoffTime
< device
->maxBackoffDelay
)
112 backoffEvent
.reschedule(curTick
+ backoffTime
, true);
114 DPRINTF(DMA
, "Backoff time set to %d ticks\n", backoffTime
);
118 } else if (pkt
->senderState
) {
122 DPRINTF(DMA
, "Received response %s addr %#x size %#x\n",
123 pkt
->cmdString(), pkt
->getAddr(), pkt
->req
->getSize());
124 state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
127 assert(pendingCount
>= 0);
130 state
->numBytes
+= pkt
->req
->getSize();
131 assert(state
->totBytes
>= state
->numBytes
);
132 if (state
->totBytes
== state
->numBytes
) {
133 state
->completionEvent
->process();
139 if (pendingCount
== 0 && drainEvent
) {
140 drainEvent
->process();
144 panic("Got packet without sender state... huh?\n");
150 DmaDevice::DmaDevice(Params
*p
)
151 : PioDevice(p
), dmaPort(NULL
), minBackoffDelay(p
->min_backoff_delay
),
152 maxBackoffDelay(p
->max_backoff_delay
)
157 DmaDevice::drain(Event
*de
)
160 count
= pioPort
->drain(de
) + dmaPort
->drain(de
);
162 changeState(Draining
);
164 changeState(Drained
);
169 DmaPort::drain(Event
*de
)
171 if (pendingCount
== 0)
181 assert(transmitList
.size());
182 PacketPtr pkt
= transmitList
.front();
185 DPRINTF(DMA
, "Retry on %s addr %#x\n",
186 pkt
->cmdString(), pkt
->getAddr());
187 result
= sendTiming(pkt
);
189 DPRINTF(DMA
, "-- Done\n");
190 transmitList
.pop_front();
194 DPRINTF(DMA
, "-- Failed, queued\n");
196 } while (!backoffTime
&& result
&& transmitList
.size());
198 if (transmitList
.size() && backoffTime
&& !inRetry
) {
199 DPRINTF(DMA
, "Scheduling backoff for %d\n", curTick
+backoffTime
);
200 if (!backoffEvent
.scheduled())
201 backoffEvent
.schedule(backoffTime
+curTick
);
203 DPRINTF(DMA
, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
204 transmitList
.size(), backoffTime
, inRetry
,
205 backoffEvent
.scheduled());
210 DmaPort::dmaAction(Packet::Command cmd
, Addr addr
, int size
, Event
*event
,
215 assert(device
->getState() == SimObject::Running
);
217 DmaReqState
*reqState
= new DmaReqState(event
, this, size
);
220 DPRINTF(DMA
, "Starting DMA for addr: %#x size: %d sched: %d\n", addr
, size
,
222 for (ChunkGenerator
gen(addr
, size
, peerBlockSize());
223 !gen
.done(); gen
.next()) {
224 Request
*req
= new Request(gen
.addr(), gen
.size(), 0);
225 PacketPtr pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
227 // Increment the data pointer on a write
229 pkt
->dataStatic(data
+ gen
.complete());
231 pkt
->senderState
= reqState
;
233 assert(pendingCount
>= 0);
235 DPRINTF(DMA
, "--Queuing DMA for addr: %#x size: %d\n", gen
.addr(),
243 DmaPort::queueDma(PacketPtr pkt
, bool front
)
247 transmitList
.push_front(pkt
);
249 transmitList
.push_back(pkt
);
257 // some kind of selction between access methods
258 // more work is going to have to be done to make
259 // switching actually work
260 assert(transmitList
.size());
261 PacketPtr pkt
= transmitList
.front();
263 System::MemoryMode state
= sys
->getMemoryMode();
264 if (state
== System::Timing
) {
265 if (backoffEvent
.scheduled() || inRetry
) {
266 DPRINTF(DMA
, "Can't send immediately, waiting for retry or backoff timer\n");
270 DPRINTF(DMA
, "Attempting to send %s addr %#x\n",
271 pkt
->cmdString(), pkt
->getAddr());
275 result
= sendTiming(pkt
);
277 transmitList
.pop_front();
278 DPRINTF(DMA
, "-- Done\n");
281 DPRINTF(DMA
, "-- Failed: queued\n");
283 } while (result
&& !backoffTime
&& transmitList
.size());
285 if (transmitList
.size() && backoffTime
&& !inRetry
&&
286 !backoffEvent
.scheduled()) {
287 DPRINTF(DMA
, "-- Scheduling backoff timer for %d\n",
288 backoffTime
+curTick
);
289 backoffEvent
.schedule(backoffTime
+curTick
);
291 } else if (state
== System::Atomic
) {
292 transmitList
.pop_front();
295 DPRINTF(DMA
, "--Sending DMA for addr: %#x size: %d\n",
296 pkt
->req
->getPaddr(), pkt
->req
->getSize());
297 lat
= sendAtomic(pkt
);
298 assert(pkt
->senderState
);
299 DmaReqState
*state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
301 state
->numBytes
+= pkt
->req
->getSize();
303 DPRINTF(DMA
, "--Received response for DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
304 pkt
->req
->getPaddr(), pkt
->req
->getSize(), state
->numBytes
,
305 state
->totBytes
, state
->completionEvent
->scheduled());
307 if (state
->totBytes
== state
->numBytes
) {
308 assert(!state
->completionEvent
->scheduled());
309 state
->completionEvent
->schedule(curTick
+ lat
);
314 assert(pendingCount
>= 0);
317 if (pendingCount
== 0 && drainEvent
) {
318 drainEvent
->process();
323 panic("Unknown memory command state.");
326 DmaDevice::~DmaDevice()