1/*
2 * Copyright (c) 2013-2016,2019 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: Stephan Diestelhorst
38 */
39
40/**
41 * @file
42 * Definition of a snoop filter.
43 */
44
45#ifndef __MEM_SNOOP_FILTER_HH__
46#define __MEM_SNOOP_FILTER_HH__
47
48#include <bitset>
49#include <unordered_map>
50#include <utility>
51
52#include "mem/packet.hh"
53#include "mem/port.hh"
54#include "mem/qport.hh"
55#include "params/SnoopFilter.hh"
56#include "sim/sim_object.hh"
57#include "sim/system.hh"
58
59/**
60 * This snoop filter keeps track of which connected port has a
61 * particular line of data. It can be queried (through lookup*) on
62 * memory requests from above (reads / writes / ...); and also from
63 * below (snoops). The snoop filter precisely knows about the location
64 * of lines "above" it through a map from cache line address to
65 * sharers/ports. The snoop filter ties into the flows of requests
66 * (when they succeed at the lower interface), regular responses from
67 * below and also responses from sideway's caches (in update*). This
68 * allows the snoop filter to model cache-line residency by snooping
69 * the messages.
70 *
71 * The tracking happens in two fields to be able to distinguish
72 * between in-flight requests (in requested) and already pulled in
73 * lines (in holder). This distinction is used for producing tighter
74 * assertions and tracking request completion. For safety, (requested
75 * | holder) should be notified and the requesting MSHRs will take
76 * care of ordering.
77 *
78 * Overall, some trickery is required because:
79 * (1) snoops are not followed by an ACK, but only evoke a response if
80 *     they need to (hit dirty)
81 * (2) side-channel information is funnelled through direct modifications of
82 *     pkt, instead of proper messages through the bus
83 * (3) there are no clean evict messages telling the snoop filter that a local,
84 *     upper cache dropped a line, making the snoop filter pessimistic for now
85 * (4) ordering: there is no single point of order in the system.  Instead,
86 *     requesting MSHRs track order between local requests and remote snoops
87 */
88class SnoopFilter : public SimObject {
89  public:
90
91    // Change for systems with more than 256 ports tracked by this object
92    static const int SNOOP_MASK_SIZE = 256;
93
94    typedef std::vector<QueuedSlavePort*> SnoopList;
95
96    SnoopFilter (const SnoopFilterParams *p) :
97        SimObject(p), reqLookupResult(cachedLocations.end()),
98        linesize(p->system->cacheLineSize()), lookupLatency(p->lookup_latency),
99        maxEntryCount(p->max_capacity / p->system->cacheLineSize())
100    {
101    }
102
103    /**
104     * Init a new snoop filter and tell it about all the slave ports
105     * of the enclosing bus.
106     *
107     * @param slave_ports Slave ports that the bus is attached to.
108     */
109    void setSlavePorts(const SnoopList& slave_ports) {
110        localSlavePortIds.resize(slave_ports.size(), InvalidPortID);
111
112        PortID id = 0;
113        for (const auto& p : slave_ports) {
114            // no need to track this port if it is not snooping
115            if (p->isSnooping()) {
116                slavePorts.push_back(p);
117                localSlavePortIds[p->getId()] = id++;
118            }
119        }
120
121        // make sure we can deal with this many ports
122        fatal_if(id > SNOOP_MASK_SIZE,
123                 "Snoop filter only supports %d snooping ports, got %d\n",
124                 SNOOP_MASK_SIZE, id);
125    }
126
127    /**
128     * Lookup a request (from a slave port) in the snoop filter and
129     * return a list of other slave ports that need forwarding of the
130     * resulting snoops.  Additionally, update the tracking structures
131     * with new request information. Note that the caller must also
132     * call finishRequest once it is known if the request needs to
133     * retry or not.
134     *
135     * @param cpkt          Pointer to the request packet. Not changed.
136     * @param slave_port    Slave port where the request came from.
137     * @return Pair of a vector of snoop target ports and lookup latency.
138     */
139    std::pair<SnoopList, Cycles> lookupRequest(const Packet* cpkt,
140                                               const SlavePort& slave_port);
141
142    /**
143     * For an un-successful request, revert the change to the snoop
144     * filter. Also take care of erasing any null entries. This method
145     * relies on the result from lookupRequest being stored in
146     * reqLookupResult.
147     *
148     * @param will_retry    This request will retry on this bus / snoop filter
149     * @param addr          Packet address, merely for sanity checking
150     */
151    void finishRequest(bool will_retry, Addr addr, bool is_secure);
152
153    /**
154     * Handle an incoming snoop from below (the master port). These
155     * can upgrade the tracking logic and may also benefit from
156     * additional steering thanks to the snoop filter.
157     *
158     * @param cpkt Pointer to const Packet containing the snoop.
159     * @return Pair with a vector of SlavePorts that need snooping and a lookup
160     *         latency.
161     */
162    std::pair<SnoopList, Cycles> lookupSnoop(const Packet* cpkt);
163
164    /**
165     * Let the snoop filter see any snoop responses that turn into
166     * request responses and indicate cache to cache transfers. These
167     * will update the corresponding state in the filter.
168     *
169     * @param cpkt     Pointer to const Packet holding the snoop response.
170     * @param rsp_port SlavePort that sends the response.
171     * @param req_port SlavePort that made the original request and is the
172     *                 destination of the snoop response.
173     */
174    void updateSnoopResponse(const Packet *cpkt, const SlavePort& rsp_port,
175                             const SlavePort& req_port);
176
177    /**
178     * Pass snoop responses that travel downward through the snoop
179     * filter and let them update the snoop filter state.  No
180     * additional routing happens.
181     *
182     * @param cpkt     Pointer to const Packet holding the snoop response.
183     * @param rsp_port SlavePort that sends the response.
184     * @param req_port MasterPort through which the response is forwarded.
185     */
186    void updateSnoopForward(const Packet *cpkt, const SlavePort& rsp_port,
187                            const MasterPort& req_port);
188
189    /**
190     * Update the snoop filter with a response from below (outer /
191     * other cache, or memory) and update the tracking information in
192     * the snoop filter.
193     *
194     * @param cpkt       Pointer to const Packet holding the snoop response.
195     * @param slave_port SlavePort that made the original request and
196     *                   is the target of this response.
197     */
198    void updateResponse(const Packet *cpkt, const SlavePort& slave_port);
199
200    virtual void regStats();
201
202  protected:
203
204    /**
205     * The underlying type for the bitmask we use for tracking. This
206     * limits the number of snooping ports supported per crossbar.
207     */
208    typedef std::bitset<SNOOP_MASK_SIZE> SnoopMask;
209
210    /**
211    * Per cache line item tracking a bitmask of SlavePorts who have an
212    * outstanding request to this line (requested) or already share a
213    * cache line with this address (holder).
214    */
215    struct SnoopItem {
216        SnoopMask requested;
217        SnoopMask holder;
218    };
219    /**
220     * HashMap of SnoopItems indexed by line address
221     */
222    typedef std::unordered_map<Addr, SnoopItem> SnoopFilterCache;
223
224    /**
225     * Simple factory methods for standard return values.
226     */
227    std::pair<SnoopList, Cycles> snoopAll(Cycles latency) const
228    {
229        return std::make_pair(slavePorts, latency);
230    }
231    std::pair<SnoopList, Cycles> snoopSelected(const SnoopList& slave_ports,
232                                               Cycles latency) const
233    {
234        return std::make_pair(slave_ports, latency);
235    }
236    std::pair<SnoopList, Cycles> snoopDown(Cycles latency) const
237    {
238        SnoopList empty;
239        return std::make_pair(empty , latency);
240    }
241
242    /**
243     * Convert a single port to a corresponding, one-hot bitmask
244     * @param port SlavePort that should be converted.
245     * @return One-hot bitmask corresponding to the port.
246     */
247    SnoopMask portToMask(const SlavePort& port) const;
248    /**
249     * Converts a bitmask of ports into the corresponing list of ports
250     * @param ports SnoopMask of the requested ports
251     * @return SnoopList containing all the requested SlavePorts
252     */
253    SnoopList maskToPortList(SnoopMask ports) const;
254
255  private:
256
257    /**
258     * Removes snoop filter items which have no requesters and no holders.
259     */
260    void eraseIfNullEntry(SnoopFilterCache::iterator& sf_it);
261
262    /** Simple hash set of cached addresses. */
263    SnoopFilterCache cachedLocations;
264
265    /**
266     * A request lookup must be followed by a call to finishRequest to inform
267     * the operation's success. If a retry is needed, however, all changes
268     * made to the snoop filter while performing the lookup must be undone.
269     * This structure keeps track of the state previous to such changes.
270     */
271    struct ReqLookupResult {
272        /** Iterator used to store the result from lookupRequest. */
273        SnoopFilterCache::iterator it;
274
275        /**
276         * Variable to temporarily store value of snoopfilter entry
277         * in case finishRequest needs to undo changes made in lookupRequest
278         * (because of crossbar retry)
279         */
280        SnoopItem retryItem;
281
282        /**
283         * The constructor must be informed of the internal cache's end
284         * iterator, so do not allow the compiler to implictly define it.
285         *
286         * @param end_it Iterator to the end of the internal cache.
287         */
288        ReqLookupResult(SnoopFilterCache::iterator end_it)
289            : it(end_it), retryItem{0, 0}
290        {
291        }
292        ReqLookupResult() = delete;
293    } reqLookupResult;
294
295    /** List of all attached snooping slave ports. */
296    SnoopList slavePorts;
297    /** Track the mapping from port ids to the local mask ids. */
298    std::vector<PortID> localSlavePortIds;
299    /** Cache line size. */
300    const unsigned linesize;
301    /** Latency for doing a lookup in the filter */
302    const Cycles lookupLatency;
303    /** Max capacity in terms of cache blocks tracked, for sanity checking */
304    const unsigned maxEntryCount;
305
306    /**
307     * Use the lower bits of the address to keep track of the line status
308     */
309    enum LineStatus {
310        /** block holds data from the secure memory space */
311        LineSecure = 0x01,
312    };
313
314    /** Statistics */
315    Stats::Scalar totRequests;
316    Stats::Scalar hitSingleRequests;
317    Stats::Scalar hitMultiRequests;
318
319    Stats::Scalar totSnoops;
320    Stats::Scalar hitSingleSnoops;
321    Stats::Scalar hitMultiSnoops;
322};
323
324inline SnoopFilter::SnoopMask
325SnoopFilter::portToMask(const SlavePort& port) const
326{
327    assert(port.getId() != InvalidPortID);
328    // if this is not a snooping port, return a zero mask
329    return !port.isSnooping() ? 0 :
330        ((SnoopMask)1) << localSlavePortIds[port.getId()];
331}
332
333inline SnoopFilter::SnoopList
334SnoopFilter::maskToPortList(SnoopMask port_mask) const
335{
336    SnoopList res;
337    for (const auto& p : slavePorts)
338        if ((port_mask & portToMask(*p)).any())
339            res.push_back(p);
340    return res;
341}
342
343#endif // __MEM_SNOOP_FILTER_HH__
344