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