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 reschedule(backoffEvent
, 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
) {
142 schedule(state
->completionEvent
, curTick
+ state
->delay
);
144 state
->completionEvent
->process();
150 if (pendingCount
== 0 && drainEvent
) {
151 drainEvent
->process();
155 panic("Got packet without sender state... huh?\n");
161 DmaDevice::DmaDevice(const Params
*p
)
162 : PioDevice(p
), dmaPort(NULL
), minBackoffDelay(p
->min_backoff_delay
),
163 maxBackoffDelay(p
->max_backoff_delay
)
168 DmaDevice::drain(Event
*de
)
171 count
= pioPort
->drain(de
) + dmaPort
->drain(de
);
173 changeState(Draining
);
175 changeState(Drained
);
180 DmaPort::drain(Event
*de
)
182 if (pendingCount
== 0)
192 assert(transmitList
.size());
195 PacketPtr pkt
= transmitList
.front();
196 DPRINTF(DMA
, "Retry on %s addr %#x\n",
197 pkt
->cmdString(), pkt
->getAddr());
198 result
= sendTiming(pkt
);
200 DPRINTF(DMA
, "-- Done\n");
201 transmitList
.pop_front();
205 DPRINTF(DMA
, "-- Failed, queued\n");
207 } while (!backoffTime
&& result
&& transmitList
.size());
209 if (transmitList
.size() && backoffTime
&& !inRetry
) {
210 DPRINTF(DMA
, "Scheduling backoff for %d\n", curTick
+backoffTime
);
211 if (!backoffEvent
.scheduled())
212 schedule(backoffEvent
, backoffTime
+ curTick
);
214 DPRINTF(DMA
, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
215 transmitList
.size(), backoffTime
, inRetry
,
216 backoffEvent
.scheduled());
221 DmaPort::dmaAction(Packet::Command cmd
, Addr addr
, int size
, Event
*event
,
222 uint8_t *data
, Tick delay
)
226 assert(device
->getState() == SimObject::Running
);
228 DmaReqState
*reqState
= new DmaReqState(event
, this, size
, delay
);
231 DPRINTF(DMA
, "Starting DMA for addr: %#x size: %d sched: %d\n", addr
, size
,
233 for (ChunkGenerator
gen(addr
, size
, peerBlockSize());
234 !gen
.done(); gen
.next()) {
235 Request
*req
= new Request(gen
.addr(), gen
.size(), 0);
236 PacketPtr pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
238 // Increment the data pointer on a write
240 pkt
->dataStatic(data
+ gen
.complete());
242 pkt
->senderState
= reqState
;
244 assert(pendingCount
>= 0);
246 DPRINTF(DMA
, "--Queuing DMA for addr: %#x size: %d\n", gen
.addr(),
254 DmaPort::queueDma(PacketPtr pkt
, bool front
)
258 transmitList
.push_front(pkt
);
260 transmitList
.push_back(pkt
);
268 // some kind of selction between access methods
269 // more work is going to have to be done to make
270 // switching actually work
271 assert(transmitList
.size());
272 PacketPtr pkt
= transmitList
.front();
274 Enums::MemoryMode state
= sys
->getMemoryMode();
275 if (state
== Enums::timing
) {
276 if (backoffEvent
.scheduled() || inRetry
) {
277 DPRINTF(DMA
, "Can't send immediately, waiting for retry or backoff timer\n");
281 DPRINTF(DMA
, "Attempting to send %s addr %#x\n",
282 pkt
->cmdString(), pkt
->getAddr());
286 result
= sendTiming(pkt
);
288 transmitList
.pop_front();
289 DPRINTF(DMA
, "-- Done\n");
292 DPRINTF(DMA
, "-- Failed: queued\n");
294 } while (result
&& !backoffTime
&& transmitList
.size());
296 if (transmitList
.size() && backoffTime
&& !inRetry
&&
297 !backoffEvent
.scheduled()) {
298 DPRINTF(DMA
, "-- Scheduling backoff timer for %d\n",
299 backoffTime
+curTick
);
300 schedule(backoffEvent
, backoffTime
+ curTick
);
302 } else if (state
== Enums::atomic
) {
303 transmitList
.pop_front();
306 DPRINTF(DMA
, "--Sending DMA for addr: %#x size: %d\n",
307 pkt
->req
->getPaddr(), pkt
->req
->getSize());
308 lat
= sendAtomic(pkt
);
309 assert(pkt
->senderState
);
310 DmaReqState
*state
= dynamic_cast<DmaReqState
*>(pkt
->senderState
);
312 state
->numBytes
+= pkt
->req
->getSize();
314 DPRINTF(DMA
, "--Received response for DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
315 pkt
->req
->getPaddr(), pkt
->req
->getSize(), state
->numBytes
,
316 state
->totBytes
, state
->completionEvent
->scheduled());
318 if (state
->totBytes
== state
->numBytes
) {
319 assert(!state
->completionEvent
->scheduled());
320 schedule(state
->completionEvent
, curTick
+ lat
+ state
->delay
);
325 assert(pendingCount
>= 0);
328 if (pendingCount
== 0 && drainEvent
) {
329 drainEvent
->process();
334 panic("Unknown memory command state.");
337 DmaDevice::~DmaDevice()