Fixes for functional path.
[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 void
204 Bus::functionalSnoop(Packet *pkt)
205 {
206 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
207
208 while (!ports.empty())
209 {
210 interfaces[ports.back()]->sendFunctional(pkt);
211 ports.pop_back();
212 }
213 }
214
215 bool
216 Bus::timingSnoop(Packet *pkt)
217 {
218 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
219 bool success = true;
220
221 while (!ports.empty() && success)
222 {
223 success = interfaces[ports.back()]->sendTiming(pkt);
224 ports.pop_back();
225 }
226
227 return success;
228 }
229
230
231 /** Function called by the port when the bus is receiving a Atomic
232 * transaction.*/
233 Tick
234 Bus::recvAtomic(Packet *pkt)
235 {
236 DPRINTF(Bus, "recvAtomic: 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 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
241 }
242
243 /** Function called by the port when the bus is receiving a Functional
244 * transaction.*/
245 void
246 Bus::recvFunctional(Packet *pkt)
247 {
248 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
249 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
250 assert(pkt->getDest() == Packet::Broadcast);
251 functionalSnoop(pkt);
252 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
253 }
254
255 /** Function called by the port when the bus is receiving a status change.*/
256 void
257 Bus::recvStatusChange(Port::Status status, int id)
258 {
259 AddrRangeList ranges;
260 AddrRangeList snoops;
261 int x;
262 AddrRangeIter iter;
263
264 assert(status == Port::RangeChange &&
265 "The other statuses need to be implemented.");
266
267 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
268
269 if (id == defaultId) {
270 defaultRange.clear();
271 defaultPort->getPeerAddressRanges(ranges, snoops);
272 assert(snoops.size() == 0);
273 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
274 defaultRange.push_back(*iter);
275 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
276 iter->start, iter->end);
277 }
278 } else {
279
280 assert((id < interfaces.size() && id >= 0) || id == -1);
281 Port *port = interfaces[id];
282 std::vector<DevMap>::iterator portIter;
283 std::vector<DevMap>::iterator snoopIter;
284
285 // Clean out any previously existent ids
286 for (portIter = portList.begin(); portIter != portList.end(); ) {
287 if (portIter->portId == id)
288 portIter = portList.erase(portIter);
289 else
290 portIter++;
291 }
292
293 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
294 if (snoopIter->portId == id)
295 snoopIter = portSnoopList.erase(snoopIter);
296 else
297 snoopIter++;
298 }
299
300 port->getPeerAddressRanges(ranges, snoops);
301
302 for(iter = snoops.begin(); iter != snoops.end(); iter++) {
303 DevMap dm;
304 dm.portId = id;
305 dm.range = *iter;
306
307 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
308 dm.range.start, dm.range.end, id);
309 portSnoopList.push_back(dm);
310 }
311
312 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
313 DevMap dm;
314 dm.portId = id;
315 dm.range = *iter;
316
317 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
318 dm.range.start, dm.range.end, id);
319 portList.push_back(dm);
320 }
321 }
322 DPRINTF(MMU, "port list has %d entries\n", portList.size());
323
324 // tell all our peers that our address range has changed.
325 // Don't tell the device that caused this change, it already knows
326 for (x = 0; x < interfaces.size(); x++)
327 if (x != id)
328 interfaces[x]->sendStatusChange(Port::RangeChange);
329
330 if (id != defaultId && defaultPort)
331 defaultPort->sendStatusChange(Port::RangeChange);
332 }
333
334 void
335 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
336 {
337 std::vector<DevMap>::iterator portIter;
338 AddrRangeIter dflt_iter;
339 bool subset;
340
341 resp.clear();
342 snoop.clear();
343
344 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
345
346 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
347 dflt_iter++) {
348 resp.push_back(*dflt_iter);
349 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",dflt_iter->start,
350 dflt_iter->end);
351 }
352 for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
353 subset = false;
354 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
355 dflt_iter++) {
356 if ((portIter->range.start < dflt_iter->start &&
357 portIter->range.end >= dflt_iter->start) ||
358 (portIter->range.start < dflt_iter->end &&
359 portIter->range.end >= dflt_iter->end))
360 fatal("Devices can not set ranges that itersect the default set\
361 but are not a subset of the default set.\n");
362 if (portIter->range.start >= dflt_iter->start &&
363 portIter->range.end <= dflt_iter->end) {
364 subset = true;
365 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
366 portIter->range.start, portIter->range.end);
367 }
368 }
369 if (portIter->portId != id && !subset) {
370 resp.push_back(portIter->range);
371 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
372 portIter->range.start, portIter->range.end);
373 }
374 }
375 }
376
377 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
378
379 Param<int> bus_id;
380
381 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
382
383 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
384 INIT_PARAM(bus_id, "a globally unique bus id")
385 END_INIT_SIM_OBJECT_PARAMS(Bus)
386
387 CREATE_SIM_OBJECT(Bus)
388 {
389 return new Bus(getInstanceName(), bus_id);
390 }
391
392 REGISTER_SIM_OBJECT("Bus", Bus)