6bf4baff03a13d666c42ae8822006a58263ff0de
[gem5.git] / src / mem / xbar.cc
1 /*
2 * Copyright (c) 2011-2015, 2018-2020 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
41 /**
42 * @file
43 * Definition of a crossbar object.
44 */
45
46 #include "mem/xbar.hh"
47
48 #include "base/logging.hh"
49 #include "base/trace.hh"
50 #include "debug/AddrRanges.hh"
51 #include "debug/Drain.hh"
52 #include "debug/XBar.hh"
53
54 BaseXBar::BaseXBar(const BaseXBarParams &p)
55 : ClockedObject(p),
56 frontendLatency(p.frontend_latency),
57 forwardLatency(p.forward_latency),
58 responseLatency(p.response_latency),
59 headerLatency(p.header_latency),
60 width(p.width),
61 gotAddrRanges(p.port_default_connection_count +
62 p.port_mem_side_ports_connection_count, false),
63 gotAllAddrRanges(false), defaultPortID(InvalidPortID),
64 useDefaultRange(p.use_default_range),
65
66 ADD_STAT(transDist, "Transaction distribution"),
67 ADD_STAT(pktCount, "Packet count per connected requestor and responder"),
68 ADD_STAT(pktSize, "Cumulative packet size per connected requestor and "
69 "responder (bytes)")
70 {
71 }
72
73 BaseXBar::~BaseXBar()
74 {
75 for (auto port: memSidePorts)
76 delete port;
77
78 for (auto port: cpuSidePorts)
79 delete port;
80 }
81
82 Port &
83 BaseXBar::getPort(const std::string &if_name, PortID idx)
84 {
85 if (if_name == "mem_side_ports" && idx < memSidePorts.size()) {
86 // the memory-side ports index translates directly to the vector
87 // position
88 return *memSidePorts[idx];
89 } else if (if_name == "default") {
90 return *memSidePorts[defaultPortID];
91 } else if (if_name == "cpu_side_ports" && idx < cpuSidePorts.size()) {
92 // the CPU-side ports index translates directly to the vector position
93 return *cpuSidePorts[idx];
94 } else {
95 return ClockedObject::getPort(if_name, idx);
96 }
97 }
98
99 void
100 BaseXBar::calcPacketTiming(PacketPtr pkt, Tick header_delay)
101 {
102 // the crossbar will be called at a time that is not necessarily
103 // coinciding with its own clock, so start by determining how long
104 // until the next clock edge (could be zero)
105 Tick offset = clockEdge() - curTick();
106
107 // the header delay depends on the path through the crossbar, and
108 // we therefore rely on the caller to provide the actual
109 // value
110 pkt->headerDelay += offset + header_delay;
111
112 // note that we add the header delay to the existing value, and
113 // align it to the crossbar clock
114
115 // do a quick sanity check to ensure the timings are not being
116 // ignored, note that this specific value may cause problems for
117 // slower interconnects
118 panic_if(pkt->headerDelay > SimClock::Int::us,
119 "Encountered header delay exceeding 1 us\n");
120
121 if (pkt->hasData()) {
122 // the payloadDelay takes into account the relative time to
123 // deliver the payload of the packet, after the header delay,
124 // we take the maximum since the payload delay could already
125 // be longer than what this parcitular crossbar enforces.
126 pkt->payloadDelay = std::max<Tick>(pkt->payloadDelay,
127 divCeil(pkt->getSize(), width) *
128 clockPeriod());
129 }
130
131 // the payload delay is not paying for the clock offset as that is
132 // already done using the header delay, and the payload delay is
133 // also used to determine how long the crossbar layer is busy and
134 // thus regulates throughput
135 }
136
137 template <typename SrcType, typename DstType>
138 BaseXBar::Layer<SrcType, DstType>::Layer(DstType& _port, BaseXBar& _xbar,
139 const std::string& _name) :
140 Stats::Group(&_xbar, _name.c_str()),
141 port(_port), xbar(_xbar), _name(xbar.name() + "." + _name), state(IDLE),
142 waitingForPeer(NULL), releaseEvent([this]{ releaseLayer(); }, name()),
143 ADD_STAT(occupancy, "Layer occupancy (ticks)"),
144 ADD_STAT(utilization, "Layer utilization")
145 {
146 occupancy
147 .flags(Stats::nozero);
148
149 utilization
150 .precision(1)
151 .flags(Stats::nozero);
152
153 utilization = occupancy / simTicks;
154 }
155
156 template <typename SrcType, typename DstType>
157 void BaseXBar::Layer<SrcType, DstType>::occupyLayer(Tick until)
158 {
159 // ensure the state is busy at this point, as the layer should
160 // transition from idle as soon as it has decided to forward the
161 // packet to prevent any follow-on calls to sendTiming seeing an
162 // unoccupied layer
163 assert(state == BUSY);
164
165 // until should never be 0 as express snoops never occupy the layer
166 assert(until != 0);
167 xbar.schedule(releaseEvent, until);
168
169 // account for the occupied ticks
170 occupancy += until - curTick();
171
172 DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n",
173 curTick(), until);
174 }
175
176 template <typename SrcType, typename DstType>
177 bool
178 BaseXBar::Layer<SrcType, DstType>::tryTiming(SrcType* src_port)
179 {
180 // if we are in the retry state, we will not see anything but the
181 // retrying port (or in the case of the snoop ports the snoop
182 // response port that mirrors the actual CPU-side port) as we leave
183 // this state again in zero time if the peer does not immediately
184 // call the layer when receiving the retry
185
186 // first we see if the layer is busy, next we check if the
187 // destination port is already engaged in a transaction waiting
188 // for a retry from the peer
189 if (state == BUSY || waitingForPeer != NULL) {
190 // the port should not be waiting already
191 assert(std::find(waitingForLayer.begin(), waitingForLayer.end(),
192 src_port) == waitingForLayer.end());
193
194 // put the port at the end of the retry list waiting for the
195 // layer to be freed up (and in the case of a busy peer, for
196 // that transaction to go through, and then the layer to free
197 // up)
198 waitingForLayer.push_back(src_port);
199 return false;
200 }
201
202 state = BUSY;
203
204 return true;
205 }
206
207 template <typename SrcType, typename DstType>
208 void
209 BaseXBar::Layer<SrcType, DstType>::succeededTiming(Tick busy_time)
210 {
211 // we should have gone from idle or retry to busy in the tryTiming
212 // test
213 assert(state == BUSY);
214
215 // occupy the layer accordingly
216 occupyLayer(busy_time);
217 }
218
219 template <typename SrcType, typename DstType>
220 void
221 BaseXBar::Layer<SrcType, DstType>::failedTiming(SrcType* src_port,
222 Tick busy_time)
223 {
224 // ensure no one got in between and tried to send something to
225 // this port
226 assert(waitingForPeer == NULL);
227
228 // if the source port is the current retrying one or not, we have
229 // failed in forwarding and should track that we are now waiting
230 // for the peer to send a retry
231 waitingForPeer = src_port;
232
233 // we should have gone from idle or retry to busy in the tryTiming
234 // test
235 assert(state == BUSY);
236
237 // occupy the bus accordingly
238 occupyLayer(busy_time);
239 }
240
241 template <typename SrcType, typename DstType>
242 void
243 BaseXBar::Layer<SrcType, DstType>::releaseLayer()
244 {
245 // releasing the bus means we should now be idle
246 assert(state == BUSY);
247 assert(!releaseEvent.scheduled());
248
249 // update the state
250 state = IDLE;
251
252 // bus layer is now idle, so if someone is waiting we can retry
253 if (!waitingForLayer.empty()) {
254 // there is no point in sending a retry if someone is still
255 // waiting for the peer
256 if (waitingForPeer == NULL)
257 retryWaiting();
258 } else if (waitingForPeer == NULL && drainState() == DrainState::Draining) {
259 DPRINTF(Drain, "Crossbar done draining, signaling drain manager\n");
260 //If we weren't able to drain before, do it now.
261 signalDrainDone();
262 }
263 }
264
265 template <typename SrcType, typename DstType>
266 void
267 BaseXBar::Layer<SrcType, DstType>::retryWaiting()
268 {
269 // this should never be called with no one waiting
270 assert(!waitingForLayer.empty());
271
272 // we always go to retrying from idle
273 assert(state == IDLE);
274
275 // update the state
276 state = RETRY;
277
278 // set the retrying port to the front of the retry list and pop it
279 // off the list
280 SrcType* retryingPort = waitingForLayer.front();
281 waitingForLayer.pop_front();
282
283 // tell the port to retry, which in some cases ends up calling the
284 // layer again
285 sendRetry(retryingPort);
286
287 // If the layer is still in the retry state, sendTiming wasn't
288 // called in zero time (e.g. the cache does this when a writeback
289 // is squashed)
290 if (state == RETRY) {
291 // update the state to busy and reset the retrying port, we
292 // have done our bit and sent the retry
293 state = BUSY;
294
295 // occupy the crossbar layer until the next clock edge
296 occupyLayer(xbar.clockEdge());
297 }
298 }
299
300 template <typename SrcType, typename DstType>
301 void
302 BaseXBar::Layer<SrcType, DstType>::recvRetry()
303 {
304 // we should never get a retry without having failed to forward
305 // something to this port
306 assert(waitingForPeer != NULL);
307
308 // add the port where the failed packet originated to the front of
309 // the waiting ports for the layer, this allows us to call retry
310 // on the port immediately if the crossbar layer is idle
311 waitingForLayer.push_front(waitingForPeer);
312
313 // we are no longer waiting for the peer
314 waitingForPeer = NULL;
315
316 // if the layer is idle, retry this port straight away, if we
317 // are busy, then simply let the port wait for its turn
318 if (state == IDLE) {
319 retryWaiting();
320 } else {
321 assert(state == BUSY);
322 }
323 }
324
325 PortID
326 BaseXBar::findPort(AddrRange addr_range)
327 {
328 // we should never see any address lookups before we've got the
329 // ranges of all connected CPU-side-port modules
330 assert(gotAllAddrRanges);
331
332 // Check the address map interval tree
333 auto i = portMap.contains(addr_range);
334 if (i != portMap.end()) {
335 return i->second;
336 }
337
338 // Check if this matches the default range
339 if (useDefaultRange) {
340 if (addr_range.isSubset(defaultRange)) {
341 DPRINTF(AddrRanges, " found addr %s on default\n",
342 addr_range.to_string());
343 return defaultPortID;
344 }
345 } else if (defaultPortID != InvalidPortID) {
346 DPRINTF(AddrRanges, "Unable to find destination for %s, "
347 "will use default port\n", addr_range.to_string());
348 return defaultPortID;
349 }
350
351 // we should use the range for the default port and it did not
352 // match, or the default port is not set
353 fatal("Unable to find destination for %s on %s\n", addr_range.to_string(),
354 name());
355 }
356
357 /** Function called by the port when the crossbar is receiving a range change.*/
358 void
359 BaseXBar::recvRangeChange(PortID mem_side_port_id)
360 {
361 DPRINTF(AddrRanges, "Received range change from cpu_side_ports %s\n",
362 memSidePorts[mem_side_port_id]->getPeer());
363
364 // remember that we got a range from this memory-side port and thus the
365 // connected CPU-side-port module
366 gotAddrRanges[mem_side_port_id] = true;
367
368 // update the global flag
369 if (!gotAllAddrRanges) {
370 // take a logical AND of all the ports and see if we got
371 // ranges from everyone
372 gotAllAddrRanges = true;
373 std::vector<bool>::const_iterator r = gotAddrRanges.begin();
374 while (gotAllAddrRanges && r != gotAddrRanges.end()) {
375 gotAllAddrRanges &= *r++;
376 }
377 if (gotAllAddrRanges)
378 DPRINTF(AddrRanges, "Got address ranges from all responders\n");
379 }
380
381 // note that we could get the range from the default port at any
382 // point in time, and we cannot assume that the default range is
383 // set before the other ones are, so we do additional checks once
384 // all ranges are provided
385 if (mem_side_port_id == defaultPortID) {
386 // only update if we are indeed checking ranges for the
387 // default port since the port might not have a valid range
388 // otherwise
389 if (useDefaultRange) {
390 AddrRangeList ranges = memSidePorts[mem_side_port_id]->
391 getAddrRanges();
392
393 if (ranges.size() != 1)
394 fatal("Crossbar %s may only have a single default range",
395 name());
396
397 defaultRange = ranges.front();
398 }
399 } else {
400 // the ports are allowed to update their address ranges
401 // dynamically, so remove any existing entries
402 if (gotAddrRanges[mem_side_port_id]) {
403 for (auto p = portMap.begin(); p != portMap.end(); ) {
404 if (p->second == mem_side_port_id)
405 // erasing invalidates the iterator, so advance it
406 // before the deletion takes place
407 portMap.erase(p++);
408 else
409 p++;
410 }
411 }
412
413 AddrRangeList ranges = memSidePorts[mem_side_port_id]->
414 getAddrRanges();
415
416 for (const auto& r: ranges) {
417 DPRINTF(AddrRanges, "Adding range %s for id %d\n",
418 r.to_string(), mem_side_port_id);
419 if (portMap.insert(r, mem_side_port_id) == portMap.end()) {
420 PortID conflict_id = portMap.intersects(r)->second;
421 fatal("%s has two ports responding within range "
422 "%s:\n\t%s\n\t%s\n",
423 name(),
424 r.to_string(),
425 memSidePorts[mem_side_port_id]->getPeer(),
426 memSidePorts[conflict_id]->getPeer());
427 }
428 }
429 }
430
431 // if we have received ranges from all our neighbouring CPU-side-port
432 // modules, go ahead and tell our connected memory-side-port modules in
433 // turn, this effectively assumes a tree structure of the system
434 if (gotAllAddrRanges) {
435 DPRINTF(AddrRanges, "Aggregating address ranges\n");
436 xbarRanges.clear();
437
438 // start out with the default range
439 if (useDefaultRange) {
440 if (!gotAddrRanges[defaultPortID])
441 fatal("Crossbar %s uses default range, but none provided",
442 name());
443
444 xbarRanges.push_back(defaultRange);
445 DPRINTF(AddrRanges, "-- Adding default %s\n",
446 defaultRange.to_string());
447 }
448
449 // merge all interleaved ranges and add any range that is not
450 // a subset of the default range
451 std::vector<AddrRange> intlv_ranges;
452 for (const auto& r: portMap) {
453 // if the range is interleaved then save it for now
454 if (r.first.interleaved()) {
455 // if we already got interleaved ranges that are not
456 // part of the same range, then first do a merge
457 // before we add the new one
458 if (!intlv_ranges.empty() &&
459 !intlv_ranges.back().mergesWith(r.first)) {
460 DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
461 intlv_ranges.size());
462 AddrRange merged_range(intlv_ranges);
463 // next decide if we keep the merged range or not
464 if (!(useDefaultRange &&
465 merged_range.isSubset(defaultRange))) {
466 xbarRanges.push_back(merged_range);
467 DPRINTF(AddrRanges, "-- Adding merged range %s\n",
468 merged_range.to_string());
469 }
470 intlv_ranges.clear();
471 }
472 intlv_ranges.push_back(r.first);
473 } else {
474 // keep the current range if not a subset of the default
475 if (!(useDefaultRange &&
476 r.first.isSubset(defaultRange))) {
477 xbarRanges.push_back(r.first);
478 DPRINTF(AddrRanges, "-- Adding range %s\n",
479 r.first.to_string());
480 }
481 }
482 }
483
484 // if there is still interleaved ranges waiting to be merged,
485 // go ahead and do it
486 if (!intlv_ranges.empty()) {
487 DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
488 intlv_ranges.size());
489 AddrRange merged_range(intlv_ranges);
490 if (!(useDefaultRange && merged_range.isSubset(defaultRange))) {
491 xbarRanges.push_back(merged_range);
492 DPRINTF(AddrRanges, "-- Adding merged range %s\n",
493 merged_range.to_string());
494 }
495 }
496
497 // also check that no range partially intersects with the
498 // default range, this has to be done after all ranges are set
499 // as there are no guarantees for when the default range is
500 // update with respect to the other ones
501 if (useDefaultRange) {
502 for (const auto& r: xbarRanges) {
503 // see if the new range is partially
504 // overlapping the default range
505 if (r.intersects(defaultRange) &&
506 !r.isSubset(defaultRange))
507 fatal("Range %s intersects the " \
508 "default range of %s but is not a " \
509 "subset\n", r.to_string(), name());
510 }
511 }
512
513 // tell all our neighbouring memory-side ports that our address
514 // ranges have changed
515 for (const auto& port: cpuSidePorts)
516 port->sendRangeChange();
517 }
518 }
519
520 AddrRangeList
521 BaseXBar::getAddrRanges() const
522 {
523 // we should never be asked without first having sent a range
524 // change, and the latter is only done once we have all the ranges
525 // of the connected devices
526 assert(gotAllAddrRanges);
527
528 // at the moment, this never happens, as there are no cycles in
529 // the range queries and no devices on the memory side of a crossbar
530 // (CPU, cache, bridge etc) actually care about the ranges of the
531 // ports they are connected to
532
533 DPRINTF(AddrRanges, "Received address range request\n");
534
535 return xbarRanges;
536 }
537
538 void
539 BaseXBar::regStats()
540 {
541 ClockedObject::regStats();
542
543 using namespace Stats;
544
545 transDist
546 .init(MemCmd::NUM_MEM_CMDS)
547 .flags(nozero);
548
549 // get the string representation of the commands
550 for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) {
551 MemCmd cmd(i);
552 const std::string &cstr = cmd.toString();
553 transDist.subname(i, cstr);
554 }
555
556 pktCount
557 .init(cpuSidePorts.size(), memSidePorts.size())
558 .flags(total | nozero | nonan);
559
560 pktSize
561 .init(cpuSidePorts.size(), memSidePorts.size())
562 .flags(total | nozero | nonan);
563
564 // both the packet count and total size are two-dimensional
565 // vectors, indexed by CPU-side port id and memory-side port id, thus the
566 // neighbouring memory-side ports and CPU-side ports, they do not
567 // differentiate what came from the memory-side ports and was forwarded to
568 // the CPU-side ports (requests and snoop responses) and what came from
569 // the CPU-side ports and was forwarded to the memory-side ports (responses
570 // and snoop requests)
571 for (int i = 0; i < cpuSidePorts.size(); i++) {
572 pktCount.subname(i, cpuSidePorts[i]->getPeer().name());
573 pktSize.subname(i, cpuSidePorts[i]->getPeer().name());
574 for (int j = 0; j < memSidePorts.size(); j++) {
575 pktCount.ysubname(j, memSidePorts[j]->getPeer().name());
576 pktSize.ysubname(j, memSidePorts[j]->getPeer().name());
577 }
578 }
579 }
580
581 template <typename SrcType, typename DstType>
582 DrainState
583 BaseXBar::Layer<SrcType, DstType>::drain()
584 {
585 //We should check that we're not "doing" anything, and that noone is
586 //waiting. We might be idle but have someone waiting if the device we
587 //contacted for a retry didn't actually retry.
588 if (state != IDLE) {
589 DPRINTF(Drain, "Crossbar not drained\n");
590 return DrainState::Draining;
591 } else {
592 return DrainState::Drained;
593 }
594 }
595
596 /**
597 * Crossbar layer template instantiations. Could be removed with _impl.hh
598 * file, but since there are only two given options (RequestPort and
599 * ResponsePort) it seems a bit excessive at this point.
600 */
601 template class BaseXBar::Layer<ResponsePort, RequestPort>;
602 template class BaseXBar::Layer<RequestPort, ResponsePort>;