Merge zeep.pool:/z/saidi/work/m5.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
29 /**
30 * @file Definition of a bus object.
31 */
32
33
34 #include "base/trace.hh"
35 #include "mem/bus.hh"
36 #include "sim/builder.hh"
37
38 Port *
39 Bus::getPort(const std::string &if_name)
40 {
41 // if_name ignored? forced to be empty?
42 int id = interfaces.size();
43 BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
44 interfaces.push_back(bp);
45 return bp;
46 }
47
48 /** Get the ranges of anyone that we are connected to. */
49 void
50 Bus::init()
51 {
52 std::vector<Port*>::iterator intIter;
53 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
54 (*intIter)->sendStatusChange(Port::RangeChange);
55 }
56
57
58 /** Function called by the port when the bus is receiving a Timing
59 * transaction.*/
60 bool
61 Bus::recvTiming(Packet *pkt)
62 {
63 Port *port;
64 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
65 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
66
67 short dest = pkt->getDest();
68 if (dest == Packet::Broadcast) {
69 port = findPort(pkt->getAddr(), pkt->getSrc());
70 } else {
71 assert(dest >= 0 && dest < interfaces.size());
72 assert(dest != pkt->getSrc()); // catch infinite loops
73 port = interfaces[dest];
74 }
75 return port->sendTiming(pkt);
76 }
77
78 Port *
79 Bus::findPort(Addr addr, int id)
80 {
81 /* An interval tree would be a better way to do this. --ali. */
82 int dest_id = -1;
83 int i = 0;
84 bool found = false;
85
86 while (i < portList.size() && !found)
87 {
88 if (portList[i].range == addr) {
89 dest_id = portList[i].portId;
90 found = true;
91 DPRINTF(Bus, " found addr 0x%llx on device %d\n", addr, dest_id);
92 }
93 i++;
94 }
95 if (dest_id == -1)
96 panic("Unable to find destination for addr: %llx", addr);
97
98 // we shouldn't be sending this back to where it came from
99 assert(dest_id != id);
100
101 return interfaces[dest_id];
102 }
103
104 /** Function called by the port when the bus is receiving a Atomic
105 * transaction.*/
106 Tick
107 Bus::recvAtomic(Packet *pkt)
108 {
109 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
110 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
111 assert(pkt->getDest() == Packet::Broadcast);
112 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
113 }
114
115 /** Function called by the port when the bus is receiving a Functional
116 * transaction.*/
117 void
118 Bus::recvFunctional(Packet *pkt)
119 {
120 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
121 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
122 assert(pkt->getDest() == Packet::Broadcast);
123 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
124 }
125
126 /** Function called by the port when the bus is receiving a status change.*/
127 void
128 Bus::recvStatusChange(Port::Status status, int id)
129 {
130 assert(status == Port::RangeChange &&
131 "The other statuses need to be implemented.");
132
133 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
134
135 assert(id < interfaces.size() && id >= 0);
136 int x;
137 Port *port = interfaces[id];
138 AddrRangeList ranges;
139 AddrRangeList snoops;
140 AddrRangeIter iter;
141 std::vector<DevMap>::iterator portIter;
142
143 // Clean out any previously existent ids
144 for (portIter = portList.begin(); portIter != portList.end(); ) {
145 if (portIter->portId == id)
146 portIter = portList.erase(portIter);
147 else
148 portIter++;
149 }
150
151 port->getPeerAddressRanges(ranges, snoops);
152
153 // not dealing with snooping yet either
154 assert(snoops.size() == 0);
155 for(iter = ranges.begin(); iter != ranges.end(); iter++) {
156 DevMap dm;
157 dm.portId = id;
158 dm.range = *iter;
159
160 DPRINTF(BusAddrRanges, "Adding range %llx - %llx for id %d\n",
161 dm.range.start, dm.range.end, id);
162 portList.push_back(dm);
163 }
164 DPRINTF(MMU, "port list has %d entries\n", portList.size());
165
166 // tell all our peers that our address range has changed.
167 // Don't tell the device that caused this change, it already knows
168 for (x = 0; x < interfaces.size(); x++)
169 if (x != id)
170 interfaces[x]->sendStatusChange(Port::RangeChange);
171 }
172
173 void
174 Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
175 {
176 std::vector<DevMap>::iterator portIter;
177
178 resp.clear();
179 snoop.clear();
180
181 DPRINTF(BusAddrRanges, "received address range request, returning:\n");
182 for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
183 if (portIter->portId != id) {
184 resp.push_back(portIter->range);
185 DPRINTF(BusAddrRanges, " -- %#llX : %#llX\n",
186 portIter->range.start, portIter->range.end);
187 }
188 }
189 }
190
191 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
192
193 Param<int> bus_id;
194
195 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
196
197 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
198 INIT_PARAM(bus_id, "a globally unique bus id")
199 END_INIT_SIM_OBJECT_PARAMS(Bus)
200
201 CREATE_SIM_OBJECT(Bus)
202 {
203 return new Bus(getInstanceName(), bus_id);
204 }
205
206 REGISTER_SIM_OBJECT("Bus", Bus)