Debugging info
[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 // if_name ignored? forced to be empty?
54 int id = interfaces.size();
55 BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
56 interfaces.push_back(bp);
57 return bp;
58 }
59
60 /** Get the ranges of anyone other buses that we are connected to. */
61 void
62 Bus::init()
63 {
64 std::vector<Port*>::iterator intIter;
65
66 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
67 (*intIter)->sendStatusChange(Port::RangeChange);
68 }
69
70 Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
71 {}
72
73 void Bus::BusFreeEvent::process()
74 {
75 bus->recvRetry(-1);
76 }
77
78 const char * Bus::BusFreeEvent::description()
79 {
80 return "bus became available";
81 }
82
83 /** Function called by the port when the bus is receiving a Timing
84 * transaction.*/
85 bool
86 Bus::recvTiming(Packet *pkt)
87 {
88 Port *port;
89 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
90 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
91
92 Port *pktPort = interfaces[pkt->getSrc()];
93
94 // If the bus is busy, or other devices are in line ahead of the current
95 // one, put this device on the retry list.
96 if (tickNextIdle > curTick ||
97 (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
98 DPRINTF(Bus, "Adding RETRY for %i\n", pktPort);
99 addToRetryList(pktPort);
100 return false;
101 }
102
103 short dest = pkt->getDest();
104 if (dest == Packet::Broadcast) {
105 if (timingSnoop(pkt)) {
106 pkt->flags |= SNOOP_COMMIT;
107 bool success = timingSnoop(pkt);
108 assert(success);
109 if (pkt->flags & SATISFIED) {
110 //Cache-Cache transfer occuring
111 if (inRetry) {
112 DPRINTF(Bus, "Removing RETRY %i\n", retryList.front());
113 retryList.pop_front();
114 inRetry = false;
115 }
116 return true;
117 }
118 port = findPort(pkt->getAddr(), pkt->getSrc());
119 } else {
120 //Snoop didn't succeed
121 DPRINTF(Bus, "Snoop caused adding to RETRY list %i\n", pktPort);
122 addToRetryList(pktPort);
123 return false;
124 }
125 } else {
126 assert(dest >= 0 && dest < interfaces.size());
127 assert(dest != pkt->getSrc()); // catch infinite loops
128 port = interfaces[dest];
129 }
130
131 //Bring tickNextIdle up to the present tick
132 //There is some potential ambiguity where a cycle starts, which might make
133 //a difference when devices are acting right around a cycle boundary. Using
134 //a < allows things which happen exactly on a cycle boundary to take up only
135 //the following cycle. Anthing that happens later will have to "wait" for
136 //the end of that cycle, and then start using the bus after that.
137 while (tickNextIdle < curTick)
138 tickNextIdle += clock;
139
140 // The packet will be sent. Figure out how long it occupies the bus, and
141 // how much of that time is for the first "word", aka bus width.
142 int numCycles = 0;
143 // Requests need one cycle to send an address
144 if (pkt->isRequest())
145 numCycles++;
146 else if (pkt->isResponse() || pkt->hasData()) {
147 // If a packet has data, it needs ceil(size/width) cycles to send it
148 // We're using the "adding instead of dividing" trick again here
149 if (pkt->hasData()) {
150 int dataSize = pkt->getSize();
151 for (int transmitted = 0; transmitted < dataSize;
152 transmitted += width) {
153 numCycles++;
154 }
155 } else {
156 // If the packet didn't have data, it must have been a response.
157 // Those use the bus for one cycle to send their data.
158 numCycles++;
159 }
160 }
161
162 // The first word will be delivered after the current tick, the delivery
163 // of the address if any, and one bus cycle to deliver the data
164 pkt->firstWordTime =
165 tickNextIdle +
166 pkt->isRequest() ? clock : 0 +
167 clock;
168
169 //Advance it numCycles bus cycles.
170 //XXX Should this use the repeated addition trick as well?
171 tickNextIdle += (numCycles * clock);
172 if (!busIdle.scheduled()) {
173 busIdle.schedule(tickNextIdle);
174 } else {
175 busIdle.reschedule(tickNextIdle);
176 }
177 DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
178 curTick, tickNextIdle);
179
180 // The bus will become idle once the current packet is delivered.
181 pkt->finishTime = tickNextIdle;
182
183 if (port->sendTiming(pkt)) {
184 // Packet was successfully sent. Return true.
185 // Also take care of retries
186 if (inRetry) {
187 DPRINTF(Bus, "Remove retry from list %i\n", retryList.front());
188 retryList.pop_front();
189 inRetry = false;
190 }
191 return true;
192 }
193
194 // Packet not successfully sent. Leave or put it on the retry list.
195 DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
196 addToRetryList(pktPort);
197 return false;
198 }
199
200 void
201 Bus::recvRetry(int id)
202 {
203 DPRINTF(Bus, "Received a retry\n");
204 // If there's anything waiting...
205 if (retryList.size()) {
206 //retryingPort = retryList.front();
207 inRetry = true;
208 DPRINTF(Bus, "Sending a retry\n");
209 retryList.front()->sendRetry();
210 // If inRetry is still true, sendTiming wasn't called
211 if (inRetry)
212 panic("Port %s didn't call sendTiming in it's recvRetry\n",\
213 retryList.front()->getPeer()->name());
214 //assert(!inRetry);
215 }
216 }
217
218 Port *
219 Bus::findPort(Addr addr, int id)
220 {
221 /* An interval tree would be a better way to do this. --ali. */
222 int dest_id = -1;
223 int i = 0;
224 bool found = false;
225 AddrRangeIter iter;
226
227 while (i < portList.size() && !found)
228 {
229 if (portList[i].range == addr) {
230 dest_id = portList[i].portId;
231 found = true;
232 DPRINTF(Bus, " found addr %#llx on device %d\n", addr, dest_id);
233 }
234 i++;
235 }
236
237 // Check if this matches the default range
238 if (dest_id == -1) {
239 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
240 if (*iter == addr) {
241 DPRINTF(Bus, " found addr %#llx on default\n", addr);
242 return defaultPort;
243 }
244 }
245 panic("Unable to find destination for addr: %#llx", addr);
246 }
247
248
249 // we shouldn't be sending this back to where it came from
250 assert(dest_id != id);
251
252 return interfaces[dest_id];
253 }
254
255 std::vector<int>
256 Bus::findSnoopPorts(Addr addr, int id)
257 {
258 int i = 0;
259 AddrRangeIter iter;
260 std::vector<int> ports;
261
262 while (i < portSnoopList.size())
263 {
264 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
265 //Careful to not overlap ranges
266 //or snoop will be called more than once on the port
267 ports.push_back(portSnoopList[i].portId);
268 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
269 // portSnoopList[i].portId);
270 }
271 i++;
272 }
273 return ports;
274 }
275
276 void
277 Bus::atomicSnoop(Packet *pkt)
278 {
279 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
280
281 while (!ports.empty())
282 {
283 interfaces[ports.back()]->sendAtomic(pkt);
284 ports.pop_back();
285 }
286 }
287
288 void
289 Bus::functionalSnoop(Packet *pkt)
290 {
291 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
292
293 while (!ports.empty())
294 {
295 interfaces[ports.back()]->sendFunctional(pkt);
296 ports.pop_back();
297 }
298 }
299
300 bool
301 Bus::timingSnoop(Packet *pkt)
302 {
303 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
304 bool success = true;
305
306 while (!ports.empty() && success)
307 {
308 success = interfaces[ports.back()]->sendTiming(pkt);
309 ports.pop_back();
310 }
311
312 return success;
313 }
314
315
316 /** Function called by the port when the bus is receiving a Atomic
317 * transaction.*/
318 Tick
319 Bus::recvAtomic(Packet *pkt)
320 {
321 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
322 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
323 assert(pkt->getDest() == Packet::Broadcast);
324 atomicSnoop(pkt);
325 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
326 }
327
328 /** Function called by the port when the bus is receiving a Functional
329 * transaction.*/
330 void
331 Bus::recvFunctional(Packet *pkt)
332 {
333 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
334 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
335 assert(pkt->getDest() == Packet::Broadcast);
336 functionalSnoop(pkt);
337 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
338 }
339
340 /** Function called by the port when the bus is receiving a status change.*/
341 void
342 Bus::recvStatusChange(Port::Status status, int id)
343 {
344 AddrRangeList ranges;
345 AddrRangeList snoops;
346 int x;
347 AddrRangeIter iter;
348
349 assert(status == Port::RangeChange &&
350 "The other statuses need to be implemented.");
351
352 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
353
354 if (id == defaultId) {
355 defaultRange.clear();
356 defaultPort->getPeerAddressRanges(ranges, snoops);
357 assert(snoops.size() == 0);
358 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
359 defaultRange.push_back(*iter);
360 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
361 iter->start, iter->end);
362 }
363 } else {
364
365 assert((id < interfaces.size() && id >= 0) || id == -1);
366 Port *port = interfaces[id];
367 std::vector<DevMap>::iterator portIter;
368 std::vector<DevMap>::iterator snoopIter;
369
370 // Clean out any previously existent ids
371 for (portIter = portList.begin(); portIter != portList.end(); ) {
372 if (portIter->portId == id)
373 portIter = portList.erase(portIter);
374 else
375 portIter++;
376 }
377
378 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
379 if (snoopIter->portId == id)
380 snoopIter = portSnoopList.erase(snoopIter);
381 else
382 snoopIter++;
383 }
384
385 port->getPeerAddressRanges(ranges, snoops);
386
387 for(iter = snoops.begin(); iter != snoops.end(); iter++) {
388 DevMap dm;
389 dm.portId = id;
390 dm.range = *iter;
391
392 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
393 dm.range.start, dm.range.end, id);
394 portSnoopList.push_back(dm);
395 }
396
397 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
398 DevMap dm;
399 dm.portId = id;
400 dm.range = *iter;
401
402 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
403 dm.range.start, dm.range.end, id);
404 portList.push_back(dm);
405 }
406 }
407 DPRINTF(MMU, "port list has %d entries\n", portList.size());
408
409 // tell all our peers that our address range has changed.
410 // Don't tell the device that caused this change, it already knows
411 for (x = 0; x < interfaces.size(); x++)
412 if (x != id)
413 interfaces[x]->sendStatusChange(Port::RangeChange);
414
415 if (id != defaultId && defaultPort)
416 defaultPort->sendStatusChange(Port::RangeChange);
417 }
418
419 void
420 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
421 {
422 std::vector<DevMap>::iterator portIter;
423 AddrRangeIter dflt_iter;
424 bool subset;
425
426 resp.clear();
427 snoop.clear();
428
429 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
430
431 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
432 dflt_iter++) {
433 resp.push_back(*dflt_iter);
434 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",dflt_iter->start,
435 dflt_iter->end);
436 }
437 for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
438 subset = false;
439 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
440 dflt_iter++) {
441 if ((portIter->range.start < dflt_iter->start &&
442 portIter->range.end >= dflt_iter->start) ||
443 (portIter->range.start < dflt_iter->end &&
444 portIter->range.end >= dflt_iter->end))
445 fatal("Devices can not set ranges that itersect the default set\
446 but are not a subset of the default set.\n");
447 if (portIter->range.start >= dflt_iter->start &&
448 portIter->range.end <= dflt_iter->end) {
449 subset = true;
450 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
451 portIter->range.start, portIter->range.end);
452 }
453 }
454 if (portIter->portId != id && !subset) {
455 resp.push_back(portIter->range);
456 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
457 portIter->range.start, portIter->range.end);
458 }
459 }
460 }
461
462 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
463
464 Param<int> bus_id;
465 Param<int> clock;
466 Param<int> width;
467
468 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
469
470 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
471 INIT_PARAM(bus_id, "a globally unique bus id"),
472 INIT_PARAM(clock, "bus clock speed"),
473 INIT_PARAM(width, "width of the bus (bits)")
474 END_INIT_SIM_OBJECT_PARAMS(Bus)
475
476 CREATE_SIM_OBJECT(Bus)
477 {
478 return new Bus(getInstanceName(), bus_id, clock, width);
479 }
480
481 REGISTER_SIM_OBJECT("Bus", Bus)