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