1/**
2 * Copyright (c) 2018 Metempsy Technology Consulting
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: Javier Bueno
29 */
30
31#ifndef __CACHE_PREFETCH_ASSOCIATIVE_SET_IMPL_HH__
32#define __CACHE_PREFETCH_ASSOCIATIVE_SET_IMPL_HH__
33
34#include "mem/cache/prefetch/associative_set.hh"
35
36template<class Entry>
37AssociativeSet<Entry>::AssociativeSet(int assoc, int num_entries,
38        BaseIndexingPolicy *idx_policy, BaseReplacementPolicy *rpl_policy,
39        Entry const &init_value)
40  : associativity(assoc), numEntries(num_entries), indexingPolicy(idx_policy),
41    replacementPolicy(rpl_policy), entries(numEntries, init_value)
42{
43    fatal_if(!isPowerOf2(num_entries), "The number of entries of an "
44             "AssociativeSet<> must be a power of 2");
45    fatal_if(!isPowerOf2(assoc), "The associativity of an AssociativeSet<> "
46             "must be a power of 2");
47    for (unsigned int entry_idx = 0; entry_idx < numEntries; entry_idx += 1) {
48        Entry* entry = &entries[entry_idx];
49        indexingPolicy->setEntry(entry, entry_idx);
50        entry->replacementData = replacementPolicy->instantiateEntry();
51    }
52}
53
54template<class Entry>
55Entry*
56AssociativeSet<Entry>::findEntry(Addr addr, bool is_secure) const
57{
58    Addr tag = indexingPolicy->extractTag(addr);
59    const std::vector<ReplaceableEntry*> selected_entries =
60        indexingPolicy->getPossibleEntries(addr);
61
62    for (const auto& location : selected_entries) {
63        Entry* entry = static_cast<Entry *>(location);
64        if ((entry->getTag() == tag) && entry->isValid() &&
65            entry->isSecure() == is_secure) {
66            return entry;
67        }
68    }
69    return nullptr;
70}
71
72template<class Entry>
73void
74AssociativeSet<Entry>::accessEntry(Entry *entry)
75{
76    replacementPolicy->touch(entry->replacementData);
77}
78
79template<class Entry>
80Entry*
81AssociativeSet<Entry>::findVictim(Addr addr)
82{
83    // Get possible entries to be victimized
84    const std::vector<ReplaceableEntry*> selected_entries =
85        indexingPolicy->getPossibleEntries(addr);
86    Entry* victim = static_cast<Entry*>(replacementPolicy->getVictim(
87                            selected_entries));
88    // There is only one eviction for this replacement
89    victim->reset();
90    return victim;
91}
92
93
94template<class Entry>
95std::vector<Entry *>
96AssociativeSet<Entry>::getPossibleEntries(const Addr addr) const
97{
98    std::vector<ReplaceableEntry *> selected_entries =
99        indexingPolicy->getPossibleEntries(addr);
100    std::vector<Entry *> entries(selected_entries.size(), nullptr);
101
102    unsigned int idx = 0;
103    for (auto &entry : selected_entries) {
104        entries[idx++] = static_cast<Entry *>(entry);
105    }
106    return entries;
107}
108
109template<class Entry>
110void
111AssociativeSet<Entry>::insertEntry(Addr addr, bool is_secure, Entry* entry)
112{
113   entry->setValid();
114   entry->setTag(indexingPolicy->extractTag(addr));
115   entry->setSecure(is_secure);
116   replacementPolicy->reset(entry->replacementData);
117}
118
119#endif//__CACHE_PREFETCH_ASSOCIATIVE_SET_IMPL_HH__
120