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
)
59 // create the ports based on the size of the master and slave
60 // vector ports, and the presence of the default port, the ports
61 // are enumerated starting from zero
62 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
63 std::string portName
= csprintf("%s-p%d", name(), i
);
64 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this, i
);
65 masterPorts
.push_back(bp
);
68 // see if we have a default slave device connected and if so add
69 // our corresponding master port
70 if (p
->port_default_connection_count
) {
71 defaultPortID
= masterPorts
.size();
72 std::string portName
= csprintf("%s-default", name());
73 MasterPort
* bp
= new CoherentBusMasterPort(portName
, *this,
75 masterPorts
.push_back(bp
);
78 // create the slave ports, once again starting at zero
79 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
80 std::string portName
= csprintf("%s-p%d", name(), i
);
81 SlavePort
* bp
= new CoherentBusSlavePort(portName
, *this, i
);
82 slavePorts
.push_back(bp
);
91 // iterate over our slave ports and determine which of our
92 // neighbouring master ports are snooping and add them as snoopers
93 for (SlavePortConstIter p
= slavePorts
.begin(); p
!= slavePorts
.end();
95 // check if the connected master port is snooping
96 if ((*p
)->isSnooping()) {
97 DPRINTF(BusAddrRanges
, "Adding snooping master %s\n",
98 (*p
)->getMasterPort().name());
99 snoopPorts
.push_back(*p
);
103 if (snoopPorts
.empty())
104 warn("CoherentBus %s has no snooping ports attached!\n", name());
108 CoherentBus::recvTimingReq(PacketPtr pkt
, PortID slave_port_id
)
110 // determine the source port based on the id
111 SlavePort
*src_port
= slavePorts
[slave_port_id
];
113 // test if the bus should be considered occupied for the current
114 // port, and exclude express snoops from the check
115 if (!pkt
->isExpressSnoop() && isOccupied(src_port
)) {
116 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x BUSY\n",
117 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
121 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x\n",
122 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
124 // set the source port for routing of the response
125 pkt
->setSrc(slave_port_id
);
127 Tick headerFinishTime
= pkt
->isExpressSnoop() ? 0 : calcPacketTiming(pkt
);
128 Tick packetFinishTime
= pkt
->isExpressSnoop() ? 0 : pkt
->finishTime
;
130 // uncacheable requests need never be snooped
131 if (!pkt
->req
->isUncacheable()) {
132 // the packet is a memory-mapped request and should be
133 // broadcasted to our snoopers but the source
134 forwardTiming(pkt
, slave_port_id
);
137 // remember if we add an outstanding req so we can undo it if
138 // necessary, if the packet needs a response, we should add it
139 // as outstanding and express snoops never fail so there is
140 // not need to worry about them
141 bool add_outstanding
= !pkt
->isExpressSnoop() && pkt
->needsResponse();
143 // keep track that we have an outstanding request packet
144 // matching this request, this is used by the coherency
145 // mechanism in determining what to do with snoop responses
146 // (in recvTimingSnoop)
147 if (add_outstanding
) {
148 // we should never have an exsiting request outstanding
149 assert(outstandingReq
.find(pkt
->req
) == outstandingReq
.end());
150 outstandingReq
.insert(pkt
->req
);
153 // since it is a normal request, determine the destination
154 // based on the address and attempt to send the packet
155 bool success
= masterPorts
[findPort(pkt
->getAddr())]->sendTimingReq(pkt
);
158 // inhibited packets should never be forced to retry
159 assert(!pkt
->memInhibitAsserted());
161 // if it was added as outstanding and the send failed, then
164 outstandingReq
.erase(pkt
->req
);
166 DPRINTF(CoherentBus
, "recvTimingReq: src %s %s 0x%x RETRY\n",
167 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
169 addToRetryList(src_port
);
170 occupyBus(headerFinishTime
);
175 succeededTiming(packetFinishTime
);
181 CoherentBus::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
183 // determine the source port based on the id
184 MasterPort
*src_port
= masterPorts
[master_port_id
];
186 // test if the bus should be considered occupied for the current
188 if (isOccupied(src_port
)) {
189 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x BUSY\n",
190 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
194 DPRINTF(CoherentBus
, "recvTimingResp: src %s %s 0x%x\n",
195 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
197 calcPacketTiming(pkt
);
198 Tick packetFinishTime
= pkt
->finishTime
;
200 // the packet is a normal response to a request that we should
201 // have seen passing through the bus
202 assert(outstandingReq
.find(pkt
->req
) != outstandingReq
.end());
204 // remove it as outstanding
205 outstandingReq
.erase(pkt
->req
);
207 // send the packet to the destination through one of our slave
208 // ports, as determined by the destination field
209 bool success M5_VAR_USED
= slavePorts
[pkt
->getDest()]->sendTimingResp(pkt
);
211 // currently it is illegal to block responses... can lead to
215 succeededTiming(packetFinishTime
);
221 CoherentBus::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
223 DPRINTF(CoherentBus
, "recvTimingSnoopReq: src %s %s 0x%x\n",
224 masterPorts
[master_port_id
]->name(), pkt
->cmdString(),
227 // we should only see express snoops from caches
228 assert(pkt
->isExpressSnoop());
230 // set the source port for routing of the response
231 pkt
->setSrc(master_port_id
);
233 // forward to all snoopers
234 forwardTiming(pkt
, InvalidPortID
);
236 // a snoop request came from a connected slave device (one of
237 // our master ports), and if it is not coming from the slave
238 // device responsible for the address range something is
239 // wrong, hence there is nothing further to do as the packet
240 // would be going back to where it came from
241 assert(master_port_id
== findPort(pkt
->getAddr()));
243 // this is an express snoop and is never forced to retry
248 CoherentBus::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
250 // determine the source port based on the id
251 SlavePort
* src_port
= slavePorts
[slave_port_id
];
253 // test if the bus should be considered occupied for the current
255 if (isOccupied(src_port
)) {
256 DPRINTF(CoherentBus
, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
257 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
261 DPRINTF(CoherentBus
, "recvTimingSnoop: src %s %s 0x%x\n",
262 src_port
->name(), pkt
->cmdString(), pkt
->getAddr());
264 // get the destination from the packet
265 PortID dest
= pkt
->getDest();
267 // responses are never express snoops
268 assert(!pkt
->isExpressSnoop());
270 calcPacketTiming(pkt
);
271 Tick packetFinishTime
= pkt
->finishTime
;
273 // determine if the response is from a snoop request we
274 // created as the result of a normal request (in which case it
275 // should be in the outstandingReq), or if we merely forwarded
276 // someone else's snoop request
277 if (outstandingReq
.find(pkt
->req
) == outstandingReq
.end()) {
278 // this is a snoop response to a snoop request we
279 // forwarded, e.g. coming from the L1 and going to the L2
280 // this should be forwarded as a snoop response
281 bool success M5_VAR_USED
= masterPorts
[dest
]->sendTimingSnoopResp(pkt
);
284 // we got a snoop response on one of our slave ports,
285 // i.e. from a coherent master connected to the bus, and
286 // since we created the snoop request as part of
287 // recvTiming, this should now be a normal response again
288 outstandingReq
.erase(pkt
->req
);
290 // this is a snoop response from a coherent master, with a
291 // destination field set on its way through the bus as
292 // request, hence it should never go back to where the
293 // snoop response came from, but instead to where the
294 // original request came from
295 assert(slave_port_id
!= dest
);
297 // as a normal response, it should go back to a master
298 // through one of our slave ports
299 bool success M5_VAR_USED
= slavePorts
[dest
]->sendTimingResp(pkt
);
301 // currently it is illegal to block responses... can lead
306 succeededTiming(packetFinishTime
);
313 CoherentBus::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
)
315 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
317 // we could have gotten this request from a snooping master
318 // (corresponding to our own slave port that is also in
319 // snoopPorts) and should not send it back to where it came
321 if (exclude_slave_port_id
== InvalidPortID
||
322 p
->getId() != exclude_slave_port_id
) {
323 // cache is not allowed to refuse snoop
324 p
->sendTimingSnoopReq(pkt
);
330 CoherentBus::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
332 DPRINTF(CoherentBus
, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
333 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
336 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
337 Tick snoop_response_latency
= 0;
339 // uncacheable requests need never be snooped
340 if (!pkt
->req
->isUncacheable()) {
341 // forward to all snoopers but the source
342 std::pair
<MemCmd
, Tick
> snoop_result
=
343 forwardAtomic(pkt
, slave_port_id
);
344 snoop_response_cmd
= snoop_result
.first
;
345 snoop_response_latency
= snoop_result
.second
;
348 // even if we had a snoop response, we must continue and also
349 // perform the actual request at the destination
350 PortID dest_id
= findPort(pkt
->getAddr());
352 // forward the request to the appropriate destination
353 Tick response_latency
= masterPorts
[dest_id
]->sendAtomic(pkt
);
355 // if we got a response from a snooper, restore it here
356 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
357 // no one else should have responded
358 assert(!pkt
->isResponse());
359 pkt
->cmd
= snoop_response_cmd
;
360 response_latency
= snoop_response_latency
;
363 pkt
->finishTime
= curTick() + response_latency
;
364 return response_latency
;
368 CoherentBus::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
370 DPRINTF(CoherentBus
, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
371 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
374 // forward to all snoopers
375 std::pair
<MemCmd
, Tick
> snoop_result
=
376 forwardAtomic(pkt
, InvalidPortID
);
377 MemCmd snoop_response_cmd
= snoop_result
.first
;
378 Tick snoop_response_latency
= snoop_result
.second
;
380 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
381 pkt
->cmd
= snoop_response_cmd
;
383 pkt
->finishTime
= curTick() + snoop_response_latency
;
384 return snoop_response_latency
;
387 std::pair
<MemCmd
, Tick
>
388 CoherentBus::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
)
390 // the packet may be changed on snoops, record the original
391 // command to enable us to restore it between snoops so that
392 // additional snoops can take place properly
393 MemCmd orig_cmd
= pkt
->cmd
;
394 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
395 Tick snoop_response_latency
= 0;
397 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
399 // we could have gotten this request from a snooping master
400 // (corresponding to our own slave port that is also in
401 // snoopPorts) and should not send it back to where it came
403 if (exclude_slave_port_id
== InvalidPortID
||
404 p
->getId() != exclude_slave_port_id
) {
405 Tick latency
= p
->sendAtomicSnoop(pkt
);
406 // in contrast to a functional access, we have to keep on
407 // going as all snoopers must be updated even if we get a
409 if (pkt
->isResponse()) {
410 // response from snoop agent
411 assert(pkt
->cmd
!= orig_cmd
);
412 assert(pkt
->memInhibitAsserted());
413 // should only happen once
414 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
415 // save response state
416 snoop_response_cmd
= pkt
->cmd
;
417 snoop_response_latency
= latency
;
418 // restore original packet state for remaining snoopers
424 // the packet is restored as part of the loop and any potential
425 // snoop response is part of the returned pair
426 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
430 CoherentBus::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
432 if (!pkt
->isPrint()) {
433 // don't do DPRINTFs on PrintReq as it clutters up the output
435 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
436 slavePorts
[slave_port_id
]->name(), pkt
->getAddr(),
440 // uncacheable requests need never be snooped
441 if (!pkt
->req
->isUncacheable()) {
442 // forward to all snoopers but the source
443 forwardFunctional(pkt
, slave_port_id
);
446 // there is no need to continue if the snooping has found what we
447 // were looking for and the packet is already a response
448 if (!pkt
->isResponse()) {
449 PortID dest_id
= findPort(pkt
->getAddr());
451 masterPorts
[dest_id
]->sendFunctional(pkt
);
456 CoherentBus::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
458 if (!pkt
->isPrint()) {
459 // don't do DPRINTFs on PrintReq as it clutters up the output
461 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
462 masterPorts
[master_port_id
]->name(), pkt
->getAddr(),
466 // forward to all snoopers
467 forwardFunctional(pkt
, InvalidPortID
);
471 CoherentBus::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
473 for (SlavePortIter s
= snoopPorts
.begin(); s
!= snoopPorts
.end(); ++s
) {
475 // we could have gotten this request from a snooping master
476 // (corresponding to our own slave port that is also in
477 // snoopPorts) and should not send it back to where it came
479 if (exclude_slave_port_id
== InvalidPortID
||
480 p
->getId() != exclude_slave_port_id
)
481 p
->sendFunctionalSnoop(pkt
);
483 // if we get a response we are done
484 if (pkt
->isResponse()) {
491 CoherentBusParams::create()
493 return new CoherentBus(this);