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