sector_tags.cc (13218:5e7df60c6cab) | sector_tags.cc (13219:454ecc63338d) |
---|---|
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; --- 16 unchanged lines hidden (view full) --- 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 | 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; --- 16 unchanged lines hidden (view full) --- 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 * Definitions of a base set associative sector tag store. | 33 * Definitions of a sector tag store. |
34 */ 35 36#include "mem/cache/tags/sector_tags.hh" 37 38#include <cassert> 39#include <memory> 40#include <string> 41 42#include "base/intmath.hh" 43#include "base/logging.hh" 44#include "base/types.hh" 45#include "debug/CacheRepl.hh" 46#include "mem/cache/base.hh" 47#include "mem/cache/replacement_policies/base.hh" | 34 */ 35 36#include "mem/cache/tags/sector_tags.hh" 37 38#include <cassert> 39#include <memory> 40#include <string> 41 42#include "base/intmath.hh" 43#include "base/logging.hh" 44#include "base/types.hh" 45#include "debug/CacheRepl.hh" 46#include "mem/cache/base.hh" 47#include "mem/cache/replacement_policies/base.hh" |
48#include "mem/cache/tags/indexing_policies/base.hh" |
|
48 49SectorTags::SectorTags(const SectorTagsParams *p) | 49 50SectorTags::SectorTags(const SectorTagsParams *p) |
50 : BaseTags(p), assoc(p->assoc), allocAssoc(p->assoc), | 51 : BaseTags(p), allocAssoc(p->assoc), |
51 sequentialAccess(p->sequential_access), 52 replacementPolicy(p->replacement_policy), 53 numBlocksPerSector(p->num_blocks_per_sector), | 52 sequentialAccess(p->sequential_access), 53 replacementPolicy(p->replacement_policy), 54 numBlocksPerSector(p->num_blocks_per_sector), |
54 numSectors(numBlocks / p->num_blocks_per_sector), 55 numSets(numSectors / p->assoc), 56 blks(numBlocks), secBlks(numSectors), sets(numSets), 57 sectorShift(floorLog2(blkSize)), 58 setShift(sectorShift + floorLog2(numBlocksPerSector)), 59 tagShift(setShift + floorLog2(numSets)), 60 sectorMask(numBlocksPerSector - 1), setMask(numSets - 1) | 55 numSectors(numBlocks / p->num_blocks_per_sector), blks(numBlocks), 56 secBlks(numSectors), sectorShift(floorLog2(blkSize)), 57 sectorMask(numBlocksPerSector - 1) |
61{ 62 // Check parameters 63 fatal_if(blkSize < 4 || !isPowerOf2(blkSize), 64 "Block size must be at least 4 and a power of 2"); | 58{ 59 // Check parameters 60 fatal_if(blkSize < 4 || !isPowerOf2(blkSize), 61 "Block size must be at least 4 and a power of 2"); |
65 fatal_if(!isPowerOf2(numSets), 66 "# of sets must be non-zero and a power of 2"); | |
67 fatal_if(!isPowerOf2(numBlocksPerSector), 68 "# of blocks per sector must be non-zero and a power of 2"); | 62 fatal_if(!isPowerOf2(numBlocksPerSector), 63 "# of blocks per sector must be non-zero and a power of 2"); |
69 fatal_if(assoc <= 0, "associativity must be greater than zero"); | |
70} 71 72void 73SectorTags::init(BaseCache* cache) 74{ 75 // Set parent cache 76 setCache(cache); 77 | 64} 65 66void 67SectorTags::init(BaseCache* cache) 68{ 69 // Set parent cache 70 setCache(cache); 71 |
78 // Initialize all sets 79 unsigned sec_blk_index = 0; // index into sector blks array | 72 // Initialize all blocks |
80 unsigned blk_index = 0; // index into blks array | 73 unsigned blk_index = 0; // index into blks array |
81 for (unsigned i = 0; i < numSets; ++i) { 82 sets[i].resize(assoc); | 74 for (unsigned sec_blk_index = 0; sec_blk_index < numSectors; 75 sec_blk_index++) 76 { 77 // Locate next cache sector 78 SectorBlk* sec_blk = &secBlks[sec_blk_index]; |
83 | 79 |
84 // Initialize all sectors in this set 85 for (unsigned j = 0; j < assoc; ++j) { 86 // Select block within the set to be linked 87 SectorBlk*& sec_blk = sets[i][j]; | 80 // Link block to indexing policy 81 indexingPolicy->setEntry(sec_blk, sec_blk_index); |
88 | 82 |
89 // Locate next cache sector 90 sec_blk = &secBlks[sec_blk_index]; | 83 // Associate a replacement data entry to the sector 84 sec_blk->replacementData = replacementPolicy->instantiateEntry(); |
91 | 85 |
92 // Associate a replacement data entry to the sector 93 sec_blk->replacementData = replacementPolicy->instantiateEntry(); | 86 // Initialize all blocks in this sector 87 sec_blk->blks.resize(numBlocksPerSector); 88 for (unsigned k = 0; k < numBlocksPerSector; ++k){ 89 // Select block within the set to be linked 90 SectorSubBlk*& blk = sec_blk->blks[k]; |
94 | 91 |
95 // Set its index 96 sec_blk->setPosition(i, j); | 92 // Locate next cache block 93 blk = &blks[blk_index]; |
97 | 94 |
98 // Initialize all blocks in this sector 99 sec_blk->blks.resize(numBlocksPerSector); 100 for (unsigned k = 0; k < numBlocksPerSector; ++k){ 101 // Select block within the set to be linked 102 SectorSubBlk*& blk = sec_blk->blks[k]; | 95 // Associate a data chunk to the block 96 blk->data = &dataBlks[blkSize*blk_index]; |
103 | 97 |
104 // Locate next cache block 105 blk = &blks[blk_index]; | 98 // Associate sector block to this block 99 blk->setSectorBlock(sec_blk); |
106 | 100 |
107 // Associate a data chunk to the block 108 blk->data = &dataBlks[blkSize*blk_index]; | 101 // Associate the sector replacement data to this block 102 blk->replacementData = sec_blk->replacementData; |
109 | 103 |
110 // Associate sector block to this block 111 blk->setSectorBlock(sec_blk); | 104 // Set its index and sector offset 105 blk->setSectorOffset(k); |
112 | 106 |
113 // Associate the sector replacement data to this block 114 blk->replacementData = sec_blk->replacementData; 115 116 // Set its index and sector offset 117 blk->setPosition(i, j); 118 blk->setSectorOffset(k); 119 120 // Update block index 121 ++blk_index; 122 } 123 124 // Update sector block index 125 ++sec_blk_index; | 107 // Update block index 108 ++blk_index; |
126 } 127 } 128} 129 130void 131SectorTags::invalidate(CacheBlk *blk) 132{ 133 BaseTags::invalidate(blk); --- 57 unchanged lines hidden (view full) --- 191 } else { 192 // If a cache miss 193 lat = lookupLatency; 194 } 195 196 return blk; 197} 198 | 109 } 110 } 111} 112 113void 114SectorTags::invalidate(CacheBlk *blk) 115{ 116 BaseTags::invalidate(blk); --- 57 unchanged lines hidden (view full) --- 174 } else { 175 // If a cache miss 176 lat = lookupLatency; 177 } 178 179 return blk; 180} 181 |
199std::vector<ReplaceableEntry*> 200SectorTags::getPossibleLocations(const Addr addr) const 201{ 202 std::vector<ReplaceableEntry*> locations; 203 for (const auto& blk : sets[extractSet(addr)]) { 204 locations.push_back(static_cast<ReplaceableEntry*>(blk)); 205 } 206 return locations; 207} 208 | |
209void 210SectorTags::insertBlock(const Addr addr, const bool is_secure, 211 const int src_master_ID, const uint32_t task_ID, 212 CacheBlk *blk) 213{ 214 // Do common block insertion functionality 215 BaseTags::insertBlock(addr, is_secure, src_master_ID, task_ID, blk); 216 --- 21 unchanged lines hidden (view full) --- 238{ 239 // Extract sector tag 240 const Addr tag = extractTag(addr); 241 242 // The address can only be mapped to a specific location of a sector 243 // due to sectors being composed of contiguous-address entries 244 const Addr offset = extractSectorOffset(addr); 245 | 182void 183SectorTags::insertBlock(const Addr addr, const bool is_secure, 184 const int src_master_ID, const uint32_t task_ID, 185 CacheBlk *blk) 186{ 187 // Do common block insertion functionality 188 BaseTags::insertBlock(addr, is_secure, src_master_ID, task_ID, blk); 189 --- 21 unchanged lines hidden (view full) --- 211{ 212 // Extract sector tag 213 const Addr tag = extractTag(addr); 214 215 // The address can only be mapped to a specific location of a sector 216 // due to sectors being composed of contiguous-address entries 217 const Addr offset = extractSectorOffset(addr); 218 |
246 // Find all possible sector locations for the given address 247 const std::vector<ReplaceableEntry*> locations = 248 getPossibleLocations(addr); | 219 // Find all possible sector entries that may contain the given address 220 const std::vector<ReplaceableEntry*> entries = 221 indexingPolicy->getPossibleEntries(addr); |
249 250 // Search for block | 222 223 // Search for block |
251 for (const auto& sector : locations) { | 224 for (const auto& sector : entries) { |
252 auto blk = static_cast<SectorBlk*>(sector)->blks[offset]; 253 if (blk->getTag() == tag && blk->isValid() && 254 blk->isSecure() == is_secure) { 255 return blk; 256 } 257 } 258 259 // Did not find block 260 return nullptr; 261} 262 | 225 auto blk = static_cast<SectorBlk*>(sector)->blks[offset]; 226 if (blk->getTag() == tag && blk->isValid() && 227 blk->isSecure() == is_secure) { 228 return blk; 229 } 230 } 231 232 // Did not find block 233 return nullptr; 234} 235 |
263ReplaceableEntry* 264SectorTags::findBlockBySetAndWay(int set, int way) const 265{ 266 return sets[set][way]; 267} 268 | |
269CacheBlk* 270SectorTags::findVictim(Addr addr, const bool is_secure, 271 std::vector<CacheBlk*>& evict_blks) const 272{ | 236CacheBlk* 237SectorTags::findVictim(Addr addr, const bool is_secure, 238 std::vector<CacheBlk*>& evict_blks) const 239{ |
273 // Get all possible locations of this sector 274 const std::vector<ReplaceableEntry*> sector_locations = 275 getPossibleLocations(addr); | 240 // Get possible entries to be victimized 241 const std::vector<ReplaceableEntry*> sector_entries = 242 indexingPolicy->getPossibleEntries(addr); |
276 277 // Check if the sector this address belongs to has been allocated 278 Addr tag = extractTag(addr); 279 SectorBlk* victim_sector = nullptr; | 243 244 // Check if the sector this address belongs to has been allocated 245 Addr tag = extractTag(addr); 246 SectorBlk* victim_sector = nullptr; |
280 for (const auto& sector : sector_locations) { | 247 for (const auto& sector : sector_entries) { |
281 SectorBlk* sector_blk = static_cast<SectorBlk*>(sector); 282 if ((tag == sector_blk->getTag()) && sector_blk->isValid() && 283 (is_secure == sector_blk->isSecure())){ 284 victim_sector = sector_blk; 285 break; 286 } 287 } 288 289 // If the sector is not present 290 if (victim_sector == nullptr){ 291 // Choose replacement victim from replacement candidates 292 victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim( | 248 SectorBlk* sector_blk = static_cast<SectorBlk*>(sector); 249 if ((tag == sector_blk->getTag()) && sector_blk->isValid() && 250 (is_secure == sector_blk->isSecure())){ 251 victim_sector = sector_blk; 252 break; 253 } 254 } 255 256 // If the sector is not present 257 if (victim_sector == nullptr){ 258 // Choose replacement victim from replacement candidates 259 victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim( |
293 sector_locations)); | 260 sector_entries)); |
294 } 295 | 261 } 262 |
296 // Get the location of the victim block within the sector | 263 // Get the entry of the victim block within the sector |
297 SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)]; 298 299 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and 300 // the currently existing sector is valid. 301 if ((tag == victim_sector->getTag()) && 302 (is_secure == victim_sector->isSecure())){ 303 // It would be a hit if victim was valid, and upgrades do not call 304 // findVictim, so it cannot happen --- 7 unchanged lines hidden (view full) --- 312 313 DPRINTF(CacheRepl, "set %x, way %x, sector offset %x: selecting blk " \ 314 "for replacement\n", victim->getSet(), victim->getWay(), 315 victim->getSectorOffset()); 316 317 return victim; 318} 319 | 264 SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)]; 265 266 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and 267 // the currently existing sector is valid. 268 if ((tag == victim_sector->getTag()) && 269 (is_secure == victim_sector->isSecure())){ 270 // It would be a hit if victim was valid, and upgrades do not call 271 // findVictim, so it cannot happen --- 7 unchanged lines hidden (view full) --- 279 280 DPRINTF(CacheRepl, "set %x, way %x, sector offset %x: selecting blk " \ 281 "for replacement\n", victim->getSet(), victim->getWay(), 282 victim->getSectorOffset()); 283 284 return victim; 285} 286 |
320Addr 321SectorTags::extractTag(Addr addr) const 322{ 323 return addr >> tagShift; 324} 325 | |
326int | 287int |
327SectorTags::extractSet(Addr addr) const 328{ 329 return (addr >> setShift) & setMask; 330} 331 332int | |
333SectorTags::extractSectorOffset(Addr addr) const 334{ 335 return (addr >> sectorShift) & sectorMask; 336} 337 338Addr 339SectorTags::regenerateBlkAddr(const CacheBlk* blk) const 340{ 341 const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk); | 288SectorTags::extractSectorOffset(Addr addr) const 289{ 290 return (addr >> sectorShift) & sectorMask; 291} 292 293Addr 294SectorTags::regenerateBlkAddr(const CacheBlk* blk) const 295{ 296 const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk); |
342 const Addr set = blk_cast->getSectorBlock()->getSet() << setShift; 343 return ((blk_cast->getTag() << tagShift) | set | 344 ((Addr)blk_cast->getSectorOffset() << sectorShift)); | 297 const SectorBlk* sec_blk = blk_cast->getSectorBlock(); 298 const Addr sec_addr = indexingPolicy->regenerateAddr(blk->tag, sec_blk); 299 return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift); |
345} 346 347void 348SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor) 349{ 350 for (SectorSubBlk& blk : blks) { 351 visitor(blk); 352 } --- 8 unchanged lines hidden (view full) --- 361 } 362 } 363 return false; 364} 365 366SectorTags * 367SectorTagsParams::create() 368{ | 300} 301 302void 303SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor) 304{ 305 for (SectorSubBlk& blk : blks) { 306 visitor(blk); 307 } --- 8 unchanged lines hidden (view full) --- 316 } 317 } 318 return false; 319} 320 321SectorTags * 322SectorTagsParams::create() 323{ |
324 // There must be a indexing policy 325 fatal_if(!indexing_policy, "An indexing policy is required"); 326 |
|
369 return new SectorTags(this); 370} | 327 return new SectorTags(this); 328} |