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/system.hh"
38 PioPort::PioPort(PioDevice
*dev
, System
*s
, std::string pname
)
39 : SimpleTimingPort(dev
->name() + pname
, dev
), device(dev
)
44 PioPort::recvAtomic(PacketPtr pkt
)
46 return pkt
->isRead() ? device
->read(pkt
) : device
->write(pkt
);
50 PioPort::getDeviceAddressRanges(AddrRangeList
&resp
, bool &snoop
)
53 device
->addressRanges(resp
);
57 PioDevice::PioDevice(const Params
*p
)
58 : MemObject(p
), platform(p
->platform
), sys(p
->system
), pioPort(NULL
)
61 PioDevice::~PioDevice()
71 panic("Pio port not connected to anything!");
72 pioPort
->sendStatusChange(Port::RangeChange
);
77 PioDevice::drain(Event
*de
)
80 count
= pioPort
->drain(de
);
82 changeState(Draining
);
88 BasicPioDevice::BasicPioDevice(const Params
*p
)
89 : PioDevice(p
), pioAddr(p
->pio_addr
), pioSize(0),
90 pioDelay(p
->pio_latency
)
94 BasicPioDevice::addressRanges(AddrRangeList
&range_list
)
98 range_list
.push_back(RangeSize(pioAddr
, pioSize
));
102 DmaPort::DmaPort(DmaDevice
*dev
, System
*s
)
103 : Port(dev
->name() + "-dmaport", dev
), device(dev
), sys(s
),
104 pendingCount(0), actionInProgress(0), drainEvent(NULL
),
105 backoffTime(0), inRetry(false), backoffEvent(this)
109 DmaPort::recvTiming(PacketPtr pkt
)
111 if (pkt
->wasNacked()) {
112 DPRINTF(DMA
, "Received nacked %s addr %#x\n",
113 pkt
->cmdString(), pkt
->getAddr());
115 if (backoffTime
< device
->minBackoffDelay
)
116 backoffTime
= device
->minBackoffDelay
;
117 else if (backoffTime
< device
->maxBackoffDelay
)
120 backoffEvent
.reschedule(curTick
+ backoffTime
, true);
122 DPRINTF(DMA
, "Backoff time set to %d ticks\n", backoffTime
);
126 } else if (pkt
->senderState
) {
130 DPRINTF(DMA
, "Received response %s addr %#x size %#x\n",
131 pkt
->cmdString(), pkt
->getAddr(), pkt
->req
->getSize());
132 state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
135 assert(pendingCount
>= 0);
138 state
->numBytes
+= pkt
->req
->getSize();
139 assert(state
->totBytes
>= state
->numBytes
);
140 if (state
->totBytes
== state
->numBytes
) {
141 state
->completionEvent
->process();
147 if (pendingCount
== 0 && drainEvent
) {
148 drainEvent
->process();
152 panic("Got packet without sender state... huh?\n");
158 DmaDevice::DmaDevice(const Params
*p
)
159 : PioDevice(p
), dmaPort(NULL
), minBackoffDelay(p
->min_backoff_delay
),
160 maxBackoffDelay(p
->max_backoff_delay
)
165 DmaDevice::drain(Event
*de
)
168 count
= pioPort
->drain(de
) + dmaPort
->drain(de
);
170 changeState(Draining
);
172 changeState(Drained
);
177 DmaPort::drain(Event
*de
)
179 if (pendingCount
== 0)
189 assert(transmitList
.size());
190 PacketPtr pkt
= transmitList
.front();
193 DPRINTF(DMA
, "Retry on %s addr %#x\n",
194 pkt
->cmdString(), pkt
->getAddr());
195 result
= sendTiming(pkt
);
197 DPRINTF(DMA
, "-- Done\n");
198 transmitList
.pop_front();
202 DPRINTF(DMA
, "-- Failed, queued\n");
204 } while (!backoffTime
&& result
&& transmitList
.size());
206 if (transmitList
.size() && backoffTime
&& !inRetry
) {
207 DPRINTF(DMA
, "Scheduling backoff for %d\n", curTick
+backoffTime
);
208 if (!backoffEvent
.scheduled())
209 backoffEvent
.schedule(backoffTime
+curTick
);
211 DPRINTF(DMA
, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
212 transmitList
.size(), backoffTime
, inRetry
,
213 backoffEvent
.scheduled());
218 DmaPort::dmaAction(Packet::Command cmd
, Addr addr
, int size
, Event
*event
,
223 assert(device
->getState() == SimObject::Running
);
225 DmaReqState
*reqState
= new DmaReqState(event
, this, size
);
228 DPRINTF(DMA
, "Starting DMA for addr: %#x size: %d sched: %d\n", addr
, size
,
230 for (ChunkGenerator
gen(addr
, size
, peerBlockSize());
231 !gen
.done(); gen
.next()) {
232 Request
*req
= new Request(gen
.addr(), gen
.size(), 0);
233 PacketPtr pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
235 // Increment the data pointer on a write
237 pkt
->dataStatic(data
+ gen
.complete());
239 pkt
->senderState
= reqState
;
241 assert(pendingCount
>= 0);
243 DPRINTF(DMA
, "--Queuing DMA for addr: %#x size: %d\n", gen
.addr(),
251 DmaPort::queueDma(PacketPtr pkt
, bool front
)
255 transmitList
.push_front(pkt
);
257 transmitList
.push_back(pkt
);
265 // some kind of selction between access methods
266 // more work is going to have to be done to make
267 // switching actually work
268 assert(transmitList
.size());
269 PacketPtr pkt
= transmitList
.front();
271 Enums::MemoryMode state
= sys
->getMemoryMode();
272 if (state
== Enums::timing
) {
273 if (backoffEvent
.scheduled() || inRetry
) {
274 DPRINTF(DMA
, "Can't send immediately, waiting for retry or backoff timer\n");
278 DPRINTF(DMA
, "Attempting to send %s addr %#x\n",
279 pkt
->cmdString(), pkt
->getAddr());
283 result
= sendTiming(pkt
);
285 transmitList
.pop_front();
286 DPRINTF(DMA
, "-- Done\n");
289 DPRINTF(DMA
, "-- Failed: queued\n");
291 } while (result
&& !backoffTime
&& transmitList
.size());
293 if (transmitList
.size() && backoffTime
&& !inRetry
&&
294 !backoffEvent
.scheduled()) {
295 DPRINTF(DMA
, "-- Scheduling backoff timer for %d\n",
296 backoffTime
+curTick
);
297 backoffEvent
.schedule(backoffTime
+curTick
);
299 } else if (state
== Enums::atomic
) {
300 transmitList
.pop_front();
303 DPRINTF(DMA
, "--Sending DMA for addr: %#x size: %d\n",
304 pkt
->req
->getPaddr(), pkt
->req
->getSize());
305 lat
= sendAtomic(pkt
);
306 assert(pkt
->senderState
);
307 DmaReqState
*state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
309 state
->numBytes
+= pkt
->req
->getSize();
311 DPRINTF(DMA
, "--Received response for DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
312 pkt
->req
->getPaddr(), pkt
->req
->getSize(), state
->numBytes
,
313 state
->totBytes
, state
->completionEvent
->scheduled());
315 if (state
->totBytes
== state
->numBytes
) {
316 assert(!state
->completionEvent
->scheduled());
317 state
->completionEvent
->schedule(curTick
+ lat
);
322 assert(pendingCount
>= 0);
325 if (pendingCount
== 0 && drainEvent
) {
326 drainEvent
->process();
331 panic("Unknown memory command state.");
334 DmaDevice::~DmaDevice()