2 * Copyright (c) 2013 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 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "mem/dramsim3.hh"
40 #include "base/callback.hh"
41 #include "base/trace.hh"
42 #include "debug/DRAMsim3.hh"
43 #include "debug/Drain.hh"
44 #include "sim/system.hh"
46 DRAMsim3::DRAMsim3(const Params
&p
) :
48 port(name() + ".port", *this),
49 read_cb(std::bind(&DRAMsim3::readComplete
,
50 this, 0, std::placeholders::_1
)),
51 write_cb(std::bind(&DRAMsim3::writeComplete
,
52 this, 0, std::placeholders::_1
)),
53 wrapper(p
.configFile
, p
.filePath
, read_cb
, write_cb
),
54 retryReq(false), retryResp(false), startTick(0),
55 nbrOutstandingReads(0), nbrOutstandingWrites(0),
56 sendResponseEvent([this]{ sendResponse(); }, name()),
57 tickEvent([this]{ tick(); }, name())
60 "Instantiated DRAMsim3 with clock %d ns and queue size %d\n",
61 wrapper
.clockPeriod(), wrapper
.queueSize());
63 // Register a callback to compensate for the destructor not
64 // being called. The callback prints the DRAMsim3 stats.
65 registerExitCallback([this]() { wrapper
.printStats(); });
71 AbstractMemory::init();
73 if (!port
.isConnected()) {
74 fatal("DRAMsim3 %s is unconnected!\n", name());
76 port
.sendRangeChange();
79 if (system()->cacheLineSize() != wrapper
.burstSize())
80 fatal("DRAMsim3 burst size %d does not match cache line size %d\n",
81 wrapper
.burstSize(), system()->cacheLineSize());
87 startTick
= curTick();
89 // kick off the clock ticks
90 schedule(tickEvent
, clockEdge());
94 DRAMsim3::resetStats() {
99 DRAMsim3::sendResponse()
102 assert(!responseQueue
.empty());
104 DPRINTF(DRAMsim3
, "Attempting to send response\n");
106 bool success
= port
.sendTimingResp(responseQueue
.front());
108 responseQueue
.pop_front();
110 DPRINTF(DRAMsim3
, "Have %d read, %d write, %d responses outstanding\n",
111 nbrOutstandingReads
, nbrOutstandingWrites
,
112 responseQueue
.size());
114 if (!responseQueue
.empty() && !sendResponseEvent
.scheduled())
115 schedule(sendResponseEvent
, curTick());
117 if (nbrOutstanding() == 0)
122 DPRINTF(DRAMsim3
, "Waiting for response retry\n");
124 assert(!sendResponseEvent
.scheduled());
129 DRAMsim3::nbrOutstanding() const
131 return nbrOutstandingReads
+ nbrOutstandingWrites
+ responseQueue
.size();
137 // Only tick when it's timing mode
138 if (system()->isTimingMode()) {
141 // is the connected port waiting for a retry, if so check the
142 // state and send a retry if conditions have changed
143 if (retryReq
&& nbrOutstanding() < wrapper
.queueSize()) {
149 schedule(tickEvent
, curTick() + wrapper
.clockPeriod() * SimClock::Int::ns
);
153 DRAMsim3::recvAtomic(PacketPtr pkt
)
157 // 50 ns is just an arbitrary value at this point
158 return pkt
->cacheResponding() ? 0 : 50000;
162 DRAMsim3::recvFunctional(PacketPtr pkt
)
164 pkt
->pushLabel(name());
166 functionalAccess(pkt
);
168 // potentially update the packets in our response queue as well
169 for (auto i
= responseQueue
.begin(); i
!= responseQueue
.end(); ++i
)
170 pkt
->trySatisfyFunctional(*i
);
176 DRAMsim3::recvTimingReq(PacketPtr pkt
)
178 // if a cache is responding, sink the packet without further action
179 if (pkt
->cacheResponding()) {
180 pendingDelete
.reset(pkt
);
184 // we should not get a new request after committing to retry the
185 // current one, but unfortunately the CPU violates this rule, so
186 // simply ignore it for now
190 // if we cannot accept we need to send a retry once progress can
192 bool can_accept
= nbrOutstanding() < wrapper
.queueSize();
194 // keep track of the transaction
197 outstandingReads
[pkt
->getAddr()].push(pkt
);
199 // we count a transaction as outstanding until it has left the
200 // queue in the controller, and the response has been sent
201 // back, note that this will differ for reads and writes
202 ++nbrOutstandingReads
;
204 } else if (pkt
->isWrite()) {
206 outstandingWrites
[pkt
->getAddr()].push(pkt
);
208 ++nbrOutstandingWrites
;
210 // perform the access for writes
211 accessAndRespond(pkt
);
214 // keep it simple and just respond if necessary
215 accessAndRespond(pkt
);
220 // we should never have a situation when we think there is space,
222 assert(wrapper
.canAccept(pkt
->getAddr(), pkt
->isWrite()));
224 DPRINTF(DRAMsim3
, "Enqueueing address %lld\n", pkt
->getAddr());
226 // @todo what about the granularity here, implicit assumption that
227 // a transaction matches the burst size of the memory (which we
228 // cannot determine without parsing the ini file ourselves)
229 wrapper
.enqueue(pkt
->getAddr(), pkt
->isWrite());
239 DRAMsim3::recvRespRetry()
241 DPRINTF(DRAMsim3
, "Retrying\n");
249 DRAMsim3::accessAndRespond(PacketPtr pkt
)
251 DPRINTF(DRAMsim3
, "Access for address %lld\n", pkt
->getAddr());
253 bool needsResponse
= pkt
->needsResponse();
255 // do the actual memory access which also turns the packet into a
259 // turn packet around to go back to requestor if response expected
261 // access already turned the packet into a response
262 assert(pkt
->isResponse());
263 // Here we pay for xbar additional delay and to process the payload
265 Tick time
= curTick() + pkt
->headerDelay
+ pkt
->payloadDelay
;
266 // Reset the timings of the packet
267 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
269 DPRINTF(DRAMsim3
, "Queuing response for address %lld\n",
272 // queue it to be sent back
273 responseQueue
.push_back(pkt
);
275 // if we are not already waiting for a retry, or are scheduled
276 // to send a response, schedule an event
277 if (!retryResp
&& !sendResponseEvent
.scheduled())
278 schedule(sendResponseEvent
, time
);
280 // queue the packet for deletion
281 pendingDelete
.reset(pkt
);
285 void DRAMsim3::readComplete(unsigned id
, uint64_t addr
)
288 DPRINTF(DRAMsim3
, "Read to address %lld complete\n", addr
);
290 // get the outstanding reads for the address in question
291 auto p
= outstandingReads
.find(addr
);
292 assert(p
!= outstandingReads
.end());
294 // first in first out, which is not necessarily true, but it is
295 // the best we can do at this point
296 PacketPtr pkt
= p
->second
.front();
299 if (p
->second
.empty())
300 outstandingReads
.erase(p
);
302 // no need to check for drain here as the next call will add a
303 // response to the response queue straight away
304 assert(nbrOutstandingReads
!= 0);
305 --nbrOutstandingReads
;
307 // perform the actual memory access
308 accessAndRespond(pkt
);
311 void DRAMsim3::writeComplete(unsigned id
, uint64_t addr
)
314 DPRINTF(DRAMsim3
, "Write to address %lld complete\n", addr
);
316 // get the outstanding reads for the address in question
317 auto p
= outstandingWrites
.find(addr
);
318 assert(p
!= outstandingWrites
.end());
320 // we have already responded, and this is only to keep track of
321 // what is outstanding
323 if (p
->second
.empty())
324 outstandingWrites
.erase(p
);
326 assert(nbrOutstandingWrites
!= 0);
327 --nbrOutstandingWrites
;
329 if (nbrOutstanding() == 0)
334 DRAMsim3::getPort(const std::string
&if_name
, PortID idx
)
336 if (if_name
!= "port") {
337 return ClockedObject::getPort(if_name
, idx
);
346 // check our outstanding reads and writes and if any they need to
348 return nbrOutstanding() != 0 ? DrainState::Draining
: DrainState::Drained
;
351 DRAMsim3::MemoryPort::MemoryPort(const std::string
& _name
,
353 : ResponsePort(_name
, &_memory
), memory(_memory
)
357 DRAMsim3::MemoryPort::getAddrRanges() const
359 AddrRangeList ranges
;
360 ranges
.push_back(memory
.getAddrRange());
365 DRAMsim3::MemoryPort::recvAtomic(PacketPtr pkt
)
367 return memory
.recvAtomic(pkt
);
371 DRAMsim3::MemoryPort::recvFunctional(PacketPtr pkt
)
373 memory
.recvFunctional(pkt
);
377 DRAMsim3::MemoryPort::recvTimingReq(PacketPtr pkt
)
379 // pass it to the memory controller
380 return memory
.recvTimingReq(pkt
);
384 DRAMsim3::MemoryPort::recvRespRetry()
386 memory
.recvRespRetry();