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