snoop_filter.hh revision 12429:beefb9f5f551
112855Sgabeblack@google.com/* 212855Sgabeblack@google.com * Copyright (c) 2013-2016 ARM Limited 312855Sgabeblack@google.com * All rights reserved 412855Sgabeblack@google.com * 512855Sgabeblack@google.com * The license below extends only to copyright in the software and shall 612855Sgabeblack@google.com * not be construed as granting a license to any other intellectual 712855Sgabeblack@google.com * property including but not limited to intellectual property relating 812855Sgabeblack@google.com * to a hardware implementation of the functionality of the software 912855Sgabeblack@google.com * licensed hereunder. You may use the software subject to the license 1012855Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated 1112855Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software, 1212855Sgabeblack@google.com * modified or unmodified, in source code or in binary form. 1312855Sgabeblack@google.com * 1412855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 1512855Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1612855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 1712855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 1812855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1912855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 2012855Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 2112855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 2212855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 2312855Sgabeblack@google.com * this software without specific prior written permission. 2412855Sgabeblack@google.com * 2512855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2612855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2712855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2812855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2912855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3012855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3112855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3212855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3312855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3412855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3512855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3612855Sgabeblack@google.com * 3712855Sgabeblack@google.com * Authors: Stephan Diestelhorst 3812855Sgabeblack@google.com */ 3912855Sgabeblack@google.com 4012855Sgabeblack@google.com/** 4112855Sgabeblack@google.com * @file 4212855Sgabeblack@google.com * Definition of a snoop filter. 4312855Sgabeblack@google.com */ 4412855Sgabeblack@google.com 4512855Sgabeblack@google.com#ifndef __MEM_SNOOP_FILTER_HH__ 4612855Sgabeblack@google.com#define __MEM_SNOOP_FILTER_HH__ 4712855Sgabeblack@google.com 4812855Sgabeblack@google.com#include <unordered_map> 4912855Sgabeblack@google.com#include <utility> 5012855Sgabeblack@google.com 5112855Sgabeblack@google.com#include "mem/packet.hh" 5212855Sgabeblack@google.com#include "mem/port.hh" 5312855Sgabeblack@google.com#include "mem/qport.hh" 5412855Sgabeblack@google.com#include "params/SnoopFilter.hh" 5512855Sgabeblack@google.com#include "sim/sim_object.hh" 5612855Sgabeblack@google.com#include "sim/system.hh" 5712855Sgabeblack@google.com 5812855Sgabeblack@google.com/** 5912855Sgabeblack@google.com * This snoop filter keeps track of which connected port has a 6012855Sgabeblack@google.com * particular line of data. It can be queried (through lookup*) on 6112855Sgabeblack@google.com * memory requests from above (reads / writes / ...); and also from 6212855Sgabeblack@google.com * below (snoops). The snoop filter precisely knows about the location 6312855Sgabeblack@google.com * of lines "above" it through a map from cache line address to 6412855Sgabeblack@google.com * sharers/ports. The snoop filter ties into the flows of requests 6512855Sgabeblack@google.com * (when they succeed at the lower interface), regular responses from 6612855Sgabeblack@google.com * below and also responses from sideway's caches (in update*). This 6712855Sgabeblack@google.com * allows the snoop filter to model cache-line residency by snooping 6812855Sgabeblack@google.com * the messages. 6912855Sgabeblack@google.com * 7012855Sgabeblack@google.com * The tracking happens in two fields to be able to distinguish 7112855Sgabeblack@google.com * between in-flight requests (in requested) and already pulled in 7212855Sgabeblack@google.com * lines (in holder). This distinction is used for producing tighter 7312855Sgabeblack@google.com * assertions and tracking request completion. For safety, (requested 7412855Sgabeblack@google.com * | holder) should be notified and the requesting MSHRs will take 7512855Sgabeblack@google.com * care of ordering. 7612855Sgabeblack@google.com * 7712855Sgabeblack@google.com * Overall, some trickery is required because: 7812855Sgabeblack@google.com * (1) snoops are not followed by an ACK, but only evoke a response if 7912855Sgabeblack@google.com * they need to (hit dirty) 8012855Sgabeblack@google.com * (2) side-channel information is funnelled through direct modifications of 8112855Sgabeblack@google.com * pkt, instead of proper messages through the bus 8212855Sgabeblack@google.com * (3) there are no clean evict messages telling the snoop filter that a local, 8312855Sgabeblack@google.com * upper cache dropped a line, making the snoop filter pessimistic for now 8412855Sgabeblack@google.com * (4) ordering: there is no single point of order in the system. Instead, 8512855Sgabeblack@google.com * requesting MSHRs track order between local requests and remote snoops 8612855Sgabeblack@google.com */ 8712855Sgabeblack@google.comclass SnoopFilter : public SimObject { 8812855Sgabeblack@google.com public: 8912855Sgabeblack@google.com typedef std::vector<QueuedSlavePort*> SnoopList; 9012855Sgabeblack@google.com 9112855Sgabeblack@google.com SnoopFilter (const SnoopFilterParams *p) : 9212855Sgabeblack@google.com SimObject(p), reqLookupResult(cachedLocations.end()), retryItem{0, 0}, 9312855Sgabeblack@google.com linesize(p->system->cacheLineSize()), lookupLatency(p->lookup_latency), 9412855Sgabeblack@google.com maxEntryCount(p->max_capacity / p->system->cacheLineSize()) 9512855Sgabeblack@google.com { 9612855Sgabeblack@google.com } 9712855Sgabeblack@google.com 9812855Sgabeblack@google.com /** 9912855Sgabeblack@google.com * Init a new snoop filter and tell it about all the slave ports 10012855Sgabeblack@google.com * of the enclosing bus. 10112855Sgabeblack@google.com * 102 * @param slave_ports Slave ports that the bus is attached to. 103 */ 104 void setSlavePorts(const SnoopList& slave_ports) { 105 localSlavePortIds.resize(slave_ports.size(), InvalidPortID); 106 107 PortID id = 0; 108 for (const auto& p : slave_ports) { 109 // no need to track this port if it is not snooping 110 if (p->isSnooping()) { 111 slavePorts.push_back(p); 112 localSlavePortIds[p->getId()] = id++; 113 } 114 } 115 116 // make sure we can deal with this many ports 117 fatal_if(id > 8 * sizeof(SnoopMask), 118 "Snoop filter only supports %d snooping ports, got %d\n", 119 8 * sizeof(SnoopMask), id); 120 } 121 122 /** 123 * Lookup a request (from a slave port) in the snoop filter and 124 * return a list of other slave ports that need forwarding of the 125 * resulting snoops. Additionally, update the tracking structures 126 * with new request information. Note that the caller must also 127 * call finishRequest once it is known if the request needs to 128 * retry or not. 129 * 130 * @param cpkt Pointer to the request packet. Not changed. 131 * @param slave_port Slave port where the request came from. 132 * @return Pair of a vector of snoop target ports and lookup latency. 133 */ 134 std::pair<SnoopList, Cycles> lookupRequest(const Packet* cpkt, 135 const SlavePort& slave_port); 136 137 /** 138 * For an un-successful request, revert the change to the snoop 139 * filter. Also take care of erasing any null entries. This method 140 * relies on the result from lookupRequest being stored in 141 * reqLookupResult. 142 * 143 * @param will_retry This request will retry on this bus / snoop filter 144 * @param addr Packet address, merely for sanity checking 145 */ 146 void finishRequest(bool will_retry, Addr addr, bool is_secure); 147 148 /** 149 * Handle an incoming snoop from below (the master port). These 150 * can upgrade the tracking logic and may also benefit from 151 * additional steering thanks to the snoop filter. 152 * 153 * @param cpkt Pointer to const Packet containing the snoop. 154 * @return Pair with a vector of SlavePorts that need snooping and a lookup 155 * latency. 156 */ 157 std::pair<SnoopList, Cycles> lookupSnoop(const Packet* cpkt); 158 159 /** 160 * Let the snoop filter see any snoop responses that turn into 161 * request responses and indicate cache to cache transfers. These 162 * will update the corresponding state in the filter. 163 * 164 * @param cpkt Pointer to const Packet holding the snoop response. 165 * @param rsp_port SlavePort that sends the response. 166 * @param req_port SlavePort that made the original request and is the 167 * destination of the snoop response. 168 */ 169 void updateSnoopResponse(const Packet *cpkt, const SlavePort& rsp_port, 170 const SlavePort& req_port); 171 172 /** 173 * Pass snoop responses that travel downward through the snoop 174 * filter and let them update the snoop filter state. No 175 * additional routing happens. 176 * 177 * @param cpkt Pointer to const Packet holding the snoop response. 178 * @param rsp_port SlavePort that sends the response. 179 * @param req_port MasterPort through which the response is forwarded. 180 */ 181 void updateSnoopForward(const Packet *cpkt, const SlavePort& rsp_port, 182 const MasterPort& req_port); 183 184 /** 185 * Update the snoop filter with a response from below (outer / 186 * other cache, or memory) and update the tracking information in 187 * the snoop filter. 188 * 189 * @param cpkt Pointer to const Packet holding the snoop response. 190 * @param slave_port SlavePort that made the original request and 191 * is the target of this response. 192 */ 193 void updateResponse(const Packet *cpkt, const SlavePort& slave_port); 194 195 virtual void regStats(); 196 197 protected: 198 199 /** 200 * The underlying type for the bitmask we use for tracking. This 201 * limits the number of snooping ports supported per crossbar. For 202 * the moment it is an uint64_t to offer maximum 203 * scalability. However, it is possible to use e.g. a uint16_t or 204 * uint32_to slim down the footprint of the hash map (and 205 * ultimately improve the simulation performance). 206 */ 207 typedef uint64_t SnoopMask; 208 209 /** 210 * Per cache line item tracking a bitmask of SlavePorts who have an 211 * outstanding request to this line (requested) or already share a 212 * cache line with this address (holder). 213 */ 214 struct SnoopItem { 215 SnoopMask requested; 216 SnoopMask holder; 217 }; 218 /** 219 * HashMap of SnoopItems indexed by line address 220 */ 221 typedef std::unordered_map<Addr, SnoopItem> SnoopFilterCache; 222 223 /** 224 * Simple factory methods for standard return values. 225 */ 226 std::pair<SnoopList, Cycles> snoopAll(Cycles latency) const 227 { 228 return std::make_pair(slavePorts, latency); 229 } 230 std::pair<SnoopList, Cycles> snoopSelected(const SnoopList& slave_ports, 231 Cycles latency) const 232 { 233 return std::make_pair(slave_ports, latency); 234 } 235 std::pair<SnoopList, Cycles> snoopDown(Cycles latency) const 236 { 237 SnoopList empty; 238 return std::make_pair(empty , latency); 239 } 240 241 /** 242 * Convert a single port to a corresponding, one-hot bitmask 243 * @param port SlavePort that should be converted. 244 * @return One-hot bitmask corresponding to the port. 245 */ 246 SnoopMask portToMask(const SlavePort& port) const; 247 /** 248 * Converts a bitmask of ports into the corresponing list of ports 249 * @param ports SnoopMask of the requested ports 250 * @return SnoopList containing all the requested SlavePorts 251 */ 252 SnoopList maskToPortList(SnoopMask ports) const; 253 254 private: 255 256 /** 257 * Removes snoop filter items which have no requesters and no holders. 258 */ 259 void eraseIfNullEntry(SnoopFilterCache::iterator& sf_it); 260 261 /** Simple hash set of cached addresses. */ 262 SnoopFilterCache cachedLocations; 263 /** 264 * Iterator used to store the result from lookupRequest until we 265 * call finishRequest. 266 */ 267 SnoopFilterCache::iterator reqLookupResult; 268 /** 269 * Variable to temporarily store value of snoopfilter entry 270 * incase finishRequest needs to undo changes made in lookupRequest 271 * (because of crossbar retry) 272 */ 273 SnoopItem retryItem; 274 /** List of all attached snooping slave ports. */ 275 SnoopList slavePorts; 276 /** Track the mapping from port ids to the local mask ids. */ 277 std::vector<PortID> localSlavePortIds; 278 /** Cache line size. */ 279 const unsigned linesize; 280 /** Latency for doing a lookup in the filter */ 281 const Cycles lookupLatency; 282 /** Max capacity in terms of cache blocks tracked, for sanity checking */ 283 const unsigned maxEntryCount; 284 285 /** 286 * Use the lower bits of the address to keep track of the line status 287 */ 288 enum LineStatus { 289 /** block holds data from the secure memory space */ 290 LineSecure = 0x01, 291 }; 292 293 /** Statistics */ 294 Stats::Scalar totRequests; 295 Stats::Scalar hitSingleRequests; 296 Stats::Scalar hitMultiRequests; 297 298 Stats::Scalar totSnoops; 299 Stats::Scalar hitSingleSnoops; 300 Stats::Scalar hitMultiSnoops; 301}; 302 303inline SnoopFilter::SnoopMask 304SnoopFilter::portToMask(const SlavePort& port) const 305{ 306 assert(port.getId() != InvalidPortID); 307 // if this is not a snooping port, return a zero mask 308 return !port.isSnooping() ? 0 : 309 ((SnoopMask)1) << localSlavePortIds[port.getId()]; 310} 311 312inline SnoopFilter::SnoopList 313SnoopFilter::maskToPortList(SnoopMask port_mask) const 314{ 315 SnoopList res; 316 for (const auto& p : slavePorts) 317 if (port_mask & portToMask(*p)) 318 res.push_back(p); 319 return res; 320} 321 322#endif // __MEM_SNOOP_FILTER_HH__ 323