ruby: get rid of RefCnt and Allocator stuff use base/refcnt.hh
[gem5.git] / src / mem / bus.cc
1 /*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 */
30
31 /**
32 * @file
33 * Definition of a bus object.
34 */
35
36 #include <algorithm>
37 #include <limits>
38
39 #include "base/misc.hh"
40 #include "base/trace.hh"
41 #include "mem/bus.hh"
42
43 Port *
44 Bus::getPort(const std::string &if_name, int idx)
45 {
46 if (if_name == "default") {
47 if (defaultPort == NULL) {
48 defaultPort = new BusPort(csprintf("%s-default",name()), this,
49 defaultId);
50 cachedBlockSizeValid = false;
51 return defaultPort;
52 } else
53 fatal("Default port already set\n");
54 }
55 int id;
56 if (if_name == "functional") {
57 if (!funcPort) {
58 id = maxId++;
59 funcPort = new BusPort(csprintf("%s-p%d-func", name(), id), this, id);
60 funcPortId = id;
61 interfaces[id] = funcPort;
62 }
63 return funcPort;
64 }
65
66 // if_name ignored? forced to be empty?
67 id = maxId++;
68 assert(maxId < std::numeric_limits<typeof(maxId)>::max());
69 BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
70 interfaces[id] = bp;
71 cachedBlockSizeValid = false;
72 return bp;
73 }
74
75 void
76 Bus::deletePortRefs(Port *p)
77 {
78
79 BusPort *bp = dynamic_cast<BusPort*>(p);
80 if (bp == NULL)
81 panic("Couldn't convert Port* to BusPort*\n");
82 // If this is our one functional port
83 if (funcPort == bp)
84 return;
85 interfaces.erase(bp->getId());
86 clearBusCache();
87 delete bp;
88 }
89
90 /** Get the ranges of anyone other buses that we are connected to. */
91 void
92 Bus::init()
93 {
94 m5::hash_map<short,BusPort*>::iterator intIter;
95
96 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
97 intIter->second->sendStatusChange(Port::RangeChange);
98 }
99
100 Bus::BusFreeEvent::BusFreeEvent(Bus *_bus)
101 : bus(_bus)
102 {}
103
104 void
105 Bus::BusFreeEvent::process()
106 {
107 bus->recvRetry(-1);
108 }
109
110 const char *
111 Bus::BusFreeEvent::description() const
112 {
113 return "bus became available";
114 }
115
116 Tick
117 Bus::calcPacketTiming(PacketPtr pkt)
118 {
119 // Bring tickNextIdle up to the present tick.
120 // There is some potential ambiguity where a cycle starts, which
121 // might make a difference when devices are acting right around a
122 // cycle boundary. Using a < allows things which happen exactly on
123 // a cycle boundary to take up only the following cycle. Anything
124 // that happens later will have to "wait" for the end of that
125 // cycle, and then start using the bus after that.
126 if (tickNextIdle < curTick) {
127 tickNextIdle = curTick;
128 if (tickNextIdle % clock != 0)
129 tickNextIdle = curTick - (curTick % clock) + clock;
130 }
131
132 Tick headerTime = tickNextIdle + headerCycles * clock;
133
134 // The packet will be sent. Figure out how long it occupies the bus, and
135 // how much of that time is for the first "word", aka bus width.
136 int numCycles = 0;
137 if (pkt->hasData()) {
138 // If a packet has data, it needs ceil(size/width) cycles to send it
139 int dataSize = pkt->getSize();
140 numCycles += dataSize/width;
141 if (dataSize % width)
142 numCycles++;
143 }
144
145 // The first word will be delivered after the current tick, the delivery
146 // of the address if any, and one bus cycle to deliver the data
147 pkt->firstWordTime = headerTime + clock;
148
149 pkt->finishTime = headerTime + numCycles * clock;
150
151 return headerTime;
152 }
153
154 void Bus::occupyBus(Tick until)
155 {
156 if (until == 0) {
157 // shortcut for express snoop packets
158 return;
159 }
160
161 tickNextIdle = until;
162 reschedule(busIdle, tickNextIdle, true);
163
164 DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
165 curTick, tickNextIdle);
166 }
167
168 /** Function called by the port when the bus is receiving a Timing
169 * transaction.*/
170 bool
171 Bus::recvTiming(PacketPtr pkt)
172 {
173 short src = pkt->getSrc();
174
175 BusPort *src_port;
176 if (src == defaultId)
177 src_port = defaultPort;
178 else {
179 src_port = checkBusCache(src);
180 if (src_port == NULL) {
181 src_port = interfaces[src];
182 updateBusCache(src, src_port);
183 }
184 }
185
186 // If the bus is busy, or other devices are in line ahead of the current
187 // one, put this device on the retry list.
188 if (!pkt->isExpressSnoop() &&
189 (tickNextIdle > curTick ||
190 (retryList.size() && (!inRetry || src_port != retryList.front()))))
191 {
192 addToRetryList(src_port);
193 DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x BUSY\n",
194 src, pkt->getDest(), pkt->cmdString(), pkt->getAddr());
195 return false;
196 }
197
198 DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x\n",
199 src, pkt->getDest(), pkt->cmdString(), pkt->getAddr());
200
201 Tick headerFinishTime = pkt->isExpressSnoop() ? 0 : calcPacketTiming(pkt);
202 Tick packetFinishTime = pkt->isExpressSnoop() ? 0 : pkt->finishTime;
203
204 short dest = pkt->getDest();
205 int dest_port_id;
206 Port *dest_port;
207
208 if (dest == Packet::Broadcast) {
209 dest_port_id = findPort(pkt->getAddr());
210 dest_port = (dest_port_id == defaultId) ?
211 defaultPort : interfaces[dest_port_id];
212 SnoopIter s_end = snoopPorts.end();
213 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) {
214 BusPort *p = *s_iter;
215 if (p != dest_port && p != src_port) {
216 // cache is not allowed to refuse snoop
217 bool success M5_VAR_USED = p->sendTiming(pkt);
218 assert(success);
219 }
220 }
221 } else {
222 assert(dest < maxId);
223 assert(dest != src); // catch infinite loops
224 dest_port_id = dest;
225 if (dest_port_id == defaultId)
226 dest_port = defaultPort;
227 else {
228 dest_port = checkBusCache(dest);
229 if (dest_port == NULL) {
230 dest_port = interfaces[dest_port_id];
231 // updateBusCache(dest_port_id, dest_port);
232 }
233 }
234 dest_port = (dest_port_id == defaultId) ?
235 defaultPort : interfaces[dest_port_id];
236 }
237
238 if (dest_port_id == src) {
239 // Must be forwarded snoop up from below...
240 assert(dest == Packet::Broadcast);
241 } else {
242 // send to actual target
243 if (!dest_port->sendTiming(pkt)) {
244 // Packet not successfully sent. Leave or put it on the retry list.
245 // illegal to block responses... can lead to deadlock
246 assert(!pkt->isResponse());
247 // It's also illegal to force a transaction to retry after
248 // someone else has committed to respond.
249 assert(!pkt->memInhibitAsserted());
250 DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x TGT RETRY\n",
251 src, pkt->getDest(), pkt->cmdString(), pkt->getAddr());
252 addToRetryList(src_port);
253 occupyBus(headerFinishTime);
254 return false;
255 }
256 // send OK, fall through... pkt may have been deleted by
257 // target at this point, so it should *not* be referenced
258 // again. We'll set it to NULL here just to be safe.
259 pkt = NULL;
260 }
261
262 occupyBus(packetFinishTime);
263
264 // Packet was successfully sent.
265 // Also take care of retries
266 if (inRetry) {
267 DPRINTF(Bus, "Remove retry from list %d\n", src);
268 retryList.front()->onRetryList(false);
269 retryList.pop_front();
270 inRetry = false;
271 }
272 return true;
273 }
274
275 void
276 Bus::recvRetry(int id)
277 {
278 // If there's anything waiting, and the bus isn't busy...
279 if (retryList.size() && curTick >= tickNextIdle) {
280 //retryingPort = retryList.front();
281 inRetry = true;
282 DPRINTF(Bus, "Sending a retry to %s\n", retryList.front()->getPeer()->name());
283 retryList.front()->sendRetry();
284 // If inRetry is still true, sendTiming wasn't called
285 if (inRetry)
286 {
287 retryList.front()->onRetryList(false);
288 retryList.pop_front();
289 inRetry = false;
290
291 //Bring tickNextIdle up to the present
292 while (tickNextIdle < curTick)
293 tickNextIdle += clock;
294
295 //Burn a cycle for the missed grant.
296 tickNextIdle += clock;
297
298 reschedule(busIdle, tickNextIdle, true);
299 }
300 }
301 //If we weren't able to drain before, we might be able to now.
302 if (drainEvent && retryList.size() == 0 && curTick >= tickNextIdle) {
303 drainEvent->process();
304 // Clear the drain event once we're done with it.
305 drainEvent = NULL;
306 }
307 }
308
309 int
310 Bus::findPort(Addr addr)
311 {
312 /* An interval tree would be a better way to do this. --ali. */
313 int dest_id = -1;
314
315 dest_id = checkPortCache(addr);
316 if (dest_id == -1) {
317 PortIter i = portMap.find(RangeSize(addr,1));
318 if (i != portMap.end()) {
319 dest_id = i->second;
320 updatePortCache(dest_id, i->first.start, i->first.end);
321 }
322 }
323
324 // Check if this matches the default range
325 if (dest_id == -1) {
326 AddrRangeIter a_end = defaultRange.end();
327 for (AddrRangeIter i = defaultRange.begin(); i != a_end; i++) {
328 if (*i == addr) {
329 DPRINTF(Bus, " found addr %#llx on default\n", addr);
330 return defaultId;
331 }
332 }
333
334 if (responderSet) {
335 panic("Unable to find destination for addr (user set default "
336 "responder): %#llx\n", addr);
337 } else {
338 DPRINTF(Bus, "Unable to find destination for addr: %#llx, will use "
339 "default port\n", addr);
340
341 return defaultId;
342 }
343 }
344
345 return dest_id;
346 }
347
348
349 /** Function called by the port when the bus is receiving a Atomic
350 * transaction.*/
351 Tick
352 Bus::recvAtomic(PacketPtr pkt)
353 {
354 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
355 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
356 assert(pkt->getDest() == Packet::Broadcast);
357 assert(pkt->isRequest());
358
359 // Variables for recording original command and snoop response (if
360 // any)... if a snooper respondes, we will need to restore
361 // original command so that additional snoops can take place
362 // properly
363 MemCmd orig_cmd = pkt->cmd;
364 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
365 Tick snoop_response_latency = 0;
366 int orig_src = pkt->getSrc();
367
368 int target_port_id = findPort(pkt->getAddr());
369 BusPort *target_port;
370 if (target_port_id == defaultId)
371 target_port = defaultPort;
372 else {
373 target_port = checkBusCache(target_port_id);
374 if (target_port == NULL) {
375 target_port = interfaces[target_port_id];
376 updateBusCache(target_port_id, target_port);
377 }
378 }
379
380 SnoopIter s_end = snoopPorts.end();
381 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) {
382 BusPort *p = *s_iter;
383 // same port should not have both target addresses and snooping
384 assert(p != target_port);
385 if (p->getId() != pkt->getSrc()) {
386 Tick latency = p->sendAtomic(pkt);
387 if (pkt->isResponse()) {
388 // response from snoop agent
389 assert(pkt->cmd != orig_cmd);
390 assert(pkt->memInhibitAsserted());
391 // should only happen once
392 assert(snoop_response_cmd == MemCmd::InvalidCmd);
393 // save response state
394 snoop_response_cmd = pkt->cmd;
395 snoop_response_latency = latency;
396 // restore original packet state for remaining snoopers
397 pkt->cmd = orig_cmd;
398 pkt->setSrc(orig_src);
399 pkt->setDest(Packet::Broadcast);
400 }
401 }
402 }
403
404 Tick response_latency = 0;
405
406 // we can get requests sent up from the memory side of the bus for
407 // snooping... don't send them back down!
408 if (target_port_id != pkt->getSrc()) {
409 response_latency = target_port->sendAtomic(pkt);
410 }
411
412 // if we got a response from a snooper, restore it here
413 if (snoop_response_cmd != MemCmd::InvalidCmd) {
414 // no one else should have responded
415 assert(!pkt->isResponse());
416 assert(pkt->cmd == orig_cmd);
417 pkt->cmd = snoop_response_cmd;
418 response_latency = snoop_response_latency;
419 }
420
421 // why do we have this packet field and the return value both???
422 pkt->finishTime = curTick + response_latency;
423 return response_latency;
424 }
425
426 /** Function called by the port when the bus is receiving a Functional
427 * transaction.*/
428 void
429 Bus::recvFunctional(PacketPtr pkt)
430 {
431 if (!pkt->isPrint()) {
432 // don't do DPRINTFs on PrintReq as it clutters up the output
433 DPRINTF(Bus,
434 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
435 pkt->getSrc(), pkt->getDest(), pkt->getAddr(),
436 pkt->cmdString());
437 }
438 assert(pkt->getDest() == Packet::Broadcast);
439
440 int port_id = findPort(pkt->getAddr());
441 Port *port = (port_id == defaultId) ? defaultPort : interfaces[port_id];
442 // The packet may be changed by another bus on snoops, restore the
443 // id after each
444 int src_id = pkt->getSrc();
445
446 assert(pkt->isRequest()); // hasn't already been satisfied
447
448 SnoopIter s_end = snoopPorts.end();
449 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) {
450 BusPort *p = *s_iter;
451 if (p != port && p->getId() != src_id) {
452 p->sendFunctional(pkt);
453 }
454 if (pkt->isResponse()) {
455 break;
456 }
457 pkt->setSrc(src_id);
458 }
459
460 // If the snooping hasn't found what we were looking for, keep going.
461 if (!pkt->isResponse() && port_id != pkt->getSrc()) {
462 port->sendFunctional(pkt);
463 }
464 }
465
466 /** Function called by the port when the bus is receiving a status change.*/
467 void
468 Bus::recvStatusChange(Port::Status status, int id)
469 {
470 AddrRangeList ranges;
471 bool snoops;
472 AddrRangeIter iter;
473
474 if (inRecvStatusChange.count(id))
475 return;
476 inRecvStatusChange.insert(id);
477
478 assert(status == Port::RangeChange &&
479 "The other statuses need to be implemented.");
480
481 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
482
483 clearPortCache();
484 if (id == defaultId) {
485 defaultRange.clear();
486 // Only try to update these ranges if the user set a default responder.
487 if (responderSet) {
488 defaultPort->getPeerAddressRanges(ranges, snoops);
489 assert(snoops == false);
490 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
491 defaultRange.push_back(*iter);
492 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
493 iter->start, iter->end);
494 }
495 }
496 } else {
497
498 assert((id < maxId && id >= 0) || id == defaultId);
499 BusPort *port = interfaces[id];
500
501 // Clean out any previously existent ids
502 for (PortIter portIter = portMap.begin();
503 portIter != portMap.end(); ) {
504 if (portIter->second == id)
505 portMap.erase(portIter++);
506 else
507 portIter++;
508 }
509
510 for (SnoopIter s_iter = snoopPorts.begin();
511 s_iter != snoopPorts.end(); ) {
512 if ((*s_iter)->getId() == id)
513 s_iter = snoopPorts.erase(s_iter);
514 else
515 s_iter++;
516 }
517
518 port->getPeerAddressRanges(ranges, snoops);
519
520 if (snoops) {
521 DPRINTF(BusAddrRanges, "Adding id %d to snoop list\n", id);
522 snoopPorts.push_back(port);
523 }
524
525 for (iter = ranges.begin(); iter != ranges.end(); iter++) {
526 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
527 iter->start, iter->end, id);
528 if (portMap.insert(*iter, id) == portMap.end()) {
529 int conflict_id = portMap.find(*iter)->second;
530 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
531 name(), interfaces[id]->getPeer()->name(),
532 interfaces[conflict_id]->getPeer()->name());
533 }
534 }
535 }
536 DPRINTF(MMU, "port list has %d entries\n", portMap.size());
537
538 // tell all our peers that our address range has changed.
539 // Don't tell the device that caused this change, it already knows
540 m5::hash_map<short,BusPort*>::iterator intIter;
541
542 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
543 if (intIter->first != id && intIter->first != funcPortId)
544 intIter->second->sendStatusChange(Port::RangeChange);
545
546 if (id != defaultId && defaultPort)
547 defaultPort->sendStatusChange(Port::RangeChange);
548 inRecvStatusChange.erase(id);
549 }
550
551 void
552 Bus::addressRanges(AddrRangeList &resp, bool &snoop, int id)
553 {
554 resp.clear();
555 snoop = false;
556
557 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
558
559 for (AddrRangeIter dflt_iter = defaultRange.begin();
560 dflt_iter != defaultRange.end(); dflt_iter++) {
561 resp.push_back(*dflt_iter);
562 DPRINTF(BusAddrRanges, " -- Dflt: %#llx : %#llx\n",dflt_iter->start,
563 dflt_iter->end);
564 }
565 for (PortIter portIter = portMap.begin();
566 portIter != portMap.end(); portIter++) {
567 bool subset = false;
568 for (AddrRangeIter dflt_iter = defaultRange.begin();
569 dflt_iter != defaultRange.end(); dflt_iter++) {
570 if ((portIter->first.start < dflt_iter->start &&
571 portIter->first.end >= dflt_iter->start) ||
572 (portIter->first.start < dflt_iter->end &&
573 portIter->first.end >= dflt_iter->end))
574 fatal("Devices can not set ranges that itersect the default set\
575 but are not a subset of the default set.\n");
576 if (portIter->first.start >= dflt_iter->start &&
577 portIter->first.end <= dflt_iter->end) {
578 subset = true;
579 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
580 portIter->first.start, portIter->first.end);
581 }
582 }
583 if (portIter->second != id && !subset) {
584 resp.push_back(portIter->first);
585 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
586 portIter->first.start, portIter->first.end);
587 }
588 }
589
590 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != snoopPorts.end();
591 s_iter++) {
592 if ((*s_iter)->getId() != id) {
593 snoop = true;
594 break;
595 }
596 }
597 }
598
599 unsigned
600 Bus::findBlockSize(int id)
601 {
602 if (cachedBlockSizeValid)
603 return cachedBlockSize;
604
605 unsigned max_bs = 0;
606
607 PortIter p_end = portMap.end();
608 for (PortIter p_iter = portMap.begin(); p_iter != p_end; p_iter++) {
609 unsigned tmp_bs = interfaces[p_iter->second]->peerBlockSize();
610 if (tmp_bs > max_bs)
611 max_bs = tmp_bs;
612 }
613 SnoopIter s_end = snoopPorts.end();
614 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) {
615 unsigned tmp_bs = (*s_iter)->peerBlockSize();
616 if (tmp_bs > max_bs)
617 max_bs = tmp_bs;
618 }
619 if (max_bs == 0)
620 max_bs = defaultBlockSize;
621
622 if (max_bs != 64)
623 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
624 cachedBlockSize = max_bs;
625 cachedBlockSizeValid = true;
626 return max_bs;
627 }
628
629
630 unsigned int
631 Bus::drain(Event * de)
632 {
633 //We should check that we're not "doing" anything, and that noone is
634 //waiting. We might be idle but have someone waiting if the device we
635 //contacted for a retry didn't actually retry.
636 if (retryList.size() || (curTick < tickNextIdle && busIdle.scheduled())) {
637 drainEvent = de;
638 return 1;
639 }
640 return 0;
641 }
642
643 void
644 Bus::startup()
645 {
646 if (tickNextIdle < curTick)
647 tickNextIdle = (curTick / clock) * clock + clock;
648 }
649
650 Bus *
651 BusParams::create()
652 {
653 return new Bus(this);
654 }