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 compressed set associative tag store using superblocks.
34 */
35
36#ifndef __MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__
37#define __MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__
38
39#include <vector>
40
41#include "mem/cache/tags/sector_tags.hh"
42#include "mem/cache/tags/super_blk.hh"
43
44class BaseCache;
45class CacheBlk;
46struct CompressedTagsParams;
47
48/**
49 * A CompressedTags cache tag store.
50 * @sa  \ref gem5MemorySystem "gem5 Memory System"
51 *
52 * The Compression Ratio (CR) of a superblock is defined by
53 *     CR = uncompressed_size / compressed_size.
54 *
55 * The CompressedTags placement policy divides the cache into s sets of w
56 * superblocks (ways). Each superblock can then contain up to CR compressed
57 * blocks.
58 *
59 * For each tag entry there can be multiple data blocks. We have the same
60 * number of tags a conventional cache would have, but we instantiate the
61 * maximum number of data blocks (according to the compression ratio) per
62 * tag, to virtually implement compression without increasing the complexity
63 * of the simulator.
64 *
65 * This is a simple implementation of cache compression, where superblocks
66 * can only have at most numBlocksPerSector compressed blocks, each compressed
67 * to at least (100/numBlocksPerSector)% of its size.
68 *
69 * numBlocksPerSector holds the maximum number of blocks a superblock with
70 * the best possible compression factor would hold. It is equivalent to CR
71 * from the previous definition.
72 */
73class CompressedTags : public SectorTags
74{
75  private:
76    /** The cache blocks. */
77    std::vector<CompressionBlk> blks;
78    /** The cache superblocks. */
79    std::vector<SuperBlk> superBlks;
80
81  public:
82    /** Convenience typedef. */
83     typedef CompressedTagsParams Params;
84
85    /**
86     * Construct and initialize this tag store.
87     */
88    CompressedTags(const Params *p);
89
90    /**
91     * Destructor.
92     */
93    virtual ~CompressedTags() {};
94
95    /**
96     * Initialize blocks as SuperBlk and CompressionBlk instances.
97     */
98    void tagsInit() override;
99
100    /**
101     * Find replacement victim based on address. Checks if data can be co-
102     * allocated before choosing blocks to be evicted.
103     *
104     * @param addr Address to find a victim for.
105     * @param is_secure True if the target memory space is secure.
106     * @param compressed_size Size, in bits, of new block to allocate.
107     * @param evict_blks Cache blocks to be evicted.
108     * @return Cache block to be replaced.
109     */
110    CacheBlk* findVictim(Addr addr, const bool is_secure,
111                         const std::size_t compressed_size,
112                         std::vector<CacheBlk*>& evict_blks) const override;
113
114    /**
115     * Insert the new block into the cache and update replacement data.
116     *
117     * @param pkt Packet holding the address to update
118     * @param blk The block to update.
119     */
120    void insertBlock(const PacketPtr pkt, CacheBlk *blk) override;
121
122    /**
123     * Visit each sub-block in the tags and apply a visitor.
124     *
125     * The visitor should be a std::function that takes a cache block.
126     * reference as its parameter.
127     *
128     * @param visitor Visitor to call on each block.
129     */
130    void forEachBlk(std::function<void(CacheBlk &)> visitor) override;
131
132    /**
133     * Find if any of the sub-blocks satisfies a condition.
134     *
135     * The visitor should be a std::function that takes a cache block
136     * reference as its parameter. The visitor will terminate the
137     * traversal early if the condition is satisfied.
138     *
139     * @param visitor Visitor to call on each block.
140     */
141    bool anyBlk(std::function<bool(CacheBlk &)> visitor) override;
142};
143
144#endif //__MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__
145