2 * Copyright (c) 2010-2013, 2015 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "mem/simple_mem.hh"
43 #include "base/random.hh"
44 #include "base/trace.hh"
45 #include "debug/Drain.hh"
47 SimpleMemory::SimpleMemory(const SimpleMemoryParams
&p
) :
49 port(name() + ".port", *this), latency(p
.latency
),
50 latency_var(p
.latency_var
), bandwidth(p
.bandwidth
), isBusy(false),
51 retryReq(false), retryResp(false),
52 releaseEvent([this]{ release(); }, name()),
53 dequeueEvent([this]{ dequeue(); }, name())
60 AbstractMemory::init();
62 // allow unconnected memories as this is used in several ruby
63 // systems at the moment
64 if (port
.isConnected()) {
65 port
.sendRangeChange();
70 SimpleMemory::recvAtomic(PacketPtr pkt
)
72 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
80 SimpleMemory::recvAtomicBackdoor(PacketPtr pkt
, MemBackdoorPtr
&_backdoor
)
82 Tick latency
= recvAtomic(pkt
);
85 _backdoor
= &backdoor
;
90 SimpleMemory::recvFunctional(PacketPtr pkt
)
92 pkt
->pushLabel(name());
94 functionalAccess(pkt
);
97 auto p
= packetQueue
.begin();
98 // potentially update the packets in our packet queue as well
99 while (!done
&& p
!= packetQueue
.end()) {
100 done
= pkt
->trySatisfyFunctional(p
->pkt
);
108 SimpleMemory::recvTimingReq(PacketPtr pkt
)
110 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
113 panic_if(!(pkt
->isRead() || pkt
->isWrite()),
114 "Should only see read and writes at memory controller, "
115 "saw %s to %#llx\n", pkt
->cmdString(), pkt
->getAddr());
117 // we should not get a new request after committing to retry the
118 // current one, but unfortunately the CPU violates this rule, so
119 // simply ignore it for now
123 // if we are busy with a read or write, remember that we have to
130 // technically the packet only reaches us after the header delay,
131 // and since this is a memory controller we also need to
132 // deserialise the payload before performing any write operation
133 Tick receive_delay
= pkt
->headerDelay
+ pkt
->payloadDelay
;
134 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
136 // update the release time according to the bandwidth limit, and
137 // do so with respect to the time it takes to finish this request
138 // rather than long term as it is the short term data rate that is
139 // limited for any real memory
141 // calculate an appropriate tick to release to not exceed
142 // the bandwidth limit
143 Tick duration
= pkt
->getSize() * bandwidth
;
145 // only consider ourselves busy if there is any need to wait
146 // to avoid extra events being scheduled for (infinitely) fast
149 schedule(releaseEvent
, curTick() + duration
);
153 // go ahead and deal with the packet and put the response in the
154 // queue if there is one
155 bool needsResponse
= pkt
->needsResponse();
157 // turn packet around to go back to requestor if response expected
159 // recvAtomic() should already have turned packet into
161 assert(pkt
->isResponse());
163 Tick when_to_send
= curTick() + receive_delay
+ getLatency();
165 // typically this should be added at the end, so start the
166 // insertion sort with the last element, also make sure not to
167 // re-order in front of some existing packet with the same
168 // address, the latter is important as this memory effectively
169 // hands out exclusive copies (shared is not asserted)
170 auto i
= packetQueue
.end();
172 while (i
!= packetQueue
.begin() && when_to_send
< i
->tick
&&
173 !i
->pkt
->matchAddr(pkt
))
176 // emplace inserts the element before the position pointed to by
177 // the iterator, so advance it one step
178 packetQueue
.emplace(++i
, pkt
, when_to_send
);
180 if (!retryResp
&& !dequeueEvent
.scheduled())
181 schedule(dequeueEvent
, packetQueue
.back().tick
);
183 pendingDelete
.reset(pkt
);
190 SimpleMemory::release()
201 SimpleMemory::dequeue()
203 assert(!packetQueue
.empty());
204 DeferredPacket deferred_pkt
= packetQueue
.front();
206 retryResp
= !port
.sendTimingResp(deferred_pkt
.pkt
);
209 packetQueue
.pop_front();
211 // if the queue is not empty, schedule the next dequeue event,
212 // otherwise signal that we are drained if we were asked to do so
213 if (!packetQueue
.empty()) {
214 // if there were packets that got in-between then we
215 // already have an event scheduled, so use re-schedule
216 reschedule(dequeueEvent
,
217 std::max(packetQueue
.front().tick
, curTick()), true);
218 } else if (drainState() == DrainState::Draining
) {
219 DPRINTF(Drain
, "Draining of SimpleMemory complete\n");
226 SimpleMemory::getLatency() const
229 (latency_var
? random_mt
.random
<Tick
>(0, latency_var
) : 0);
233 SimpleMemory::recvRespRetry()
241 SimpleMemory::getPort(const std::string
&if_name
, PortID idx
)
243 if (if_name
!= "port") {
244 return AbstractMemory::getPort(if_name
, idx
);
251 SimpleMemory::drain()
253 if (!packetQueue
.empty()) {
254 DPRINTF(Drain
, "SimpleMemory Queue has requests, waiting to drain\n");
255 return DrainState::Draining
;
257 return DrainState::Drained
;
261 SimpleMemory::MemoryPort::MemoryPort(const std::string
& _name
,
262 SimpleMemory
& _memory
)
263 : ResponsePort(_name
, &_memory
), memory(_memory
)
267 SimpleMemory::MemoryPort::getAddrRanges() const
269 AddrRangeList ranges
;
270 ranges
.push_back(memory
.getAddrRange());
275 SimpleMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
277 return memory
.recvAtomic(pkt
);
281 SimpleMemory::MemoryPort::recvAtomicBackdoor(
282 PacketPtr pkt
, MemBackdoorPtr
&_backdoor
)
284 return memory
.recvAtomicBackdoor(pkt
, _backdoor
);
288 SimpleMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
290 memory
.recvFunctional(pkt
);
294 SimpleMemory::MemoryPort::recvTimingReq(PacketPtr pkt
)
296 return memory
.recvTimingReq(pkt
);
300 SimpleMemory::MemoryPort::recvRespRetry()
302 memory
.recvRespRetry();