sector_tags.hh revision 12752:6a0e3eb1cc5d
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;
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 */
57class 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 pkt Packet holding the address to update
153     * @param blk The block to update.
154     */
155    void insertBlock(PacketPtr pkt, CacheBlk *blk) override;
156
157    /**
158     * Finds the given address in the cache, do not update replacement data.
159     * i.e. This is a no-side-effect find of a block.
160     *
161     * @param addr The address to find.
162     * @param is_secure True if the target memory space is secure.
163     * @return Pointer to the cache block if found.
164     */
165    CacheBlk* findBlock(Addr addr, bool is_secure) const override;
166
167    /**
168     * Find a sector block given set and way.
169     *
170     * @param set The set of the block.
171     * @param way The way of the block.
172     * @return The block.
173     */
174    ReplaceableEntry* findBlockBySetAndWay(int set, int way) const override;
175
176    /**
177     * Find replacement victim based on address.
178     *
179     * @param addr Address to find a victim for.
180     * @param is_secure True if the target memory space is secure.
181     * @param evict_blks Cache blocks to be evicted.
182     * @return Cache block to be replaced.
183     */
184    CacheBlk* findVictim(Addr addr, const bool is_secure,
185                         std::vector<CacheBlk*>& evict_blks) const override;
186
187    /**
188     * Generate the sector tag from the given address.
189     *
190     * @param addr The address to get the sector tag from.
191     * @return The sector tag of the address.
192     */
193    Addr extractTag(Addr addr) const override;
194
195    /**
196     * Calculate the set index from the address.
197     *
198     * @param addr The address to get the set from.
199     * @return The set index of the address.
200     */
201    int extractSet(Addr addr) const;
202
203    /**
204     * Calculate a block's offset in a sector from the address.
205     *
206     * @param addr The address to get the offset from.
207     * @return Offset of the corresponding block within its sector.
208     */
209    int extractSectorOffset(Addr addr) const;
210
211    /**
212     * Regenerate the block address from the tag and set.
213     *
214     * @param block The block.
215     * @return the block address.
216     */
217    Addr regenerateBlkAddr(const CacheBlk* blk) const override;
218
219    /**
220     * Visit each sub-block in the tags and apply a visitor.
221     *
222     * The visitor should be a std::function that takes a cache block.
223     * reference as its parameter.
224     *
225     * @param visitor Visitor to call on each block.
226     */
227    void forEachBlk(std::function<void(CacheBlk &)> visitor) override;
228
229    /**
230     * Find if any of the sub-blocks satisfies a condition.
231     *
232     * The visitor should be a std::function that takes a cache block
233     * reference as its parameter. The visitor will terminate the
234     * traversal early if the condition is satisfied.
235     *
236     * @param visitor Visitor to call on each block.
237     */
238    bool anyBlk(std::function<bool(CacheBlk &)> visitor) override;
239};
240
241#endif //__MEM_CACHE_TAGS_SECTOR_TAGS_HH__
242