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 "debug/BusAddrRanges.hh"
35 #include "debug/DMA.hh"
36 #include "dev/io_device.hh"
37 #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
);
55 for (AddrRangeIter i
= resp
.begin(); i
!= resp
.end(); i
++)
56 DPRINTF(BusAddrRanges
, "Adding Range %#x-%#x\n", i
->start
, i
->end
);
60 PioDevice::PioDevice(const Params
*p
)
61 : MemObject(p
), platform(p
->platform
), sys(p
->system
), pioPort(NULL
)
64 PioDevice::~PioDevice()
74 panic("Pio port not connected to anything!");
75 pioPort
->sendStatusChange(Port::RangeChange
);
80 PioDevice::drain(Event
*de
)
83 count
= pioPort
->drain(de
);
85 changeState(Draining
);
91 BasicPioDevice::BasicPioDevice(const Params
*p
)
92 : PioDevice(p
), pioAddr(p
->pio_addr
), pioSize(0),
93 pioDelay(p
->pio_latency
)
97 BasicPioDevice::addressRanges(AddrRangeList
&range_list
)
101 DPRINTF(BusAddrRanges
, "registering range: %#x-%#x\n", pioAddr
, pioSize
);
102 range_list
.push_back(RangeSize(pioAddr
, pioSize
));
106 DmaPort::DmaPort(MemObject
*dev
, System
*s
, Tick min_backoff
, Tick max_backoff
)
107 : Port(dev
->name() + "-dmaport", dev
), device(dev
), sys(s
),
108 pendingCount(0), actionInProgress(0), drainEvent(NULL
),
109 backoffTime(0), minBackoffDelay(min_backoff
),
110 maxBackoffDelay(max_backoff
), inRetry(false), backoffEvent(this)
114 DmaPort::recvTiming(PacketPtr pkt
)
116 if (pkt
->wasNacked()) {
117 DPRINTF(DMA
, "Received nacked %s addr %#x\n",
118 pkt
->cmdString(), pkt
->getAddr());
120 if (backoffTime
< minBackoffDelay
)
121 backoffTime
= minBackoffDelay
;
122 else if (backoffTime
< maxBackoffDelay
)
125 reschedule(backoffEvent
, curTick() + backoffTime
, true);
127 DPRINTF(DMA
, "Backoff time set to %d ticks\n", backoffTime
);
131 } else if (pkt
->senderState
) {
135 DPRINTF(DMA
, "Received response %s addr %#x size %#x\n",
136 pkt
->cmdString(), pkt
->getAddr(), pkt
->req
->getSize());
137 state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
140 assert(pendingCount
>= 0);
143 // We shouldn't ever get a block in ownership state
144 assert(!(pkt
->memInhibitAsserted() && !pkt
->sharedAsserted()));
146 state
->numBytes
+= pkt
->req
->getSize();
147 assert(state
->totBytes
>= state
->numBytes
);
148 if (state
->totBytes
== state
->numBytes
) {
149 if (state
->completionEvent
) {
151 schedule(state
->completionEvent
, curTick() + state
->delay
);
153 state
->completionEvent
->process();
160 if (pendingCount
== 0 && drainEvent
) {
161 drainEvent
->process();
165 panic("Got packet without sender state... huh?\n");
171 DmaDevice::DmaDevice(const Params
*p
)
172 : PioDevice(p
), dmaPort(NULL
)
177 DmaDevice::drain(Event
*de
)
180 count
= pioPort
->drain(de
) + dmaPort
->drain(de
);
182 changeState(Draining
);
184 changeState(Drained
);
189 DmaPort::drain(Event
*de
)
191 if (pendingCount
== 0)
201 assert(transmitList
.size());
204 PacketPtr pkt
= transmitList
.front();
205 DPRINTF(DMA
, "Retry on %s addr %#x\n",
206 pkt
->cmdString(), pkt
->getAddr());
207 result
= sendTiming(pkt
);
209 DPRINTF(DMA
, "-- Done\n");
210 transmitList
.pop_front();
214 DPRINTF(DMA
, "-- Failed, queued\n");
216 } while (!backoffTime
&& result
&& transmitList
.size());
218 if (transmitList
.size() && backoffTime
&& !inRetry
) {
219 DPRINTF(DMA
, "Scheduling backoff for %d\n", curTick()+backoffTime
);
220 if (!backoffEvent
.scheduled())
221 schedule(backoffEvent
, backoffTime
+ curTick());
223 DPRINTF(DMA
, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
224 transmitList
.size(), backoffTime
, inRetry
,
225 backoffEvent
.scheduled());
230 DmaPort::dmaAction(Packet::Command cmd
, Addr addr
, int size
, Event
*event
,
231 uint8_t *data
, Tick delay
, Request::Flags flag
)
233 assert(device
->getState() == SimObject::Running
);
235 DmaReqState
*reqState
= new DmaReqState(event
, this, size
, delay
);
238 DPRINTF(DMA
, "Starting DMA for addr: %#x size: %d sched: %d\n", addr
, size
,
239 event
? event
->scheduled() : -1 );
240 for (ChunkGenerator
gen(addr
, size
, peerBlockSize());
241 !gen
.done(); gen
.next()) {
242 Request
*req
= new Request(gen
.addr(), gen
.size(), flag
);
243 PacketPtr pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
245 // Increment the data pointer on a write
247 pkt
->dataStatic(data
+ gen
.complete());
249 pkt
->senderState
= reqState
;
251 assert(pendingCount
>= 0);
253 DPRINTF(DMA
, "--Queuing DMA for addr: %#x size: %d\n", gen
.addr(),
261 DmaPort::queueDma(PacketPtr pkt
, bool front
)
265 transmitList
.push_front(pkt
);
267 transmitList
.push_back(pkt
);
275 // some kind of selction between access methods
276 // more work is going to have to be done to make
277 // switching actually work
278 assert(transmitList
.size());
279 PacketPtr pkt
= transmitList
.front();
281 Enums::MemoryMode state
= sys
->getMemoryMode();
282 if (state
== Enums::timing
) {
283 if (backoffEvent
.scheduled() || inRetry
) {
284 DPRINTF(DMA
, "Can't send immediately, waiting for retry or backoff timer\n");
288 DPRINTF(DMA
, "Attempting to send %s addr %#x\n",
289 pkt
->cmdString(), pkt
->getAddr());
293 result
= sendTiming(pkt
);
295 transmitList
.pop_front();
296 DPRINTF(DMA
, "-- Done\n");
299 DPRINTF(DMA
, "-- Failed: queued\n");
301 } while (result
&& !backoffTime
&& transmitList
.size());
303 if (transmitList
.size() && backoffTime
&& !inRetry
&&
304 !backoffEvent
.scheduled()) {
305 DPRINTF(DMA
, "-- Scheduling backoff timer for %d\n",
306 backoffTime
+curTick());
307 schedule(backoffEvent
, backoffTime
+ curTick());
309 } else if (state
== Enums::atomic
) {
310 transmitList
.pop_front();
313 DPRINTF(DMA
, "--Sending DMA for addr: %#x size: %d\n",
314 pkt
->req
->getPaddr(), pkt
->req
->getSize());
315 lat
= sendAtomic(pkt
);
316 assert(pkt
->senderState
);
317 DmaReqState
*state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
319 state
->numBytes
+= pkt
->req
->getSize();
321 DPRINTF(DMA
, "--Received response for DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
322 pkt
->req
->getPaddr(), pkt
->req
->getSize(), state
->numBytes
,
324 state
->completionEvent
? state
->completionEvent
->scheduled() : 0 );
326 if (state
->totBytes
== state
->numBytes
) {
327 if (state
->completionEvent
) {
328 assert(!state
->completionEvent
->scheduled());
329 schedule(state
->completionEvent
, curTick() + lat
+ state
->delay
);
335 assert(pendingCount
>= 0);
338 if (pendingCount
== 0 && drainEvent
) {
339 drainEvent
->process();
344 panic("Unknown memory command state.");
347 DmaDevice::~DmaDevice()