sim: Make clock private and access using clockPeriod()
[gem5.git] / src / mem / bus.cc
1 /*
2 * Copyright (c) 2011-2013 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 bus object.
48 */
49
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/Bus.hh"
53 #include "debug/BusAddrRanges.hh"
54 #include "debug/Drain.hh"
55 #include "mem/bus.hh"
56
57 BaseBus::BaseBus(const BaseBusParams *p)
58 : MemObject(p),
59 headerCycles(p->header_cycles), width(p->width),
60 gotAddrRanges(p->port_default_connection_count +
61 p->port_master_connection_count, false),
62 gotAllAddrRanges(false), defaultPortID(InvalidPortID),
63 useDefaultRange(p->use_default_range),
64 blockSize(p->block_size)
65 {}
66
67 BaseBus::~BaseBus()
68 {
69 for (MasterPortIter m = masterPorts.begin(); m != masterPorts.end();
70 ++m) {
71 delete *m;
72 }
73
74 for (SlavePortIter s = slavePorts.begin(); s != slavePorts.end();
75 ++s) {
76 delete *s;
77 }
78 }
79
80 void
81 BaseBus::init()
82 {
83 // determine the maximum peer block size, look at both the
84 // connected master and slave modules
85 uint32_t peer_block_size = 0;
86
87 for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
88 ++m) {
89 peer_block_size = std::max((*m)->peerBlockSize(), peer_block_size);
90 }
91
92 for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
93 ++s) {
94 peer_block_size = std::max((*s)->peerBlockSize(), peer_block_size);
95 }
96
97 // if the peers do not have a block size, use the default value
98 // set through the bus parameters
99 if (peer_block_size != 0)
100 blockSize = peer_block_size;
101
102 // check if the block size is a value known to work
103 if (!(blockSize == 16 || blockSize == 32 || blockSize == 64 ||
104 blockSize == 128))
105 warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
106 }
107
108 BaseMasterPort &
109 BaseBus::getMasterPort(const std::string &if_name, PortID idx)
110 {
111 if (if_name == "master" && idx < masterPorts.size()) {
112 // the master port index translates directly to the vector position
113 return *masterPorts[idx];
114 } else if (if_name == "default") {
115 return *masterPorts[defaultPortID];
116 } else {
117 return MemObject::getMasterPort(if_name, idx);
118 }
119 }
120
121 BaseSlavePort &
122 BaseBus::getSlavePort(const std::string &if_name, PortID idx)
123 {
124 if (if_name == "slave" && idx < slavePorts.size()) {
125 // the slave port index translates directly to the vector position
126 return *slavePorts[idx];
127 } else {
128 return MemObject::getSlavePort(if_name, idx);
129 }
130 }
131
132 Tick
133 BaseBus::calcPacketTiming(PacketPtr pkt)
134 {
135 // determine the header time rounded to the closest following
136 // clock edge
137 Tick headerTime = clockEdge(headerCycles);
138
139 // The packet will be sent. Figure out how long it occupies the bus, and
140 // how much of that time is for the first "word", aka bus width.
141 Cycles numCycles(0);
142 if (pkt->hasData()) {
143 // If a packet has data, it needs ceil(size/width) cycles to send it
144 unsigned dataSize = pkt->getSize();
145 numCycles = Cycles(divCeil(dataSize, width));
146 }
147
148 // The first word will be delivered on the cycle after the header.
149 pkt->firstWordTime = headerTime + clockPeriod();
150
151 // Note that currently finishTime can be smaller than
152 // firstWordTime if the packet has no data
153 pkt->finishTime = headerTime + numCycles * clockPeriod();
154
155 return headerTime;
156 }
157
158 template <typename PortClass>
159 BaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name) :
160 Drainable(),
161 bus(_bus), _name(_name), state(IDLE), drainManager(NULL),
162 releaseEvent(this)
163 {
164 }
165
166 template <typename PortClass>
167 void BaseBus::Layer<PortClass>::occupyLayer(Tick until)
168 {
169 // ensure the state is busy or in retry and never idle at this
170 // point, as the bus should transition from idle as soon as it has
171 // decided to forward the packet to prevent any follow-on calls to
172 // sendTiming seeing an unoccupied bus
173 assert(state != IDLE);
174
175 // note that we do not change the bus state here, if we are going
176 // from idle to busy it is handled by tryTiming, and if we
177 // are in retry we should remain in retry such that
178 // succeededTiming still sees the accurate state
179
180 // until should never be 0 as express snoops never occupy the bus
181 assert(until != 0);
182 bus.schedule(releaseEvent, until);
183
184 DPRINTF(BaseBus, "The bus is now busy from tick %d to %d\n",
185 curTick(), until);
186 }
187
188 template <typename PortClass>
189 bool
190 BaseBus::Layer<PortClass>::tryTiming(PortClass* port)
191 {
192 // first we see if the bus is busy, next we check if we are in a
193 // retry with a port other than the current one
194 if (state == BUSY || (state == RETRY && port != retryList.front())) {
195 // put the port at the end of the retry list
196 retryList.push_back(port);
197 return false;
198 }
199
200 // update the state which is shared for request, response and
201 // snoop responses, if we were idle we are now busy, if we are in
202 // a retry, then do not change
203 if (state == IDLE)
204 state = BUSY;
205
206 return true;
207 }
208
209 template <typename PortClass>
210 void
211 BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time)
212 {
213 // if a retrying port succeeded, also take it off the retry list
214 if (state == RETRY) {
215 DPRINTF(BaseBus, "Remove retry from list %s\n",
216 retryList.front()->name());
217 retryList.pop_front();
218 state = BUSY;
219 }
220
221 // we should either have gone from idle to busy in the
222 // tryTiming test, or just gone from a retry to busy
223 assert(state == BUSY);
224
225 // occupy the bus accordingly
226 occupyLayer(busy_time);
227 }
228
229 template <typename PortClass>
230 void
231 BaseBus::Layer<PortClass>::failedTiming(PortClass* port, Tick busy_time)
232 {
233 // if we are not in a retry, i.e. busy (but never idle), or we are
234 // in a retry but not for the current port, then add the port at
235 // the end of the retry list
236 if (state != RETRY || port != retryList.front()) {
237 retryList.push_back(port);
238 }
239
240 // even if we retried the current one and did not succeed,
241 // we are no longer retrying but instead busy
242 state = BUSY;
243
244 // occupy the bus accordingly
245 occupyLayer(busy_time);
246 }
247
248 template <typename PortClass>
249 void
250 BaseBus::Layer<PortClass>::releaseLayer()
251 {
252 // releasing the bus means we should now be idle
253 assert(state == BUSY);
254 assert(!releaseEvent.scheduled());
255
256 // update the state
257 state = IDLE;
258
259 // bus is now idle, so if someone is waiting we can retry
260 if (!retryList.empty()) {
261 // note that we block (return false on recvTiming) both
262 // because the bus is busy and because the destination is
263 // busy, and in the latter case the bus may be released before
264 // we see a retry from the destination
265 retryWaiting();
266 } else if (drainManager) {
267 DPRINTF(Drain, "Bus done draining, signaling drain manager\n");
268 //If we weren't able to drain before, do it now.
269 drainManager->signalDrainDone();
270 // Clear the drain event once we're done with it.
271 drainManager = NULL;
272 }
273 }
274
275 template <typename PortClass>
276 void
277 BaseBus::Layer<PortClass>::retryWaiting()
278 {
279 // this should never be called with an empty retry list
280 assert(!retryList.empty());
281
282 // we always go to retrying from idle
283 assert(state == IDLE);
284
285 // update the state which is shared for request, response and
286 // snoop responses
287 state = RETRY;
288
289 // note that we might have blocked on the receiving port being
290 // busy (rather than the bus itself) and now call retry before the
291 // destination called retry on the bus
292 retryList.front()->sendRetry();
293
294 // If the bus is still in the retry state, sendTiming wasn't
295 // called in zero time (e.g. the cache does this)
296 if (state == RETRY) {
297 retryList.pop_front();
298
299 //Burn a cycle for the missed grant.
300
301 // update the state which is shared for request, response and
302 // snoop responses
303 state = BUSY;
304
305 // occupy the bus layer until the next cycle ends
306 occupyLayer(bus.clockEdge(Cycles(1)));
307 }
308 }
309
310 template <typename PortClass>
311 void
312 BaseBus::Layer<PortClass>::recvRetry()
313 {
314 // we got a retry from a peer that we tried to send something to
315 // and failed, but we sent it on the account of someone else, and
316 // that source port should be on our retry list, however if the
317 // bus layer is released before this happens and the retry (from
318 // the bus point of view) is successful then this no longer holds
319 // and we could in fact have an empty retry list
320 if (retryList.empty())
321 return;
322
323 // if the bus layer is idle
324 if (state == IDLE) {
325 // note that we do not care who told us to retry at the moment, we
326 // merely let the first one on the retry list go
327 retryWaiting();
328 }
329 }
330
331 PortID
332 BaseBus::findPort(Addr addr)
333 {
334 // we should never see any address lookups before we've got the
335 // ranges of all connected slave modules
336 assert(gotAllAddrRanges);
337
338 // Check the cache
339 PortID dest_id = checkPortCache(addr);
340 if (dest_id != InvalidPortID)
341 return dest_id;
342
343 // Check the address map interval tree
344 PortMapConstIter i = portMap.find(addr);
345 if (i != portMap.end()) {
346 dest_id = i->second;
347 updatePortCache(dest_id, i->first);
348 return dest_id;
349 }
350
351 // Check if this matches the default range
352 if (useDefaultRange) {
353 if (defaultRange.contains(addr)) {
354 DPRINTF(BusAddrRanges, " found addr %#llx on default\n",
355 addr);
356 return defaultPortID;
357 }
358 } else if (defaultPortID != InvalidPortID) {
359 DPRINTF(BusAddrRanges, "Unable to find destination for addr %#llx, "
360 "will use default port\n", addr);
361 return defaultPortID;
362 }
363
364 // we should use the range for the default port and it did not
365 // match, or the default port is not set
366 fatal("Unable to find destination for addr %#llx on bus %s\n", addr,
367 name());
368 }
369
370 /** Function called by the port when the bus is receiving a range change.*/
371 void
372 BaseBus::recvRangeChange(PortID master_port_id)
373 {
374 DPRINTF(BusAddrRanges, "Received range change from slave port %s\n",
375 masterPorts[master_port_id]->getSlavePort().name());
376
377 // remember that we got a range from this master port and thus the
378 // connected slave module
379 gotAddrRanges[master_port_id] = true;
380
381 // update the global flag
382 if (!gotAllAddrRanges) {
383 // take a logical AND of all the ports and see if we got
384 // ranges from everyone
385 gotAllAddrRanges = true;
386 std::vector<bool>::const_iterator r = gotAddrRanges.begin();
387 while (gotAllAddrRanges && r != gotAddrRanges.end()) {
388 gotAllAddrRanges &= *r++;
389 }
390 if (gotAllAddrRanges)
391 DPRINTF(BusAddrRanges, "Got address ranges from all slaves\n");
392 }
393
394 // note that we could get the range from the default port at any
395 // point in time, and we cannot assume that the default range is
396 // set before the other ones are, so we do additional checks once
397 // all ranges are provided
398 if (master_port_id == defaultPortID) {
399 // only update if we are indeed checking ranges for the
400 // default port since the port might not have a valid range
401 // otherwise
402 if (useDefaultRange) {
403 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
404
405 if (ranges.size() != 1)
406 fatal("Bus %s may only have a single default range",
407 name());
408
409 defaultRange = ranges.front();
410 }
411 } else {
412 // the ports are allowed to update their address ranges
413 // dynamically, so remove any existing entries
414 if (gotAddrRanges[master_port_id]) {
415 for (PortMapIter p = portMap.begin(); p != portMap.end(); ) {
416 if (p->second == master_port_id)
417 // erasing invalidates the iterator, so advance it
418 // before the deletion takes place
419 portMap.erase(p++);
420 else
421 p++;
422 }
423 }
424
425 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
426
427 for (AddrRangeConstIter r = ranges.begin(); r != ranges.end(); ++r) {
428 DPRINTF(BusAddrRanges, "Adding range %s for id %d\n",
429 r->to_string(), master_port_id);
430 if (portMap.insert(*r, master_port_id) == portMap.end()) {
431 PortID conflict_id = portMap.find(*r)->second;
432 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
433 name(),
434 masterPorts[master_port_id]->getSlavePort().name(),
435 masterPorts[conflict_id]->getSlavePort().name());
436 }
437 }
438 }
439
440 // if we have received ranges from all our neighbouring slave
441 // modules, go ahead and tell our connected master modules in
442 // turn, this effectively assumes a tree structure of the system
443 if (gotAllAddrRanges) {
444 // also check that no range partially overlaps with the
445 // default range, this has to be done after all ranges are set
446 // as there are no guarantees for when the default range is
447 // update with respect to the other ones
448 if (useDefaultRange) {
449 for (PortID port_id = 0; port_id < masterPorts.size(); ++port_id) {
450 if (port_id == defaultPortID) {
451 if (!gotAddrRanges[port_id])
452 fatal("Bus %s uses default range, but none provided",
453 name());
454 } else {
455 AddrRangeList ranges =
456 masterPorts[port_id]->getAddrRanges();
457
458 for (AddrRangeConstIter r = ranges.begin();
459 r != ranges.end(); ++r) {
460 // see if the new range is partially
461 // overlapping the default range
462 if (r->intersects(defaultRange) &&
463 !r->isSubset(defaultRange))
464 fatal("Range %s intersects the " \
465 "default range of %s but is not a " \
466 "subset\n", r->to_string(), name());
467 }
468 }
469 }
470 }
471
472 // tell all our neighbouring master ports that our address
473 // ranges have changed
474 for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
475 ++s)
476 (*s)->sendRangeChange();
477 }
478
479 clearPortCache();
480 }
481
482 AddrRangeList
483 BaseBus::getAddrRanges() const
484 {
485 // we should never be asked without first having sent a range
486 // change, and the latter is only done once we have all the ranges
487 // of the connected devices
488 assert(gotAllAddrRanges);
489
490 // at the moment, this never happens, as there are no cycles in
491 // the range queries and no devices on the master side of a bus
492 // (CPU, cache, bridge etc) actually care about the ranges of the
493 // ports they are connected to
494
495 DPRINTF(BusAddrRanges, "Received address range request, returning:\n");
496
497 // start out with the default range
498 AddrRangeList ranges;
499 if (useDefaultRange) {
500 ranges.push_back(defaultRange);
501 DPRINTF(BusAddrRanges, " -- Default %s\n", defaultRange.to_string());
502 }
503
504 // add any range that is not a subset of the default range
505 for (PortMapConstIter p = portMap.begin(); p != portMap.end(); ++p) {
506 if (useDefaultRange && p->first.isSubset(defaultRange)) {
507 DPRINTF(BusAddrRanges, " -- %s is a subset of default\n",
508 p->first.to_string());
509 } else {
510 ranges.push_back(p->first);
511 DPRINTF(BusAddrRanges, " -- %s\n", p->first.to_string());
512 }
513 }
514
515 return ranges;
516 }
517
518 unsigned
519 BaseBus::deviceBlockSize() const
520 {
521 return blockSize;
522 }
523
524 template <typename PortClass>
525 unsigned int
526 BaseBus::Layer<PortClass>::drain(DrainManager *dm)
527 {
528 //We should check that we're not "doing" anything, and that noone is
529 //waiting. We might be idle but have someone waiting if the device we
530 //contacted for a retry didn't actually retry.
531 if (!retryList.empty() || state != IDLE) {
532 DPRINTF(Drain, "Bus not drained\n");
533 drainManager = dm;
534 return 1;
535 }
536 return 0;
537 }
538
539 /**
540 * Bus layer template instantiations. Could be removed with _impl.hh
541 * file, but since there are only two given options (MasterPort and
542 * SlavePort) it seems a bit excessive at this point.
543 */
544 template class BaseBus::Layer<SlavePort>;
545 template class BaseBus::Layer<MasterPort>;