mem: Make the requests carried by packets const
[gem5.git] / src / mem / noncoherent_xbar.cc
1 /*
2 * Copyright (c) 2011-2014 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 */
44
45 /**
46 * @file
47 * Definition of a non-coherent crossbar object.
48 */
49
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/NoncoherentXBar.hh"
53 #include "debug/XBar.hh"
54 #include "mem/noncoherent_xbar.hh"
55
56 NoncoherentXBar::NoncoherentXBar(const NoncoherentXBarParams *p)
57 : BaseXBar(p)
58 {
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.master[%d]", name(), i);
64 MasterPort* bp = new NoncoherentXBarMasterPort(portName, *this, i);
65 masterPorts.push_back(bp);
66 reqLayers.push_back(new ReqLayer(*bp, *this,
67 csprintf(".reqLayer%d", i)));
68 }
69
70 // see if we have a default slave device connected and if so add
71 // our corresponding master port
72 if (p->port_default_connection_count) {
73 defaultPortID = masterPorts.size();
74 std::string portName = name() + ".default";
75 MasterPort* bp = new NoncoherentXBarMasterPort(portName, *this,
76 defaultPortID);
77 masterPorts.push_back(bp);
78 reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
79 defaultPortID)));
80 }
81
82 // create the slave ports, once again starting at zero
83 for (int i = 0; i < p->port_slave_connection_count; ++i) {
84 std::string portName = csprintf("%s.slave[%d]", name(), i);
85 SlavePort* bp = new NoncoherentXBarSlavePort(portName, *this, i);
86 slavePorts.push_back(bp);
87 respLayers.push_back(new RespLayer(*bp, *this,
88 csprintf(".respLayer%d", i)));
89 }
90
91 clearPortCache();
92 }
93
94 NoncoherentXBar::~NoncoherentXBar()
95 {
96 for (auto l: reqLayers)
97 delete l;
98 for (auto l: respLayers)
99 delete l;
100 }
101
102 bool
103 NoncoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
104 {
105 // determine the source port based on the id
106 SlavePort *src_port = slavePorts[slave_port_id];
107
108 // we should never see express snoops on a non-coherent crossbar
109 assert(!pkt->isExpressSnoop());
110
111 // determine the destination based on the address
112 PortID master_port_id = findPort(pkt->getAddr());
113
114 // test if the layer should be considered occupied for the current
115 // port
116 if (!reqLayers[master_port_id]->tryTiming(src_port)) {
117 DPRINTF(NoncoherentXBar, "recvTimingReq: src %s %s 0x%x BUSY\n",
118 src_port->name(), pkt->cmdString(), pkt->getAddr());
119 return false;
120 }
121
122 DPRINTF(NoncoherentXBar, "recvTimingReq: src %s %s 0x%x\n",
123 src_port->name(), pkt->cmdString(), pkt->getAddr());
124
125 // store size and command as they might be modified when
126 // forwarding the packet
127 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
128 unsigned int pkt_cmd = pkt->cmdToIndex();
129
130 // set the source port for routing of the response
131 pkt->setSrc(slave_port_id);
132
133 calcPacketTiming(pkt);
134 Tick packetFinishTime = pkt->lastWordDelay + curTick();
135
136 // since it is a normal request, attempt to send the packet
137 bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
138
139 if (!success) {
140 // inhibited packets should never be forced to retry
141 assert(!pkt->memInhibitAsserted());
142
143 DPRINTF(NoncoherentXBar, "recvTimingReq: src %s %s 0x%x RETRY\n",
144 src_port->name(), pkt->cmdString(), pkt->getAddr());
145
146 // undo the calculation so we can check for 0 again
147 pkt->firstWordDelay = pkt->lastWordDelay = 0;
148
149 // occupy until the header is sent
150 reqLayers[master_port_id]->failedTiming(src_port,
151 clockEdge(headerCycles));
152
153 return false;
154 }
155
156 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
157
158 // stats updates
159 pktCount[slave_port_id][master_port_id]++;
160 pktSize[slave_port_id][master_port_id] += pkt_size;
161 transDist[pkt_cmd]++;
162
163 return true;
164 }
165
166 bool
167 NoncoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
168 {
169 // determine the source port based on the id
170 MasterPort *src_port = masterPorts[master_port_id];
171
172 // determine the destination based on what is stored in the packet
173 PortID slave_port_id = pkt->getDest();
174
175 // test if the layer should be considered occupied for the current
176 // port
177 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
178 DPRINTF(NoncoherentXBar, "recvTimingResp: src %s %s 0x%x BUSY\n",
179 src_port->name(), pkt->cmdString(), pkt->getAddr());
180 return false;
181 }
182
183 DPRINTF(NoncoherentXBar, "recvTimingResp: src %s %s 0x%x\n",
184 src_port->name(), pkt->cmdString(), pkt->getAddr());
185
186 // store size and command as they might be modified when
187 // forwarding the packet
188 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
189 unsigned int pkt_cmd = pkt->cmdToIndex();
190
191 calcPacketTiming(pkt);
192 Tick packetFinishTime = pkt->lastWordDelay + curTick();
193
194 // send the packet through the destination slave port
195 bool success M5_VAR_USED = slavePorts[slave_port_id]->sendTimingResp(pkt);
196
197 // currently it is illegal to block responses... can lead to
198 // deadlock
199 assert(success);
200
201 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
202
203 // stats updates
204 pktCount[slave_port_id][master_port_id]++;
205 pktSize[slave_port_id][master_port_id] += pkt_size;
206 transDist[pkt_cmd]++;
207
208 return true;
209 }
210
211 void
212 NoncoherentXBar::recvRetry(PortID master_port_id)
213 {
214 // responses never block on forwarding them, so the retry will
215 // always be coming from a port to which we tried to forward a
216 // request
217 reqLayers[master_port_id]->recvRetry();
218 }
219
220 Tick
221 NoncoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
222 {
223 DPRINTF(NoncoherentXBar, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
224 slavePorts[slave_port_id]->name(), pkt->getAddr(),
225 pkt->cmdString());
226
227 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
228 unsigned int pkt_cmd = pkt->cmdToIndex();
229
230 // determine the destination port
231 PortID master_port_id = findPort(pkt->getAddr());
232
233 // stats updates for the request
234 pktCount[slave_port_id][master_port_id]++;
235 pktSize[slave_port_id][master_port_id] += pkt_size;
236 transDist[pkt_cmd]++;
237
238 // forward the request to the appropriate destination
239 Tick response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
240
241 // add the response data
242 if (pkt->isResponse()) {
243 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
244 pkt_cmd = pkt->cmdToIndex();
245
246 // stats updates
247 pktCount[slave_port_id][master_port_id]++;
248 pktSize[slave_port_id][master_port_id] += pkt_size;
249 transDist[pkt_cmd]++;
250 }
251
252 // @todo: Not setting first-word time
253 pkt->lastWordDelay = response_latency;
254 return response_latency;
255 }
256
257 void
258 NoncoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
259 {
260 if (!pkt->isPrint()) {
261 // don't do DPRINTFs on PrintReq as it clutters up the output
262 DPRINTF(NoncoherentXBar,
263 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
264 slavePorts[slave_port_id]->name(), pkt->getAddr(),
265 pkt->cmdString());
266 }
267
268 // determine the destination port
269 PortID dest_id = findPort(pkt->getAddr());
270
271 // forward the request to the appropriate destination
272 masterPorts[dest_id]->sendFunctional(pkt);
273 }
274
275 unsigned int
276 NoncoherentXBar::drain(DrainManager *dm)
277 {
278 // sum up the individual layers
279 unsigned int total = 0;
280 for (auto l: reqLayers)
281 total += l->drain(dm);
282 for (auto l: respLayers)
283 total += l->drain(dm);
284 return total;
285 }
286
287 NoncoherentXBar*
288 NoncoherentXBarParams::create()
289 {
290 return new NoncoherentXBar(this);
291 }
292
293 void
294 NoncoherentXBar::regStats()
295 {
296 // register the stats of the base class and our layers
297 BaseXBar::regStats();
298 for (auto l: reqLayers)
299 l->regStats();
300 for (auto l: respLayers)
301 l->regStats();
302 }