mshr_queue.cc revision 4908
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 */
30
31/** @file
32 * Definition of MSHRQueue class functions.
33 */
34
35#include "mem/cache/miss/mshr_queue.hh"
36
37using namespace std;
38
39MSHRQueue::MSHRQueue(int num_entries, int reserve, int _index)
40    : numEntries(num_entries + reserve - 1), numReserve(reserve),
41      index(_index)
42{
43    allocated = 0;
44    inServiceEntries = 0;
45    registers = new MSHR[numEntries];
46    for (int i = 0; i < numEntries; ++i) {
47        registers[i].queue = this;
48        freeList.push_back(&registers[i]);
49    }
50}
51
52MSHRQueue::~MSHRQueue()
53{
54    delete [] registers;
55}
56
57MSHR *
58MSHRQueue::findMatch(Addr addr) const
59{
60    MSHR::ConstIterator i = allocatedList.begin();
61    MSHR::ConstIterator end = allocatedList.end();
62    for (; i != end; ++i) {
63        MSHR *mshr = *i;
64        if (mshr->addr == addr) {
65            return mshr;
66        }
67    }
68    return NULL;
69}
70
71bool
72MSHRQueue::findMatches(Addr addr, vector<MSHR*>& matches) const
73{
74    // Need an empty vector
75    assert(matches.empty());
76    bool retval = false;
77    MSHR::ConstIterator i = allocatedList.begin();
78    MSHR::ConstIterator end = allocatedList.end();
79    for (; i != end; ++i) {
80        MSHR *mshr = *i;
81        if (mshr->addr == addr) {
82            retval = true;
83            matches.push_back(mshr);
84        }
85    }
86    return retval;
87
88}
89
90MSHR *
91MSHRQueue::findPending(Addr addr, int size) const
92{
93    MSHR::ConstIterator i = readyList.begin();
94    MSHR::ConstIterator end = readyList.end();
95    for (; i != end; ++i) {
96        MSHR *mshr = *i;
97        if (mshr->addr < addr) {
98            if (mshr->addr + mshr->size > addr) {
99                return mshr;
100            }
101        } else {
102            if (addr + size > mshr->addr) {
103                return mshr;
104            }
105        }
106    }
107    return NULL;
108}
109
110
111MSHR::Iterator
112MSHRQueue::addToReadyList(MSHR *mshr)
113{
114    if (readyList.empty() || readyList.back()->readyTime <= mshr->readyTime) {
115        return readyList.insert(readyList.end(), mshr);
116    }
117
118    MSHR::Iterator i = readyList.begin();
119    MSHR::Iterator end = readyList.end();
120    for (; i != end; ++i) {
121        if ((*i)->readyTime > mshr->readyTime) {
122            return readyList.insert(i, mshr);
123        }
124    }
125    assert(false);
126    return end;  // keep stupid compilers happy
127}
128
129
130MSHR *
131MSHRQueue::allocate(Addr addr, int size, PacketPtr &pkt,
132                    Tick when, Counter order)
133{
134    assert(!freeList.empty());
135    MSHR *mshr = freeList.front();
136    assert(mshr->getNumTargets() == 0);
137    freeList.pop_front();
138
139    mshr->allocate(addr, size, pkt, when, order);
140    mshr->allocIter = allocatedList.insert(allocatedList.end(), mshr);
141    mshr->readyIter = addToReadyList(mshr);
142
143    allocated += 1;
144    return mshr;
145}
146
147
148void
149MSHRQueue::deallocate(MSHR *mshr)
150{
151    deallocateOne(mshr);
152}
153
154MSHR::Iterator
155MSHRQueue::deallocateOne(MSHR *mshr)
156{
157    MSHR::Iterator retval = allocatedList.erase(mshr->allocIter);
158    freeList.push_front(mshr);
159    allocated--;
160    if (mshr->inService) {
161        inServiceEntries--;
162    } else {
163        readyList.erase(mshr->readyIter);
164    }
165    mshr->deallocate();
166    return retval;
167}
168
169void
170MSHRQueue::moveToFront(MSHR *mshr)
171{
172    if (!mshr->inService) {
173        assert(mshr == *(mshr->readyIter));
174        readyList.erase(mshr->readyIter);
175        mshr->readyIter = readyList.insert(readyList.begin(), mshr);
176    }
177}
178
179void
180MSHRQueue::markInService(MSHR *mshr)
181{
182    if (mshr->markInService()) {
183        deallocate(mshr);
184    } else {
185        readyList.erase(mshr->readyIter);
186        inServiceEntries += 1;
187    }
188}
189
190void
191MSHRQueue::markPending(MSHR *mshr)
192{
193    assert(mshr->inService);
194    mshr->inService = false;
195    --inServiceEntries;
196    /**
197     * @ todo might want to add rerequests to front of pending list for
198     * performance.
199     */
200    mshr->readyIter = addToReadyList(mshr);
201}
202
203void
204MSHRQueue::squash(int threadNum)
205{
206    MSHR::Iterator i = allocatedList.begin();
207    MSHR::Iterator end = allocatedList.end();
208    for (; i != end;) {
209        MSHR *mshr = *i;
210        if (mshr->threadNum == threadNum) {
211            while (mshr->hasTargets()) {
212                mshr->popTarget();
213                assert(0/*target->req->getThreadNum()*/ == threadNum);
214            }
215            assert(!mshr->hasTargets());
216            assert(mshr->ntargets==0);
217            if (!mshr->inService) {
218                i = deallocateOne(mshr);
219            } else {
220                //mshr->pkt->flags &= ~CACHE_LINE_FILL;
221                ++i;
222            }
223        } else {
224            ++i;
225        }
226    }
227}
228