mshr_queue.cc revision 4628
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 = pendingList.begin();
94    MSHR::ConstIterator end = pendingList.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
110MSHR *
111MSHRQueue::allocate(Addr addr, int size, PacketPtr &pkt)
112{
113    assert(!freeList.empty());
114    MSHR *mshr = freeList.front();
115    assert(mshr->getNumTargets() == 0);
116    freeList.pop_front();
117
118    mshr->allocate(addr, size, pkt);
119    mshr->allocIter = allocatedList.insert(allocatedList.end(), mshr);
120    mshr->readyIter = pendingList.insert(pendingList.end(), mshr);
121
122    allocated += 1;
123    return mshr;
124}
125
126
127void
128MSHRQueue::deallocate(MSHR *mshr)
129{
130    deallocateOne(mshr);
131}
132
133MSHR::Iterator
134MSHRQueue::deallocateOne(MSHR *mshr)
135{
136    MSHR::Iterator retval = allocatedList.erase(mshr->allocIter);
137    freeList.push_front(mshr);
138    allocated--;
139    if (mshr->inService) {
140        inServiceEntries--;
141    } else {
142        pendingList.erase(mshr->readyIter);
143    }
144    mshr->deallocate();
145    return retval;
146}
147
148void
149MSHRQueue::moveToFront(MSHR *mshr)
150{
151    if (!mshr->inService) {
152        assert(mshr == *(mshr->readyIter));
153        pendingList.erase(mshr->readyIter);
154        mshr->readyIter = pendingList.insert(pendingList.begin(), mshr);
155    }
156}
157
158void
159MSHRQueue::markInService(MSHR *mshr)
160{
161    //assert(mshr == pendingList.front());
162#if 0
163    if (!mshr->pkt->needsResponse() && !(mshr->pkt->cmd == MemCmd::UpgradeReq)) {
164        assert(mshr->getNumTargets() == 0);
165        deallocate(mshr);
166        return;
167    }
168#endif
169    mshr->inService = true;
170    pendingList.erase(mshr->readyIter);
171    //mshr->readyIter = NULL;
172    inServiceEntries += 1;
173    //pendingList.pop_front();
174}
175
176void
177MSHRQueue::markPending(MSHR *mshr)
178{
179    //assert(mshr->readyIter == NULL);
180    mshr->inService = false;
181    --inServiceEntries;
182    /**
183     * @ todo might want to add rerequests to front of pending list for
184     * performance.
185     */
186    mshr->readyIter = pendingList.insert(pendingList.end(), mshr);
187}
188
189void
190MSHRQueue::squash(int threadNum)
191{
192    MSHR::Iterator i = allocatedList.begin();
193    MSHR::Iterator end = allocatedList.end();
194    for (; i != end;) {
195        MSHR *mshr = *i;
196        if (mshr->threadNum == threadNum) {
197            while (mshr->hasTargets()) {
198                mshr->popTarget();
199                assert(0/*target->req->getThreadNum()*/ == threadNum);
200            }
201            assert(!mshr->hasTargets());
202            assert(mshr->ntargets==0);
203            if (!mshr->inService) {
204                i = deallocateOne(mshr);
205            } else {
206                //mshr->pkt->flags &= ~CACHE_LINE_FILL;
207                ++i;
208            }
209        } else {
210            ++i;
211        }
212    }
213}
214