mem: Make cache terminology easier to understand
[gem5.git] / src / mem / coherent_xbar.cc
1 /*
2 * Copyright (c) 2011-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 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
43 */
44
45 /**
46 * @file
47 * Definition of a crossbar object.
48 */
49
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/AddrRanges.hh"
53 #include "debug/CoherentXBar.hh"
54 #include "mem/coherent_xbar.hh"
55 #include "sim/system.hh"
56
57 CoherentXBar::CoherentXBar(const CoherentXBarParams *p)
58 : BaseXBar(p), system(p->system), snoopFilter(p->snoop_filter),
59 snoopResponseLatency(p->snoop_response_latency)
60 {
61 // create the ports based on the size of the master and slave
62 // vector ports, and the presence of the default port, the ports
63 // are enumerated starting from zero
64 for (int i = 0; i < p->port_master_connection_count; ++i) {
65 std::string portName = csprintf("%s.master[%d]", name(), i);
66 MasterPort* bp = new CoherentXBarMasterPort(portName, *this, i);
67 masterPorts.push_back(bp);
68 reqLayers.push_back(new ReqLayer(*bp, *this,
69 csprintf(".reqLayer%d", i)));
70 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
71 csprintf(".snoopLayer%d", i)));
72 }
73
74 // see if we have a default slave device connected and if so add
75 // our corresponding master port
76 if (p->port_default_connection_count) {
77 defaultPortID = masterPorts.size();
78 std::string portName = name() + ".default";
79 MasterPort* bp = new CoherentXBarMasterPort(portName, *this,
80 defaultPortID);
81 masterPorts.push_back(bp);
82 reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
83 defaultPortID)));
84 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
85 csprintf(".snoopLayer%d",
86 defaultPortID)));
87 }
88
89 // create the slave ports, once again starting at zero
90 for (int i = 0; i < p->port_slave_connection_count; ++i) {
91 std::string portName = csprintf("%s.slave[%d]", name(), i);
92 QueuedSlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
93 slavePorts.push_back(bp);
94 respLayers.push_back(new RespLayer(*bp, *this,
95 csprintf(".respLayer%d", i)));
96 snoopRespPorts.push_back(new SnoopRespPort(*bp, *this));
97 }
98
99 clearPortCache();
100 }
101
102 CoherentXBar::~CoherentXBar()
103 {
104 for (auto l: reqLayers)
105 delete l;
106 for (auto l: respLayers)
107 delete l;
108 for (auto l: snoopLayers)
109 delete l;
110 for (auto p: snoopRespPorts)
111 delete p;
112 }
113
114 void
115 CoherentXBar::init()
116 {
117 // the base class is responsible for determining the block size
118 BaseXBar::init();
119
120 // iterate over our slave ports and determine which of our
121 // neighbouring master ports are snooping and add them as snoopers
122 for (const auto& p: slavePorts) {
123 // check if the connected master port is snooping
124 if (p->isSnooping()) {
125 DPRINTF(AddrRanges, "Adding snooping master %s\n",
126 p->getMasterPort().name());
127 snoopPorts.push_back(p);
128 }
129 }
130
131 if (snoopPorts.empty())
132 warn("CoherentXBar %s has no snooping ports attached!\n", name());
133
134 // inform the snoop filter about the slave ports so it can create
135 // its own internal representation
136 if (snoopFilter)
137 snoopFilter->setSlavePorts(slavePorts);
138 }
139
140 bool
141 CoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
142 {
143 // determine the source port based on the id
144 SlavePort *src_port = slavePorts[slave_port_id];
145
146 // remember if the packet is an express snoop
147 bool is_express_snoop = pkt->isExpressSnoop();
148 bool cache_responding = pkt->cacheResponding();
149 // for normal requests, going downstream, the express snoop flag
150 // and the cache responding flag should always be the same
151 assert(is_express_snoop == cache_responding);
152
153 // determine the destination based on the address
154 PortID master_port_id = findPort(pkt->getAddr());
155
156 // test if the crossbar should be considered occupied for the current
157 // port, and exclude express snoops from the check
158 if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
159 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x BUSY\n",
160 src_port->name(), pkt->cmdString(), pkt->getAddr());
161 return false;
162 }
163
164 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s expr %d 0x%x\n",
165 src_port->name(), pkt->cmdString(), is_express_snoop,
166 pkt->getAddr());
167
168 // store size and command as they might be modified when
169 // forwarding the packet
170 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
171 unsigned int pkt_cmd = pkt->cmdToIndex();
172
173 // store the old header delay so we can restore it if needed
174 Tick old_header_delay = pkt->headerDelay;
175
176 // a request sees the frontend and forward latency
177 Tick xbar_delay = (frontendLatency + forwardLatency) * clockPeriod();
178
179 // set the packet header and payload delay
180 calcPacketTiming(pkt, xbar_delay);
181
182 // determine how long to be crossbar layer is busy
183 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
184
185 if (!system->bypassCaches()) {
186 assert(pkt->snoopDelay == 0);
187
188 // the packet is a memory-mapped request and should be
189 // broadcasted to our snoopers but the source
190 if (snoopFilter) {
191 // check with the snoop filter where to forward this packet
192 auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
193 // the time required by a packet to be delivered through
194 // the xbar has to be charged also with to lookup latency
195 // of the snoop filter
196 pkt->headerDelay += sf_res.second * clockPeriod();
197 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x"\
198 " SF size: %i lat: %i\n", src_port->name(),
199 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
200 sf_res.second);
201
202 if (pkt->isEviction()) {
203 // for block-evicting packets, i.e. writebacks and
204 // clean evictions, there is no need to snoop up, as
205 // all we do is determine if the block is cached or
206 // not, instead just set it here based on the snoop
207 // filter result
208 if (!sf_res.first.empty())
209 pkt->setBlockCached();
210 } else {
211 forwardTiming(pkt, slave_port_id, sf_res.first);
212 }
213 } else {
214 forwardTiming(pkt, slave_port_id);
215 }
216
217 // add the snoop delay to our header delay, and then reset it
218 pkt->headerDelay += pkt->snoopDelay;
219 pkt->snoopDelay = 0;
220 }
221
222 // forwardTiming snooped into peer caches of the sender, and if
223 // this is a clean evict or clean writeback, but the packet is
224 // found in a cache, do not forward it
225 if ((pkt->cmd == MemCmd::CleanEvict ||
226 pkt->cmd == MemCmd::WritebackClean) && pkt->isBlockCached()) {
227 DPRINTF(CoherentXBar, "Clean evict/writeback %#llx still cached, "
228 "not forwarding\n", pkt->getAddr());
229
230 // update the layer state and schedule an idle event
231 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
232
233 // queue the packet for deletion
234 pendingDelete.reset(pkt);
235
236 return true;
237 }
238
239 // remember if the packet will generate a snoop response by
240 // checking if a cache set the cacheResponding flag during the
241 // snooping above
242 const bool expect_snoop_resp = !cache_responding && pkt->cacheResponding();
243 const bool expect_response = pkt->needsResponse() &&
244 !pkt->cacheResponding();
245
246 // since it is a normal request, attempt to send the packet
247 bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
248
249 if (snoopFilter && !system->bypassCaches()) {
250 // Let the snoop filter know about the success of the send operation
251 snoopFilter->finishRequest(!success, pkt);
252 }
253
254 // check if we were successful in sending the packet onwards
255 if (!success) {
256 // express snoops should never be forced to retry
257 assert(!is_express_snoop);
258
259 // restore the header delay
260 pkt->headerDelay = old_header_delay;
261
262 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x RETRY\n",
263 src_port->name(), pkt->cmdString(), pkt->getAddr());
264
265 // update the layer state and schedule an idle event
266 reqLayers[master_port_id]->failedTiming(src_port,
267 clockEdge(Cycles(1)));
268 } else {
269 // express snoops currently bypass the crossbar state entirely
270 if (!is_express_snoop) {
271 // if this particular request will generate a snoop
272 // response
273 if (expect_snoop_resp) {
274 // we should never have an exsiting request outstanding
275 assert(outstandingSnoop.find(pkt->req) ==
276 outstandingSnoop.end());
277 outstandingSnoop.insert(pkt->req);
278
279 // basic sanity check on the outstanding snoops
280 panic_if(outstandingSnoop.size() > 512,
281 "Outstanding snoop requests exceeded 512\n");
282 }
283
284 // remember where to route the normal response to
285 if (expect_response || expect_snoop_resp) {
286 assert(routeTo.find(pkt->req) == routeTo.end());
287 routeTo[pkt->req] = slave_port_id;
288
289 panic_if(routeTo.size() > 512,
290 "Routing table exceeds 512 packets\n");
291 }
292
293 // update the layer state and schedule an idle event
294 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
295 }
296
297 // stats updates only consider packets that were successfully sent
298 pktCount[slave_port_id][master_port_id]++;
299 pktSize[slave_port_id][master_port_id] += pkt_size;
300 transDist[pkt_cmd]++;
301
302 if (is_express_snoop)
303 snoops++;
304 }
305
306 return success;
307 }
308
309 bool
310 CoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
311 {
312 // determine the source port based on the id
313 MasterPort *src_port = masterPorts[master_port_id];
314
315 // determine the destination
316 const auto route_lookup = routeTo.find(pkt->req);
317 assert(route_lookup != routeTo.end());
318 const PortID slave_port_id = route_lookup->second;
319 assert(slave_port_id != InvalidPortID);
320 assert(slave_port_id < respLayers.size());
321
322 // test if the crossbar should be considered occupied for the
323 // current port
324 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
325 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x BUSY\n",
326 src_port->name(), pkt->cmdString(), pkt->getAddr());
327 return false;
328 }
329
330 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x\n",
331 src_port->name(), pkt->cmdString(), pkt->getAddr());
332
333 // store size and command as they might be modified when
334 // forwarding the packet
335 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
336 unsigned int pkt_cmd = pkt->cmdToIndex();
337
338 // a response sees the response latency
339 Tick xbar_delay = responseLatency * clockPeriod();
340
341 // set the packet header and payload delay
342 calcPacketTiming(pkt, xbar_delay);
343
344 // determine how long to be crossbar layer is busy
345 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
346
347 if (snoopFilter && !system->bypassCaches()) {
348 // let the snoop filter inspect the response and update its state
349 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
350 }
351
352 // send the packet through the destination slave port and pay for
353 // any outstanding header delay
354 Tick latency = pkt->headerDelay;
355 pkt->headerDelay = 0;
356 slavePorts[slave_port_id]->schedTimingResp(pkt, curTick() + latency);
357
358 // remove the request from the routing table
359 routeTo.erase(route_lookup);
360
361 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
362
363 // stats updates
364 pktCount[slave_port_id][master_port_id]++;
365 pktSize[slave_port_id][master_port_id] += pkt_size;
366 transDist[pkt_cmd]++;
367
368 return true;
369 }
370
371 void
372 CoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
373 {
374 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x\n",
375 masterPorts[master_port_id]->name(), pkt->cmdString(),
376 pkt->getAddr());
377
378 // update stats here as we know the forwarding will succeed
379 transDist[pkt->cmdToIndex()]++;
380 snoops++;
381
382 // we should only see express snoops from caches
383 assert(pkt->isExpressSnoop());
384
385 // set the packet header and payload delay, for now use forward latency
386 // @todo Assess the choice of latency further
387 calcPacketTiming(pkt, forwardLatency * clockPeriod());
388
389 // remember if a cache has already committed to responding so we
390 // can see if it changes during the snooping
391 const bool cache_responding = pkt->cacheResponding();
392
393 assert(pkt->snoopDelay == 0);
394
395 if (snoopFilter) {
396 // let the Snoop Filter work its magic and guide probing
397 auto sf_res = snoopFilter->lookupSnoop(pkt);
398 // the time required by a packet to be delivered through
399 // the xbar has to be charged also with to lookup latency
400 // of the snoop filter
401 pkt->headerDelay += sf_res.second * clockPeriod();
402 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x"\
403 " SF size: %i lat: %i\n", masterPorts[master_port_id]->name(),
404 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
405 sf_res.second);
406
407 // forward to all snoopers
408 forwardTiming(pkt, InvalidPortID, sf_res.first);
409 } else {
410 forwardTiming(pkt, InvalidPortID);
411 }
412
413 // add the snoop delay to our header delay, and then reset it
414 pkt->headerDelay += pkt->snoopDelay;
415 pkt->snoopDelay = 0;
416
417 // if we can expect a response, remember how to route it
418 if (!cache_responding && pkt->cacheResponding()) {
419 assert(routeTo.find(pkt->req) == routeTo.end());
420 routeTo[pkt->req] = master_port_id;
421 }
422
423 // a snoop request came from a connected slave device (one of
424 // our master ports), and if it is not coming from the slave
425 // device responsible for the address range something is
426 // wrong, hence there is nothing further to do as the packet
427 // would be going back to where it came from
428 assert(master_port_id == findPort(pkt->getAddr()));
429 }
430
431 bool
432 CoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
433 {
434 // determine the source port based on the id
435 SlavePort* src_port = slavePorts[slave_port_id];
436
437 // get the destination
438 const auto route_lookup = routeTo.find(pkt->req);
439 assert(route_lookup != routeTo.end());
440 const PortID dest_port_id = route_lookup->second;
441 assert(dest_port_id != InvalidPortID);
442
443 // determine if the response is from a snoop request we
444 // created as the result of a normal request (in which case it
445 // should be in the outstandingSnoop), or if we merely forwarded
446 // someone else's snoop request
447 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
448 outstandingSnoop.end();
449
450 // test if the crossbar should be considered occupied for the
451 // current port, note that the check is bypassed if the response
452 // is being passed on as a normal response since this is occupying
453 // the response layer rather than the snoop response layer
454 if (forwardAsSnoop) {
455 assert(dest_port_id < snoopLayers.size());
456 if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
457 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
458 src_port->name(), pkt->cmdString(), pkt->getAddr());
459 return false;
460 }
461 } else {
462 // get the master port that mirrors this slave port internally
463 MasterPort* snoop_port = snoopRespPorts[slave_port_id];
464 assert(dest_port_id < respLayers.size());
465 if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
466 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
467 snoop_port->name(), pkt->cmdString(), pkt->getAddr());
468 return false;
469 }
470 }
471
472 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x\n",
473 src_port->name(), pkt->cmdString(), pkt->getAddr());
474
475 // store size and command as they might be modified when
476 // forwarding the packet
477 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
478 unsigned int pkt_cmd = pkt->cmdToIndex();
479
480 // responses are never express snoops
481 assert(!pkt->isExpressSnoop());
482
483 // a snoop response sees the snoop response latency, and if it is
484 // forwarded as a normal response, the response latency
485 Tick xbar_delay =
486 (forwardAsSnoop ? snoopResponseLatency : responseLatency) *
487 clockPeriod();
488
489 // set the packet header and payload delay
490 calcPacketTiming(pkt, xbar_delay);
491
492 // determine how long to be crossbar layer is busy
493 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
494
495 // forward it either as a snoop response or a normal response
496 if (forwardAsSnoop) {
497 // this is a snoop response to a snoop request we forwarded,
498 // e.g. coming from the L1 and going to the L2, and it should
499 // be forwarded as a snoop response
500
501 if (snoopFilter) {
502 // update the probe filter so that it can properly track the line
503 snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
504 *masterPorts[dest_port_id]);
505 }
506
507 bool success M5_VAR_USED =
508 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
509 pktCount[slave_port_id][dest_port_id]++;
510 pktSize[slave_port_id][dest_port_id] += pkt_size;
511 assert(success);
512
513 snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
514 } else {
515 // we got a snoop response on one of our slave ports,
516 // i.e. from a coherent master connected to the crossbar, and
517 // since we created the snoop request as part of recvTiming,
518 // this should now be a normal response again
519 outstandingSnoop.erase(pkt->req);
520
521 // this is a snoop response from a coherent master, hence it
522 // should never go back to where the snoop response came from,
523 // but instead to where the original request came from
524 assert(slave_port_id != dest_port_id);
525
526 if (snoopFilter) {
527 // update the probe filter so that it can properly track the line
528 snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
529 *slavePorts[dest_port_id]);
530 }
531
532 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x"\
533 " FWD RESP\n", src_port->name(), pkt->cmdString(),
534 pkt->getAddr());
535
536 // as a normal response, it should go back to a master through
537 // one of our slave ports, we also pay for any outstanding
538 // header latency
539 Tick latency = pkt->headerDelay;
540 pkt->headerDelay = 0;
541 slavePorts[dest_port_id]->schedTimingResp(pkt, curTick() + latency);
542
543 respLayers[dest_port_id]->succeededTiming(packetFinishTime);
544 }
545
546 // remove the request from the routing table
547 routeTo.erase(route_lookup);
548
549 // stats updates
550 transDist[pkt_cmd]++;
551 snoops++;
552
553 return true;
554 }
555
556
557 void
558 CoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
559 const std::vector<QueuedSlavePort*>& dests)
560 {
561 DPRINTF(CoherentXBar, "%s for %s address %x size %d\n", __func__,
562 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
563
564 // snoops should only happen if the system isn't bypassing caches
565 assert(!system->bypassCaches());
566
567 unsigned fanout = 0;
568
569 for (const auto& p: dests) {
570 // we could have gotten this request from a snooping master
571 // (corresponding to our own slave port that is also in
572 // snoopPorts) and should not send it back to where it came
573 // from
574 if (exclude_slave_port_id == InvalidPortID ||
575 p->getId() != exclude_slave_port_id) {
576 // cache is not allowed to refuse snoop
577 p->sendTimingSnoopReq(pkt);
578 fanout++;
579 }
580 }
581
582 // Stats for fanout of this forward operation
583 snoopFanout.sample(fanout);
584 }
585
586 void
587 CoherentXBar::recvReqRetry(PortID master_port_id)
588 {
589 // responses and snoop responses never block on forwarding them,
590 // so the retry will always be coming from a port to which we
591 // tried to forward a request
592 reqLayers[master_port_id]->recvRetry();
593 }
594
595 Tick
596 CoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
597 {
598 DPRINTF(CoherentXBar, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
599 slavePorts[slave_port_id]->name(), pkt->getAddr(),
600 pkt->cmdString());
601
602 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
603 unsigned int pkt_cmd = pkt->cmdToIndex();
604
605 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
606 Tick snoop_response_latency = 0;
607
608 if (!system->bypassCaches()) {
609 // forward to all snoopers but the source
610 std::pair<MemCmd, Tick> snoop_result;
611 if (snoopFilter) {
612 // check with the snoop filter where to forward this packet
613 auto sf_res =
614 snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
615 snoop_response_latency += sf_res.second * clockPeriod();
616 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x"\
617 " SF size: %i lat: %i\n", __func__,
618 slavePorts[slave_port_id]->name(), pkt->cmdString(),
619 pkt->getAddr(), sf_res.first.size(), sf_res.second);
620
621 // let the snoop filter know about the success of the send
622 // operation, and do it even before sending it onwards to
623 // avoid situations where atomic upward snoops sneak in
624 // between and change the filter state
625 snoopFilter->finishRequest(false, pkt);
626
627 snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
628 sf_res.first);
629 } else {
630 snoop_result = forwardAtomic(pkt, slave_port_id);
631 }
632 snoop_response_cmd = snoop_result.first;
633 snoop_response_latency += snoop_result.second;
634 }
635
636 // forwardAtomic snooped into peer caches of the sender, and if
637 // this is a clean evict, but the packet is found in a cache, do
638 // not forward it
639 if ((pkt->cmd == MemCmd::CleanEvict ||
640 pkt->cmd == MemCmd::WritebackClean) && pkt->isBlockCached()) {
641 DPRINTF(CoherentXBar, "Clean evict/writeback %#llx still cached, "
642 "not forwarding\n", pkt->getAddr());
643 return 0;
644 }
645
646 // even if we had a snoop response, we must continue and also
647 // perform the actual request at the destination
648 PortID master_port_id = findPort(pkt->getAddr());
649
650 // stats updates for the request
651 pktCount[slave_port_id][master_port_id]++;
652 pktSize[slave_port_id][master_port_id] += pkt_size;
653 transDist[pkt_cmd]++;
654
655 // forward the request to the appropriate destination
656 Tick response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
657
658 // if lower levels have replied, tell the snoop filter
659 if (!system->bypassCaches() && snoopFilter && pkt->isResponse()) {
660 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
661 }
662
663 // if we got a response from a snooper, restore it here
664 if (snoop_response_cmd != MemCmd::InvalidCmd) {
665 // no one else should have responded
666 assert(!pkt->isResponse());
667 pkt->cmd = snoop_response_cmd;
668 response_latency = snoop_response_latency;
669 }
670
671 // add the response data
672 if (pkt->isResponse()) {
673 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
674 pkt_cmd = pkt->cmdToIndex();
675
676 // stats updates
677 pktCount[slave_port_id][master_port_id]++;
678 pktSize[slave_port_id][master_port_id] += pkt_size;
679 transDist[pkt_cmd]++;
680 }
681
682 // @todo: Not setting header time
683 pkt->payloadDelay = response_latency;
684 return response_latency;
685 }
686
687 Tick
688 CoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
689 {
690 DPRINTF(CoherentXBar, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
691 masterPorts[master_port_id]->name(), pkt->getAddr(),
692 pkt->cmdString());
693
694 // add the request snoop data
695 snoops++;
696
697 // forward to all snoopers
698 std::pair<MemCmd, Tick> snoop_result;
699 Tick snoop_response_latency = 0;
700 if (snoopFilter) {
701 auto sf_res = snoopFilter->lookupSnoop(pkt);
702 snoop_response_latency += sf_res.second * clockPeriod();
703 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x SF size: %i lat: %i\n",
704 __func__, masterPorts[master_port_id]->name(), pkt->cmdString(),
705 pkt->getAddr(), sf_res.first.size(), sf_res.second);
706 snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
707 sf_res.first);
708 } else {
709 snoop_result = forwardAtomic(pkt, InvalidPortID);
710 }
711 MemCmd snoop_response_cmd = snoop_result.first;
712 snoop_response_latency += snoop_result.second;
713
714 if (snoop_response_cmd != MemCmd::InvalidCmd)
715 pkt->cmd = snoop_response_cmd;
716
717 // add the response snoop data
718 if (pkt->isResponse()) {
719 snoops++;
720 }
721
722 // @todo: Not setting header time
723 pkt->payloadDelay = snoop_response_latency;
724 return snoop_response_latency;
725 }
726
727 std::pair<MemCmd, Tick>
728 CoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
729 PortID source_master_port_id,
730 const std::vector<QueuedSlavePort*>& dests)
731 {
732 // the packet may be changed on snoops, record the original
733 // command to enable us to restore it between snoops so that
734 // additional snoops can take place properly
735 MemCmd orig_cmd = pkt->cmd;
736 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
737 Tick snoop_response_latency = 0;
738
739 // snoops should only happen if the system isn't bypassing caches
740 assert(!system->bypassCaches());
741
742 unsigned fanout = 0;
743
744 for (const auto& p: dests) {
745 // we could have gotten this request from a snooping master
746 // (corresponding to our own slave port that is also in
747 // snoopPorts) and should not send it back to where it came
748 // from
749 if (exclude_slave_port_id != InvalidPortID &&
750 p->getId() == exclude_slave_port_id)
751 continue;
752
753 Tick latency = p->sendAtomicSnoop(pkt);
754 fanout++;
755
756 // in contrast to a functional access, we have to keep on
757 // going as all snoopers must be updated even if we get a
758 // response
759 if (!pkt->isResponse())
760 continue;
761
762 // response from snoop agent
763 assert(pkt->cmd != orig_cmd);
764 assert(pkt->cacheResponding());
765 // should only happen once
766 assert(snoop_response_cmd == MemCmd::InvalidCmd);
767 // save response state
768 snoop_response_cmd = pkt->cmd;
769 snoop_response_latency = latency;
770
771 if (snoopFilter) {
772 // Handle responses by the snoopers and differentiate between
773 // responses to requests from above and snoops from below
774 if (source_master_port_id != InvalidPortID) {
775 // Getting a response for a snoop from below
776 assert(exclude_slave_port_id == InvalidPortID);
777 snoopFilter->updateSnoopForward(pkt, *p,
778 *masterPorts[source_master_port_id]);
779 } else {
780 // Getting a response for a request from above
781 assert(source_master_port_id == InvalidPortID);
782 snoopFilter->updateSnoopResponse(pkt, *p,
783 *slavePorts[exclude_slave_port_id]);
784 }
785 }
786 // restore original packet state for remaining snoopers
787 pkt->cmd = orig_cmd;
788 }
789
790 // Stats for fanout
791 snoopFanout.sample(fanout);
792
793 // the packet is restored as part of the loop and any potential
794 // snoop response is part of the returned pair
795 return std::make_pair(snoop_response_cmd, snoop_response_latency);
796 }
797
798 void
799 CoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
800 {
801 if (!pkt->isPrint()) {
802 // don't do DPRINTFs on PrintReq as it clutters up the output
803 DPRINTF(CoherentXBar,
804 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
805 slavePorts[slave_port_id]->name(), pkt->getAddr(),
806 pkt->cmdString());
807 }
808
809 if (!system->bypassCaches()) {
810 // forward to all snoopers but the source
811 forwardFunctional(pkt, slave_port_id);
812 }
813
814 // there is no need to continue if the snooping has found what we
815 // were looking for and the packet is already a response
816 if (!pkt->isResponse()) {
817 // since our slave ports are queued ports we need to check them as well
818 for (const auto& p : slavePorts) {
819 // if we find a response that has the data, then the
820 // downstream caches/memories may be out of date, so simply stop
821 // here
822 if (p->checkFunctional(pkt)) {
823 if (pkt->needsResponse())
824 pkt->makeResponse();
825 return;
826 }
827 }
828
829 PortID dest_id = findPort(pkt->getAddr());
830
831 masterPorts[dest_id]->sendFunctional(pkt);
832 }
833 }
834
835 void
836 CoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
837 {
838 if (!pkt->isPrint()) {
839 // don't do DPRINTFs on PrintReq as it clutters up the output
840 DPRINTF(CoherentXBar,
841 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
842 masterPorts[master_port_id]->name(), pkt->getAddr(),
843 pkt->cmdString());
844 }
845
846 for (const auto& p : slavePorts) {
847 if (p->checkFunctional(pkt)) {
848 if (pkt->needsResponse())
849 pkt->makeResponse();
850 return;
851 }
852 }
853
854 // forward to all snoopers
855 forwardFunctional(pkt, InvalidPortID);
856 }
857
858 void
859 CoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
860 {
861 // snoops should only happen if the system isn't bypassing caches
862 assert(!system->bypassCaches());
863
864 for (const auto& p: snoopPorts) {
865 // we could have gotten this request from a snooping master
866 // (corresponding to our own slave port that is also in
867 // snoopPorts) and should not send it back to where it came
868 // from
869 if (exclude_slave_port_id == InvalidPortID ||
870 p->getId() != exclude_slave_port_id)
871 p->sendFunctionalSnoop(pkt);
872
873 // if we get a response we are done
874 if (pkt->isResponse()) {
875 break;
876 }
877 }
878 }
879
880 void
881 CoherentXBar::regStats()
882 {
883 // register the stats of the base class and our layers
884 BaseXBar::regStats();
885 for (auto l: reqLayers)
886 l->regStats();
887 for (auto l: respLayers)
888 l->regStats();
889 for (auto l: snoopLayers)
890 l->regStats();
891
892 snoops
893 .name(name() + ".snoops")
894 .desc("Total snoops (count)")
895 ;
896
897 snoopFanout
898 .init(0, snoopPorts.size(), 1)
899 .name(name() + ".snoop_fanout")
900 .desc("Request fanout histogram")
901 ;
902 }
903
904 CoherentXBar *
905 CoherentXBarParams::create()
906 {
907 return new CoherentXBar(this);
908 }