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 skewed associative indexing policy.
34 */
35
36#ifndef __MEM_CACHE_INDEXING_POLICIES_SKEWED_ASSOCIATIVE_HH__
37#define __MEM_CACHE_INDEXING_POLICIES_SKEWED_ASSOCIATIVE_HH__
38
39#include <vector>
40
41#include "mem/cache/tags/indexing_policies/base.hh"
42#include "params/SkewedAssociative.hh"
43
44class ReplaceableEntry;
45
46/**
47 * A skewed associative indexing policy.
48 * @sa  \ref gem5MemorySystem "gem5 Memory System"
49 *
50 * The skewed indexing policy has a variable mapping based on a hash function,
51 * so a value x can be mapped to different sets, based on the way being used.
52 *
53 * For example, let's assume address A maps to set 3 on way 0. It will likely
54 * have a different set for every other way. Visually, the possible locations
55 * of A are, for a table with 4 ways and 8 sets (arbitrarily chosen sets; these
56 * locations depend on A and the hashing function used):
57 *    Way 0   1   2   3
58 *  Set   _   _   _   _
59 *    0  |_| |_| |X| |_|
60 *    1  |_| |_| |_| |X|
61 *    2  |_| |_| |_| |_|
62 *    3  |X| |_| |_| |_|
63 *    4  |_| |_| |_| |_|
64 *    5  |_| |X| |_| |_|
65 *    6  |_| |_| |_| |_|
66 *    7  |_| |_| |_| |_|
67 *
68 * If provided with an associativity higher than the number of skewing
69 * functions, the skewing functions of the extra ways might be sub-optimal.
70 */
71class SkewedAssociative : public BaseIndexingPolicy
72{
73  private:
74    /**
75     * The number of skewing functions implemented. Should be updated if more
76     * functions are added. If more than this number of skewing functions are
77     * needed (i.e., assoc > this value), we programatically generate new ones,
78     * which may be sub-optimal.
79     */
80    const int NUM_SKEWING_FUNCTIONS = 8;
81
82    /**
83     * The amount to shift a set index to get its MSB.
84     */
85    const int msbShift;
86
87    /**
88     * The hash function itself. Uses the hash function H, as described in
89     * "Skewed-Associative Caches", from Seznec et al. (section 3.3): It
90     * applies an XOR to the MSB and LSB, shifts all bits one bit to the right,
91     * and set the result of the XOR as the new MSB.
92     *
93     * This function is not bijective if the address has only 1 bit, as the MSB
94     * and LSB will be the same, and therefore the xor will always be 0.
95     *
96     * @param addr The address to be hashed.
97     * @param The hashed address.
98     */
99    Addr hash(const Addr addr) const;
100
101    /**
102     * Inverse of the hash function.
103     * @sa hash().
104     *
105     * @param addr The address to be dehashed.
106     * @param The dehashed address.
107     */
108    Addr dehash(const Addr addr) const;
109
110    /**
111     * Address skewing function selection. It selects and applies one of the
112     * skewing functions functions based on the way provided.
113     *
114     * @param addr Address to be skewed. Should contain the set and tag bits.
115     * @param way The cache way, used to select a hash function.
116     * @return The skewed address.
117     */
118    Addr skew(const Addr addr, const uint32_t way) const;
119
120    /**
121     * Address deskewing function (inverse of the skew function) of the given
122     * way.
123     * @sa skew()
124     *
125     * @param addr Address to be deskewed. Should contain the set and tag bits.
126     * @param way The cache way, used to select a hash function.
127     * @return The deskewed address.
128     */
129    Addr deskew(const Addr addr, const uint32_t way) const;
130
131    /**
132     * Apply a skewing function to calculate address' set given a way.
133     *
134     * @param addr The address to calculate the set for.
135     * @param way The way to get the set from.
136     * @return The set index for given combination of address and way.
137     */
138    uint32_t extractSet(const Addr addr, const uint32_t way) const;
139
140  public:
141    /** Convenience typedef. */
142     typedef SkewedAssociativeParams Params;
143
144    /**
145     * Construct and initialize this policy.
146     */
147    SkewedAssociative(const Params *p);
148
149    /**
150     * Destructor.
151     */
152    ~SkewedAssociative() {};
153
154    /**
155     * Find all possible entries for insertion and replacement of an address.
156     * Should be called immediately before ReplacementPolicy's findVictim()
157     * not to break cache resizing.
158     *
159     * @param addr The addr to a find possible entries for.
160     * @return The possible entries.
161     */
162    std::vector<ReplaceableEntry*> getPossibleEntries(const Addr addr) const
163                                                                   override;
164
165    /**
166     * Regenerate an entry's address from its tag and assigned set and way.
167     * Uses the inverse of the skewing function.
168     *
169     * @param tag The tag bits.
170     * @param entry The entry.
171     * @return the entry's address.
172     */
173    Addr regenerateAddr(const Addr tag, const ReplaceableEntry* entry) const
174                                                                   override;
175};
176
177#endif //__MEM_CACHE_INDEXING_POLICIES_SKEWED_ASSOCIATIVE_HH__
178