mem-cache: Add function to move blocks in the tags
[gem5.git] / src / mem / cache / tags / sector_tags.hh
1 /*
2 * Copyright (c) 2018 Inria
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
29 /**
30 * @file
31 * Declaration of a sector tag store.
32 */
33
34 #ifndef __MEM_CACHE_TAGS_SECTOR_TAGS_HH__
35 #define __MEM_CACHE_TAGS_SECTOR_TAGS_HH__
36
37 #include <cstdint>
38 #include <string>
39 #include <vector>
40
41 #include "base/statistics.hh"
42 #include "mem/cache/tags/base.hh"
43 #include "mem/cache/tags/sector_blk.hh"
44 #include "mem/packet.hh"
45 #include "params/SectorTags.hh"
46
47 namespace ReplacementPolicy {
48 class Base;
49 }
50 class ReplaceableEntry;
51
52 /**
53 * A SectorTags cache tag store.
54 * @sa \ref gem5MemorySystem "gem5 Memory System"
55 *
56 * The SectorTags placement policy divides the cache into s sectors of w
57 * consecutive sectors (ways). Each sector then consists of a number of
58 * sequential cache lines that may or may not be present.
59 */
60 class SectorTags : public BaseTags
61 {
62 private:
63 /** The cache blocks. */
64 std::vector<SectorSubBlk> blks;
65 /** The cache sector blocks. */
66 std::vector<SectorBlk> secBlks;
67
68 protected:
69 /** The allocatable associativity of the cache (alloc mask). */
70 unsigned allocAssoc;
71
72 /** Whether tags and data are accessed sequentially. */
73 const bool sequentialAccess;
74
75 /** Replacement policy */
76 ReplacementPolicy::Base *replacementPolicy;
77
78 /** Number of data blocks per sector. */
79 const unsigned numBlocksPerSector;
80
81 /** The number of sectors in the cache. */
82 const unsigned numSectors;
83
84 // Organization of an address:
85 // Tag | Placement Location | Sector Offset # | Offset #
86 /** The amount to shift the address to get the sector tag. */
87 const int sectorShift;
88
89 /** Mask out all bits that aren't part of the sector tag. */
90 const unsigned sectorMask;
91
92 struct SectorTagsStats : public Stats::Group
93 {
94 const SectorTags& tags;
95
96 SectorTagsStats(BaseTagStats &base_group, SectorTags& _tags);
97
98 void regStats() override;
99
100 /** Number of sub-blocks evicted due to a replacement. */
101 Stats::Vector evictionsReplacement;
102 } sectorStats;
103
104 public:
105 /** Convenience typedef. */
106 typedef SectorTagsParams Params;
107
108 /**
109 * Construct and initialize this tag store.
110 */
111 SectorTags(const Params &p);
112
113 /**
114 * Destructor.
115 */
116 virtual ~SectorTags() {};
117
118 /**
119 * Initialize blocks as SectorBlk and SectorSubBlk instances.
120 */
121 void tagsInit() override;
122
123 /**
124 * This function updates the tags when a block is invalidated but does
125 * not invalidate the block itself. It also updates the replacement data.
126 *
127 * @param blk The block to invalidate.
128 */
129 void invalidate(CacheBlk *blk) override;
130
131 /**
132 * Access block and update replacement data. May not succeed, in which
133 * case nullptr is returned. This has all the implications of a cache
134 * access and should only be used as such. Returns the tag lookup latency
135 * as a side effect.
136 *
137 * @param addr The address to find.
138 * @param is_secure True if the target memory space is secure.
139 * @param lat The latency of the tag lookup.
140 * @return Pointer to the cache block if found.
141 */
142 CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override;
143
144 /**
145 * Insert the new block into the cache and update replacement data.
146 *
147 * @param pkt Packet holding the address to update
148 * @param blk The block to update.
149 */
150 void insertBlock(const PacketPtr pkt, CacheBlk *blk) override;
151
152 void moveBlock(CacheBlk *src_blk, CacheBlk *dest_blk) override;
153
154 /**
155 * Finds the given address in the cache, do not update replacement data.
156 * i.e. This is a no-side-effect find of a block.
157 *
158 * @param addr The address to find.
159 * @param is_secure True if the target memory space is secure.
160 * @return Pointer to the cache block if found.
161 */
162 CacheBlk* findBlock(Addr addr, bool is_secure) const override;
163
164 /**
165 * Find replacement victim based on address.
166 *
167 * @param addr Address to find a victim for.
168 * @param is_secure True if the target memory space is secure.
169 * @param size Size, in bits, of new block to allocate.
170 * @param evict_blks Cache blocks to be evicted.
171 * @return Cache block to be replaced.
172 */
173 CacheBlk* findVictim(Addr addr, const bool is_secure,
174 const std::size_t size,
175 std::vector<CacheBlk*>& evict_blks) override;
176
177 /**
178 * Calculate a block's offset in a sector from the address.
179 *
180 * @param addr The address to get the offset from.
181 * @return Offset of the corresponding block within its sector.
182 */
183 int extractSectorOffset(Addr addr) const;
184
185 /**
186 * Regenerate the block address from the tag and location.
187 *
188 * @param block The block.
189 * @return the block address.
190 */
191 Addr regenerateBlkAddr(const CacheBlk* blk) const override;
192
193 /**
194 * Visit each sub-block in the tags and apply a visitor.
195 *
196 * The visitor should be a std::function that takes a cache block.
197 * reference as its parameter.
198 *
199 * @param visitor Visitor to call on each block.
200 */
201 void forEachBlk(std::function<void(CacheBlk &)> visitor) override;
202
203 /**
204 * Find if any of the sub-blocks satisfies a condition.
205 *
206 * The visitor should be a std::function that takes a cache block
207 * reference as its parameter. The visitor will terminate the
208 * traversal early if the condition is satisfied.
209 *
210 * @param visitor Visitor to call on each block.
211 */
212 bool anyBlk(std::function<bool(CacheBlk &)> visitor) override;
213 };
214
215 #endif //__MEM_CACHE_TAGS_SECTOR_TAGS_HH__