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