A possible implementation of a multiplexed 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(0);
76 }
77
78 const char * Bus::BusFreeEvent::description()
79 {
80 return "bus became available";
81 }
82
83 void
84 Bus::occupyBus(int numCycles)
85 {
86 //Move up when the bus will next be free
87 //We avoid the use of divide by adding repeatedly
88 //This should be faster if the value is updated frequently, but should
89 //be may be slower otherwise.
90
91 //Bring tickNextIdle up to the present tick
92 //There is some potential ambiguity where a cycle starts, which might make
93 //a difference when devices are acting right around a cycle boundary. Using
94 //a < allows things which happen exactly on a cycle boundary to take up only
95 //the following cycle. Anthing that happens later will have to "wait" for the
96 //end of that cycle, and then start using the bus after that.
97 while (tickNextIdle < curTick)
98 tickNextIdle += clock;
99 //Advance it numCycles bus cycles.
100 //XXX Should this use the repeating add trick as well?
101 tickNextIdle += (numCycles * clock);
102 if (!busIdle.scheduled()) {
103 busIdle.schedule(tickNextIdle);
104 } else {
105 busIdle.reschedule(tickNextIdle);
106 }
107 }
108
109 /** Function called by the port when the bus is receiving a Timing
110 * transaction.*/
111 bool
112 Bus::recvTiming(Packet *pkt)
113 {
114 Port *port;
115 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
116 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
117
118 Port *pktPort = interfaces[pkt->getSrc()];
119
120 // If the bus is busy, or other devices are in line ahead of the current one,
121 // put this device on the retry list.
122 if (tickNextIdle > curTick || (retryList.size() && pktPort != retryingPort)) {
123 addToRetryList(pktPort);
124 return false;
125 }
126
127 // If the bus is blocked, make the device wait.
128 if (!(port = findDestPort(pkt, pkt->getSrc()))) {
129 addToRetryList(pktPort);
130 return false;
131 }
132
133 // The packet will be sent. Figure out how long it occupies the bus.
134 int numCycles = 0;
135 // Requests need one cycle to send an address
136 if (pkt->isRequest())
137 numCycles++;
138 else if (pkt->isResponse() || pkt->hasData()) {
139 // If a packet has data, it needs ceil(size/width) cycles to send it
140 // We're using the "adding instead of dividing" trick again here
141 if (pkt->hasData()) {
142 int dataSize = pkt->getSize();
143 for (int transmitted = 0; transmitted < dataSize;
144 transmitted += width) {
145 numCycles++;
146 }
147 } else {
148 // If the packet didn't have data, it must have been a response.
149 // Those use the bus for one cycle to send their data.
150 numCycles++;
151 }
152 }
153
154 occupyBus(numCycles);
155
156 if (port->sendTiming(pkt)) {
157 // Packet was successfully sent. Return true.
158 return true;
159 }
160
161 // Packet not successfully sent. Leave or put it on the retry list.
162 addToRetryList(pktPort);
163 return false;
164 }
165
166 void
167 Bus::recvRetry(int id)
168 {
169 //If there's anything waiting...
170 if (retryList.size()) {
171 retryingPort = retryList.front();
172 retryingPort->sendRetry();
173 //If the retryingPort pointer isn't null, either sendTiming wasn't
174 //called, or it was and the packet was successfully sent.
175 if (retryingPort) {
176 retryList.pop_front();
177 retryingPort = 0;
178 }
179 }
180 }
181
182 Port *
183 Bus::findDestPort(PacketPtr pkt, int id)
184 {
185 Port * port = NULL;
186 short dest = pkt->getDest();
187
188 if (dest == Packet::Broadcast) {
189 if (timingSnoopPhase1(pkt)) {
190 timingSnoopPhase2(pkt);
191 port = findPort(pkt->getAddr(), pkt->getSrc());
192 }
193 //else, port stays NULL
194 } else {
195 assert(dest >= 0 && dest < interfaces.size());
196 assert(dest != pkt->getSrc()); // catch infinite loops
197 port = interfaces[dest];
198 }
199 return port;
200 }
201
202 Port *
203 Bus::findPort(Addr addr, int id)
204 {
205 /* An interval tree would be a better way to do this. --ali. */
206 int dest_id = -1;
207 int i = 0;
208 bool found = false;
209 AddrRangeIter iter;
210
211 while (i < portList.size() && !found)
212 {
213 if (portList[i].range == addr) {
214 dest_id = portList[i].portId;
215 found = true;
216 DPRINTF(Bus, " found addr %#llx on device %d\n", addr, dest_id);
217 }
218 i++;
219 }
220
221 // Check if this matches the default range
222 if (dest_id == -1) {
223 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
224 if (*iter == addr) {
225 DPRINTF(Bus, " found addr %#llx on default\n", addr);
226 return defaultPort;
227 }
228 }
229 panic("Unable to find destination for addr: %#llx", addr);
230 }
231
232
233 // we shouldn't be sending this back to where it came from
234 assert(dest_id != id);
235
236 return interfaces[dest_id];
237 }
238
239 std::vector<int>
240 Bus::findSnoopPorts(Addr addr, int id)
241 {
242 int i = 0;
243 AddrRangeIter iter;
244 std::vector<int> ports;
245
246 while (i < portSnoopList.size())
247 {
248 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
249 //Careful to not overlap ranges
250 //or snoop will be called more than once on the port
251 ports.push_back(portSnoopList[i].portId);
252 DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
253 portSnoopList[i].portId);
254 }
255 i++;
256 }
257 return ports;
258 }
259
260 void
261 Bus::atomicSnoop(Packet *pkt)
262 {
263 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
264
265 while (!ports.empty())
266 {
267 interfaces[ports.back()]->sendAtomic(pkt);
268 ports.pop_back();
269 }
270 }
271
272 bool
273 Bus::timingSnoopPhase1(Packet *pkt)
274 {
275 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
276 bool success = true;
277
278 while (!ports.empty() && success)
279 {
280 snoopCallbacks.push_back(ports.back());
281 success = interfaces[ports.back()]->sendTiming(pkt);
282 ports.pop_back();
283 }
284 if (!success)
285 {
286 while (!snoopCallbacks.empty())
287 {
288 interfaces[snoopCallbacks.back()]->sendStatusChange(Port::SnoopSquash);
289 snoopCallbacks.pop_back();
290 }
291 return false;
292 }
293 return true;
294 }
295
296 void
297 Bus::timingSnoopPhase2(Packet *pkt)
298 {
299 bool success;
300 pkt->flags |= SNOOP_COMMIT;
301 while (!snoopCallbacks.empty())
302 {
303 success = interfaces[snoopCallbacks.back()]->sendTiming(pkt);
304 //We should not fail on snoop callbacks
305 assert(success);
306 snoopCallbacks.pop_back();
307 }
308 }
309
310 /** Function called by the port when the bus is receiving a Atomic
311 * transaction.*/
312 Tick
313 Bus::recvAtomic(Packet *pkt)
314 {
315 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
316 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
317 assert(pkt->getDest() == Packet::Broadcast);
318 atomicSnoop(pkt);
319 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
320 }
321
322 /** Function called by the port when the bus is receiving a Functional
323 * transaction.*/
324 void
325 Bus::recvFunctional(Packet *pkt)
326 {
327 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
328 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
329 assert(pkt->getDest() == Packet::Broadcast);
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)