coherent_xbar.cc (12346:9b1144d046ca) coherent_xbar.cc (12351:17eaa27bef22)
1/*
2 * Copyright (c) 2011-2017 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
1/*
2 * Copyright (c) 2011-2017 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
43 * Nikos Nikoleris
43 */
44
45/**
46 * @file
47 * Definition of a crossbar object.
48 */
49
50#include "mem/coherent_xbar.hh"
51
52#include "base/logging.hh"
53#include "base/trace.hh"
54#include "debug/AddrRanges.hh"
55#include "debug/CoherentXBar.hh"
56#include "sim/system.hh"
57
58CoherentXBar::CoherentXBar(const CoherentXBarParams *p)
59 : BaseXBar(p), system(p->system), snoopFilter(p->snoop_filter),
60 snoopResponseLatency(p->snoop_response_latency),
61 pointOfCoherency(p->point_of_coherency),
62 pointOfUnification(p->point_of_unification)
63{
64 // create the ports based on the size of the master and slave
65 // vector ports, and the presence of the default port, the ports
66 // are enumerated starting from zero
67 for (int i = 0; i < p->port_master_connection_count; ++i) {
68 std::string portName = csprintf("%s.master[%d]", name(), i);
69 MasterPort* bp = new CoherentXBarMasterPort(portName, *this, i);
70 masterPorts.push_back(bp);
71 reqLayers.push_back(new ReqLayer(*bp, *this,
72 csprintf(".reqLayer%d", i)));
73 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
74 csprintf(".snoopLayer%d", i)));
75 }
76
77 // see if we have a default slave device connected and if so add
78 // our corresponding master port
79 if (p->port_default_connection_count) {
80 defaultPortID = masterPorts.size();
81 std::string portName = name() + ".default";
82 MasterPort* bp = new CoherentXBarMasterPort(portName, *this,
83 defaultPortID);
84 masterPorts.push_back(bp);
85 reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
86 defaultPortID)));
87 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
88 csprintf(".snoopLayer%d",
89 defaultPortID)));
90 }
91
92 // create the slave ports, once again starting at zero
93 for (int i = 0; i < p->port_slave_connection_count; ++i) {
94 std::string portName = csprintf("%s.slave[%d]", name(), i);
95 QueuedSlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
96 slavePorts.push_back(bp);
97 respLayers.push_back(new RespLayer(*bp, *this,
98 csprintf(".respLayer%d", i)));
99 snoopRespPorts.push_back(new SnoopRespPort(*bp, *this));
100 }
101
102 clearPortCache();
103}
104
105CoherentXBar::~CoherentXBar()
106{
107 for (auto l: reqLayers)
108 delete l;
109 for (auto l: respLayers)
110 delete l;
111 for (auto l: snoopLayers)
112 delete l;
113 for (auto p: snoopRespPorts)
114 delete p;
115}
116
117void
118CoherentXBar::init()
119{
120 BaseXBar::init();
121
122 // iterate over our slave ports and determine which of our
123 // neighbouring master ports are snooping and add them as snoopers
124 for (const auto& p: slavePorts) {
125 // check if the connected master port is snooping
126 if (p->isSnooping()) {
127 DPRINTF(AddrRanges, "Adding snooping master %s\n",
128 p->getMasterPort().name());
129 snoopPorts.push_back(p);
130 }
131 }
132
133 if (snoopPorts.empty())
134 warn("CoherentXBar %s has no snooping ports attached!\n", name());
135
136 // inform the snoop filter about the slave ports so it can create
137 // its own internal representation
138 if (snoopFilter)
139 snoopFilter->setSlavePorts(slavePorts);
140}
141
142bool
143CoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
144{
145 // determine the source port based on the id
146 SlavePort *src_port = slavePorts[slave_port_id];
147
148 // remember if the packet is an express snoop
149 bool is_express_snoop = pkt->isExpressSnoop();
150 bool cache_responding = pkt->cacheResponding();
151 // for normal requests, going downstream, the express snoop flag
152 // and the cache responding flag should always be the same
153 assert(is_express_snoop == cache_responding);
154
155 // determine the destination based on the address
156 PortID master_port_id = findPort(pkt->getAddr());
157
158 // test if the crossbar should be considered occupied for the current
159 // port, and exclude express snoops from the check
160 if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
161 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
162 src_port->name(), pkt->print());
163 return false;
164 }
165
166 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
167 src_port->name(), pkt->print());
168
169 // store size and command as they might be modified when
170 // forwarding the packet
171 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
172 unsigned int pkt_cmd = pkt->cmdToIndex();
173
174 // store the old header delay so we can restore it if needed
175 Tick old_header_delay = pkt->headerDelay;
176
177 // a request sees the frontend and forward latency
178 Tick xbar_delay = (frontendLatency + forwardLatency) * clockPeriod();
179
180 // set the packet header and payload delay
181 calcPacketTiming(pkt, xbar_delay);
182
183 // determine how long to be crossbar layer is busy
184 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
185
186 // is this the destination point for this packet? (e.g. true if
187 // this xbar is the PoC for a cache maintenance operation to the
188 // PoC) otherwise the destination is any cache that can satisfy
189 // the request
190 const bool is_destination = isDestination(pkt);
191
192 const bool snoop_caches = !system->bypassCaches() &&
193 pkt->cmd != MemCmd::WriteClean;
194 if (snoop_caches) {
195 assert(pkt->snoopDelay == 0);
196
44 */
45
46/**
47 * @file
48 * Definition of a crossbar object.
49 */
50
51#include "mem/coherent_xbar.hh"
52
53#include "base/logging.hh"
54#include "base/trace.hh"
55#include "debug/AddrRanges.hh"
56#include "debug/CoherentXBar.hh"
57#include "sim/system.hh"
58
59CoherentXBar::CoherentXBar(const CoherentXBarParams *p)
60 : BaseXBar(p), system(p->system), snoopFilter(p->snoop_filter),
61 snoopResponseLatency(p->snoop_response_latency),
62 pointOfCoherency(p->point_of_coherency),
63 pointOfUnification(p->point_of_unification)
64{
65 // create the ports based on the size of the master and slave
66 // vector ports, and the presence of the default port, the ports
67 // are enumerated starting from zero
68 for (int i = 0; i < p->port_master_connection_count; ++i) {
69 std::string portName = csprintf("%s.master[%d]", name(), i);
70 MasterPort* bp = new CoherentXBarMasterPort(portName, *this, i);
71 masterPorts.push_back(bp);
72 reqLayers.push_back(new ReqLayer(*bp, *this,
73 csprintf(".reqLayer%d", i)));
74 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
75 csprintf(".snoopLayer%d", i)));
76 }
77
78 // see if we have a default slave device connected and if so add
79 // our corresponding master port
80 if (p->port_default_connection_count) {
81 defaultPortID = masterPorts.size();
82 std::string portName = name() + ".default";
83 MasterPort* bp = new CoherentXBarMasterPort(portName, *this,
84 defaultPortID);
85 masterPorts.push_back(bp);
86 reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
87 defaultPortID)));
88 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
89 csprintf(".snoopLayer%d",
90 defaultPortID)));
91 }
92
93 // create the slave ports, once again starting at zero
94 for (int i = 0; i < p->port_slave_connection_count; ++i) {
95 std::string portName = csprintf("%s.slave[%d]", name(), i);
96 QueuedSlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
97 slavePorts.push_back(bp);
98 respLayers.push_back(new RespLayer(*bp, *this,
99 csprintf(".respLayer%d", i)));
100 snoopRespPorts.push_back(new SnoopRespPort(*bp, *this));
101 }
102
103 clearPortCache();
104}
105
106CoherentXBar::~CoherentXBar()
107{
108 for (auto l: reqLayers)
109 delete l;
110 for (auto l: respLayers)
111 delete l;
112 for (auto l: snoopLayers)
113 delete l;
114 for (auto p: snoopRespPorts)
115 delete p;
116}
117
118void
119CoherentXBar::init()
120{
121 BaseXBar::init();
122
123 // iterate over our slave ports and determine which of our
124 // neighbouring master ports are snooping and add them as snoopers
125 for (const auto& p: slavePorts) {
126 // check if the connected master port is snooping
127 if (p->isSnooping()) {
128 DPRINTF(AddrRanges, "Adding snooping master %s\n",
129 p->getMasterPort().name());
130 snoopPorts.push_back(p);
131 }
132 }
133
134 if (snoopPorts.empty())
135 warn("CoherentXBar %s has no snooping ports attached!\n", name());
136
137 // inform the snoop filter about the slave ports so it can create
138 // its own internal representation
139 if (snoopFilter)
140 snoopFilter->setSlavePorts(slavePorts);
141}
142
143bool
144CoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
145{
146 // determine the source port based on the id
147 SlavePort *src_port = slavePorts[slave_port_id];
148
149 // remember if the packet is an express snoop
150 bool is_express_snoop = pkt->isExpressSnoop();
151 bool cache_responding = pkt->cacheResponding();
152 // for normal requests, going downstream, the express snoop flag
153 // and the cache responding flag should always be the same
154 assert(is_express_snoop == cache_responding);
155
156 // determine the destination based on the address
157 PortID master_port_id = findPort(pkt->getAddr());
158
159 // test if the crossbar should be considered occupied for the current
160 // port, and exclude express snoops from the check
161 if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
162 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
163 src_port->name(), pkt->print());
164 return false;
165 }
166
167 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
168 src_port->name(), pkt->print());
169
170 // store size and command as they might be modified when
171 // forwarding the packet
172 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
173 unsigned int pkt_cmd = pkt->cmdToIndex();
174
175 // store the old header delay so we can restore it if needed
176 Tick old_header_delay = pkt->headerDelay;
177
178 // a request sees the frontend and forward latency
179 Tick xbar_delay = (frontendLatency + forwardLatency) * clockPeriod();
180
181 // set the packet header and payload delay
182 calcPacketTiming(pkt, xbar_delay);
183
184 // determine how long to be crossbar layer is busy
185 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
186
187 // is this the destination point for this packet? (e.g. true if
188 // this xbar is the PoC for a cache maintenance operation to the
189 // PoC) otherwise the destination is any cache that can satisfy
190 // the request
191 const bool is_destination = isDestination(pkt);
192
193 const bool snoop_caches = !system->bypassCaches() &&
194 pkt->cmd != MemCmd::WriteClean;
195 if (snoop_caches) {
196 assert(pkt->snoopDelay == 0);
197
198 if (pkt->isClean() && !is_destination) {
199 // before snooping we need to make sure that the memory
200 // below is not busy and the cache clean request can be
201 // forwarded to it
202 if (!masterPorts[master_port_id]->tryTiming(pkt)) {
203 DPRINTF(CoherentXBar, "%s: src %s packet %s RETRY\n", __func__,
204 src_port->name(), pkt->print());
205
206 // update the layer state and schedule an idle event
207 reqLayers[master_port_id]->failedTiming(src_port,
208 clockEdge(Cycles(1)));
209 return false;
210 }
211 }
212
213
197 // the packet is a memory-mapped request and should be
198 // broadcasted to our snoopers but the source
199 if (snoopFilter) {
200 // check with the snoop filter where to forward this packet
201 auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
202 // the time required by a packet to be delivered through
203 // the xbar has to be charged also with to lookup latency
204 // of the snoop filter
205 pkt->headerDelay += sf_res.second * clockPeriod();
206 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
207 __func__, src_port->name(), pkt->print(),
208 sf_res.first.size(), sf_res.second);
209
210 if (pkt->isEviction()) {
211 // for block-evicting packets, i.e. writebacks and
212 // clean evictions, there is no need to snoop up, as
213 // all we do is determine if the block is cached or
214 // not, instead just set it here based on the snoop
215 // filter result
216 if (!sf_res.first.empty())
217 pkt->setBlockCached();
218 } else {
219 forwardTiming(pkt, slave_port_id, sf_res.first);
220 }
221 } else {
222 forwardTiming(pkt, slave_port_id);
223 }
224
225 // add the snoop delay to our header delay, and then reset it
226 pkt->headerDelay += pkt->snoopDelay;
227 pkt->snoopDelay = 0;
228 }
229
230 // set up a sensible starting point
231 bool success = true;
232
233 // remember if the packet will generate a snoop response by
234 // checking if a cache set the cacheResponding flag during the
235 // snooping above
236 const bool expect_snoop_resp = !cache_responding && pkt->cacheResponding();
237 bool expect_response = pkt->needsResponse() && !pkt->cacheResponding();
238
239 const bool sink_packet = sinkPacket(pkt);
240
241 // in certain cases the crossbar is responsible for responding
242 bool respond_directly = false;
243 // store the original address as an address mapper could possibly
244 // modify the address upon a sendTimingRequest
245 const Addr addr(pkt->getAddr());
246 if (sink_packet) {
247 DPRINTF(CoherentXBar, "%s: Not forwarding %s\n", __func__,
248 pkt->print());
249 } else {
250 // determine if we are forwarding the packet, or responding to
251 // it
252 if (forwardPacket(pkt)) {
253 // if we are passing on, rather than sinking, a packet to
254 // which an upstream cache has committed to responding,
255 // the line was needs writable, and the responding only
256 // had an Owned copy, so we need to immidiately let the
257 // downstream caches know, bypass any flow control
258 if (pkt->cacheResponding()) {
259 pkt->setExpressSnoop();
260 }
261
262 // make sure that the write request (e.g., WriteClean)
263 // will stop at the memory below if this crossbar is its
264 // destination
265 if (pkt->isWrite() && is_destination) {
266 pkt->clearWriteThrough();
267 }
268
269 // since it is a normal request, attempt to send the packet
270 success = masterPorts[master_port_id]->sendTimingReq(pkt);
271 } else {
272 // no need to forward, turn this packet around and respond
273 // directly
274 assert(pkt->needsResponse());
275
276 respond_directly = true;
277 assert(!expect_snoop_resp);
278 expect_response = false;
279 }
280 }
281
282 if (snoopFilter && snoop_caches) {
283 // Let the snoop filter know about the success of the send operation
284 snoopFilter->finishRequest(!success, addr, pkt->isSecure());
285 }
286
287 // check if we were successful in sending the packet onwards
288 if (!success) {
289 // express snoops should never be forced to retry
290 assert(!is_express_snoop);
291
292 // restore the header delay
293 pkt->headerDelay = old_header_delay;
294
295 DPRINTF(CoherentXBar, "%s: src %s packet %s RETRY\n", __func__,
296 src_port->name(), pkt->print());
297
298 // update the layer state and schedule an idle event
299 reqLayers[master_port_id]->failedTiming(src_port,
300 clockEdge(Cycles(1)));
301 } else {
302 // express snoops currently bypass the crossbar state entirely
303 if (!is_express_snoop) {
304 // if this particular request will generate a snoop
305 // response
306 if (expect_snoop_resp) {
307 // we should never have an exsiting request outstanding
308 assert(outstandingSnoop.find(pkt->req) ==
309 outstandingSnoop.end());
310 outstandingSnoop.insert(pkt->req);
311
312 // basic sanity check on the outstanding snoops
313 panic_if(outstandingSnoop.size() > 512,
314 "Outstanding snoop requests exceeded 512\n");
315 }
316
317 // remember where to route the normal response to
318 if (expect_response || expect_snoop_resp) {
319 assert(routeTo.find(pkt->req) == routeTo.end());
320 routeTo[pkt->req] = slave_port_id;
321
322 panic_if(routeTo.size() > 512,
323 "Routing table exceeds 512 packets\n");
324 }
325
326 // update the layer state and schedule an idle event
327 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
328 }
329
330 // stats updates only consider packets that were successfully sent
331 pktCount[slave_port_id][master_port_id]++;
332 pktSize[slave_port_id][master_port_id] += pkt_size;
333 transDist[pkt_cmd]++;
334
335 if (is_express_snoop) {
336 snoops++;
337 snoopTraffic += pkt_size;
338 }
339 }
340
341 if (sink_packet)
342 // queue the packet for deletion
343 pendingDelete.reset(pkt);
344
214 // the packet is a memory-mapped request and should be
215 // broadcasted to our snoopers but the source
216 if (snoopFilter) {
217 // check with the snoop filter where to forward this packet
218 auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
219 // the time required by a packet to be delivered through
220 // the xbar has to be charged also with to lookup latency
221 // of the snoop filter
222 pkt->headerDelay += sf_res.second * clockPeriod();
223 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
224 __func__, src_port->name(), pkt->print(),
225 sf_res.first.size(), sf_res.second);
226
227 if (pkt->isEviction()) {
228 // for block-evicting packets, i.e. writebacks and
229 // clean evictions, there is no need to snoop up, as
230 // all we do is determine if the block is cached or
231 // not, instead just set it here based on the snoop
232 // filter result
233 if (!sf_res.first.empty())
234 pkt->setBlockCached();
235 } else {
236 forwardTiming(pkt, slave_port_id, sf_res.first);
237 }
238 } else {
239 forwardTiming(pkt, slave_port_id);
240 }
241
242 // add the snoop delay to our header delay, and then reset it
243 pkt->headerDelay += pkt->snoopDelay;
244 pkt->snoopDelay = 0;
245 }
246
247 // set up a sensible starting point
248 bool success = true;
249
250 // remember if the packet will generate a snoop response by
251 // checking if a cache set the cacheResponding flag during the
252 // snooping above
253 const bool expect_snoop_resp = !cache_responding && pkt->cacheResponding();
254 bool expect_response = pkt->needsResponse() && !pkt->cacheResponding();
255
256 const bool sink_packet = sinkPacket(pkt);
257
258 // in certain cases the crossbar is responsible for responding
259 bool respond_directly = false;
260 // store the original address as an address mapper could possibly
261 // modify the address upon a sendTimingRequest
262 const Addr addr(pkt->getAddr());
263 if (sink_packet) {
264 DPRINTF(CoherentXBar, "%s: Not forwarding %s\n", __func__,
265 pkt->print());
266 } else {
267 // determine if we are forwarding the packet, or responding to
268 // it
269 if (forwardPacket(pkt)) {
270 // if we are passing on, rather than sinking, a packet to
271 // which an upstream cache has committed to responding,
272 // the line was needs writable, and the responding only
273 // had an Owned copy, so we need to immidiately let the
274 // downstream caches know, bypass any flow control
275 if (pkt->cacheResponding()) {
276 pkt->setExpressSnoop();
277 }
278
279 // make sure that the write request (e.g., WriteClean)
280 // will stop at the memory below if this crossbar is its
281 // destination
282 if (pkt->isWrite() && is_destination) {
283 pkt->clearWriteThrough();
284 }
285
286 // since it is a normal request, attempt to send the packet
287 success = masterPorts[master_port_id]->sendTimingReq(pkt);
288 } else {
289 // no need to forward, turn this packet around and respond
290 // directly
291 assert(pkt->needsResponse());
292
293 respond_directly = true;
294 assert(!expect_snoop_resp);
295 expect_response = false;
296 }
297 }
298
299 if (snoopFilter && snoop_caches) {
300 // Let the snoop filter know about the success of the send operation
301 snoopFilter->finishRequest(!success, addr, pkt->isSecure());
302 }
303
304 // check if we were successful in sending the packet onwards
305 if (!success) {
306 // express snoops should never be forced to retry
307 assert(!is_express_snoop);
308
309 // restore the header delay
310 pkt->headerDelay = old_header_delay;
311
312 DPRINTF(CoherentXBar, "%s: src %s packet %s RETRY\n", __func__,
313 src_port->name(), pkt->print());
314
315 // update the layer state and schedule an idle event
316 reqLayers[master_port_id]->failedTiming(src_port,
317 clockEdge(Cycles(1)));
318 } else {
319 // express snoops currently bypass the crossbar state entirely
320 if (!is_express_snoop) {
321 // if this particular request will generate a snoop
322 // response
323 if (expect_snoop_resp) {
324 // we should never have an exsiting request outstanding
325 assert(outstandingSnoop.find(pkt->req) ==
326 outstandingSnoop.end());
327 outstandingSnoop.insert(pkt->req);
328
329 // basic sanity check on the outstanding snoops
330 panic_if(outstandingSnoop.size() > 512,
331 "Outstanding snoop requests exceeded 512\n");
332 }
333
334 // remember where to route the normal response to
335 if (expect_response || expect_snoop_resp) {
336 assert(routeTo.find(pkt->req) == routeTo.end());
337 routeTo[pkt->req] = slave_port_id;
338
339 panic_if(routeTo.size() > 512,
340 "Routing table exceeds 512 packets\n");
341 }
342
343 // update the layer state and schedule an idle event
344 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
345 }
346
347 // stats updates only consider packets that were successfully sent
348 pktCount[slave_port_id][master_port_id]++;
349 pktSize[slave_port_id][master_port_id] += pkt_size;
350 transDist[pkt_cmd]++;
351
352 if (is_express_snoop) {
353 snoops++;
354 snoopTraffic += pkt_size;
355 }
356 }
357
358 if (sink_packet)
359 // queue the packet for deletion
360 pendingDelete.reset(pkt);
361
362 // normally we respond to the packet we just received if we need to
363 PacketPtr rsp_pkt = pkt;
364 PortID rsp_port_id = slave_port_id;
365
366 // If this is the destination of the cache clean operation the
367 // crossbar is responsible for responding. This crossbar will
368 // respond when the cache clean is complete. A cache clean
369 // is complete either:
370 // * direcly, if no cache above had a dirty copy of the block
371 // as indicated by the satisfied flag of the packet, or
372 // * when the crossbar has seen both the cache clean request
373 // (CleanSharedReq, CleanInvalidReq) and the corresponding
374 // write (WriteClean) which updates the block in the memory
375 // below.
376 if (success &&
377 ((pkt->isClean() && pkt->satisfied()) ||
378 pkt->cmd == MemCmd::WriteClean) &&
379 is_destination) {
380 PacketPtr deferred_rsp = pkt->isWrite() ? nullptr : pkt;
381 auto cmo_lookup = outstandingCMO.find(pkt->id);
382 if (cmo_lookup != outstandingCMO.end()) {
383 // the cache clean request has already reached this xbar
384 respond_directly = true;
385 if (pkt->isWrite()) {
386 rsp_pkt = cmo_lookup->second;
387 assert(rsp_pkt);
388
389 // determine the destination
390 const auto route_lookup = routeTo.find(rsp_pkt->req);
391 assert(route_lookup != routeTo.end());
392 rsp_port_id = route_lookup->second;
393 assert(rsp_port_id != InvalidPortID);
394 assert(rsp_port_id < respLayers.size());
395 // remove the request from the routing table
396 routeTo.erase(route_lookup);
397 }
398 outstandingCMO.erase(cmo_lookup);
399 } else {
400 respond_directly = false;
401 outstandingCMO.emplace(pkt->id, deferred_rsp);
402 if (!pkt->isWrite()) {
403 assert(routeTo.find(pkt->req) == routeTo.end());
404 routeTo[pkt->req] = slave_port_id;
405
406 panic_if(routeTo.size() > 512,
407 "Routing table exceeds 512 packets\n");
408 }
409 }
410 }
411
412
345 if (respond_directly) {
413 if (respond_directly) {
346 assert(pkt->needsResponse());
414 assert(rsp_pkt->needsResponse());
347 assert(success);
348
415 assert(success);
416
349 pkt->makeResponse();
417 rsp_pkt->makeResponse();
350
351 if (snoopFilter && !system->bypassCaches()) {
352 // let the snoop filter inspect the response and update its state
418
419 if (snoopFilter && !system->bypassCaches()) {
420 // let the snoop filter inspect the response and update its state
353 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
421 snoopFilter->updateResponse(rsp_pkt, *slavePorts[rsp_port_id]);
354 }
355
422 }
423
424 // we send the response after the current packet, even if the
425 // response is not for this packet (e.g. cache clean operation
426 // where both the request and the write packet have to cross
427 // the destination xbar before the response is sent.)
356 Tick response_time = clockEdge() + pkt->headerDelay;
428 Tick response_time = clockEdge() + pkt->headerDelay;
357 pkt->headerDelay = 0;
429 rsp_pkt->headerDelay = 0;
358
430
359 slavePorts[slave_port_id]->schedTimingResp(pkt, response_time);
431 slavePorts[rsp_port_id]->schedTimingResp(rsp_pkt, response_time);
360 }
361
362 return success;
363}
364
365bool
366CoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
367{
368 // determine the source port based on the id
369 MasterPort *src_port = masterPorts[master_port_id];
370
371 // determine the destination
372 const auto route_lookup = routeTo.find(pkt->req);
373 assert(route_lookup != routeTo.end());
374 const PortID slave_port_id = route_lookup->second;
375 assert(slave_port_id != InvalidPortID);
376 assert(slave_port_id < respLayers.size());
377
378 // test if the crossbar should be considered occupied for the
379 // current port
380 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
381 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
382 src_port->name(), pkt->print());
383 return false;
384 }
385
386 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
387 src_port->name(), pkt->print());
388
389 // store size and command as they might be modified when
390 // forwarding the packet
391 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
392 unsigned int pkt_cmd = pkt->cmdToIndex();
393
394 // a response sees the response latency
395 Tick xbar_delay = responseLatency * clockPeriod();
396
397 // set the packet header and payload delay
398 calcPacketTiming(pkt, xbar_delay);
399
400 // determine how long to be crossbar layer is busy
401 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
402
403 if (snoopFilter && !system->bypassCaches()) {
404 // let the snoop filter inspect the response and update its state
405 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
406 }
407
408 // send the packet through the destination slave port and pay for
409 // any outstanding header delay
410 Tick latency = pkt->headerDelay;
411 pkt->headerDelay = 0;
412 slavePorts[slave_port_id]->schedTimingResp(pkt, curTick() + latency);
413
414 // remove the request from the routing table
415 routeTo.erase(route_lookup);
416
417 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
418
419 // stats updates
420 pktCount[slave_port_id][master_port_id]++;
421 pktSize[slave_port_id][master_port_id] += pkt_size;
422 transDist[pkt_cmd]++;
423
424 return true;
425}
426
427void
428CoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
429{
430 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
431 masterPorts[master_port_id]->name(), pkt->print());
432
433 // update stats here as we know the forwarding will succeed
434 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
435 transDist[pkt->cmdToIndex()]++;
436 snoops++;
437 snoopTraffic += pkt_size;
438
439 // we should only see express snoops from caches
440 assert(pkt->isExpressSnoop());
441
442 // set the packet header and payload delay, for now use forward latency
443 // @todo Assess the choice of latency further
444 calcPacketTiming(pkt, forwardLatency * clockPeriod());
445
446 // remember if a cache has already committed to responding so we
447 // can see if it changes during the snooping
448 const bool cache_responding = pkt->cacheResponding();
449
450 assert(pkt->snoopDelay == 0);
451
452 if (snoopFilter) {
453 // let the Snoop Filter work its magic and guide probing
454 auto sf_res = snoopFilter->lookupSnoop(pkt);
455 // the time required by a packet to be delivered through
456 // the xbar has to be charged also with to lookup latency
457 // of the snoop filter
458 pkt->headerDelay += sf_res.second * clockPeriod();
459 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
460 __func__, masterPorts[master_port_id]->name(), pkt->print(),
461 sf_res.first.size(), sf_res.second);
462
463 // forward to all snoopers
464 forwardTiming(pkt, InvalidPortID, sf_res.first);
465 } else {
466 forwardTiming(pkt, InvalidPortID);
467 }
468
469 // add the snoop delay to our header delay, and then reset it
470 pkt->headerDelay += pkt->snoopDelay;
471 pkt->snoopDelay = 0;
472
473 // if we can expect a response, remember how to route it
474 if (!cache_responding && pkt->cacheResponding()) {
475 assert(routeTo.find(pkt->req) == routeTo.end());
476 routeTo[pkt->req] = master_port_id;
477 }
478
479 // a snoop request came from a connected slave device (one of
480 // our master ports), and if it is not coming from the slave
481 // device responsible for the address range something is
482 // wrong, hence there is nothing further to do as the packet
483 // would be going back to where it came from
484 assert(master_port_id == findPort(pkt->getAddr()));
485}
486
487bool
488CoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
489{
490 // determine the source port based on the id
491 SlavePort* src_port = slavePorts[slave_port_id];
492
493 // get the destination
494 const auto route_lookup = routeTo.find(pkt->req);
495 assert(route_lookup != routeTo.end());
496 const PortID dest_port_id = route_lookup->second;
497 assert(dest_port_id != InvalidPortID);
498
499 // determine if the response is from a snoop request we
500 // created as the result of a normal request (in which case it
501 // should be in the outstandingSnoop), or if we merely forwarded
502 // someone else's snoop request
503 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
504 outstandingSnoop.end();
505
506 // test if the crossbar should be considered occupied for the
507 // current port, note that the check is bypassed if the response
508 // is being passed on as a normal response since this is occupying
509 // the response layer rather than the snoop response layer
510 if (forwardAsSnoop) {
511 assert(dest_port_id < snoopLayers.size());
512 if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
513 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
514 src_port->name(), pkt->print());
515 return false;
516 }
517 } else {
518 // get the master port that mirrors this slave port internally
519 MasterPort* snoop_port = snoopRespPorts[slave_port_id];
520 assert(dest_port_id < respLayers.size());
521 if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
522 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
523 snoop_port->name(), pkt->print());
524 return false;
525 }
526 }
527
528 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
529 src_port->name(), pkt->print());
530
531 // store size and command as they might be modified when
532 // forwarding the packet
533 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
534 unsigned int pkt_cmd = pkt->cmdToIndex();
535
536 // responses are never express snoops
537 assert(!pkt->isExpressSnoop());
538
539 // a snoop response sees the snoop response latency, and if it is
540 // forwarded as a normal response, the response latency
541 Tick xbar_delay =
542 (forwardAsSnoop ? snoopResponseLatency : responseLatency) *
543 clockPeriod();
544
545 // set the packet header and payload delay
546 calcPacketTiming(pkt, xbar_delay);
547
548 // determine how long to be crossbar layer is busy
549 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
550
551 // forward it either as a snoop response or a normal response
552 if (forwardAsSnoop) {
553 // this is a snoop response to a snoop request we forwarded,
554 // e.g. coming from the L1 and going to the L2, and it should
555 // be forwarded as a snoop response
556
557 if (snoopFilter) {
558 // update the probe filter so that it can properly track the line
559 snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
560 *masterPorts[dest_port_id]);
561 }
562
563 bool success M5_VAR_USED =
564 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
565 pktCount[slave_port_id][dest_port_id]++;
566 pktSize[slave_port_id][dest_port_id] += pkt_size;
567 assert(success);
568
569 snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
570 } else {
571 // we got a snoop response on one of our slave ports,
572 // i.e. from a coherent master connected to the crossbar, and
573 // since we created the snoop request as part of recvTiming,
574 // this should now be a normal response again
575 outstandingSnoop.erase(pkt->req);
576
577 // this is a snoop response from a coherent master, hence it
578 // should never go back to where the snoop response came from,
579 // but instead to where the original request came from
580 assert(slave_port_id != dest_port_id);
581
582 if (snoopFilter) {
583 // update the probe filter so that it can properly track the line
584 snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
585 *slavePorts[dest_port_id]);
586 }
587
588 DPRINTF(CoherentXBar, "%s: src %s packet %s FWD RESP\n", __func__,
589 src_port->name(), pkt->print());
590
591 // as a normal response, it should go back to a master through
592 // one of our slave ports, we also pay for any outstanding
593 // header latency
594 Tick latency = pkt->headerDelay;
595 pkt->headerDelay = 0;
596 slavePorts[dest_port_id]->schedTimingResp(pkt, curTick() + latency);
597
598 respLayers[dest_port_id]->succeededTiming(packetFinishTime);
599 }
600
601 // remove the request from the routing table
602 routeTo.erase(route_lookup);
603
604 // stats updates
605 transDist[pkt_cmd]++;
606 snoops++;
607 snoopTraffic += pkt_size;
608
609 return true;
610}
611
612
613void
614CoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
615 const std::vector<QueuedSlavePort*>& dests)
616{
617 DPRINTF(CoherentXBar, "%s for %s\n", __func__, pkt->print());
618
619 // snoops should only happen if the system isn't bypassing caches
620 assert(!system->bypassCaches());
621
622 unsigned fanout = 0;
623
624 for (const auto& p: dests) {
625 // we could have gotten this request from a snooping master
626 // (corresponding to our own slave port that is also in
627 // snoopPorts) and should not send it back to where it came
628 // from
629 if (exclude_slave_port_id == InvalidPortID ||
630 p->getId() != exclude_slave_port_id) {
631 // cache is not allowed to refuse snoop
632 p->sendTimingSnoopReq(pkt);
633 fanout++;
634 }
635 }
636
637 // Stats for fanout of this forward operation
638 snoopFanout.sample(fanout);
639}
640
641void
642CoherentXBar::recvReqRetry(PortID master_port_id)
643{
644 // responses and snoop responses never block on forwarding them,
645 // so the retry will always be coming from a port to which we
646 // tried to forward a request
647 reqLayers[master_port_id]->recvRetry();
648}
649
650Tick
651CoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
652{
653 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
654 slavePorts[slave_port_id]->name(), pkt->print());
655
656 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
657 unsigned int pkt_cmd = pkt->cmdToIndex();
658
659 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
660 Tick snoop_response_latency = 0;
661
662 // is this the destination point for this packet? (e.g. true if
663 // this xbar is the PoC for a cache maintenance operation to the
664 // PoC) otherwise the destination is any cache that can satisfy
665 // the request
666 const bool is_destination = isDestination(pkt);
667
668 const bool snoop_caches = !system->bypassCaches() &&
669 pkt->cmd != MemCmd::WriteClean;
670 if (snoop_caches) {
671 // forward to all snoopers but the source
672 std::pair<MemCmd, Tick> snoop_result;
673 if (snoopFilter) {
674 // check with the snoop filter where to forward this packet
675 auto sf_res =
676 snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
677 snoop_response_latency += sf_res.second * clockPeriod();
678 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
679 __func__, slavePorts[slave_port_id]->name(), pkt->print(),
680 sf_res.first.size(), sf_res.second);
681
682 // let the snoop filter know about the success of the send
683 // operation, and do it even before sending it onwards to
684 // avoid situations where atomic upward snoops sneak in
685 // between and change the filter state
686 snoopFilter->finishRequest(false, pkt->getAddr(), pkt->isSecure());
687
688 if (pkt->isEviction()) {
689 // for block-evicting packets, i.e. writebacks and
690 // clean evictions, there is no need to snoop up, as
691 // all we do is determine if the block is cached or
692 // not, instead just set it here based on the snoop
693 // filter result
694 if (!sf_res.first.empty())
695 pkt->setBlockCached();
696 } else {
697 snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
698 sf_res.first);
699 }
700 } else {
701 snoop_result = forwardAtomic(pkt, slave_port_id);
702 }
703 snoop_response_cmd = snoop_result.first;
704 snoop_response_latency += snoop_result.second;
705 }
706
707 // set up a sensible default value
708 Tick response_latency = 0;
709
710 const bool sink_packet = sinkPacket(pkt);
711
712 // even if we had a snoop response, we must continue and also
713 // perform the actual request at the destination
714 PortID master_port_id = findPort(pkt->getAddr());
715
716 if (sink_packet) {
717 DPRINTF(CoherentXBar, "%s: Not forwarding %s\n", __func__,
718 pkt->print());
719 } else {
720 if (forwardPacket(pkt)) {
721 // make sure that the write request (e.g., WriteClean)
722 // will stop at the memory below if this crossbar is its
723 // destination
724 if (pkt->isWrite() && is_destination) {
725 pkt->clearWriteThrough();
726 }
727
728 // forward the request to the appropriate destination
729 response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
730 } else {
731 // if it does not need a response we sink the packet above
732 assert(pkt->needsResponse());
733
734 pkt->makeResponse();
735 }
736 }
737
738 // stats updates for the request
739 pktCount[slave_port_id][master_port_id]++;
740 pktSize[slave_port_id][master_port_id] += pkt_size;
741 transDist[pkt_cmd]++;
742
743
744 // if lower levels have replied, tell the snoop filter
745 if (!system->bypassCaches() && snoopFilter && pkt->isResponse()) {
746 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
747 }
748
749 // if we got a response from a snooper, restore it here
750 if (snoop_response_cmd != MemCmd::InvalidCmd) {
751 // no one else should have responded
752 assert(!pkt->isResponse());
753 pkt->cmd = snoop_response_cmd;
754 response_latency = snoop_response_latency;
755 }
756
432 }
433
434 return success;
435}
436
437bool
438CoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
439{
440 // determine the source port based on the id
441 MasterPort *src_port = masterPorts[master_port_id];
442
443 // determine the destination
444 const auto route_lookup = routeTo.find(pkt->req);
445 assert(route_lookup != routeTo.end());
446 const PortID slave_port_id = route_lookup->second;
447 assert(slave_port_id != InvalidPortID);
448 assert(slave_port_id < respLayers.size());
449
450 // test if the crossbar should be considered occupied for the
451 // current port
452 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
453 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
454 src_port->name(), pkt->print());
455 return false;
456 }
457
458 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
459 src_port->name(), pkt->print());
460
461 // store size and command as they might be modified when
462 // forwarding the packet
463 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
464 unsigned int pkt_cmd = pkt->cmdToIndex();
465
466 // a response sees the response latency
467 Tick xbar_delay = responseLatency * clockPeriod();
468
469 // set the packet header and payload delay
470 calcPacketTiming(pkt, xbar_delay);
471
472 // determine how long to be crossbar layer is busy
473 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
474
475 if (snoopFilter && !system->bypassCaches()) {
476 // let the snoop filter inspect the response and update its state
477 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
478 }
479
480 // send the packet through the destination slave port and pay for
481 // any outstanding header delay
482 Tick latency = pkt->headerDelay;
483 pkt->headerDelay = 0;
484 slavePorts[slave_port_id]->schedTimingResp(pkt, curTick() + latency);
485
486 // remove the request from the routing table
487 routeTo.erase(route_lookup);
488
489 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
490
491 // stats updates
492 pktCount[slave_port_id][master_port_id]++;
493 pktSize[slave_port_id][master_port_id] += pkt_size;
494 transDist[pkt_cmd]++;
495
496 return true;
497}
498
499void
500CoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
501{
502 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
503 masterPorts[master_port_id]->name(), pkt->print());
504
505 // update stats here as we know the forwarding will succeed
506 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
507 transDist[pkt->cmdToIndex()]++;
508 snoops++;
509 snoopTraffic += pkt_size;
510
511 // we should only see express snoops from caches
512 assert(pkt->isExpressSnoop());
513
514 // set the packet header and payload delay, for now use forward latency
515 // @todo Assess the choice of latency further
516 calcPacketTiming(pkt, forwardLatency * clockPeriod());
517
518 // remember if a cache has already committed to responding so we
519 // can see if it changes during the snooping
520 const bool cache_responding = pkt->cacheResponding();
521
522 assert(pkt->snoopDelay == 0);
523
524 if (snoopFilter) {
525 // let the Snoop Filter work its magic and guide probing
526 auto sf_res = snoopFilter->lookupSnoop(pkt);
527 // the time required by a packet to be delivered through
528 // the xbar has to be charged also with to lookup latency
529 // of the snoop filter
530 pkt->headerDelay += sf_res.second * clockPeriod();
531 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
532 __func__, masterPorts[master_port_id]->name(), pkt->print(),
533 sf_res.first.size(), sf_res.second);
534
535 // forward to all snoopers
536 forwardTiming(pkt, InvalidPortID, sf_res.first);
537 } else {
538 forwardTiming(pkt, InvalidPortID);
539 }
540
541 // add the snoop delay to our header delay, and then reset it
542 pkt->headerDelay += pkt->snoopDelay;
543 pkt->snoopDelay = 0;
544
545 // if we can expect a response, remember how to route it
546 if (!cache_responding && pkt->cacheResponding()) {
547 assert(routeTo.find(pkt->req) == routeTo.end());
548 routeTo[pkt->req] = master_port_id;
549 }
550
551 // a snoop request came from a connected slave device (one of
552 // our master ports), and if it is not coming from the slave
553 // device responsible for the address range something is
554 // wrong, hence there is nothing further to do as the packet
555 // would be going back to where it came from
556 assert(master_port_id == findPort(pkt->getAddr()));
557}
558
559bool
560CoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
561{
562 // determine the source port based on the id
563 SlavePort* src_port = slavePorts[slave_port_id];
564
565 // get the destination
566 const auto route_lookup = routeTo.find(pkt->req);
567 assert(route_lookup != routeTo.end());
568 const PortID dest_port_id = route_lookup->second;
569 assert(dest_port_id != InvalidPortID);
570
571 // determine if the response is from a snoop request we
572 // created as the result of a normal request (in which case it
573 // should be in the outstandingSnoop), or if we merely forwarded
574 // someone else's snoop request
575 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
576 outstandingSnoop.end();
577
578 // test if the crossbar should be considered occupied for the
579 // current port, note that the check is bypassed if the response
580 // is being passed on as a normal response since this is occupying
581 // the response layer rather than the snoop response layer
582 if (forwardAsSnoop) {
583 assert(dest_port_id < snoopLayers.size());
584 if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
585 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
586 src_port->name(), pkt->print());
587 return false;
588 }
589 } else {
590 // get the master port that mirrors this slave port internally
591 MasterPort* snoop_port = snoopRespPorts[slave_port_id];
592 assert(dest_port_id < respLayers.size());
593 if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
594 DPRINTF(CoherentXBar, "%s: src %s packet %s BUSY\n", __func__,
595 snoop_port->name(), pkt->print());
596 return false;
597 }
598 }
599
600 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
601 src_port->name(), pkt->print());
602
603 // store size and command as they might be modified when
604 // forwarding the packet
605 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
606 unsigned int pkt_cmd = pkt->cmdToIndex();
607
608 // responses are never express snoops
609 assert(!pkt->isExpressSnoop());
610
611 // a snoop response sees the snoop response latency, and if it is
612 // forwarded as a normal response, the response latency
613 Tick xbar_delay =
614 (forwardAsSnoop ? snoopResponseLatency : responseLatency) *
615 clockPeriod();
616
617 // set the packet header and payload delay
618 calcPacketTiming(pkt, xbar_delay);
619
620 // determine how long to be crossbar layer is busy
621 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
622
623 // forward it either as a snoop response or a normal response
624 if (forwardAsSnoop) {
625 // this is a snoop response to a snoop request we forwarded,
626 // e.g. coming from the L1 and going to the L2, and it should
627 // be forwarded as a snoop response
628
629 if (snoopFilter) {
630 // update the probe filter so that it can properly track the line
631 snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
632 *masterPorts[dest_port_id]);
633 }
634
635 bool success M5_VAR_USED =
636 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
637 pktCount[slave_port_id][dest_port_id]++;
638 pktSize[slave_port_id][dest_port_id] += pkt_size;
639 assert(success);
640
641 snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
642 } else {
643 // we got a snoop response on one of our slave ports,
644 // i.e. from a coherent master connected to the crossbar, and
645 // since we created the snoop request as part of recvTiming,
646 // this should now be a normal response again
647 outstandingSnoop.erase(pkt->req);
648
649 // this is a snoop response from a coherent master, hence it
650 // should never go back to where the snoop response came from,
651 // but instead to where the original request came from
652 assert(slave_port_id != dest_port_id);
653
654 if (snoopFilter) {
655 // update the probe filter so that it can properly track the line
656 snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
657 *slavePorts[dest_port_id]);
658 }
659
660 DPRINTF(CoherentXBar, "%s: src %s packet %s FWD RESP\n", __func__,
661 src_port->name(), pkt->print());
662
663 // as a normal response, it should go back to a master through
664 // one of our slave ports, we also pay for any outstanding
665 // header latency
666 Tick latency = pkt->headerDelay;
667 pkt->headerDelay = 0;
668 slavePorts[dest_port_id]->schedTimingResp(pkt, curTick() + latency);
669
670 respLayers[dest_port_id]->succeededTiming(packetFinishTime);
671 }
672
673 // remove the request from the routing table
674 routeTo.erase(route_lookup);
675
676 // stats updates
677 transDist[pkt_cmd]++;
678 snoops++;
679 snoopTraffic += pkt_size;
680
681 return true;
682}
683
684
685void
686CoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
687 const std::vector<QueuedSlavePort*>& dests)
688{
689 DPRINTF(CoherentXBar, "%s for %s\n", __func__, pkt->print());
690
691 // snoops should only happen if the system isn't bypassing caches
692 assert(!system->bypassCaches());
693
694 unsigned fanout = 0;
695
696 for (const auto& p: dests) {
697 // we could have gotten this request from a snooping master
698 // (corresponding to our own slave port that is also in
699 // snoopPorts) and should not send it back to where it came
700 // from
701 if (exclude_slave_port_id == InvalidPortID ||
702 p->getId() != exclude_slave_port_id) {
703 // cache is not allowed to refuse snoop
704 p->sendTimingSnoopReq(pkt);
705 fanout++;
706 }
707 }
708
709 // Stats for fanout of this forward operation
710 snoopFanout.sample(fanout);
711}
712
713void
714CoherentXBar::recvReqRetry(PortID master_port_id)
715{
716 // responses and snoop responses never block on forwarding them,
717 // so the retry will always be coming from a port to which we
718 // tried to forward a request
719 reqLayers[master_port_id]->recvRetry();
720}
721
722Tick
723CoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
724{
725 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
726 slavePorts[slave_port_id]->name(), pkt->print());
727
728 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
729 unsigned int pkt_cmd = pkt->cmdToIndex();
730
731 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
732 Tick snoop_response_latency = 0;
733
734 // is this the destination point for this packet? (e.g. true if
735 // this xbar is the PoC for a cache maintenance operation to the
736 // PoC) otherwise the destination is any cache that can satisfy
737 // the request
738 const bool is_destination = isDestination(pkt);
739
740 const bool snoop_caches = !system->bypassCaches() &&
741 pkt->cmd != MemCmd::WriteClean;
742 if (snoop_caches) {
743 // forward to all snoopers but the source
744 std::pair<MemCmd, Tick> snoop_result;
745 if (snoopFilter) {
746 // check with the snoop filter where to forward this packet
747 auto sf_res =
748 snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
749 snoop_response_latency += sf_res.second * clockPeriod();
750 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
751 __func__, slavePorts[slave_port_id]->name(), pkt->print(),
752 sf_res.first.size(), sf_res.second);
753
754 // let the snoop filter know about the success of the send
755 // operation, and do it even before sending it onwards to
756 // avoid situations where atomic upward snoops sneak in
757 // between and change the filter state
758 snoopFilter->finishRequest(false, pkt->getAddr(), pkt->isSecure());
759
760 if (pkt->isEviction()) {
761 // for block-evicting packets, i.e. writebacks and
762 // clean evictions, there is no need to snoop up, as
763 // all we do is determine if the block is cached or
764 // not, instead just set it here based on the snoop
765 // filter result
766 if (!sf_res.first.empty())
767 pkt->setBlockCached();
768 } else {
769 snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
770 sf_res.first);
771 }
772 } else {
773 snoop_result = forwardAtomic(pkt, slave_port_id);
774 }
775 snoop_response_cmd = snoop_result.first;
776 snoop_response_latency += snoop_result.second;
777 }
778
779 // set up a sensible default value
780 Tick response_latency = 0;
781
782 const bool sink_packet = sinkPacket(pkt);
783
784 // even if we had a snoop response, we must continue and also
785 // perform the actual request at the destination
786 PortID master_port_id = findPort(pkt->getAddr());
787
788 if (sink_packet) {
789 DPRINTF(CoherentXBar, "%s: Not forwarding %s\n", __func__,
790 pkt->print());
791 } else {
792 if (forwardPacket(pkt)) {
793 // make sure that the write request (e.g., WriteClean)
794 // will stop at the memory below if this crossbar is its
795 // destination
796 if (pkt->isWrite() && is_destination) {
797 pkt->clearWriteThrough();
798 }
799
800 // forward the request to the appropriate destination
801 response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
802 } else {
803 // if it does not need a response we sink the packet above
804 assert(pkt->needsResponse());
805
806 pkt->makeResponse();
807 }
808 }
809
810 // stats updates for the request
811 pktCount[slave_port_id][master_port_id]++;
812 pktSize[slave_port_id][master_port_id] += pkt_size;
813 transDist[pkt_cmd]++;
814
815
816 // if lower levels have replied, tell the snoop filter
817 if (!system->bypassCaches() && snoopFilter && pkt->isResponse()) {
818 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
819 }
820
821 // if we got a response from a snooper, restore it here
822 if (snoop_response_cmd != MemCmd::InvalidCmd) {
823 // no one else should have responded
824 assert(!pkt->isResponse());
825 pkt->cmd = snoop_response_cmd;
826 response_latency = snoop_response_latency;
827 }
828
829 // If this is the destination of the cache clean operation the
830 // crossbar is responsible for responding. This crossbar will
831 // respond when the cache clean is complete. An atomic cache clean
832 // is complete when the crossbars receives the cache clean
833 // request (CleanSharedReq, CleanInvalidReq), as either:
834 // * no cache above had a dirty copy of the block as indicated by
835 // the satisfied flag of the packet, or
836 // * the crossbar has already seen the corresponding write
837 // (WriteClean) which updates the block in the memory below.
838 if (pkt->isClean() && isDestination(pkt) && pkt->satisfied()) {
839 auto it = outstandingCMO.find(pkt->id);
840 assert(it != outstandingCMO.end());
841 // we are responding right away
842 outstandingCMO.erase(it);
843 } else if (pkt->cmd == MemCmd::WriteClean && isDestination(pkt)) {
844 // if this is the destination of the operation, the xbar
845 // sends the responce to the cache clean operation only
846 // after having encountered the cache clean request
847 auto M5_VAR_USED ret = outstandingCMO.emplace(pkt->id, nullptr);
848 // in atomic mode we know that the WriteClean packet should
849 // precede the clean request
850 assert(ret.second);
851 }
852
757 // add the response data
758 if (pkt->isResponse()) {
759 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
760 pkt_cmd = pkt->cmdToIndex();
761
762 // stats updates
763 pktCount[slave_port_id][master_port_id]++;
764 pktSize[slave_port_id][master_port_id] += pkt_size;
765 transDist[pkt_cmd]++;
766 }
767
768 // @todo: Not setting header time
769 pkt->payloadDelay = response_latency;
770 return response_latency;
771}
772
773Tick
774CoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
775{
776 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
777 masterPorts[master_port_id]->name(), pkt->print());
778
779 // add the request snoop data
780 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
781 snoops++;
782 snoopTraffic += pkt_size;
783
784 // forward to all snoopers
785 std::pair<MemCmd, Tick> snoop_result;
786 Tick snoop_response_latency = 0;
787 if (snoopFilter) {
788 auto sf_res = snoopFilter->lookupSnoop(pkt);
789 snoop_response_latency += sf_res.second * clockPeriod();
790 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
791 __func__, masterPorts[master_port_id]->name(), pkt->print(),
792 sf_res.first.size(), sf_res.second);
793 snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
794 sf_res.first);
795 } else {
796 snoop_result = forwardAtomic(pkt, InvalidPortID);
797 }
798 MemCmd snoop_response_cmd = snoop_result.first;
799 snoop_response_latency += snoop_result.second;
800
801 if (snoop_response_cmd != MemCmd::InvalidCmd)
802 pkt->cmd = snoop_response_cmd;
803
804 // add the response snoop data
805 if (pkt->isResponse()) {
806 snoops++;
807 }
808
809 // @todo: Not setting header time
810 pkt->payloadDelay = snoop_response_latency;
811 return snoop_response_latency;
812}
813
814std::pair<MemCmd, Tick>
815CoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
816 PortID source_master_port_id,
817 const std::vector<QueuedSlavePort*>& dests)
818{
819 // the packet may be changed on snoops, record the original
820 // command to enable us to restore it between snoops so that
821 // additional snoops can take place properly
822 MemCmd orig_cmd = pkt->cmd;
823 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
824 Tick snoop_response_latency = 0;
825
826 // snoops should only happen if the system isn't bypassing caches
827 assert(!system->bypassCaches());
828
829 unsigned fanout = 0;
830
831 for (const auto& p: dests) {
832 // we could have gotten this request from a snooping master
833 // (corresponding to our own slave port that is also in
834 // snoopPorts) and should not send it back to where it came
835 // from
836 if (exclude_slave_port_id != InvalidPortID &&
837 p->getId() == exclude_slave_port_id)
838 continue;
839
840 Tick latency = p->sendAtomicSnoop(pkt);
841 fanout++;
842
843 // in contrast to a functional access, we have to keep on
844 // going as all snoopers must be updated even if we get a
845 // response
846 if (!pkt->isResponse())
847 continue;
848
849 // response from snoop agent
850 assert(pkt->cmd != orig_cmd);
851 assert(pkt->cacheResponding());
852 // should only happen once
853 assert(snoop_response_cmd == MemCmd::InvalidCmd);
854 // save response state
855 snoop_response_cmd = pkt->cmd;
856 snoop_response_latency = latency;
857
858 if (snoopFilter) {
859 // Handle responses by the snoopers and differentiate between
860 // responses to requests from above and snoops from below
861 if (source_master_port_id != InvalidPortID) {
862 // Getting a response for a snoop from below
863 assert(exclude_slave_port_id == InvalidPortID);
864 snoopFilter->updateSnoopForward(pkt, *p,
865 *masterPorts[source_master_port_id]);
866 } else {
867 // Getting a response for a request from above
868 assert(source_master_port_id == InvalidPortID);
869 snoopFilter->updateSnoopResponse(pkt, *p,
870 *slavePorts[exclude_slave_port_id]);
871 }
872 }
873 // restore original packet state for remaining snoopers
874 pkt->cmd = orig_cmd;
875 }
876
877 // Stats for fanout
878 snoopFanout.sample(fanout);
879
880 // the packet is restored as part of the loop and any potential
881 // snoop response is part of the returned pair
882 return std::make_pair(snoop_response_cmd, snoop_response_latency);
883}
884
885void
886CoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
887{
888 if (!pkt->isPrint()) {
889 // don't do DPRINTFs on PrintReq as it clutters up the output
890 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
891 slavePorts[slave_port_id]->name(), pkt->print());
892 }
893
894 if (!system->bypassCaches()) {
895 // forward to all snoopers but the source
896 forwardFunctional(pkt, slave_port_id);
897 }
898
899 // there is no need to continue if the snooping has found what we
900 // were looking for and the packet is already a response
901 if (!pkt->isResponse()) {
902 // since our slave ports are queued ports we need to check them as well
903 for (const auto& p : slavePorts) {
904 // if we find a response that has the data, then the
905 // downstream caches/memories may be out of date, so simply stop
906 // here
907 if (p->checkFunctional(pkt)) {
908 if (pkt->needsResponse())
909 pkt->makeResponse();
910 return;
911 }
912 }
913
914 PortID dest_id = findPort(pkt->getAddr());
915
916 masterPorts[dest_id]->sendFunctional(pkt);
917 }
918}
919
920void
921CoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
922{
923 if (!pkt->isPrint()) {
924 // don't do DPRINTFs on PrintReq as it clutters up the output
925 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
926 masterPorts[master_port_id]->name(), pkt->print());
927 }
928
929 for (const auto& p : slavePorts) {
930 if (p->checkFunctional(pkt)) {
931 if (pkt->needsResponse())
932 pkt->makeResponse();
933 return;
934 }
935 }
936
937 // forward to all snoopers
938 forwardFunctional(pkt, InvalidPortID);
939}
940
941void
942CoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
943{
944 // snoops should only happen if the system isn't bypassing caches
945 assert(!system->bypassCaches());
946
947 for (const auto& p: snoopPorts) {
948 // we could have gotten this request from a snooping master
949 // (corresponding to our own slave port that is also in
950 // snoopPorts) and should not send it back to where it came
951 // from
952 if (exclude_slave_port_id == InvalidPortID ||
953 p->getId() != exclude_slave_port_id)
954 p->sendFunctionalSnoop(pkt);
955
956 // if we get a response we are done
957 if (pkt->isResponse()) {
958 break;
959 }
960 }
961}
962
963bool
964CoherentXBar::sinkPacket(const PacketPtr pkt) const
965{
966 // we can sink the packet if:
967 // 1) the crossbar is the point of coherency, and a cache is
968 // responding after being snooped
969 // 2) the crossbar is the point of coherency, and the packet is a
970 // coherency packet (not a read or a write) that does not
971 // require a response
972 // 3) this is a clean evict or clean writeback, but the packet is
973 // found in a cache above this crossbar
974 // 4) a cache is responding after being snooped, and the packet
975 // either does not need the block to be writable, or the cache
976 // that has promised to respond (setting the cache responding
977 // flag) is providing writable and thus had a Modified block,
978 // and no further action is needed
979 return (pointOfCoherency && pkt->cacheResponding()) ||
980 (pointOfCoherency && !(pkt->isRead() || pkt->isWrite()) &&
981 !pkt->needsResponse()) ||
982 (pkt->isCleanEviction() && pkt->isBlockCached()) ||
983 (pkt->cacheResponding() &&
984 (!pkt->needsWritable() || pkt->responderHadWritable()));
985}
986
987bool
988CoherentXBar::forwardPacket(const PacketPtr pkt)
989{
990 // we are forwarding the packet if:
853 // add the response data
854 if (pkt->isResponse()) {
855 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
856 pkt_cmd = pkt->cmdToIndex();
857
858 // stats updates
859 pktCount[slave_port_id][master_port_id]++;
860 pktSize[slave_port_id][master_port_id] += pkt_size;
861 transDist[pkt_cmd]++;
862 }
863
864 // @todo: Not setting header time
865 pkt->payloadDelay = response_latency;
866 return response_latency;
867}
868
869Tick
870CoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
871{
872 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
873 masterPorts[master_port_id]->name(), pkt->print());
874
875 // add the request snoop data
876 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
877 snoops++;
878 snoopTraffic += pkt_size;
879
880 // forward to all snoopers
881 std::pair<MemCmd, Tick> snoop_result;
882 Tick snoop_response_latency = 0;
883 if (snoopFilter) {
884 auto sf_res = snoopFilter->lookupSnoop(pkt);
885 snoop_response_latency += sf_res.second * clockPeriod();
886 DPRINTF(CoherentXBar, "%s: src %s packet %s SF size: %i lat: %i\n",
887 __func__, masterPorts[master_port_id]->name(), pkt->print(),
888 sf_res.first.size(), sf_res.second);
889 snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
890 sf_res.first);
891 } else {
892 snoop_result = forwardAtomic(pkt, InvalidPortID);
893 }
894 MemCmd snoop_response_cmd = snoop_result.first;
895 snoop_response_latency += snoop_result.second;
896
897 if (snoop_response_cmd != MemCmd::InvalidCmd)
898 pkt->cmd = snoop_response_cmd;
899
900 // add the response snoop data
901 if (pkt->isResponse()) {
902 snoops++;
903 }
904
905 // @todo: Not setting header time
906 pkt->payloadDelay = snoop_response_latency;
907 return snoop_response_latency;
908}
909
910std::pair<MemCmd, Tick>
911CoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
912 PortID source_master_port_id,
913 const std::vector<QueuedSlavePort*>& dests)
914{
915 // the packet may be changed on snoops, record the original
916 // command to enable us to restore it between snoops so that
917 // additional snoops can take place properly
918 MemCmd orig_cmd = pkt->cmd;
919 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
920 Tick snoop_response_latency = 0;
921
922 // snoops should only happen if the system isn't bypassing caches
923 assert(!system->bypassCaches());
924
925 unsigned fanout = 0;
926
927 for (const auto& p: dests) {
928 // we could have gotten this request from a snooping master
929 // (corresponding to our own slave port that is also in
930 // snoopPorts) and should not send it back to where it came
931 // from
932 if (exclude_slave_port_id != InvalidPortID &&
933 p->getId() == exclude_slave_port_id)
934 continue;
935
936 Tick latency = p->sendAtomicSnoop(pkt);
937 fanout++;
938
939 // in contrast to a functional access, we have to keep on
940 // going as all snoopers must be updated even if we get a
941 // response
942 if (!pkt->isResponse())
943 continue;
944
945 // response from snoop agent
946 assert(pkt->cmd != orig_cmd);
947 assert(pkt->cacheResponding());
948 // should only happen once
949 assert(snoop_response_cmd == MemCmd::InvalidCmd);
950 // save response state
951 snoop_response_cmd = pkt->cmd;
952 snoop_response_latency = latency;
953
954 if (snoopFilter) {
955 // Handle responses by the snoopers and differentiate between
956 // responses to requests from above and snoops from below
957 if (source_master_port_id != InvalidPortID) {
958 // Getting a response for a snoop from below
959 assert(exclude_slave_port_id == InvalidPortID);
960 snoopFilter->updateSnoopForward(pkt, *p,
961 *masterPorts[source_master_port_id]);
962 } else {
963 // Getting a response for a request from above
964 assert(source_master_port_id == InvalidPortID);
965 snoopFilter->updateSnoopResponse(pkt, *p,
966 *slavePorts[exclude_slave_port_id]);
967 }
968 }
969 // restore original packet state for remaining snoopers
970 pkt->cmd = orig_cmd;
971 }
972
973 // Stats for fanout
974 snoopFanout.sample(fanout);
975
976 // the packet is restored as part of the loop and any potential
977 // snoop response is part of the returned pair
978 return std::make_pair(snoop_response_cmd, snoop_response_latency);
979}
980
981void
982CoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
983{
984 if (!pkt->isPrint()) {
985 // don't do DPRINTFs on PrintReq as it clutters up the output
986 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
987 slavePorts[slave_port_id]->name(), pkt->print());
988 }
989
990 if (!system->bypassCaches()) {
991 // forward to all snoopers but the source
992 forwardFunctional(pkt, slave_port_id);
993 }
994
995 // there is no need to continue if the snooping has found what we
996 // were looking for and the packet is already a response
997 if (!pkt->isResponse()) {
998 // since our slave ports are queued ports we need to check them as well
999 for (const auto& p : slavePorts) {
1000 // if we find a response that has the data, then the
1001 // downstream caches/memories may be out of date, so simply stop
1002 // here
1003 if (p->checkFunctional(pkt)) {
1004 if (pkt->needsResponse())
1005 pkt->makeResponse();
1006 return;
1007 }
1008 }
1009
1010 PortID dest_id = findPort(pkt->getAddr());
1011
1012 masterPorts[dest_id]->sendFunctional(pkt);
1013 }
1014}
1015
1016void
1017CoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
1018{
1019 if (!pkt->isPrint()) {
1020 // don't do DPRINTFs on PrintReq as it clutters up the output
1021 DPRINTF(CoherentXBar, "%s: src %s packet %s\n", __func__,
1022 masterPorts[master_port_id]->name(), pkt->print());
1023 }
1024
1025 for (const auto& p : slavePorts) {
1026 if (p->checkFunctional(pkt)) {
1027 if (pkt->needsResponse())
1028 pkt->makeResponse();
1029 return;
1030 }
1031 }
1032
1033 // forward to all snoopers
1034 forwardFunctional(pkt, InvalidPortID);
1035}
1036
1037void
1038CoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
1039{
1040 // snoops should only happen if the system isn't bypassing caches
1041 assert(!system->bypassCaches());
1042
1043 for (const auto& p: snoopPorts) {
1044 // we could have gotten this request from a snooping master
1045 // (corresponding to our own slave port that is also in
1046 // snoopPorts) and should not send it back to where it came
1047 // from
1048 if (exclude_slave_port_id == InvalidPortID ||
1049 p->getId() != exclude_slave_port_id)
1050 p->sendFunctionalSnoop(pkt);
1051
1052 // if we get a response we are done
1053 if (pkt->isResponse()) {
1054 break;
1055 }
1056 }
1057}
1058
1059bool
1060CoherentXBar::sinkPacket(const PacketPtr pkt) const
1061{
1062 // we can sink the packet if:
1063 // 1) the crossbar is the point of coherency, and a cache is
1064 // responding after being snooped
1065 // 2) the crossbar is the point of coherency, and the packet is a
1066 // coherency packet (not a read or a write) that does not
1067 // require a response
1068 // 3) this is a clean evict or clean writeback, but the packet is
1069 // found in a cache above this crossbar
1070 // 4) a cache is responding after being snooped, and the packet
1071 // either does not need the block to be writable, or the cache
1072 // that has promised to respond (setting the cache responding
1073 // flag) is providing writable and thus had a Modified block,
1074 // and no further action is needed
1075 return (pointOfCoherency && pkt->cacheResponding()) ||
1076 (pointOfCoherency && !(pkt->isRead() || pkt->isWrite()) &&
1077 !pkt->needsResponse()) ||
1078 (pkt->isCleanEviction() && pkt->isBlockCached()) ||
1079 (pkt->cacheResponding() &&
1080 (!pkt->needsWritable() || pkt->responderHadWritable()));
1081}
1082
1083bool
1084CoherentXBar::forwardPacket(const PacketPtr pkt)
1085{
1086 // we are forwarding the packet if:
991 // 1) this is a read or a write
992 // 2) this crossbar is above the point of coherency
1087 // 1) this is a cache clean request to the PoU/PoC and this
1088 // crossbar is above the PoU/PoC
1089 // 2) this is a read or a write
1090 // 3) this crossbar is above the point of coherency
1091 if (pkt->isClean()) {
1092 return !isDestination(pkt);
1093 }
993 return pkt->isRead() || pkt->isWrite() || !pointOfCoherency;
994}
995
996
997void
998CoherentXBar::regStats()
999{
1000 // register the stats of the base class and our layers
1001 BaseXBar::regStats();
1002 for (auto l: reqLayers)
1003 l->regStats();
1004 for (auto l: respLayers)
1005 l->regStats();
1006 for (auto l: snoopLayers)
1007 l->regStats();
1008
1009 snoops
1010 .name(name() + ".snoops")
1011 .desc("Total snoops (count)")
1012 ;
1013
1014 snoopTraffic
1015 .name(name() + ".snoopTraffic")
1016 .desc("Total snoop traffic (bytes)")
1017 ;
1018
1019 snoopFanout
1020 .init(0, snoopPorts.size(), 1)
1021 .name(name() + ".snoop_fanout")
1022 .desc("Request fanout histogram")
1023 ;
1024}
1025
1026CoherentXBar *
1027CoherentXBarParams::create()
1028{
1029 return new CoherentXBar(this);
1030}
1094 return pkt->isRead() || pkt->isWrite() || !pointOfCoherency;
1095}
1096
1097
1098void
1099CoherentXBar::regStats()
1100{
1101 // register the stats of the base class and our layers
1102 BaseXBar::regStats();
1103 for (auto l: reqLayers)
1104 l->regStats();
1105 for (auto l: respLayers)
1106 l->regStats();
1107 for (auto l: snoopLayers)
1108 l->regStats();
1109
1110 snoops
1111 .name(name() + ".snoops")
1112 .desc("Total snoops (count)")
1113 ;
1114
1115 snoopTraffic
1116 .name(name() + ".snoopTraffic")
1117 .desc("Total snoop traffic (bytes)")
1118 ;
1119
1120 snoopFanout
1121 .init(0, snoopPorts.size(), 1)
1122 .name(name() + ".snoop_fanout")
1123 .desc("Request fanout histogram")
1124 ;
1125}
1126
1127CoherentXBar *
1128CoherentXBarParams::create()
1129{
1130 return new CoherentXBar(this);
1131}