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 * 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 "mem/cache/base.hh" 46#include "mem/cache/replacement_policies/base.hh" 47#include "mem/cache/replacement_policies/replaceable_entry.hh" 48#include "mem/cache/tags/indexing_policies/base.hh" 49 50SectorTags::SectorTags(const SectorTagsParams *p) 51 : BaseTags(p), allocAssoc(p->assoc), 52 sequentialAccess(p->sequential_access), 53 replacementPolicy(p->replacement_policy), 54 numBlocksPerSector(p->num_blocks_per_sector), 55 numSectors(numBlocks / p->num_blocks_per_sector), blks(numBlocks), 56 secBlks(numSectors), sectorShift(floorLog2(blkSize)), 57 sectorMask(numBlocksPerSector - 1) 58{ 59 // Check parameters 60 fatal_if(blkSize < 4 || !isPowerOf2(blkSize), 61 "Block size must be at least 4 and a power of 2"); 62 fatal_if(!isPowerOf2(numBlocksPerSector), 63 "# of blocks per sector must be non-zero and a power of 2"); 64} 65 66void 67SectorTags::tagsInit() 68{ 69 // Initialize all blocks 70 unsigned blk_index = 0; // index into blks array 71 for (unsigned sec_blk_index = 0; sec_blk_index < numSectors; 72 sec_blk_index++) 73 { 74 // Locate next cache sector 75 SectorBlk* sec_blk = &secBlks[sec_blk_index]; 76 77 // Link block to indexing policy 78 indexingPolicy->setEntry(sec_blk, sec_blk_index); 79 80 // Associate a replacement data entry to the sector 81 sec_blk->replacementData = replacementPolicy->instantiateEntry(); 82 83 // Initialize all blocks in this sector 84 sec_blk->blks.resize(numBlocksPerSector); 85 for (unsigned k = 0; k < numBlocksPerSector; ++k){ 86 // Select block within the set to be linked 87 SectorSubBlk*& blk = sec_blk->blks[k]; 88 89 // Locate next cache block 90 blk = &blks[blk_index]; 91 92 // Associate a data chunk to the block 93 blk->data = &dataBlks[blkSize*blk_index]; 94 95 // Associate sector block to this block 96 blk->setSectorBlock(sec_blk); 97 98 // Associate the sector replacement data to this block 99 blk->replacementData = sec_blk->replacementData; 100 101 // Set its index and sector offset 102 blk->setSectorOffset(k); 103 104 // Update block index 105 ++blk_index; 106 } 107 } 108} 109 110void 111SectorTags::invalidate(CacheBlk *blk) 112{ 113 BaseTags::invalidate(blk); 114 115 // Get block's sector 116 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk); 117 const SectorBlk* sector_blk = sub_blk->getSectorBlock(); 118 119 // When a block in a sector is invalidated, it does not make the tag 120 // invalid automatically, as there might be other blocks in the sector 121 // using it. The tag is invalidated only when there is a single block 122 // in the sector. 123 if (!sector_blk->isValid()) { 124 // Decrease the number of tags in use 125 tagsInUse--; 126 127 // Invalidate replacement data, as we're invalidating the sector 128 replacementPolicy->invalidate(sector_blk->replacementData); 129 } 130} 131 132CacheBlk* 133SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat) 134{ 135 CacheBlk *blk = findBlock(addr, is_secure); 136 137 // Access all tags in parallel, hence one in each way. The data side 138 // either accesses all blocks in parallel, or one block sequentially on 139 // a hit. Sequential access with a miss doesn't access data. 140 tagAccesses += allocAssoc; 141 if (sequentialAccess) { 142 if (blk != nullptr) { 143 dataAccesses += 1; 144 } 145 } else { 146 dataAccesses += allocAssoc*numBlocksPerSector; 147 } 148 149 // If a cache hit 150 if (blk != nullptr) { 151 // Update number of references to accessed block 152 blk->refCount++; 153 154 // Get block's sector 155 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk); 156 const SectorBlk* sector_blk = sub_blk->getSectorBlock(); 157 158 // Update replacement data of accessed block, which is shared with 159 // the whole sector it belongs to 160 replacementPolicy->touch(sector_blk->replacementData); 161 } 162 163 // The tag lookup latency is the same for a hit or a miss 164 lat = lookupLatency; 165 166 return blk; 167} 168 169void
|
191} 192 193CacheBlk* 194SectorTags::findBlock(Addr addr, bool is_secure) const 195{ 196 // Extract sector tag 197 const Addr tag = extractTag(addr); 198 199 // The address can only be mapped to a specific location of a sector 200 // due to sectors being composed of contiguous-address entries 201 const Addr offset = extractSectorOffset(addr); 202 203 // Find all possible sector entries that may contain the given address 204 const std::vector<ReplaceableEntry*> entries = 205 indexingPolicy->getPossibleEntries(addr); 206 207 // Search for block 208 for (const auto& sector : entries) { 209 auto blk = static_cast<SectorBlk*>(sector)->blks[offset]; 210 if (blk->getTag() == tag && blk->isValid() && 211 blk->isSecure() == is_secure) { 212 return blk; 213 } 214 } 215 216 // Did not find block 217 return nullptr; 218} 219 220CacheBlk* 221SectorTags::findVictim(Addr addr, const bool is_secure, 222 std::vector<CacheBlk*>& evict_blks) const 223{ 224 // Get possible entries to be victimized 225 const std::vector<ReplaceableEntry*> sector_entries = 226 indexingPolicy->getPossibleEntries(addr); 227 228 // Check if the sector this address belongs to has been allocated 229 Addr tag = extractTag(addr); 230 SectorBlk* victim_sector = nullptr; 231 for (const auto& sector : sector_entries) { 232 SectorBlk* sector_blk = static_cast<SectorBlk*>(sector); 233 if ((tag == sector_blk->getTag()) && sector_blk->isValid() && 234 (is_secure == sector_blk->isSecure())){ 235 victim_sector = sector_blk; 236 break; 237 } 238 } 239 240 // If the sector is not present 241 if (victim_sector == nullptr){ 242 // Choose replacement victim from replacement candidates 243 victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim( 244 sector_entries)); 245 } 246 247 // Get the entry of the victim block within the sector 248 SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)]; 249 250 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and 251 // the currently existing sector is valid. 252 if ((tag == victim_sector->getTag()) && 253 (is_secure == victim_sector->isSecure())){ 254 // It would be a hit if victim was valid, and upgrades do not call 255 // findVictim, so it cannot happen 256 assert(!victim->isValid()); 257 } else { 258 // The whole sector must be evicted to make room for the new sector 259 for (const auto& blk : victim_sector->blks){ 260 evict_blks.push_back(blk); 261 } 262 } 263 264 return victim; 265} 266 267int 268SectorTags::extractSectorOffset(Addr addr) const 269{ 270 return (addr >> sectorShift) & sectorMask; 271} 272 273Addr 274SectorTags::regenerateBlkAddr(const CacheBlk* blk) const 275{ 276 const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk); 277 const SectorBlk* sec_blk = blk_cast->getSectorBlock(); 278 const Addr sec_addr = indexingPolicy->regenerateAddr(blk->tag, sec_blk); 279 return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift); 280} 281 282void 283SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor) 284{ 285 for (SectorSubBlk& blk : blks) { 286 visitor(blk); 287 } 288} 289 290bool 291SectorTags::anyBlk(std::function<bool(CacheBlk &)> visitor) 292{ 293 for (SectorSubBlk& blk : blks) { 294 if (visitor(blk)) { 295 return true; 296 } 297 } 298 return false; 299} 300 301SectorTags * 302SectorTagsParams::create() 303{ 304 // There must be a indexing policy 305 fatal_if(!indexing_policy, "An indexing policy is required"); 306 307 return new SectorTags(this); 308}
|