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"
44 Bus::getPort(const std::string
&if_name
, int idx
)
46 if (if_name
== "default") {
47 if (defaultPort
== NULL
) {
48 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
50 cachedBlockSizeValid
= false;
53 fatal("Default port already set\n");
56 if (if_name
== "functional") {
59 funcPort
= new BusPort(csprintf("%s-p%d-func", name(), id
), this, id
);
61 interfaces
[id
] = funcPort
;
66 // if_name ignored? forced to be empty?
68 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
69 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
71 cachedBlockSizeValid
= false;
76 Bus::deletePortRefs(Port
*p
)
79 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
81 panic("Couldn't convert Port* to BusPort*\n");
82 // If this is our one functional port
85 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() const
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
150 pkt
->firstWordTime
= tickNextIdle
+ (pkt
->isRequest() ? clock
: 0) + clock
;
152 //Advance it numCycles bus cycles.
153 //XXX Should this use the repeated addition trick as well?
154 tickNextIdle
+= (numCycles
* clock
);
155 if (!busIdle
.scheduled()) {
156 busIdle
.schedule(tickNextIdle
);
158 busIdle
.reschedule(tickNextIdle
);
160 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
161 curTick
, tickNextIdle
);
163 // The bus will become idle once the current packet is delivered.
164 pkt
->finishTime
= tickNextIdle
;
167 /** Function called by the port when the bus is receiving a Timing
170 Bus::recvTiming(PacketPtr pkt
)
172 short src
= pkt
->getSrc();
175 if (src
== defaultId
)
176 src_port
= defaultPort
;
178 src_port
= checkBusCache(src
);
179 if (src_port
== NULL
) {
180 src_port
= interfaces
[src
];
181 updateBusCache(src
, src_port
);
185 // If the bus is busy, or other devices are in line ahead of the current
186 // one, put this device on the retry list.
187 if (!pkt
->isExpressSnoop() &&
188 (tickNextIdle
> curTick
||
189 (retryList
.size() && (!inRetry
|| src_port
!= retryList
.front()))))
191 addToRetryList(src_port
);
192 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x BUSY\n",
193 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
197 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x\n",
198 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
200 if (!pkt
->isExpressSnoop()) {
204 short dest
= pkt
->getDest();
208 if (dest
== Packet::Broadcast
) {
209 dest_port_id
= findPort(pkt
->getAddr());
210 dest_port
= (dest_port_id
== defaultId
) ?
211 defaultPort
: interfaces
[dest_port_id
];
212 SnoopIter s_end
= snoopPorts
.end();
213 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
214 BusPort
*p
= *s_iter
;
215 if (p
!= dest_port
&& p
!= src_port
) {
216 // cache is not allowed to refuse snoop
217 bool success M5_VAR_USED
= p
->sendTiming(pkt
);
222 assert(dest
>= 0 && dest
< maxId
);
223 assert(dest
!= src
); // catch infinite loops
225 if (dest_port_id
== defaultId
)
226 dest_port
= defaultPort
;
228 dest_port
= checkBusCache(dest
);
229 if (dest_port
== NULL
) {
230 dest_port
= interfaces
[dest_port_id
];
231 // updateBusCache(dest_port_id, dest_port);
234 dest_port
= (dest_port_id
== defaultId
) ?
235 defaultPort
: interfaces
[dest_port_id
];
238 if (dest_port_id
== src
) {
239 // Must be forwarded snoop up from below...
240 assert(dest
== Packet::Broadcast
);
241 assert(src
!= defaultId
); // catch infinite loops
243 // send to actual target
244 if (!dest_port
->sendTiming(pkt
)) {
245 // Packet not successfully sent. Leave or put it on the retry list.
246 // illegal to block responses... can lead to deadlock
247 assert(!pkt
->isResponse());
248 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x TGT RETRY\n",
249 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
250 addToRetryList(src_port
);
253 // send OK, fall through
256 // Packet was successfully sent.
257 // Also take care of retries
259 DPRINTF(Bus
, "Remove retry from list %d\n", src
);
260 retryList
.front()->onRetryList(false);
261 retryList
.pop_front();
268 Bus::recvRetry(int id
)
270 // If there's anything waiting, and the bus isn't busy...
271 if (retryList
.size() && curTick
>= tickNextIdle
) {
272 //retryingPort = retryList.front();
274 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
275 retryList
.front()->sendRetry();
276 // If inRetry is still true, sendTiming wasn't called
279 retryList
.front()->onRetryList(false);
280 retryList
.pop_front();
283 //Bring tickNextIdle up to the present
284 while (tickNextIdle
< curTick
)
285 tickNextIdle
+= clock
;
287 //Burn a cycle for the missed grant.
288 tickNextIdle
+= clock
;
290 busIdle
.reschedule(tickNextIdle
, true);
293 //If we weren't able to drain before, we might be able to now.
294 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
295 drainEvent
->process();
296 // Clear the drain event once we're done with it.
302 Bus::findPort(Addr addr
)
304 /* An interval tree would be a better way to do this. --ali. */
307 dest_id
= checkPortCache(addr
);
309 PortIter i
= portMap
.find(RangeSize(addr
,1));
310 if (i
!= portMap
.end()) {
312 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
316 // Check if this matches the default range
318 AddrRangeIter a_end
= defaultRange
.end();
319 for (AddrRangeIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
321 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
327 panic("Unable to find destination for addr (user set default "
328 "responder): %#llx", addr
);
330 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
331 "default port", addr
);
341 /** Function called by the port when the bus is receiving a Atomic
344 Bus::recvAtomic(PacketPtr pkt
)
346 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
347 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
348 assert(pkt
->getDest() == Packet::Broadcast
);
349 assert(pkt
->isRequest());
351 // Variables for recording original command and snoop response (if
352 // any)... if a snooper respondes, we will need to restore
353 // original command so that additional snoops can take place
355 MemCmd orig_cmd
= pkt
->cmd
;
356 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
357 Tick snoop_response_latency
= 0;
358 int orig_src
= pkt
->getSrc();
360 int target_port_id
= findPort(pkt
->getAddr());
361 BusPort
*target_port
;
362 if (target_port_id
== defaultId
)
363 target_port
= defaultPort
;
365 target_port
= checkBusCache(target_port_id
);
366 if (target_port
== NULL
) {
367 target_port
= interfaces
[target_port_id
];
368 updateBusCache(target_port_id
, target_port
);
372 SnoopIter s_end
= snoopPorts
.end();
373 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
374 BusPort
*p
= *s_iter
;
375 // same port should not have both target addresses and snooping
376 assert(p
!= target_port
);
377 if (p
->getId() != pkt
->getSrc()) {
378 Tick latency
= p
->sendAtomic(pkt
);
379 if (pkt
->isResponse()) {
380 // response from snoop agent
381 assert(pkt
->cmd
!= orig_cmd
);
382 assert(pkt
->memInhibitAsserted());
383 // should only happen once
384 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
385 // save response state
386 snoop_response_cmd
= pkt
->cmd
;
387 snoop_response_latency
= latency
;
388 // restore original packet state for remaining snoopers
390 pkt
->setSrc(orig_src
);
391 pkt
->setDest(Packet::Broadcast
);
396 Tick response_latency
= 0;
398 // we can get requests sent up from the memory side of the bus for
399 // snooping... don't send them back down!
400 if (target_port_id
!= pkt
->getSrc()) {
401 response_latency
= target_port
->sendAtomic(pkt
);
404 // if we got a response from a snooper, restore it here
405 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
406 // no one else should have responded
407 assert(!pkt
->isResponse());
408 assert(pkt
->cmd
== orig_cmd
);
409 pkt
->cmd
= snoop_response_cmd
;
410 response_latency
= snoop_response_latency
;
413 // why do we have this packet field and the return value both???
414 pkt
->finishTime
= curTick
+ response_latency
;
415 return response_latency
;
418 /** Function called by the port when the bus is receiving a Functional
421 Bus::recvFunctional(PacketPtr pkt
)
423 if (!pkt
->isPrint()) {
424 // don't do DPRINTFs on PrintReq as it clutters up the output
426 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
427 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(),
430 assert(pkt
->getDest() == Packet::Broadcast
);
432 int port_id
= findPort(pkt
->getAddr());
433 Port
*port
= (port_id
== defaultId
) ? defaultPort
: interfaces
[port_id
];
434 // The packet may be changed by another bus on snoops, restore the
436 int src_id
= pkt
->getSrc();
438 assert(pkt
->isRequest()); // hasn't already been satisfied
440 SnoopIter s_end
= snoopPorts
.end();
441 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
442 BusPort
*p
= *s_iter
;
443 if (p
!= port
&& p
->getId() != src_id
) {
444 p
->sendFunctional(pkt
);
446 if (pkt
->isResponse()) {
452 // If the snooping hasn't found what we were looking for, keep going.
453 if (!pkt
->isResponse() && port_id
!= pkt
->getSrc()) {
454 port
->sendFunctional(pkt
);
458 /** Function called by the port when the bus is receiving a status change.*/
460 Bus::recvStatusChange(Port::Status status
, int id
)
462 AddrRangeList ranges
;
466 if (inRecvStatusChange
.count(id
))
468 inRecvStatusChange
.insert(id
);
470 assert(status
== Port::RangeChange
&&
471 "The other statuses need to be implemented.");
473 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
476 if (id
== defaultId
) {
477 defaultRange
.clear();
478 // Only try to update these ranges if the user set a default responder.
480 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
481 assert(snoops
== false);
482 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
483 defaultRange
.push_back(*iter
);
484 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
485 iter
->start
, iter
->end
);
490 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
491 BusPort
*port
= interfaces
[id
];
493 // Clean out any previously existent ids
494 for (PortIter portIter
= portMap
.begin();
495 portIter
!= portMap
.end(); ) {
496 if (portIter
->second
== id
)
497 portMap
.erase(portIter
++);
502 for (SnoopIter s_iter
= snoopPorts
.begin();
503 s_iter
!= snoopPorts
.end(); ) {
504 if ((*s_iter
)->getId() == id
)
505 s_iter
= snoopPorts
.erase(s_iter
);
510 port
->getPeerAddressRanges(ranges
, snoops
);
513 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
514 snoopPorts
.push_back(port
);
517 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
518 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
519 iter
->start
, iter
->end
, id
);
520 if (portMap
.insert(*iter
, id
) == portMap
.end())
521 panic("Two devices with same range\n");
525 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
527 // tell all our peers that our address range has changed.
528 // Don't tell the device that caused this change, it already knows
529 m5::hash_map
<short,BusPort
*>::iterator intIter
;
531 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
532 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
533 intIter
->second
->sendStatusChange(Port::RangeChange
);
535 if (id
!= defaultId
&& defaultPort
)
536 defaultPort
->sendStatusChange(Port::RangeChange
);
537 inRecvStatusChange
.erase(id
);
541 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
546 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
548 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
549 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
550 resp
.push_back(*dflt_iter
);
551 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
554 for (PortIter portIter
= portMap
.begin();
555 portIter
!= portMap
.end(); portIter
++) {
557 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
558 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
559 if ((portIter
->first
.start
< dflt_iter
->start
&&
560 portIter
->first
.end
>= dflt_iter
->start
) ||
561 (portIter
->first
.start
< dflt_iter
->end
&&
562 portIter
->first
.end
>= dflt_iter
->end
))
563 fatal("Devices can not set ranges that itersect the default set\
564 but are not a subset of the default set.\n");
565 if (portIter
->first
.start
>= dflt_iter
->start
&&
566 portIter
->first
.end
<= dflt_iter
->end
) {
568 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
569 portIter
->first
.start
, portIter
->first
.end
);
572 if (portIter
->second
!= id
&& !subset
) {
573 resp
.push_back(portIter
->first
);
574 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
575 portIter
->first
.start
, portIter
->first
.end
);
579 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
581 if ((*s_iter
)->getId() != id
) {
589 Bus::findBlockSize(int id
)
591 if (cachedBlockSizeValid
)
592 return cachedBlockSize
;
596 PortIter p_end
= portMap
.end();
597 for (PortIter p_iter
= portMap
.begin(); p_iter
!= p_end
; p_iter
++) {
598 int tmp_bs
= interfaces
[p_iter
->second
]->peerBlockSize();
602 SnoopIter s_end
= snoopPorts
.end();
603 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
604 int tmp_bs
= (*s_iter
)->peerBlockSize();
609 max_bs
= defaultBlockSize
;
612 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
613 cachedBlockSize
= max_bs
;
614 cachedBlockSizeValid
= true;
620 Bus::drain(Event
* de
)
622 //We should check that we're not "doing" anything, and that noone is
623 //waiting. We might be idle but have someone waiting if the device we
624 //contacted for a retry didn't actually retry.
625 if (retryList
.size() || (curTick
< tickNextIdle
&& busIdle
.scheduled())) {
635 if (tickNextIdle
< curTick
)
636 tickNextIdle
= (curTick
/ clock
) * clock
+ clock
;
642 return new Bus(this);