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