simple_mem.cc revision 10372:0a810481d511
1/*
2 * Copyright (c) 2010-2013 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ron Dreslinski
41 *          Ali Saidi
42 *          Andreas Hansson
43 */
44
45#include "base/random.hh"
46#include "mem/simple_mem.hh"
47
48using namespace std;
49
50SimpleMemory::SimpleMemory(const SimpleMemoryParams* p) :
51    AbstractMemory(p),
52    port(name() + ".port", *this), latency(p->latency),
53    latency_var(p->latency_var), bandwidth(p->bandwidth), isBusy(false),
54    retryReq(false), retryResp(false),
55    releaseEvent(this), dequeueEvent(this), drainManager(NULL)
56{
57}
58
59void
60SimpleMemory::init()
61{
62    // allow unconnected memories as this is used in several ruby
63    // systems at the moment
64    if (port.isConnected()) {
65        port.sendRangeChange();
66    }
67}
68
69Tick
70SimpleMemory::recvAtomic(PacketPtr pkt)
71{
72    access(pkt);
73    return pkt->memInhibitAsserted() ? 0 : getLatency();
74}
75
76void
77SimpleMemory::recvFunctional(PacketPtr pkt)
78{
79    pkt->pushLabel(name());
80
81    functionalAccess(pkt);
82
83    bool done = false;
84    auto p = packetQueue.begin();
85    // potentially update the packets in our packet queue as well
86    while (!done && p != packetQueue.end()) {
87        done = pkt->checkFunctional(p->pkt);
88        ++p;
89    }
90
91    pkt->popLabel();
92}
93
94bool
95SimpleMemory::recvTimingReq(PacketPtr pkt)
96{
97    /// @todo temporary hack to deal with memory corruption issues until
98    /// 4-phase transactions are complete
99    for (int x = 0; x < pendingDelete.size(); x++)
100        delete pendingDelete[x];
101    pendingDelete.clear();
102
103    if (pkt->memInhibitAsserted()) {
104        // snooper will supply based on copy of packet
105        // still target's responsibility to delete packet
106        pendingDelete.push_back(pkt);
107        return true;
108    }
109
110    // we should never get a new request after committing to retry the
111    // current one, the bus violates the rule as it simply sends a
112    // retry to the next one waiting on the retry list, so simply
113    // ignore it
114    if (retryReq)
115        return false;
116
117    // if we are busy with a read or write, remember that we have to
118    // retry
119    if (isBusy) {
120        retryReq = true;
121        return false;
122    }
123
124    // @todo someone should pay for this
125    pkt->busFirstWordDelay = pkt->busLastWordDelay = 0;
126
127    // update the release time according to the bandwidth limit, and
128    // do so with respect to the time it takes to finish this request
129    // rather than long term as it is the short term data rate that is
130    // limited for any real memory
131
132    // only look at reads and writes when determining if we are busy,
133    // and for how long, as it is not clear what to regulate for the
134    // other types of commands
135    if (pkt->isRead() || pkt->isWrite()) {
136        // calculate an appropriate tick to release to not exceed
137        // the bandwidth limit
138        Tick duration = pkt->getSize() * bandwidth;
139
140        // only consider ourselves busy if there is any need to wait
141        // to avoid extra events being scheduled for (infinitely) fast
142        // memories
143        if (duration != 0) {
144            schedule(releaseEvent, curTick() + duration);
145            isBusy = true;
146        }
147    }
148
149    // go ahead and deal with the packet and put the response in the
150    // queue if there is one
151    bool needsResponse = pkt->needsResponse();
152    recvAtomic(pkt);
153    // turn packet around to go back to requester if response expected
154    if (needsResponse) {
155        // recvAtomic() should already have turned packet into
156        // atomic response
157        assert(pkt->isResponse());
158        // to keep things simple (and in order), we put the packet at
159        // the end even if the latency suggests it should be sent
160        // before the packet(s) before it
161        packetQueue.push_back(DeferredPacket(pkt, curTick() + getLatency()));
162        if (!retryResp && !dequeueEvent.scheduled())
163            schedule(dequeueEvent, packetQueue.back().tick);
164    } else {
165        pendingDelete.push_back(pkt);
166    }
167
168    return true;
169}
170
171void
172SimpleMemory::release()
173{
174    assert(isBusy);
175    isBusy = false;
176    if (retryReq) {
177        retryReq = false;
178        port.sendRetry();
179    }
180}
181
182void
183SimpleMemory::dequeue()
184{
185    assert(!packetQueue.empty());
186    DeferredPacket deferred_pkt = packetQueue.front();
187
188    retryResp = !port.sendTimingResp(deferred_pkt.pkt);
189
190    if (!retryResp) {
191        packetQueue.pop_front();
192
193        // if the queue is not empty, schedule the next dequeue event,
194        // otherwise signal that we are drained if we were asked to do so
195        if (!packetQueue.empty()) {
196            // if there were packets that got in-between then we
197            // already have an event scheduled, so use re-schedule
198            reschedule(dequeueEvent,
199                       std::max(packetQueue.front().tick, curTick()), true);
200        } else if (drainManager) {
201            drainManager->signalDrainDone();
202            drainManager = NULL;
203        }
204    }
205}
206
207Tick
208SimpleMemory::getLatency() const
209{
210    return latency +
211        (latency_var ? random_mt.random<Tick>(0, latency_var) : 0);
212}
213
214void
215SimpleMemory::recvRetry()
216{
217    assert(retryResp);
218
219    dequeue();
220}
221
222BaseSlavePort &
223SimpleMemory::getSlavePort(const std::string &if_name, PortID idx)
224{
225    if (if_name != "port") {
226        return MemObject::getSlavePort(if_name, idx);
227    } else {
228        return port;
229    }
230}
231
232unsigned int
233SimpleMemory::drain(DrainManager *dm)
234{
235    int count = 0;
236
237    // also track our internal queue
238    if (!packetQueue.empty()) {
239        count += 1;
240        drainManager = dm;
241    }
242
243    if (count)
244        setDrainState(Drainable::Draining);
245    else
246        setDrainState(Drainable::Drained);
247    return count;
248}
249
250SimpleMemory::MemoryPort::MemoryPort(const std::string& _name,
251                                     SimpleMemory& _memory)
252    : SlavePort(_name, &_memory), memory(_memory)
253{ }
254
255AddrRangeList
256SimpleMemory::MemoryPort::getAddrRanges() const
257{
258    AddrRangeList ranges;
259    ranges.push_back(memory.getAddrRange());
260    return ranges;
261}
262
263Tick
264SimpleMemory::MemoryPort::recvAtomic(PacketPtr pkt)
265{
266    return memory.recvAtomic(pkt);
267}
268
269void
270SimpleMemory::MemoryPort::recvFunctional(PacketPtr pkt)
271{
272    memory.recvFunctional(pkt);
273}
274
275bool
276SimpleMemory::MemoryPort::recvTimingReq(PacketPtr pkt)
277{
278    return memory.recvTimingReq(pkt);
279}
280
281void
282SimpleMemory::MemoryPort::recvRetry()
283{
284    memory.recvRetry();
285}
286
287SimpleMemory*
288SimpleMemoryParams::create()
289{
290    return new SimpleMemory(this);
291}
292