Fix bus in FS mode.
[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(Packet *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 = interfaces[pkt->getSrc()];
148
149 // If the bus is busy, or other devices are in line ahead of the current
150 // one, put this device on the retry list.
151 if (tickNextIdle > curTick ||
152 (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
153 addToRetryList(pktPort);
154 return false;
155 }
156
157 short dest = pkt->getDest();
158 if (dest == Packet::Broadcast) {
159 if (timingSnoop(pkt)) {
160 pkt->flags |= SNOOP_COMMIT;
161 bool success = timingSnoop(pkt);
162 assert(success);
163 if (pkt->flags & SATISFIED) {
164 //Cache-Cache transfer occuring
165 if (inRetry) {
166 retryList.front()->onRetryList(false);
167 retryList.pop_front();
168 inRetry = false;
169 }
170 occupyBus(pkt);
171 return true;
172 }
173 port = findPort(pkt->getAddr(), pkt->getSrc());
174 } else {
175 //Snoop didn't succeed
176 DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
177 addToRetryList(pktPort);
178 return false;
179 }
180 } else {
181 assert(dest >= 0 && dest < interfaces.size());
182 assert(dest != pkt->getSrc()); // catch infinite loops
183 port = interfaces[dest];
184 }
185
186 occupyBus(pkt);
187
188 if (port->sendTiming(pkt)) {
189 // Packet was successfully sent. Return true.
190 // Also take care of retries
191 if (inRetry) {
192 DPRINTF(Bus, "Remove retry from list %i\n", retryList.front());
193 retryList.front()->onRetryList(false);
194 retryList.pop_front();
195 inRetry = false;
196 }
197 return true;
198 }
199
200 // Packet not successfully sent. Leave or put it on the retry list.
201 DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
202 addToRetryList(pktPort);
203 return false;
204 }
205
206 void
207 Bus::recvRetry(int id)
208 {
209 DPRINTF(Bus, "Received a retry\n");
210 // If there's anything waiting, and the bus isn't busy...
211 if (retryList.size() && curTick >= tickNextIdle) {
212 //retryingPort = retryList.front();
213 inRetry = true;
214 DPRINTF(Bus, "Sending a retry\n");
215 retryList.front()->sendRetry();
216 // If inRetry is still true, sendTiming wasn't called
217 if (inRetry)
218 {
219 retryList.front()->onRetryList(false);
220 retryList.pop_front();
221 inRetry = false;
222
223 //Bring tickNextIdle up to the present
224 while (tickNextIdle < curTick)
225 tickNextIdle += clock;
226
227 //Burn a cycle for the missed grant.
228 tickNextIdle += clock;
229
230 if (!busIdle.scheduled()) {
231 busIdle.schedule(tickNextIdle);
232 } else {
233 busIdle.reschedule(tickNextIdle);
234 }
235 }
236 }
237 }
238
239 Port *
240 Bus::findPort(Addr addr, int id)
241 {
242 /* An interval tree would be a better way to do this. --ali. */
243 int dest_id = -1;
244 int i = 0;
245 bool found = false;
246 AddrRangeIter iter;
247
248 while (i < portList.size() && !found)
249 {
250 if (portList[i].range == addr) {
251 dest_id = portList[i].portId;
252 found = true;
253 DPRINTF(Bus, " found addr %#llx on device %d\n", addr, dest_id);
254 }
255 i++;
256 }
257
258 // Check if this matches the default range
259 if (dest_id == -1) {
260 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
261 if (*iter == addr) {
262 DPRINTF(Bus, " found addr %#llx on default\n", addr);
263 return defaultPort;
264 }
265 }
266 panic("Unable to find destination for addr: %#llx", addr);
267 }
268
269
270 // we shouldn't be sending this back to where it came from
271 assert(dest_id != id);
272
273 return interfaces[dest_id];
274 }
275
276 std::vector<int>
277 Bus::findSnoopPorts(Addr addr, int id)
278 {
279 int i = 0;
280 AddrRangeIter iter;
281 std::vector<int> ports;
282
283 while (i < portSnoopList.size())
284 {
285 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
286 //Careful to not overlap ranges
287 //or snoop will be called more than once on the port
288 ports.push_back(portSnoopList[i].portId);
289 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
290 // portSnoopList[i].portId);
291 }
292 i++;
293 }
294 return ports;
295 }
296
297 Tick
298 Bus::atomicSnoop(Packet *pkt)
299 {
300 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
301 Tick response_time = 0;
302
303 while (!ports.empty())
304 {
305 Tick response = interfaces[ports.back()]->sendAtomic(pkt);
306 if (response) {
307 assert(!response_time); //Multiple responders
308 response_time = response;
309 }
310 ports.pop_back();
311 }
312 return response_time;
313 }
314
315 void
316 Bus::functionalSnoop(Packet *pkt)
317 {
318 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
319
320 while (!ports.empty())
321 {
322 interfaces[ports.back()]->sendFunctional(pkt);
323 ports.pop_back();
324 }
325 }
326
327 bool
328 Bus::timingSnoop(Packet *pkt)
329 {
330 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
331 bool success = true;
332
333 while (!ports.empty() && success)
334 {
335 success = interfaces[ports.back()]->sendTiming(pkt);
336 ports.pop_back();
337 }
338
339 return success;
340 }
341
342
343 /** Function called by the port when the bus is receiving a Atomic
344 * transaction.*/
345 Tick
346 Bus::recvAtomic(Packet *pkt)
347 {
348 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
349 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
350 assert(pkt->getDest() == Packet::Broadcast);
351 Tick snoopTime = atomicSnoop(pkt);
352 if (snoopTime)
353 return snoopTime; //Snoop satisfies it
354 else
355 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
356 }
357
358 /** Function called by the port when the bus is receiving a Functional
359 * transaction.*/
360 void
361 Bus::recvFunctional(Packet *pkt)
362 {
363 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
364 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
365 assert(pkt->getDest() == Packet::Broadcast);
366 functionalSnoop(pkt);
367 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
368 }
369
370 /** Function called by the port when the bus is receiving a status change.*/
371 void
372 Bus::recvStatusChange(Port::Status status, int id)
373 {
374 AddrRangeList ranges;
375 AddrRangeList snoops;
376 int x;
377 AddrRangeIter iter;
378
379 assert(status == Port::RangeChange &&
380 "The other statuses need to be implemented.");
381
382 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
383
384 if (id == defaultId) {
385 defaultRange.clear();
386 defaultPort->getPeerAddressRanges(ranges, snoops);
387 assert(snoops.size() == 0);
388 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
389 defaultRange.push_back(*iter);
390 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
391 iter->start, iter->end);
392 }
393 } else {
394
395 assert((id < interfaces.size() && id >= 0) || id == -1);
396 Port *port = interfaces[id];
397 std::vector<DevMap>::iterator portIter;
398 std::vector<DevMap>::iterator snoopIter;
399
400 // Clean out any previously existent ids
401 for (portIter = portList.begin(); portIter != portList.end(); ) {
402 if (portIter->portId == id)
403 portIter = portList.erase(portIter);
404 else
405 portIter++;
406 }
407
408 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
409 if (snoopIter->portId == id)
410 snoopIter = portSnoopList.erase(snoopIter);
411 else
412 snoopIter++;
413 }
414
415 port->getPeerAddressRanges(ranges, snoops);
416
417 for(iter = snoops.begin(); iter != snoops.end(); iter++) {
418 DevMap dm;
419 dm.portId = id;
420 dm.range = *iter;
421
422 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
423 dm.range.start, dm.range.end, id);
424 portSnoopList.push_back(dm);
425 }
426
427 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
428 DevMap dm;
429 dm.portId = id;
430 dm.range = *iter;
431
432 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
433 dm.range.start, dm.range.end, id);
434 portList.push_back(dm);
435 }
436 }
437 DPRINTF(MMU, "port list has %d entries\n", portList.size());
438
439 // tell all our peers that our address range has changed.
440 // Don't tell the device that caused this change, it already knows
441 for (x = 0; x < interfaces.size(); x++)
442 if (x != id)
443 interfaces[x]->sendStatusChange(Port::RangeChange);
444
445 if (id != defaultId && defaultPort)
446 defaultPort->sendStatusChange(Port::RangeChange);
447 }
448
449 void
450 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
451 {
452 std::vector<DevMap>::iterator portIter;
453 AddrRangeIter dflt_iter;
454 bool subset;
455
456 resp.clear();
457 snoop.clear();
458
459 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
460
461 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
462 dflt_iter++) {
463 resp.push_back(*dflt_iter);
464 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",dflt_iter->start,
465 dflt_iter->end);
466 }
467 for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
468 subset = false;
469 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
470 dflt_iter++) {
471 if ((portIter->range.start < dflt_iter->start &&
472 portIter->range.end >= dflt_iter->start) ||
473 (portIter->range.start < dflt_iter->end &&
474 portIter->range.end >= dflt_iter->end))
475 fatal("Devices can not set ranges that itersect the default set\
476 but are not a subset of the default set.\n");
477 if (portIter->range.start >= dflt_iter->start &&
478 portIter->range.end <= dflt_iter->end) {
479 subset = true;
480 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
481 portIter->range.start, portIter->range.end);
482 }
483 }
484 if (portIter->portId != id && !subset) {
485 resp.push_back(portIter->range);
486 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
487 portIter->range.start, portIter->range.end);
488 }
489 }
490 }
491
492 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
493
494 Param<int> bus_id;
495 Param<int> clock;
496 Param<int> width;
497
498 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
499
500 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
501 INIT_PARAM(bus_id, "a globally unique bus id"),
502 INIT_PARAM(clock, "bus clock speed"),
503 INIT_PARAM(width, "width of the bus (bits)")
504 END_INIT_SIM_OBJECT_PARAMS(Bus)
505
506 CREATE_SIM_OBJECT(Bus)
507 {
508 return new Bus(getInstanceName(), bus_id, clock, width);
509 }
510
511 REGISTER_SIM_OBJECT("Bus", Bus)