mem-cache: Add setters to validate and secure block
[gem5.git] / src / mem / bridge.cc
1 /*
2 * Copyright (c) 2011-2013, 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 * Steve Reinhardt
42 * Andreas Hansson
43 */
44
45 /**
46 * @file
47 * Implementation of a memory-mapped bridge that connects a master
48 * and a slave through a request and response queue.
49 */
50
51 #include "mem/bridge.hh"
52
53 #include "base/trace.hh"
54 #include "debug/Bridge.hh"
55 #include "params/Bridge.hh"
56
57 Bridge::BridgeSlavePort::BridgeSlavePort(const std::string& _name,
58 Bridge& _bridge,
59 BridgeMasterPort& _masterPort,
60 Cycles _delay, int _resp_limit,
61 std::vector<AddrRange> _ranges)
62 : SlavePort(_name, &_bridge), bridge(_bridge), masterPort(_masterPort),
63 delay(_delay), ranges(_ranges.begin(), _ranges.end()),
64 outstandingResponses(0), retryReq(false), respQueueLimit(_resp_limit),
65 sendEvent([this]{ trySendTiming(); }, _name)
66 {
67 }
68
69 Bridge::BridgeMasterPort::BridgeMasterPort(const std::string& _name,
70 Bridge& _bridge,
71 BridgeSlavePort& _slavePort,
72 Cycles _delay, int _req_limit)
73 : MasterPort(_name, &_bridge), bridge(_bridge), slavePort(_slavePort),
74 delay(_delay), reqQueueLimit(_req_limit),
75 sendEvent([this]{ trySendTiming(); }, _name)
76 {
77 }
78
79 Bridge::Bridge(Params *p)
80 : MemObject(p),
81 slavePort(p->name + ".slave", *this, masterPort,
82 ticksToCycles(p->delay), p->resp_size, p->ranges),
83 masterPort(p->name + ".master", *this, slavePort,
84 ticksToCycles(p->delay), p->req_size)
85 {
86 }
87
88 BaseMasterPort&
89 Bridge::getMasterPort(const std::string &if_name, PortID idx)
90 {
91 if (if_name == "master")
92 return masterPort;
93 else
94 // pass it along to our super class
95 return MemObject::getMasterPort(if_name, idx);
96 }
97
98 BaseSlavePort&
99 Bridge::getSlavePort(const std::string &if_name, PortID idx)
100 {
101 if (if_name == "slave")
102 return slavePort;
103 else
104 // pass it along to our super class
105 return MemObject::getSlavePort(if_name, idx);
106 }
107
108 void
109 Bridge::init()
110 {
111 // make sure both sides are connected and have the same block size
112 if (!slavePort.isConnected() || !masterPort.isConnected())
113 fatal("Both ports of a bridge must be connected.\n");
114
115 // notify the master side of our address ranges
116 slavePort.sendRangeChange();
117 }
118
119 bool
120 Bridge::BridgeSlavePort::respQueueFull() const
121 {
122 return outstandingResponses == respQueueLimit;
123 }
124
125 bool
126 Bridge::BridgeMasterPort::reqQueueFull() const
127 {
128 return transmitList.size() == reqQueueLimit;
129 }
130
131 bool
132 Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
133 {
134 // all checks are done when the request is accepted on the slave
135 // side, so we are guaranteed to have space for the response
136 DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
137 pkt->cmdString(), pkt->getAddr());
138
139 DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
140
141 // technically the packet only reaches us after the header delay,
142 // and typically we also need to deserialise any payload (unless
143 // the two sides of the bridge are synchronous)
144 Tick receive_delay = pkt->headerDelay + pkt->payloadDelay;
145 pkt->headerDelay = pkt->payloadDelay = 0;
146
147 slavePort.schedTimingResp(pkt, bridge.clockEdge(delay) +
148 receive_delay);
149
150 return true;
151 }
152
153 bool
154 Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
155 {
156 DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
157 pkt->cmdString(), pkt->getAddr());
158
159 panic_if(pkt->cacheResponding(), "Should not see packets where cache "
160 "is responding");
161
162 // we should not get a new request after committing to retry the
163 // current one, but unfortunately the CPU violates this rule, so
164 // simply ignore it for now
165 if (retryReq)
166 return false;
167
168 DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
169 transmitList.size(), outstandingResponses);
170
171 // if the request queue is full then there is no hope
172 if (masterPort.reqQueueFull()) {
173 DPRINTF(Bridge, "Request queue full\n");
174 retryReq = true;
175 } else {
176 // look at the response queue if we expect to see a response
177 bool expects_response = pkt->needsResponse();
178 if (expects_response) {
179 if (respQueueFull()) {
180 DPRINTF(Bridge, "Response queue full\n");
181 retryReq = true;
182 } else {
183 // ok to send the request with space for the response
184 DPRINTF(Bridge, "Reserving space for response\n");
185 assert(outstandingResponses != respQueueLimit);
186 ++outstandingResponses;
187
188 // no need to set retryReq to false as this is already the
189 // case
190 }
191 }
192
193 if (!retryReq) {
194 // technically the packet only reaches us after the header
195 // delay, and typically we also need to deserialise any
196 // payload (unless the two sides of the bridge are
197 // synchronous)
198 Tick receive_delay = pkt->headerDelay + pkt->payloadDelay;
199 pkt->headerDelay = pkt->payloadDelay = 0;
200
201 masterPort.schedTimingReq(pkt, bridge.clockEdge(delay) +
202 receive_delay);
203 }
204 }
205
206 // remember that we are now stalling a packet and that we have to
207 // tell the sending master to retry once space becomes available,
208 // we make no distinction whether the stalling is due to the
209 // request queue or response queue being full
210 return !retryReq;
211 }
212
213 void
214 Bridge::BridgeSlavePort::retryStalledReq()
215 {
216 if (retryReq) {
217 DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
218 retryReq = false;
219 sendRetryReq();
220 }
221 }
222
223 void
224 Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
225 {
226 // If we're about to put this packet at the head of the queue, we
227 // need to schedule an event to do the transmit. Otherwise there
228 // should already be an event scheduled for sending the head
229 // packet.
230 if (transmitList.empty()) {
231 bridge.schedule(sendEvent, when);
232 }
233
234 assert(transmitList.size() != reqQueueLimit);
235
236 transmitList.emplace_back(pkt, when);
237 }
238
239
240 void
241 Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
242 {
243 // If we're about to put this packet at the head of the queue, we
244 // need to schedule an event to do the transmit. Otherwise there
245 // should already be an event scheduled for sending the head
246 // packet.
247 if (transmitList.empty()) {
248 bridge.schedule(sendEvent, when);
249 }
250
251 transmitList.emplace_back(pkt, when);
252 }
253
254 void
255 Bridge::BridgeMasterPort::trySendTiming()
256 {
257 assert(!transmitList.empty());
258
259 DeferredPacket req = transmitList.front();
260
261 assert(req.tick <= curTick());
262
263 PacketPtr pkt = req.pkt;
264
265 DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
266 pkt->getAddr(), transmitList.size());
267
268 if (sendTimingReq(pkt)) {
269 // send successful
270 transmitList.pop_front();
271 DPRINTF(Bridge, "trySend request successful\n");
272
273 // If there are more packets to send, schedule event to try again.
274 if (!transmitList.empty()) {
275 DeferredPacket next_req = transmitList.front();
276 DPRINTF(Bridge, "Scheduling next send\n");
277 bridge.schedule(sendEvent, std::max(next_req.tick,
278 bridge.clockEdge()));
279 }
280
281 // if we have stalled a request due to a full request queue,
282 // then send a retry at this point, also note that if the
283 // request we stalled was waiting for the response queue
284 // rather than the request queue we might stall it again
285 slavePort.retryStalledReq();
286 }
287
288 // if the send failed, then we try again once we receive a retry,
289 // and therefore there is no need to take any action
290 }
291
292 void
293 Bridge::BridgeSlavePort::trySendTiming()
294 {
295 assert(!transmitList.empty());
296
297 DeferredPacket resp = transmitList.front();
298
299 assert(resp.tick <= curTick());
300
301 PacketPtr pkt = resp.pkt;
302
303 DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
304 pkt->getAddr(), outstandingResponses);
305
306 if (sendTimingResp(pkt)) {
307 // send successful
308 transmitList.pop_front();
309 DPRINTF(Bridge, "trySend response successful\n");
310
311 assert(outstandingResponses != 0);
312 --outstandingResponses;
313
314 // If there are more packets to send, schedule event to try again.
315 if (!transmitList.empty()) {
316 DeferredPacket next_resp = transmitList.front();
317 DPRINTF(Bridge, "Scheduling next send\n");
318 bridge.schedule(sendEvent, std::max(next_resp.tick,
319 bridge.clockEdge()));
320 }
321
322 // if there is space in the request queue and we were stalling
323 // a request, it will definitely be possible to accept it now
324 // since there is guaranteed space in the response queue
325 if (!masterPort.reqQueueFull() && retryReq) {
326 DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
327 retryReq = false;
328 sendRetryReq();
329 }
330 }
331
332 // if the send failed, then we try again once we receive a retry,
333 // and therefore there is no need to take any action
334 }
335
336 void
337 Bridge::BridgeMasterPort::recvReqRetry()
338 {
339 trySendTiming();
340 }
341
342 void
343 Bridge::BridgeSlavePort::recvRespRetry()
344 {
345 trySendTiming();
346 }
347
348 Tick
349 Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
350 {
351 panic_if(pkt->cacheResponding(), "Should not see packets where cache "
352 "is responding");
353
354 return delay * bridge.clockPeriod() + masterPort.sendAtomic(pkt);
355 }
356
357 void
358 Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
359 {
360 pkt->pushLabel(name());
361
362 // check the response queue
363 for (auto i = transmitList.begin(); i != transmitList.end(); ++i) {
364 if (pkt->trySatisfyFunctional((*i).pkt)) {
365 pkt->makeResponse();
366 return;
367 }
368 }
369
370 // also check the master port's request queue
371 if (masterPort.trySatisfyFunctional(pkt)) {
372 return;
373 }
374
375 pkt->popLabel();
376
377 // fall through if pkt still not satisfied
378 masterPort.sendFunctional(pkt);
379 }
380
381 bool
382 Bridge::BridgeMasterPort::trySatisfyFunctional(PacketPtr pkt)
383 {
384 bool found = false;
385 auto i = transmitList.begin();
386
387 while (i != transmitList.end() && !found) {
388 if (pkt->trySatisfyFunctional((*i).pkt)) {
389 pkt->makeResponse();
390 found = true;
391 }
392 ++i;
393 }
394
395 return found;
396 }
397
398 AddrRangeList
399 Bridge::BridgeSlavePort::getAddrRanges() const
400 {
401 return ranges;
402 }
403
404 Bridge *
405 BridgeParams::create()
406 {
407 return new Bridge(this);
408 }