Merge zizzer:/z/m5/Bitkeeper/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
71 /** Function called by the port when the bus is receiving a Timing
72 * transaction.*/
73 bool
74 Bus::recvTiming(Packet *pkt)
75 {
76 Port *port;
77 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
78 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
79
80 short dest = pkt->getDest();
81 if (dest == Packet::Broadcast) {
82 if (timingSnoop(pkt))
83 {
84 pkt->flags |= SNOOP_COMMIT;
85 bool success = timingSnoop(pkt);
86 assert(success);
87 if (pkt->flags & SATISFIED) {
88 //Cache-Cache transfer occuring
89 return true;
90 }
91 port = findPort(pkt->getAddr(), pkt->getSrc());
92 }
93 else
94 {
95 //Snoop didn't succeed
96 retryList.push_back(interfaces[pkt->getSrc()]);
97 return false;
98 }
99 } else {
100 assert(dest >= 0 && dest < interfaces.size());
101 assert(dest != pkt->getSrc()); // catch infinite loops
102 port = interfaces[dest];
103 }
104 if (port->sendTiming(pkt)) {
105 // packet was successfully sent, just return true.
106 return true;
107 }
108
109 // packet not successfully sent
110 retryList.push_back(interfaces[pkt->getSrc()]);
111 return false;
112 }
113
114 void
115 Bus::recvRetry(int id)
116 {
117 // Go through all the elements on the list calling sendRetry on each
118 // This is not very efficient at all but it works. Ultimately we should end
119 // up with something that is more intelligent.
120 int initialSize = retryList.size();
121 int i;
122 Port *p;
123
124 for (i = 0; i < initialSize; i++) {
125 assert(retryList.size() > 0);
126 p = retryList.front();
127 retryList.pop_front();
128 p->sendRetry();
129 }
130 }
131
132
133 Port *
134 Bus::findPort(Addr addr, int id)
135 {
136 /* An interval tree would be a better way to do this. --ali. */
137 int dest_id = -1;
138 int i = 0;
139 bool found = false;
140 AddrRangeIter iter;
141
142 while (i < portList.size() && !found)
143 {
144 if (portList[i].range == addr) {
145 dest_id = portList[i].portId;
146 found = true;
147 DPRINTF(Bus, " found addr %#llx on device %d\n", addr, dest_id);
148 }
149 i++;
150 }
151
152 // Check if this matches the default range
153 if (dest_id == -1) {
154 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
155 if (*iter == addr) {
156 DPRINTF(Bus, " found addr %#llx on default\n", addr);
157 return defaultPort;
158 }
159 }
160 panic("Unable to find destination for addr: %#llx", addr);
161 }
162
163
164 // we shouldn't be sending this back to where it came from
165 assert(dest_id != id);
166
167 return interfaces[dest_id];
168 }
169
170 std::vector<int>
171 Bus::findSnoopPorts(Addr addr, int id)
172 {
173 int i = 0;
174 AddrRangeIter iter;
175 std::vector<int> ports;
176
177 while (i < portSnoopList.size())
178 {
179 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
180 //Careful to not overlap ranges
181 //or snoop will be called more than once on the port
182 ports.push_back(portSnoopList[i].portId);
183 DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
184 portSnoopList[i].portId);
185 }
186 i++;
187 }
188 return ports;
189 }
190
191 void
192 Bus::atomicSnoop(Packet *pkt)
193 {
194 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
195
196 while (!ports.empty())
197 {
198 interfaces[ports.back()]->sendAtomic(pkt);
199 ports.pop_back();
200 }
201 }
202
203 bool
204 Bus::timingSnoop(Packet *pkt)
205 {
206 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
207 bool success = true;
208
209 while (!ports.empty() && success)
210 {
211 success = interfaces[ports.back()]->sendTiming(pkt);
212 ports.pop_back();
213 }
214
215 return success;
216 }
217
218
219 /** Function called by the port when the bus is receiving a Atomic
220 * transaction.*/
221 Tick
222 Bus::recvAtomic(Packet *pkt)
223 {
224 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
225 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
226 assert(pkt->getDest() == Packet::Broadcast);
227 atomicSnoop(pkt);
228 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
229 }
230
231 /** Function called by the port when the bus is receiving a Functional
232 * transaction.*/
233 void
234 Bus::recvFunctional(Packet *pkt)
235 {
236 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
237 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
238 assert(pkt->getDest() == Packet::Broadcast);
239 atomicSnoop(pkt);
240 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
241 }
242
243 /** Function called by the port when the bus is receiving a status change.*/
244 void
245 Bus::recvStatusChange(Port::Status status, int id)
246 {
247 AddrRangeList ranges;
248 AddrRangeList snoops;
249 int x;
250 AddrRangeIter iter;
251
252 assert(status == Port::RangeChange &&
253 "The other statuses need to be implemented.");
254
255 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
256
257 if (id == defaultId) {
258 defaultRange.clear();
259 defaultPort->getPeerAddressRanges(ranges, snoops);
260 assert(snoops.size() == 0);
261 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
262 defaultRange.push_back(*iter);
263 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
264 iter->start, iter->end);
265 }
266 } else {
267
268 assert((id < interfaces.size() && id >= 0) || id == -1);
269 Port *port = interfaces[id];
270 std::vector<DevMap>::iterator portIter;
271 std::vector<DevMap>::iterator snoopIter;
272
273 // Clean out any previously existent ids
274 for (portIter = portList.begin(); portIter != portList.end(); ) {
275 if (portIter->portId == id)
276 portIter = portList.erase(portIter);
277 else
278 portIter++;
279 }
280
281 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
282 if (snoopIter->portId == id)
283 snoopIter = portSnoopList.erase(snoopIter);
284 else
285 snoopIter++;
286 }
287
288 port->getPeerAddressRanges(ranges, snoops);
289
290 for(iter = snoops.begin(); iter != snoops.end(); iter++) {
291 DevMap dm;
292 dm.portId = id;
293 dm.range = *iter;
294
295 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
296 dm.range.start, dm.range.end, id);
297 portSnoopList.push_back(dm);
298 }
299
300 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
301 DevMap dm;
302 dm.portId = id;
303 dm.range = *iter;
304
305 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
306 dm.range.start, dm.range.end, id);
307 portList.push_back(dm);
308 }
309 }
310 DPRINTF(MMU, "port list has %d entries\n", portList.size());
311
312 // tell all our peers that our address range has changed.
313 // Don't tell the device that caused this change, it already knows
314 for (x = 0; x < interfaces.size(); x++)
315 if (x != id)
316 interfaces[x]->sendStatusChange(Port::RangeChange);
317
318 if (id != defaultId && defaultPort)
319 defaultPort->sendStatusChange(Port::RangeChange);
320 }
321
322 void
323 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
324 {
325 std::vector<DevMap>::iterator portIter;
326 AddrRangeIter dflt_iter;
327 bool subset;
328
329 resp.clear();
330 snoop.clear();
331
332 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
333
334 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
335 dflt_iter++) {
336 resp.push_back(*dflt_iter);
337 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",dflt_iter->start,
338 dflt_iter->end);
339 }
340 for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
341 subset = false;
342 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
343 dflt_iter++) {
344 if ((portIter->range.start < dflt_iter->start &&
345 portIter->range.end >= dflt_iter->start) ||
346 (portIter->range.start < dflt_iter->end &&
347 portIter->range.end >= dflt_iter->end))
348 fatal("Devices can not set ranges that itersect the default set\
349 but are not a subset of the default set.\n");
350 if (portIter->range.start >= dflt_iter->start &&
351 portIter->range.end <= dflt_iter->end) {
352 subset = true;
353 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
354 portIter->range.start, portIter->range.end);
355 }
356 }
357 if (portIter->portId != id && !subset) {
358 resp.push_back(portIter->range);
359 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
360 portIter->range.start, portIter->range.end);
361 }
362 }
363 }
364
365 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
366
367 Param<int> bus_id;
368
369 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
370
371 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
372 INIT_PARAM(bus_id, "a globally unique bus id")
373 END_INIT_SIM_OBJECT_PARAMS(Bus)
374
375 CREATE_SIM_OBJECT(Bus)
376 {
377 return new Bus(getInstanceName(), bus_id);
378 }
379
380 REGISTER_SIM_OBJECT("Bus", Bus)