mshr_queue.cc revision 10764
1/*
2 * Copyright (c) 2012-2013, 2015 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) 2003-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: Erik Hallnor
41 *          Andreas Sandberg
42 */
43
44/** @file
45 * Definition of MSHRQueue class functions.
46 */
47
48#include "base/trace.hh"
49#include "mem/cache/mshr_queue.hh"
50#include "debug/Drain.hh"
51
52using namespace std;
53
54MSHRQueue::MSHRQueue(const std::string &_label,
55                     int num_entries, int reserve, int demand_reserve,
56                     int _index)
57    : label(_label), numEntries(num_entries + reserve - 1),
58      numReserve(reserve), demandReserve(demand_reserve),
59      registers(numEntries), drainManager(NULL), allocated(0),
60      inServiceEntries(0), index(_index)
61{
62    for (int i = 0; i < numEntries; ++i) {
63        registers[i].queue = this;
64        freeList.push_back(&registers[i]);
65    }
66}
67
68MSHR *
69MSHRQueue::findMatch(Addr blk_addr, bool is_secure) const
70{
71    MSHR::ConstIterator i = allocatedList.begin();
72    MSHR::ConstIterator end = allocatedList.end();
73    for (; i != end; ++i) {
74        MSHR *mshr = *i;
75        if (mshr->blkAddr == blk_addr && mshr->isSecure == is_secure) {
76            return mshr;
77        }
78    }
79    return NULL;
80}
81
82bool
83MSHRQueue::findMatches(Addr blk_addr, bool is_secure,
84                       vector<MSHR*>& matches) const
85{
86    // Need an empty vector
87    assert(matches.empty());
88    bool retval = false;
89    MSHR::ConstIterator i = allocatedList.begin();
90    MSHR::ConstIterator end = allocatedList.end();
91    for (; i != end; ++i) {
92        MSHR *mshr = *i;
93        if (mshr->blkAddr == blk_addr && mshr->isSecure == is_secure) {
94            retval = true;
95            matches.push_back(mshr);
96        }
97    }
98    return retval;
99}
100
101
102bool
103MSHRQueue::checkFunctional(PacketPtr pkt, Addr blk_addr)
104{
105    pkt->pushLabel(label);
106    MSHR::ConstIterator i = allocatedList.begin();
107    MSHR::ConstIterator end = allocatedList.end();
108    for (; i != end; ++i) {
109        MSHR *mshr = *i;
110        if (mshr->blkAddr == blk_addr && mshr->checkFunctional(pkt)) {
111            pkt->popLabel();
112            return true;
113        }
114    }
115    pkt->popLabel();
116    return false;
117}
118
119
120MSHR *
121MSHRQueue::findPending(Addr blk_addr, bool is_secure) const
122{
123    MSHR::ConstIterator i = readyList.begin();
124    MSHR::ConstIterator end = readyList.end();
125    for (; i != end; ++i) {
126        MSHR *mshr = *i;
127        if (mshr->blkAddr == blk_addr && mshr->isSecure == is_secure) {
128            return mshr;
129        }
130    }
131    return NULL;
132}
133
134
135MSHR::Iterator
136MSHRQueue::addToReadyList(MSHR *mshr)
137{
138    if (readyList.empty() || readyList.back()->readyTime <= mshr->readyTime) {
139        return readyList.insert(readyList.end(), mshr);
140    }
141
142    MSHR::Iterator i = readyList.begin();
143    MSHR::Iterator end = readyList.end();
144    for (; i != end; ++i) {
145        if ((*i)->readyTime > mshr->readyTime) {
146            return readyList.insert(i, mshr);
147        }
148    }
149    assert(false);
150    return end;  // keep stupid compilers happy
151}
152
153
154MSHR *
155MSHRQueue::allocate(Addr blk_addr, unsigned blk_size, PacketPtr pkt,
156                    Tick when_ready, Counter order)
157{
158    assert(!freeList.empty());
159    MSHR *mshr = freeList.front();
160    assert(mshr->getNumTargets() == 0);
161    freeList.pop_front();
162
163    mshr->allocate(blk_addr, blk_size, pkt, when_ready, order);
164    mshr->allocIter = allocatedList.insert(allocatedList.end(), mshr);
165    mshr->readyIter = addToReadyList(mshr);
166
167    allocated += 1;
168    return mshr;
169}
170
171
172void
173MSHRQueue::deallocate(MSHR *mshr)
174{
175    deallocateOne(mshr);
176}
177
178MSHR::Iterator
179MSHRQueue::deallocateOne(MSHR *mshr)
180{
181    MSHR::Iterator retval = allocatedList.erase(mshr->allocIter);
182    freeList.push_front(mshr);
183    allocated--;
184    if (mshr->inService) {
185        inServiceEntries--;
186    } else {
187        readyList.erase(mshr->readyIter);
188    }
189    mshr->deallocate();
190    if (drainManager && allocated == 0) {
191        // Notify the drain manager that we have completed draining if
192        // there are no other outstanding requests in this MSHR queue.
193        DPRINTF(Drain, "MSHRQueue now empty, signalling drained\n");
194        drainManager->signalDrainDone();
195        drainManager = NULL;
196        setDrainState(Drainable::Drained);
197    }
198    return retval;
199}
200
201void
202MSHRQueue::moveToFront(MSHR *mshr)
203{
204    if (!mshr->inService) {
205        assert(mshr == *(mshr->readyIter));
206        readyList.erase(mshr->readyIter);
207        mshr->readyIter = readyList.insert(readyList.begin(), mshr);
208    }
209}
210
211void
212MSHRQueue::markInService(MSHR *mshr, bool pending_dirty_resp)
213{
214    if (mshr->markInService(pending_dirty_resp)) {
215        deallocate(mshr);
216    } else {
217        readyList.erase(mshr->readyIter);
218        inServiceEntries += 1;
219    }
220}
221
222void
223MSHRQueue::markPending(MSHR *mshr)
224{
225    assert(mshr->inService);
226    mshr->inService = false;
227    --inServiceEntries;
228    /**
229     * @ todo might want to add rerequests to front of pending list for
230     * performance.
231     */
232    mshr->readyIter = addToReadyList(mshr);
233}
234
235bool
236MSHRQueue::forceDeallocateTarget(MSHR *mshr)
237{
238    bool was_full = isFull();
239    assert(mshr->hasTargets());
240    // Pop the prefetch off of the target list
241    mshr->popTarget();
242    // Delete mshr if no remaining targets
243    if (!mshr->hasTargets() && !mshr->promoteDeferredTargets()) {
244        deallocateOne(mshr);
245    }
246
247    // Notify if MSHR queue no longer full
248    return was_full && !isFull();
249}
250
251void
252MSHRQueue::squash(int threadNum)
253{
254    MSHR::Iterator i = allocatedList.begin();
255    MSHR::Iterator end = allocatedList.end();
256    for (; i != end;) {
257        MSHR *mshr = *i;
258        if (mshr->threadNum == threadNum) {
259            while (mshr->hasTargets()) {
260                mshr->popTarget();
261                assert(0/*target->req->threadId()*/ == threadNum);
262            }
263            assert(!mshr->hasTargets());
264            assert(mshr->getNumTargets()==0);
265            if (!mshr->inService) {
266                i = deallocateOne(mshr);
267            } else {
268                //mshr->pkt->flags &= ~CACHE_LINE_FILL;
269                ++i;
270            }
271        } else {
272            ++i;
273        }
274    }
275}
276
277unsigned int
278MSHRQueue::drain(DrainManager *dm)
279{
280    if (allocated == 0) {
281        setDrainState(Drainable::Drained);
282        return 0;
283    } else {
284        drainManager = dm;
285        setDrainState(Drainable::Draining);
286        return 1;
287    }
288}
289