61b84d82e4502ceebdfc8fece17d36d11d23e9d2
[gem5.git] / src / mem / bus.cc
1 /*
2 * Copyright (c) 2011-2012 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 bus object.
48 */
49
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/Bus.hh"
53 #include "debug/BusAddrRanges.hh"
54 #include "mem/bus.hh"
55
56 Bus::Bus(const BusParams *p)
57 : MemObject(p), clock(p->clock),
58 headerCycles(p->header_cycles), width(p->width), tickNextIdle(0),
59 drainEvent(NULL), busIdleEvent(this), inRetry(false),
60 defaultPortId(Port::INVALID_PORT_ID),
61 useDefaultRange(p->use_default_range),
62 defaultBlockSize(p->block_size),
63 cachedBlockSize(0), cachedBlockSizeValid(false)
64 {
65 //width, clock period, and header cycles must be positive
66 if (width <= 0)
67 fatal("Bus width must be positive\n");
68 if (clock <= 0)
69 fatal("Bus clock period must be positive\n");
70 if (headerCycles <= 0)
71 fatal("Number of header cycles must be positive\n");
72
73 // create the ports based on the size of the master and slave
74 // vector ports, and the presence of the default port, the ports
75 // are enumerated starting from zero
76 for (int i = 0; i < p->port_master_connection_count; ++i) {
77 std::string portName = csprintf("%s-p%d", name(), i);
78 MasterPort* bp = new BusMasterPort(portName, this, i);
79 masterPorts.push_back(bp);
80 }
81
82 // see if we have a default slave device connected and if so add
83 // our corresponding master port
84 if (p->port_default_connection_count) {
85 defaultPortId = masterPorts.size();
86 std::string portName = csprintf("%s-default", name());
87 MasterPort* bp = new BusMasterPort(portName, this, defaultPortId);
88 masterPorts.push_back(bp);
89 }
90
91 // create the slave ports, once again starting at zero
92 for (int i = 0; i < p->port_slave_connection_count; ++i) {
93 std::string portName = csprintf("%s-p%d", name(), i);
94 SlavePort* bp = new BusSlavePort(portName, this, i);
95 slavePorts.push_back(bp);
96 }
97
98 clearPortCache();
99 }
100
101 MasterPort &
102 Bus::getMasterPort(const std::string &if_name, int idx)
103 {
104 if (if_name == "master" && idx < masterPorts.size()) {
105 // the master port index translates directly to the vector position
106 return *masterPorts[idx];
107 } else if (if_name == "default") {
108 return *masterPorts[defaultPortId];
109 } else {
110 return MemObject::getMasterPort(if_name, idx);
111 }
112 }
113
114 SlavePort &
115 Bus::getSlavePort(const std::string &if_name, int idx)
116 {
117 if (if_name == "slave" && idx < slavePorts.size()) {
118 // the slave port index translates directly to the vector position
119 return *slavePorts[idx];
120 } else {
121 return MemObject::getSlavePort(if_name, idx);
122 }
123 }
124
125 void
126 Bus::init()
127 {
128 // iterate over our slave ports and determine which of our
129 // neighbouring master ports are snooping and add them as snoopers
130 for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
131 ++p) {
132 if ((*p)->getMasterPort().isSnooping()) {
133 DPRINTF(BusAddrRanges, "Adding snooping neighbour %s\n",
134 (*p)->getMasterPort().name());
135 snoopPorts.push_back(*p);
136 }
137 }
138 }
139
140 Tick
141 Bus::calcPacketTiming(PacketPtr pkt)
142 {
143 // determine the current time rounded to the closest following
144 // clock edge
145 Tick now = curTick();
146 if (now % clock != 0) {
147 now = ((now / clock) + 1) * clock;
148 }
149
150 Tick headerTime = now + headerCycles * clock;
151
152 // The packet will be sent. Figure out how long it occupies the bus, and
153 // how much of that time is for the first "word", aka bus width.
154 int numCycles = 0;
155 if (pkt->hasData()) {
156 // If a packet has data, it needs ceil(size/width) cycles to send it
157 int dataSize = pkt->getSize();
158 numCycles += dataSize/width;
159 if (dataSize % width)
160 numCycles++;
161 }
162
163 // The first word will be delivered after the current tick, the delivery
164 // of the address if any, and one bus cycle to deliver the data
165 pkt->firstWordTime = headerTime + clock;
166
167 pkt->finishTime = headerTime + numCycles * clock;
168
169 return headerTime;
170 }
171
172 void Bus::occupyBus(Tick until)
173 {
174 if (until == 0) {
175 // shortcut for express snoop packets
176 return;
177 }
178
179 tickNextIdle = until;
180 reschedule(busIdleEvent, tickNextIdle, true);
181
182 DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
183 curTick(), tickNextIdle);
184 }
185
186 bool
187 Bus::isOccupied(PacketPtr pkt, Port* port)
188 {
189 // first we see if the next idle tick is in the future, next the
190 // bus is considered occupied if there are ports on the retry list
191 // and we are not in a retry with the current port
192 if (tickNextIdle > curTick() ||
193 (!retryList.empty() && !(inRetry && port == retryList.front()))) {
194 addToRetryList(port);
195 return true;
196 }
197 return false;
198 }
199
200 bool
201 Bus::recvTimingReq(PacketPtr pkt)
202 {
203 // determine the source port based on the id
204 SlavePort *src_port = slavePorts[pkt->getSrc()];
205
206 // test if the bus should be considered occupied for the current
207 // packet, and exclude express snoops from the check
208 if (!pkt->isExpressSnoop() && isOccupied(pkt, src_port)) {
209 DPRINTF(Bus, "recvTimingReq: src %s %s 0x%x BUSY\n",
210 src_port->name(), pkt->cmdString(), pkt->getAddr());
211 return false;
212 }
213
214 DPRINTF(Bus, "recvTimingReq: src %s %s 0x%x\n",
215 src_port->name(), pkt->cmdString(), pkt->getAddr());
216
217 Tick headerFinishTime = pkt->isExpressSnoop() ? 0 : calcPacketTiming(pkt);
218 Tick packetFinishTime = pkt->isExpressSnoop() ? 0 : pkt->finishTime;
219
220 // the packet is a memory-mapped request and should be
221 // broadcasted to our snoopers but the source
222 forwardTiming(pkt, pkt->getSrc());
223
224 // remember if we add an outstanding req so we can undo it if
225 // necessary, if the packet needs a response, we should add it
226 // as outstanding and express snoops never fail so there is
227 // not need to worry about them
228 bool add_outstanding = !pkt->isExpressSnoop() && pkt->needsResponse();
229
230 // keep track that we have an outstanding request packet
231 // matching this request, this is used by the coherency
232 // mechanism in determining what to do with snoop responses
233 // (in recvTimingSnoop)
234 if (add_outstanding) {
235 // we should never have an exsiting request outstanding
236 assert(outstandingReq.find(pkt->req) == outstandingReq.end());
237 outstandingReq.insert(pkt->req);
238 }
239
240 // since it is a normal request, determine the destination
241 // based on the address and attempt to send the packet
242 bool success = masterPorts[findPort(pkt->getAddr())]->sendTimingReq(pkt);
243
244 if (!success) {
245 // inhibited packets should never be forced to retry
246 assert(!pkt->memInhibitAsserted());
247
248 // if it was added as outstanding and the send failed, then
249 // erase it again
250 if (add_outstanding)
251 outstandingReq.erase(pkt->req);
252
253 DPRINTF(Bus, "recvTimingReq: src %s %s 0x%x RETRY\n",
254 src_port->name(), pkt->cmdString(), pkt->getAddr());
255
256 addToRetryList(src_port);
257 occupyBus(headerFinishTime);
258
259 return false;
260 }
261
262 succeededTiming(packetFinishTime);
263
264 return true;
265 }
266
267 bool
268 Bus::recvTimingResp(PacketPtr pkt)
269 {
270 // determine the source port based on the id
271 MasterPort *src_port = masterPorts[pkt->getSrc()];
272
273 // test if the bus should be considered occupied for the current
274 // packet
275 if (isOccupied(pkt, src_port)) {
276 DPRINTF(Bus, "recvTimingResp: src %s %s 0x%x BUSY\n",
277 src_port->name(), pkt->cmdString(), pkt->getAddr());
278 return false;
279 }
280
281 DPRINTF(Bus, "recvTimingResp: src %s %s 0x%x\n",
282 src_port->name(), pkt->cmdString(), pkt->getAddr());
283
284 calcPacketTiming(pkt);
285 Tick packetFinishTime = pkt->finishTime;
286
287 // the packet is a normal response to a request that we should
288 // have seen passing through the bus
289 assert(outstandingReq.find(pkt->req) != outstandingReq.end());
290
291 // remove it as outstanding
292 outstandingReq.erase(pkt->req);
293
294 // send the packet to the destination through one of our slave
295 // ports, as determined by the destination field
296 bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTimingResp(pkt);
297
298 // currently it is illegal to block responses... can lead to
299 // deadlock
300 assert(success);
301
302 succeededTiming(packetFinishTime);
303
304 return true;
305 }
306
307 void
308 Bus::recvTimingSnoopReq(PacketPtr pkt)
309 {
310 DPRINTF(Bus, "recvTimingSnoopReq: src %s %s 0x%x\n",
311 masterPorts[pkt->getSrc()]->name(), pkt->cmdString(),
312 pkt->getAddr());
313
314 // we should only see express snoops from caches
315 assert(pkt->isExpressSnoop());
316
317 // forward to all snoopers
318 forwardTiming(pkt, Port::INVALID_PORT_ID);
319
320 // a snoop request came from a connected slave device (one of
321 // our master ports), and if it is not coming from the slave
322 // device responsible for the address range something is
323 // wrong, hence there is nothing further to do as the packet
324 // would be going back to where it came from
325 assert(pkt->getSrc() == findPort(pkt->getAddr()));
326
327 // this is an express snoop and is never forced to retry
328 assert(!inRetry);
329 }
330
331 bool
332 Bus::recvTimingSnoopResp(PacketPtr pkt)
333 {
334 // determine the source port based on the id
335 SlavePort* src_port = slavePorts[pkt->getSrc()];
336
337 if (isOccupied(pkt, src_port)) {
338 DPRINTF(Bus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
339 src_port->name(), pkt->cmdString(), pkt->getAddr());
340 return false;
341 }
342
343 DPRINTF(Bus, "recvTimingSnoop: src %s %s 0x%x\n",
344 src_port->name(), pkt->cmdString(), pkt->getAddr());
345
346 // get the destination from the packet
347 Packet::NodeID dest = pkt->getDest();
348
349 // responses are never express snoops
350 assert(!pkt->isExpressSnoop());
351
352 calcPacketTiming(pkt);
353 Tick packetFinishTime = pkt->finishTime;
354
355 // determine if the response is from a snoop request we
356 // created as the result of a normal request (in which case it
357 // should be in the outstandingReq), or if we merely forwarded
358 // someone else's snoop request
359 if (outstandingReq.find(pkt->req) == outstandingReq.end()) {
360 // this is a snoop response to a snoop request we
361 // forwarded, e.g. coming from the L1 and going to the L2
362 // this should be forwarded as a snoop response
363 bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoopResp(pkt);
364 assert(success);
365 } else {
366 // we got a snoop response on one of our slave ports,
367 // i.e. from a coherent master connected to the bus, and
368 // since we created the snoop request as part of
369 // recvTiming, this should now be a normal response again
370 outstandingReq.erase(pkt->req);
371
372 // this is a snoop response from a coherent master, with a
373 // destination field set on its way through the bus as
374 // request, hence it should never go back to where the
375 // snoop response came from, but instead to where the
376 // original request came from
377 assert(pkt->getSrc() != dest);
378
379 // as a normal response, it should go back to a master
380 // through one of our slave ports
381 bool success M5_VAR_USED = slavePorts[dest]->sendTimingResp(pkt);
382
383 // currently it is illegal to block responses... can lead
384 // to deadlock
385 assert(success);
386 }
387
388 succeededTiming(packetFinishTime);
389
390 return true;
391 }
392
393
394 void
395 Bus::succeededTiming(Tick busy_time)
396 {
397 // occupy the bus accordingly
398 occupyBus(busy_time);
399
400 // if a retrying port succeeded, also take it off the retry list
401 if (inRetry) {
402 DPRINTF(Bus, "Remove retry from list %s\n",
403 retryList.front()->name());
404 retryList.pop_front();
405 inRetry = false;
406 }
407 }
408
409 void
410 Bus::forwardTiming(PacketPtr pkt, int exclude_slave_port_id)
411 {
412 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
413 SlavePort *p = *s;
414 // we could have gotten this request from a snooping master
415 // (corresponding to our own slave port that is also in
416 // snoopPorts) and should not send it back to where it came
417 // from
418 if (exclude_slave_port_id == Port::INVALID_PORT_ID ||
419 p->getId() != exclude_slave_port_id) {
420 // cache is not allowed to refuse snoop
421 p->sendTimingSnoopReq(pkt);
422 }
423 }
424 }
425
426 void
427 Bus::releaseBus()
428 {
429 // releasing the bus means we should now be idle
430 assert(curTick() >= tickNextIdle);
431
432 // bus is now idle, so if someone is waiting we can retry
433 if (!retryList.empty()) {
434 // note that we block (return false on recvTiming) both
435 // because the bus is busy and because the destination is
436 // busy, and in the latter case the bus may be released before
437 // we see a retry from the destination
438 retryWaiting();
439 }
440
441 //If we weren't able to drain before, we might be able to now.
442 if (drainEvent && retryList.empty() && curTick() >= tickNextIdle) {
443 drainEvent->process();
444 // Clear the drain event once we're done with it.
445 drainEvent = NULL;
446 }
447 }
448
449 void
450 Bus::retryWaiting()
451 {
452 // this should never be called with an empty retry list
453 assert(!retryList.empty());
454
455 // send a retry to the port at the head of the retry list
456 inRetry = true;
457
458 // note that we might have blocked on the receiving port being
459 // busy (rather than the bus itself) and now call retry before the
460 // destination called retry on the bus
461 retryList.front()->sendRetry();
462
463 // If inRetry is still true, sendTiming wasn't called in zero time
464 // (e.g. the cache does this)
465 if (inRetry) {
466 retryList.pop_front();
467 inRetry = false;
468
469 //Bring tickNextIdle up to the present
470 while (tickNextIdle < curTick())
471 tickNextIdle += clock;
472
473 //Burn a cycle for the missed grant.
474 tickNextIdle += clock;
475
476 reschedule(busIdleEvent, tickNextIdle, true);
477 }
478 }
479
480 void
481 Bus::recvRetry(Port::PortId id)
482 {
483 // we got a retry from a peer that we tried to send something to
484 // and failed, but we sent it on the account of someone else, and
485 // that source port should be on our retry list, however if the
486 // bus is released before this happens and the retry (from the bus
487 // point of view) is successful then this no longer holds and we
488 // could in fact have an empty retry list
489 if (retryList.empty())
490 return;
491
492 // if the bus isn't busy
493 if (curTick() >= tickNextIdle) {
494 // note that we do not care who told us to retry at the moment, we
495 // merely let the first one on the retry list go
496 retryWaiting();
497 }
498 }
499
500 int
501 Bus::findPort(Addr addr)
502 {
503 /* An interval tree would be a better way to do this. --ali. */
504 int dest_id;
505
506 dest_id = checkPortCache(addr);
507 if (dest_id != Port::INVALID_PORT_ID)
508 return dest_id;
509
510 // Check normal port ranges
511 PortIter i = portMap.find(RangeSize(addr,1));
512 if (i != portMap.end()) {
513 dest_id = i->second;
514 updatePortCache(dest_id, i->first.start, i->first.end);
515 return dest_id;
516 }
517
518 // Check if this matches the default range
519 if (useDefaultRange) {
520 AddrRangeIter a_end = defaultRange.end();
521 for (AddrRangeIter i = defaultRange.begin(); i != a_end; i++) {
522 if (*i == addr) {
523 DPRINTF(Bus, " found addr %#llx on default\n", addr);
524 return defaultPortId;
525 }
526 }
527 } else if (defaultPortId != Port::INVALID_PORT_ID) {
528 DPRINTF(Bus, "Unable to find destination for addr %#llx, "
529 "will use default port\n", addr);
530 return defaultPortId;
531 }
532
533 // we should use the range for the default port and it did not
534 // match, or the default port is not set
535 fatal("Unable to find destination for addr %#llx on bus %s\n", addr,
536 name());
537 }
538
539 Tick
540 Bus::recvAtomic(PacketPtr pkt)
541 {
542 DPRINTF(Bus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
543 slavePorts[pkt->getSrc()]->name(), pkt->getAddr(),
544 pkt->cmdString());
545
546 // forward to all snoopers but the source
547 std::pair<MemCmd, Tick> snoop_result = forwardAtomic(pkt, pkt->getSrc());
548 MemCmd snoop_response_cmd = snoop_result.first;
549 Tick snoop_response_latency = snoop_result.second;
550
551 // even if we had a snoop response, we must continue and also
552 // perform the actual request at the destination
553 int dest_id = findPort(pkt->getAddr());
554
555 // forward the request to the appropriate destination
556 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
557
558 // if we got a response from a snooper, restore it here
559 if (snoop_response_cmd != MemCmd::InvalidCmd) {
560 // no one else should have responded
561 assert(!pkt->isResponse());
562 pkt->cmd = snoop_response_cmd;
563 response_latency = snoop_response_latency;
564 }
565
566 pkt->finishTime = curTick() + response_latency;
567 return response_latency;
568 }
569
570 Tick
571 Bus::recvAtomicSnoop(PacketPtr pkt)
572 {
573 DPRINTF(Bus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
574 masterPorts[pkt->getSrc()]->name(), pkt->getAddr(),
575 pkt->cmdString());
576
577 // forward to all snoopers
578 std::pair<MemCmd, Tick> snoop_result =
579 forwardAtomic(pkt, Port::INVALID_PORT_ID);
580 MemCmd snoop_response_cmd = snoop_result.first;
581 Tick snoop_response_latency = snoop_result.second;
582
583 if (snoop_response_cmd != MemCmd::InvalidCmd)
584 pkt->cmd = snoop_response_cmd;
585
586 pkt->finishTime = curTick() + snoop_response_latency;
587 return snoop_response_latency;
588 }
589
590 std::pair<MemCmd, Tick>
591 Bus::forwardAtomic(PacketPtr pkt, int exclude_slave_port_id)
592 {
593 // the packet may be changed on snoops, record the original source
594 // and command to enable us to restore it between snoops so that
595 // additional snoops can take place properly
596 Packet::NodeID orig_src_id = pkt->getSrc();
597 MemCmd orig_cmd = pkt->cmd;
598 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
599 Tick snoop_response_latency = 0;
600
601 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
602 SlavePort *p = *s;
603 // we could have gotten this request from a snooping master
604 // (corresponding to our own slave port that is also in
605 // snoopPorts) and should not send it back to where it came
606 // from
607 if (exclude_slave_port_id == Port::INVALID_PORT_ID ||
608 p->getId() != exclude_slave_port_id) {
609 Tick latency = p->sendAtomicSnoop(pkt);
610 // in contrast to a functional access, we have to keep on
611 // going as all snoopers must be updated even if we get a
612 // response
613 if (pkt->isResponse()) {
614 // response from snoop agent
615 assert(pkt->cmd != orig_cmd);
616 assert(pkt->memInhibitAsserted());
617 // should only happen once
618 assert(snoop_response_cmd == MemCmd::InvalidCmd);
619 // save response state
620 snoop_response_cmd = pkt->cmd;
621 snoop_response_latency = latency;
622 // restore original packet state for remaining snoopers
623 pkt->cmd = orig_cmd;
624 pkt->setSrc(orig_src_id);
625 pkt->clearDest();
626 }
627 }
628 }
629
630 // the packet is restored as part of the loop and any potential
631 // snoop response is part of the returned pair
632 return std::make_pair(snoop_response_cmd, snoop_response_latency);
633 }
634
635 void
636 Bus::recvFunctional(PacketPtr pkt)
637 {
638 if (!pkt->isPrint()) {
639 // don't do DPRINTFs on PrintReq as it clutters up the output
640 DPRINTF(Bus,
641 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
642 slavePorts[pkt->getSrc()]->name(), pkt->getAddr(),
643 pkt->cmdString());
644 }
645
646 // forward to all snoopers but the source
647 forwardFunctional(pkt, pkt->getSrc());
648
649 // there is no need to continue if the snooping has found what we
650 // were looking for and the packet is already a response
651 if (!pkt->isResponse()) {
652 int dest_id = findPort(pkt->getAddr());
653
654 masterPorts[dest_id]->sendFunctional(pkt);
655 }
656 }
657
658 void
659 Bus::recvFunctionalSnoop(PacketPtr pkt)
660 {
661 if (!pkt->isPrint()) {
662 // don't do DPRINTFs on PrintReq as it clutters up the output
663 DPRINTF(Bus,
664 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
665 masterPorts[pkt->getSrc()]->name(), pkt->getAddr(),
666 pkt->cmdString());
667 }
668
669 // forward to all snoopers
670 forwardFunctional(pkt, Port::INVALID_PORT_ID);
671 }
672
673 void
674 Bus::forwardFunctional(PacketPtr pkt, int exclude_slave_port_id)
675 {
676 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
677 SlavePort *p = *s;
678 // we could have gotten this request from a snooping master
679 // (corresponding to our own slave port that is also in
680 // snoopPorts) and should not send it back to where it came
681 // from
682 if (exclude_slave_port_id == Port::INVALID_PORT_ID ||
683 p->getId() != exclude_slave_port_id)
684 p->sendFunctionalSnoop(pkt);
685
686 // if we get a response we are done
687 if (pkt->isResponse()) {
688 break;
689 }
690 }
691 }
692
693 /** Function called by the port when the bus is receiving a range change.*/
694 void
695 Bus::recvRangeChange(Port::PortId id)
696 {
697 AddrRangeList ranges;
698 AddrRangeIter iter;
699
700 if (inRecvRangeChange.count(id))
701 return;
702 inRecvRangeChange.insert(id);
703
704 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
705
706 clearPortCache();
707 if (id == defaultPortId) {
708 defaultRange.clear();
709 // Only try to update these ranges if the user set a default responder.
710 if (useDefaultRange) {
711 AddrRangeList ranges =
712 masterPorts[id]->getSlavePort().getAddrRanges();
713 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
714 defaultRange.push_back(*iter);
715 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
716 iter->start, iter->end);
717 }
718 }
719 } else {
720
721 assert(id < masterPorts.size() && id >= 0);
722 MasterPort *port = masterPorts[id];
723
724 // Clean out any previously existent ids
725 for (PortIter portIter = portMap.begin();
726 portIter != portMap.end(); ) {
727 if (portIter->second == id)
728 portMap.erase(portIter++);
729 else
730 portIter++;
731 }
732
733 ranges = port->getSlavePort().getAddrRanges();
734
735 for (iter = ranges.begin(); iter != ranges.end(); iter++) {
736 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
737 iter->start, iter->end, id);
738 if (portMap.insert(*iter, id) == portMap.end()) {
739 int conflict_id = portMap.find(*iter)->second;
740 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
741 name(), masterPorts[id]->getSlavePort().name(),
742 masterPorts[conflict_id]->getSlavePort().name());
743 }
744 }
745 }
746 DPRINTF(BusAddrRanges, "port list has %d entries\n", portMap.size());
747
748 // tell all our neighbouring master ports that our address range
749 // has changed
750 for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
751 ++p)
752 (*p)->sendRangeChange();
753
754 inRecvRangeChange.erase(id);
755 }
756
757 AddrRangeList
758 Bus::getAddrRanges(Port::PortId id)
759 {
760 AddrRangeList ranges;
761
762 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
763
764 for (AddrRangeIter dflt_iter = defaultRange.begin();
765 dflt_iter != defaultRange.end(); dflt_iter++) {
766 ranges.push_back(*dflt_iter);
767 DPRINTF(BusAddrRanges, " -- Dflt: %#llx : %#llx\n",dflt_iter->start,
768 dflt_iter->end);
769 }
770 for (PortIter portIter = portMap.begin();
771 portIter != portMap.end(); portIter++) {
772 bool subset = false;
773 for (AddrRangeIter dflt_iter = defaultRange.begin();
774 dflt_iter != defaultRange.end(); dflt_iter++) {
775 if ((portIter->first.start < dflt_iter->start &&
776 portIter->first.end >= dflt_iter->start) ||
777 (portIter->first.start < dflt_iter->end &&
778 portIter->first.end >= dflt_iter->end))
779 fatal("Devices can not set ranges that itersect the default set\
780 but are not a subset of the default set.\n");
781 if (portIter->first.start >= dflt_iter->start &&
782 portIter->first.end <= dflt_iter->end) {
783 subset = true;
784 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
785 portIter->first.start, portIter->first.end);
786 }
787 }
788 if (portIter->second != id && !subset) {
789 ranges.push_back(portIter->first);
790 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
791 portIter->first.start, portIter->first.end);
792 }
793 }
794
795 return ranges;
796 }
797
798 bool
799 Bus::isSnooping(Port::PortId id) const
800 {
801 // in essence, answer the question if there are snooping ports
802 return !snoopPorts.empty();
803 }
804
805 unsigned
806 Bus::findBlockSize(Port::PortId id)
807 {
808 if (cachedBlockSizeValid)
809 return cachedBlockSize;
810
811 unsigned max_bs = 0;
812
813 PortIter p_end = portMap.end();
814 for (PortIter p_iter = portMap.begin(); p_iter != p_end; p_iter++) {
815 unsigned tmp_bs = masterPorts[p_iter->second]->peerBlockSize();
816 if (tmp_bs > max_bs)
817 max_bs = tmp_bs;
818 }
819
820 for (SlavePortConstIter s = snoopPorts.begin(); s != snoopPorts.end();
821 ++s) {
822 unsigned tmp_bs = (*s)->peerBlockSize();
823 if (tmp_bs > max_bs)
824 max_bs = tmp_bs;
825 }
826 if (max_bs == 0)
827 max_bs = defaultBlockSize;
828
829 if (max_bs != 64)
830 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
831 cachedBlockSize = max_bs;
832 cachedBlockSizeValid = true;
833 return max_bs;
834 }
835
836
837 unsigned int
838 Bus::drain(Event * de)
839 {
840 //We should check that we're not "doing" anything, and that noone is
841 //waiting. We might be idle but have someone waiting if the device we
842 //contacted for a retry didn't actually retry.
843 if (!retryList.empty() || (curTick() < tickNextIdle &&
844 busIdleEvent.scheduled())) {
845 drainEvent = de;
846 return 1;
847 }
848 return 0;
849 }
850
851 void
852 Bus::startup()
853 {
854 if (tickNextIdle < curTick())
855 tickNextIdle = (curTick() / clock) * clock + clock;
856 }
857
858 Bus *
859 BusParams::create()
860 {
861 return new Bus(this);
862 }