2 * Copyright (c) 2012-2013, 2015-2016, 2018 ARM Limited
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.
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
42 * Declaration of a high-level queue structure
45 #ifndef __MEM_CACHE_QUEUE_HH__
46 #define __MEM_CACHE_QUEUE_HH__
50 #include <type_traits>
52 #include "base/logging.hh"
53 #include "base/trace.hh"
54 #include "base/types.hh"
55 #include "debug/Drain.hh"
56 #include "mem/cache/queue_entry.hh"
57 #include "mem/packet.hh"
58 #include "sim/core.hh"
59 #include "sim/drain.hh"
62 * A high-level queue interface, to be used by both the MSHR queue and
66 class Queue : public Drainable
68 static_assert(std::is_base_of<QueueEntry, Entry>::value,
69 "Entry must be derived from QueueEntry");
72 /** Local label (for functional print requests) */
73 const std::string label;
76 * The total number of entries in this queue. This number is set
77 * as the number of entries requested plus any reserve. This
78 * allows for the same number of effective entries while still
79 * maintaining an overflow reserve.
84 * The number of entries to hold as a temporary overflow
85 * space. This is used to allow temporary overflow of the number
86 * of entries as we only check the full condition under certain
91 /** Actual storage. */
92 std::vector<Entry> entries;
93 /** Holds pointers to all allocated entries. */
94 typename Entry::List allocatedList;
95 /** Holds pointers to entries that haven't been sent downstream. */
96 typename Entry::List readyList;
97 /** Holds non allocated entries. */
98 typename Entry::List freeList;
100 typename Entry::Iterator addToReadyList(Entry* entry)
102 if (readyList.empty() ||
103 readyList.back()->readyTime <= entry->readyTime) {
104 return readyList.insert(readyList.end(), entry);
107 for (auto i = readyList.begin(); i != readyList.end(); ++i) {
108 if ((*i)->readyTime > entry->readyTime) {
109 return readyList.insert(i, entry);
112 panic("Failed to add to ready list.");
115 /** The number of entries that are in service. */
118 /** The number of currently allocated entries. */
124 * Create a queue with a given number of entries.
126 * @param num_entries The number of entries in this queue.
127 * @param reserve The extra overflow entries needed.
129 Queue(const std::string &_label, int num_entries, int reserve) :
130 label(_label), numEntries(num_entries + reserve),
131 numReserve(reserve), entries(numEntries), _numInService(0),
134 for (int i = 0; i < numEntries; ++i) {
135 freeList.push_back(&entries[i]);
141 return allocated == 0;
146 return (allocated >= numEntries - numReserve);
149 int numInService() const
151 return _numInService;
155 * Find the first entry that matches the provided address.
157 * @param blk_addr The block address to find.
158 * @param is_secure True if the target memory space is secure.
159 * @param ignore_uncacheable Should uncacheables be ignored or not
160 * @return Pointer to the matching WriteQueueEntry, null if not found.
162 Entry* findMatch(Addr blk_addr, bool is_secure,
163 bool ignore_uncacheable = true) const
165 for (const auto& entry : allocatedList) {
166 // we ignore any entries allocated for uncacheable
167 // accesses and simply ignore them when matching, in the
168 // cache we never check for matches when adding new
169 // uncacheable entries, and we do not want normal
170 // cacheable accesses being added to an WriteQueueEntry
171 // serving an uncacheable access
172 if (!(ignore_uncacheable && entry->isUncacheable()) &&
173 entry->matchBlockAddr(blk_addr, is_secure)) {
180 bool trySatisfyFunctional(PacketPtr pkt)
182 pkt->pushLabel(label);
183 for (const auto& entry : allocatedList) {
184 if (entry->matchBlockAddr(pkt) &&
185 entry->trySatisfyFunctional(pkt)) {
195 * Find any pending requests that overlap the given request of a
198 * @param entry The entry to be compared against.
199 * @return A pointer to the earliest matching entry.
201 Entry* findPending(const QueueEntry* entry) const
203 for (const auto& ready_entry : readyList) {
204 if (ready_entry->conflictAddr(entry)) {
212 * Returns the WriteQueueEntry at the head of the readyList.
213 * @return The next request to service.
215 Entry* getNext() const
217 if (readyList.empty() || readyList.front()->readyTime > curTick()) {
220 return readyList.front();
223 Tick nextReadyTime() const
225 return readyList.empty() ? MaxTick : readyList.front()->readyTime;
229 * Removes the given entry from the queue. This places the entry
234 void deallocate(Entry *entry)
236 allocatedList.erase(entry->allocIter);
237 freeList.push_front(entry);
239 if (entry->inService) {
242 readyList.erase(entry->readyIter);
245 if (drainState() == DrainState::Draining && allocated == 0) {
246 // Notify the drain manager that we have completed
247 // draining if there are no other outstanding requests in
249 DPRINTF(Drain, "Queue now empty, signalling drained\n");
254 DrainState drain() override
256 return allocated == 0 ? DrainState::Drained : DrainState::Draining;
260 #endif //__MEM_CACHE_QUEUE_HH__