mem-cache: Remove Packet dependency in 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 * Authors: Daniel Carvalho
29 */
30
31 /**
32 * @file
33 * Declaration of a sector set associative tag store.
34 */
35
36 #ifndef __MEM_CACHE_TAGS_SECTOR_TAGS_HH__
37 #define __MEM_CACHE_TAGS_SECTOR_TAGS_HH__
38
39 #include <string>
40 #include <vector>
41
42 #include "mem/cache/sector_blk.hh"
43 #include "mem/cache/tags/base.hh"
44 #include "params/SectorTags.hh"
45
46 class BaseReplacementPolicy;
47 class ReplaceableEntry;
48
49 /**
50 * A SectorTags cache tag store.
51 * @sa \ref gem5MemorySystem "gem5 Memory System"
52 *
53 * The SectorTags placement policy divides the cache into s sectors of w
54 * consecutive sectors (ways). Each sector then consists of a number of
55 * sequential cache lines that may or may not be present.
56 */
57 class SectorTags : public BaseTags
58 {
59 protected:
60 /** Typedef the set type used in this tag store. */
61 typedef std::vector<SectorBlk*> SetType;
62
63 /** The associativity of the cache. */
64 const unsigned assoc;
65 /** The allocatable associativity of the cache (alloc mask). */
66 unsigned allocAssoc;
67
68 /** Whether tags and data are accessed sequentially. */
69 const bool sequentialAccess;
70
71 /** Replacement policy */
72 BaseReplacementPolicy *replacementPolicy;
73
74 /** Number of data blocks per sector. */
75 const unsigned numBlocksPerSector;
76
77 /** The number of sectors in the cache. */
78 const unsigned numSectors;
79 /** The number of sets in the cache. */
80 const unsigned numSets;
81
82 /** The cache blocks. */
83 std::vector<SectorSubBlk> blks;
84 /** The cache sector blocks. */
85 std::vector<SectorBlk> secBlks;
86 /** The cache sets. */
87 std::vector<SetType> sets;
88
89 // Organization of an address: Tag | Set # | Sector Offset # | Offset #
90 /** The amount to shift the address to get the sector tag. */
91 const int sectorShift;
92 /** The amount to shift the address to get the set. */
93 const int setShift;
94 /** The amount to shift the address to get the tag. */
95 const int tagShift;
96
97 /** Mask out all bits that aren't part of the sector tag. */
98 const unsigned sectorMask;
99 /** Mask out all bits that aren't part of the set index. */
100 const unsigned setMask;
101
102 public:
103 /** Convenience typedef. */
104 typedef SectorTagsParams Params;
105
106 /**
107 * Construct and initialize this tag store.
108 */
109 SectorTags(const Params *p);
110
111 /**
112 * Destructor.
113 */
114 virtual ~SectorTags() {};
115
116 /**
117 * This function updates the tags when a block is invalidated but does
118 * not invalidate the block itself. It also updates the replacement data.
119 *
120 * @param blk The block to invalidate.
121 */
122 void invalidate(CacheBlk *blk) override;
123
124 /**
125 * Access block and update replacement data. May not succeed, in which
126 * case nullptr is returned. This has all the implications of a cache
127 * access and should only be used as such. Returns the access latency
128 * as a side effect.
129 *
130 * @param addr The address to find.
131 * @param is_secure True if the target memory space is secure.
132 * @param lat The access latency.
133 * @return Pointer to the cache block if found.
134 */
135 CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override;
136
137 /**
138 * Find all possible block locations for insertion and replacement of
139 * an address. Should be called immediately before ReplacementPolicy's
140 * findVictim() not to break cache resizing.
141 * Returns sector blocks in all ways belonging to the set of the address.
142 *
143 * @param addr The addr to a find possible locations for.
144 * @return The possible locations.
145 */
146 virtual const std::vector<SectorBlk*> getPossibleLocations(Addr addr)
147 const;
148
149 /**
150 * Insert the new block into the cache and update replacement data.
151 *
152 * @param addr Address of the block.
153 * @param is_secure Whether the block is in secure space or not.
154 * @param src_master_ID The source requestor ID.
155 * @param task_ID The new task ID.
156 * @param blk The block to update.
157 */
158 void insertBlock(const Addr addr, const bool is_secure,
159 const int src_master_ID, const uint32_t task_ID,
160 CacheBlk *blk) override;
161
162 /**
163 * Finds the given address in the cache, do not update replacement data.
164 * i.e. This is a no-side-effect find of a block.
165 *
166 * @param addr The address to find.
167 * @param is_secure True if the target memory space is secure.
168 * @return Pointer to the cache block if found.
169 */
170 CacheBlk* findBlock(Addr addr, bool is_secure) const override;
171
172 /**
173 * Find a sector block given set and way.
174 *
175 * @param set The set of the block.
176 * @param way The way of the block.
177 * @return The block.
178 */
179 ReplaceableEntry* findBlockBySetAndWay(int set, int way) const override;
180
181 /**
182 * Find replacement victim based on address.
183 *
184 * @param addr Address to find a victim for.
185 * @param is_secure True if the target memory space is secure.
186 * @param evict_blks Cache blocks to be evicted.
187 * @return Cache block to be replaced.
188 */
189 CacheBlk* findVictim(Addr addr, const bool is_secure,
190 std::vector<CacheBlk*>& evict_blks) const override;
191
192 /**
193 * Generate the sector tag from the given address.
194 *
195 * @param addr The address to get the sector tag from.
196 * @return The sector tag of the address.
197 */
198 Addr extractTag(Addr addr) const override;
199
200 /**
201 * Calculate the set index from the address.
202 *
203 * @param addr The address to get the set from.
204 * @return The set index of the address.
205 */
206 int extractSet(Addr addr) const;
207
208 /**
209 * Calculate a block's offset in a sector from the address.
210 *
211 * @param addr The address to get the offset from.
212 * @return Offset of the corresponding block within its sector.
213 */
214 int extractSectorOffset(Addr addr) const;
215
216 /**
217 * Regenerate the block address from the tag and set.
218 *
219 * @param block The block.
220 * @return the block address.
221 */
222 Addr regenerateBlkAddr(const CacheBlk* blk) const override;
223
224 /**
225 * Visit each sub-block in the tags and apply a visitor.
226 *
227 * The visitor should be a std::function that takes a cache block.
228 * reference as its parameter.
229 *
230 * @param visitor Visitor to call on each block.
231 */
232 void forEachBlk(std::function<void(CacheBlk &)> visitor) override;
233
234 /**
235 * Find if any of the sub-blocks satisfies a condition.
236 *
237 * The visitor should be a std::function that takes a cache block
238 * reference as its parameter. The visitor will terminate the
239 * traversal early if the condition is satisfied.
240 *
241 * @param visitor Visitor to call on each block.
242 */
243 bool anyBlk(std::function<bool(CacheBlk &)> visitor) override;
244 };
245
246 #endif //__MEM_CACHE_TAGS_SECTOR_TAGS_HH__