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
57CoherentXBar::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 if (snoopFilter)
100 snoopFilter->setSlavePorts(slavePorts);
101
99 clearPortCache();
100}
101
102CoherentXBar::~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
114void
115CoherentXBar::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
140bool
141CoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
142{
143 // @todo temporary hack to deal with memory corruption issue until
144 // 4-phase transactions are complete
145 for (int x = 0; x < pendingDelete.size(); x++)
146 delete pendingDelete[x];
147 pendingDelete.clear();
148
149 // determine the source port based on the id
150 SlavePort *src_port = slavePorts[slave_port_id];
151
152 // remember if the packet is an express snoop
153 bool is_express_snoop = pkt->isExpressSnoop();
154 bool is_inhibited = pkt->memInhibitAsserted();
155 // for normal requests, going downstream, the express snoop flag
156 // and the inhibited flag should always be the same
157 assert(is_express_snoop == is_inhibited);
158
159 // determine the destination based on the address
160 PortID master_port_id = findPort(pkt->getAddr());
161
162 // test if the crossbar should be considered occupied for the current
163 // port, and exclude express snoops from the check
164 if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
165 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x BUSY\n",
166 src_port->name(), pkt->cmdString(), pkt->getAddr());
167 return false;
168 }
169
170 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s expr %d 0x%x\n",
171 src_port->name(), pkt->cmdString(), is_express_snoop,
172 pkt->getAddr());
173
174 // store size and command as they might be modified when
175 // forwarding the packet
176 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
177 unsigned int pkt_cmd = pkt->cmdToIndex();
178
179 // store the old header delay so we can restore it if needed
180 Tick old_header_delay = pkt->headerDelay;
181
182 // a request sees the frontend and forward latency
183 Tick xbar_delay = (frontendLatency + forwardLatency) * clockPeriod();
184
185 // set the packet header and payload delay
186 calcPacketTiming(pkt, xbar_delay);
187
188 // determine how long to be crossbar layer is busy
189 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
190
191 if (!system->bypassCaches()) {
192 assert(pkt->snoopDelay == 0);
193
194 // the packet is a memory-mapped request and should be
195 // broadcasted to our snoopers but the source
196 if (snoopFilter) {
197 // check with the snoop filter where to forward this packet
198 auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
199 // the time required by a packet to be delivered through
200 // the xbar has to be charged also with to lookup latency
201 // of the snoop filter
202 pkt->headerDelay += sf_res.second * clockPeriod();
203 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x"\
204 " SF size: %i lat: %i\n", src_port->name(),
205 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
206 sf_res.second);
207 forwardTiming(pkt, slave_port_id, sf_res.first);
208 } else {
209 forwardTiming(pkt, slave_port_id);
210 }
211
212 // add the snoop delay to our header delay, and then reset it
213 pkt->headerDelay += pkt->snoopDelay;
214 pkt->snoopDelay = 0;
215 }
216
217 // forwardTiming snooped into peer caches of the sender, and if
218 // this is a clean evict, but the packet is found in a cache, do
219 // not forward it
220 if (pkt->cmd == MemCmd::CleanEvict && pkt->isBlockCached()) {
221 DPRINTF(CoherentXBar, "recvTimingReq: Clean evict 0x%x still cached, "
222 "not forwarding\n", pkt->getAddr());
223
224 // update the layer state and schedule an idle event
225 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
226 pendingDelete.push_back(pkt);
227 return true;
228 }
229
230 // remember if the packet will generate a snoop response
231 const bool expect_snoop_resp = !is_inhibited && pkt->memInhibitAsserted();
232 const bool expect_response = pkt->needsResponse() &&
233 !pkt->memInhibitAsserted();
234
235 // since it is a normal request, attempt to send the packet
236 bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
237
238 if (snoopFilter && !system->bypassCaches()) {
239 // Let the snoop filter know about the success of the send operation
240 snoopFilter->finishRequest(!success, pkt);
241 }
242
243 // check if we were successful in sending the packet onwards
244 if (!success) {
245 // express snoops and inhibited packets should never be forced
246 // to retry
247 assert(!is_express_snoop);
248 assert(!pkt->memInhibitAsserted());
249
250 // restore the header delay
251 pkt->headerDelay = old_header_delay;
252
253 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x RETRY\n",
254 src_port->name(), pkt->cmdString(), pkt->getAddr());
255
256 // update the layer state and schedule an idle event
257 reqLayers[master_port_id]->failedTiming(src_port,
258 clockEdge(Cycles(1)));
259 } else {
260 // express snoops currently bypass the crossbar state entirely
261 if (!is_express_snoop) {
262 // if this particular request will generate a snoop
263 // response
264 if (expect_snoop_resp) {
265 // we should never have an exsiting request outstanding
266 assert(outstandingSnoop.find(pkt->req) ==
267 outstandingSnoop.end());
268 outstandingSnoop.insert(pkt->req);
269
270 // basic sanity check on the outstanding snoops
271 panic_if(outstandingSnoop.size() > 512,
272 "Outstanding snoop requests exceeded 512\n");
273 }
274
275 // remember where to route the normal response to
276 if (expect_response || expect_snoop_resp) {
277 assert(routeTo.find(pkt->req) == routeTo.end());
278 routeTo[pkt->req] = slave_port_id;
279
280 panic_if(routeTo.size() > 512,
281 "Routing table exceeds 512 packets\n");
282 }
283
284 // update the layer state and schedule an idle event
285 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
286 }
287
288 // stats updates only consider packets that were successfully sent
289 pktCount[slave_port_id][master_port_id]++;
290 pktSize[slave_port_id][master_port_id] += pkt_size;
291 transDist[pkt_cmd]++;
292
293 if (is_express_snoop)
294 snoops++;
295 }
296
297 return success;
298}
299
300bool
301CoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
302{
303 // determine the source port based on the id
304 MasterPort *src_port = masterPorts[master_port_id];
305
306 // determine the destination
307 const auto route_lookup = routeTo.find(pkt->req);
308 assert(route_lookup != routeTo.end());
309 const PortID slave_port_id = route_lookup->second;
310 assert(slave_port_id != InvalidPortID);
311 assert(slave_port_id < respLayers.size());
312
313 // test if the crossbar should be considered occupied for the
314 // current port
315 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
316 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x BUSY\n",
317 src_port->name(), pkt->cmdString(), pkt->getAddr());
318 return false;
319 }
320
321 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x\n",
322 src_port->name(), pkt->cmdString(), pkt->getAddr());
323
324 // store size and command as they might be modified when
325 // forwarding the packet
326 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
327 unsigned int pkt_cmd = pkt->cmdToIndex();
328
329 // a response sees the response latency
330 Tick xbar_delay = responseLatency * clockPeriod();
331
332 // set the packet header and payload delay
333 calcPacketTiming(pkt, xbar_delay);
334
335 // determine how long to be crossbar layer is busy
336 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
337
338 if (snoopFilter && !system->bypassCaches()) {
339 // let the snoop filter inspect the response and update its state
340 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
341 }
342
343 // send the packet through the destination slave port and pay for
344 // any outstanding header delay
345 Tick latency = pkt->headerDelay;
346 pkt->headerDelay = 0;
347 slavePorts[slave_port_id]->schedTimingResp(pkt, curTick() + latency);
348
349 // remove the request from the routing table
350 routeTo.erase(route_lookup);
351
352 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
353
354 // stats updates
355 pktCount[slave_port_id][master_port_id]++;
356 pktSize[slave_port_id][master_port_id] += pkt_size;
357 transDist[pkt_cmd]++;
358
359 return true;
360}
361
362void
363CoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
364{
365 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x\n",
366 masterPorts[master_port_id]->name(), pkt->cmdString(),
367 pkt->getAddr());
368
369 // update stats here as we know the forwarding will succeed
370 transDist[pkt->cmdToIndex()]++;
371 snoops++;
372
373 // we should only see express snoops from caches
374 assert(pkt->isExpressSnoop());
375
376 // set the packet header and payload delay, for now use forward latency
377 // @todo Assess the choice of latency further
378 calcPacketTiming(pkt, forwardLatency * clockPeriod());
379
380 // remeber if the packet is inhibited so we can see if it changes
381 const bool is_inhibited = pkt->memInhibitAsserted();
382
383 assert(pkt->snoopDelay == 0);
384
385 if (snoopFilter) {
386 // let the Snoop Filter work its magic and guide probing
387 auto sf_res = snoopFilter->lookupSnoop(pkt);
388 // the time required by a packet to be delivered through
389 // the xbar has to be charged also with to lookup latency
390 // of the snoop filter
391 pkt->headerDelay += sf_res.second * clockPeriod();
392 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x"\
393 " SF size: %i lat: %i\n", masterPorts[master_port_id]->name(),
394 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
395 sf_res.second);
396
397 // forward to all snoopers
398 forwardTiming(pkt, InvalidPortID, sf_res.first);
399 } else {
400 forwardTiming(pkt, InvalidPortID);
401 }
402
403 // add the snoop delay to our header delay, and then reset it
404 pkt->headerDelay += pkt->snoopDelay;
405 pkt->snoopDelay = 0;
406
407 // if we can expect a response, remember how to route it
408 if (!is_inhibited && pkt->memInhibitAsserted()) {
409 assert(routeTo.find(pkt->req) == routeTo.end());
410 routeTo[pkt->req] = master_port_id;
411 }
412
413 // a snoop request came from a connected slave device (one of
414 // our master ports), and if it is not coming from the slave
415 // device responsible for the address range something is
416 // wrong, hence there is nothing further to do as the packet
417 // would be going back to where it came from
418 assert(master_port_id == findPort(pkt->getAddr()));
419}
420
421bool
422CoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
423{
424 // determine the source port based on the id
425 SlavePort* src_port = slavePorts[slave_port_id];
426
427 // get the destination
428 const auto route_lookup = routeTo.find(pkt->req);
429 assert(route_lookup != routeTo.end());
430 const PortID dest_port_id = route_lookup->second;
431 assert(dest_port_id != InvalidPortID);
432
433 // determine if the response is from a snoop request we
434 // created as the result of a normal request (in which case it
435 // should be in the outstandingSnoop), or if we merely forwarded
436 // someone else's snoop request
437 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
438 outstandingSnoop.end();
439
440 // test if the crossbar should be considered occupied for the
441 // current port, note that the check is bypassed if the response
442 // is being passed on as a normal response since this is occupying
443 // the response layer rather than the snoop response layer
444 if (forwardAsSnoop) {
445 assert(dest_port_id < snoopLayers.size());
446 if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
447 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
448 src_port->name(), pkt->cmdString(), pkt->getAddr());
449 return false;
450 }
451 } else {
452 // get the master port that mirrors this slave port internally
453 MasterPort* snoop_port = snoopRespPorts[slave_port_id];
454 assert(dest_port_id < respLayers.size());
455 if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
456 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
457 snoop_port->name(), pkt->cmdString(), pkt->getAddr());
458 return false;
459 }
460 }
461
462 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x\n",
463 src_port->name(), pkt->cmdString(), pkt->getAddr());
464
465 // store size and command as they might be modified when
466 // forwarding the packet
467 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
468 unsigned int pkt_cmd = pkt->cmdToIndex();
469
470 // responses are never express snoops
471 assert(!pkt->isExpressSnoop());
472
473 // a snoop response sees the snoop response latency, and if it is
474 // forwarded as a normal response, the response latency
475 Tick xbar_delay =
476 (forwardAsSnoop ? snoopResponseLatency : responseLatency) *
477 clockPeriod();
478
479 // set the packet header and payload delay
480 calcPacketTiming(pkt, xbar_delay);
481
482 // determine how long to be crossbar layer is busy
483 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
484
485 // forward it either as a snoop response or a normal response
486 if (forwardAsSnoop) {
487 // this is a snoop response to a snoop request we forwarded,
488 // e.g. coming from the L1 and going to the L2, and it should
489 // be forwarded as a snoop response
490
491 if (snoopFilter) {
492 // update the probe filter so that it can properly track the line
493 snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
494 *masterPorts[dest_port_id]);
495 }
496
497 bool success M5_VAR_USED =
498 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
499 pktCount[slave_port_id][dest_port_id]++;
500 pktSize[slave_port_id][dest_port_id] += pkt_size;
501 assert(success);
502
503 snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
504 } else {
505 // we got a snoop response on one of our slave ports,
506 // i.e. from a coherent master connected to the crossbar, and
507 // since we created the snoop request as part of recvTiming,
508 // this should now be a normal response again
509 outstandingSnoop.erase(pkt->req);
510
511 // this is a snoop response from a coherent master, hence it
512 // should never go back to where the snoop response came from,
513 // but instead to where the original request came from
514 assert(slave_port_id != dest_port_id);
515
516 if (snoopFilter) {
517 // update the probe filter so that it can properly track the line
518 snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
519 *slavePorts[dest_port_id]);
520 }
521
522 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x"\
523 " FWD RESP\n", src_port->name(), pkt->cmdString(),
524 pkt->getAddr());
525
526 // as a normal response, it should go back to a master through
527 // one of our slave ports, we also pay for any outstanding
528 // header latency
529 Tick latency = pkt->headerDelay;
530 pkt->headerDelay = 0;
531 slavePorts[dest_port_id]->schedTimingResp(pkt, curTick() + latency);
532
533 respLayers[dest_port_id]->succeededTiming(packetFinishTime);
534 }
535
536 // remove the request from the routing table
537 routeTo.erase(route_lookup);
538
539 // stats updates
540 transDist[pkt_cmd]++;
541 snoops++;
542
543 return true;
544}
545
546
547void
548CoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
549 const std::vector<QueuedSlavePort*>& dests)
550{
551 DPRINTF(CoherentXBar, "%s for %s address %x size %d\n", __func__,
552 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
553
554 // snoops should only happen if the system isn't bypassing caches
555 assert(!system->bypassCaches());
556
557 unsigned fanout = 0;
558
559 for (const auto& p: dests) {
560 // we could have gotten this request from a snooping master
561 // (corresponding to our own slave port that is also in
562 // snoopPorts) and should not send it back to where it came
563 // from
564 if (exclude_slave_port_id == InvalidPortID ||
565 p->getId() != exclude_slave_port_id) {
566 // cache is not allowed to refuse snoop
567 p->sendTimingSnoopReq(pkt);
568 fanout++;
569 }
570 }
571
572 // Stats for fanout of this forward operation
573 snoopFanout.sample(fanout);
574}
575
576void
577CoherentXBar::recvReqRetry(PortID master_port_id)
578{
579 // responses and snoop responses never block on forwarding them,
580 // so the retry will always be coming from a port to which we
581 // tried to forward a request
582 reqLayers[master_port_id]->recvRetry();
583}
584
585Tick
586CoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
587{
588 DPRINTF(CoherentXBar, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
589 slavePorts[slave_port_id]->name(), pkt->getAddr(),
590 pkt->cmdString());
591
592 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
593 unsigned int pkt_cmd = pkt->cmdToIndex();
594
595 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
596 Tick snoop_response_latency = 0;
597
598 if (!system->bypassCaches()) {
599 // forward to all snoopers but the source
600 std::pair<MemCmd, Tick> snoop_result;
601 if (snoopFilter) {
602 // check with the snoop filter where to forward this packet
603 auto sf_res =
604 snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
605 snoop_response_latency += sf_res.second * clockPeriod();
606 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x"\
607 " SF size: %i lat: %i\n", __func__,
608 slavePorts[slave_port_id]->name(), pkt->cmdString(),
609 pkt->getAddr(), sf_res.first.size(), sf_res.second);
610
611 // let the snoop filter know about the success of the send
612 // operation, and do it even before sending it onwards to
613 // avoid situations where atomic upward snoops sneak in
614 // between and change the filter state
615 snoopFilter->finishRequest(false, pkt);
616
617 snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
618 sf_res.first);
619 } else {
620 snoop_result = forwardAtomic(pkt, slave_port_id);
621 }
622 snoop_response_cmd = snoop_result.first;
623 snoop_response_latency += snoop_result.second;
624 }
625
626 // forwardAtomic snooped into peer caches of the sender, and if
627 // this is a clean evict, but the packet is found in a cache, do
628 // not forward it
629 if (pkt->cmd == MemCmd::CleanEvict && pkt->isBlockCached()) {
630 DPRINTF(CoherentXBar, "recvAtomic: Clean evict 0x%x still cached, "
631 "not forwarding\n", pkt->getAddr());
632 return 0;
633 }
634
635 // even if we had a snoop response, we must continue and also
636 // perform the actual request at the destination
637 PortID master_port_id = findPort(pkt->getAddr());
638
639 // stats updates for the request
640 pktCount[slave_port_id][master_port_id]++;
641 pktSize[slave_port_id][master_port_id] += pkt_size;
642 transDist[pkt_cmd]++;
643
644 // forward the request to the appropriate destination
645 Tick response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
646
647 // if lower levels have replied, tell the snoop filter
648 if (!system->bypassCaches() && snoopFilter && pkt->isResponse()) {
649 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
650 }
651
652 // if we got a response from a snooper, restore it here
653 if (snoop_response_cmd != MemCmd::InvalidCmd) {
654 // no one else should have responded
655 assert(!pkt->isResponse());
656 pkt->cmd = snoop_response_cmd;
657 response_latency = snoop_response_latency;
658 }
659
660 // add the response data
661 if (pkt->isResponse()) {
662 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
663 pkt_cmd = pkt->cmdToIndex();
664
665 // stats updates
666 pktCount[slave_port_id][master_port_id]++;
667 pktSize[slave_port_id][master_port_id] += pkt_size;
668 transDist[pkt_cmd]++;
669 }
670
671 // @todo: Not setting header time
672 pkt->payloadDelay = response_latency;
673 return response_latency;
674}
675
676Tick
677CoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
678{
679 DPRINTF(CoherentXBar, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
680 masterPorts[master_port_id]->name(), pkt->getAddr(),
681 pkt->cmdString());
682
683 // add the request snoop data
684 snoops++;
685
686 // forward to all snoopers
687 std::pair<MemCmd, Tick> snoop_result;
688 Tick snoop_response_latency = 0;
689 if (snoopFilter) {
690 auto sf_res = snoopFilter->lookupSnoop(pkt);
691 snoop_response_latency += sf_res.second * clockPeriod();
692 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x SF size: %i lat: %i\n",
693 __func__, masterPorts[master_port_id]->name(), pkt->cmdString(),
694 pkt->getAddr(), sf_res.first.size(), sf_res.second);
695 snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
696 sf_res.first);
697 } else {
698 snoop_result = forwardAtomic(pkt, InvalidPortID);
699 }
700 MemCmd snoop_response_cmd = snoop_result.first;
701 snoop_response_latency += snoop_result.second;
702
703 if (snoop_response_cmd != MemCmd::InvalidCmd)
704 pkt->cmd = snoop_response_cmd;
705
706 // add the response snoop data
707 if (pkt->isResponse()) {
708 snoops++;
709 }
710
711 // @todo: Not setting header time
712 pkt->payloadDelay = snoop_response_latency;
713 return snoop_response_latency;
714}
715
716std::pair<MemCmd, Tick>
717CoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
718 PortID source_master_port_id,
719 const std::vector<QueuedSlavePort*>& dests)
720{
721 // the packet may be changed on snoops, record the original
722 // command to enable us to restore it between snoops so that
723 // additional snoops can take place properly
724 MemCmd orig_cmd = pkt->cmd;
725 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
726 Tick snoop_response_latency = 0;
727
728 // snoops should only happen if the system isn't bypassing caches
729 assert(!system->bypassCaches());
730
731 unsigned fanout = 0;
732
733 for (const auto& p: dests) {
734 // we could have gotten this request from a snooping master
735 // (corresponding to our own slave port that is also in
736 // snoopPorts) and should not send it back to where it came
737 // from
738 if (exclude_slave_port_id != InvalidPortID &&
739 p->getId() == exclude_slave_port_id)
740 continue;
741
742 Tick latency = p->sendAtomicSnoop(pkt);
743 fanout++;
744
745 // in contrast to a functional access, we have to keep on
746 // going as all snoopers must be updated even if we get a
747 // response
748 if (!pkt->isResponse())
749 continue;
750
751 // response from snoop agent
752 assert(pkt->cmd != orig_cmd);
753 assert(pkt->memInhibitAsserted());
754 // should only happen once
755 assert(snoop_response_cmd == MemCmd::InvalidCmd);
756 // save response state
757 snoop_response_cmd = pkt->cmd;
758 snoop_response_latency = latency;
759
760 if (snoopFilter) {
761 // Handle responses by the snoopers and differentiate between
762 // responses to requests from above and snoops from below
763 if (source_master_port_id != InvalidPortID) {
764 // Getting a response for a snoop from below
765 assert(exclude_slave_port_id == InvalidPortID);
766 snoopFilter->updateSnoopForward(pkt, *p,
767 *masterPorts[source_master_port_id]);
768 } else {
769 // Getting a response for a request from above
770 assert(source_master_port_id == InvalidPortID);
771 snoopFilter->updateSnoopResponse(pkt, *p,
772 *slavePorts[exclude_slave_port_id]);
773 }
774 }
775 // restore original packet state for remaining snoopers
776 pkt->cmd = orig_cmd;
777 }
778
779 // Stats for fanout
780 snoopFanout.sample(fanout);
781
782 // the packet is restored as part of the loop and any potential
783 // snoop response is part of the returned pair
784 return std::make_pair(snoop_response_cmd, snoop_response_latency);
785}
786
787void
788CoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
789{
790 if (!pkt->isPrint()) {
791 // don't do DPRINTFs on PrintReq as it clutters up the output
792 DPRINTF(CoherentXBar,
793 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
794 slavePorts[slave_port_id]->name(), pkt->getAddr(),
795 pkt->cmdString());
796 }
797
798 if (!system->bypassCaches()) {
799 // forward to all snoopers but the source
800 forwardFunctional(pkt, slave_port_id);
801 }
802
803 // there is no need to continue if the snooping has found what we
804 // were looking for and the packet is already a response
805 if (!pkt->isResponse()) {
806 // since our slave ports are queued ports we need to check them as well
807 for (const auto& p : slavePorts) {
808 // if we find a response that has the data, then the
809 // downstream caches/memories may be out of date, so simply stop
810 // here
811 if (p->checkFunctional(pkt)) {
812 if (pkt->needsResponse())
813 pkt->makeResponse();
814 return;
815 }
816 }
817
818 PortID dest_id = findPort(pkt->getAddr());
819
820 masterPorts[dest_id]->sendFunctional(pkt);
821 }
822}
823
824void
825CoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
826{
827 if (!pkt->isPrint()) {
828 // don't do DPRINTFs on PrintReq as it clutters up the output
829 DPRINTF(CoherentXBar,
830 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
831 masterPorts[master_port_id]->name(), pkt->getAddr(),
832 pkt->cmdString());
833 }
834
835 // forward to all snoopers
836 forwardFunctional(pkt, InvalidPortID);
837}
838
839void
840CoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
841{
842 // snoops should only happen if the system isn't bypassing caches
843 assert(!system->bypassCaches());
844
845 for (const auto& p: snoopPorts) {
846 // we could have gotten this request from a snooping master
847 // (corresponding to our own slave port that is also in
848 // snoopPorts) and should not send it back to where it came
849 // from
850 if (exclude_slave_port_id == InvalidPortID ||
851 p->getId() != exclude_slave_port_id)
852 p->sendFunctionalSnoop(pkt);
853
854 // if we get a response we are done
855 if (pkt->isResponse()) {
856 break;
857 }
858 }
859}
860
861void
862CoherentXBar::regStats()
863{
864 // register the stats of the base class and our layers
865 BaseXBar::regStats();
866 for (auto l: reqLayers)
867 l->regStats();
868 for (auto l: respLayers)
869 l->regStats();
870 for (auto l: snoopLayers)
871 l->regStats();
872
873 snoops
874 .name(name() + ".snoops")
875 .desc("Total snoops (count)")
876 ;
877
878 snoopFanout
879 .init(0, snoopPorts.size(), 1)
880 .name(name() + ".snoop_fanout")
881 .desc("Request fanout histogram")
882 ;
883}
884
885CoherentXBar *
886CoherentXBarParams::create()
887{
888 return new CoherentXBar(this);
889}