1/*
2 * Copyright (c) 2014-2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Mitch Hayenga
38 */
39
40#ifndef __MEM_CACHE_PREFETCH_QUEUED_HH__
41#define __MEM_CACHE_PREFETCH_QUEUED_HH__
42
43#include <cstdint>
44#include <list>
45#include <utility>
46
47#include "base/statistics.hh"
48#include "base/types.hh"
49#include "mem/cache/prefetch/base.hh"
50#include "mem/packet.hh"
51
52struct QueuedPrefetcherParams;
53
54class QueuedPrefetcher : public BasePrefetcher
55{
56  protected:
57    struct DeferredPacket : public BaseTLB::Translation {
58        /** Owner of the packet */
59        QueuedPrefetcher *owner;
60        /** Prefetch info corresponding to this packet */
61        PrefetchInfo pfInfo;
62        /** Time when this prefetch becomes ready */
63        Tick tick;
64        /** The memory packet generated by this prefetch */
65        PacketPtr pkt;
66        /** The priority of this prefetch */
67        int32_t priority;
68        /** Request used when a translation is needed */
69        RequestPtr translationRequest;
70        ThreadContext *tc;
71        bool ongoingTranslation;
72
73        /**
74         * Constructor
75         * @param o QueuedPrefetcher in charge of this request
76         * @param pfi PrefechInfo object associated to this packet
77         * @param t Time when this prefetch becomes ready
78         * @param p PacketPtr with the memory request of the prefetch
79         * @param prio This prefetch priority
80         */
81        DeferredPacket(QueuedPrefetcher *o, PrefetchInfo const &pfi, Tick t,
82            int32_t prio) : owner(o), pfInfo(pfi), tick(t), pkt(nullptr),
83            priority(prio), translationRequest() {
84        }
85
86        bool operator>(const DeferredPacket& that) const
87        {
88            return priority > that.priority;
89        }
90        bool operator<(const DeferredPacket& that) const
91        {
92            return priority < that.priority;
93        }
94        bool operator<=(const DeferredPacket& that) const
95        {
96            return !(*this > that);
97        }
98
99        /**
100         * Create the associated memory packet
101         * @param paddr physical address of this packet
102         * @param blk_size block size used by the prefetcher
103         * @param mid Requester ID of the access that generated this prefetch
104         * @param tag_prefetch flag to indicate if the packet needs to be
105         *        tagged
106         * @param t time when the prefetch becomes ready
107         */
108        void createPkt(Addr paddr, unsigned blk_size, MasterID mid,
109                       bool tag_prefetch, Tick t);
110
111        /**
112         * Sets the translation request needed to obtain the physical address
113         * of this request.
114         * @param req The Request with the virtual address of this request
115         */
116        void setTranslationRequest(const RequestPtr &req)
117        {
118            translationRequest = req;
119        }
120
121        void markDelayed() override
122        {}
123
124        void finish(const Fault &fault, const RequestPtr &req,
125                            ThreadContext *tc, BaseTLB::Mode mode) override;
126
127        /**
128         * Issues the translation request to the provided TLB
129         * @param tlb the tlb that has to translate the address
130         */
131        void startTranslation(BaseTLB *tlb);
132    };
133
134    std::list<DeferredPacket> pfq;
135    std::list<DeferredPacket> pfqMissingTranslation;
136
137    using const_iterator = std::list<DeferredPacket>::const_iterator;
138    using iterator = std::list<DeferredPacket>::iterator;
139
140    // PARAMETERS
141
142    /** Maximum size of the prefetch queue */
143    const unsigned queueSize;
144
145    /**
146     * Maximum size of the queue holding prefetch requests with missing
147     * address translations
148     */
149    const unsigned missingTranslationQueueSize;
150
151    /** Cycles after generation when a prefetch can first be issued */
152    const Cycles latency;
153
154    /** Squash queued prefetch if demand access observed */
155    const bool queueSquash;
156
157    /** Filter prefetches if already queued */
158    const bool queueFilter;
159
160    /** Snoop the cache before generating prefetch (cheating basically) */
161    const bool cacheSnoop;
162
163    /** Tag prefetch with PC of generating access? */
164    const bool tagPrefetch;
165
166    /** Percentage of requests that can be throttled */
167    const unsigned int throttleControlPct;
168
169    // STATS
170    Stats::Scalar pfIdentified;
171    Stats::Scalar pfBufferHit;
172    Stats::Scalar pfInCache;
173    Stats::Scalar pfRemovedFull;
174    Stats::Scalar pfSpanPage;
175
176  public:
177    using AddrPriority = std::pair<Addr, int32_t>;
178
179    QueuedPrefetcher(const QueuedPrefetcherParams *p);
180    virtual ~QueuedPrefetcher();
181
182    void notify(const PacketPtr &pkt, const PrefetchInfo &pfi) override;
183
184    void insert(const PacketPtr &pkt, PrefetchInfo &new_pfi, int32_t priority);
185
186    virtual void calculatePrefetch(const PrefetchInfo &pfi,
187                                   std::vector<AddrPriority> &addresses) = 0;
188    PacketPtr getPacket() override;
189
190    Tick nextPrefetchReadyTime() const override
191    {
192        return pfq.empty() ? MaxTick : pfq.front().tick;
193    }
194
195    void regStats() override;
196
197  private:
198
199    /**
200     * Adds a DeferredPacket to the specified queue
201     * @param queue selected queue to use
202     * @param dpp DeferredPacket to add
203     */
204    void addToQueue(std::list<DeferredPacket> &queue, DeferredPacket &dpp);
205
206    /**
207     * Starts the translations of the queued prefetches with a
208     * missing translation. It performs a maximum specified number of
209     * translations. Successful translations cause the prefetch request to be
210     * queued in the queue of ready requests.
211     * @param max maximum number of translations to perform
212     */
213    void processMissingTranslations(unsigned max);
214
215    /**
216     * Indicates that the translation of the address of the provided  deferred
217     * packet has been successfully completed, and it can be enqueued as a
218     * new prefetch request.
219     * @param dp the deferred packet that has completed the translation request
220     * @param failed whether the translation was successful
221     */
222    void translationComplete(DeferredPacket *dp, bool failed);
223
224    /**
225     * Checks whether the specified prefetch request is already in the
226     * specified queue. If the request is found, its priority is updated.
227     * @param queue selected queue to check
228     * @param pfi information of the prefetch request to be added
229     * @param priority priority of the prefetch request to be added
230     * @return True if the prefetch request was found in the queue
231     */
232    bool alreadyInQueue(std::list<DeferredPacket> &queue,
233                        const PrefetchInfo &pfi, int32_t priority);
234
235    /**
236     * Returns the maxmimum number of prefetch requests that are allowed
237     * to be created from the number of prefetch candidates provided.
238     * The behavior of this service is controlled with the throttleControlPct
239     * parameter.
240     * @param total number of prefetch candidates generated by the prefetcher
241     * @return the number of these request candidates are allowed to be created
242     */
243    size_t getMaxPermittedPrefetches(size_t total) const;
244
245    RequestPtr createPrefetchRequest(Addr addr, PrefetchInfo const &pfi,
246                                        PacketPtr pkt);
247};
248
249#endif //__MEM_CACHE_PREFETCH_QUEUED_HH__
250
251