2 * Copyright (c) 2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Definition of a bus object.
39 #include "base/misc.hh"
40 #include "base/trace.hh"
42 #include "params/Bus.hh"
45 Bus::getPort(const std::string
&if_name
, int idx
)
47 if (if_name
== "default") {
48 if (defaultPort
== NULL
) {
49 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
51 cachedBlockSizeValid
= false;
54 fatal("Default port already set\n");
57 if (if_name
== "functional") {
60 funcPort
= new BusPort(csprintf("%s-p%d-func", name(), id
), this, id
);
62 interfaces
[id
] = funcPort
;
67 // if_name ignored? forced to be empty?
69 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
70 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
72 cachedBlockSizeValid
= false;
77 Bus::deletePortRefs(Port
*p
)
80 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
82 panic("Couldn't convert Port* to BusPort*\n");
83 // If this is our one functional port
86 interfaces
.erase(bp
->getId());
90 /** Get the ranges of anyone other buses that we are connected to. */
94 m5::hash_map
<short,BusPort
*>::iterator intIter
;
96 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
97 intIter
->second
->sendStatusChange(Port::RangeChange
);
100 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
103 void Bus::BusFreeEvent::process()
108 const char * Bus::BusFreeEvent::description()
110 return "bus became available";
113 void Bus::occupyBus(PacketPtr pkt
)
115 //Bring tickNextIdle up to the present tick
116 //There is some potential ambiguity where a cycle starts, which might make
117 //a difference when devices are acting right around a cycle boundary. Using
118 //a < allows things which happen exactly on a cycle boundary to take up
119 //only the following cycle. Anything that happens later will have to "wait"
120 //for the end of that cycle, and then start using the bus after that.
121 if (tickNextIdle
< curTick
) {
122 tickNextIdle
= curTick
;
123 if (tickNextIdle
% clock
!= 0)
124 tickNextIdle
= curTick
- (curTick
% clock
) + clock
;
127 // The packet will be sent. Figure out how long it occupies the bus, and
128 // how much of that time is for the first "word", aka bus width.
130 // Requests need one cycle to send an address
131 if (pkt
->isRequest())
133 else if (pkt
->isResponse() || pkt
->hasData()) {
134 // If a packet has data, it needs ceil(size/width) cycles to send it
135 // We're using the "adding instead of dividing" trick again here
136 if (pkt
->hasData()) {
137 int dataSize
= pkt
->getSize();
138 numCycles
+= dataSize
/width
;
139 if (dataSize
% width
)
142 // If the packet didn't have data, it must have been a response.
143 // Those use the bus for one cycle to send their data.
148 // The first word will be delivered after the current tick, the delivery
149 // of the address if any, and one bus cycle to deliver the data
152 pkt
->isRequest() ? clock
: 0 +
155 //Advance it numCycles bus cycles.
156 //XXX Should this use the repeated addition trick as well?
157 tickNextIdle
+= (numCycles
* clock
);
158 if (!busIdle
.scheduled()) {
159 busIdle
.schedule(tickNextIdle
);
161 busIdle
.reschedule(tickNextIdle
);
163 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
164 curTick
, tickNextIdle
);
166 // The bus will become idle once the current packet is delivered.
167 pkt
->finishTime
= tickNextIdle
;
170 /** Function called by the port when the bus is receiving a Timing
173 Bus::recvTiming(PacketPtr pkt
)
176 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
177 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
180 if (pkt
->getSrc() == defaultId
)
181 pktPort
= defaultPort
;
182 else pktPort
= interfaces
[pkt
->getSrc()];
184 // If the bus is busy, or other devices are in line ahead of the current
185 // one, put this device on the retry list.
186 if (tickNextIdle
> curTick
||
187 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
188 addToRetryList(pktPort
);
189 DPRINTF(Bus
, "recvTiming: Bus is busy, returning false\n");
193 short dest
= pkt
->getDest();
195 // Make sure to clear the snoop commit flag so it doesn't think an
196 // access has been handled twice.
197 if (dest
== Packet::Broadcast
) {
198 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
199 pkt
->flags
&= ~SNOOP_COMMIT
;
200 if (timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()])) {
203 pkt
->flags
|= SNOOP_COMMIT
;
204 success
= timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
207 if (pkt
->flags
& SATISFIED
) {
208 //Cache-Cache transfer occuring
210 retryList
.front()->onRetryList(false);
211 retryList
.pop_front();
215 DPRINTF(Bus
, "recvTiming: Packet sucessfully sent\n");
219 //Snoop didn't succeed
220 DPRINTF(Bus
, "Adding1 a retry to RETRY list %d\n",
222 addToRetryList(pktPort
);
226 assert(dest
>= 0 && dest
< maxId
);
227 assert(dest
!= pkt
->getSrc()); // catch infinite loops
228 port
= interfaces
[dest
];
234 if (port
->sendTiming(pkt
)) {
235 // Packet was successfully sent. Return true.
236 // Also take care of retries
238 DPRINTF(Bus
, "Remove retry from list %d\n",
239 retryList
.front()->getId());
240 retryList
.front()->onRetryList(false);
241 retryList
.pop_front();
247 // Packet not successfully sent. Leave or put it on the retry list.
248 DPRINTF(Bus
, "Adding2 a retry to RETRY list %d\n",
250 addToRetryList(pktPort
);
254 //Forwarding up from responder, just return true;
255 DPRINTF(Bus
, "recvTiming: can we be here?\n");
261 Bus::recvRetry(int id
)
263 // If there's anything waiting, and the bus isn't busy...
264 if (retryList
.size() && curTick
>= tickNextIdle
) {
265 //retryingPort = retryList.front();
267 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
268 retryList
.front()->sendRetry();
269 // If inRetry is still true, sendTiming wasn't called
272 retryList
.front()->onRetryList(false);
273 retryList
.pop_front();
276 //Bring tickNextIdle up to the present
277 while (tickNextIdle
< curTick
)
278 tickNextIdle
+= clock
;
280 //Burn a cycle for the missed grant.
281 tickNextIdle
+= clock
;
283 busIdle
.reschedule(tickNextIdle
, true);
286 //If we weren't able to drain before, we might be able to now.
287 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
288 drainEvent
->process();
289 // Clear the drain event once we're done with it.
295 Bus::findPort(Addr addr
, int id
)
297 /* An interval tree would be a better way to do this. --ali. */
300 PortIter i
= portMap
.find(RangeSize(addr
,1));
301 if (i
!= portMap
.end())
304 // Check if this matches the default range
306 for (AddrRangeIter iter
= defaultRange
.begin();
307 iter
!= defaultRange
.end(); iter
++) {
309 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
315 panic("Unable to find destination for addr (user set default "
316 "responder): %#llx", addr
);
318 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
319 "default port", addr
);
326 // we shouldn't be sending this back to where it came from
327 // do the snoop access and then we should terminate
328 // the cyclical call.
332 return interfaces
[dest_id
];
336 Bus::atomicSnoop(PacketPtr pkt
, Port
*responder
)
338 Tick response_time
= 0;
340 for (SnoopIter s_iter
= snoopPorts
.begin();
341 s_iter
!= snoopPorts
.end();
343 BusPort
*p
= *s_iter
;
344 if (p
!= responder
&& p
->getId() != pkt
->getSrc()) {
345 Tick response
= p
->sendAtomic(pkt
);
347 assert(!response_time
); //Multiple responders
348 response_time
= response
;
353 return response_time
;
357 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
359 // The packet may be changed by another bus on snoops, restore the
361 int src_id
= pkt
->getSrc();
363 for (SnoopIter s_iter
= snoopPorts
.begin();
364 s_iter
!= snoopPorts
.end();
366 BusPort
*p
= *s_iter
;
367 if (p
!= responder
&& p
->getId() != src_id
) {
368 p
->sendFunctional(pkt
);
370 if (pkt
->result
== Packet::Success
) {
378 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
380 for (SnoopIter s_iter
= snoopPorts
.begin();
381 s_iter
!= snoopPorts
.end();
383 BusPort
*p
= *s_iter
;
384 if (p
!= responder
&& p
->getId() != pkt
->getSrc()) {
385 bool success
= p
->sendTiming(pkt
);
395 /** Function called by the port when the bus is receiving a Atomic
398 Bus::recvAtomic(PacketPtr pkt
)
400 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
401 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
402 assert(pkt
->getDest() == Packet::Broadcast
);
403 pkt
->flags
|= SNOOP_COMMIT
;
405 // Assume one bus cycle in order to get through. This may have
406 // some clock skew issues yet again...
407 pkt
->finishTime
= curTick
+ clock
;
409 Port
*port
= findPort(pkt
->getAddr(), pkt
->getSrc());
410 Tick snoopTime
= atomicSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
413 return snoopTime
; //Snoop satisfies it
415 return port
->sendAtomic(pkt
);
420 /** Function called by the port when the bus is receiving a Functional
423 Bus::recvFunctional(PacketPtr pkt
)
425 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
426 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
427 assert(pkt
->getDest() == Packet::Broadcast
);
428 pkt
->flags
|= SNOOP_COMMIT
;
430 Port
* port
= findPort(pkt
->getAddr(), pkt
->getSrc());
431 functionalSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
433 // If the snooping found what we were looking for, we're done.
434 if (pkt
->result
!= Packet::Success
&& port
) {
435 port
->sendFunctional(pkt
);
439 /** Function called by the port when the bus is receiving a status change.*/
441 Bus::recvStatusChange(Port::Status status
, int id
)
443 AddrRangeList ranges
;
447 assert(status
== Port::RangeChange
&&
448 "The other statuses need to be implemented.");
450 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
452 if (id
== defaultId
) {
453 defaultRange
.clear();
454 // Only try to update these ranges if the user set a default responder.
456 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
457 assert(snoops
== false);
458 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
459 defaultRange
.push_back(*iter
);
460 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
461 iter
->start
, iter
->end
);
466 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
467 BusPort
*port
= interfaces
[id
];
469 // Clean out any previously existent ids
470 for (PortIter portIter
= portMap
.begin();
471 portIter
!= portMap
.end(); ) {
472 if (portIter
->second
== id
)
473 portMap
.erase(portIter
++);
478 for (SnoopIter s_iter
= snoopPorts
.begin();
479 s_iter
!= snoopPorts
.end(); ) {
480 if ((*s_iter
)->getId() == id
)
481 s_iter
= snoopPorts
.erase(s_iter
);
486 port
->getPeerAddressRanges(ranges
, snoops
);
489 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
490 snoopPorts
.push_back(port
);
493 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
494 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
495 iter
->start
, iter
->end
, id
);
496 if (portMap
.insert(*iter
, id
) == portMap
.end())
497 panic("Two devices with same range\n");
501 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
503 // tell all our peers that our address range has changed.
504 // Don't tell the device that caused this change, it already knows
505 m5::hash_map
<short,BusPort
*>::iterator intIter
;
507 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
508 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
509 intIter
->second
->sendStatusChange(Port::RangeChange
);
511 if (id
!= defaultId
&& defaultPort
)
512 defaultPort
->sendStatusChange(Port::RangeChange
);
516 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
521 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
523 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
524 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
525 resp
.push_back(*dflt_iter
);
526 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
529 for (PortIter portIter
= portMap
.begin();
530 portIter
!= portMap
.end(); portIter
++) {
532 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
533 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
534 if ((portIter
->first
.start
< dflt_iter
->start
&&
535 portIter
->first
.end
>= dflt_iter
->start
) ||
536 (portIter
->first
.start
< dflt_iter
->end
&&
537 portIter
->first
.end
>= dflt_iter
->end
))
538 fatal("Devices can not set ranges that itersect the default set\
539 but are not a subset of the default set.\n");
540 if (portIter
->first
.start
>= dflt_iter
->start
&&
541 portIter
->first
.end
<= dflt_iter
->end
) {
543 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
544 portIter
->first
.start
, portIter
->first
.end
);
547 if (portIter
->second
!= id
&& !subset
) {
548 resp
.push_back(portIter
->first
);
549 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
550 portIter
->first
.start
, portIter
->first
.end
);
554 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
556 if ((*s_iter
)->getId() != id
) {
564 Bus::findBlockSize(int id
)
566 if (cachedBlockSizeValid
)
567 return cachedBlockSize
;
571 for (PortIter portIter
= portMap
.begin();
572 portIter
!= portMap
.end(); portIter
++) {
573 int tmp_bs
= interfaces
[portIter
->second
]->peerBlockSize();
577 for (SnoopIter s_iter
= snoopPorts
.begin();
578 s_iter
!= snoopPorts
.end(); s_iter
++) {
579 int tmp_bs
= (*s_iter
)->peerBlockSize();
584 max_bs
= defaultBlockSize
;
587 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
588 cachedBlockSize
= max_bs
;
589 cachedBlockSizeValid
= true;
595 Bus::drain(Event
* de
)
597 //We should check that we're not "doing" anything, and that noone is
598 //waiting. We might be idle but have someone waiting if the device we
599 //contacted for a retry didn't actually retry.
600 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
611 if (tickNextIdle
< curTick
)
612 tickNextIdle
= (curTick
/ clock
) * clock
+ clock
;
618 return new Bus(name
, bus_id
, clock
, width
, responder_set
, block_size
);