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