queue.hh revision 12823
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
55#include "base/trace.hh"
56#include "base/types.hh"
57#include "debug/Drain.hh"
58#include "mem/cache/queue_entry.hh"
59#include "mem/packet.hh"
60#include "sim/core.hh"
61#include "sim/drain.hh"
62
63/**
64 * A high-level queue interface, to be used by both the MSHR queue and
65 * the write buffer.
66 */
67template<class Entry>
68class Queue : public Drainable
69{
70  protected:
71    /** Local label (for functional print requests) */
72    const std::string label;
73
74    /**
75     * The total number of entries in this queue. This number is set
76     * as the number of entries requested plus any reserve. This
77     * allows for the same number of effective entries while still
78     * maintaining an overflow reserve.
79     */
80    const int numEntries;
81
82    /**
83     * The number of entries to hold as a temporary overflow
84     * space. This is used to allow temporary overflow of the number
85     * of entries as we only check the full condition under certain
86     * conditions.
87     */
88    const int numReserve;
89
90    /**  Actual storage. */
91    std::vector<Entry> entries;
92    /** Holds pointers to all allocated entries. */
93    typename Entry::List allocatedList;
94    /** Holds pointers to entries that haven't been sent downstream. */
95    typename Entry::List readyList;
96    /** Holds non allocated entries. */
97    typename Entry::List freeList;
98
99    typename Entry::Iterator addToReadyList(Entry* entry)
100    {
101        if (readyList.empty() ||
102            readyList.back()->readyTime <= entry->readyTime) {
103            return readyList.insert(readyList.end(), entry);
104        }
105
106        for (auto i = readyList.begin(); i != readyList.end(); ++i) {
107            if ((*i)->readyTime > entry->readyTime) {
108                return readyList.insert(i, entry);
109            }
110        }
111        assert(false);
112        return readyList.end();  // keep stupid compilers happy
113    }
114
115    /** The number of entries that are in service. */
116    int _numInService;
117
118    /** The number of currently allocated entries. */
119    int allocated;
120
121  public:
122
123    /**
124     * Create a queue with a given number of entries.
125     *
126     * @param num_entries The number of entries in this queue.
127     * @param reserve The extra overflow entries needed.
128     */
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),
132        allocated(0)
133    {
134        for (int i = 0; i < numEntries; ++i) {
135            freeList.push_back(&entries[i]);
136        }
137    }
138
139    bool isEmpty() const
140    {
141        return allocated == 0;
142    }
143
144    bool isFull() const
145    {
146        return (allocated >= numEntries - numReserve);
147    }
148
149    int numInService() const
150    {
151        return _numInService;
152    }
153
154    /**
155     * Find the first entry that matches the provided address.
156     *
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.
161     */
162    Entry* findMatch(Addr blk_addr, bool is_secure,
163                     bool ignore_uncacheable = true) const
164    {
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->blkAddr == blk_addr && entry->isSecure == is_secure) {
174                return entry;
175            }
176        }
177        return nullptr;
178    }
179
180    bool trySatisfyFunctional(PacketPtr pkt, Addr blk_addr)
181    {
182        pkt->pushLabel(label);
183        for (const auto& entry : allocatedList) {
184            if (entry->blkAddr == blk_addr && entry->trySatisfyFunctional(pkt)) {
185                pkt->popLabel();
186                return true;
187            }
188        }
189        pkt->popLabel();
190        return false;
191    }
192
193    /**
194     * Find any pending requests that overlap the given request.
195     * @param blk_addr Block address.
196     * @param is_secure True if the target memory space is secure.
197     * @return A pointer to the earliest matching WriteQueueEntry.
198     */
199    Entry* findPending(Addr blk_addr, bool is_secure) const
200    {
201        for (const auto& entry : readyList) {
202            if (entry->blkAddr == blk_addr && entry->isSecure == is_secure) {
203                return entry;
204            }
205        }
206        return nullptr;
207    }
208
209    /**
210     * Returns the WriteQueueEntry at the head of the readyList.
211     * @return The next request to service.
212     */
213    Entry* getNext() const
214    {
215        if (readyList.empty() || readyList.front()->readyTime > curTick()) {
216            return nullptr;
217        }
218        return readyList.front();
219    }
220
221    Tick nextReadyTime() const
222    {
223        return readyList.empty() ? MaxTick : readyList.front()->readyTime;
224    }
225
226    /**
227     * Removes the given entry from the queue. This places the entry
228     * on the free list.
229     *
230     * @param entry
231     */
232    void deallocate(Entry *entry)
233    {
234        allocatedList.erase(entry->allocIter);
235        freeList.push_front(entry);
236        allocated--;
237        if (entry->inService) {
238            _numInService--;
239        } else {
240            readyList.erase(entry->readyIter);
241        }
242        entry->deallocate();
243        if (drainState() == DrainState::Draining && allocated == 0) {
244            // Notify the drain manager that we have completed
245            // draining if there are no other outstanding requests in
246            // this queue.
247            DPRINTF(Drain, "Queue now empty, signalling drained\n");
248            signalDrainDone();
249        }
250    }
251
252    DrainState drain() override
253    {
254        return allocated == 0 ? DrainState::Drained : DrainState::Draining;
255    }
256};
257
258#endif //__MEM_CACHE_QUEUE_HH__
259