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/**
32 * Implementation of the Irregular Stream Buffer prefetcher
33 * Reference:
34 *   Jain, A., & Lin, C. (2013, December). Linearizing irregular memory
35 *   accesses for improved correlated prefetching. In Proceedings of the
36 *   46th Annual IEEE/ACM International Symposium on Microarchitecture
37 *   (pp. 247-259). ACM.
38 */
39
40#ifndef __MEM_CACHE_PREFETCH_IRREGULAR_STREAM_BUFFER_HH__
41#define __MEM_CACHE_PREFETCH_IRREGULAR_STREAM_BUFFER_HH__
42
43#include "base/callback.hh"
44#include "base/sat_counter.hh"
45#include "mem/cache/prefetch/associative_set.hh"
46#include "mem/cache/prefetch/queued.hh"
47
48struct IrregularStreamBufferPrefetcherParams;
49
50class IrregularStreamBufferPrefetcher : public QueuedPrefetcher
51{
52    /** Size in bytes of a temporal stream */
53    const size_t chunkSize;
54    /** Number of prefetch candidates per Physical-to-Structural entry */
55    const unsigned prefetchCandidatesPerEntry;
56    /** Number of maximum prefetches requests created when predicting */
57    const unsigned degree;
58
59    /**
60     * Training Unit Entry datatype, it holds the last accessed address and
61     * its secure flag
62     */
63    struct TrainingUnitEntry : public TaggedEntry {
64        Addr lastAddress;
65        bool lastAddressSecure;
66    };
67    /** Map of PCs to Training unit entries */
68    AssociativeSet<TrainingUnitEntry> trainingUnit;
69
70    /** Address Mapping entry, holds an address and a confidence counter */
71    struct AddressMapping {
72        Addr address;
73        SatCounter counter;
74        AddressMapping(unsigned bits) : address(0), counter(bits)
75        {}
76    };
77
78    /**
79     * Maps a set of contiguous addresses to another set of (not necessarily
80     * contiguos) addresses, with their corresponding confidence counters
81     */
82    struct AddressMappingEntry : public TaggedEntry {
83        std::vector<AddressMapping> mappings;
84        AddressMappingEntry(size_t num_mappings, unsigned counter_bits)
85            : mappings(num_mappings, counter_bits)
86        {}
87        void reset() override
88        {
89            for (auto &entry : mappings) {
90                entry.address = 0;
91                entry.counter.reset();
92            }
93        }
94    };
95
96    /** Physical-to-Structured mappings table */
97    AssociativeSet<AddressMappingEntry> psAddressMappingCache;
98    /** Structured-to-Physical mappings table */
99    AssociativeSet<AddressMappingEntry> spAddressMappingCache;
100    /**
101     * Counter of allocated structural addresses, increased by "chunkSize",
102     * each time a new structured address is allocated
103     */
104    uint64_t structuralAddressCounter;
105
106    /**
107     * Add a mapping to the Structured-to-Physica mapping table
108     * @param structuralAddress structural address
109     * @param is_secure whether this page is inside the secure memory area
110     * @param physical_address corresponding physical address
111     */
112    void addStructuralToPhysicalEntry(Addr structuralAddress, bool is_secure,
113                                      Addr physical_address);
114
115    /**
116     * Obtain the Physical-to-Structured mapping entry of the given physical
117     * address. If the entry does not exist a new one is allocated, replacing
118     * an existing one if needed.
119     * @param paddr physical address
120     * @param is_secure whether this page is inside the secure memory area
121     * @result reference to the entry
122     */
123    AddressMapping& getPSMapping(Addr paddr, bool is_secure);
124  public:
125    IrregularStreamBufferPrefetcher(
126        const IrregularStreamBufferPrefetcherParams *p);
127    ~IrregularStreamBufferPrefetcher() {}
128    void calculatePrefetch(const PrefetchInfo &pfi,
129                           std::vector<AddrPriority> &addresses) override;
130};
131#endif//__MEM_CACHE_PREFETCH_IRREGULAR_STREAM_BUFFER_HH__
132