mshr_queue.cc revision 4904
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    assert(!mshr->inService);
183    if (mshr->isSimpleForward()) {
184        // we just forwarded the request packet & don't expect a
185        // response, so get rid of it
186        assert(mshr->getNumTargets() == 1);
187        mshr->popTarget();
188        deallocate(mshr);
189        return;
190    }
191    mshr->inService = true;
192    readyList.erase(mshr->readyIter);
193    //mshr->readyIter = NULL;
194    inServiceEntries += 1;
195    //readyList.pop_front();
196}
197
198void
199MSHRQueue::markPending(MSHR *mshr)
200{
201    assert(mshr->inService);
202    mshr->inService = false;
203    --inServiceEntries;
204    /**
205     * @ todo might want to add rerequests to front of pending list for
206     * performance.
207     */
208    mshr->readyIter = addToReadyList(mshr);
209}
210
211void
212MSHRQueue::squash(int threadNum)
213{
214    MSHR::Iterator i = allocatedList.begin();
215    MSHR::Iterator end = allocatedList.end();
216    for (; i != end;) {
217        MSHR *mshr = *i;
218        if (mshr->threadNum == threadNum) {
219            while (mshr->hasTargets()) {
220                mshr->popTarget();
221                assert(0/*target->req->getThreadNum()*/ == threadNum);
222            }
223            assert(!mshr->hasTargets());
224            assert(mshr->ntargets==0);
225            if (!mshr->inService) {
226                i = deallocateOne(mshr);
227            } else {
228                //mshr->pkt->flags &= ~CACHE_LINE_FILL;
229                ++i;
230            }
231        } else {
232            ++i;
233        }
234    }
235}
236