112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2010-2013, 2015 ARM Limited
312855Sgabeblack@google.com * All rights reserved
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612855Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712855Sgabeblack@google.com * property including but not limited to intellectual property relating
812855Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912855Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012855Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112855Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212855Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312855Sgabeblack@google.com *
1412855Sgabeblack@google.com * Copyright (c) 2001-2005 The Regents of The University of Michigan
1512855Sgabeblack@google.com * All rights reserved.
1612855Sgabeblack@google.com *
1712855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1812855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1912855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2112855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2212855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2312855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2412855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2512855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2612855Sgabeblack@google.com * this software without specific prior written permission.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2912855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3012855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3112855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3212855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3312855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3412855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3512855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3612855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3712855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3812855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3912855Sgabeblack@google.com *
4012855Sgabeblack@google.com * Authors: Ron Dreslinski
4112855Sgabeblack@google.com *          Ali Saidi
4212855Sgabeblack@google.com *          Andreas Hansson
4312855Sgabeblack@google.com */
4412855Sgabeblack@google.com
4512855Sgabeblack@google.com#include "mem/simple_mem.hh"
4612855Sgabeblack@google.com
4712855Sgabeblack@google.com#include "base/random.hh"
4812855Sgabeblack@google.com#include "base/trace.hh"
4912855Sgabeblack@google.com#include "debug/Drain.hh"
5012855Sgabeblack@google.com
5112855Sgabeblack@google.comSimpleMemory::SimpleMemory(const SimpleMemoryParams* p) :
5212855Sgabeblack@google.com    AbstractMemory(p),
5312855Sgabeblack@google.com    port(name() + ".port", *this), latency(p->latency),
5412855Sgabeblack@google.com    latency_var(p->latency_var), bandwidth(p->bandwidth), isBusy(false),
5512855Sgabeblack@google.com    retryReq(false), retryResp(false),
5612855Sgabeblack@google.com    releaseEvent([this]{ release(); }, name()),
5712855Sgabeblack@google.com    dequeueEvent([this]{ dequeue(); }, name())
5812855Sgabeblack@google.com{
5912855Sgabeblack@google.com}
6012855Sgabeblack@google.com
6112855Sgabeblack@google.comvoid
6212855Sgabeblack@google.comSimpleMemory::init()
6312855Sgabeblack@google.com{
6412855Sgabeblack@google.com    AbstractMemory::init();
6512855Sgabeblack@google.com
6612855Sgabeblack@google.com    // allow unconnected memories as this is used in several ruby
67    // systems at the moment
68    if (port.isConnected()) {
69        port.sendRangeChange();
70    }
71}
72
73Tick
74SimpleMemory::recvAtomic(PacketPtr pkt)
75{
76    panic_if(pkt->cacheResponding(), "Should not see packets where cache "
77             "is responding");
78
79    access(pkt);
80    return getLatency();
81}
82
83Tick
84SimpleMemory::recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &_backdoor)
85{
86    Tick latency = recvAtomic(pkt);
87
88    if (backdoor.ptr())
89        _backdoor = &backdoor;
90    return latency;
91}
92
93void
94SimpleMemory::recvFunctional(PacketPtr pkt)
95{
96    pkt->pushLabel(name());
97
98    functionalAccess(pkt);
99
100    bool done = false;
101    auto p = packetQueue.begin();
102    // potentially update the packets in our packet queue as well
103    while (!done && p != packetQueue.end()) {
104        done = pkt->trySatisfyFunctional(p->pkt);
105        ++p;
106    }
107
108    pkt->popLabel();
109}
110
111bool
112SimpleMemory::recvTimingReq(PacketPtr pkt)
113{
114    panic_if(pkt->cacheResponding(), "Should not see packets where cache "
115             "is responding");
116
117    panic_if(!(pkt->isRead() || pkt->isWrite()),
118             "Should only see read and writes at memory controller, "
119             "saw %s to %#llx\n", pkt->cmdString(), pkt->getAddr());
120
121    // we should not get a new request after committing to retry the
122    // current one, but unfortunately the CPU violates this rule, so
123    // simply ignore it for now
124    if (retryReq)
125        return false;
126
127    // if we are busy with a read or write, remember that we have to
128    // retry
129    if (isBusy) {
130        retryReq = true;
131        return false;
132    }
133
134    // technically the packet only reaches us after the header delay,
135    // and since this is a memory controller we also need to
136    // deserialise the payload before performing any write operation
137    Tick receive_delay = pkt->headerDelay + pkt->payloadDelay;
138    pkt->headerDelay = pkt->payloadDelay = 0;
139
140    // update the release time according to the bandwidth limit, and
141    // do so with respect to the time it takes to finish this request
142    // rather than long term as it is the short term data rate that is
143    // limited for any real memory
144
145    // calculate an appropriate tick to release to not exceed
146    // the bandwidth limit
147    Tick duration = pkt->getSize() * bandwidth;
148
149    // only consider ourselves busy if there is any need to wait
150    // to avoid extra events being scheduled for (infinitely) fast
151    // memories
152    if (duration != 0) {
153        schedule(releaseEvent, curTick() + duration);
154        isBusy = true;
155    }
156
157    // go ahead and deal with the packet and put the response in the
158    // queue if there is one
159    bool needsResponse = pkt->needsResponse();
160    recvAtomic(pkt);
161    // turn packet around to go back to requester if response expected
162    if (needsResponse) {
163        // recvAtomic() should already have turned packet into
164        // atomic response
165        assert(pkt->isResponse());
166
167        Tick when_to_send = curTick() + receive_delay + getLatency();
168
169        // typically this should be added at the end, so start the
170        // insertion sort with the last element, also make sure not to
171        // re-order in front of some existing packet with the same
172        // address, the latter is important as this memory effectively
173        // hands out exclusive copies (shared is not asserted)
174        auto i = packetQueue.end();
175        --i;
176        while (i != packetQueue.begin() && when_to_send < i->tick &&
177               !i->pkt->matchAddr(pkt))
178            --i;
179
180        // emplace inserts the element before the position pointed to by
181        // the iterator, so advance it one step
182        packetQueue.emplace(++i, pkt, when_to_send);
183
184        if (!retryResp && !dequeueEvent.scheduled())
185            schedule(dequeueEvent, packetQueue.back().tick);
186    } else {
187        pendingDelete.reset(pkt);
188    }
189
190    return true;
191}
192
193void
194SimpleMemory::release()
195{
196    assert(isBusy);
197    isBusy = false;
198    if (retryReq) {
199        retryReq = false;
200        port.sendRetryReq();
201    }
202}
203
204void
205SimpleMemory::dequeue()
206{
207    assert(!packetQueue.empty());
208    DeferredPacket deferred_pkt = packetQueue.front();
209
210    retryResp = !port.sendTimingResp(deferred_pkt.pkt);
211
212    if (!retryResp) {
213        packetQueue.pop_front();
214
215        // if the queue is not empty, schedule the next dequeue event,
216        // otherwise signal that we are drained if we were asked to do so
217        if (!packetQueue.empty()) {
218            // if there were packets that got in-between then we
219            // already have an event scheduled, so use re-schedule
220            reschedule(dequeueEvent,
221                       std::max(packetQueue.front().tick, curTick()), true);
222        } else if (drainState() == DrainState::Draining) {
223            DPRINTF(Drain, "Draining of SimpleMemory complete\n");
224            signalDrainDone();
225        }
226    }
227}
228
229Tick
230SimpleMemory::getLatency() const
231{
232    return latency +
233        (latency_var ? random_mt.random<Tick>(0, latency_var) : 0);
234}
235
236void
237SimpleMemory::recvRespRetry()
238{
239    assert(retryResp);
240
241    dequeue();
242}
243
244Port &
245SimpleMemory::getPort(const std::string &if_name, PortID idx)
246{
247    if (if_name != "port") {
248        return AbstractMemory::getPort(if_name, idx);
249    } else {
250        return port;
251    }
252}
253
254DrainState
255SimpleMemory::drain()
256{
257    if (!packetQueue.empty()) {
258        DPRINTF(Drain, "SimpleMemory Queue has requests, waiting to drain\n");
259        return DrainState::Draining;
260    } else {
261        return DrainState::Drained;
262    }
263}
264
265SimpleMemory::MemoryPort::MemoryPort(const std::string& _name,
266                                     SimpleMemory& _memory)
267    : SlavePort(_name, &_memory), memory(_memory)
268{ }
269
270AddrRangeList
271SimpleMemory::MemoryPort::getAddrRanges() const
272{
273    AddrRangeList ranges;
274    ranges.push_back(memory.getAddrRange());
275    return ranges;
276}
277
278Tick
279SimpleMemory::MemoryPort::recvAtomic(PacketPtr pkt)
280{
281    return memory.recvAtomic(pkt);
282}
283
284Tick
285SimpleMemory::MemoryPort::recvAtomicBackdoor(
286        PacketPtr pkt, MemBackdoorPtr &_backdoor)
287{
288    return memory.recvAtomicBackdoor(pkt, _backdoor);
289}
290
291void
292SimpleMemory::MemoryPort::recvFunctional(PacketPtr pkt)
293{
294    memory.recvFunctional(pkt);
295}
296
297bool
298SimpleMemory::MemoryPort::recvTimingReq(PacketPtr pkt)
299{
300    return memory.recvTimingReq(pkt);
301}
302
303void
304SimpleMemory::MemoryPort::recvRespRetry()
305{
306    memory.recvRespRetry();
307}
308
309SimpleMemory*
310SimpleMemoryParams::create()
311{
312    return new SimpleMemory(this);
313}
314