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