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