mshr_queue.cc revision 4666
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()->readyTick <= mshr->readyTick) {
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)->readyTick > mshr->readyTick) {
122            return readyList.insert(i, mshr);
123        }
124    }
125    assert(false);
126}
127
128
129MSHR *
130MSHRQueue::allocate(Addr addr, int size, PacketPtr &pkt,
131                    Tick when, Counter order)
132{
133    assert(!freeList.empty());
134    MSHR *mshr = freeList.front();
135    assert(mshr->getNumTargets() == 0);
136    freeList.pop_front();
137
138    mshr->allocate(addr, size, pkt, when, order);
139    mshr->allocIter = allocatedList.insert(allocatedList.end(), mshr);
140    mshr->readyIter = addToReadyList(mshr);
141
142    allocated += 1;
143    return mshr;
144}
145
146
147void
148MSHRQueue::deallocate(MSHR *mshr)
149{
150    deallocateOne(mshr);
151}
152
153MSHR::Iterator
154MSHRQueue::deallocateOne(MSHR *mshr)
155{
156    MSHR::Iterator retval = allocatedList.erase(mshr->allocIter);
157    freeList.push_front(mshr);
158    allocated--;
159    if (mshr->inService) {
160        inServiceEntries--;
161    } else {
162        readyList.erase(mshr->readyIter);
163    }
164    mshr->deallocate();
165    return retval;
166}
167
168void
169MSHRQueue::moveToFront(MSHR *mshr)
170{
171    if (!mshr->inService) {
172        assert(mshr == *(mshr->readyIter));
173        readyList.erase(mshr->readyIter);
174        mshr->readyIter = readyList.insert(readyList.begin(), mshr);
175    }
176}
177
178void
179MSHRQueue::markInService(MSHR *mshr)
180{
181    assert(!mshr->inService);
182    if (mshr->isSimpleForward()) {
183        // we just forwarded the request packet & don't expect a
184        // response, so get rid of it
185        assert(mshr->getNumTargets() == 1);
186        mshr->popTarget();
187        deallocate(mshr);
188        return;
189    }
190    mshr->inService = true;
191    readyList.erase(mshr->readyIter);
192    //mshr->readyIter = NULL;
193    inServiceEntries += 1;
194    //readyList.pop_front();
195}
196
197void
198MSHRQueue::markPending(MSHR *mshr)
199{
200    assert(mshr->inService);
201    mshr->inService = false;
202    --inServiceEntries;
203    /**
204     * @ todo might want to add rerequests to front of pending list for
205     * performance.
206     */
207    mshr->readyIter = addToReadyList(mshr);
208}
209
210void
211MSHRQueue::squash(int threadNum)
212{
213    MSHR::Iterator i = allocatedList.begin();
214    MSHR::Iterator end = allocatedList.end();
215    for (; i != end;) {
216        MSHR *mshr = *i;
217        if (mshr->threadNum == threadNum) {
218            while (mshr->hasTargets()) {
219                mshr->popTarget();
220                assert(0/*target->req->getThreadNum()*/ == threadNum);
221            }
222            assert(!mshr->hasTargets());
223            assert(mshr->ntargets==0);
224            if (!mshr->inService) {
225                i = deallocateOne(mshr);
226            } else {
227                //mshr->pkt->flags &= ~CACHE_LINE_FILL;
228                ++i;
229            }
230        } else {
231            ++i;
232        }
233    }
234}
235