Merge zizzer:/bk/newmem
[gem5.git] / src / mem / bridge.cc
1
2 /*
3 * Copyright (c) 2006 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Ali Saidi
30 * Steve Reinhardt
31 */
32
33 /**
34 * @file
35 * Definition of a simple bus bridge without buffering.
36 */
37
38 #include <algorithm>
39
40 #include "base/trace.hh"
41 #include "mem/bridge.hh"
42 #include "sim/builder.hh"
43
44 Bridge::BridgePort::BridgePort(const std::string &_name,
45 Bridge *_bridge, BridgePort *_otherPort,
46 int _delay, int _queueLimit)
47 : Port(_name), bridge(_bridge), otherPort(_otherPort),
48 delay(_delay), outstandingResponses(0),
49 queueLimit(_queueLimit), sendEvent(this)
50 {
51 }
52
53 Bridge::Bridge(const std::string &n, int qsa, int qsb,
54 Tick _delay, int write_ack)
55 : MemObject(n),
56 portA(n + "-portA", this, &portB, _delay, qsa),
57 portB(n + "-portB", this, &portA, _delay, qsa),
58 ackWrites(write_ack)
59 {
60 }
61
62 Port *
63 Bridge::getPort(const std::string &if_name, int idx)
64 {
65 BridgePort *port;
66
67 if (if_name == "side_a")
68 port = &portA;
69 else if (if_name == "side_b")
70 port = &portB;
71 else
72 return NULL;
73
74 if (port->getPeer() != NULL)
75 panic("bridge side %s already connected to.", if_name);
76 return port;
77 }
78
79
80 void
81 Bridge::init()
82 {
83 // Make sure that both sides are connected to.
84 if (portA.getPeer() == NULL || portB.getPeer() == NULL)
85 panic("Both ports of bus bridge are not connected to a bus.\n");
86 }
87
88
89 /** Function called by the port when the bus is receiving a Timing
90 * transaction.*/
91 bool
92 Bridge::BridgePort::recvTiming(PacketPtr pkt)
93 {
94 if (pkt->flags & SNOOP_COMMIT) {
95 DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
96 pkt->getSrc(), pkt->getDest(), pkt->getAddr());
97
98 return otherPort->queueForSendTiming(pkt);
99 }
100 else {
101 // Else it's just a snoop, properly return if we are blocking
102 return !queueFull();
103 }
104 }
105
106
107 bool
108 Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
109 {
110 if (queueFull())
111 return false;
112
113 if (pkt->isResponse()) {
114 // This is a response for a request we forwarded earlier. The
115 // corresponding PacketBuffer should be stored in the packet's
116 // senderState field.
117 PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
118 assert(buf != NULL);
119 // set up new packet dest & senderState based on values saved
120 // from original request
121 buf->fixResponse(pkt);
122 DPRINTF(BusBridge, "restoring sender state: %#X, from packet buffer: %#X\n",
123 pkt->senderState, buf);
124 DPRINTF(BusBridge, " is response, new dest %d\n", pkt->getDest());
125 delete buf;
126 }
127
128 Tick readyTime = curTick + delay;
129 PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
130 DPRINTF(BusBridge, "old sender state: %#X, new sender state: %#X\n",
131 buf->origSenderState, buf);
132
133 // If we're about to put this packet at the head of the queue, we
134 // need to schedule an event to do the transmit. Otherwise there
135 // should already be an event scheduled for sending the head
136 // packet.
137 if (sendQueue.empty()) {
138 sendEvent.schedule(readyTime);
139 }
140
141 sendQueue.push_back(buf);
142
143 return true;
144 }
145
146 void
147 Bridge::BridgePort::trySend()
148 {
149 assert(!sendQueue.empty());
150
151 bool was_full = queueFull();
152
153 PacketBuffer *buf = sendQueue.front();
154
155 assert(buf->ready <= curTick);
156
157 PacketPtr pkt = buf->pkt;
158
159 DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
160 buf->origSrc, pkt->getDest(), pkt->getAddr());
161
162 pkt->flags &= ~SNOOP_COMMIT; //CLear it if it was set
163 if (sendTiming(pkt)) {
164 // send successful
165 sendQueue.pop_front();
166 buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
167
168 if (buf->expectResponse) {
169 // Must wait for response. We just need to count outstanding
170 // responses (in case we want to cap them); PacketBuffer
171 // pointer will be recovered on response.
172 ++outstandingResponses;
173 DPRINTF(BusBridge, " successful: awaiting response (%d)\n",
174 outstandingResponses);
175 } else {
176 // no response expected... deallocate packet buffer now.
177 DPRINTF(BusBridge, " successful: no response expected\n");
178 delete buf;
179 }
180
181 // If there are more packets to send, schedule event to try again.
182 if (!sendQueue.empty()) {
183 buf = sendQueue.front();
184 sendEvent.schedule(std::max(buf->ready, curTick + 1));
185 }
186 // Let things start sending again
187 if (was_full) {
188 DPRINTF(BusBridge, "Queue was full, sending retry\n");
189 otherPort->sendRetry();
190 }
191
192 } else {
193 DPRINTF(BusBridge, " unsuccessful\n");
194 }
195 }
196
197
198 void
199 Bridge::BridgePort::recvRetry()
200 {
201 trySend();
202 }
203
204 /** Function called by the port when the bus is receiving a Atomic
205 * transaction.*/
206 Tick
207 Bridge::BridgePort::recvAtomic(PacketPtr pkt)
208 {
209 return otherPort->sendAtomic(pkt) + delay;
210 }
211
212 /** Function called by the port when the bus is receiving a Functional
213 * transaction.*/
214 void
215 Bridge::BridgePort::recvFunctional(PacketPtr pkt)
216 {
217 std::list<PacketBuffer*>::iterator i;
218 bool pktContinue = true;
219
220 for (i = sendQueue.begin(); i != sendQueue.end(); ++i) {
221 if (pkt->intersect((*i)->pkt)) {
222 pktContinue &= fixPacket(pkt, (*i)->pkt);
223 }
224 }
225
226 if (pktContinue) {
227 otherPort->sendFunctional(pkt);
228 }
229 }
230
231 /** Function called by the port when the bus is receiving a status change.*/
232 void
233 Bridge::BridgePort::recvStatusChange(Port::Status status)
234 {
235 otherPort->sendStatusChange(status);
236 }
237
238 void
239 Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
240 AddrRangeList &snoop)
241 {
242 otherPort->getPeerAddressRanges(resp, snoop);
243 }
244
245 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge)
246
247 Param<int> queue_size_a;
248 Param<int> queue_size_b;
249 Param<Tick> delay;
250 Param<bool> write_ack;
251
252 END_DECLARE_SIM_OBJECT_PARAMS(Bridge)
253
254 BEGIN_INIT_SIM_OBJECT_PARAMS(Bridge)
255
256 INIT_PARAM(queue_size_a, "The size of the queue for data coming into side a"),
257 INIT_PARAM(queue_size_b, "The size of the queue for data coming into side b"),
258 INIT_PARAM(delay, "The miminum delay to cross this bridge"),
259 INIT_PARAM(write_ack, "Acknowledge any writes that are received.")
260
261 END_INIT_SIM_OBJECT_PARAMS(Bridge)
262
263 CREATE_SIM_OBJECT(Bridge)
264 {
265 return new Bridge(getInstanceName(), queue_size_a, queue_size_b, delay,
266 write_ack);
267 }
268
269 REGISTER_SIM_OBJECT("Bridge", Bridge)