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"
56 CoherentBus::CoherentBus(const CoherentBusParams
*p
)
57 : BaseBus(p
), reqLayer(*this, ".reqLayer", p
->clock
),
58 respLayer(*this, ".respLayer", p
->clock
),
59 snoopRespLayer(*this, ".snoopRespLayer", p
->clock
)
61 // create the ports based on the size of the master and slave
62 // vector ports, and the presence of the default port, the ports
63 // are enumerated starting from zero
64 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
65 std::string portName
= csprintf("%s.master[%d]", name(), i
);
66 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this, i
);
67 masterPorts
.push_back(bp
);
70 // see if we have a default slave device connected and if so add
71 // our corresponding master port
72 if (p
->port_default_connection_count
) {
73 defaultPortID
= masterPorts
.size();
74 std::string portName
= name() + ".default";
75 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this,
77 masterPorts
.push_back(bp
);
80 // create the slave ports, once again starting at zero
81 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
82 std::string portName
= csprintf("%s.slave[%d]", name(), i
);
83 SlavePort
* bp
= new CoherentBusSlavePort(portName
, *this, i
);
84 slavePorts
.push_back(bp
);
93 // the base class is responsible for determining the block size
96 // iterate over our slave ports and determine which of our
97 // neighbouring master ports are snooping and add them as snoopers
98 for (SlavePortConstIter p
= slavePorts
.begin(); p
!= slavePorts
.end();
100 // check if the connected master port is snooping
101 if ((*p
)->isSnooping()) {
102 DPRINTF(BusAddrRanges
, "Adding snooping master %s\n",
103 (*p
)->getMasterPort().name());
104 snoopPorts
.push_back(*p
);
108 if (snoopPorts
.empty())
109 warn("CoherentBus %s has no snooping ports attached!\n", name());
113 CoherentBus::recvTimingReq(PacketPtr pkt
, PortID slave_port_id
)
115 // determine the source port based on the id
116 SlavePort
*src_port
= slavePorts
[slave_port_id
];
118 // remember if the packet is an express snoop
119 bool is_express_snoop
= pkt
->isExpressSnoop();
121 // test if the bus should be considered occupied for the current
122 // port, and exclude express snoops from the check
123 if (!is_express_snoop
&& !reqLayer
.tryTiming(src_port
)) {
124 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x BUSY\n",
125 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
129 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s expr %d 0x%x\n",
130 src_port
->name(), pkt
->cmdString(), is_express_snoop
,
133 // set the source port for routing of the response
134 pkt
->setSrc(slave_port_id
);
136 Tick headerFinishTime
= is_express_snoop
? 0 : calcPacketTiming(pkt
);
137 Tick packetFinishTime
= is_express_snoop
? 0 : pkt
->finishTime
;
139 // uncacheable requests need never be snooped
140 if (!pkt
->req
->isUncacheable()) {
141 // the packet is a memory-mapped request and should be
142 // broadcasted to our snoopers but the source
143 forwardTiming(pkt
, slave_port_id
);
146 // remember if we add an outstanding req so we can undo it if
147 // necessary, if the packet needs a response, we should add it
148 // as outstanding and express snoops never fail so there is
149 // not need to worry about them
150 bool add_outstanding
= !is_express_snoop
&& pkt
->needsResponse();
152 // keep track that we have an outstanding request packet
153 // matching this request, this is used by the coherency
154 // mechanism in determining what to do with snoop responses
155 // (in recvTimingSnoop)
156 if (add_outstanding
) {
157 // we should never have an exsiting request outstanding
158 assert(outstandingReq
.find(pkt
->req
) == outstandingReq
.end());
159 outstandingReq
.insert(pkt
->req
);
162 // since it is a normal request, determine the destination
163 // based on the address and attempt to send the packet
164 bool success
= masterPorts
[findPort(pkt
->getAddr())]->sendTimingReq(pkt
);
166 // if this is an express snoop, we are done at this point
167 if (is_express_snoop
) {
170 // for normal requests, check if successful
172 // inhibited packets should never be forced to retry
173 assert(!pkt
->memInhibitAsserted());
175 // if it was added as outstanding and the send failed, then
178 outstandingReq
.erase(pkt
->req
);
180 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x RETRY\n",
181 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
183 // update the bus state and schedule an idle event
184 reqLayer
.failedTiming(src_port
, headerFinishTime
);
186 // update the bus state and schedule an idle event
187 reqLayer
.succeededTiming(packetFinishTime
);
195 CoherentBus::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
197 // determine the source port based on the id
198 MasterPort
*src_port
= masterPorts
[master_port_id
];
200 // test if the bus should be considered occupied for the current
202 if (!respLayer
.tryTiming(src_port
)) {
203 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x BUSY\n",
204 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
208 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x\n",
209 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
211 calcPacketTiming(pkt
);
212 Tick packetFinishTime
= pkt
->finishTime
;
214 // the packet is a normal response to a request that we should
215 // have seen passing through the bus
216 assert(outstandingReq
.find(pkt
->req
) != outstandingReq
.end());
218 // remove it as outstanding
219 outstandingReq
.erase(pkt
->req
);
221 // send the packet to the destination through one of our slave
222 // ports, as determined by the destination field
223 bool success M5_VAR_USED
= slavePorts
[pkt
->getDest()]->sendTimingResp(pkt
);
225 // currently it is illegal to block responses... can lead to
229 respLayer
.succeededTiming(packetFinishTime
);
235 CoherentBus::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
237 DPRINTF(CoherentBus
, "recvTimingSnoopReq: src %s %s 0x%x\n",
238 masterPorts
[master_port_id
]->name(), pkt
->cmdString(),
241 // we should only see express snoops from caches
242 assert(pkt
->isExpressSnoop());
244 // set the source port for routing of the response
245 pkt
->setSrc(master_port_id
);
247 // forward to all snoopers
248 forwardTiming(pkt
, InvalidPortID
);
250 // a snoop request came from a connected slave device (one of
251 // our master ports), and if it is not coming from the slave
252 // device responsible for the address range something is
253 // wrong, hence there is nothing further to do as the packet
254 // would be going back to where it came from
255 assert(master_port_id
== findPort(pkt
->getAddr()));
259 CoherentBus::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
261 // determine the source port based on the id
262 SlavePort
* src_port
= slavePorts
[slave_port_id
];
264 // test if the bus should be considered occupied for the current
266 if (!snoopRespLayer
.tryTiming(src_port
)) {
267 DPRINTF(CoherentBus
, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
268 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
272 DPRINTF(CoherentBus
, "recvTimingSnoop: src %s %s 0x%x\n",
273 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
275 // get the destination from the packet
276 PortID dest
= pkt
->getDest();
278 // responses are never express snoops
279 assert(!pkt
->isExpressSnoop());
281 calcPacketTiming(pkt
);
282 Tick packetFinishTime
= pkt
->finishTime
;
284 // determine if the response is from a snoop request we
285 // created as the result of a normal request (in which case it
286 // should be in the outstandingReq), or if we merely forwarded
287 // someone else's snoop request
288 if (outstandingReq
.find(pkt
->req
) == outstandingReq
.end()) {
289 // this is a snoop response to a snoop request we
290 // forwarded, e.g. coming from the L1 and going to the L2
291 // this should be forwarded as a snoop response
292 bool success M5_VAR_USED
= masterPorts
[dest
]->sendTimingSnoopResp(pkt
);
295 // we got a snoop response on one of our slave ports,
296 // i.e. from a coherent master connected to the bus, and
297 // since we created the snoop request as part of
298 // recvTiming, this should now be a normal response again
299 outstandingReq
.erase(pkt
->req
);
301 // this is a snoop response from a coherent master, with a
302 // destination field set on its way through the bus as
303 // request, hence it should never go back to where the
304 // snoop response came from, but instead to where the
305 // original request came from
306 assert(slave_port_id
!= dest
);
308 // as a normal response, it should go back to a master
309 // through one of our slave ports
310 bool success M5_VAR_USED
= slavePorts
[dest
]->sendTimingResp(pkt
);
312 // currently it is illegal to block responses... can lead
317 snoopRespLayer
.succeededTiming(packetFinishTime
);
324 CoherentBus::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
)
326 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
328 // we could have gotten this request from a snooping master
329 // (corresponding to our own slave port that is also in
330 // snoopPorts) and should not send it back to where it came
332 if (exclude_slave_port_id
== InvalidPortID
||
333 p
->getId() != exclude_slave_port_id
) {
334 // cache is not allowed to refuse snoop
335 p
->sendTimingSnoopReq(pkt
);
341 CoherentBus::recvRetry()
343 // responses and snoop responses never block on forwarding them,
344 // so the retry will always be coming from a port to which we
345 // tried to forward a request
346 reqLayer
.recvRetry();
350 CoherentBus::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
352 DPRINTF(CoherentBus
, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
353 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
356 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
357 Tick snoop_response_latency
= 0;
359 // uncacheable requests need never be snooped
360 if (!pkt
->req
->isUncacheable()) {
361 // forward to all snoopers but the source
362 std::pair
<MemCmd
, Tick
> snoop_result
=
363 forwardAtomic(pkt
, slave_port_id
);
364 snoop_response_cmd
= snoop_result
.first
;
365 snoop_response_latency
= snoop_result
.second
;
368 // even if we had a snoop response, we must continue and also
369 // perform the actual request at the destination
370 PortID dest_id
= findPort(pkt
->getAddr());
372 // forward the request to the appropriate destination
373 Tick response_latency
= masterPorts
[dest_id
]->sendAtomic(pkt
);
375 // if we got a response from a snooper, restore it here
376 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
377 // no one else should have responded
378 assert(!pkt
->isResponse());
379 pkt
->cmd
= snoop_response_cmd
;
380 response_latency
= snoop_response_latency
;
383 pkt
->finishTime
= curTick() + response_latency
;
384 return response_latency
;
388 CoherentBus::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
390 DPRINTF(CoherentBus
, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
391 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
394 // forward to all snoopers
395 std::pair
<MemCmd
, Tick
> snoop_result
=
396 forwardAtomic(pkt
, InvalidPortID
);
397 MemCmd snoop_response_cmd
= snoop_result
.first
;
398 Tick snoop_response_latency
= snoop_result
.second
;
400 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
401 pkt
->cmd
= snoop_response_cmd
;
403 pkt
->finishTime
= curTick() + snoop_response_latency
;
404 return snoop_response_latency
;
407 std::pair
<MemCmd
, Tick
>
408 CoherentBus::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
)
410 // the packet may be changed on snoops, record the original
411 // command to enable us to restore it between snoops so that
412 // additional snoops can take place properly
413 MemCmd orig_cmd
= pkt
->cmd
;
414 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
415 Tick snoop_response_latency
= 0;
417 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
419 // we could have gotten this request from a snooping master
420 // (corresponding to our own slave port that is also in
421 // snoopPorts) and should not send it back to where it came
423 if (exclude_slave_port_id
== InvalidPortID
||
424 p
->getId() != exclude_slave_port_id
) {
425 Tick latency
= p
->sendAtomicSnoop(pkt
);
426 // in contrast to a functional access, we have to keep on
427 // going as all snoopers must be updated even if we get a
429 if (pkt
->isResponse()) {
430 // response from snoop agent
431 assert(pkt
->cmd
!= orig_cmd
);
432 assert(pkt
->memInhibitAsserted());
433 // should only happen once
434 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
435 // save response state
436 snoop_response_cmd
= pkt
->cmd
;
437 snoop_response_latency
= latency
;
438 // restore original packet state for remaining snoopers
444 // the packet is restored as part of the loop and any potential
445 // snoop response is part of the returned pair
446 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
450 CoherentBus::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
452 if (!pkt
->isPrint()) {
453 // don't do DPRINTFs on PrintReq as it clutters up the output
455 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
456 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
460 // uncacheable requests need never be snooped
461 if (!pkt
->req
->isUncacheable()) {
462 // forward to all snoopers but the source
463 forwardFunctional(pkt
, slave_port_id
);
466 // there is no need to continue if the snooping has found what we
467 // were looking for and the packet is already a response
468 if (!pkt
->isResponse()) {
469 PortID dest_id
= findPort(pkt
->getAddr());
471 masterPorts
[dest_id
]->sendFunctional(pkt
);
476 CoherentBus::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
478 if (!pkt
->isPrint()) {
479 // don't do DPRINTFs on PrintReq as it clutters up the output
481 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
482 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
486 // forward to all snoopers
487 forwardFunctional(pkt
, InvalidPortID
);
491 CoherentBus::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
493 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
495 // we could have gotten this request from a snooping master
496 // (corresponding to our own slave port that is also in
497 // snoopPorts) and should not send it back to where it came
499 if (exclude_slave_port_id
== InvalidPortID
||
500 p
->getId() != exclude_slave_port_id
)
501 p
->sendFunctionalSnoop(pkt
);
503 // if we get a response we are done
504 if (pkt
->isResponse()) {
511 CoherentBus::drain(DrainManager
*dm
)
513 // sum up the individual layers
514 return reqLayer
.drain(dm
) + respLayer
.drain(dm
) + snoopRespLayer
.drain(dm
);
518 CoherentBusParams::create()
520 return new CoherentBus(this);