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 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x RETRY\n",
183 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
185 // update the bus state and schedule an idle event
186 reqLayer
.failedTiming(src_port
, clockEdge(Cycles(headerCycles
)));
188 // update the bus state and schedule an idle event
189 reqLayer
.succeededTiming(packetFinishTime
);
197 CoherentBus::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
199 // determine the source port based on the id
200 MasterPort
*src_port
= masterPorts
[master_port_id
];
202 // test if the bus should be considered occupied for the current
204 if (!respLayer
.tryTiming(src_port
)) {
205 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x BUSY\n",
206 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
210 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x\n",
211 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
213 calcPacketTiming(pkt
);
214 Tick packetFinishTime
= pkt
->busLastWordDelay
+ curTick();
216 // the packet is a normal response to a request that we should
217 // have seen passing through the bus
218 assert(outstandingReq
.find(pkt
->req
) != outstandingReq
.end());
220 // remove it as outstanding
221 outstandingReq
.erase(pkt
->req
);
223 // send the packet to the destination through one of our slave
224 // ports, as determined by the destination field
225 bool success M5_VAR_USED
= slavePorts
[pkt
->getDest()]->sendTimingResp(pkt
);
227 // currently it is illegal to block responses... can lead to
231 respLayer
.succeededTiming(packetFinishTime
);
237 CoherentBus::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
239 DPRINTF(CoherentBus
, "recvTimingSnoopReq: src %s %s 0x%x\n",
240 masterPorts
[master_port_id
]->name(), pkt
->cmdString(),
243 // we should only see express snoops from caches
244 assert(pkt
->isExpressSnoop());
246 // set the source port for routing of the response
247 pkt
->setSrc(master_port_id
);
249 // forward to all snoopers
250 forwardTiming(pkt
, InvalidPortID
);
252 // a snoop request came from a connected slave device (one of
253 // our master ports), and if it is not coming from the slave
254 // device responsible for the address range something is
255 // wrong, hence there is nothing further to do as the packet
256 // would be going back to where it came from
257 assert(master_port_id
== findPort(pkt
->getAddr()));
261 CoherentBus::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
263 // determine the source port based on the id
264 SlavePort
* src_port
= slavePorts
[slave_port_id
];
266 // test if the bus should be considered occupied for the current
268 if (!snoopRespLayer
.tryTiming(src_port
)) {
269 DPRINTF(CoherentBus
, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
270 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
274 DPRINTF(CoherentBus
, "recvTimingSnoop: src %s %s 0x%x\n",
275 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
277 // get the destination from the packet
278 PortID dest
= pkt
->getDest();
280 // responses are never express snoops
281 assert(!pkt
->isExpressSnoop());
283 calcPacketTiming(pkt
);
284 Tick packetFinishTime
= pkt
->busLastWordDelay
+ curTick();
286 // determine if the response is from a snoop request we
287 // created as the result of a normal request (in which case it
288 // should be in the outstandingReq), or if we merely forwarded
289 // someone else's snoop request
290 if (outstandingReq
.find(pkt
->req
) == outstandingReq
.end()) {
291 // this is a snoop response to a snoop request we
292 // forwarded, e.g. coming from the L1 and going to the L2
293 // this should be forwarded as a snoop response
294 bool success M5_VAR_USED
= masterPorts
[dest
]->sendTimingSnoopResp(pkt
);
297 // we got a snoop response on one of our slave ports,
298 // i.e. from a coherent master connected to the bus, and
299 // since we created the snoop request as part of
300 // recvTiming, this should now be a normal response again
301 outstandingReq
.erase(pkt
->req
);
303 // this is a snoop response from a coherent master, with a
304 // destination field set on its way through the bus as
305 // request, hence it should never go back to where the
306 // snoop response came from, but instead to where the
307 // original request came from
308 assert(slave_port_id
!= dest
);
310 // as a normal response, it should go back to a master
311 // through one of our slave ports
312 bool success M5_VAR_USED
= slavePorts
[dest
]->sendTimingResp(pkt
);
314 // currently it is illegal to block responses... can lead
319 snoopRespLayer
.succeededTiming(packetFinishTime
);
326 CoherentBus::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
)
328 // snoops should only happen if the system isn't bypassing caches
329 assert(!system
->bypassCaches());
331 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
333 // we could have gotten this request from a snooping master
334 // (corresponding to our own slave port that is also in
335 // snoopPorts) and should not send it back to where it came
337 if (exclude_slave_port_id
== InvalidPortID
||
338 p
->getId() != exclude_slave_port_id
) {
339 // cache is not allowed to refuse snoop
340 p
->sendTimingSnoopReq(pkt
);
346 CoherentBus::recvRetry()
348 // responses and snoop responses never block on forwarding them,
349 // so the retry will always be coming from a port to which we
350 // tried to forward a request
351 reqLayer
.recvRetry();
355 CoherentBus::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
357 DPRINTF(CoherentBus
, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
358 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
361 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
362 Tick snoop_response_latency
= 0;
364 // uncacheable requests need never be snooped
365 if (!pkt
->req
->isUncacheable() && !system
->bypassCaches()) {
366 // forward to all snoopers but the source
367 std::pair
<MemCmd
, Tick
> snoop_result
=
368 forwardAtomic(pkt
, slave_port_id
);
369 snoop_response_cmd
= snoop_result
.first
;
370 snoop_response_latency
= snoop_result
.second
;
373 // even if we had a snoop response, we must continue and also
374 // perform the actual request at the destination
375 PortID dest_id
= findPort(pkt
->getAddr());
377 // forward the request to the appropriate destination
378 Tick response_latency
= masterPorts
[dest_id
]->sendAtomic(pkt
);
380 // if we got a response from a snooper, restore it here
381 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
382 // no one else should have responded
383 assert(!pkt
->isResponse());
384 pkt
->cmd
= snoop_response_cmd
;
385 response_latency
= snoop_response_latency
;
388 // @todo: Not setting first-word time
389 pkt
->busLastWordDelay
= response_latency
;
390 return response_latency
;
394 CoherentBus::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
396 DPRINTF(CoherentBus
, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
397 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
400 // forward to all snoopers
401 std::pair
<MemCmd
, Tick
> snoop_result
=
402 forwardAtomic(pkt
, InvalidPortID
);
403 MemCmd snoop_response_cmd
= snoop_result
.first
;
404 Tick snoop_response_latency
= snoop_result
.second
;
406 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
407 pkt
->cmd
= snoop_response_cmd
;
409 // @todo: Not setting first-word time
410 pkt
->busLastWordDelay
= snoop_response_latency
;
411 return snoop_response_latency
;
414 std::pair
<MemCmd
, Tick
>
415 CoherentBus::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
)
417 // the packet may be changed on snoops, record the original
418 // command to enable us to restore it between snoops so that
419 // additional snoops can take place properly
420 MemCmd orig_cmd
= pkt
->cmd
;
421 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
422 Tick snoop_response_latency
= 0;
424 // snoops should only happen if the system isn't bypassing caches
425 assert(!system
->bypassCaches());
427 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
429 // we could have gotten this request from a snooping master
430 // (corresponding to our own slave port that is also in
431 // snoopPorts) and should not send it back to where it came
433 if (exclude_slave_port_id
== InvalidPortID
||
434 p
->getId() != exclude_slave_port_id
) {
435 Tick latency
= p
->sendAtomicSnoop(pkt
);
436 // in contrast to a functional access, we have to keep on
437 // going as all snoopers must be updated even if we get a
439 if (pkt
->isResponse()) {
440 // response from snoop agent
441 assert(pkt
->cmd
!= orig_cmd
);
442 assert(pkt
->memInhibitAsserted());
443 // should only happen once
444 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
445 // save response state
446 snoop_response_cmd
= pkt
->cmd
;
447 snoop_response_latency
= latency
;
448 // restore original packet state for remaining snoopers
454 // the packet is restored as part of the loop and any potential
455 // snoop response is part of the returned pair
456 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
460 CoherentBus::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
462 if (!pkt
->isPrint()) {
463 // don't do DPRINTFs on PrintReq as it clutters up the output
465 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
466 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
470 // uncacheable requests need never be snooped
471 if (!pkt
->req
->isUncacheable() && !system
->bypassCaches()) {
472 // forward to all snoopers but the source
473 forwardFunctional(pkt
, slave_port_id
);
476 // there is no need to continue if the snooping has found what we
477 // were looking for and the packet is already a response
478 if (!pkt
->isResponse()) {
479 PortID dest_id
= findPort(pkt
->getAddr());
481 masterPorts
[dest_id
]->sendFunctional(pkt
);
486 CoherentBus::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
488 if (!pkt
->isPrint()) {
489 // don't do DPRINTFs on PrintReq as it clutters up the output
491 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
492 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
496 // forward to all snoopers
497 forwardFunctional(pkt
, InvalidPortID
);
501 CoherentBus::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
503 // snoops should only happen if the system isn't bypassing caches
504 assert(!system
->bypassCaches());
506 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
508 // we could have gotten this request from a snooping master
509 // (corresponding to our own slave port that is also in
510 // snoopPorts) and should not send it back to where it came
512 if (exclude_slave_port_id
== InvalidPortID
||
513 p
->getId() != exclude_slave_port_id
)
514 p
->sendFunctionalSnoop(pkt
);
516 // if we get a response we are done
517 if (pkt
->isResponse()) {
524 CoherentBus::drain(DrainManager
*dm
)
526 // sum up the individual layers
527 return reqLayer
.drain(dm
) + respLayer
.drain(dm
) + snoopRespLayer
.drain(dm
);
531 CoherentBusParams::create()
533 return new CoherentBus(this);