snoop_filter.cc revision 11132
1/*
2 * Copyright (c) 2013-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: Stephan Diestelhorst <stephan.diestelhorst@arm.com>
38 */
39
40/**
41 * @file
42 * Definition of a snoop filter.
43 */
44
45#include "base/misc.hh"
46#include "base/trace.hh"
47#include "debug/SnoopFilter.hh"
48#include "mem/snoop_filter.hh"
49#include "sim/system.hh"
50
51void
52SnoopFilter::eraseIfNullEntry(SnoopFilterCache::iterator& sf_it)
53{
54    SnoopItem& sf_item = sf_it->second;
55    if (!(sf_item.requested | sf_item.holder)) {
56        cachedLocations.erase(sf_it);
57        DPRINTF(SnoopFilter, "%s:   Removed SF entry.\n",
58                __func__);
59    }
60}
61
62std::pair<SnoopFilter::SnoopList, Cycles>
63SnoopFilter::lookupRequest(const Packet* cpkt, const SlavePort& slave_port)
64{
65    DPRINTF(SnoopFilter, "%s: packet src %s addr 0x%x cmd %s\n",
66            __func__, slave_port.name(), cpkt->getAddr(), cpkt->cmdString());
67
68    // Ultimately we should check if the packet came from an
69    // allocating source, not just if the port is snooping
70    bool allocate = !cpkt->req->isUncacheable() && slave_port.isSnooping();
71    Addr line_addr = cpkt->getBlockAddr(linesize);
72    SnoopMask req_port = portToMask(slave_port);
73    reqLookupResult = cachedLocations.find(line_addr);
74    bool is_hit = (reqLookupResult != cachedLocations.end());
75
76    // If the snoop filter has no entry, and we should not allocate,
77    // do not create a new snoop filter entry, simply return a NULL
78    // portlist.
79    if (!is_hit && !allocate)
80        return snoopDown(lookupLatency);
81
82    // If no hit in snoop filter create a new element and update iterator
83    if (!is_hit)
84        reqLookupResult = cachedLocations.emplace(line_addr, SnoopItem()).first;
85    SnoopItem& sf_item = reqLookupResult->second;
86    SnoopMask interested = sf_item.holder | sf_item.requested;
87
88    // Store unmodified value of snoop filter item in temp storage in
89    // case we need to revert because of a send retry in
90    // updateRequest.
91    retryItem = sf_item;
92
93    totRequests++;
94    if (is_hit) {
95        // Single bit set -> value is a power of two
96        if (isPow2(interested))
97            hitSingleRequests++;
98        else
99            hitMultiRequests++;
100    }
101
102    DPRINTF(SnoopFilter, "%s:   SF value %x.%x\n",
103            __func__, sf_item.requested, sf_item.holder);
104
105    // If we are not allocating, we are done
106    if (!allocate)
107        return snoopSelected(maskToPortList(interested & ~req_port),
108                             lookupLatency);
109
110    if (cpkt->needsResponse()) {
111        if (!cpkt->memInhibitAsserted()) {
112            // Max one request per address per port
113            panic_if(sf_item.requested & req_port, "double request :( " \
114                     "SF value %x.%x\n", sf_item.requested, sf_item.holder);
115
116            // Mark in-flight requests to distinguish later on
117            sf_item.requested |= req_port;
118            DPRINTF(SnoopFilter, "%s:   new SF value %x.%x\n",
119                    __func__,  sf_item.requested, sf_item.holder);
120        } else {
121            // NOTE: The memInhibit might have been asserted by a cache closer
122            // to the CPU, already -> the response will not be seen by this
123            // filter -> we do not need to keep the in-flight request, but make
124            // sure that we know that that cluster has a copy
125            panic_if(!(sf_item.holder & req_port), "Need to hold the value!");
126            DPRINTF(SnoopFilter,
127                    "%s: not marking request. SF value %x.%x\n",
128                    __func__,  sf_item.requested, sf_item.holder);
129        }
130    } else { // if (!cpkt->needsResponse())
131        assert(cpkt->evictingBlock());
132        // make sure that the sender actually had the line
133        panic_if(!(sf_item.holder & req_port), "requester %x is not a " \
134                 "holder :( SF value %x.%x\n", req_port,
135                 sf_item.requested, sf_item.holder);
136        // CleanEvicts and Writebacks -> the sender and all caches above
137        // it may not have the line anymore.
138        if (!cpkt->isBlockCached()) {
139            sf_item.holder &= ~req_port;
140            DPRINTF(SnoopFilter, "%s:   new SF value %x.%x\n",
141                    __func__,  sf_item.requested, sf_item.holder);
142        }
143    }
144
145    return snoopSelected(maskToPortList(interested & ~req_port), lookupLatency);
146}
147
148void
149SnoopFilter::finishRequest(bool will_retry, const Packet* cpkt)
150{
151    if (reqLookupResult != cachedLocations.end()) {
152        // since we rely on the caller, do a basic check to ensure
153        // that finishRequest is being called following lookupRequest
154        assert(reqLookupResult->first == cpkt->getBlockAddr(linesize));
155        if (will_retry) {
156            // Undo any changes made in lookupRequest to the snoop filter
157            // entry if the request will come again. retryItem holds
158            // the previous value of the snoopfilter entry.
159            reqLookupResult->second = retryItem;
160
161            DPRINTF(SnoopFilter, "%s:   restored SF value %x.%x\n",
162                    __func__,  retryItem.requested, retryItem.holder);
163        }
164
165        eraseIfNullEntry(reqLookupResult);
166    }
167}
168
169std::pair<SnoopFilter::SnoopList, Cycles>
170SnoopFilter::lookupSnoop(const Packet* cpkt)
171{
172    DPRINTF(SnoopFilter, "%s: packet addr 0x%x cmd %s\n",
173            __func__, cpkt->getAddr(), cpkt->cmdString());
174
175    assert(cpkt->isRequest());
176
177    // Broadcast / filter upward snoops
178    const bool filter_upward = true;  // @todo: Make configurable
179
180    if (!filter_upward)
181        return snoopAll(lookupLatency);
182
183    Addr line_addr = cpkt->getBlockAddr(linesize);
184    auto sf_it = cachedLocations.find(line_addr);
185    bool is_hit = (sf_it != cachedLocations.end());
186
187    panic_if(!is_hit && (cachedLocations.size() >= maxEntryCount),
188             "snoop filter exceeded capacity of %d cache blocks\n",
189             maxEntryCount);
190
191    // If the snoop filter has no entry and its an uncacheable
192    // request, do not create a new snoop filter entry, simply return
193    // a NULL portlist.
194    if (!is_hit && cpkt->req->isUncacheable())
195        return snoopDown(lookupLatency);
196
197    // If no hit in snoop filter create a new element and update iterator
198    if (!is_hit)
199        sf_it = cachedLocations.emplace(line_addr, SnoopItem()).first;
200    SnoopItem& sf_item = sf_it->second;
201
202    DPRINTF(SnoopFilter, "%s:   old SF value %x.%x\n",
203            __func__, sf_item.requested, sf_item.holder);
204
205    SnoopMask interested = (sf_item.holder | sf_item.requested);
206
207    totSnoops++;
208    if (is_hit) {
209        // Single bit set -> value is a power of two
210        if (isPow2(interested))
211            hitSingleSnoops++;
212        else
213            hitMultiSnoops++;
214    }
215    // ReadEx and Writes require both invalidation and exlusivity, while reads
216    // require neither. Writebacks on the other hand require exclusivity but
217    // not the invalidation. Previously Writebacks did not generate upward
218    // snoops so this was never an aissue. Now that Writebacks generate snoops
219    // we need to special case for Writebacks.
220    assert(cpkt->cmd == MemCmd::Writeback || cpkt->req->isUncacheable() ||
221           (cpkt->isInvalidate() == cpkt->needsExclusive()));
222    if (cpkt->isInvalidate() && !sf_item.requested) {
223        // Early clear of the holder, if no other request is currently going on
224        // @todo: This should possibly be updated even though we do not filter
225        // upward snoops
226        sf_item.holder = 0;
227    }
228
229    eraseIfNullEntry(sf_it);
230    DPRINTF(SnoopFilter, "%s:   new SF value %x.%x interest: %x \n",
231            __func__, sf_item.requested, sf_item.holder, interested);
232
233    return snoopSelected(maskToPortList(interested), lookupLatency);
234}
235
236void
237SnoopFilter::updateSnoopResponse(const Packet* cpkt,
238                                 const SlavePort& rsp_port,
239                                 const SlavePort& req_port)
240{
241    DPRINTF(SnoopFilter, "%s: packet rsp %s req %s addr 0x%x cmd %s\n",
242            __func__, rsp_port.name(), req_port.name(), cpkt->getAddr(),
243            cpkt->cmdString());
244
245    assert(cpkt->isResponse());
246    assert(cpkt->memInhibitAsserted());
247
248    // Ultimately we should check if the packet came from an
249    // allocating source, not just if the port is snooping
250    bool allocate = !cpkt->req->isUncacheable() && req_port.isSnooping();
251    if (!allocate)
252        return;
253
254    Addr line_addr = cpkt->getBlockAddr(linesize);
255    SnoopMask rsp_mask = portToMask(rsp_port);
256    SnoopMask req_mask = portToMask(req_port);
257    SnoopItem& sf_item = cachedLocations[line_addr];
258
259    DPRINTF(SnoopFilter, "%s:   old SF value %x.%x\n",
260            __func__,  sf_item.requested, sf_item.holder);
261
262    // The source should have the line
263    panic_if(!(sf_item.holder & rsp_mask), "SF value %x.%x does not have "\
264             "the line\n", sf_item.requested, sf_item.holder);
265
266    // The destination should have had a request in
267    panic_if(!(sf_item.requested & req_mask), "SF value %x.%x missing "\
268             "the original request\n",  sf_item.requested, sf_item.holder);
269
270    // Update the residency of the cache line.
271    if (cpkt->needsExclusive() || !cpkt->sharedAsserted()) {
272        DPRINTF(SnoopFilter, "%s:  dropping %x because needs: %i shared: %i "\
273                "SF val: %x.%x\n", __func__,  rsp_mask,
274                cpkt->needsExclusive(), cpkt->sharedAsserted(),
275                sf_item.requested, sf_item.holder);
276
277        sf_item.holder &= ~rsp_mask;
278        // The snoop filter does not see any ACKs from non-responding sharers
279        // that have been invalidated :(  So below assert would be nice, but..
280        //assert(sf_item.holder == 0);
281        sf_item.holder = 0;
282    }
283    assert(cpkt->cmd != MemCmd::Writeback);
284    sf_item.holder |=  req_mask;
285    sf_item.requested &= ~req_mask;
286    assert(sf_item.requested | sf_item.holder);
287    DPRINTF(SnoopFilter, "%s:   new SF value %x.%x\n",
288            __func__, sf_item.requested, sf_item.holder);
289}
290
291void
292SnoopFilter::updateSnoopForward(const Packet* cpkt,
293        const SlavePort& rsp_port, const MasterPort& req_port)
294{
295    DPRINTF(SnoopFilter, "%s: packet rsp %s req %s addr 0x%x cmd %s\n",
296            __func__, rsp_port.name(), req_port.name(), cpkt->getAddr(),
297            cpkt->cmdString());
298
299    Addr line_addr = cpkt->getBlockAddr(linesize);
300    auto sf_it = cachedLocations.find(line_addr);
301    if (sf_it == cachedLocations.end())
302        sf_it = cachedLocations.emplace(line_addr, SnoopItem()).first;
303    SnoopItem& sf_item = sf_it->second;
304    SnoopMask rsp_mask M5_VAR_USED = portToMask(rsp_port);
305
306    assert(cpkt->isResponse());
307    assert(cpkt->memInhibitAsserted());
308
309    DPRINTF(SnoopFilter, "%s:   old SF value %x.%x\n",
310            __func__,  sf_item.requested, sf_item.holder);
311
312    // Remote (to this snoop filter) snoops update the filter already when they
313    // arrive from below, because we may not see any response.
314    if (cpkt->needsExclusive()) {
315        // If the request to this snoop response hit an in-flight transaction,
316        // the holder was not reset -> no assertion & do that here, now!
317        //assert(sf_item.holder == 0);
318        sf_item.holder = 0;
319    }
320    DPRINTF(SnoopFilter, "%s:   new SF value %x.%x\n",
321            __func__, sf_item.requested, sf_item.holder);
322    eraseIfNullEntry(sf_it);
323}
324
325void
326SnoopFilter::updateResponse(const Packet* cpkt, const SlavePort& slave_port)
327{
328    DPRINTF(SnoopFilter, "%s: packet src %s addr 0x%x cmd %s\n",
329            __func__, slave_port.name(), cpkt->getAddr(), cpkt->cmdString());
330
331    assert(cpkt->isResponse());
332
333    // Ultimately we should check if the packet came from an
334    // allocating source, not just if the port is snooping
335    bool allocate = !cpkt->req->isUncacheable() && slave_port.isSnooping();
336    if (!allocate)
337        return;
338
339    Addr line_addr = cpkt->getBlockAddr(linesize);
340    SnoopMask slave_mask = portToMask(slave_port);
341    SnoopItem& sf_item = cachedLocations[line_addr];
342
343    DPRINTF(SnoopFilter, "%s:   old SF value %x.%x\n",
344            __func__,  sf_item.requested, sf_item.holder);
345
346    // Make sure we have seen the actual request, too
347    panic_if(!(sf_item.requested & slave_mask), "SF value %x.%x missing "\
348             "request bit\n", sf_item.requested, sf_item.holder);
349
350    // Update the residency of the cache line. Here we assume that the
351    // line has been zapped in all caches that are not the responder.
352     if (cpkt->needsExclusive() || !cpkt->sharedAsserted())
353        sf_item.holder = 0;
354    sf_item.holder |=  slave_mask;
355    sf_item.requested &= ~slave_mask;
356    assert(sf_item.holder | sf_item.requested);
357    DPRINTF(SnoopFilter, "%s:   new SF value %x.%x\n",
358            __func__, sf_item.requested, sf_item.holder);
359}
360
361void
362SnoopFilter::regStats()
363{
364    totRequests
365        .name(name() + ".tot_requests")
366        .desc("Total number of requests made to the snoop filter.");
367
368    hitSingleRequests
369        .name(name() + ".hit_single_requests")
370        .desc("Number of requests hitting in the snoop filter with a single "\
371              "holder of the requested data.");
372
373    hitMultiRequests
374        .name(name() + ".hit_multi_requests")
375        .desc("Number of requests hitting in the snoop filter with multiple "\
376              "(>1) holders of the requested data.");
377
378    totSnoops
379        .name(name() + ".tot_snoops")
380        .desc("Total number of snoops made to the snoop filter.");
381
382    hitSingleSnoops
383        .name(name() + ".hit_single_snoops")
384        .desc("Number of snoops hitting in the snoop filter with a single "\
385              "holder of the requested data.");
386
387    hitMultiSnoops
388        .name(name() + ".hit_multi_snoops")
389        .desc("Number of snoops hitting in the snoop filter with multiple "\
390              "(>1) holders of the requested data.");
391}
392
393SnoopFilter *
394SnoopFilterParams::create()
395{
396    return new SnoopFilter(this);
397}
398