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