snoop_filter.hh (11131:22e739752f47) snoop_filter.hh (11132:fbd597034299)
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#ifndef __MEM_SNOOP_FILTER_HH__
46#define __MEM_SNOOP_FILTER_HH__
47
48#include <utility>
49
50#include "base/hashmap.hh"
51#include "mem/packet.hh"
52#include "mem/port.hh"
53#include "mem/qport.hh"
54#include "params/SnoopFilter.hh"
55#include "sim/sim_object.hh"
56#include "sim/system.hh"
57
58/**
59 * This snoop filter keeps track of which connected port has a
60 * particular line of data. It can be queried (through lookup*) on
61 * memory requests from above (reads / writes / ...); and also from
62 * below (snoops). The snoop filter precisely knows about the location
63 * of lines "above" it through a map from cache line address to
64 * sharers/ports. The snoop filter ties into the flows of requests
65 * (when they succeed at the lower interface), regular responses from
66 * below and also responses from sideway's caches (in update*). This
67 * allows the snoop filter to model cache-line residency by snooping
68 * the messages.
69 *
70 * The tracking happens in two fields to be able to distinguish
71 * between in-flight requests (in requested) and already pulled in
72 * lines (in holder). This distinction is used for producing tighter
73 * assertions and tracking request completion. For safety, (requested
74 * | holder) should be notified and the requesting MSHRs will take
75 * care of ordering.
76 *
77 * Overall, some trickery is required because:
78 * (1) snoops are not followed by an ACK, but only evoke a response if
79 * they need to (hit dirty)
80 * (2) side-channel information is funnelled through direct modifications of
81 * pkt, instead of proper messages through the bus
82 * (3) there are no clean evict messages telling the snoop filter that a local,
83 * upper cache dropped a line, making the snoop filter pessimistic for now
84 * (4) ordering: there is no single point of order in the system. Instead,
85 * requesting MSHRs track order between local requests and remote snoops
86 */
87class SnoopFilter : public SimObject {
88 public:
89 typedef std::vector<QueuedSlavePort*> SnoopList;
90
91 SnoopFilter (const SnoopFilterParams *p) :
92 SimObject(p), reqLookupResult(cachedLocations.end()), retryItem{0, 0},
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#ifndef __MEM_SNOOP_FILTER_HH__
46#define __MEM_SNOOP_FILTER_HH__
47
48#include <utility>
49
50#include "base/hashmap.hh"
51#include "mem/packet.hh"
52#include "mem/port.hh"
53#include "mem/qport.hh"
54#include "params/SnoopFilter.hh"
55#include "sim/sim_object.hh"
56#include "sim/system.hh"
57
58/**
59 * This snoop filter keeps track of which connected port has a
60 * particular line of data. It can be queried (through lookup*) on
61 * memory requests from above (reads / writes / ...); and also from
62 * below (snoops). The snoop filter precisely knows about the location
63 * of lines "above" it through a map from cache line address to
64 * sharers/ports. The snoop filter ties into the flows of requests
65 * (when they succeed at the lower interface), regular responses from
66 * below and also responses from sideway's caches (in update*). This
67 * allows the snoop filter to model cache-line residency by snooping
68 * the messages.
69 *
70 * The tracking happens in two fields to be able to distinguish
71 * between in-flight requests (in requested) and already pulled in
72 * lines (in holder). This distinction is used for producing tighter
73 * assertions and tracking request completion. For safety, (requested
74 * | holder) should be notified and the requesting MSHRs will take
75 * care of ordering.
76 *
77 * Overall, some trickery is required because:
78 * (1) snoops are not followed by an ACK, but only evoke a response if
79 * they need to (hit dirty)
80 * (2) side-channel information is funnelled through direct modifications of
81 * pkt, instead of proper messages through the bus
82 * (3) there are no clean evict messages telling the snoop filter that a local,
83 * upper cache dropped a line, making the snoop filter pessimistic for now
84 * (4) ordering: there is no single point of order in the system. Instead,
85 * requesting MSHRs track order between local requests and remote snoops
86 */
87class SnoopFilter : public SimObject {
88 public:
89 typedef std::vector<QueuedSlavePort*> SnoopList;
90
91 SnoopFilter (const SnoopFilterParams *p) :
92 SimObject(p), reqLookupResult(cachedLocations.end()), retryItem{0, 0},
93 linesize(p->system->cacheLineSize()), lookupLatency(p->lookup_latency)
93 linesize(p->system->cacheLineSize()), lookupLatency(p->lookup_latency),
94 maxEntryCount(p->max_capacity / p->system->cacheLineSize())
94 {
95 }
96
97 /**
98 * Init a new snoop filter and tell it about all the slave ports of the
99 * enclosing bus.
100 *
101 * @param bus_slave_ports Vector of slave ports that the bus is attached to.
102 */
103 void setSlavePorts(const SnoopList& bus_slave_ports) {
104 slavePorts = bus_slave_ports;
105 }
106
107 /**
108 * Lookup a request (from a slave port) in the snoop filter and
109 * return a list of other slave ports that need forwarding of the
110 * resulting snoops. Additionally, update the tracking structures
111 * with new request information. Note that the caller must also
112 * call finishRequest once it is known if the request needs to
113 * retry or not.
114 *
115 * @param cpkt Pointer to the request packet. Not changed.
116 * @param slave_port Slave port where the request came from.
117 * @return Pair of a vector of snoop target ports and lookup latency.
118 */
119 std::pair<SnoopList, Cycles> lookupRequest(const Packet* cpkt,
120 const SlavePort& slave_port);
121
122 /**
123 * For an un-successful request, revert the change to the snoop
124 * filter. Also take care of erasing any null entries. This method
125 * relies on the result from lookupRequest being stored in
126 * reqLookupResult.
127 *
128 * @param will_retry This request will retry on this bus / snoop filter
129 * @param cpkt Request packet, merely for sanity checking
130 */
131 void finishRequest(bool will_retry, const Packet* cpkt);
132
133 /**
134 * Handle an incoming snoop from below (the master port). These can upgrade the
135 * tracking logic and may also benefit from additional steering thanks to the
136 * snoop filter.
137 * @param cpkt Pointer to const Packet containing the snoop.
138 * @return Pair with a vector of SlavePorts that need snooping and a lookup
139 * latency.
140 */
141 std::pair<SnoopList, Cycles> lookupSnoop(const Packet* cpkt);
142
143 /**
144 * Let the snoop filter see any snoop responses that turn into request responses
145 * and indicate cache to cache transfers. These will update the corresponding
146 * state in the filter.
147 *
148 * @param cpkt Pointer to const Packet holding the snoop response.
149 * @param rsp_port SlavePort that sends the response.
150 * @param req_port SlavePort that made the original request and is the
151 * destination of the snoop response.
152 */
153 void updateSnoopResponse(const Packet *cpkt, const SlavePort& rsp_port,
154 const SlavePort& req_port);
155
156 /**
157 * Pass snoop responses that travel downward through the snoop filter and let
158 * them update the snoop filter state. No additional routing happens.
159 *
160 * @param cpkt Pointer to const Packet holding the snoop response.
161 * @param rsp_port SlavePort that sends the response.
162 * @param req_port MasterPort through which the response leaves this cluster.
163 */
164 void updateSnoopForward(const Packet *cpkt, const SlavePort& rsp_port,
165 const MasterPort& req_port);
166
167 /**
168 * Update the snoop filter with a response from below (outer / other cache,
169 * or memory) and update the tracking information in the snoop filter.
170 *
171 * @param cpkt Pointer to const Packet holding the snoop response.
172 * @param slave_port SlavePort that made the original request and is the target
173 * of this response.
174 */
175 void updateResponse(const Packet *cpkt, const SlavePort& slave_port);
176
177 /**
178 * Simple factory methods for standard return values for lookupRequest
179 */
180 std::pair<SnoopList, Cycles> snoopAll(Cycles latency) const
181 {
182 return std::make_pair(slavePorts, latency);
183 }
184 std::pair<SnoopList, Cycles> snoopSelected(const SnoopList& slave_ports,
185 Cycles latency) const
186 {
187 return std::make_pair(slave_ports, latency);
188 }
189 std::pair<SnoopList, Cycles> snoopDown(Cycles latency) const
190 {
191 SnoopList empty;
192 return std::make_pair(empty , latency);
193 }
194
195 virtual void regStats();
196
197 protected:
198 typedef uint64_t SnoopMask;
199 /**
200 * Per cache line item tracking a bitmask of SlavePorts who have an
201 * outstanding request to this line (requested) or already share a cache line
202 * with this address (holder).
203 */
204 struct SnoopItem {
205 SnoopMask requested;
206 SnoopMask holder;
207 };
208 /**
209 * HashMap of SnoopItems indexed by line address
210 */
211 typedef m5::hash_map<Addr, SnoopItem> SnoopFilterCache;
212
213 /**
214 * Convert a single port to a corresponding, one-hot bitmask
215 * @param port SlavePort that should be converted.
216 * @return One-hot bitmask corresponding to the port.
217 */
218 SnoopMask portToMask(const SlavePort& port) const;
219 /**
220 * Convert multiple ports to a corresponding bitmask
221 * @param ports SnoopList that should be converted.
222 * @return Bitmask corresponding to the ports in the list.
223 */
224 SnoopMask portListToMask(const SnoopList& ports) const;
225 /**
226 * Converts a bitmask of ports into the corresponing list of ports
227 * @param ports SnoopMask of the requested ports
228 * @return SnoopList containing all the requested SlavePorts
229 */
230 SnoopList maskToPortList(SnoopMask ports) const;
231
232 private:
233
234 /**
235 * Removes snoop filter items which have no requesters and no holders.
236 */
237 void eraseIfNullEntry(SnoopFilterCache::iterator& sf_it);
238 /** Simple hash set of cached addresses. */
239 SnoopFilterCache cachedLocations;
240 /**
241 * Iterator used to store the result from lookupRequest until we
242 * call finishRequest.
243 */
244 SnoopFilterCache::iterator reqLookupResult;
245 /**
246 * Variable to temporarily store value of snoopfilter entry
247 * incase finishRequest needs to undo changes made in lookupRequest
248 * (because of crossbar retry)
249 */
250 SnoopItem retryItem;
251 /** List of all attached slave ports. */
252 SnoopList slavePorts;
253 /** Cache line size. */
254 const unsigned linesize;
255 /** Latency for doing a lookup in the filter */
256 const Cycles lookupLatency;
95 {
96 }
97
98 /**
99 * Init a new snoop filter and tell it about all the slave ports of the
100 * enclosing bus.
101 *
102 * @param bus_slave_ports Vector of slave ports that the bus is attached to.
103 */
104 void setSlavePorts(const SnoopList& bus_slave_ports) {
105 slavePorts = bus_slave_ports;
106 }
107
108 /**
109 * Lookup a request (from a slave port) in the snoop filter and
110 * return a list of other slave ports that need forwarding of the
111 * resulting snoops. Additionally, update the tracking structures
112 * with new request information. Note that the caller must also
113 * call finishRequest once it is known if the request needs to
114 * retry or not.
115 *
116 * @param cpkt Pointer to the request packet. Not changed.
117 * @param slave_port Slave port where the request came from.
118 * @return Pair of a vector of snoop target ports and lookup latency.
119 */
120 std::pair<SnoopList, Cycles> lookupRequest(const Packet* cpkt,
121 const SlavePort& slave_port);
122
123 /**
124 * For an un-successful request, revert the change to the snoop
125 * filter. Also take care of erasing any null entries. This method
126 * relies on the result from lookupRequest being stored in
127 * reqLookupResult.
128 *
129 * @param will_retry This request will retry on this bus / snoop filter
130 * @param cpkt Request packet, merely for sanity checking
131 */
132 void finishRequest(bool will_retry, const Packet* cpkt);
133
134 /**
135 * Handle an incoming snoop from below (the master port). These can upgrade the
136 * tracking logic and may also benefit from additional steering thanks to the
137 * snoop filter.
138 * @param cpkt Pointer to const Packet containing the snoop.
139 * @return Pair with a vector of SlavePorts that need snooping and a lookup
140 * latency.
141 */
142 std::pair<SnoopList, Cycles> lookupSnoop(const Packet* cpkt);
143
144 /**
145 * Let the snoop filter see any snoop responses that turn into request responses
146 * and indicate cache to cache transfers. These will update the corresponding
147 * state in the filter.
148 *
149 * @param cpkt Pointer to const Packet holding the snoop response.
150 * @param rsp_port SlavePort that sends the response.
151 * @param req_port SlavePort that made the original request and is the
152 * destination of the snoop response.
153 */
154 void updateSnoopResponse(const Packet *cpkt, const SlavePort& rsp_port,
155 const SlavePort& req_port);
156
157 /**
158 * Pass snoop responses that travel downward through the snoop filter and let
159 * them update the snoop filter state. No additional routing happens.
160 *
161 * @param cpkt Pointer to const Packet holding the snoop response.
162 * @param rsp_port SlavePort that sends the response.
163 * @param req_port MasterPort through which the response leaves this cluster.
164 */
165 void updateSnoopForward(const Packet *cpkt, const SlavePort& rsp_port,
166 const MasterPort& req_port);
167
168 /**
169 * Update the snoop filter with a response from below (outer / other cache,
170 * or memory) and update the tracking information in the snoop filter.
171 *
172 * @param cpkt Pointer to const Packet holding the snoop response.
173 * @param slave_port SlavePort that made the original request and is the target
174 * of this response.
175 */
176 void updateResponse(const Packet *cpkt, const SlavePort& slave_port);
177
178 /**
179 * Simple factory methods for standard return values for lookupRequest
180 */
181 std::pair<SnoopList, Cycles> snoopAll(Cycles latency) const
182 {
183 return std::make_pair(slavePorts, latency);
184 }
185 std::pair<SnoopList, Cycles> snoopSelected(const SnoopList& slave_ports,
186 Cycles latency) const
187 {
188 return std::make_pair(slave_ports, latency);
189 }
190 std::pair<SnoopList, Cycles> snoopDown(Cycles latency) const
191 {
192 SnoopList empty;
193 return std::make_pair(empty , latency);
194 }
195
196 virtual void regStats();
197
198 protected:
199 typedef uint64_t SnoopMask;
200 /**
201 * Per cache line item tracking a bitmask of SlavePorts who have an
202 * outstanding request to this line (requested) or already share a cache line
203 * with this address (holder).
204 */
205 struct SnoopItem {
206 SnoopMask requested;
207 SnoopMask holder;
208 };
209 /**
210 * HashMap of SnoopItems indexed by line address
211 */
212 typedef m5::hash_map<Addr, SnoopItem> SnoopFilterCache;
213
214 /**
215 * Convert a single port to a corresponding, one-hot bitmask
216 * @param port SlavePort that should be converted.
217 * @return One-hot bitmask corresponding to the port.
218 */
219 SnoopMask portToMask(const SlavePort& port) const;
220 /**
221 * Convert multiple ports to a corresponding bitmask
222 * @param ports SnoopList that should be converted.
223 * @return Bitmask corresponding to the ports in the list.
224 */
225 SnoopMask portListToMask(const SnoopList& ports) const;
226 /**
227 * Converts a bitmask of ports into the corresponing list of ports
228 * @param ports SnoopMask of the requested ports
229 * @return SnoopList containing all the requested SlavePorts
230 */
231 SnoopList maskToPortList(SnoopMask ports) const;
232
233 private:
234
235 /**
236 * Removes snoop filter items which have no requesters and no holders.
237 */
238 void eraseIfNullEntry(SnoopFilterCache::iterator& sf_it);
239 /** Simple hash set of cached addresses. */
240 SnoopFilterCache cachedLocations;
241 /**
242 * Iterator used to store the result from lookupRequest until we
243 * call finishRequest.
244 */
245 SnoopFilterCache::iterator reqLookupResult;
246 /**
247 * Variable to temporarily store value of snoopfilter entry
248 * incase finishRequest needs to undo changes made in lookupRequest
249 * (because of crossbar retry)
250 */
251 SnoopItem retryItem;
252 /** List of all attached slave ports. */
253 SnoopList slavePorts;
254 /** Cache line size. */
255 const unsigned linesize;
256 /** Latency for doing a lookup in the filter */
257 const Cycles lookupLatency;
258 /** Max capacity in terms of cache blocks tracked, for sanity checking */
259 const unsigned maxEntryCount;
257
258 /** Statistics */
259 Stats::Scalar totRequests;
260 Stats::Scalar hitSingleRequests;
261 Stats::Scalar hitMultiRequests;
262
263 Stats::Scalar totSnoops;
264 Stats::Scalar hitSingleSnoops;
265 Stats::Scalar hitMultiSnoops;
266};
267
268inline SnoopFilter::SnoopMask
269SnoopFilter::portToMask(const SlavePort& port) const
270{
271 unsigned id = (unsigned)port.getId();
272 assert(id != (unsigned)InvalidPortID);
273 assert((int)id < 8 * sizeof(SnoopMask));
274
275 return ((SnoopMask)1) << id;
276}
277
278inline SnoopFilter::SnoopMask
279SnoopFilter::portListToMask(const SnoopList& ports) const
280{
281 SnoopMask m = 0;
282 for (auto port = ports.begin(); port != ports.end(); ++port)
283 m |= portToMask(**port);
284 return m;
285}
286
287inline SnoopFilter::SnoopList
288SnoopFilter::maskToPortList(SnoopMask port_mask) const
289{
290 SnoopList res;
291 for (auto port = slavePorts.begin(); port != slavePorts.end(); ++port)
292 if (port_mask & portToMask(**port))
293 res.push_back(*port);
294 return res;
295}
296#endif // __MEM_SNOOP_FILTER_HH__
260
261 /** Statistics */
262 Stats::Scalar totRequests;
263 Stats::Scalar hitSingleRequests;
264 Stats::Scalar hitMultiRequests;
265
266 Stats::Scalar totSnoops;
267 Stats::Scalar hitSingleSnoops;
268 Stats::Scalar hitMultiSnoops;
269};
270
271inline SnoopFilter::SnoopMask
272SnoopFilter::portToMask(const SlavePort& port) const
273{
274 unsigned id = (unsigned)port.getId();
275 assert(id != (unsigned)InvalidPortID);
276 assert((int)id < 8 * sizeof(SnoopMask));
277
278 return ((SnoopMask)1) << id;
279}
280
281inline SnoopFilter::SnoopMask
282SnoopFilter::portListToMask(const SnoopList& ports) const
283{
284 SnoopMask m = 0;
285 for (auto port = ports.begin(); port != ports.end(); ++port)
286 m |= portToMask(**port);
287 return m;
288}
289
290inline SnoopFilter::SnoopList
291SnoopFilter::maskToPortList(SnoopMask port_mask) const
292{
293 SnoopList res;
294 for (auto port = slavePorts.begin(); port != slavePorts.end(); ++port)
295 if (port_mask & portToMask(**port))
296 res.push_back(*port);
297 return res;
298}
299#endif // __MEM_SNOOP_FILTER_HH__