queue.hh revision 13861:7815aef6668f
1/*
2 * Copyright (c) 2012-2013, 2015-2016, 2018 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 *          Andreas Hansson
43 */
44
45/** @file
46 * Declaration of a high-level queue structure
47 */
48
49#ifndef __MEM_CACHE_QUEUE_HH__
50#define __MEM_CACHE_QUEUE_HH__
51
52#include <cassert>
53#include <string>
54#include <type_traits>
55
56#include "base/logging.hh"
57#include "base/trace.hh"
58#include "base/types.hh"
59#include "debug/Drain.hh"
60#include "mem/cache/queue_entry.hh"
61#include "mem/packet.hh"
62#include "sim/core.hh"
63#include "sim/drain.hh"
64
65/**
66 * A high-level queue interface, to be used by both the MSHR queue and
67 * the write buffer.
68 */
69template<class Entry>
70class Queue : public Drainable
71{
72    static_assert(std::is_base_of<QueueEntry, Entry>::value,
73        "Entry must be derived from QueueEntry");
74
75  protected:
76    /** Local label (for functional print requests) */
77    const std::string label;
78
79    /**
80     * The total number of entries in this queue. This number is set
81     * as the number of entries requested plus any reserve. This
82     * allows for the same number of effective entries while still
83     * maintaining an overflow reserve.
84     */
85    const int numEntries;
86
87    /**
88     * The number of entries to hold as a temporary overflow
89     * space. This is used to allow temporary overflow of the number
90     * of entries as we only check the full condition under certain
91     * conditions.
92     */
93    const int numReserve;
94
95    /**  Actual storage. */
96    std::vector<Entry> entries;
97    /** Holds pointers to all allocated entries. */
98    typename Entry::List allocatedList;
99    /** Holds pointers to entries that haven't been sent downstream. */
100    typename Entry::List readyList;
101    /** Holds non allocated entries. */
102    typename Entry::List freeList;
103
104    typename Entry::Iterator addToReadyList(Entry* entry)
105    {
106        if (readyList.empty() ||
107            readyList.back()->readyTime <= entry->readyTime) {
108            return readyList.insert(readyList.end(), entry);
109        }
110
111        for (auto i = readyList.begin(); i != readyList.end(); ++i) {
112            if ((*i)->readyTime > entry->readyTime) {
113                return readyList.insert(i, entry);
114            }
115        }
116        panic("Failed to add to ready list.");
117    }
118
119    /** The number of entries that are in service. */
120    int _numInService;
121
122    /** The number of currently allocated entries. */
123    int allocated;
124
125  public:
126
127    /**
128     * Create a queue with a given number of entries.
129     *
130     * @param num_entries The number of entries in this queue.
131     * @param reserve The extra overflow entries needed.
132     */
133    Queue(const std::string &_label, int num_entries, int reserve) :
134        label(_label), numEntries(num_entries + reserve),
135        numReserve(reserve), entries(numEntries), _numInService(0),
136        allocated(0)
137    {
138        for (int i = 0; i < numEntries; ++i) {
139            freeList.push_back(&entries[i]);
140        }
141    }
142
143    bool isEmpty() const
144    {
145        return allocated == 0;
146    }
147
148    bool isFull() const
149    {
150        return (allocated >= numEntries - numReserve);
151    }
152
153    int numInService() const
154    {
155        return _numInService;
156    }
157
158    /**
159     * Find the first entry that matches the provided address.
160     *
161     * @param blk_addr The block address to find.
162     * @param is_secure True if the target memory space is secure.
163     * @param ignore_uncacheable Should uncacheables be ignored or not
164     * @return Pointer to the matching WriteQueueEntry, null if not found.
165     */
166    Entry* findMatch(Addr blk_addr, bool is_secure,
167                     bool ignore_uncacheable = true) const
168    {
169        for (const auto& entry : allocatedList) {
170            // we ignore any entries allocated for uncacheable
171            // accesses and simply ignore them when matching, in the
172            // cache we never check for matches when adding new
173            // uncacheable entries, and we do not want normal
174            // cacheable accesses being added to an WriteQueueEntry
175            // serving an uncacheable access
176            if (!(ignore_uncacheable && entry->isUncacheable()) &&
177                entry->matchBlockAddr(blk_addr, is_secure)) {
178                return entry;
179            }
180        }
181        return nullptr;
182    }
183
184    bool trySatisfyFunctional(PacketPtr pkt, Addr blk_addr)
185    {
186        pkt->pushLabel(label);
187        for (const auto& entry : allocatedList) {
188            if (entry->matchBlockAddr(blk_addr, pkt->isSecure()) &&
189                entry->trySatisfyFunctional(pkt)) {
190                pkt->popLabel();
191                return true;
192            }
193        }
194        pkt->popLabel();
195        return false;
196    }
197
198    /**
199     * Find any pending requests that overlap the given request of a
200     * different queue.
201     *
202     * @param entry The entry to be compared against.
203     * @return A pointer to the earliest matching entry.
204     */
205    Entry* findPending(const QueueEntry* entry) const
206    {
207        for (const auto& ready_entry : readyList) {
208            if (ready_entry->conflictAddr(entry)) {
209                return ready_entry;
210            }
211        }
212        return nullptr;
213    }
214
215    /**
216     * Returns the WriteQueueEntry at the head of the readyList.
217     * @return The next request to service.
218     */
219    Entry* getNext() const
220    {
221        if (readyList.empty() || readyList.front()->readyTime > curTick()) {
222            return nullptr;
223        }
224        return readyList.front();
225    }
226
227    Tick nextReadyTime() const
228    {
229        return readyList.empty() ? MaxTick : readyList.front()->readyTime;
230    }
231
232    /**
233     * Removes the given entry from the queue. This places the entry
234     * on the free list.
235     *
236     * @param entry
237     */
238    void deallocate(Entry *entry)
239    {
240        allocatedList.erase(entry->allocIter);
241        freeList.push_front(entry);
242        allocated--;
243        if (entry->inService) {
244            _numInService--;
245        } else {
246            readyList.erase(entry->readyIter);
247        }
248        entry->deallocate();
249        if (drainState() == DrainState::Draining && allocated == 0) {
250            // Notify the drain manager that we have completed
251            // draining if there are no other outstanding requests in
252            // this queue.
253            DPRINTF(Drain, "Queue now empty, signalling drained\n");
254            signalDrainDone();
255        }
256    }
257
258    DrainState drain() override
259    {
260        return allocated == 0 ? DrainState::Drained : DrainState::Draining;
261    }
262};
263
264#endif //__MEM_CACHE_QUEUE_HH__
265