mem-cache: Forward declare ReplaceableEntry
[gem5.git] / src / mem / cache / tags / cacheset.hh
1 /*
2 * Copyright (c) 2013 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) 2009 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: Lisa Hsu
41 */
42
43 /**
44 * @file
45 * Declaration of an associative set
46 */
47
48 #ifndef __MEM_CACHE_TAGS_CACHESET_HH__
49 #define __MEM_CACHE_TAGS_CACHESET_HH__
50
51 #include <cassert>
52 #include <vector>
53
54 #include "base/types.hh"
55
56 /**
57 * An associative set of cache blocks.
58 */
59 template <class Blktype>
60 class CacheSet
61 {
62 public:
63 /** The associativity of this set. */
64 int assoc;
65
66 /** Cache blocks in this set, maintained in LRU order 0 = MRU. */
67 std::vector<Blktype*> blks;
68
69 /**
70 * Find a block matching the tag in this set.
71 * @param way_id The id of the way that matches the tag.
72 * @param tag The Tag to find.
73 * @param is_secure True if the target memory space is secure.
74 * @return Pointer to the block if found. Set way_id to assoc if none found
75 */
76 Blktype* findBlk(Addr tag, bool is_secure, int& way_id) const ;
77 Blktype* findBlk(Addr tag, bool is_secure) const ;
78
79 /**
80 * Move the given block to the head of the list.
81 * @param blk The block to move.
82 */
83 void moveToHead(Blktype *blk);
84
85 /**
86 * Move the given block to the tail of the list.
87 * @param blk The block to move
88 */
89 void moveToTail(Blktype *blk);
90
91 };
92
93 template <class Blktype>
94 Blktype*
95 CacheSet<Blktype>::findBlk(Addr tag, bool is_secure, int& way_id) const
96 {
97 /**
98 * Way_id returns the id of the way that matches the block
99 * If no block is found way_id is set to assoc.
100 */
101 way_id = assoc;
102 for (int i = 0; i < assoc; ++i) {
103 if (blks[i]->tag == tag && blks[i]->isValid() &&
104 blks[i]->isSecure() == is_secure) {
105 way_id = i;
106 return blks[i];
107 }
108 }
109 return nullptr;
110 }
111
112 template <class Blktype>
113 Blktype*
114 CacheSet<Blktype>::findBlk(Addr tag, bool is_secure) const
115 {
116 int ignored_way_id;
117 return findBlk(tag, is_secure, ignored_way_id);
118 }
119
120 template <class Blktype>
121 void
122 CacheSet<Blktype>::moveToHead(Blktype *blk)
123 {
124 // nothing to do if blk is already head
125 if (blks[0] == blk)
126 return;
127
128 // write 'next' block into blks[i], moving up from MRU toward LRU
129 // until we overwrite the block we moved to head.
130
131 // start by setting up to write 'blk' into blks[0]
132 int i = 0;
133 Blktype *next = blk;
134
135 do {
136 assert(i < assoc);
137 std::swap(blks[i], next);
138 ++i;
139 } while (next != blk);
140 }
141
142 template <class Blktype>
143 void
144 CacheSet<Blktype>::moveToTail(Blktype *blk)
145 {
146 // nothing to do if blk is already tail
147 if (blks[assoc - 1] == blk)
148 return;
149
150 // write 'next' block into blks[i], moving from LRU to MRU
151 // until we overwrite the block we moved to tail.
152
153 // start by setting up to write 'blk' into tail
154 int i = assoc - 1;
155 Blktype *next = blk;
156
157 do {
158 assert(i >= 0);
159 std::swap(blks[i], next);
160 --i;
161 } while (next != blk);
162 }
163
164 #endif