Merge ktlim@zizzer:/bk/newmem
[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
37 #include <limits>
38
39 #include "base/misc.hh"
40 #include "base/trace.hh"
41 #include "mem/bus.hh"
42 #include "sim/builder.hh"
43
44 Port *
45 Bus::getPort(const std::string &if_name, int idx)
46 {
47 if (if_name == "default") {
48 if (defaultPort == NULL) {
49 defaultPort = new BusPort(csprintf("%s-default",name()), this,
50 defaultId);
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 return bp;
72 }
73
74 void
75 Bus::deletePortRefs(Port *p)
76 {
77
78 BusPort *bp = dynamic_cast<BusPort*>(p);
79 if (bp == NULL)
80 panic("Couldn't convert Port* to BusPort*\n");
81 // If this is our one functional port
82 if (funcPort == bp)
83 return;
84 interfaces.erase(bp->getId());
85 delete bp;
86 }
87
88 /** Get the ranges of anyone other buses that we are connected to. */
89 void
90 Bus::init()
91 {
92 m5::hash_map<short,BusPort*>::iterator intIter;
93
94 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
95 intIter->second->sendStatusChange(Port::RangeChange);
96 }
97
98 Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
99 {}
100
101 void Bus::BusFreeEvent::process()
102 {
103 bus->recvRetry(-1);
104 }
105
106 const char * Bus::BusFreeEvent::description()
107 {
108 return "bus became available";
109 }
110
111 void Bus::occupyBus(PacketPtr pkt)
112 {
113 //Bring tickNextIdle up to the present tick
114 //There is some potential ambiguity where a cycle starts, which might make
115 //a difference when devices are acting right around a cycle boundary. Using
116 //a < allows things which happen exactly on a cycle boundary to take up only
117 //the following cycle. Anthing that happens later will have to "wait" for
118 //the end of that cycle, and then start using the bus after that.
119 while (tickNextIdle < curTick)
120 tickNextIdle += clock;
121
122 // The packet will be sent. Figure out how long it occupies the bus, and
123 // how much of that time is for the first "word", aka bus width.
124 int numCycles = 0;
125 // Requests need one cycle to send an address
126 if (pkt->isRequest())
127 numCycles++;
128 else if (pkt->isResponse() || pkt->hasData()) {
129 // If a packet has data, it needs ceil(size/width) cycles to send it
130 // We're using the "adding instead of dividing" trick again here
131 if (pkt->hasData()) {
132 int dataSize = pkt->getSize();
133 for (int transmitted = 0; transmitted < dataSize;
134 transmitted += width) {
135 numCycles++;
136 }
137 } else {
138 // If the packet didn't have data, it must have been a response.
139 // Those use the bus for one cycle to send their data.
140 numCycles++;
141 }
142 }
143
144 // The first word will be delivered after the current tick, the delivery
145 // of the address if any, and one bus cycle to deliver the data
146 pkt->firstWordTime =
147 tickNextIdle +
148 pkt->isRequest() ? clock : 0 +
149 clock;
150
151 //Advance it numCycles bus cycles.
152 //XXX Should this use the repeated addition trick as well?
153 tickNextIdle += (numCycles * clock);
154 if (!busIdle.scheduled()) {
155 busIdle.schedule(tickNextIdle);
156 } else {
157 busIdle.reschedule(tickNextIdle);
158 }
159 DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
160 curTick, tickNextIdle);
161
162 // The bus will become idle once the current packet is delivered.
163 pkt->finishTime = tickNextIdle;
164 }
165
166 /** Function called by the port when the bus is receiving a Timing
167 * transaction.*/
168 bool
169 Bus::recvTiming(PacketPtr pkt)
170 {
171 Port *port;
172 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
173 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
174
175 BusPort *pktPort;
176 if (pkt->getSrc() == defaultId)
177 pktPort = defaultPort;
178 else pktPort = interfaces[pkt->getSrc()];
179
180 // If the bus is busy, or other devices are in line ahead of the current
181 // one, put this device on the retry list.
182 if (tickNextIdle > curTick ||
183 (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
184 addToRetryList(pktPort);
185 return false;
186 }
187
188 short dest = pkt->getDest();
189
190 // Make sure to clear the snoop commit flag so it doesn't think an
191 // access has been handled twice.
192 if (dest == Packet::Broadcast) {
193 port = findPort(pkt->getAddr(), pkt->getSrc());
194 pkt->flags &= ~SNOOP_COMMIT;
195 if (timingSnoop(pkt, port ? port : interfaces[pkt->getSrc()])) {
196 bool success;
197
198 pkt->flags |= SNOOP_COMMIT;
199 success = timingSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
200 assert(success);
201
202 if (pkt->flags & SATISFIED) {
203 //Cache-Cache transfer occuring
204 if (inRetry) {
205 retryList.front()->onRetryList(false);
206 retryList.pop_front();
207 inRetry = false;
208 }
209 occupyBus(pkt);
210 return true;
211 }
212 } else {
213 //Snoop didn't succeed
214 DPRINTF(Bus, "Adding a retry to RETRY list %d\n",
215 pktPort->getId());
216 addToRetryList(pktPort);
217 return false;
218 }
219 } else {
220 assert(dest >= 0 && dest < maxId);
221 assert(dest != pkt->getSrc()); // catch infinite loops
222 port = interfaces[dest];
223 }
224
225 occupyBus(pkt);
226
227 if (port) {
228 if (port->sendTiming(pkt)) {
229 // Packet was successfully sent. Return true.
230 // Also take care of retries
231 if (inRetry) {
232 DPRINTF(Bus, "Remove retry from list %d\n",
233 retryList.front()->getId());
234 retryList.front()->onRetryList(false);
235 retryList.pop_front();
236 inRetry = false;
237 }
238 return true;
239 }
240
241 // Packet not successfully sent. Leave or put it on the retry list.
242 DPRINTF(Bus, "Adding a retry to RETRY list %d\n",
243 pktPort->getId());
244 addToRetryList(pktPort);
245 return false;
246 }
247 else {
248 //Forwarding up from responder, just return true;
249 return true;
250 }
251 }
252
253 void
254 Bus::recvRetry(int id)
255 {
256 DPRINTF(Bus, "Received a retry\n");
257 // If there's anything waiting, and the bus isn't busy...
258 if (retryList.size() && curTick >= tickNextIdle) {
259 //retryingPort = retryList.front();
260 inRetry = true;
261 DPRINTF(Bus, "Sending a retry\n");
262 retryList.front()->sendRetry();
263 // If inRetry is still true, sendTiming wasn't called
264 if (inRetry)
265 {
266 retryList.front()->onRetryList(false);
267 retryList.pop_front();
268 inRetry = false;
269
270 //Bring tickNextIdle up to the present
271 while (tickNextIdle < curTick)
272 tickNextIdle += clock;
273
274 //Burn a cycle for the missed grant.
275 tickNextIdle += clock;
276
277 if (!busIdle.scheduled()) {
278 busIdle.schedule(tickNextIdle);
279 } else {
280 busIdle.reschedule(tickNextIdle);
281 }
282 }
283 }
284 //If we weren't able to drain before, we might be able to now.
285 if (drainEvent && retryList.size() == 0 && curTick >= tickNextIdle) {
286 drainEvent->process();
287 // Clear the drain event once we're done with it.
288 drainEvent = NULL;
289 }
290 }
291
292 Port *
293 Bus::findPort(Addr addr, int id)
294 {
295 /* An interval tree would be a better way to do this. --ali. */
296 int dest_id = -1;
297 AddrRangeIter iter;
298 range_map<Addr,int>::iterator i;
299
300 i = portMap.find(RangeSize(addr,1));
301 if (i != portMap.end())
302 dest_id = i->second;
303
304 // Check if this matches the default range
305 if (dest_id == -1) {
306 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
307 if (*iter == addr) {
308 DPRINTF(Bus, " found addr %#llx on default\n", addr);
309 return defaultPort;
310 }
311 }
312
313 if (responderSet) {
314 panic("Unable to find destination for addr (user set default "
315 "responder): %#llx", addr);
316 } else {
317 DPRINTF(Bus, "Unable to find destination for addr: %#llx, will use "
318 "default port", addr);
319
320 return defaultPort;
321 }
322 }
323
324
325 // we shouldn't be sending this back to where it came from
326 // do the snoop access and then we should terminate
327 // the cyclical call.
328 if (dest_id == id)
329 return 0;
330
331 return interfaces[dest_id];
332 }
333
334 std::vector<int>
335 Bus::findSnoopPorts(Addr addr, int id)
336 {
337 int i = 0;
338 AddrRangeIter iter;
339 std::vector<int> ports;
340
341 while (i < portSnoopList.size())
342 {
343 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
344 //Careful to not overlap ranges
345 //or snoop will be called more than once on the port
346
347 //@todo Fix this hack because ranges are overlapping
348 //need to make sure we dont't create overlapping ranges
349 bool hack_overlap = false;
350 int size = ports.size();
351 for (int j=0; j < size; j++) {
352 if (ports[j] == portSnoopList[i].portId)
353 hack_overlap = true;
354 }
355
356 if (!hack_overlap)
357 ports.push_back(portSnoopList[i].portId);
358 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
359 // portSnoopList[i].portId);
360 }
361 i++;
362 }
363 return ports;
364 }
365
366 Tick
367 Bus::atomicSnoop(PacketPtr pkt, Port *responder)
368 {
369 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
370 Tick response_time = 0;
371
372 while (!ports.empty())
373 {
374 if (interfaces[ports.back()] != responder) {
375 Tick response = interfaces[ports.back()]->sendAtomic(pkt);
376 if (response) {
377 assert(!response_time); //Multiple responders
378 response_time = response;
379 }
380 }
381 ports.pop_back();
382 }
383 return response_time;
384 }
385
386 void
387 Bus::functionalSnoop(PacketPtr pkt, Port *responder)
388 {
389 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
390
391 //The packet may be changed by another bus on snoops, restore the id after each
392 int id = pkt->getSrc();
393 while (!ports.empty() && pkt->result != Packet::Success)
394 {
395 if (interfaces[ports.back()] != responder)
396 interfaces[ports.back()]->sendFunctional(pkt);
397 ports.pop_back();
398 pkt->setSrc(id);
399 }
400 }
401
402 bool
403 Bus::timingSnoop(PacketPtr pkt, Port* responder)
404 {
405 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
406 bool success = true;
407
408 while (!ports.empty() && success)
409 {
410 if (interfaces[ports.back()] != responder) //Don't call if responder also, once will do
411 success = interfaces[ports.back()]->sendTiming(pkt);
412 ports.pop_back();
413 }
414
415 return success;
416 }
417
418
419 /** Function called by the port when the bus is receiving a Atomic
420 * transaction.*/
421 Tick
422 Bus::recvAtomic(PacketPtr pkt)
423 {
424 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
425 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
426 assert(pkt->getDest() == Packet::Broadcast);
427 pkt->flags |= SNOOP_COMMIT;
428
429 // Assume one bus cycle in order to get through. This may have
430 // some clock skew issues yet again...
431 pkt->finishTime = curTick + clock;
432
433 Port *port = findPort(pkt->getAddr(), pkt->getSrc());
434 Tick snoopTime = atomicSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
435
436 if (snoopTime)
437 return snoopTime; //Snoop satisfies it
438 else if (port)
439 return port->sendAtomic(pkt);
440 else
441 return 0;
442 }
443
444 /** Function called by the port when the bus is receiving a Functional
445 * transaction.*/
446 void
447 Bus::recvFunctional(PacketPtr pkt)
448 {
449 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
450 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
451 assert(pkt->getDest() == Packet::Broadcast);
452 pkt->flags |= SNOOP_COMMIT;
453
454 Port* port = findPort(pkt->getAddr(), pkt->getSrc());
455 functionalSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
456
457 // If the snooping found what we were looking for, we're done.
458 if (pkt->result != Packet::Success && port) {
459 port->sendFunctional(pkt);
460 }
461 }
462
463 /** Function called by the port when the bus is receiving a status change.*/
464 void
465 Bus::recvStatusChange(Port::Status status, int id)
466 {
467 AddrRangeList ranges;
468 AddrRangeList snoops;
469 AddrRangeIter iter;
470
471 assert(status == Port::RangeChange &&
472 "The other statuses need to be implemented.");
473
474 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
475
476 if (id == defaultId) {
477 defaultRange.clear();
478 // Only try to update these ranges if the user set a default responder.
479 if (responderSet) {
480 defaultPort->getPeerAddressRanges(ranges, snoops);
481 assert(snoops.size() == 0);
482 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
483 defaultRange.push_back(*iter);
484 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
485 iter->start, iter->end);
486 }
487 }
488 } else {
489
490 assert((id < maxId && id >= 0) || id == defaultId);
491 Port *port = interfaces[id];
492 range_map<Addr,int>::iterator portIter;
493 std::vector<DevMap>::iterator snoopIter;
494
495 // Clean out any previously existent ids
496 for (portIter = portMap.begin(); portIter != portMap.end(); ) {
497 if (portIter->second == id)
498 portMap.erase(portIter++);
499 else
500 portIter++;
501 }
502
503 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
504 if (snoopIter->portId == id)
505 snoopIter = portSnoopList.erase(snoopIter);
506 else
507 snoopIter++;
508 }
509
510 port->getPeerAddressRanges(ranges, snoops);
511
512 for(iter = snoops.begin(); iter != snoops.end(); iter++) {
513 DevMap dm;
514 dm.portId = id;
515 dm.range = *iter;
516
517 //@todo, make sure we don't overlap ranges
518 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
519 dm.range.start, dm.range.end, id);
520 portSnoopList.push_back(dm);
521 }
522
523 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
524 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
525 iter->start, iter->end, id);
526 if (portMap.insert(*iter, id) == portMap.end())
527 panic("Two devices with same range\n");
528
529 }
530 }
531 DPRINTF(MMU, "port list has %d entries\n", portMap.size());
532
533 // tell all our peers that our address range has changed.
534 // Don't tell the device that caused this change, it already knows
535 m5::hash_map<short,BusPort*>::iterator intIter;
536
537 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
538 if (intIter->first != id && intIter->first != funcPortId)
539 intIter->second->sendStatusChange(Port::RangeChange);
540
541 if (id != defaultId && defaultPort)
542 defaultPort->sendStatusChange(Port::RangeChange);
543 }
544
545 void
546 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
547 {
548 std::vector<DevMap>::iterator snoopIter;
549 range_map<Addr,int>::iterator portIter;
550 AddrRangeIter dflt_iter;
551 bool subset;
552
553 resp.clear();
554 snoop.clear();
555
556 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
557
558 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
559 dflt_iter++) {
560 resp.push_back(*dflt_iter);
561 DPRINTF(BusAddrRanges, " -- Dflt: %#llx : %#llx\n",dflt_iter->start,
562 dflt_iter->end);
563 }
564 for (portIter = portMap.begin(); portIter != portMap.end(); portIter++) {
565 subset = false;
566 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
567 dflt_iter++) {
568 if ((portIter->first.start < dflt_iter->start &&
569 portIter->first.end >= dflt_iter->start) ||
570 (portIter->first.start < dflt_iter->end &&
571 portIter->first.end >= dflt_iter->end))
572 fatal("Devices can not set ranges that itersect the default set\
573 but are not a subset of the default set.\n");
574 if (portIter->first.start >= dflt_iter->start &&
575 portIter->first.end <= dflt_iter->end) {
576 subset = true;
577 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
578 portIter->first.start, portIter->first.end);
579 }
580 }
581 if (portIter->second != id && !subset) {
582 resp.push_back(portIter->first);
583 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
584 portIter->first.start, portIter->first.end);
585 }
586 }
587
588 for (snoopIter = portSnoopList.begin();
589 snoopIter != portSnoopList.end(); snoopIter++)
590 {
591 if (snoopIter->portId != id) {
592 snoop.push_back(snoopIter->range);
593 DPRINTF(BusAddrRanges, " -- Snoop: %#llx : %#llx\n",
594 snoopIter->range.start, snoopIter->range.end);
595 //@todo We need to properly insert snoop ranges
596 //not overlapping the ranges (multiple)
597 }
598 }
599 }
600
601 unsigned int
602 Bus::drain(Event * de)
603 {
604 //We should check that we're not "doing" anything, and that noone is
605 //waiting. We might be idle but have someone waiting if the device we
606 //contacted for a retry didn't actually retry.
607 if (curTick >= tickNextIdle && retryList.size() == 0) {
608 return 0;
609 } else {
610 drainEvent = de;
611 return 1;
612 }
613 }
614
615 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
616
617 Param<int> bus_id;
618 Param<int> clock;
619 Param<int> width;
620 Param<bool> responder_set;
621
622 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
623
624 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
625 INIT_PARAM(bus_id, "a globally unique bus id"),
626 INIT_PARAM(clock, "bus clock speed"),
627 INIT_PARAM(width, "width of the bus (bits)"),
628 INIT_PARAM(responder_set, "Is a default responder set by the user")
629 END_INIT_SIM_OBJECT_PARAMS(Bus)
630
631 CREATE_SIM_OBJECT(Bus)
632 {
633 return new Bus(getInstanceName(), bus_id, clock, width, responder_set);
634 }
635
636 REGISTER_SIM_OBJECT("Bus", Bus)