2 * Copyright (c) 2011-2012 ARM Limited
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.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
47 * Definition of a bus object.
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/BusAddrRanges.hh"
53 #include "debug/CoherentBus.hh"
54 #include "mem/coherent_bus.hh"
55 #include "sim/system.hh"
57 CoherentBus::CoherentBus(const CoherentBusParams
*p
)
58 : BaseBus(p
), reqLayer(*this, ".reqLayer"),
59 respLayer(*this, ".respLayer"),
60 snoopRespLayer(*this, ".snoopRespLayer"),
63 // create the ports based on the size of the master and slave
64 // vector ports, and the presence of the default port, the ports
65 // are enumerated starting from zero
66 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
67 std::string portName
= csprintf("%s.master[%d]", name(), i
);
68 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this, i
);
69 masterPorts
.push_back(bp
);
72 // see if we have a default slave device connected and if so add
73 // our corresponding master port
74 if (p
->port_default_connection_count
) {
75 defaultPortID
= masterPorts
.size();
76 std::string portName
= name() + ".default";
77 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this,
79 masterPorts
.push_back(bp
);
82 // create the slave ports, once again starting at zero
83 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
84 std::string portName
= csprintf("%s.slave[%d]", name(), i
);
85 SlavePort
* bp
= new CoherentBusSlavePort(portName
, *this, i
);
86 slavePorts
.push_back(bp
);
95 // the base class is responsible for determining the block size
98 // iterate over our slave ports and determine which of our
99 // neighbouring master ports are snooping and add them as snoopers
100 for (SlavePortConstIter p
= slavePorts
.begin(); p
!= slavePorts
.end();
102 // check if the connected master port is snooping
103 if ((*p
)->isSnooping()) {
104 DPRINTF(BusAddrRanges
, "Adding snooping master %s\n",
105 (*p
)->getMasterPort().name());
106 snoopPorts
.push_back(*p
);
110 if (snoopPorts
.empty())
111 warn("CoherentBus %s has no snooping ports attached!\n", name());
115 CoherentBus::recvTimingReq(PacketPtr pkt
, PortID slave_port_id
)
117 // determine the source port based on the id
118 SlavePort
*src_port
= slavePorts
[slave_port_id
];
120 // remember if the packet is an express snoop
121 bool is_express_snoop
= pkt
->isExpressSnoop();
123 // test if the bus should be considered occupied for the current
124 // port, and exclude express snoops from the check
125 if (!is_express_snoop
&& !reqLayer
.tryTiming(src_port
)) {
126 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x BUSY\n",
127 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
131 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s expr %d 0x%x\n",
132 src_port
->name(), pkt
->cmdString(), is_express_snoop
,
135 // set the source port for routing of the response
136 pkt
->setSrc(slave_port_id
);
138 calcPacketTiming(pkt
);
139 Tick packetFinishTime
= pkt
->busLastWordDelay
+ curTick();
141 // uncacheable requests need never be snooped
142 if (!pkt
->req
->isUncacheable() && !system
->bypassCaches()) {
143 // the packet is a memory-mapped request and should be
144 // broadcasted to our snoopers but the source
145 forwardTiming(pkt
, slave_port_id
);
148 // remember if we add an outstanding req so we can undo it if
149 // necessary, if the packet needs a response, we should add it
150 // as outstanding and express snoops never fail so there is
151 // not need to worry about them
152 bool add_outstanding
= !is_express_snoop
&& pkt
->needsResponse();
154 // keep track that we have an outstanding request packet
155 // matching this request, this is used by the coherency
156 // mechanism in determining what to do with snoop responses
157 // (in recvTimingSnoop)
158 if (add_outstanding
) {
159 // we should never have an exsiting request outstanding
160 assert(outstandingReq
.find(pkt
->req
) == outstandingReq
.end());
161 outstandingReq
.insert(pkt
->req
);
164 // since it is a normal request, determine the destination
165 // based on the address and attempt to send the packet
166 bool success
= masterPorts
[findPort(pkt
->getAddr())]->sendTimingReq(pkt
);
168 // if this is an express snoop, we are done at this point
169 if (is_express_snoop
) {
172 // for normal requests, check if successful
174 // inhibited packets should never be forced to retry
175 assert(!pkt
->memInhibitAsserted());
177 // if it was added as outstanding and the send failed, then
180 outstandingReq
.erase(pkt
->req
);
182 // undo the calculation so we can check for 0 again
183 pkt
->busFirstWordDelay
= pkt
->busLastWordDelay
= 0;
185 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x RETRY\n",
186 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
188 // update the bus state and schedule an idle event
189 reqLayer
.failedTiming(src_port
, clockEdge(Cycles(headerCycles
)));
191 // update the bus state and schedule an idle event
192 reqLayer
.succeededTiming(packetFinishTime
);
200 CoherentBus::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
202 // determine the source port based on the id
203 MasterPort
*src_port
= masterPorts
[master_port_id
];
205 // test if the bus should be considered occupied for the current
207 if (!respLayer
.tryTiming(src_port
)) {
208 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x BUSY\n",
209 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
213 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x\n",
214 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
216 calcPacketTiming(pkt
);
217 Tick packetFinishTime
= pkt
->busLastWordDelay
+ curTick();
219 // the packet is a normal response to a request that we should
220 // have seen passing through the bus
221 assert(outstandingReq
.find(pkt
->req
) != outstandingReq
.end());
223 // remove it as outstanding
224 outstandingReq
.erase(pkt
->req
);
226 // send the packet to the destination through one of our slave
227 // ports, as determined by the destination field
228 bool success M5_VAR_USED
= slavePorts
[pkt
->getDest()]->sendTimingResp(pkt
);
230 // currently it is illegal to block responses... can lead to
234 respLayer
.succeededTiming(packetFinishTime
);
240 CoherentBus::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
242 DPRINTF(CoherentBus
, "recvTimingSnoopReq: src %s %s 0x%x\n",
243 masterPorts
[master_port_id
]->name(), pkt
->cmdString(),
246 // we should only see express snoops from caches
247 assert(pkt
->isExpressSnoop());
249 // set the source port for routing of the response
250 pkt
->setSrc(master_port_id
);
252 // forward to all snoopers
253 forwardTiming(pkt
, InvalidPortID
);
255 // a snoop request came from a connected slave device (one of
256 // our master ports), and if it is not coming from the slave
257 // device responsible for the address range something is
258 // wrong, hence there is nothing further to do as the packet
259 // would be going back to where it came from
260 assert(master_port_id
== findPort(pkt
->getAddr()));
264 CoherentBus::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
266 // determine the source port based on the id
267 SlavePort
* src_port
= slavePorts
[slave_port_id
];
269 // test if the bus should be considered occupied for the current
271 if (!snoopRespLayer
.tryTiming(src_port
)) {
272 DPRINTF(CoherentBus
, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
273 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
277 DPRINTF(CoherentBus
, "recvTimingSnoop: src %s %s 0x%x\n",
278 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
280 // get the destination from the packet
281 PortID dest
= pkt
->getDest();
283 // responses are never express snoops
284 assert(!pkt
->isExpressSnoop());
286 calcPacketTiming(pkt
);
287 Tick packetFinishTime
= pkt
->busLastWordDelay
+ curTick();
289 // determine if the response is from a snoop request we
290 // created as the result of a normal request (in which case it
291 // should be in the outstandingReq), or if we merely forwarded
292 // someone else's snoop request
293 if (outstandingReq
.find(pkt
->req
) == outstandingReq
.end()) {
294 // this is a snoop response to a snoop request we
295 // forwarded, e.g. coming from the L1 and going to the L2
296 // this should be forwarded as a snoop response
297 bool success M5_VAR_USED
= masterPorts
[dest
]->sendTimingSnoopResp(pkt
);
300 // we got a snoop response on one of our slave ports,
301 // i.e. from a coherent master connected to the bus, and
302 // since we created the snoop request as part of
303 // recvTiming, this should now be a normal response again
304 outstandingReq
.erase(pkt
->req
);
306 // this is a snoop response from a coherent master, with a
307 // destination field set on its way through the bus as
308 // request, hence it should never go back to where the
309 // snoop response came from, but instead to where the
310 // original request came from
311 assert(slave_port_id
!= dest
);
313 // as a normal response, it should go back to a master
314 // through one of our slave ports
315 bool success M5_VAR_USED
= slavePorts
[dest
]->sendTimingResp(pkt
);
317 // currently it is illegal to block responses... can lead
322 snoopRespLayer
.succeededTiming(packetFinishTime
);
329 CoherentBus::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
)
331 // snoops should only happen if the system isn't bypassing caches
332 assert(!system
->bypassCaches());
334 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
336 // we could have gotten this request from a snooping master
337 // (corresponding to our own slave port that is also in
338 // snoopPorts) and should not send it back to where it came
340 if (exclude_slave_port_id
== InvalidPortID
||
341 p
->getId() != exclude_slave_port_id
) {
342 // cache is not allowed to refuse snoop
343 p
->sendTimingSnoopReq(pkt
);
349 CoherentBus::recvRetry()
351 // responses and snoop responses never block on forwarding them,
352 // so the retry will always be coming from a port to which we
353 // tried to forward a request
354 reqLayer
.recvRetry();
358 CoherentBus::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
360 DPRINTF(CoherentBus
, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
361 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
364 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
365 Tick snoop_response_latency
= 0;
367 // uncacheable requests need never be snooped
368 if (!pkt
->req
->isUncacheable() && !system
->bypassCaches()) {
369 // forward to all snoopers but the source
370 std::pair
<MemCmd
, Tick
> snoop_result
=
371 forwardAtomic(pkt
, slave_port_id
);
372 snoop_response_cmd
= snoop_result
.first
;
373 snoop_response_latency
= snoop_result
.second
;
376 // even if we had a snoop response, we must continue and also
377 // perform the actual request at the destination
378 PortID dest_id
= findPort(pkt
->getAddr());
380 // forward the request to the appropriate destination
381 Tick response_latency
= masterPorts
[dest_id
]->sendAtomic(pkt
);
383 // if we got a response from a snooper, restore it here
384 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
385 // no one else should have responded
386 assert(!pkt
->isResponse());
387 pkt
->cmd
= snoop_response_cmd
;
388 response_latency
= snoop_response_latency
;
391 // @todo: Not setting first-word time
392 pkt
->busLastWordDelay
= response_latency
;
393 return response_latency
;
397 CoherentBus::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
399 DPRINTF(CoherentBus
, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
400 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
403 // forward to all snoopers
404 std::pair
<MemCmd
, Tick
> snoop_result
=
405 forwardAtomic(pkt
, InvalidPortID
);
406 MemCmd snoop_response_cmd
= snoop_result
.first
;
407 Tick snoop_response_latency
= snoop_result
.second
;
409 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
410 pkt
->cmd
= snoop_response_cmd
;
412 // @todo: Not setting first-word time
413 pkt
->busLastWordDelay
= snoop_response_latency
;
414 return snoop_response_latency
;
417 std::pair
<MemCmd
, Tick
>
418 CoherentBus::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
)
420 // the packet may be changed on snoops, record the original
421 // command to enable us to restore it between snoops so that
422 // additional snoops can take place properly
423 MemCmd orig_cmd
= pkt
->cmd
;
424 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
425 Tick snoop_response_latency
= 0;
427 // snoops should only happen if the system isn't bypassing caches
428 assert(!system
->bypassCaches());
430 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
432 // we could have gotten this request from a snooping master
433 // (corresponding to our own slave port that is also in
434 // snoopPorts) and should not send it back to where it came
436 if (exclude_slave_port_id
== InvalidPortID
||
437 p
->getId() != exclude_slave_port_id
) {
438 Tick latency
= p
->sendAtomicSnoop(pkt
);
439 // in contrast to a functional access, we have to keep on
440 // going as all snoopers must be updated even if we get a
442 if (pkt
->isResponse()) {
443 // response from snoop agent
444 assert(pkt
->cmd
!= orig_cmd
);
445 assert(pkt
->memInhibitAsserted());
446 // should only happen once
447 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
448 // save response state
449 snoop_response_cmd
= pkt
->cmd
;
450 snoop_response_latency
= latency
;
451 // restore original packet state for remaining snoopers
457 // the packet is restored as part of the loop and any potential
458 // snoop response is part of the returned pair
459 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
463 CoherentBus::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
465 if (!pkt
->isPrint()) {
466 // don't do DPRINTFs on PrintReq as it clutters up the output
468 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
469 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
473 // uncacheable requests need never be snooped
474 if (!pkt
->req
->isUncacheable() && !system
->bypassCaches()) {
475 // forward to all snoopers but the source
476 forwardFunctional(pkt
, slave_port_id
);
479 // there is no need to continue if the snooping has found what we
480 // were looking for and the packet is already a response
481 if (!pkt
->isResponse()) {
482 PortID dest_id
= findPort(pkt
->getAddr());
484 masterPorts
[dest_id
]->sendFunctional(pkt
);
489 CoherentBus::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
491 if (!pkt
->isPrint()) {
492 // don't do DPRINTFs on PrintReq as it clutters up the output
494 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
495 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
499 // forward to all snoopers
500 forwardFunctional(pkt
, InvalidPortID
);
504 CoherentBus::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
506 // snoops should only happen if the system isn't bypassing caches
507 assert(!system
->bypassCaches());
509 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
511 // we could have gotten this request from a snooping master
512 // (corresponding to our own slave port that is also in
513 // snoopPorts) and should not send it back to where it came
515 if (exclude_slave_port_id
== InvalidPortID
||
516 p
->getId() != exclude_slave_port_id
)
517 p
->sendFunctionalSnoop(pkt
);
519 // if we get a response we are done
520 if (pkt
->isResponse()) {
527 CoherentBus::drain(DrainManager
*dm
)
529 // sum up the individual layers
530 return reqLayer
.drain(dm
) + respLayer
.drain(dm
) + snoopRespLayer
.drain(dm
);
534 CoherentBusParams::create()
536 return new CoherentBus(this);