cpu: Apply the ARM TLB rework to the O3 checker CPU.
[gem5.git] / src / mem / xbar.hh
1 /*
2 * Copyright (c) 2011-2015, 2018-2019 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) 2002-2005 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: Ron Dreslinski
41 * Ali Saidi
42 * Andreas Hansson
43 * William Wang
44 */
45
46 /**
47 * @file
48 * Declaration of an abstract crossbar base class.
49 */
50
51 #ifndef __MEM_XBAR_HH__
52 #define __MEM_XBAR_HH__
53
54 #include <deque>
55 #include <unordered_map>
56
57 #include "base/addr_range_map.hh"
58 #include "base/types.hh"
59 #include "mem/qport.hh"
60 #include "params/BaseXBar.hh"
61 #include "sim/clocked_object.hh"
62 #include "sim/stats.hh"
63
64 /**
65 * The base crossbar contains the common elements of the non-coherent
66 * and coherent crossbar. It is an abstract class that does not have
67 * any of the functionality relating to the actual reception and
68 * transmission of packets, as this is left for the subclasses.
69 *
70 * The BaseXBar is responsible for the basic flow control (busy or
71 * not), the administration of retries, and the address decoding.
72 */
73 class BaseXBar : public ClockedObject
74 {
75
76 protected:
77
78 /**
79 * A layer is an internal crossbar arbitration point with its own
80 * flow control. Each layer is a converging multiplexer tree. By
81 * instantiating one layer per destination port (and per packet
82 * type, i.e. request, response, snoop request and snoop
83 * response), we model full crossbar structures like AXI, ACE,
84 * PCIe, etc.
85 *
86 * The template parameter, PortClass, indicates the destination
87 * port type for the layer. The retry list holds either master
88 * ports or slave ports, depending on the direction of the
89 * layer. Thus, a request layer has a retry list containing slave
90 * ports, whereas a response layer holds master ports.
91 */
92 template <typename SrcType, typename DstType>
93 class Layer : public Drainable, public Stats::Group
94 {
95
96 public:
97
98 /**
99 * Create a layer and give it a name. The layer uses
100 * the crossbar an event manager.
101 *
102 * @param _port destination port the layer converges at
103 * @param _xbar the crossbar this layer belongs to
104 * @param _name the layer's name
105 */
106 Layer(DstType& _port, BaseXBar& _xbar, const std::string& _name);
107
108 /**
109 * Drain according to the normal semantics, so that the crossbar
110 * can tell the layer to drain, and pass an event to signal
111 * back when drained.
112 *
113 * @param de drain event to call once drained
114 *
115 * @return 1 if busy or waiting to retry, or 0 if idle
116 */
117 DrainState drain() override;
118
119 const std::string name() const { return _name; }
120
121
122 /**
123 * Determine if the layer accepts a packet from a specific
124 * port. If not, the port in question is also added to the
125 * retry list. In either case the state of the layer is
126 * updated accordingly.
127 *
128 * @param port Source port presenting the packet
129 *
130 * @return True if the layer accepts the packet
131 */
132 bool tryTiming(SrcType* src_port);
133
134 /**
135 * Deal with a destination port accepting a packet by potentially
136 * removing the source port from the retry list (if retrying) and
137 * occupying the layer accordingly.
138 *
139 * @param busy_time Time to spend as a result of a successful send
140 */
141 void succeededTiming(Tick busy_time);
142
143 /**
144 * Deal with a destination port not accepting a packet by
145 * potentially adding the source port to the retry list (if
146 * not already at the front) and occupying the layer
147 * accordingly.
148 *
149 * @param src_port Source port
150 * @param busy_time Time to spend as a result of a failed send
151 */
152 void failedTiming(SrcType* src_port, Tick busy_time);
153
154 void occupyLayer(Tick until);
155
156 /**
157 * Send a retry to the port at the head of waitingForLayer. The
158 * caller must ensure that the list is not empty.
159 */
160 void retryWaiting();
161
162 /**
163 * Handle a retry from a neighbouring module. This wraps
164 * retryWaiting by verifying that there are ports waiting
165 * before calling retryWaiting.
166 */
167 void recvRetry();
168
169 protected:
170
171 /**
172 * Sending the actual retry, in a manner specific to the
173 * individual layers. Note that for a MasterPort, there is
174 * both a RequestLayer and a SnoopResponseLayer using the same
175 * port, but using different functions for the flow control.
176 */
177 virtual void sendRetry(SrcType* retry_port) = 0;
178
179 private:
180
181 /** The destination port this layer converges at. */
182 DstType& port;
183
184 /** The crossbar this layer is a part of. */
185 BaseXBar& xbar;
186
187 std::string _name;
188
189 /**
190 * We declare an enum to track the state of the layer. The
191 * starting point is an idle state where the layer is waiting
192 * for a packet to arrive. Upon arrival, the layer
193 * transitions to the busy state, where it remains either
194 * until the packet transfer is done, or the header time is
195 * spent. Once the layer leaves the busy state, it can
196 * either go back to idle, if no packets have arrived while it
197 * was busy, or the layer goes on to retry the first port
198 * in waitingForLayer. A similar transition takes place from
199 * idle to retry if the layer receives a retry from one of
200 * its connected ports. The retry state lasts until the port
201 * in questions calls sendTiming and returns control to the
202 * layer, or goes to a busy state if the port does not
203 * immediately react to the retry by calling sendTiming.
204 */
205 enum State { IDLE, BUSY, RETRY };
206
207 State state;
208
209 /**
210 * A deque of ports that retry should be called on because
211 * the original send was delayed due to a busy layer.
212 */
213 std::deque<SrcType*> waitingForLayer;
214
215 /**
216 * Track who is waiting for the retry when receiving it from a
217 * peer. If no port is waiting NULL is stored.
218 */
219 SrcType* waitingForPeer;
220
221 /**
222 * Release the layer after being occupied and return to an
223 * idle state where we proceed to send a retry to any
224 * potential waiting port, or drain if asked to do so.
225 */
226 void releaseLayer();
227 EventFunctionWrapper releaseEvent;
228
229 /**
230 * Stats for occupancy and utilization. These stats capture
231 * the time the layer spends in the busy state and are thus only
232 * relevant when the memory system is in timing mode.
233 */
234 Stats::Scalar occupancy;
235 Stats::Formula utilization;
236
237 };
238
239 class ReqLayer : public Layer<SlavePort, MasterPort>
240 {
241 public:
242 /**
243 * Create a request layer and give it a name.
244 *
245 * @param _port destination port the layer converges at
246 * @param _xbar the crossbar this layer belongs to
247 * @param _name the layer's name
248 */
249 ReqLayer(MasterPort& _port, BaseXBar& _xbar, const std::string& _name) :
250 Layer(_port, _xbar, _name)
251 {}
252
253 protected:
254 void
255 sendRetry(SlavePort* retry_port) override
256 {
257 retry_port->sendRetryReq();
258 }
259 };
260
261 class RespLayer : public Layer<MasterPort, SlavePort>
262 {
263 public:
264 /**
265 * Create a response layer and give it a name.
266 *
267 * @param _port destination port the layer converges at
268 * @param _xbar the crossbar this layer belongs to
269 * @param _name the layer's name
270 */
271 RespLayer(SlavePort& _port, BaseXBar& _xbar,
272 const std::string& _name) :
273 Layer(_port, _xbar, _name)
274 {}
275
276 protected:
277 void
278 sendRetry(MasterPort* retry_port) override
279 {
280 retry_port->sendRetryResp();
281 }
282 };
283
284 class SnoopRespLayer : public Layer<SlavePort, MasterPort>
285 {
286 public:
287 /**
288 * Create a snoop response layer and give it a name.
289 *
290 * @param _port destination port the layer converges at
291 * @param _xbar the crossbar this layer belongs to
292 * @param _name the layer's name
293 */
294 SnoopRespLayer(MasterPort& _port, BaseXBar& _xbar,
295 const std::string& _name) :
296 Layer(_port, _xbar, _name)
297 {}
298
299 protected:
300
301 void
302 sendRetry(SlavePort* retry_port) override
303 {
304 retry_port->sendRetrySnoopResp();
305 }
306 };
307
308 /**
309 * Cycles of front-end pipeline including the delay to accept the request
310 * and to decode the address.
311 */
312 const Cycles frontendLatency;
313 const Cycles forwardLatency;
314 const Cycles responseLatency;
315 /** the width of the xbar in bytes */
316 const uint32_t width;
317
318 AddrRangeMap<PortID, 3> portMap;
319
320 /**
321 * Remember where request packets came from so that we can route
322 * responses to the appropriate port. This relies on the fact that
323 * the underlying Request pointer inside the Packet stays
324 * constant.
325 */
326 std::unordered_map<RequestPtr, PortID> routeTo;
327
328 /** all contigous ranges seen by this crossbar */
329 AddrRangeList xbarRanges;
330
331 AddrRange defaultRange;
332
333 /**
334 * Function called by the port when the crossbar is recieving a
335 * range change.
336 *
337 * @param master_port_id id of the port that received the change
338 */
339 virtual void recvRangeChange(PortID master_port_id);
340
341 /**
342 * Find which port connected to this crossbar (if any) should be
343 * given a packet with this address range.
344 *
345 * @param addr_range Address range to find port for.
346 * @return id of port that the packet should be sent out of.
347 */
348 PortID findPort(AddrRange addr_range);
349
350 /**
351 * Return the address ranges the crossbar is responsible for.
352 *
353 * @return a list of non-overlapping address ranges
354 */
355 AddrRangeList getAddrRanges() const;
356
357 /**
358 * Calculate the timing parameters for the packet. Updates the
359 * headerDelay and payloadDelay fields of the packet
360 * object with the relative number of ticks required to transmit
361 * the header and the payload, respectively.
362 *
363 * @param pkt Packet to populate with timings
364 * @param header_delay Header delay to be added
365 */
366 void calcPacketTiming(PacketPtr pkt, Tick header_delay);
367
368 /**
369 * Remember for each of the master ports of the crossbar if we got
370 * an address range from the connected slave. For convenience,
371 * also keep track of if we got ranges from all the slave modules
372 * or not.
373 */
374 std::vector<bool> gotAddrRanges;
375 bool gotAllAddrRanges;
376
377 /** The master and slave ports of the crossbar */
378 std::vector<QueuedSlavePort*> slavePorts;
379 std::vector<MasterPort*> masterPorts;
380
381 /** Port that handles requests that don't match any of the interfaces.*/
382 PortID defaultPortID;
383
384 /** If true, use address range provided by default device. Any
385 address not handled by another port and not in default device's
386 range will cause a fatal error. If false, just send all
387 addresses not handled by another port to default device. */
388 const bool useDefaultRange;
389
390 BaseXBar(const BaseXBarParams *p);
391
392 /**
393 * Stats for transaction distribution and data passing through the
394 * crossbar. The transaction distribution is globally counting
395 * different types of commands. The packet count and total packet
396 * size are two-dimensional vectors that are indexed by the
397 * slave port and master port id (thus the neighbouring master and
398 * neighbouring slave), summing up both directions (request and
399 * response).
400 */
401 Stats::Vector transDist;
402 Stats::Vector2d pktCount;
403 Stats::Vector2d pktSize;
404
405 public:
406
407 virtual ~BaseXBar();
408
409 /** A function used to return the port associated with this object. */
410 Port &getPort(const std::string &if_name,
411 PortID idx=InvalidPortID) override;
412
413 void regStats() override;
414 };
415
416 #endif //__MEM_XBAR_HH__