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