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